Modus UI

Scratch to Reveal

Web (React)

An interactive canvas component that lets users scratch off an overlay to reveal the content underneath.

A highly optimized HTML5 <canvas> interaction. As the user scrubs their mouse across the container, it visually erases the overlay and calculates the cleared percentage of the bounding box. Once the threshold is crossed, it snaps the rest of the overlay away.

🎉

You Won!

Enjoy your prize.

Installation

CLI

npx @modus-ui/cli add scratch-to-reveal

Manual

  1. Copy the code from the component tab.
  2. Create a file at components/ui/scratch-to-reveal.tsx.
  3. Paste the code.

Usage

tsx
import { ScratchToReveal } from "@/components/ui/scratch-to-reveal";

export default function Example() {
  return (
    <ScratchToReveal width={300} height={300} threshold={0.5}>
      <div className="flex h-full w-full items-center justify-center rounded-xl bg-muted p-8">
        <h2 className="text-xl font-bold">Hidden Treasure</h2>
      </div>
    </ScratchToReveal>
  );
}

Props

PropTypeDefaultDescription
widthnumber-Width of the scratch area in pixels.
heightnumber-Height of the scratch area in pixels.
brushSizenumber40Radius of the scratch brush in pixels.
thresholdnumber0.5Fraction cleared before the rest is revealed.
overlayImagestring-Image drawn on the scratch surface.
overlayColorstring"hsl(var(--primary))"Colour of the scratch surface when there is no image.

Comments

  • No comments yet.