HoverPremium

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.

CanvasScratchCoupon

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

Thank-you code to scratch after payment — a coffee roaster's online shop — Scratch Coupon Reveal example 1

① Thank-you code to scratch after payment — a coffee roaster's online shop

WhenOn the “order confirmed” page, next to the summary: the customer has just paid and gets a discount for next time. The coupon sits in a small card, with its Copy button and the validity period written underneath.
WhyThis is when the customer is in the best mood, with nothing left to do on the page. Scratching gives them a ten-second gesture, and the button lighting up afterwards nudges them to keep the code rather than close the tab.
SettingsCoupon reduced to 230 × 116 px (width and height of .scr-coupon), data-code="CALVENE10", Copy button recoloured, background of .scr-scene removed so the light card shows. In the capture, scratching is replayed by a script that sends pointer events to the coupon.
Three-box ticket for the online raffle — a school fair — Scratch Coupon Reveal example 2

② Three-box ticket for the online raffle — a school fair

WhenA ticket sold for €2 on the fair's website: three small square boxes to scratch one after the other, with the rule written underneath (“three identical amounts”). Each box is a coupon in its own right, with no button.
WhyHere the layer does not hide a code to copy but a result. Three coupons side by side, each with its own layer and its own threshold: the player uncovers the ticket box by box, and the suspense rests on the third.
SettingsThree 104 × 104 px .scr-coupon in one root, with no [data-scr-copy] (the button is optional), data-scr-label="?" instead of the default hint, too long for a box. Night-blue .scr-under background, amount in yellow. Scratching replayed by script in the capture.
Secret dinner venue to scratch on the invitation — a food lovers' club — Scratch Coupon Reveal example 3

③ Secret dinner venue to scratch on the invitation — a food lovers' club

WhenAn invitation sent to members as a link: the date is announced, the venue is hidden under a large scratch area that takes up most of the page. No code, no button: the reveal is the content.
WhyThe club plays on secrecy and the invitation extends it. A large surface needs more strokes before reaching half: the venue appears piece by piece, which suits an announcement meant to last.
SettingsCoupon enlarged to 456 × 232 px, content of .scr-under replaced by three lines of text on a cream background (it is free HTML), default hint kept. Threshold left at 50%. Scratching replayed by script in the capture.

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: none on 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.

Chrome 87+✓ Full (measured on Chromium 149: scratching, hidden coupon, reset, copy)
Firefox 66+✓ Full (measured on Firefox 151)
Safari 14.1+✓ Full in the WebKit 26.5 test build; real Safari not verified
Edge 87+✓ Not verified (same engine as Chrome)
iOS 14.5+✓ Not verified on a real phone (simulated finger: full)
Android Chrome 87+✓ Not verified on a real phone (simulated finger: full)

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):

index.html — structure
<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 / propertyDefaultEffect
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

No, and that is intended. The code is written in the page (.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.
Yes. An element hidden by 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.
Send the 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.