Scratch Coupon Reveal
A grey layer painted on a canvas hides a promo code: rub it with the mouse or a finger, and once half of it is gone it fades away and the Copy button lights up.
Updated
This effect is part of Effect.Labs — 846 vanilla effects, some free, some premium. Hover has 49 effects, including 5 free. Explore the category →
3 usage examples



How it works
The promo code is real text, sitting in .scr-under. On top of it, a <canvas> of the same size holds the scratch layer: setupOverlay() sizes it to its displayed size times the screen density (capped at 2), fills it with a silver-grey gradient and writes the hint “🪙 Scratch here” (its French version on a French page, or the text of data-scr-label). The context then switches to destination-out: anything drawn afterwards removes paint instead of adding it.
While the mouse button or the finger is down (pointerdown then pointermove, with setPointerCapture so the gesture survives leaving the coupon), each event digs a disc with a 20 px radius. The disc only removes 45% of what is left: a quick pass leaves a haze and you have to go over it again to see clearly, as on a real ticket. On release, erasedRatio() reads one pixel in ten and counts the nearly transparent ones; above THRESHOLD = 0.5, reveal() fades the layer in 0.5 s, removes it, and adds .scr-on to the .scr-copy button.
The button copies the value of data-code with navigator.clipboard.writeText, shows “✓ Copied!” for 1.8 s, then takes its label back. If the clipboard is refused (HTTP page, permission), it shows the code in place of its label so it can be typed out.
A coupon stored in a closed dialog or a hidden tab has no size on load: nothing is painted at that point. A ResizeObserver waits for the coupon to get its size and paints the layer just before it is seen. For a new round, send the scr-reset event to the coupon: the layer is repainted and the button dimmed again.
Accessibility
- Screen readers: the code is page text, the layer is only a picture laid over it. A screen reader therefore reads the code without scratching anything, and the Copy button is offered to it from the start.
- Keyboard: rubbing cannot be done from the keyboard. The equivalent goes through the button: Tab reaches it even while it is dimmed, Enter or Space removes the layer and then copies the code. Without a Copy button in your HTML there is no keyboard route to the reveal: keep it, or add your own.
- Touch:
touch-action: noneon the canvas keeps the gesture for scratching, so the page does not scroll while rubbing. One consequence to know: a coupon filling a whole phone screen leaves no room to scroll with the thumb. - Reduced motion: with
prefers-reduced-motion: reduce, the layer is not shown at all. The code is readable straight away and the button is active, with no gesture needed. - Contrast: the hint is written in 45% black on the grey of the layer, a weak contrast. It is a clue, not an instruction: write the instruction in plain text next to the coupon, as the three examples do.
Browser compatibility
Three building blocks set the floor: the CSS inset property that stretches the layer over the coupon (Chrome 87, Firefox 66, Safari 14.1), Pointer Events with setPointerCapture (Chrome 55, Firefox 59, Safari 13) and navigator.clipboard.writeText for the button (Chrome 66, Firefox 63, Safari 13.1, HTTPS page). ResizeObserver (Chrome 64, Firefox 69, Safari 13.1) is only used for coupons hidden on load; without it, the layer is painted on first contact. Versions taken from @mdn/browser-compat-data.
Without JavaScript, no layer is painted: the code can be read directly, and the Copy button stays dimmed and inactive. In a browser without inset, the green background of the coupon no longer fills its frame. Without a clipboard, the button shows the code instead of copying it.
The code
HTML structure to paste into your page (the CSS + JS come with your purchase):
<div class="fx-0810-root">
<div class="scr-scene">
<div>
<div class="scr-coupon" data-scr="" data-code="…">
<div class="scr-under">
<span class="scr-off">…</span>
<span class="scr-code">…</span>
<span class="scr-off">…</span>
</div>
<canvas class="scr-canvas" data-scr-canvas="" width="0" height="0"></canvas>
</div>
<button type="button" class="scr-copy" data-scr-copy="">…</button>
</div>
</div>
</div>
One-time payment, no subscription — the code is yours forever
Customize
Options passed to the API or data-* attributes:
| Option / property | Default | Effect |
|---|---|---|
Copied code — data-code on [data-scr] |
CYBER20 | This is the value sent to the clipboard. The code on display is the text of .scr-code: change both, the script does not copy one into the other. |
Reveal threshold — THRESHOLD at the top of the JS |
0.5 | Share of the layer to erase before it leaves by itself. 0.3 for a small coupon that should open fast, 0.7 to make a large surface last. |
Brush size — ctx.arc(x, y, 20, …) in scratch() |
20 px radius | 30 to 40 px for faster finger scratching, 12 px for a fine line. The strength of each pass (45%) comes from the last fillStyle of setupOverlay(), the one that also writes the hint: add ctx.fillStyle = '#000' after the fillText to erase in one go. |
Hint on the layer — data-scr-label on [data-scr] |
“🪙 Scratch here” (a French version is used on French pages) | Replaces the text written on the grey. The language is read from data-lang, then from the closest lang attribute. The 🪙 token is a recent emoji: on an old system it may show as an empty box, so use your own text there. |
Coupon size — width and height of .scr-coupon |
300 × 150 px | The layer takes the size of the coupon when it is painted. If the coupon is resized later, the layer is stretched and the stroke stays under the pointer. |
What lies under the layer — content of .scr-under |
Label, code, benefit on a dark green background | Free HTML: an amount, a venue, a picture. The background (#064e3b to #022c22) and the colours of .scr-off and .scr-code are changed in CSS. |
Copy button — [data-scr-copy], sibling of the coupon |
Present, dimmed until the reveal | Optional: remove it when there is nothing to copy. Its label is yours and is restored after “✓ Copied!”. |
New round — scr-reset event |
No automatic reset | coupon.dispatchEvent(new CustomEvent('scr-reset')) on the [data-scr] element repaints the layer and dims the button again. No effect if the visitor asked for less motion. |
FAQ
.scr-code and data-code): a screen reader reads it, “View source” shows it, and the Copy button works from the keyboard before any scratching. The layer is a game, not a safe. For a single-use code, only send it to the page once your server has validated the win.display: none has no size, so nothing is painted on load; the script watches the coupon with a ResizeObserver and paints the layer as soon as the dialog opens, before it is displayed. If you add the coupon to the page afterwards, run the script again: coupons already in place carry data-scr-ready and are not wired a second time.scr-reset event to the coupon: document.querySelector('[data-scr]').dispatchEvent(new CustomEvent('scr-reset')). The layer is repainted, the “revealed” state is forgotten and the Copy button turns grey again. Each coupon resets separately; nothing is added to window.