Проявка полароида
Свежий моментальный снимок висит на прищепке, ещё тёмный. Схватите и потрясите его: резкие смены направления проявляют кадр слой за слоем, а после отпускания он раскачивается на верёвке.
- Первый экран
- Бесплатно
- canvas
- physics
- Добавлен 16 октября 2026
<style>
/* Polaroid Develop by @codinvisual · t.me/CodinVisual · page shell, only needed while this file is the whole page */
</style>
<!-- polaroid start -->
<div class="pd">
<canvas class="pd-grain" aria-hidden="true"></canvas>
<svg class="pd-line" aria-hidden="true"><path fill="none" stroke="#CDB894" stroke-opacity=".75" stroke-width="1.6" stroke-linecap="round"/><path fill="none" stroke="#000" stroke-opacity=".35" stroke-width="1.6" stroke-linecap="round"/></svg>
<div class="pd-card" tabindex="0" role="button" aria-label="Instant photo of a lake at dusk. Grab it and shake it to develop, or use the arrow keys.">
<canvas class="pd-tex" aria-hidden="true"></canvas>
<canvas class="pd-photo" aria-hidden="true"></canvas>
<div class="pd-face" aria-hidden="true"></div>
<svg class="pd-peg" viewBox="0 0 18 58" aria-hidden="true">
<defs>
<linearGradient id="pd-wood" x1="0" x2="1"><stop offset="0" stop-color="#8E6A45"/><stop offset=".45" stop-color="#C9A57A"/><stop offset="1" stop-color="#9A764F"/></linearGradient>
<linearGradient id="pd-steel" x1="0" x2="0" y1="0" y2="1"><stop offset="0" stop-color="#9AA0A6"/><stop offset=".5" stop-color="#E4E7EA"/><stop offset="1" stop-color="#5F656B"/></linearGradient>
</defs>
<rect x="2" y="0" width="14" height="58" rx="3" fill="url(#pd-wood)"/>
<path d="M9 2 V56" stroke="#5E4329" stroke-opacity=".55" stroke-width=".8"/>
<path d="M3 9 Q9 7 15 9 M3 20 Q9 18.5 15 20 M3 44 Q9 42.5 15 44" stroke="#6E5236" stroke-opacity=".25" stroke-width=".6" fill="none"/>
<rect x="0" y="23" width="18" height="7" rx="3.5" fill="url(#pd-steel)"/>
<path d="M2 26.5 H16" stroke="#3E4348" stroke-opacity=".5" stroke-width=".6"/>
</svg>
</div>
<button class="pd-save" type="button"><span>Save photo</span><svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true"><path d="M12 4v13M6.5 12l5.5 5.5 5.5-5.5M5 20.5h14" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/></svg></button>
<p class="pd-hint" aria-hidden="true">Grab the photo and shake it</p>
</div>
<!-- polaroid end -->
<script>
/* Polaroid Develop by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
(() => {
const DEVELOP = 1;
const SWING = 1.9;
const SETTLE = 1.5;
const PHOTO = '';
const root = document.querySelector('.pd');
if (!root) return;
const card = root.querySelector('.pd-card'), photo = root.querySelector('.pd-photo'), face = root.querySelector('.pd-face');
const tex = root.querySelector('.pd-tex'), grain = root.querySelector('.pd-grain'), hint = root.querySelector('.pd-hint');
const save = root.querySelector('.pd-save'), label = save.querySelector('span'), [twine, twineShade] = root.querySelectorAll('.pd-line path');
const calm = matchMedia('(prefers-reduced-motion: reduce)');
const pc = photo.getContext('2d');
const SIZE = 720, STAGES = 9;
const clamp = (v, a = 0, b = 1) => v < a ? a : v > b ? b : v;
const smooth = t => (t = clamp(t), t * t * (3 - 2 * t));
function make(w, h) {
const cv = document.createElement('canvas');
cv.width = w; cv.height = h;
return { cv, ctx: cv.getContext('2d') };
}
let seed = 1016;
const rnd = () => {
seed = seed + 0x6D2B79F5 | 0;
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
function hash(a, b) {
let n = Math.imul(a, 374761393) + Math.imul(b, 668265263) | 0;
n = Math.imul(n ^ n >>> 13, 1274126177);
return ((n ^ n >>> 16) >>> 0) / 4294967295;
}
function noise(x, y) {
const xi = Math.floor(x), yi = Math.floor(y), u = smooth(x - xi), v = smooth(y - yi);
return (hash(xi, yi) * (1 - u) + hash(xi + 1, yi) * u) * (1 - v) + (hash(xi, yi + 1) * (1 - u) + hash(xi + 1, yi + 1) * u) * v;
}
// the picture: a lake at dusk, drawn once
function pine(x, cx, base, h, w) {
const tiers = 7 + (rnd() * 4 | 0);
x.beginPath();
x.moveTo(cx, base - h);
for (let i = 1; i <= tiers; i++) {
const y = base - h + h * i / tiers * .9, tw = w * (.25 + .75 * i / tiers) * (.8 + rnd() * .45);
x.lineTo(cx + tw, y);
x.lineTo(cx + tw * .3, y - h / tiers * .12);
}
x.lineTo(cx + w * .07, base); x.lineTo(cx - w * .07, base);
for (let i = tiers; i >= 1; i--) {
const y = base - h + h * i / tiers * .9, tw = w * (.25 + .75 * i / tiers) * (.8 + rnd() * .45);
x.lineTo(cx - tw * .3, y - h / tiers * .12);
x.lineTo(cx - tw, y);
}
x.closePath();
x.fill();
}
function ridge(x, S, top, amp, color, k) {
x.fillStyle = color;
x.beginPath();
x.moveTo(0, S);
for (let i = 0; i <= 64; i++) {
const u = i / 64, y = top - amp * (.55 * Math.sin(u * 5.1 + k) + .3 * Math.sin(u * 11.7 + k * 2.3) + .15 * Math.sin(u * 23 + k * 4.1));
x.lineTo(u * S, y);
}
x.lineTo(S, S);
x.closePath();
x.fill();
}
function light(x, px, py, r, rgb, a) {
const g = x.createRadialGradient(px, py, 0, px, py, r);
g.addColorStop(0, `rgba(${rgb},${a})`); g.addColorStop(1, `rgba(${rgb},0)`);
x.fillStyle = g; x.fillRect(px - r, py - r, r * 2, r * 2);
}
function lake(S) {
const { cv, ctx: x } = make(S, S), H = Math.round(S * .52), u = S / 900;
seed = 1016;
const sky = x.createLinearGradient(0, 0, 0, H);
sky.addColorStop(0, '#0A1120'); sky.addColorStop(.36, '#17203A'); sky.addColorStop(.66, '#33304B'); sky.addColorStop(.88, '#6E4C4E'); sky.addColorStop(1, '#B06E4B');
x.fillStyle = sky; x.fillRect(0, 0, S, H + 2);
x.globalCompositeOperation = 'lighter';
light(x, S * .72, H, S * .6, '255,165,105', .24);
for (let i = 0; i < 52; i++) { const a = .12 + rnd() * .45; x.fillStyle = `rgba(230,236,255,${a})`; x.beginPath(); x.arc(rnd() * S, rnd() * H * .5, (.5 + rnd() * .7) * u, 0, 7); x.fill(); }
x.globalCompositeOperation = 'source-over';
ridge(x, S, H - 78 * u, 34 * u, '#2D2E45', 1.3);
ridge(x, S, H - 34 * u, 18 * u, '#1E1F2E', 4.2);
// low mist over the far water
const mist = x.createLinearGradient(0, H - 40 * u, 0, H + 4 * u);
mist.addColorStop(0, 'rgba(190,170,200,0)'); mist.addColorStop(.7, 'rgba(190,170,200,.12)'); mist.addColorStop(1, 'rgba(190,170,200,0)');
x.fillStyle = mist; x.fillRect(0, H - 40 * u, S, 44 * u);
// the far shore on the right: low spruce and a few lights
x.fillStyle = '#0D1018';
x.fillRect(S * .46, H - 4 * u, S * .6, 5 * u);
for (let px = S * .46; px < S + 10 * u; px += (6 + rnd() * 5) * u) { const h = (12 + 20 * noise(px / (70 * u), 3) + rnd() * 8) * u; pine(x, px, H + 1, h, h * .3); }
// the near shore on the left rises out of the water, the cabin stands on it among the spruce
const land = y => H - 4 * u - 46 * u * smooth(1 - y / (S * .55));
x.fillStyle = '#090B11';
x.beginPath(); x.moveTo(0, H + 2 * u);
for (let i = 0; i <= 40; i++) { const px = i / 40 * S * .58; x.lineTo(px, land(px)); }
x.lineTo(S * .58, H + 2 * u); x.closePath(); x.fill();
for (let px = -8 * u; px < S * .56; px += (8 + rnd() * 9) * u) {
if (px > S * .2 && px < S * .36) continue;
const h = (38 + 70 * noise(px / (80 * u), 7) + rnd() * 24) * u * (1 - px / (S * 1.1));
pine(x, px, land(px) + 3 * u, h, h * .3);
}
const cx = S * .28, base = land(cx) + 2 * u, cw = 92 * u, ch = 48 * u;
x.fillStyle = '#07090D';
x.fillRect(cx - cw / 2, base - ch, cw, ch);
x.beginPath(); x.moveTo(cx - cw / 2 - 9 * u, base - ch); x.lineTo(cx - 6 * u, base - ch - 38 * u); x.lineTo(cx + cw / 2 + 9 * u, base - ch); x.closePath(); x.fill();
x.fillRect(cx + 16 * u, base - ch - 40 * u, 9 * u, 24 * u);
for (const px of [cx - 64 * u, cx + 70 * u]) pine(x, px, base + 2 * u, 120 * u, 34 * u);
const wx = cx + 8 * u, wy = base - ch + 14 * u, ww = 22 * u, wh = 20 * u;
x.fillStyle = '#FFB966'; x.fillRect(wx, wy, ww, wh);
x.fillStyle = '#FFE0AE'; x.fillRect(wx + 3 * u, wy + 3 * u, ww - 6 * u, wh - 6 * u);
x.fillStyle = '#3A2414'; x.fillRect(wx + ww / 2 - 1 * u, wy, 2 * u, wh); x.fillRect(wx, wy + wh / 2 - 1 * u, ww, 2 * u);
x.globalCompositeOperation = 'lighter';
light(x, wx + ww / 2, wy + wh / 2, 70 * u, '255,170,90', .5);
light(x, wx + ww / 2, wy + wh / 2, 190 * u, '255,150,80', .14);
light(x, wx + ww / 2, base + 6 * u, 90 * u, '255,160,90', .1);
const lamps = [[.6, 2.2, '255,206,150'], [.66, 1.5, '255,226,190'], [.72, 2, '255,196,130'], [.8, 1.3, '230,236,255'], [.86, 2.1, '255,210,160'], [.93, 1.4, '255,226,190']];
for (const [lx, r, rgb] of lamps) { light(x, lx * S, H - 3 * u, r * 3.6 * u, rgb, .5); x.fillStyle = `rgba(${rgb},.95)`; x.beginPath(); x.arc(lx * S, H - 3 * u, r * .55 * u, 0, 7); x.fill(); }
x.globalCompositeOperation = 'source-over';
// the water mirrors everything above it, rippled more the closer it is
for (let y = H; y < S; y++) {
const d = y - H, src = H - d - 1;
if (src < 0) break;
const amp = (1 + d * .04) * u, dx = Math.sin(y * .83) * amp * .6 + Math.sin(y * .19 + 1.7) * amp;
x.drawImage(cv, 0, src, S, 1, dx, y, S, 1);
}
const deep = x.createLinearGradient(0, H, 0, S);
deep.addColorStop(0, 'rgba(6,10,20,.28)'); deep.addColorStop(1, 'rgba(3,5,10,.7)');
x.fillStyle = deep; x.fillRect(0, H, S, S - H);
// the window's light runs across the water toward us in broken streaks
x.globalCompositeOperation = 'lighter';
const streak = (sx, rgb, a0, len, w0) => {
for (let k = 0; k < len; k++) {
const y = H + (3 + k * 6.5) * u, f = 1 - k / len, w = (w0 * (.55 + .45 * f) + rnd() * 10) * u;
x.fillStyle = `rgba(${rgb},${(a0 * f * (.45 + rnd() * .55)).toFixed(3)})`;
x.fillRect(sx - w / 2 + (rnd() - .5) * 8 * u, y, w, (1.3 + rnd() * 1.5) * u);
}
};
streak(wx + ww / 2, '255,178,96', .7, 52, 26);
for (const [lx, , rgb] of lamps) streak(lx * S, rgb, .3, 20, 12);
x.fillStyle = 'rgba(255,190,140,.10)'; x.fillRect(S * .5, H, S * .5, 1.5 * u);
x.globalCompositeOperation = 'source-over';
// two spruces on our side of the lake, their feet below the frame
x.fillStyle = '#05070B';
pine(x, S * .95, S + 60 * u, 560 * u, 78 * u);
pine(x, S * 1.03, S + 40 * u, 640 * u, 92 * u);
return cv;
}
function cover(img, S) {
const { cv, ctx } = make(S, S), k = Math.max(S / img.naturalWidth, S / img.naturalHeight);
const w = img.naturalWidth * k, h = img.naturalHeight * k;
ctx.drawImage(img, (S - w) / 2, (S - h) / 2, w, h);
return cv;
}
// the look of instant film: soft lens, lifted blacks, teal shadows, warm highlights, grain
function film(src, S) {
const { cv, ctx } = make(S, S), small = make(S * .5 | 0, S * .5 | 0);
small.ctx.drawImage(src, 0, 0, small.cv.width, small.cv.height);
ctx.drawImage(src, 0, 0);
ctx.globalAlpha = .45; ctx.drawImage(small.cv, 0, 0, S, S); ctx.globalAlpha = 1;
const img = ctx.getImageData(0, 0, S, S), d = img.data;
for (let y = 0, i = 0; y < S; y++) for (let x = 0; x < S; x++, i += 4) {
let r = d[i] / 255, g = d[i + 1] / 255, b = d[i + 2] / 255;
const l = .2126 * r + .7152 * g + .0722 * b, sh = 1 - smooth(l / .45), hi = smooth((l - .5) / .5);
r = .04 + .93 * r - .02 * sh + .03 * hi; g = .05 + .92 * g + .012 * sh; b = .06 + .9 * b + .02 * sh - .025 * hi;
const vx = x / S - .5, vy = y / S - .5, v = 1 - .55 * (vx * vx + vy * vy) * 2, n = (rnd() - .5) * .05;
d[i] = clamp(r * v + n) * 255; d[i + 1] = clamp(g * v + n) * 255; d[i + 2] = clamp(b * v + n) * 255;
}
ctx.putImageData(img, 0, 0);
return cv;
}
// a fresh print is a dark blue-green layer that clears: the dark shapes show first, the lights and the colour later
let stages = [];
function develop(finalCv, S) {
const F = finalCv.getContext('2d').getImageData(0, 0, S, S).data, n = S * S;
const L = new Float32Array(n), tau = new Float32Array(n), mot = new Float32Array(n);
for (let y = 0, i = 0; y < S; y++) for (let x = 0; x < S; x++, i++) {
const r = F[i * 4] / 255, g = F[i * 4 + 1] / 255, b = F[i * 4 + 2] / 255;
L[i] = .2126 * r + .7152 * g + .0722 * b;
// the reagent pod sits under the wide bottom edge, so the print clears a touch sooner from below
const late = .62 * Math.pow(L[i], .8);
tau[i] = .08 + late + .1 * (noise(x / 70, y / 70) - .5) - .05 * (y / S);
mot[i] = noise(x / 26 + 9, y / 26) - .5;
}
stages = [];
for (let k = 0; k < STAGES; k++) {
const p = k / (STAGES - 1), { cv, ctx } = make(S, S), img = ctx.createImageData(S, S), d = img.data;
const cc = smooth((p - .38) / .55);
const milk = .05 * (1 - p) * (1 - p);
for (let i = 0; i < n; i++) {
const rr = smooth((p - tau[i] * .85) / .32), l = L[i], m = 1 + .14 * mot[i];
const or = .1 * m, og = .165 * m, ob = .165 * m;
const gr = l * .93, gg = l, gb = l * .98;
const R = gr + (F[i * 4] / 255 - gr) * cc, G = gg + (F[i * 4 + 1] / 255 - gg) * cc, B = gb + (F[i * 4 + 2] / 255 - gb) * cc;
d[i * 4] = clamp(or + (R - or) * rr + milk) * 255;
d[i * 4 + 1] = clamp(og + (G - og) * rr + milk) * 255;
d[i * 4 + 2] = clamp(ob + (B - ob) * rr + milk) * 255;
d[i * 4 + 3] = 255;
}
ctx.putImageData(img, 0, 0);
stages.push(cv);
}
}
function paper() {
const { cv, ctx } = make(160, 160), img = ctx.createImageData(160, 160);
for (let i = 0; i < img.data.length; i += 4) { const v = 200 + rnd() * 55; img.data[i] = img.data[i + 1] = img.data[i + 2] = v; img.data[i + 3] = 255; }
ctx.putImageData(img, 0, 0);
return cv;
}
function prepare(src) {
develop(film(src, SIZE), SIZE);
shown = -1;
}
let W = 0, H = 0, DPR = 1, pin = { x: 0, y: 0 }, cardH = 0, armLen = 1;
let th = 0, om = 0, progress = 0, pend = 0, shown = -1, held = null, ptr = null, prev = null, pv = null, hintA = 1, saved = 0;
function layout() {
const b = root.getBoundingClientRect(), r = card.getBoundingClientRect();
W = b.width; H = b.height; DPR = Math.min(2, window.devicePixelRatio || 1);
pin = { x: W / 2, y: card.offsetTop };
cardH = card.offsetHeight; armLen = cardH * .55;
const ps = Math.round(photo.offsetWidth * DPR);
photo.width = photo.height = ps; shown = -1;
tex.width = Math.round(card.offsetWidth); tex.height = Math.round(cardH);
const t = tex.getContext('2d'); t.fillStyle = t.createPattern(paperTile, 'repeat'); t.fillRect(0, 0, tex.width, tex.height);
grain.width = Math.round(W / 2); grain.height = Math.round(H / 2);
const gctx = grain.getContext('2d'), gi = gctx.createImageData(grain.width, grain.height);
for (let i = 0; i < gi.data.length; i += 4) { const v = rnd() * 255; gi.data[i] = gi.data[i + 1] = gi.data[i + 2] = v; gi.data[i + 3] = 14; }
gctx.putImageData(gi, 0, 0);
root.querySelector('.pd-line').setAttribute('viewBox', `0 0 ${W} ${H}`);
}
function drawPhoto() {
const f = progress * (STAGES - 1), a = Math.min(STAGES - 2, Math.floor(f)), t = f - a, key = Math.round(f * 200);
if (key === shown || !stages.length) return;
shown = key;
pc.globalAlpha = 1; pc.drawImage(stages[a], 0, 0, photo.width, photo.height);
if (t > 0) { pc.globalAlpha = t; pc.drawImage(stages[a + 1], 0, 0, photo.width, photo.height); pc.globalAlpha = 1; }
}
function drawTwine(view) {
const peg = { x: pin.x + 30 * Math.sin(view), y: pin.y - 30 * Math.cos(view) }, sag = 18, y0 = peg.y - 22;
const d = `M -20 ${y0} Q ${peg.x / 2} ${y0 + sag} ${peg.x} ${peg.y} Q ${(peg.x + W) / 2} ${y0 + sag} ${W + 20} ${y0}`;
twine.setAttribute('d', d);
twineShade.setAttribute('d', d); twineShade.setAttribute('transform', 'translate(0 7)');
}
function grab(e) {
const b = root.getBoundingClientRect(), x = e.clientX - b.left, y = e.clientY - b.top;
held = { id: e.pointerId, off: Math.atan2(x - pin.x, y - pin.y) + th };
ptr = { x, y }; prev = null; pv = null;
try { card.setPointerCapture(e.pointerId); } catch (err) { }
}
card.addEventListener('pointerdown', e => { if (e.button === 0 || e.pointerType !== 'mouse') grab(e); });
root.addEventListener('pointermove', e => {
if (!held || e.pointerId !== held.id) return;
const b = root.getBoundingClientRect();
ptr = { x: e.clientX - b.left, y: e.clientY - b.top };
});
const letGo = e => { if (held && e.pointerId === held.id) held = null; };
root.addEventListener('pointerup', letGo);
root.addEventListener('pointercancel', letGo);
card.addEventListener('keydown', e => {
const dir = e.key === 'ArrowLeft' ? -1 : e.key === 'ArrowRight' ? 1 : e.key === ' ' ? (th > 0 ? -1 : 1) : 0;
if (!dir) return;
e.preventDefault();
if (!calm.matches) om += dir * 2.4;
pend += .07 * DEVELOP;
});
save.addEventListener('click', () => {
const k = 2, w = Math.round(card.offsetWidth * k), h = Math.round(cardH * k), { cv, ctx } = make(w, h);
ctx.fillStyle = '#F2EFE7'; ctx.fillRect(0, 0, w, h);
ctx.globalAlpha = .5; ctx.globalCompositeOperation = 'multiply'; ctx.fillStyle = ctx.createPattern(paperTile, 'repeat'); ctx.fillRect(0, 0, w, h);
ctx.globalAlpha = 1; ctx.globalCompositeOperation = 'source-over';
const s = w * 79 / 88;
ctx.drawImage(photo, w * 4.5 / 88, w * 6 / 88, s, s);
try {
const a = document.createElement('a');
a.href = cv.toDataURL('image/png');
a.download = 'polaroid.png';
document.body.append(a); a.click(); a.remove();
label.textContent = 'Saved';
} catch (err) {
label.textContent = 'Open it from a website to save';
}
saved = performance.now();
});
function frame(now) {
const dt = Math.min(.05, frame.t ? (now - frame.t) / 1000 : .016);
frame.t = now;
const still = calm.matches, w2 = (2 * Math.PI / SWING) ** 2;
// a shake is a change of direction: the hand's velocity jumps, a calm drag barely changes it
if (held && ptr) {
if (prev) {
const vx = (ptr.x - prev.x) / dt, vy = (ptr.y - prev.y) / dt;
if (pv) {
const dv = Math.hypot(vx - pv.x, vy - pv.y);
if (dv > 140) pend += dv * 1e-5 * DEVELOP;
}
pv = { x: vx, y: vy };
}
prev = { x: ptr.x, y: ptr.y };
}
// the chemistry catches up for a moment after the shaking stops
const take = pend * (1 - Math.exp(-dt / .35));
pend -= take;
progress = Math.min(1, progress + take);
// the photo hangs on its peg: held, it follows the hand; let go, it swings and settles
const steps = Math.ceil(dt / .006), h = dt / steps;
for (let s = 0; s < steps; s++) {
if (still) { th = 0; om = 0; break; }
if (held && ptr) {
let goal = held.off - Math.atan2(ptr.x - pin.x, ptr.y - pin.y);
goal = Math.atan2(Math.sin(goal - th), Math.cos(goal - th)) + th;
om += (900 * (goal - th) - 60 * om) * h;
} else {
om += (-w2 * Math.sin(th) - SETTLE * om) * h;
}
th += om * h;
}
const view = still ? 0 : th + .004 * Math.sin(now / 1000 * 1.3);
card.style.transform = `rotate(${view.toFixed(4)}rad)`;
face.style.setProperty('--gx', `${(50 + view * 260).toFixed(1)}%`);
drawTwine(view);
drawPhoto();
hintA += ((progress > .04 || held ? 0 : 1) - hintA) * (1 - Math.exp(-dt * 6));
hint.style.opacity = hintA.toFixed(3);
if (saved && now - saved > 1400) { saved = 0; label.textContent = 'Save photo'; }
requestAnimationFrame(frame);
}
const paperTile = paper();
new ResizeObserver(() => { layout(); }).observe(root);
layout();
prepare(lake(SIZE));
if (PHOTO) { const img = new Image(); img.onload = () => prepare(cover(img, SIZE)); img.src = PHOTO; }
requestAnimationFrame(frame);
})();
</script>
Схватите снимок и потрясите из стороны в сторону чтобы проявить фото, затем отпустите.
Что можно поменять
Поменяйте число, сохраните файл и обновите страницу, разница будет видна сразу.
DEVELOP1Насколько сильно каждое встряхивание проявляет снимок. Значение 2 ускоряет вдвое.
SWING1.9Длительность одного качания подвешенного снимка в секундах.
SETTLE1.5Как быстро затухает раскачивание. Больше значит остановится быстрее.
Как поставить на свой сайт
Отлично подходит для портфолио, показа работ, промо-кампаний и страниц о студии.
Комментарии в коде на английском, так файл понятен любому разработчику.
CSS
Вставьте в свой файл стилей или в тег <style> внутри <head>.
/* Polaroid Develop by @codinvisual · t.me/CodinVisual · page shell, only needed while this file is the whole page */HTML
Поставьте в то место страницы, где должен быть эффект.
<!-- polaroid start --> <div class="pd"> <canvas class="pd-grain" aria-hidden="true"></canvas> <svg class="pd-line" aria-hidden="true"><path fill="none" stroke="#CDB894" stroke-opacity=".75" stroke-width="1.6" stroke-linecap="round"/><path fill="none" stroke="#000" stroke-opacity=".35" stroke-width="1.6" stroke-linecap="round"/></svg> <div class="pd-card" tabindex="0" role="button" aria-label="Instant photo of a lake at dusk. Grab it and shake it to develop, or use the arrow keys."> <canvas class="pd-tex" aria-hidden="true"></canvas> <canvas class="pd-photo" aria-hidden="true"></canvas> <div class="pd-face" aria-hidden="true"></div> <svg class="pd-peg" viewBox="0 0 18 58" aria-hidden="true"> <defs> <linearGradient id="pd-wood" x1="0" x2="1"><stop offset="0" stop-color="#8E6A45"/><stop offset=".45" stop-color="#C9A57A"/><stop offset="1" stop-color="#9A764F"/></linearGradient> <linearGradient id="pd-steel" x1="0" x2="0" y1="0" y2="1"><stop offset="0" stop-color="#9AA0A6"/><stop offset=".5" stop-color="#E4E7EA"/><stop offset="1" stop-color="#5F656B"/></linearGradient> </defs> <rect x="2" y="0" width="14" height="58" rx="3" fill="url(#pd-wood)"/> <path d="M9 2 V56" stroke="#5E4329" stroke-opacity=".55" stroke-width=".8"/> <path d="M3 9 Q9 7 15 9 M3 20 Q9 18.5 15 20 M3 44 Q9 42.5 15 44" stroke="#6E5236" stroke-opacity=".25" stroke-width=".6" fill="none"/> <rect x="0" y="23" width="18" height="7" rx="3.5" fill="url(#pd-steel)"/> <path d="M2 26.5 H16" stroke="#3E4348" stroke-opacity=".5" stroke-width=".6"/> </svg> </div> <button class="pd-save" type="button"><span>Save photo</span><svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true"><path d="M12 4v13M6.5 12l5.5 5.5 5.5-5.5M5 20.5h14" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round"/></svg></button> <p class="pd-hint" aria-hidden="true">Grab the photo and shake it</p> </div> <!-- polaroid end -->JavaScript
Вставьте перед закрывающим тегом </body>, внутри <script></script>.
/* Polaroid Develop by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */ (() => { const DEVELOP = 1; const SWING = 1.9; const SETTLE = 1.5; const PHOTO = ''; const root = document.querySelector('.pd'); if (!root) return; const card = root.querySelector('.pd-card'), photo = root.querySelector('.pd-photo'), face = root.querySelector('.pd-face'); const tex = root.querySelector('.pd-tex'), grain = root.querySelector('.pd-grain'), hint = root.querySelector('.pd-hint'); const save = root.querySelector('.pd-save'), label = save.querySelector('span'), [twine, twineShade] = root.querySelectorAll('.pd-line path'); const calm = matchMedia('(prefers-reduced-motion: reduce)'); const pc = photo.getContext('2d'); const SIZE = 720, STAGES = 9; const clamp = (v, a = 0, b = 1) => v < a ? a : v > b ? b : v; const smooth = t => (t = clamp(t), t * t * (3 - 2 * t)); function make(w, h) { const cv = document.createElement('canvas'); cv.width = w; cv.height = h; return { cv, ctx: cv.getContext('2d') }; } let seed = 1016; const rnd = () => { seed = seed + 0x6D2B79F5 | 0; let t = Math.imul(seed ^ seed >>> 15, 1 | seed); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; function hash(a, b) { let n = Math.imul(a, 374761393) + Math.imul(b, 668265263) | 0; n = Math.imul(n ^ n >>> 13, 1274126177); return ((n ^ n >>> 16) >>> 0) / 4294967295; } function noise(x, y) { const xi = Math.floor(x), yi = Math.floor(y), u = smooth(x - xi), v = smooth(y - yi); return (hash(xi, yi) * (1 - u) + hash(xi + 1, yi) * u) * (1 - v) + (hash(xi, yi + 1) * (1 - u) + hash(xi + 1, yi + 1) * u) * v; } // the picture: a lake at dusk, drawn once function pine(x, cx, base, h, w) { const tiers = 7 + (rnd() * 4 | 0); x.beginPath(); x.moveTo(cx, base - h); for (let i = 1; i <= tiers; i++) { const y = base - h + h * i / tiers * .9, tw = w * (.25 + .75 * i / tiers) * (.8 + rnd() * .45); x.lineTo(cx + tw, y); x.lineTo(cx + tw * .3, y - h / tiers * .12); } x.lineTo(cx + w * .07, base); x.lineTo(cx - w * .07, base); for (let i = tiers; i >= 1; i--) { const y = base - h + h * i / tiers * .9, tw = w * (.25 + .75 * i / tiers) * (.8 + rnd() * .45); x.lineTo(cx - tw * .3, y - h / tiers * .12); x.lineTo(cx - tw, y); } x.closePath(); x.fill(); } function ridge(x, S, top, amp, color, k) { x.fillStyle = color; x.beginPath(); x.moveTo(0, S); for (let i = 0; i <= 64; i++) { const u = i / 64, y = top - amp * (.55 * Math.sin(u * 5.1 + k) + .3 * Math.sin(u * 11.7 + k * 2.3) + .15 * Math.sin(u * 23 + k * 4.1)); x.lineTo(u * S, y); } x.lineTo(S, S); x.closePath(); x.fill(); } function light(x, px, py, r, rgb, a) { const g = x.createRadialGradient(px, py, 0, px, py, r); g.addColorStop(0, `rgba(${rgb},${a})`); g.addColorStop(1, `rgba(${rgb},0)`); x.fillStyle = g; x.fillRect(px - r, py - r, r * 2, r * 2); } function lake(S) { const { cv, ctx: x } = make(S, S), H = Math.round(S * .52), u = S / 900; seed = 1016; const sky = x.createLinearGradient(0, 0, 0, H); sky.addColorStop(0, '#0A1120'); sky.addColorStop(.36, '#17203A'); sky.addColorStop(.66, '#33304B'); sky.addColorStop(.88, '#6E4C4E'); sky.addColorStop(1, '#B06E4B'); x.fillStyle = sky; x.fillRect(0, 0, S, H + 2); x.globalCompositeOperation = 'lighter'; light(x, S * .72, H, S * .6, '255,165,105', .24); for (let i = 0; i < 52; i++) { const a = .12 + rnd() * .45; x.fillStyle = `rgba(230,236,255,${a})`; x.beginPath(); x.arc(rnd() * S, rnd() * H * .5, (.5 + rnd() * .7) * u, 0, 7); x.fill(); } x.globalCompositeOperation = 'source-over'; ridge(x, S, H - 78 * u, 34 * u, '#2D2E45', 1.3); ridge(x, S, H - 34 * u, 18 * u, '#1E1F2E', 4.2); // low mist over the far water const mist = x.createLinearGradient(0, H - 40 * u, 0, H + 4 * u); mist.addColorStop(0, 'rgba(190,170,200,0)'); mist.addColorStop(.7, 'rgba(190,170,200,.12)'); mist.addColorStop(1, 'rgba(190,170,200,0)'); x.fillStyle = mist; x.fillRect(0, H - 40 * u, S, 44 * u); // the far shore on the right: low spruce and a few lights x.fillStyle = '#0D1018'; x.fillRect(S * .46, H - 4 * u, S * .6, 5 * u); for (let px = S * .46; px < S + 10 * u; px += (6 + rnd() * 5) * u) { const h = (12 + 20 * noise(px / (70 * u), 3) + rnd() * 8) * u; pine(x, px, H + 1, h, h * .3); } // the near shore on the left rises out of the water, the cabin stands on it among the spruce const land = y => H - 4 * u - 46 * u * smooth(1 - y / (S * .55)); x.fillStyle = '#090B11'; x.beginPath(); x.moveTo(0, H + 2 * u); for (let i = 0; i <= 40; i++) { const px = i / 40 * S * .58; x.lineTo(px, land(px)); } x.lineTo(S * .58, H + 2 * u); x.closePath(); x.fill(); for (let px = -8 * u; px < S * .56; px += (8 + rnd() * 9) * u) { if (px > S * .2 && px < S * .36) continue; const h = (38 + 70 * noise(px / (80 * u), 7) + rnd() * 24) * u * (1 - px / (S * 1.1)); pine(x, px, land(px) + 3 * u, h, h * .3); } const cx = S * .28, base = land(cx) + 2 * u, cw = 92 * u, ch = 48 * u; x.fillStyle = '#07090D'; x.fillRect(cx - cw / 2, base - ch, cw, ch); x.beginPath(); x.moveTo(cx - cw / 2 - 9 * u, base - ch); x.lineTo(cx - 6 * u, base - ch - 38 * u); x.lineTo(cx + cw / 2 + 9 * u, base - ch); x.closePath(); x.fill(); x.fillRect(cx + 16 * u, base - ch - 40 * u, 9 * u, 24 * u); for (const px of [cx - 64 * u, cx + 70 * u]) pine(x, px, base + 2 * u, 120 * u, 34 * u); const wx = cx + 8 * u, wy = base - ch + 14 * u, ww = 22 * u, wh = 20 * u; x.fillStyle = '#FFB966'; x.fillRect(wx, wy, ww, wh); x.fillStyle = '#FFE0AE'; x.fillRect(wx + 3 * u, wy + 3 * u, ww - 6 * u, wh - 6 * u); x.fillStyle = '#3A2414'; x.fillRect(wx + ww / 2 - 1 * u, wy, 2 * u, wh); x.fillRect(wx, wy + wh / 2 - 1 * u, ww, 2 * u); x.globalCompositeOperation = 'lighter'; light(x, wx + ww / 2, wy + wh / 2, 70 * u, '255,170,90', .5); light(x, wx + ww / 2, wy + wh / 2, 190 * u, '255,150,80', .14); light(x, wx + ww / 2, base + 6 * u, 90 * u, '255,160,90', .1); const lamps = [[.6, 2.2, '255,206,150'], [.66, 1.5, '255,226,190'], [.72, 2, '255,196,130'], [.8, 1.3, '230,236,255'], [.86, 2.1, '255,210,160'], [.93, 1.4, '255,226,190']]; for (const [lx, r, rgb] of lamps) { light(x, lx * S, H - 3 * u, r * 3.6 * u, rgb, .5); x.fillStyle = `rgba(${rgb},.95)`; x.beginPath(); x.arc(lx * S, H - 3 * u, r * .55 * u, 0, 7); x.fill(); } x.globalCompositeOperation = 'source-over'; // the water mirrors everything above it, rippled more the closer it is for (let y = H; y < S; y++) { const d = y - H, src = H - d - 1; if (src < 0) break; const amp = (1 + d * .04) * u, dx = Math.sin(y * .83) * amp * .6 + Math.sin(y * .19 + 1.7) * amp; x.drawImage(cv, 0, src, S, 1, dx, y, S, 1); } const deep = x.createLinearGradient(0, H, 0, S); deep.addColorStop(0, 'rgba(6,10,20,.28)'); deep.addColorStop(1, 'rgba(3,5,10,.7)'); x.fillStyle = deep; x.fillRect(0, H, S, S - H); // the window's light runs across the water toward us in broken streaks x.globalCompositeOperation = 'lighter'; const streak = (sx, rgb, a0, len, w0) => { for (let k = 0; k < len; k++) { const y = H + (3 + k * 6.5) * u, f = 1 - k / len, w = (w0 * (.55 + .45 * f) + rnd() * 10) * u; x.fillStyle = `rgba(${rgb},${(a0 * f * (.45 + rnd() * .55)).toFixed(3)})`; x.fillRect(sx - w / 2 + (rnd() - .5) * 8 * u, y, w, (1.3 + rnd() * 1.5) * u); } }; streak(wx + ww / 2, '255,178,96', .7, 52, 26); for (const [lx, , rgb] of lamps) streak(lx * S, rgb, .3, 20, 12); x.fillStyle = 'rgba(255,190,140,.10)'; x.fillRect(S * .5, H, S * .5, 1.5 * u); x.globalCompositeOperation = 'source-over'; // two spruces on our side of the lake, their feet below the frame x.fillStyle = '#05070B'; pine(x, S * .95, S + 60 * u, 560 * u, 78 * u); pine(x, S * 1.03, S + 40 * u, 640 * u, 92 * u); return cv; } function cover(img, S) { const { cv, ctx } = make(S, S), k = Math.max(S / img.naturalWidth, S / img.naturalHeight); const w = img.naturalWidth * k, h = img.naturalHeight * k; ctx.drawImage(img, (S - w) / 2, (S - h) / 2, w, h); return cv; } // the look of instant film: soft lens, lifted blacks, teal shadows, warm highlights, grain function film(src, S) { const { cv, ctx } = make(S, S), small = make(S * .5 | 0, S * .5 | 0); small.ctx.drawImage(src, 0, 0, small.cv.width, small.cv.height); ctx.drawImage(src, 0, 0); ctx.globalAlpha = .45; ctx.drawImage(small.cv, 0, 0, S, S); ctx.globalAlpha = 1; const img = ctx.getImageData(0, 0, S, S), d = img.data; for (let y = 0, i = 0; y < S; y++) for (let x = 0; x < S; x++, i += 4) { let r = d[i] / 255, g = d[i + 1] / 255, b = d[i + 2] / 255; const l = .2126 * r + .7152 * g + .0722 * b, sh = 1 - smooth(l / .45), hi = smooth((l - .5) / .5); r = .04 + .93 * r - .02 * sh + .03 * hi; g = .05 + .92 * g + .012 * sh; b = .06 + .9 * b + .02 * sh - .025 * hi; const vx = x / S - .5, vy = y / S - .5, v = 1 - .55 * (vx * vx + vy * vy) * 2, n = (rnd() - .5) * .05; d[i] = clamp(r * v + n) * 255; d[i + 1] = clamp(g * v + n) * 255; d[i + 2] = clamp(b * v + n) * 255; } ctx.putImageData(img, 0, 0); return cv; } // a fresh print is a dark blue-green layer that clears: the dark shapes show first, the lights and the colour later let stages = []; function develop(finalCv, S) { const F = finalCv.getContext('2d').getImageData(0, 0, S, S).data, n = S * S; const L = new Float32Array(n), tau = new Float32Array(n), mot = new Float32Array(n); for (let y = 0, i = 0; y < S; y++) for (let x = 0; x < S; x++, i++) { const r = F[i * 4] / 255, g = F[i * 4 + 1] / 255, b = F[i * 4 + 2] / 255; L[i] = .2126 * r + .7152 * g + .0722 * b; // the reagent pod sits under the wide bottom edge, so the print clears a touch sooner from below const late = .62 * Math.pow(L[i], .8); tau[i] = .08 + late + .1 * (noise(x / 70, y / 70) - .5) - .05 * (y / S); mot[i] = noise(x / 26 + 9, y / 26) - .5; } stages = []; for (let k = 0; k < STAGES; k++) { const p = k / (STAGES - 1), { cv, ctx } = make(S, S), img = ctx.createImageData(S, S), d = img.data; const cc = smooth((p - .38) / .55); const milk = .05 * (1 - p) * (1 - p); for (let i = 0; i < n; i++) { const rr = smooth((p - tau[i] * .85) / .32), l = L[i], m = 1 + .14 * mot[i]; const or = .1 * m, og = .165 * m, ob = .165 * m; const gr = l * .93, gg = l, gb = l * .98; const R = gr + (F[i * 4] / 255 - gr) * cc, G = gg + (F[i * 4 + 1] / 255 - gg) * cc, B = gb + (F[i * 4 + 2] / 255 - gb) * cc; d[i * 4] = clamp(or + (R - or) * rr + milk) * 255; d[i * 4 + 1] = clamp(og + (G - og) * rr + milk) * 255; d[i * 4 + 2] = clamp(ob + (B - ob) * rr + milk) * 255; d[i * 4 + 3] = 255; } ctx.putImageData(img, 0, 0); stages.push(cv); } } function paper() { const { cv, ctx } = make(160, 160), img = ctx.createImageData(160, 160); for (let i = 0; i < img.data.length; i += 4) { const v = 200 + rnd() * 55; img.data[i] = img.data[i + 1] = img.data[i + 2] = v; img.data[i + 3] = 255; } ctx.putImageData(img, 0, 0); return cv; } function prepare(src) { develop(film(src, SIZE), SIZE); shown = -1; } let W = 0, H = 0, DPR = 1, pin = { x: 0, y: 0 }, cardH = 0, armLen = 1; let th = 0, om = 0, progress = 0, pend = 0, shown = -1, held = null, ptr = null, prev = null, pv = null, hintA = 1, saved = 0; function layout() { const b = root.getBoundingClientRect(), r = card.getBoundingClientRect(); W = b.width; H = b.height; DPR = Math.min(2, window.devicePixelRatio || 1); pin = { x: W / 2, y: card.offsetTop }; cardH = card.offsetHeight; armLen = cardH * .55; const ps = Math.round(photo.offsetWidth * DPR); photo.width = photo.height = ps; shown = -1; tex.width = Math.round(card.offsetWidth); tex.height = Math.round(cardH); const t = tex.getContext('2d'); t.fillStyle = t.createPattern(paperTile, 'repeat'); t.fillRect(0, 0, tex.width, tex.height); grain.width = Math.round(W / 2); grain.height = Math.round(H / 2); const gctx = grain.getContext('2d'), gi = gctx.createImageData(grain.width, grain.height); for (let i = 0; i < gi.data.length; i += 4) { const v = rnd() * 255; gi.data[i] = gi.data[i + 1] = gi.data[i + 2] = v; gi.data[i + 3] = 14; } gctx.putImageData(gi, 0, 0); root.querySelector('.pd-line').setAttribute('viewBox', `0 0 ${W} ${H}`); } function drawPhoto() { const f = progress * (STAGES - 1), a = Math.min(STAGES - 2, Math.floor(f)), t = f - a, key = Math.round(f * 200); if (key === shown || !stages.length) return; shown = key; pc.globalAlpha = 1; pc.drawImage(stages[a], 0, 0, photo.width, photo.height); if (t > 0) { pc.globalAlpha = t; pc.drawImage(stages[a + 1], 0, 0, photo.width, photo.height); pc.globalAlpha = 1; } } function drawTwine(view) { const peg = { x: pin.x + 30 * Math.sin(view), y: pin.y - 30 * Math.cos(view) }, sag = 18, y0 = peg.y - 22; const d = `M -20 ${y0} Q ${peg.x / 2} ${y0 + sag} ${peg.x} ${peg.y} Q ${(peg.x + W) / 2} ${y0 + sag} ${W + 20} ${y0}`; twine.setAttribute('d', d); twineShade.setAttribute('d', d); twineShade.setAttribute('transform', 'translate(0 7)'); } function grab(e) { const b = root.getBoundingClientRect(), x = e.clientX - b.left, y = e.clientY - b.top; held = { id: e.pointerId, off: Math.atan2(x - pin.x, y - pin.y) + th }; ptr = { x, y }; prev = null; pv = null; try { card.setPointerCapture(e.pointerId); } catch (err) { } } card.addEventListener('pointerdown', e => { if (e.button === 0 || e.pointerType !== 'mouse') grab(e); }); root.addEventListener('pointermove', e => { if (!held || e.pointerId !== held.id) return; const b = root.getBoundingClientRect(); ptr = { x: e.clientX - b.left, y: e.clientY - b.top }; }); const letGo = e => { if (held && e.pointerId === held.id) held = null; }; root.addEventListener('pointerup', letGo); root.addEventListener('pointercancel', letGo); card.addEventListener('keydown', e => { const dir = e.key === 'ArrowLeft' ? -1 : e.key === 'ArrowRight' ? 1 : e.key === ' ' ? (th > 0 ? -1 : 1) : 0; if (!dir) return; e.preventDefault(); if (!calm.matches) om += dir * 2.4; pend += .07 * DEVELOP; }); save.addEventListener('click', () => { const k = 2, w = Math.round(card.offsetWidth * k), h = Math.round(cardH * k), { cv, ctx } = make(w, h); ctx.fillStyle = '#F2EFE7'; ctx.fillRect(0, 0, w, h); ctx.globalAlpha = .5; ctx.globalCompositeOperation = 'multiply'; ctx.fillStyle = ctx.createPattern(paperTile, 'repeat'); ctx.fillRect(0, 0, w, h); ctx.globalAlpha = 1; ctx.globalCompositeOperation = 'source-over'; const s = w * 79 / 88; ctx.drawImage(photo, w * 4.5 / 88, w * 6 / 88, s, s); try { const a = document.createElement('a'); a.href = cv.toDataURL('image/png'); a.download = 'polaroid.png'; document.body.append(a); a.click(); a.remove(); label.textContent = 'Saved'; } catch (err) { label.textContent = 'Open it from a website to save'; } saved = performance.now(); }); function frame(now) { const dt = Math.min(.05, frame.t ? (now - frame.t) / 1000 : .016); frame.t = now; const still = calm.matches, w2 = (2 * Math.PI / SWING) ** 2; // a shake is a change of direction: the hand's velocity jumps, a calm drag barely changes it if (held && ptr) { if (prev) { const vx = (ptr.x - prev.x) / dt, vy = (ptr.y - prev.y) / dt; if (pv) { const dv = Math.hypot(vx - pv.x, vy - pv.y); if (dv > 140) pend += dv * 1e-5 * DEVELOP; } pv = { x: vx, y: vy }; } prev = { x: ptr.x, y: ptr.y }; } // the chemistry catches up for a moment after the shaking stops const take = pend * (1 - Math.exp(-dt / .35)); pend -= take; progress = Math.min(1, progress + take); // the photo hangs on its peg: held, it follows the hand; let go, it swings and settles const steps = Math.ceil(dt / .006), h = dt / steps; for (let s = 0; s < steps; s++) { if (still) { th = 0; om = 0; break; } if (held && ptr) { let goal = held.off - Math.atan2(ptr.x - pin.x, ptr.y - pin.y); goal = Math.atan2(Math.sin(goal - th), Math.cos(goal - th)) + th; om += (900 * (goal - th) - 60 * om) * h; } else { om += (-w2 * Math.sin(th) - SETTLE * om) * h; } th += om * h; } const view = still ? 0 : th + .004 * Math.sin(now / 1000 * 1.3); card.style.transform = `rotate(${view.toFixed(4)}rad)`; face.style.setProperty('--gx', `${(50 + view * 260).toFixed(1)}%`); drawTwine(view); drawPhoto(); hintA += ((progress > .04 || held ? 0 : 1) - hintA) * (1 - Math.exp(-dt * 6)); hint.style.opacity = hintA.toFixed(3); if (saved && now - saved > 1400) { saved = 0; label.textContent = 'Save photo'; } requestAnimationFrame(frame); } const paperTile = paper(); new ResizeObserver(() => { layout(); }).observe(root); layout(); prepare(lake(SIZE)); if (PHOTO) { const img = new Image(); img.onload = () => prepare(cover(img, SIZE)); img.src = PHOTO; } requestAnimationFrame(frame); })();
Подключение к серверу
Укажите свой URL картинки в PHOTO. Сохранение кадра работает при открытии через HTTP.
Полезно знать
- Учитывает настройку reduced motion: убирает раскачивание, но оставляет проявку.
Tilda, Webflow, WordPress
Нажмите «Скопировать весь код» вверху и вставьте его в один HTML-блок.
Добавить блок, Другое, T123 HTML-код. Вставьте и сохраните. Проверяйте на опубликованной странице, в редакторе скрипты не запускаются.
Панель Add, Embed. Вставьте и нажмите Save & Close. Свой код работает на опубликованном сайте.
Добавить блок, Произвольный HTML. Вставьте и нажмите «Обновить».



