Прожги и получи скидку
Скидка, спрятанная под листком старой бумаги, открывается спичкой. Пламя обугливает лист, поднимаются искры, а после порога лист вспыхивает целиком и уходит пеплом, открывая приз и код.
- Корзина и оплата
- Бесплатно
- canvas
- particles
- fire
- Добавлен 23 сентября 2026
<style>
/* Burn & Save by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
:root {
--paper: #EDE6D6;
--paper-shadow: #C9BFA8;
--cover: #C3B18C;
--cover-edge: #B69F72;
--ink: #241C10;
--ink-red: #98351F;
--type: Rockwell, Playbill, Clarendon, 'American Typewriter', Georgia, serif;
--type-small: Copperplate, 'Copperplate Gothic Bold', 'Big Caslon', Georgia, serif;
--ember: #FF7A18;
--ember-core: #FFF3D0;
--char: #3A2415;
--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; }
.hint { font: 700 15px/1 var(--type-small); letter-spacing: .1em; text-transform: uppercase; color: rgba(36,28,16,.55); }
.pct { font: 500 13px Menlo, monospace; color: rgba(36,28,16,.4); font-variant-numeric: tabular-nums; }
/* the code sits on the paper, the paper canvas lies on top of it */
.panel { position: relative; height: 86px; border-radius: 10px; overflow: hidden; background: #DCD1B8;
box-shadow: inset 0 1px 2px rgba(36,28,16,.18), inset 0 0 0 1px rgba(36,28,16,.06); }
/* under the paper: the prize first, the code under it, both pressed into the sheet */
.win { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 3px; }
.prize { font: 700 40px/1 var(--type); letter-spacing: 2px; color: var(--ink-red);
text-shadow: 0 1px 0 rgba(255,255,255,.45), 0 -1px 0 rgba(60,20,10,.25);
transition: text-shadow .5s ease-out; }
.code { font: 700 17px/1 var(--type-small); letter-spacing: .22em; color: rgba(36,28,16,.6); }
.win.hot .prize { text-shadow: 0 0 22px rgba(255,122,24,.8), 0 1px 0 rgba(255,255,255,.45); }
.cover { position: absolute; inset: 0; width: 100%; height: 100%; cursor: none; touch-action: none;
transition: opacity .3s ease-out; }
.cover.gone { opacity: 0; pointer-events: none; }
/* glow, sparks and smoke live on their own canvas above the whole ticket */
/* canvas is a replaced element: without an explicit height it stays 150px tall */
.heat { position: absolute; left: -6px; top: -56px; width: calc(100% + 12px); height: calc(100% + 56px); pointer-events: none; }
.copy { display: block; width: 100%; height: 44px; margin: 14px 0 0; padding: 0; border: 0; border-radius: 8px;
background: var(--ink); color: var(--paper); font: 700 15px/1 var(--type-small); letter-spacing: .08em; text-transform: uppercase; 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(--ember); 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: 400 12px/1.3 var(--type-small); letter-spacing: .08em; text-transform: uppercase;
color: rgba(36,28,16,.45); text-align: center; }
.bit { position: absolute; width: 4px; height: 4px; border-radius: 50%; pointer-events: none;
background: radial-gradient(circle, var(--ember-core), var(--ember) 45%, rgba(255,90,10,0) 75%); }
@media (prefers-reduced-motion: reduce) {
.copy, .copy span { transition-property: opacity; }
.copy.ready { transform: none; }
}
</style>
<div class="promo">
<div class="ticket">
<p class="top"><span class="hint">Burn to reveal</span><span class="pct">0%</span></p>
<div class="panel">
<span class="win"><span class="prize">30% OFF</span><span class="code">SAVE30</span></span>
<canvas class="cover" width="680" height="172" role="img" aria-label="Burn the paper to reveal the code"></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>
<canvas class="heat" aria-hidden="true"></canvas>
</div>
<script>
/* Burn & 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.42, BURN = 15;
// how long the edge glows before it cools to char, in s
const HEAT = 0.38;
const $ = s => document.querySelector(s);
const promo = $('.promo'), panel = $('.panel'), cover = $('.cover');
const heat = $('.heat'), win = $('.win');
const btn = $('.copy'), pct = $('.pct');
const fx = cover.getContext('2d', { willReadFrequently: true });
const hx = heat.getContext('2d');
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
const wait = ms => new Promise(r => setTimeout(r, ms));
const lerp = (a, b, k) => a + (b - a) * k;
$('.prize').textContent = DISCOUNT + '% OFF';
$('.code').textContent = CODE;
function sizeHeat() {
const r = heat.getBoundingClientRect();
heat.width = Math.round(r.width * 2);
heat.height = Math.round(r.height * 2);
}
addEventListener('resize', sizeHeat);
// the cover is a blank slip of very old paper: tan base, foxing, fibres,
// a fold and edges darkened by handling
function paintCover() {
const w = cover.width, h = cover.height;
fx.globalCompositeOperation = 'source-over';
fx.clearRect(0, 0, w, h);
const base = fx.createLinearGradient(0, 0, w, h);
base.addColorStop(0, '#D3C29C'); base.addColorStop(.42, '#C3B18C'); base.addColorStop(1, '#AD9A76');
fx.fillStyle = base; fx.fillRect(0, 0, w, h);
for (let i = 0; i < 150; i++) { // foxing: the brown blooms old paper gets
const x = Math.random() * w, y = Math.random() * h, r = 6 + Math.random() * 44;
const spot = fx.createRadialGradient(x, y, 0, x, y, r);
const dark = Math.random() < .62;
spot.addColorStop(0, dark ? 'rgba(126,88,40,.24)' : 'rgba(250,238,208,.2)');
spot.addColorStop(1, 'rgba(0,0,0,0)');
fx.fillStyle = spot; fx.fillRect(x - r, y - r, r * 2, r * 2);
}
for (let i = 0; i < 2200; i++) { // fibres pressed into the sheet
const x = Math.random() * w, y = Math.random() * h, l = 3 + Math.random() * 12, a = (Math.random() - .5) * .7;
fx.strokeStyle = Math.random() < .5 ? 'rgba(96,70,34,.14)' : 'rgba(252,242,216,.18)';
fx.lineWidth = 1;
fx.beginPath(); fx.moveTo(x, y); fx.lineTo(x + Math.cos(a) * l, y + Math.sin(a) * l); fx.stroke();
}
for (let i = 0; i < 5; i++) { // old creases
const y = h * (.18 + Math.random() * .64);
const cr = fx.createLinearGradient(0, y - 5, 0, y + 5);
cr.addColorStop(0, 'rgba(84,60,28,0)'); cr.addColorStop(.5, 'rgba(84,60,28,.13)'); cr.addColorStop(1, 'rgba(84,60,28,0)');
fx.fillStyle = cr; fx.fillRect(0, y - 5, w, 10);
}
const fold = fx.createLinearGradient(w * .5 - 18, 0, w * .5 + 18, 0);
fold.addColorStop(0, 'rgba(70,50,24,0)'); fold.addColorStop(.5, 'rgba(70,50,24,.16)'); fold.addColorStop(1, 'rgba(70,50,24,0)');
fx.fillStyle = fold; fx.fillRect(0, 0, w, h);
const edge = fx.createLinearGradient(0, 0, 0, h);
edge.addColorStop(0, 'rgba(64,44,20,.36)'); edge.addColorStop(.16, 'rgba(64,44,20,0)');
edge.addColorStop(.84, 'rgba(64,44,20,0)'); edge.addColorStop(1, 'rgba(64,44,20,.4)');
fx.fillStyle = edge; fx.fillRect(0, 0, w, h);
const side = fx.createLinearGradient(0, 0, w, 0);
side.addColorStop(0, 'rgba(64,44,20,.36)'); side.addColorStop(.1, 'rgba(64,44,20,0)');
side.addColorStop(.9, 'rgba(64,44,20,0)'); side.addColorStop(1, 'rgba(64,44,20,.36)');
fx.fillStyle = side; fx.fillRect(0, 0, w, h);
}
// the flame chars the paper along its path and eats through
// the middle of that band. source-atop keeps the char on
// the paper that is still there, never inside a hole.
let last = null;
function burn(x, y) {
const dx0 = last ? x - last[0] : 999;
const dy0 = last ? y - last[1] : 999;
const from = Math.hypot(dx0, dy0) < 90 ? last : [x, y];
last = [x, y];
const line = (w, style, mode) => {
fx.globalCompositeOperation = mode;
fx.strokeStyle = style; fx.lineWidth = w;
fx.lineCap = fx.lineJoin = 'round';
fx.beginPath();
fx.moveTo(from[0], from[1]); fx.lineTo(x, y);
fx.stroke();
};
line(BURN * 3.2, 'rgba(104,60,26,.42)', 'source-atop');
line(BURN * 2.3, 'rgba(38,20,9,.86)', 'source-atop');
line(BURN * 1.5 * (.9 + Math.random() * .2), '#000',
'destination-out');
fx.fillStyle = '#000';
for (let i = 0; i < 2; i++) { // bites at the edge
const a = Math.random() * 6.28;
const d = BURN * (.7 + Math.random() * .45);
const r = BURN * (.1 + Math.random() * .16);
const px = x + Math.cos(a) * d;
const py = y + Math.sin(a) * d;
fx.beginPath(); fx.arc(px, py, r, 0, 7); fx.fill();
}
}
// the live edge: every point cools on its own clock, white to orange to nothing
const edge = [], sparks = [], smoke = [], ash = [];
function mark(px, py) {
edge.push({ x: px, y: py, t: performance.now() });
if (reduce) return;
if (Math.random() < .6) sparks.push({
x: px, y: py, life: 1,
vx: (Math.random() - .5) * .9,
vy: -.7 - Math.random() * 1.1,
r: .8 + Math.random() * 1.4,
});
if (Math.random() < .07) smoke.push({
x: px, y: py, life: 1,
vx: (Math.random() - .5) * .3,
vy: -.5 - Math.random() * .3,
r: 4 + Math.random() * 4,
});
}
// a flame is a wavering teardrop: dark at the base, bright inside, torn at the tip
function flame(x, y, h, w, lean, seed, time, alpha) {
const s1 = Math.sin(time * 6.1 + seed), s2 = Math.sin(time * 9.7 + seed * 1.7);
const s3 = Math.sin(time * 3.3 + seed * .6), s4 = Math.sin(time * 13.3 + seed * 2.3);
const tipX = x + lean * h * .3 + (s1 * .12 + s2 * .06) * w * 2.4;
const tipY = y - h * (1 + s3 * .1);
const bw = w * (1 + s2 * .07);
hx.beginPath();
hx.moveTo(x - bw * .5, y);
hx.bezierCurveTo(x - bw * .66, y - h * .45, tipX - bw * .32 + s2 * 2, tipY + h * .32, tipX, tipY);
hx.bezierCurveTo(tipX + bw * .32 - s1 * 2, tipY + h * .32, x + bw * .66, y - h * .45, x + bw * .5, y);
hx.closePath();
const gr = hx.createLinearGradient(x, y, tipX, tipY);
gr.addColorStop(0, `rgba(190,70,10,${.16 * alpha})`);
gr.addColorStop(.2, `rgba(255,140,30,${.6 * alpha})`);
gr.addColorStop(.5, `rgba(255,200,80,${.88 * alpha})`);
gr.addColorStop(.78, `rgba(255,240,190,${.66 * alpha})`);
gr.addColorStop(1, 'rgba(255,190,90,0)');
hx.fillStyle = gr; hx.fill();
const ch = h * (.5 + s4 * .04), cw = bw * .42;
const core = hx.createLinearGradient(x, y - h * .1, x, y - ch);
core.addColorStop(0, `rgba(255,220,130,${.5 * alpha})`);
core.addColorStop(.6, `rgba(255,248,225,${.7 * alpha})`);
core.addColorStop(1, 'rgba(255,240,200,0)');
hx.fillStyle = core;
hx.beginPath(); hx.ellipse(x + s1 * cw * .3, y - ch * .5, cw * .5, ch * .5, 0, 0, 7); hx.fill();
if (s4 > .82) { // a tongue tears off the tip now and then
const ty = tipY - h * .18 * (s4 - .82) * 6;
hx.fillStyle = `rgba(255,190,90,${.3 * alpha})`;
hx.beginPath(); hx.ellipse(tipX + s1 * 3, ty, bw * .18, h * .1, 0, 0, 7); hx.fill();
}
}
// the match itself: the light it throws, the stick, the charred head, the flame
let tip = null, lit = false;
function drawMatch(time) {
if (!tip || !lit) return;
hx.setTransform(1, 0, 0, 1, 0, 0);
const fl = .92 + .08 * Math.sin(time * 5.3) + .05 * Math.sin(time * 11.7);
hx.save(); hx.translate(tip.x, tip.y);
hx.globalCompositeOperation = 'lighter';
const pool = hx.createRadialGradient(0, 0, 0, 0, 0, 150);
pool.addColorStop(0, `rgba(255,140,30,${.3 * fl})`); pool.addColorStop(1, 'rgba(255,140,30,0)');
hx.fillStyle = pool; hx.fillRect(-150, -150, 300, 300);
hx.globalCompositeOperation = 'source-over';
hx.save(); hx.rotate(26 * Math.PI / 180);
hx.fillStyle = '#B98A55'; hx.fillRect(-3, -132, 6, 112);
hx.fillStyle = '#2A1B10'; hx.fillRect(-5, -32, 10, 24);
hx.restore();
hx.globalCompositeOperation = 'lighter';
const blue = hx.createRadialGradient(0, -6, 0, 0, -6, 20); // a match burns blue at the base
blue.addColorStop(0, `rgba(110,160,255,${.3 * fl})`); blue.addColorStop(1, 'rgba(110,160,255,0)');
hx.fillStyle = blue; hx.fillRect(-20, -26, 40, 40);
flame(0, -4, 96 * fl, 34, .12, 1.3, time, 1);
hx.restore();
hx.globalCompositeOperation = 'source-over';
}
function paintHeat(now) {
hx.setTransform(1, 0, 0, 1, 0, 0);
hx.clearRect(0, 0, heat.width, heat.height);
const ox = (panel.offsetLeft - heat.offsetLeft) * 2, oy = (panel.offsetTop - heat.offsetTop) * 2;
hx.setTransform(1, 0, 0, 1, ox, oy);
for (const s of smoke) { // smoke drifts up and thins out
s.x += s.vx; s.y += s.vy; s.r += .22; s.life -= .02;
if (s.life <= 0) continue;
const g = hx.createRadialGradient(s.x, s.y, 0, s.x, s.y, s.r * 2.2);
g.addColorStop(0, `rgba(80,70,62,${.09 * s.life})`);
g.addColorStop(1, 'rgba(80,70,62,0)');
hx.fillStyle = g; hx.beginPath(); hx.arc(s.x, s.y, s.r * 2.2, 0, 7); hx.fill();
}
while (smoke.length && smoke[0].life <= 0) smoke.shift();
hx.globalCompositeOperation = 'lighter';
const time = now / 1000;
let tongues = 0;
for (let i = edge.length - 1; i >= 0; i--) {
const e = edge[i], age = (now - e.t) / 1000;
if (age > HEAT) { edge.splice(i, 1); continue; }
const k = Math.exp(-age / (HEAT * .45)), r = BURN * (1.7 + k * .7);
const g = hx.createRadialGradient(e.x, e.y, 0, e.x, e.y, r);
g.addColorStop(0, `rgba(255,243,208,${.55 * k})`);
g.addColorStop(.35, `rgba(255,150,40,${.5 * k})`);
g.addColorStop(1, 'rgba(255,90,10,0)');
hx.fillStyle = g; hx.beginPath(); hx.arc(e.x, e.y, r, 0, 7); hx.fill();
if (k > .3 && i % 2 === 0 && tongues++ < 22) // fire licks along the edge it is eating
flame(e.x, e.y + 3, (14 + 26 * k) * (.8 + .4 * Math.sin(time * 7 + i)), 11 + 7 * k, (i % 3 - 1) * .2, i * .7, time, k * .85);
}
for (const s of sparks) { // sparks ride the hot air, then die out
s.x += s.vx; s.y += s.vy; s.vy -= .012; s.vx *= .99; s.life -= .022;
if (s.life <= 0) continue;
const g = hx.createRadialGradient(s.x, s.y, 0, s.x, s.y, s.r * 4);
g.addColorStop(0, `rgba(255,240,200,${s.life})`);
g.addColorStop(.4, `rgba(255,122,24,${.6 * s.life})`);
g.addColorStop(1, 'rgba(255,90,10,0)');
hx.fillStyle = g; hx.beginPath(); hx.arc(s.x, s.y, s.r * 4, 0, 7); hx.fill();
}
while (sparks.length && sparks[0].life <= 0) sparks.shift();
hx.globalCompositeOperation = 'source-over';
for (const a of ash) { // ash lifts, flutters, cools and fades
a.x += a.vx + Math.sin(a.y * .05) * .35; a.y += a.vy; a.vy += .004; a.rot += a.dr; a.life -= .009;
if (a.life <= 0) continue;
hx.save(); hx.globalAlpha = Math.min(1, a.life * 1.6);
hx.translate(a.x, a.y); hx.rotate(a.rot);
hx.fillStyle = '#2A211A'; hx.fillRect(-a.r, -a.r * .7, a.r * 2, a.r * 1.4);
if (a.life > .72) { hx.fillStyle = `rgba(255,122,24,${(a.life - .72) * 2.6})`; hx.fillRect(-a.r, a.r * .4, a.r * 2, 1.6); }
hx.restore();
}
while (ash.length && ash[0].life <= 0) ash.shift();
}
(function frame(now) {
const ms = now || performance.now();
paintHeat(ms);
drawMatch(ms / 1000);
requestAnimationFrame(frame);
})();
let open = false, checks = 0;
// the counter reads the cover itself, from a small copy so it stays cheap
const tiny = document.createElement('canvas'); tiny.width = 44; tiny.height = 12;
const tx = tiny.getContext('2d', { willReadFrequently: true });
function revealed() {
tx.clearRect(0, 0, tiny.width, tiny.height);
tx.drawImage(cover, 0, 0, tiny.width, tiny.height);
const d = tx.getImageData(0, 0, tiny.width, tiny.height).data;
let gone = 0, all = 0;
for (let i = 3; i < d.length; i += 4) { all++; if (d[i] < 90) gone++; }
return gone / all;
}
function measure() {
const r = revealed();
pct.textContent = Math.round(r * 100) + '%';
if (r >= OPEN) catchFire();
}
// past the threshold the whole sheet is alight: the fire front runs out to the edges,
// then what is left of the paper leaves as ash
function catchFire() {
if (open) return;
open = true;
if (reduce) { cover.classList.add('gone'); pct.textContent = '100%'; done(); return; }
const w = cover.width, h = cover.height, t0 = performance.now();
(function spread(now) {
const k = Math.min(1, (now - t0) / 820);
const R = (.12 + k * 1.15) * w * .5;
const ring = rad => { // a ragged front, never a clean oval
fx.beginPath();
for (let i = 0; i <= 48; i++) {
const a = i / 48 * 6.283;
const n = 1 + .15 * Math.sin(a * 3 + k * 5) + .1 * Math.sin(a * 7 - k * 3.4)
+ .06 * Math.sin(a * 13 + 1.7);
const px = w / 2 + Math.cos(a) * rad * n, py = h / 2 + Math.sin(a) * rad * .62 * n;
i ? fx.lineTo(px, py) : fx.moveTo(px, py);
}
fx.closePath();
};
fx.globalCompositeOperation = 'source-atop';
fx.strokeStyle = 'rgba(28,15,7,.92)'; fx.lineWidth = BURN * 2.6; fx.lineJoin = 'round';
ring(R); fx.stroke();
fx.globalCompositeOperation = 'destination-out';
fx.fillStyle = '#000';
ring(Math.max(0, R - BURN)); fx.fill();
for (let i = 0; i < 7; i++) {
const a = Math.random() * 6.28;
mark(w / 2 + Math.cos(a) * R, h / 2 + Math.sin(a) * R * .62);
}
pct.textContent = Math.round(revealed() * 100) + '%';
if (k < 1) return requestAnimationFrame(spread);
cover.classList.add('gone');
pct.textContent = '100%';
for (let i = 0; i < 24; i++) ash.push({
x: Math.random() * w / 2 + panel.offsetLeft, y: panel.offsetTop + Math.random() * h / 2,
vx: (Math.random() - .5) * .7, vy: -.7 - Math.random() * 1.1,
r: 2 + Math.random() * 3.5, rot: Math.random() * 6.28, dr: (Math.random() - .5) * .1, life: 1,
});
done();
})(t0);
}
function done() {
win.classList.add('hot');
setTimeout(() => win.classList.remove('hot'), 700);
setTimeout(() => btn.classList.add('ready'), 420);
}
// copying throws a handful of embers off the ticket, not paper confetti
function burst() {
const r = panel.getBoundingClientRect(), p = promo.getBoundingClientRect();
for (let i = 0; i < 22; i++) {
const el = document.createElement('i'); el.className = 'bit';
const s = .6 + Math.random() * 1.6;
el.style.left = r.left - p.left + r.width * (.15 + .7 * Math.random()) + 'px';
el.style.top = r.top - p.top + r.height * (.4 + .6 * Math.random()) + 'px';
el.style.width = el.style.height = (4 * s).toFixed(1) + 'px';
promo.append(el);
const dx = (Math.random() - .5) * 150, dy = -70 - Math.random() * 110;
el.animate([
{ transform: 'translate(0, 0) scale(1)', opacity: 1 },
{ transform: `translate(${dx * .4}px, ${dy * .5}px) scale(1)`, opacity: .9, offset: .45 },
{ transform: `translate(${dx}px, ${dy}px) scale(.3)`, opacity: 0 },
], { duration: 1000 + Math.random() * 600, delay: i * 35, 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);
// the match sits where the pointer is, and burns while the button is held
function place(cx, cy) {
const h = heat.getBoundingClientRect();
tip = { x: (cx - h.left) * 2, y: (cy - h.top) * 2 };
lit = true;
}
const spot = e => {
const r = cover.getBoundingClientRect();
return [(e.clientX - r.left) * cover.width / r.width, (e.clientY - r.top) * cover.height / r.height];
};
cover.addEventListener('pointerdown', e => {
cover.setPointerCapture(e.pointerId);
place(e.clientX, e.clientY);
const [x, y] = spot(e); burn(x, y); mark(x, y); measure();
});
cover.addEventListener('pointermove', e => {
place(e.clientX, e.clientY);
if (e.buttons !== 1 || open) return;
const [x, y] = spot(e); burn(x, y); mark(x, y);
if (++checks % 4 === 0) measure();
});
cover.addEventListener('pointerup', measure);
cover.addEventListener('pointerleave', () => { if (!demo) lit = false; });
function reset() {
open = false; checks = 0; last = null;
for (const el of promo.querySelectorAll('.bit')) el.remove();
edge.length = sparks.length = smoke.length = ash.length = 0;
paintCover();
cover.classList.remove('gone');
win.classList.remove('hot');
btn.classList.remove('ready', 'done');
lit = false; tip = null;
pct.textContent = '0%';
}
// a hand does not draw straight lines: each pass wanders off its line, changes pace,
// hesitates once, runs past the end and lifts in an arc to the next one
const STROKES = [
{ a: [.07, .68], b: [.95, .34], bow: .09, dur: 1.15 },
{ a: [.92, .76], b: [.13, .42], bow: -.12, dur: 1.05 },
{ a: [.20, .28], b: [.64, .84], bow: .08, dur: .82 },
{ a: [.72, .30], b: [.30, .62], bow: -.07, dur: .62 },
];
const DT = 1 / 70;
function handPath() {
const out = [];
STROKES.forEach((st, si) => {
const dwellAt = .28 + Math.random() * .38, dwellLen = .07 + Math.random() * .1;
const f1 = 3 + Math.random() * 3, f2 = 8 + Math.random() * 5;
const a1 = .022 + Math.random() * .022, a2 = .008 + Math.random() * .012;
const ph1 = Math.random() * 6.28, ph2 = Math.random() * 6.28;
const dx = st.b[0] - st.a[0], dy = st.b[1] - st.a[1], dl = Math.hypot(dx, dy);
let prog = 0;
while (prog < 1.04) {
const hes = Math.abs(prog - dwellAt) < dwellLen ? .32 : 1;
const pace = .78 + .46 * Math.sin(prog * 5.3 + si * 2.1);
prog += DT / st.dur * hes * pace * 1.3;
const u = Math.min(prog, 1.04);
const off = Math.sin(u * Math.PI) * st.bow + a1 * Math.sin(u * f1 + ph1) + a2 * Math.sin(u * f2 + ph2);
out.push({ x: st.a[0] + dx * u - dy / dl * off, y: st.a[1] + dy * u + dx / dl * off, on: true });
}
if (si < STROKES.length - 1) { // the hand lifts and carries over
const from = out[out.length - 1], to = STROKES[si + 1].a;
const hop = Math.round((.2 + Math.random() * .16) / DT), lift = .14 + Math.random() * .1;
for (let i = 1; i <= hop; i++) {
const u = i / hop, k = u * u * (3 - 2 * u);
out.push({ x: lerp(from.x, to[0], k), y: lerp(from.y, to[1], k) - Math.sin(u * Math.PI) * lift, on: false });
}
}
});
return out;
}
let demo = false; // play() turns it on, a touch turns it off
const stopDemo = () => { if (!demo) return; demo = false; reset(); };
addEventListener('pointerdown', stopDemo, true);
addEventListener('keydown', stopDemo, true);
async function play() {
demo = true;
reset();
sizeHeat();
const w = cover.width, h = cover.height, r = cover.getBoundingClientRect();
await wait(240); if (!demo) return;
let step = 0;
const path = handPath();
for (let i = 0; i < path.length; i += 2) { // every other sample, the line joins them up
const p = path[i];
if (!demo || open) break;
const x = p.x * w + (Math.random() - .5) * 6, y = p.y * h + (Math.random() - .5) * 6;
place(r.left + x / 2, r.top + y / 2);
if (p.on) {
burn(x, y); mark(x, y);
if (step === 0) for (let i = 0; i < 14; i++) mark(x, y); // the paper flares at the first touch
if (++step % 5 === 0) measure();
} else { last = null; }
await wait(9);
}
measure();
if (!demo) return;
catchFire();
await wait(300); if (!demo) return;
lit = false;
await wait(700); if (!demo) return;
btn.click();
await wait(3200); if (!demo) return;
reset();
await wait(900); if (demo) play();
}
paintCover();
sizeHeat();
</script>
Ведите спичкой по бумаге. Ничто не выдаёт код, пока вы его не прожжёте.
Что можно поменять
Поменяйте число, сохраните файл и обновите страницу, разница будет видна сразу.
DISCOUNT30Число, из которого собирается код. До того как бумага сгорит, на карте его не видно.
OPEN0.55Сколько бумаги должно сгореть, чтобы лист вспыхнул целиком.
BURN13Насколько широко ест пламя, в пикселях холста.
HEAT0.38Сколько секунд край светится, прежде чем остыть до угля.
--cover#C3B18CСтарая бумага, которая прячет код. Пятна, волокна и заломы нарисованы кодом, это не картинка.
--ink-red#98351FЦвет, которым напечатан код. --type и --type-small задают шрифты плаката.
Как поставить на свой сайт
Страницы акций и скидок, страница благодарности после оплаты, лендинг из письма, розыгрыш.
Комментарии в коде на английском, так файл понятен любому разработчику.
CSS
Вставьте в свой файл стилей или в тег <style> внутри <head>.
/* Burn & Save by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */ :root { --paper: #EDE6D6; --paper-shadow: #C9BFA8; --cover: #C3B18C; --cover-edge: #B69F72; --ink: #241C10; --ink-red: #98351F; --type: Rockwell, Playbill, Clarendon, 'American Typewriter', Georgia, serif; --type-small: Copperplate, 'Copperplate Gothic Bold', 'Big Caslon', Georgia, serif; --ember: #FF7A18; --ember-core: #FFF3D0; --char: #3A2415; --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; } .hint { font: 700 15px/1 var(--type-small); letter-spacing: .1em; text-transform: uppercase; color: rgba(36,28,16,.55); } .pct { font: 500 13px Menlo, monospace; color: rgba(36,28,16,.4); font-variant-numeric: tabular-nums; } /* the code sits on the paper, the paper canvas lies on top of it */ .panel { position: relative; height: 86px; border-radius: 10px; overflow: hidden; background: #DCD1B8; box-shadow: inset 0 1px 2px rgba(36,28,16,.18), inset 0 0 0 1px rgba(36,28,16,.06); } /* under the paper: the prize first, the code under it, both pressed into the sheet */ .win { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 3px; } .prize { font: 700 40px/1 var(--type); letter-spacing: 2px; color: var(--ink-red); text-shadow: 0 1px 0 rgba(255,255,255,.45), 0 -1px 0 rgba(60,20,10,.25); transition: text-shadow .5s ease-out; } .code { font: 700 17px/1 var(--type-small); letter-spacing: .22em; color: rgba(36,28,16,.6); } .win.hot .prize { text-shadow: 0 0 22px rgba(255,122,24,.8), 0 1px 0 rgba(255,255,255,.45); } .cover { position: absolute; inset: 0; width: 100%; height: 100%; cursor: none; touch-action: none; transition: opacity .3s ease-out; } .cover.gone { opacity: 0; pointer-events: none; } /* glow, sparks and smoke live on their own canvas above the whole ticket */ /* canvas is a replaced element: without an explicit height it stays 150px tall */ .heat { position: absolute; left: -6px; top: -56px; width: calc(100% + 12px); height: calc(100% + 56px); pointer-events: none; } .copy { display: block; width: 100%; height: 44px; margin: 14px 0 0; padding: 0; border: 0; border-radius: 8px; background: var(--ink); color: var(--paper); font: 700 15px/1 var(--type-small); letter-spacing: .08em; text-transform: uppercase; 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(--ember); 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: 400 12px/1.3 var(--type-small); letter-spacing: .08em; text-transform: uppercase; color: rgba(36,28,16,.45); text-align: center; } .bit { position: absolute; width: 4px; height: 4px; border-radius: 50%; pointer-events: none; background: radial-gradient(circle, var(--ember-core), var(--ember) 45%, rgba(255,90,10,0) 75%); } @media (prefers-reduced-motion: reduce) { .copy, .copy span { transition-property: opacity; } .copy.ready { transform: none; } }HTML
Поставьте в то место страницы, где должен быть эффект.
<div class="promo"> <div class="ticket"> <p class="top"><span class="hint">Burn to reveal</span><span class="pct">0%</span></p> <div class="panel"> <span class="win"><span class="prize">30% OFF</span><span class="code">SAVE30</span></span> <canvas class="cover" width="680" height="172" role="img" aria-label="Burn the paper to reveal the code"></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> <canvas class="heat" aria-hidden="true"></canvas> </div>JavaScript
Вставьте перед закрывающим тегом </body>, внутри <script></script>.
/* Burn & 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.42, BURN = 15; // how long the edge glows before it cools to char, in s const HEAT = 0.38; const $ = s => document.querySelector(s); const promo = $('.promo'), panel = $('.panel'), cover = $('.cover'); const heat = $('.heat'), win = $('.win'); const btn = $('.copy'), pct = $('.pct'); const fx = cover.getContext('2d', { willReadFrequently: true }); const hx = heat.getContext('2d'); const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches; const wait = ms => new Promise(r => setTimeout(r, ms)); const lerp = (a, b, k) => a + (b - a) * k; $('.prize').textContent = DISCOUNT + '% OFF'; $('.code').textContent = CODE; function sizeHeat() { const r = heat.getBoundingClientRect(); heat.width = Math.round(r.width * 2); heat.height = Math.round(r.height * 2); } addEventListener('resize', sizeHeat); // the cover is a blank slip of very old paper: tan base, foxing, fibres, // a fold and edges darkened by handling function paintCover() { const w = cover.width, h = cover.height; fx.globalCompositeOperation = 'source-over'; fx.clearRect(0, 0, w, h); const base = fx.createLinearGradient(0, 0, w, h); base.addColorStop(0, '#D3C29C'); base.addColorStop(.42, '#C3B18C'); base.addColorStop(1, '#AD9A76'); fx.fillStyle = base; fx.fillRect(0, 0, w, h); for (let i = 0; i < 150; i++) { // foxing: the brown blooms old paper gets const x = Math.random() * w, y = Math.random() * h, r = 6 + Math.random() * 44; const spot = fx.createRadialGradient(x, y, 0, x, y, r); const dark = Math.random() < .62; spot.addColorStop(0, dark ? 'rgba(126,88,40,.24)' : 'rgba(250,238,208,.2)'); spot.addColorStop(1, 'rgba(0,0,0,0)'); fx.fillStyle = spot; fx.fillRect(x - r, y - r, r * 2, r * 2); } for (let i = 0; i < 2200; i++) { // fibres pressed into the sheet const x = Math.random() * w, y = Math.random() * h, l = 3 + Math.random() * 12, a = (Math.random() - .5) * .7; fx.strokeStyle = Math.random() < .5 ? 'rgba(96,70,34,.14)' : 'rgba(252,242,216,.18)'; fx.lineWidth = 1; fx.beginPath(); fx.moveTo(x, y); fx.lineTo(x + Math.cos(a) * l, y + Math.sin(a) * l); fx.stroke(); } for (let i = 0; i < 5; i++) { // old creases const y = h * (.18 + Math.random() * .64); const cr = fx.createLinearGradient(0, y - 5, 0, y + 5); cr.addColorStop(0, 'rgba(84,60,28,0)'); cr.addColorStop(.5, 'rgba(84,60,28,.13)'); cr.addColorStop(1, 'rgba(84,60,28,0)'); fx.fillStyle = cr; fx.fillRect(0, y - 5, w, 10); } const fold = fx.createLinearGradient(w * .5 - 18, 0, w * .5 + 18, 0); fold.addColorStop(0, 'rgba(70,50,24,0)'); fold.addColorStop(.5, 'rgba(70,50,24,.16)'); fold.addColorStop(1, 'rgba(70,50,24,0)'); fx.fillStyle = fold; fx.fillRect(0, 0, w, h); const edge = fx.createLinearGradient(0, 0, 0, h); edge.addColorStop(0, 'rgba(64,44,20,.36)'); edge.addColorStop(.16, 'rgba(64,44,20,0)'); edge.addColorStop(.84, 'rgba(64,44,20,0)'); edge.addColorStop(1, 'rgba(64,44,20,.4)'); fx.fillStyle = edge; fx.fillRect(0, 0, w, h); const side = fx.createLinearGradient(0, 0, w, 0); side.addColorStop(0, 'rgba(64,44,20,.36)'); side.addColorStop(.1, 'rgba(64,44,20,0)'); side.addColorStop(.9, 'rgba(64,44,20,0)'); side.addColorStop(1, 'rgba(64,44,20,.36)'); fx.fillStyle = side; fx.fillRect(0, 0, w, h); } // the flame chars the paper along its path and eats through // the middle of that band. source-atop keeps the char on // the paper that is still there, never inside a hole. let last = null; function burn(x, y) { const dx0 = last ? x - last[0] : 999; const dy0 = last ? y - last[1] : 999; const from = Math.hypot(dx0, dy0) < 90 ? last : [x, y]; last = [x, y]; const line = (w, style, mode) => { fx.globalCompositeOperation = mode; fx.strokeStyle = style; fx.lineWidth = w; fx.lineCap = fx.lineJoin = 'round'; fx.beginPath(); fx.moveTo(from[0], from[1]); fx.lineTo(x, y); fx.stroke(); }; line(BURN * 3.2, 'rgba(104,60,26,.42)', 'source-atop'); line(BURN * 2.3, 'rgba(38,20,9,.86)', 'source-atop'); line(BURN * 1.5 * (.9 + Math.random() * .2), '#000', 'destination-out'); fx.fillStyle = '#000'; for (let i = 0; i < 2; i++) { // bites at the edge const a = Math.random() * 6.28; const d = BURN * (.7 + Math.random() * .45); const r = BURN * (.1 + Math.random() * .16); const px = x + Math.cos(a) * d; const py = y + Math.sin(a) * d; fx.beginPath(); fx.arc(px, py, r, 0, 7); fx.fill(); } } // the live edge: every point cools on its own clock, white to orange to nothing const edge = [], sparks = [], smoke = [], ash = []; function mark(px, py) { edge.push({ x: px, y: py, t: performance.now() }); if (reduce) return; if (Math.random() < .6) sparks.push({ x: px, y: py, life: 1, vx: (Math.random() - .5) * .9, vy: -.7 - Math.random() * 1.1, r: .8 + Math.random() * 1.4, }); if (Math.random() < .07) smoke.push({ x: px, y: py, life: 1, vx: (Math.random() - .5) * .3, vy: -.5 - Math.random() * .3, r: 4 + Math.random() * 4, }); } // a flame is a wavering teardrop: dark at the base, bright inside, torn at the tip function flame(x, y, h, w, lean, seed, time, alpha) { const s1 = Math.sin(time * 6.1 + seed), s2 = Math.sin(time * 9.7 + seed * 1.7); const s3 = Math.sin(time * 3.3 + seed * .6), s4 = Math.sin(time * 13.3 + seed * 2.3); const tipX = x + lean * h * .3 + (s1 * .12 + s2 * .06) * w * 2.4; const tipY = y - h * (1 + s3 * .1); const bw = w * (1 + s2 * .07); hx.beginPath(); hx.moveTo(x - bw * .5, y); hx.bezierCurveTo(x - bw * .66, y - h * .45, tipX - bw * .32 + s2 * 2, tipY + h * .32, tipX, tipY); hx.bezierCurveTo(tipX + bw * .32 - s1 * 2, tipY + h * .32, x + bw * .66, y - h * .45, x + bw * .5, y); hx.closePath(); const gr = hx.createLinearGradient(x, y, tipX, tipY); gr.addColorStop(0, `rgba(190,70,10,${.16 * alpha})`); gr.addColorStop(.2, `rgba(255,140,30,${.6 * alpha})`); gr.addColorStop(.5, `rgba(255,200,80,${.88 * alpha})`); gr.addColorStop(.78, `rgba(255,240,190,${.66 * alpha})`); gr.addColorStop(1, 'rgba(255,190,90,0)'); hx.fillStyle = gr; hx.fill(); const ch = h * (.5 + s4 * .04), cw = bw * .42; const core = hx.createLinearGradient(x, y - h * .1, x, y - ch); core.addColorStop(0, `rgba(255,220,130,${.5 * alpha})`); core.addColorStop(.6, `rgba(255,248,225,${.7 * alpha})`); core.addColorStop(1, 'rgba(255,240,200,0)'); hx.fillStyle = core; hx.beginPath(); hx.ellipse(x + s1 * cw * .3, y - ch * .5, cw * .5, ch * .5, 0, 0, 7); hx.fill(); if (s4 > .82) { // a tongue tears off the tip now and then const ty = tipY - h * .18 * (s4 - .82) * 6; hx.fillStyle = `rgba(255,190,90,${.3 * alpha})`; hx.beginPath(); hx.ellipse(tipX + s1 * 3, ty, bw * .18, h * .1, 0, 0, 7); hx.fill(); } } // the match itself: the light it throws, the stick, the charred head, the flame let tip = null, lit = false; function drawMatch(time) { if (!tip || !lit) return; hx.setTransform(1, 0, 0, 1, 0, 0); const fl = .92 + .08 * Math.sin(time * 5.3) + .05 * Math.sin(time * 11.7); hx.save(); hx.translate(tip.x, tip.y); hx.globalCompositeOperation = 'lighter'; const pool = hx.createRadialGradient(0, 0, 0, 0, 0, 150); pool.addColorStop(0, `rgba(255,140,30,${.3 * fl})`); pool.addColorStop(1, 'rgba(255,140,30,0)'); hx.fillStyle = pool; hx.fillRect(-150, -150, 300, 300); hx.globalCompositeOperation = 'source-over'; hx.save(); hx.rotate(26 * Math.PI / 180); hx.fillStyle = '#B98A55'; hx.fillRect(-3, -132, 6, 112); hx.fillStyle = '#2A1B10'; hx.fillRect(-5, -32, 10, 24); hx.restore(); hx.globalCompositeOperation = 'lighter'; const blue = hx.createRadialGradient(0, -6, 0, 0, -6, 20); // a match burns blue at the base blue.addColorStop(0, `rgba(110,160,255,${.3 * fl})`); blue.addColorStop(1, 'rgba(110,160,255,0)'); hx.fillStyle = blue; hx.fillRect(-20, -26, 40, 40); flame(0, -4, 96 * fl, 34, .12, 1.3, time, 1); hx.restore(); hx.globalCompositeOperation = 'source-over'; } function paintHeat(now) { hx.setTransform(1, 0, 0, 1, 0, 0); hx.clearRect(0, 0, heat.width, heat.height); const ox = (panel.offsetLeft - heat.offsetLeft) * 2, oy = (panel.offsetTop - heat.offsetTop) * 2; hx.setTransform(1, 0, 0, 1, ox, oy); for (const s of smoke) { // smoke drifts up and thins out s.x += s.vx; s.y += s.vy; s.r += .22; s.life -= .02; if (s.life <= 0) continue; const g = hx.createRadialGradient(s.x, s.y, 0, s.x, s.y, s.r * 2.2); g.addColorStop(0, `rgba(80,70,62,${.09 * s.life})`); g.addColorStop(1, 'rgba(80,70,62,0)'); hx.fillStyle = g; hx.beginPath(); hx.arc(s.x, s.y, s.r * 2.2, 0, 7); hx.fill(); } while (smoke.length && smoke[0].life <= 0) smoke.shift(); hx.globalCompositeOperation = 'lighter'; const time = now / 1000; let tongues = 0; for (let i = edge.length - 1; i >= 0; i--) { const e = edge[i], age = (now - e.t) / 1000; if (age > HEAT) { edge.splice(i, 1); continue; } const k = Math.exp(-age / (HEAT * .45)), r = BURN * (1.7 + k * .7); const g = hx.createRadialGradient(e.x, e.y, 0, e.x, e.y, r); g.addColorStop(0, `rgba(255,243,208,${.55 * k})`); g.addColorStop(.35, `rgba(255,150,40,${.5 * k})`); g.addColorStop(1, 'rgba(255,90,10,0)'); hx.fillStyle = g; hx.beginPath(); hx.arc(e.x, e.y, r, 0, 7); hx.fill(); if (k > .3 && i % 2 === 0 && tongues++ < 22) // fire licks along the edge it is eating flame(e.x, e.y + 3, (14 + 26 * k) * (.8 + .4 * Math.sin(time * 7 + i)), 11 + 7 * k, (i % 3 - 1) * .2, i * .7, time, k * .85); } for (const s of sparks) { // sparks ride the hot air, then die out s.x += s.vx; s.y += s.vy; s.vy -= .012; s.vx *= .99; s.life -= .022; if (s.life <= 0) continue; const g = hx.createRadialGradient(s.x, s.y, 0, s.x, s.y, s.r * 4); g.addColorStop(0, `rgba(255,240,200,${s.life})`); g.addColorStop(.4, `rgba(255,122,24,${.6 * s.life})`); g.addColorStop(1, 'rgba(255,90,10,0)'); hx.fillStyle = g; hx.beginPath(); hx.arc(s.x, s.y, s.r * 4, 0, 7); hx.fill(); } while (sparks.length && sparks[0].life <= 0) sparks.shift(); hx.globalCompositeOperation = 'source-over'; for (const a of ash) { // ash lifts, flutters, cools and fades a.x += a.vx + Math.sin(a.y * .05) * .35; a.y += a.vy; a.vy += .004; a.rot += a.dr; a.life -= .009; if (a.life <= 0) continue; hx.save(); hx.globalAlpha = Math.min(1, a.life * 1.6); hx.translate(a.x, a.y); hx.rotate(a.rot); hx.fillStyle = '#2A211A'; hx.fillRect(-a.r, -a.r * .7, a.r * 2, a.r * 1.4); if (a.life > .72) { hx.fillStyle = `rgba(255,122,24,${(a.life - .72) * 2.6})`; hx.fillRect(-a.r, a.r * .4, a.r * 2, 1.6); } hx.restore(); } while (ash.length && ash[0].life <= 0) ash.shift(); } (function frame(now) { const ms = now || performance.now(); paintHeat(ms); drawMatch(ms / 1000); requestAnimationFrame(frame); })(); let open = false, checks = 0; // the counter reads the cover itself, from a small copy so it stays cheap const tiny = document.createElement('canvas'); tiny.width = 44; tiny.height = 12; const tx = tiny.getContext('2d', { willReadFrequently: true }); function revealed() { tx.clearRect(0, 0, tiny.width, tiny.height); tx.drawImage(cover, 0, 0, tiny.width, tiny.height); const d = tx.getImageData(0, 0, tiny.width, tiny.height).data; let gone = 0, all = 0; for (let i = 3; i < d.length; i += 4) { all++; if (d[i] < 90) gone++; } return gone / all; } function measure() { const r = revealed(); pct.textContent = Math.round(r * 100) + '%'; if (r >= OPEN) catchFire(); } // past the threshold the whole sheet is alight: the fire front runs out to the edges, // then what is left of the paper leaves as ash function catchFire() { if (open) return; open = true; if (reduce) { cover.classList.add('gone'); pct.textContent = '100%'; done(); return; } const w = cover.width, h = cover.height, t0 = performance.now(); (function spread(now) { const k = Math.min(1, (now - t0) / 820); const R = (.12 + k * 1.15) * w * .5; const ring = rad => { // a ragged front, never a clean oval fx.beginPath(); for (let i = 0; i <= 48; i++) { const a = i / 48 * 6.283; const n = 1 + .15 * Math.sin(a * 3 + k * 5) + .1 * Math.sin(a * 7 - k * 3.4) + .06 * Math.sin(a * 13 + 1.7); const px = w / 2 + Math.cos(a) * rad * n, py = h / 2 + Math.sin(a) * rad * .62 * n; i ? fx.lineTo(px, py) : fx.moveTo(px, py); } fx.closePath(); }; fx.globalCompositeOperation = 'source-atop'; fx.strokeStyle = 'rgba(28,15,7,.92)'; fx.lineWidth = BURN * 2.6; fx.lineJoin = 'round'; ring(R); fx.stroke(); fx.globalCompositeOperation = 'destination-out'; fx.fillStyle = '#000'; ring(Math.max(0, R - BURN)); fx.fill(); for (let i = 0; i < 7; i++) { const a = Math.random() * 6.28; mark(w / 2 + Math.cos(a) * R, h / 2 + Math.sin(a) * R * .62); } pct.textContent = Math.round(revealed() * 100) + '%'; if (k < 1) return requestAnimationFrame(spread); cover.classList.add('gone'); pct.textContent = '100%'; for (let i = 0; i < 24; i++) ash.push({ x: Math.random() * w / 2 + panel.offsetLeft, y: panel.offsetTop + Math.random() * h / 2, vx: (Math.random() - .5) * .7, vy: -.7 - Math.random() * 1.1, r: 2 + Math.random() * 3.5, rot: Math.random() * 6.28, dr: (Math.random() - .5) * .1, life: 1, }); done(); })(t0); } function done() { win.classList.add('hot'); setTimeout(() => win.classList.remove('hot'), 700); setTimeout(() => btn.classList.add('ready'), 420); } // copying throws a handful of embers off the ticket, not paper confetti function burst() { const r = panel.getBoundingClientRect(), p = promo.getBoundingClientRect(); for (let i = 0; i < 22; i++) { const el = document.createElement('i'); el.className = 'bit'; const s = .6 + Math.random() * 1.6; el.style.left = r.left - p.left + r.width * (.15 + .7 * Math.random()) + 'px'; el.style.top = r.top - p.top + r.height * (.4 + .6 * Math.random()) + 'px'; el.style.width = el.style.height = (4 * s).toFixed(1) + 'px'; promo.append(el); const dx = (Math.random() - .5) * 150, dy = -70 - Math.random() * 110; el.animate([ { transform: 'translate(0, 0) scale(1)', opacity: 1 }, { transform: `translate(${dx * .4}px, ${dy * .5}px) scale(1)`, opacity: .9, offset: .45 }, { transform: `translate(${dx}px, ${dy}px) scale(.3)`, opacity: 0 }, ], { duration: 1000 + Math.random() * 600, delay: i * 35, 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); // the match sits where the pointer is, and burns while the button is held function place(cx, cy) { const h = heat.getBoundingClientRect(); tip = { x: (cx - h.left) * 2, y: (cy - h.top) * 2 }; lit = true; } const spot = e => { const r = cover.getBoundingClientRect(); return [(e.clientX - r.left) * cover.width / r.width, (e.clientY - r.top) * cover.height / r.height]; }; cover.addEventListener('pointerdown', e => { cover.setPointerCapture(e.pointerId); place(e.clientX, e.clientY); const [x, y] = spot(e); burn(x, y); mark(x, y); measure(); }); cover.addEventListener('pointermove', e => { place(e.clientX, e.clientY); if (e.buttons !== 1 || open) return; const [x, y] = spot(e); burn(x, y); mark(x, y); if (++checks % 4 === 0) measure(); }); cover.addEventListener('pointerup', measure); cover.addEventListener('pointerleave', () => { if (!demo) lit = false; }); function reset() { open = false; checks = 0; last = null; for (const el of promo.querySelectorAll('.bit')) el.remove(); edge.length = sparks.length = smoke.length = ash.length = 0; paintCover(); cover.classList.remove('gone'); win.classList.remove('hot'); btn.classList.remove('ready', 'done'); lit = false; tip = null; pct.textContent = '0%'; } // a hand does not draw straight lines: each pass wanders off its line, changes pace, // hesitates once, runs past the end and lifts in an arc to the next one const STROKES = [ { a: [.07, .68], b: [.95, .34], bow: .09, dur: 1.15 }, { a: [.92, .76], b: [.13, .42], bow: -.12, dur: 1.05 }, { a: [.20, .28], b: [.64, .84], bow: .08, dur: .82 }, { a: [.72, .30], b: [.30, .62], bow: -.07, dur: .62 }, ]; const DT = 1 / 70; function handPath() { const out = []; STROKES.forEach((st, si) => { const dwellAt = .28 + Math.random() * .38, dwellLen = .07 + Math.random() * .1; const f1 = 3 + Math.random() * 3, f2 = 8 + Math.random() * 5; const a1 = .022 + Math.random() * .022, a2 = .008 + Math.random() * .012; const ph1 = Math.random() * 6.28, ph2 = Math.random() * 6.28; const dx = st.b[0] - st.a[0], dy = st.b[1] - st.a[1], dl = Math.hypot(dx, dy); let prog = 0; while (prog < 1.04) { const hes = Math.abs(prog - dwellAt) < dwellLen ? .32 : 1; const pace = .78 + .46 * Math.sin(prog * 5.3 + si * 2.1); prog += DT / st.dur * hes * pace * 1.3; const u = Math.min(prog, 1.04); const off = Math.sin(u * Math.PI) * st.bow + a1 * Math.sin(u * f1 + ph1) + a2 * Math.sin(u * f2 + ph2); out.push({ x: st.a[0] + dx * u - dy / dl * off, y: st.a[1] + dy * u + dx / dl * off, on: true }); } if (si < STROKES.length - 1) { // the hand lifts and carries over const from = out[out.length - 1], to = STROKES[si + 1].a; const hop = Math.round((.2 + Math.random() * .16) / DT), lift = .14 + Math.random() * .1; for (let i = 1; i <= hop; i++) { const u = i / hop, k = u * u * (3 - 2 * u); out.push({ x: lerp(from.x, to[0], k), y: lerp(from.y, to[1], k) - Math.sin(u * Math.PI) * lift, on: false }); } } }); return out; } let demo = false; // play() turns it on, a touch turns it off const stopDemo = () => { if (!demo) return; demo = false; reset(); }; addEventListener('pointerdown', stopDemo, true); addEventListener('keydown', stopDemo, true); async function play() { demo = true; reset(); sizeHeat(); const w = cover.width, h = cover.height, r = cover.getBoundingClientRect(); await wait(240); if (!demo) return; let step = 0; const path = handPath(); for (let i = 0; i < path.length; i += 2) { // every other sample, the line joins them up const p = path[i]; if (!demo || open) break; const x = p.x * w + (Math.random() - .5) * 6, y = p.y * h + (Math.random() - .5) * 6; place(r.left + x / 2, r.top + y / 2); if (p.on) { burn(x, y); mark(x, y); if (step === 0) for (let i = 0; i < 14; i++) mark(x, y); // the paper flares at the first touch if (++step % 5 === 0) measure(); } else { last = null; } await wait(9); } measure(); if (!demo) return; catchFire(); await wait(300); if (!demo) return; lit = false; await wait(700); if (!demo) return; btn.click(); await wait(3200); if (!demo) return; reset(); await wait(900); if (demo) play(); } paintCover(); sizeHeat();
Подключение к серверу
Код лежит в DISCOUNT и CODE. Чтобы выдавать персональный код, подставляйте значение с сервера в CODE до того, как карта нарисуется, и проверяйте его ещё раз на сервере при оформлении заказа.
Полезно знать
- Дыра это canvas с globalCompositeOperation destination-out, поэтому бумага действительно исчезает, а не бледнеет.
- Обугленная кромка рисуется в режиме source-atop, поэтому она остаётся только на уцелевшей бумаге и никогда не попадает в дыру.
- Счётчик считает пиксели газетной бумаги, это не придуманное число.
- Пламя это дрожащая капля, нарисованная контуром, тёмная у основания и яркая внутри, с язычком, который отрывается от кончика. Та же форма лижет кромку, которую ест огонь.
- При включённом уменьшении движения искры, дым, пепел и мерцание пропускаются, но состояния всё так же меняются.
- Шрифт брусковая антиква и Copperplate для мелких строк, с Georgia и serif про запас, поэтому ничего не скачивается.
- При копировании с карты слетают угольки, а не конфетти, так всё остаётся в одном мире.
- Спичка горит синим у основания, как настоящая, и её свет падает на карту.
- Демо ведёт спичку так, как вела бы рука. Проход уходит с линии, темп неровный, один раз она медлит и дугой поднимается к следующему проходу.
- До начала карта не говорит ничего, кроме burn to reveal. Число живёт только в коде, поэтому карта хранит секрет.
- Карта устроена как настоящая скретч-карта. Число это сюрприз под бумагой, а код лежит под ним как то, что забираешь с собой, поэтому ничто не выдаёт приз заранее.
Tilda, Webflow, WordPress
Нажмите «Скопировать весь код» вверху и вставьте его в один HTML-блок.
Добавить блок, Другое, T123 HTML-код. Вставьте и сохраните. Проверяйте на опубликованной странице, в редакторе скрипты не запускаются.
Панель Add, Embed. Вставьте и нажмите Save & Close. Свой код работает на опубликованном сайте.
Добавить блок, Произвольный HTML. Вставьте и нажмите «Обновить».


