Scratch & Save

A promo code under a foil scratch card. Drag across the foil, the flakes fly off, and when most of it is gone the rest dissolves and the Copy code button appears.

Drag across the silver foil. The demo scratches the card by itself until you touch it.

Download

What you can change

Change a number, save, and reload the page to see the difference.

DISCOUNT30

The number on the ticket. The code becomes SAVE30 with it, so the two can never drift apart.

OPEN0.55

How much foil has to go before the rest dissolves on its own.

BRUSH13

Radius of the scratch brush in canvas pixels.

STIFF0.06

How hard the spring pulls a flake to where it lands. Higher is faster.

DAMP0.88

How quickly a flake settles. Closer to 1 floats longer.

Add it to your site

Promo and discount pages, a thank you page after checkout, an email landing page, a giveaway.

  1. CSS

    Paste into your stylesheet, or inside a <style> tag in <head>.

    /* Scratch & Save by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
    :root {
      --paper: #EDE6D6;
      --paper-shadow: #C9BFA8;
      --foil-base: #8A93A6;
      --foil-sheen: #E8ECF2;
      --ink: #241C10;
      --accent-gold: #C89B3C;
      --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='120' height='120'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='2'/%3E%3C/filter%3E%3Crect width='120' height='120' filter='url(%23n)' opacity='.5'/%3E%3C/svg%3E");
    }
    .promo { position: relative; width: 340px; }
    
    /* the ticket: warm paper, a torn-off edge on both sides */
    .ticket { position: relative; box-sizing: border-box; width: 100%; padding: 20px 22px 16px; border-radius: 16px;
      background: var(--paper); color: var(--ink);
      box-shadow: 0 22px 44px -26px rgba(0,0,0,.85), inset 0 -2px 0 var(--paper-shadow), inset 0 0 0 1px rgba(36,28,16,.07);
      mask: radial-gradient(circle 13px at 0 62%, transparent 13px, #000 13.5px),
            radial-gradient(circle 13px at 100% 62%, transparent 13px, #000 13.5px);
      -webkit-mask-composite: source-in; mask-composite: intersect; }
    .ticket::before { content: ""; position: absolute; inset: 0; pointer-events: none;
      background: var(--grain) 0 0 / 120px 120px; opacity: .22; mix-blend-mode: multiply; }
    
    .top { display: flex; align-items: baseline; justify-content: space-between; margin: 0 0 12px; }
    .off { font-size: 19px; font-weight: 700; letter-spacing: -.01em; }
    .pct { font: 500 13px Menlo, monospace; color: rgba(36,28,16,.42); font-variant-numeric: tabular-nums; }
    
    /* the code sits on the paper, the foil canvas lies on top of it */
    .panel { position: relative; height: 86px; border-radius: 10px; overflow: hidden; background: #E4DBC7;
      box-shadow: inset 0 1px 2px rgba(36,28,16,.18), inset 0 0 0 1px rgba(36,28,16,.06); }
    .code { position: absolute; inset: 0; display: grid; place-items: center; font: 700 40px/1 Menlo, monospace;
      letter-spacing: 2px; color: var(--accent-gold); text-shadow: 0 1px 0 rgba(255,255,255,.4); }
    .foil { position: absolute; inset: 0; width: 100%; height: 100%; cursor: crosshair; touch-action: none;
      transition: opacity .45s ease-out, transform .45s ease-out; }
    /* what is left of the foil dissolves in place, it does not fly off */
    .foil.gone { opacity: 0; transform: scale(1.02); pointer-events: none; }
    /* one sheen runs across the foil the moment the first letters show */
    .panel::after { content: ""; position: absolute; inset: -30% -45%; pointer-events: none; opacity: 0;
      background: linear-gradient(74deg, transparent 40%, rgba(232,236,242,.8) 50%, transparent 60%); }
    .panel.sheen::after { animation: sheen .7s cubic-bezier(.77, 0, .175, 1); }
    @keyframes sheen { from { opacity: .95; transform: translateX(-70%); } to { opacity: 0; transform: translateX(70%); } }
    
    .copy { display: block; width: 100%; height: 44px; margin: 14px 0 0; padding: 0; border: 0; border-radius: 10px;
      background: var(--ink); color: var(--paper); font: 600 15px system-ui, sans-serif; cursor: pointer;
      position: relative; overflow: hidden; opacity: 0; transform: scale(.95);
      transition: opacity .2s cubic-bezier(.23, 1, .32, 1), transform .2s cubic-bezier(.23, 1, .32, 1); }
    .copy.ready { opacity: 1; transform: none; }
    .copy:active { transform: scale(.97); transition: transform .12s ease-out; }
    .copy:focus-visible { outline: 2px solid var(--accent-gold); outline-offset: 2px; }
    .copy span { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 7px;
      transition: opacity .2s ease-out, transform .2s cubic-bezier(.23, 1, .32, 1); }
    .copy .ok { opacity: 0; transform: translateY(6px); }
    .copy.done .go { opacity: 0; transform: translateY(-6px); }
    .copy.done .ok { opacity: 1; transform: none; }
    .copy .ok svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
    
    .fine { margin: 10px 0 0; font-size: 12px; color: rgba(36,28,16,.42); text-align: center; }
    
    .flake, .bit { position: absolute; pointer-events: none; }
    .flake { width: 6px; height: 5px; border-radius: 1px; background: var(--foil-base); }
    .bit { width: 7px; height: 7px; border-radius: 1px; background: var(--accent-gold); }
    
    @media (prefers-reduced-motion: reduce) {
      .panel.sheen::after { animation: none; }
      .copy, .copy span { transition-property: opacity; }
      .copy.ready { transform: none; }
    }
  2. HTML

    Put it where the effect should appear on your page.

    <div class="promo">
      <div class="ticket">
        <p class="top"><span class="off">30% off</span><span class="pct">0%</span></p>
        <div class="panel">
          <span class="code">SAVE30</span>
          <canvas class="foil" width="680" height="172" aria-label="Scratch the foil to reveal the code" role="img"></canvas>
        </div>
        <button class="copy" type="button">
          <span class="go">Copy code</span>
          <span class="ok"><svg viewBox="0 0 24 24"><path d="M5 12.5l4.5 4.5L19 7.5"/></svg>Copied</span>
        </button>
        <p class="fine">One code per order</p>
      </div>
    </div>
  3. JavaScript

    Paste right before the closing </body> tag, inside <script></script>.

    /* Scratch & Save by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
    // the number on the ticket, the code and the line above it all come from here
    const DISCOUNT = 30, CODE = 'SAVE' + DISCOUNT;
    const OPEN = 0.55, BRUSH = 13;
    // how the foil flakes fly and settle
    const STIFF = 0.06, DAMP = 0.88;
    
    const $ = s => document.querySelector(s);
    const promo = $('.promo'), panel = $('.panel'), foil = $('.foil');
    const btn = $('.copy'), pct = $('.pct');
    const ctx = foil.getContext('2d', { willReadFrequently: true });
    const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
    const wait = ms => new Promise(r => setTimeout(r, ms));
    $('.code').textContent = CODE;
    $('.off').textContent = DISCOUNT + '% off';
    
    // brushed foil: a steel base, a soft cross light and a lot of fine grain
    function paintFoil() {
      const w = foil.width, h = foil.height;
      ctx.globalCompositeOperation = 'source-over';
      ctx.clearRect(0, 0, w, h);
      const base = ctx.createLinearGradient(0, 0, w, h);
      base.addColorStop(0, '#9AA2B4'); base.addColorStop(.42, '#8A93A6'); base.addColorStop(.62, '#98A0B2'); base.addColorStop(1, '#7C8497');
      ctx.fillStyle = base; ctx.fillRect(0, 0, w, h);
      const sheen = ctx.createLinearGradient(w * .1, h, w * .7, 0);
      sheen.addColorStop(0, 'rgba(232,236,242,0)'); sheen.addColorStop(.5, 'rgba(232,236,242,.34)'); sheen.addColorStop(1, 'rgba(232,236,242,0)');
      ctx.fillStyle = sheen; ctx.fillRect(0, 0, w, h);
      for (let i = 0; i < 1800; i++) {
        const x = Math.random() * w, y = Math.random() * h, l = 2 + Math.random() * 7;
        ctx.strokeStyle = Math.random() < .5 ? 'rgba(255,255,255,.16)' : 'rgba(40,46,58,.17)';
        ctx.lineWidth = 1;
        ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + l, y - l * .35); ctx.stroke();
      }
    }
    
    // a few stretched dabs at different angles, never
    // one clean circle, so the edge stays ragged
    function scratch(x, y) {
      ctx.globalCompositeOperation = 'destination-out';
      ctx.fillStyle = '#000';
      for (let i = 0; i < 3; i++) {
        const a = Math.random() * 6.28, d = Math.random() * 9;
        const r = BRUSH * (.45 + Math.random());
        const dx = x + Math.cos(a) * d;
        const dy = y + Math.sin(a) * d;
        const flat = r * (.5 + Math.random() * .6);
        ctx.beginPath();
        ctx.ellipse(dx, dy, r, flat, a, 0, 7);
        ctx.fill();
      }
      flake(x, y);
    }
    
    let open = false, checks = 0;
    function revealed() {
      const d = ctx.getImageData(0, 0, foil.width, foil.height).data;
      let gone = 0, all = 0;
      for (let i = 3; i < d.length; i += 4 * 16) { all++; if (d[i] < 24) gone++; }
      return gone / all;
    }
    function measure() {
      const r = revealed();
      pct.textContent = Math.round(r * 100) + '%';
      if (r > .18 && !reduce) panel.classList.add('sheen');
      if (r >= OPEN) peel();
    }
    
    const flakes = [];
    function flake(x, y) {
      if (reduce || flakes.length > 40 || Math.random() < .55) return;
      const el = document.createElement('i'); el.className = 'flake';
      el.style.left = panel.offsetLeft + x / 2 + 'px';
      el.style.top = panel.offsetTop + y / 2 + 'px';
      promo.append(el);
      const f = { el, x: 0, y: 0, vx: (Math.random() - .5) * 6, vy: -2 - Math.random() * 3,
        r: Math.random() * 360, dr: (Math.random() - .5) * 22, gx: (Math.random() - .5) * 70, gy: 70 + Math.random() * 50 };
      flakes.push(f);
      el.animate([{ opacity: .9 }, { opacity: 0 }], { duration: 760, easing: 'ease-in', fill: 'forwards' })
        .onfinish = () => { el.remove(); flakes.splice(flakes.indexOf(f), 1); };
    }
    
    // one spring pulls every flake toward where it lands
    (function fly() {
      for (const f of flakes) {
        f.vx = (f.vx + (f.gx - f.x) * STIFF) * DAMP;
        f.vy = (f.vy + (f.gy - f.y) * STIFF) * DAMP;
        f.x += f.vx; f.y += f.vy; f.r += f.dr;
        const move = `translate(${f.x}px, ${f.y}px)`;
        f.el.style.transform = `${move} rotate(${f.r}deg)`;
      }
      requestAnimationFrame(fly);
    })();
    
    function peel() {
      if (open) return;
      open = true;
      foil.classList.add('gone');
      pct.textContent = '100%';
      // the chips still in the air go with it, nothing keeps falling once the code is out
      for (const f of flakes.slice()) {
        f.el.getAnimations().forEach(a => a.cancel());
        f.el.animate([{ opacity: .7 }, { opacity: 0 }], { duration: 220, easing: 'ease-out', fill: 'forwards' })
          .onfinish = () => { f.el.remove(); flakes.splice(flakes.indexOf(f), 1); };
      }
      setTimeout(() => btn.classList.add('ready'), 120);
    }
    
    function burst() {
      const r = panel.getBoundingClientRect(), p = promo.getBoundingClientRect();
      for (let i = 0; i < 18; i++) {
        const el = document.createElement('i'); el.className = 'bit';
        el.style.left = r.left - p.left + r.width * (.2 + .6 * Math.random()) + 'px';
        el.style.top = r.top - p.top + r.height + 'px';
        el.style.background = i % 3 ? 'var(--accent-gold)' : 'var(--foil-base)';
        promo.append(el);
        const dx = (Math.random() - .5) * 220, dy = -60 - Math.random() * 90;
        el.animate([
          { transform: 'translate(0, 0) rotate(0deg)', opacity: 1 },
          { transform: `translate(${dx}px, ${dy}px) rotate(${(Math.random() - .5) * 400}deg)`, opacity: 0 },
        ], { duration: 900 + Math.random() * 500, delay: i * 40, easing: 'cubic-bezier(.23, 1, .32, 1)', fill: 'backwards' })
          .onfinish = () => el.remove();
      }
    }
    
    async function copyCode() {
      if (!open) return;
      try { await navigator.clipboard.writeText(CODE); } catch (e) { /* clipboard is blocked on file:// in some browsers */ }
      btn.classList.add('done');
      if (!reduce) burst();
    }
    btn.addEventListener('click', copyCode);
    
    const spot = e => {
      const r = foil.getBoundingClientRect();
      return [(e.clientX - r.left) * foil.width / r.width, (e.clientY - r.top) * foil.height / r.height];
    };
    foil.addEventListener('pointerdown', e => {
      foil.setPointerCapture(e.pointerId);
      scratch(...spot(e)); measure();
    });
    foil.addEventListener('pointermove', e => {
      if (e.buttons !== 1 || open) return;
      scratch(...spot(e));
      if (++checks % 5 === 0) measure();
    });
    foil.addEventListener('pointerup', measure);
    
    function reset() {
      open = false; checks = 0;
      for (const el of promo.querySelectorAll('.flake, .bit')) el.remove();
      flakes.length = 0;
      paintFoil();
      foil.classList.remove('gone');
      panel.classList.remove('sheen');
      btn.classList.remove('ready', 'done');
      pct.textContent = '0%';
    }
    
    // demo: a hand crosses the foil once, and it stops as soon as you touch anything
    let demo = true;
    const stopDemo = () => { if (!demo) return; demo = false; reset(); };
    addEventListener('pointerdown', stopDemo, true);
    addEventListener('keydown', stopDemo, true);
    
    // a hand works the coin in a few crossing strokes: one long sweep, one back across it,
    // then shorter ones that pick off what the first two left
    const STROKES = [
      { a: [.04, .74], b: [.97, .28], n: 34, ms: 17 },
      { a: [.93, .76], b: [.16, .34], n: 30, ms: 20 },
      { a: [.12, .26], b: [.58, .88], n: 22, ms: 24 },
      { a: [.60, .84], b: [.96, .52], n: 16, ms: 28 },
    ];
    const lerp = (a, b, k) => a + (b - a) * k;
    
    async function play() {
      reset();
      const w = foil.width, h = foil.height;
      await wait(260); if (!demo) return;
      for (const st of STROKES) {
        for (let s = 0; s <= st.n; s++) {
          if (!demo || open) break;
          const u = s / st.n, bow = Math.sin(u * Math.PI) * 7;
          const x = lerp(st.a[0], st.b[0], u) * w + (Math.random() - .5) * 8;
          const y = lerp(st.a[1], st.b[1], u) * h + bow + (Math.random() - .5) * 8;
          scratch(x, y);
          if (s % 5 === 0) measure();
          await wait(st.ms);
        }
        measure();
        if (!demo || open) break;
        await wait(110);
      }
      if (!demo) return;
      peel();
      await wait(700); if (!demo) return;
      btn.click();
      await wait(3200); if (!demo) return;
      reset();
      await wait(900); if (demo) play();
    }
    paintFoil();
    play();

Connect it to your backend

The code lives in DISCOUNT and CODE. To hand out a personal code, put the value from your server into CODE before the ticket is drawn, and check it again on the server when the order is placed.

Good to know

Tilda, Webflow, WordPress

Press Copy all code at the top and paste it into one HTML block.

Tilda

Add block, Other, T123 HTML code. Paste and save. Check the published page, the editor preview does not run scripts.

Webflow

Add panel, Embed. Paste, then Save & Close. Custom code runs on the published site.

WordPress

Add block, Custom HTML. Paste and press Update.

More in Cart & checkout

See all
Copied