ScratchCard
Neobrutalist-style scratch card component that uses HTML5 Canvas overlay on top of the content, drawing a mottled stripe layer. After the user erases a certain area with mouse/finger, the Canvas fades out and is destroyed, fully revealing the Vue content in the slot.
Demo
🎉 恭喜中奖!
自定义底色
大笔刷 (brushRadius=40)
大笔刷
渐隐时长 (fadeDuration=1000ms)
慢速渐隐
手动揭示 (revealAll)
一键揭示
Installation
pnpm dlx brutx-vue@latest add scratch-cardUsage
<script setup>
import { ScratchCard } from 'brutx-ui-vue'
</script>
<template>
<ScratchCard class="w-full max-w-sm h-48">
<div class="flex items-center justify-center h-full bg-brutal-accent">
<p class="text-2xl font-black">🎉 Congratulations!</p>
</div>
</ScratchCard>
</template>Custom Overlay
By default, a Neobrutalist two-color stripe pattern is drawn (--brutal-primary + --brutal-secondary). You can set a solid color overlay via the overlayColor prop:
<ScratchCard overlay-color="#FFE66D">
<p>Content underneath</p>
</ScratchCard>Programmatic Control
<script setup>
import { ref } from 'vue'
import { ScratchCard } from 'brutx-ui-vue'
const scratchCardRef = ref()
</script>
<template>
<ScratchCard ref="scratchCardRef" />
<button @click="scratchCardRef?.revealAll()">Reveal All</button>
</template>API Reference
| Name | Type | Default | Description |
|---|---|---|---|
percentage | number | undefined | 50 | Automatically completes the reveal when this percentage of the covered area has been scratched; the default threshold is 50. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | |||
brushRadius | number | undefined | 20 | Sets the radius of the pointer scratch brush in pixels. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | |||
overlayColor | string | undefined | undefined | Sets a solid background color for the Canvas overlay; when omitted, a two-color theme stripe pattern is used. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | |||
fadeDuration | number | undefined | 300 | The duration of the overlay fade-out after the completion threshold is reached, in milliseconds; reduced-motion preferences skip the animation. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | |||
class | string | undefined | undefined | CSS classes added to the scratch-card root container. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | |||
| Name | Signature or type | Description |
|---|---|---|
@progress | [percent: number] | Emits the current revealed-area percentage as scratching progresses; emissions are throttled. |
DetailsDeclared type[percent: number] Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | ||
@completed | [] | Emitted when the overlay is removed after reaching the reveal threshold or calling revealAll. |
DetailsDeclared type[] Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | ||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | The underlying scratch-card content covered by the Canvas overlay and revealed as it is scratched. |
DetailsDeclared type{}Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | ||
| Name | Signature or type | Description |
|---|---|---|
isRevealed | boolean | The reveal state exposed on the component instance. Vue automatically unwraps its internal Ref, so read the boolean through scratchCardRef.value.isRevealed. |
DetailsDeclared typeboolean Literal values
Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | ||
revealAll | () => void | Immediately reveals all content and starts removing the overlay. |
DetailsDeclared type() => void Sourcepackages/ui/src/components/scratch-card/ScratchCard.vue | ||
Accessibility
- Keyboard control: The component sets
tabindex="0"; pressingEnterorSpaceautomatically reveals all content - ARIA attributes: The component sets
role="region", andaria-labeldefaults to the localized textscratchCard.ariaLabel(default: "Scratch Card") - Motion reduction: When the user prefers
prefers-reduced-motion: reduce, the fade-out animation is skipped and the Canvas is removed immediately - Interaction hint: The Canvas overlay uses
cursor-crosshaircursor to indicate interactivity