Chladni Sand
Sixteen thousand grains of sand resting on a vibrating steel plate. Tap or turn the dial to step through resonant frequencies as the sand leaps off shaking zones and settles into crisp nodal geometry.
- Particles & fun
- Free
- canvas
- physics
- particles
- Added Oct 16, 2026
<style>
/* Chladni Sand by @codinvisual · t.me/CodinVisual · page shell, only needed while this file is the whole page */
</style>
<!-- plate start -->
<div class="cs">
<canvas class="cs-grain" aria-hidden="true"></canvas>
<div class="cs-plate" aria-hidden="true"><canvas class="cs-steel"></canvas><canvas class="cs-sand"></canvas></div>
<div class="cs-side">
<button class="cs-knob" type="button" aria-label="Frequency knob. Tap for the next pattern, or use the arrow keys.">
<svg class="cs-ticks" viewBox="0 0 136 136" aria-hidden="true"></svg>
<span class="cs-dial" aria-hidden="true"></span>
</button>
<p class="cs-mode" aria-live="polite">Off</p>
<button class="cs-sound" type="button" aria-pressed="false"><svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path d="M4 9.5h3.5L12 5.5v13L7.5 14.5H4z" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"/><path class="cs-wave" d="M15.5 9a4 4 0 0 1 0 6M18 6.5a7.5 7.5 0 0 1 0 11" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg><span>Sound</span></button>
</div>
<p class="cs-hint" aria-hidden="true">Tap the knob</p>
</div>
<!-- plate end -->
<script>
/* Chladni Sand by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */
(() => {
const GRAINS = 16000;
const JUMP = 0.07;
const SETTLE = 0.8;
const MODES = [[2, 3, 1], [3, 4, -1], [4, 5, 1], [5, 6, -1]];
const root = document.querySelector('.cs');
if (!root) return;
const plate = root.querySelector('.cs-plate'), steel = root.querySelector('.cs-steel'), sand = root.querySelector('.cs-sand');
const knob = root.querySelector('.cs-knob'), dial = root.querySelector('.cs-dial'), ticks = root.querySelector('.cs-ticks');
const label = root.querySelector('.cs-mode'), soundBtn = root.querySelector('.cs-sound'), hint = root.querySelector('.cs-hint'), grain = root.querySelector('.cs-grain');
const calm = matchMedia('(prefers-reduced-motion: reduce)');
const ctx = sand.getContext('2d'), page = document.createElement('canvas'), pctx = page.getContext('2d');
const halo = document.createElement('canvas'), hctx = halo.getContext('2d');
const { cos, PI } = Math;
let seed = 1017;
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;
};
// grains live in plate units, 0 to 1 across; at first the sand is simply poured on
const gx = new Float32Array(GRAINS), gy = new Float32Array(GRAINS), lift = new Float32Array(GRAINS), tone = new Float32Array(GRAINS);
for (let i = 0; i < GRAINS; i++) { gx[i] = .03 + rnd() * .94; gy[i] = .03 + rnd() * .94; tone[i] = .75 + rnd() * .25; }
let mode = -1, n = 0, m = 0, sign = 1, shake = 0, turn = -135, spin = 0, S = 0, DPR = 1, img = null, buf = null, tapped = false;
// the standing wave: where it is zero the plate does not move, and that is where the sand stays
function wave(x, y) {
const a = cos(n * PI * x) * cos(m * PI * y);
const b = cos(m * PI * x) * cos(n * PI * y);
return a + sign * b;
}
// brushed steel, drawn once: fine strokes, a broad highlight, a bevel, the bolt in the middle
function paintSteel() {
const c = steel.getContext('2d'), w = steel.width, s = w / 560;
const base = c.createLinearGradient(0, 0, w, w);
base.addColorStop(0, '#2C2E33'); base.addColorStop(.5, '#1D1F23'); base.addColorStop(1, '#141518');
c.fillStyle = base; c.fillRect(0, 0, w, w);
for (let i = 0; i < 2600; i++) {
const y = rnd() * w, x = rnd() * w, len = (40 + rnd() * 220) * s, a = rnd() * .05;
c.strokeStyle = rnd() < .5 ? `rgba(255,255,255,${a})` : `rgba(0,0,0,${a * 1.4})`;
c.lineWidth = (.5 + rnd()) * s; c.beginPath(); c.moveTo(x, y); c.lineTo(x + len, y + (rnd() - .5) * 2 * s); c.stroke();
}
const sheen = c.createLinearGradient(0, 0, w, w * .4);
sheen.addColorStop(0, 'rgba(255,240,220,0)'); sheen.addColorStop(.45, 'rgba(255,240,220,.07)'); sheen.addColorStop(.6, 'rgba(255,240,220,0)');
c.fillStyle = sheen; c.fillRect(0, 0, w, w);
const warm = c.createRadialGradient(w * .78, w * .2, 0, w * .78, w * .2, w * .9);
warm.addColorStop(0, 'rgba(255,200,140,.10)'); warm.addColorStop(1, 'rgba(255,200,140,0)');
c.fillStyle = warm; c.fillRect(0, 0, w, w);
c.lineWidth = 3 * s; c.strokeStyle = 'rgba(255,255,255,.14)'; c.strokeRect(1.5 * s, 1.5 * s, w - 3 * s, w - 3 * s);
c.lineWidth = 2 * s; c.strokeStyle = 'rgba(0,0,0,.5)'; c.strokeRect(4 * s, 4 * s, w - 8 * s, w - 8 * s);
const bolt = c.createRadialGradient(w * .5 - 4 * s, w * .5 - 5 * s, 1, w * .5, w * .5, 15 * s);
bolt.addColorStop(0, '#6A6D73'); bolt.addColorStop(.6, '#33353A'); bolt.addColorStop(1, '#141518');
c.fillStyle = bolt; c.beginPath(); c.arc(w * .5, w * .5, 15 * s, 0, 7); c.fill();
c.strokeStyle = 'rgba(0,0,0,.6)'; c.lineWidth = 2 * s; c.beginPath(); c.moveTo(w * .5 - 8 * s, w * .5); c.lineTo(w * .5 + 8 * s, w * .5); c.stroke();
}
function paintTicks() {
const count = MODES.length + 1;
let out = '';
for (let k = 0; k < count; k++) {
const a = (-135 + k * 270 / (count - 1)) * PI / 180, r0 = 62, r1 = k ? 67 : 65;
out += `<line x1="${68 + Math.sin(a) * r0}" y1="${68 - cos(a) * r0}" x2="${68 + Math.sin(a) * r1}" y2="${68 - cos(a) * r1}" stroke="${k === mode + 1 ? '#FFD9A6' : 'rgba(238,234,228,.35)'}" stroke-width="2" stroke-linecap="round"/>`;
}
ticks.innerHTML = out;
}
function layout() {
const w = plate.clientWidth;
if (!w || w === S) return;
S = w; DPR = Math.min(2, window.devicePixelRatio || 1);
const px = Math.round(S * DPR);
steel.width = steel.height = px; sand.width = sand.height = px; page.width = page.height = px;
halo.width = halo.height = Math.ceil(px / 4);
img = pctx.createImageData(px, px); buf = img.data;
seed = 1017; paintSteel();
grain.width = Math.round(root.clientWidth / 2); grain.height = Math.round(root.clientHeight / 2);
const gc = grain.getContext('2d'), gi = gc.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] = 12; }
gc.putImageData(gi, 0, 0);
}
// a tone for the pattern, only after somebody turned the sound on
let audio = null, soundOn = false;
function ring() {
if (!soundOn) return;
audio = audio || new (window.AudioContext || window.webkitAudioContext)();
const t0 = audio.currentTime, o = audio.createOscillator(), g = audio.createGain();
o.type = 'sine'; o.frequency.value = 180 * Math.sqrt(n * n + m * m);
g.gain.setValueAtTime(0, t0); g.gain.linearRampToValueAtTime(.12, t0 + .03); g.gain.exponentialRampToValueAtTime(.0001, t0 + SETTLE * 3);
o.connect(g).connect(audio.destination); o.start(t0); o.stop(t0 + SETTLE * 3 + .1);
}
function go(next) {
mode = (next + MODES.length) % MODES.length;
[n, m, sign] = MODES[mode];
shake = 1; tapped = true;
label.textContent = `Pattern ${n} × ${m}`;
paintTicks(); ring();
}
// the knob: a tap moves on by one mark, a drag around it picks the nearest mark
let drag = null;
const angleAt = e => { const r = knob.getBoundingClientRect(); return Math.atan2(e.clientX - (r.left + r.width / 2), -(e.clientY - (r.top + r.height / 2))) * 180 / PI; };
knob.addEventListener('pointerdown', e => { drag = { id: e.pointerId, a0: angleAt(e), moved: false }; try { knob.setPointerCapture(e.pointerId); } catch (err) { } });
knob.addEventListener('pointermove', e => {
if (!drag || e.pointerId !== drag.id) return;
const a = angleAt(e);
if (Math.abs(a - drag.a0) > 14) drag.moved = true;
if (drag.moved) {
const k = Math.round((Math.max(-135, Math.min(135, a)) + 135) / (270 / MODES.length));
if (k - 1 !== mode && k > 0) go(k - 1);
}
});
const release = e => {
if (!drag || e.pointerId !== drag.id) return;
const moved = drag.moved; drag = null;
if (!moved) go(mode + 1);
};
knob.addEventListener('pointerup', release);
knob.addEventListener('pointercancel', () => { drag = null; });
knob.addEventListener('keydown', e => {
if (e.key === 'ArrowRight' || e.key === 'ArrowUp') { e.preventDefault(); go(mode + 1); }
if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') { e.preventDefault(); go(Math.max(0, mode - 1)); }
});
knob.addEventListener('click', e => { if (e.detail === 0) go(mode + 1); });
soundBtn.addEventListener('click', () => {
soundOn = !soundOn;
soundBtn.setAttribute('aria-pressed', String(soundOn));
if (soundOn) { audio = audio || new (window.AudioContext || window.webkitAudioContext)(); audio.resume(); }
});
function frame(now) {
const dt = Math.min(.05, frame.t ? (now - frame.t) / 1000 : .016);
frame.t = now;
requestAnimationFrame(frame);
if (!S) return;
const still = calm.matches;
// the knob turns to its mark on a spring and overshoots a little
const goal = -135 + (mode + 1) * 270 / MODES.length;
for (let s = 0; s < Math.max(1, Math.round(dt * 60)); s++) {
spin = (spin + (goal - turn) * .12) * .78;
turn += spin;
}
dial.style.transform = `rotate(${turn.toFixed(2)}deg)`;
// after a tap the plate shakes and calms down; the sand hops less and less
if (shake > .002) {
const hops = Math.max(1, Math.round(dt * 210));
for (let k = 0; k < hops; k++) for (let i = 0; i < GRAINS; i++) {
const f = Math.abs(wave(gx[i], gy[i]));
const step = JUMP * shake * f;
gx[i] += (rnd() - .5) * step;
gy[i] += (rnd() - .5) * step;
if (gx[i] < .01) gx[i] = .01; else if (gx[i] > .99) gx[i] = .99;
if (gy[i] < .01) gy[i] = .01; else if (gy[i] > .99) gy[i] = .99;
lift[i] = still ? 0 : step * 260 * rnd();
}
shake *= Math.exp(-dt / SETTLE);
} else if (shake) { shake = 0; lift.fill(0); }
const jx = still ? 0 : (rnd() - .5) * shake * 3, jy = still ? 0 : (rnd() - .5) * shake * 3;
plate.style.transform = shake && !still ? `translate(${jx.toFixed(2)}px, ${jy.toFixed(2)}px)` : '';
draw();
hint.style.opacity = tapped ? '0' : '1';
}
// every grain is a warm speck of light; a hopping grain leaves its shadow on the steel
function draw() {
const w = sand.width, k = w;
buf.fill(0);
for (let i = 0; i < GRAINS; i++) {
const x = gx[i] * k, y = gy[i] * k, l = lift[i] * DPR;
const lit = .7 + .3 * (gx[i] - gy[i] + 1) / 2, b = tone[i] * lit * 255;
if (l > .6) { const o = ((y | 0) * w + (x | 0)) * 4; buf[o + 3] = Math.max(buf[o + 3], 110); }
const sx = x | 0, sy = (y - l) | 0, size = DPR > 1.4 ? 2 : 1;
if (sx < 0 || sy < 0 || sx >= w - 2 || sy >= w - 2) continue;
for (let oy = 0; oy < size; oy++) for (let ox = 0; ox < size; ox++) {
const o = ((sy + oy) * w + sx + ox) * 4;
buf[o] += b; buf[o + 1] += b * .87; buf[o + 2] += b * .7; buf[o + 3] = 255;
}
}
pctx.putImageData(img, 0, 0);
ctx.clearRect(0, 0, w, w);
hctx.clearRect(0, 0, halo.width, halo.height); hctx.imageSmoothingQuality = 'high';
hctx.drawImage(page, 0, 0, halo.width, halo.height);
ctx.globalCompositeOperation = 'lighter'; ctx.imageSmoothingQuality = 'high';
ctx.globalAlpha = .45; ctx.drawImage(halo, 0, 0, w, w); ctx.globalAlpha = 1;
ctx.globalCompositeOperation = 'source-over';
ctx.drawImage(page, 0, 0);
}
new ResizeObserver(layout).observe(plate);
layout();
paintTicks();
requestAnimationFrame(frame);
})();
</script>
Tap or drag the frequency knob to switch modes and watch the sand reorganize into a new Chladni pattern.
What you can change
Change a number, save, and reload the page to see the difference.
GRAINS16000How many grains of sand are simulated. Lower for slower phones.
JUMP0.07How far a grain hops on the loudest part of the plate.
SETTLE0.8How long the plate keeps shaking after a tap, in seconds.
Add it to your site
Put it in interactive hero sections, audio/music showcases or creative studio portfolios.
CSS
Paste into your stylesheet, or inside a <style> tag in <head>.
/* Chladni Sand by @codinvisual · t.me/CodinVisual · page shell, only needed while this file is the whole page */HTML
Put it where the effect should appear on your page.
<!-- plate start --> <div class="cs"> <canvas class="cs-grain" aria-hidden="true"></canvas> <div class="cs-plate" aria-hidden="true"><canvas class="cs-steel"></canvas><canvas class="cs-sand"></canvas></div> <div class="cs-side"> <button class="cs-knob" type="button" aria-label="Frequency knob. Tap for the next pattern, or use the arrow keys."> <svg class="cs-ticks" viewBox="0 0 136 136" aria-hidden="true"></svg> <span class="cs-dial" aria-hidden="true"></span> </button> <p class="cs-mode" aria-live="polite">Off</p> <button class="cs-sound" type="button" aria-pressed="false"><svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path d="M4 9.5h3.5L12 5.5v13L7.5 14.5H4z" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linejoin="round"/><path class="cs-wave" d="M15.5 9a4 4 0 0 1 0 6M18 6.5a7.5 7.5 0 0 1 0 11" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg><span>Sound</span></button> </div> <p class="cs-hint" aria-hidden="true">Tap the knob</p> </div> <!-- plate end -->JavaScript
Paste right before the closing </body> tag, inside <script></script>.
/* Chladni Sand by @codinvisual · t.me/CodinVisual · free for your own site, don't resell this file */ (() => { const GRAINS = 16000; const JUMP = 0.07; const SETTLE = 0.8; const MODES = [[2, 3, 1], [3, 4, -1], [4, 5, 1], [5, 6, -1]]; const root = document.querySelector('.cs'); if (!root) return; const plate = root.querySelector('.cs-plate'), steel = root.querySelector('.cs-steel'), sand = root.querySelector('.cs-sand'); const knob = root.querySelector('.cs-knob'), dial = root.querySelector('.cs-dial'), ticks = root.querySelector('.cs-ticks'); const label = root.querySelector('.cs-mode'), soundBtn = root.querySelector('.cs-sound'), hint = root.querySelector('.cs-hint'), grain = root.querySelector('.cs-grain'); const calm = matchMedia('(prefers-reduced-motion: reduce)'); const ctx = sand.getContext('2d'), page = document.createElement('canvas'), pctx = page.getContext('2d'); const halo = document.createElement('canvas'), hctx = halo.getContext('2d'); const { cos, PI } = Math; let seed = 1017; 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; }; // grains live in plate units, 0 to 1 across; at first the sand is simply poured on const gx = new Float32Array(GRAINS), gy = new Float32Array(GRAINS), lift = new Float32Array(GRAINS), tone = new Float32Array(GRAINS); for (let i = 0; i < GRAINS; i++) { gx[i] = .03 + rnd() * .94; gy[i] = .03 + rnd() * .94; tone[i] = .75 + rnd() * .25; } let mode = -1, n = 0, m = 0, sign = 1, shake = 0, turn = -135, spin = 0, S = 0, DPR = 1, img = null, buf = null, tapped = false; // the standing wave: where it is zero the plate does not move, and that is where the sand stays function wave(x, y) { const a = cos(n * PI * x) * cos(m * PI * y); const b = cos(m * PI * x) * cos(n * PI * y); return a + sign * b; } // brushed steel, drawn once: fine strokes, a broad highlight, a bevel, the bolt in the middle function paintSteel() { const c = steel.getContext('2d'), w = steel.width, s = w / 560; const base = c.createLinearGradient(0, 0, w, w); base.addColorStop(0, '#2C2E33'); base.addColorStop(.5, '#1D1F23'); base.addColorStop(1, '#141518'); c.fillStyle = base; c.fillRect(0, 0, w, w); for (let i = 0; i < 2600; i++) { const y = rnd() * w, x = rnd() * w, len = (40 + rnd() * 220) * s, a = rnd() * .05; c.strokeStyle = rnd() < .5 ? `rgba(255,255,255,${a})` : `rgba(0,0,0,${a * 1.4})`; c.lineWidth = (.5 + rnd()) * s; c.beginPath(); c.moveTo(x, y); c.lineTo(x + len, y + (rnd() - .5) * 2 * s); c.stroke(); } const sheen = c.createLinearGradient(0, 0, w, w * .4); sheen.addColorStop(0, 'rgba(255,240,220,0)'); sheen.addColorStop(.45, 'rgba(255,240,220,.07)'); sheen.addColorStop(.6, 'rgba(255,240,220,0)'); c.fillStyle = sheen; c.fillRect(0, 0, w, w); const warm = c.createRadialGradient(w * .78, w * .2, 0, w * .78, w * .2, w * .9); warm.addColorStop(0, 'rgba(255,200,140,.10)'); warm.addColorStop(1, 'rgba(255,200,140,0)'); c.fillStyle = warm; c.fillRect(0, 0, w, w); c.lineWidth = 3 * s; c.strokeStyle = 'rgba(255,255,255,.14)'; c.strokeRect(1.5 * s, 1.5 * s, w - 3 * s, w - 3 * s); c.lineWidth = 2 * s; c.strokeStyle = 'rgba(0,0,0,.5)'; c.strokeRect(4 * s, 4 * s, w - 8 * s, w - 8 * s); const bolt = c.createRadialGradient(w * .5 - 4 * s, w * .5 - 5 * s, 1, w * .5, w * .5, 15 * s); bolt.addColorStop(0, '#6A6D73'); bolt.addColorStop(.6, '#33353A'); bolt.addColorStop(1, '#141518'); c.fillStyle = bolt; c.beginPath(); c.arc(w * .5, w * .5, 15 * s, 0, 7); c.fill(); c.strokeStyle = 'rgba(0,0,0,.6)'; c.lineWidth = 2 * s; c.beginPath(); c.moveTo(w * .5 - 8 * s, w * .5); c.lineTo(w * .5 + 8 * s, w * .5); c.stroke(); } function paintTicks() { const count = MODES.length + 1; let out = ''; for (let k = 0; k < count; k++) { const a = (-135 + k * 270 / (count - 1)) * PI / 180, r0 = 62, r1 = k ? 67 : 65; out += `<line x1="${68 + Math.sin(a) * r0}" y1="${68 - cos(a) * r0}" x2="${68 + Math.sin(a) * r1}" y2="${68 - cos(a) * r1}" stroke="${k === mode + 1 ? '#FFD9A6' : 'rgba(238,234,228,.35)'}" stroke-width="2" stroke-linecap="round"/>`; } ticks.innerHTML = out; } function layout() { const w = plate.clientWidth; if (!w || w === S) return; S = w; DPR = Math.min(2, window.devicePixelRatio || 1); const px = Math.round(S * DPR); steel.width = steel.height = px; sand.width = sand.height = px; page.width = page.height = px; halo.width = halo.height = Math.ceil(px / 4); img = pctx.createImageData(px, px); buf = img.data; seed = 1017; paintSteel(); grain.width = Math.round(root.clientWidth / 2); grain.height = Math.round(root.clientHeight / 2); const gc = grain.getContext('2d'), gi = gc.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] = 12; } gc.putImageData(gi, 0, 0); } // a tone for the pattern, only after somebody turned the sound on let audio = null, soundOn = false; function ring() { if (!soundOn) return; audio = audio || new (window.AudioContext || window.webkitAudioContext)(); const t0 = audio.currentTime, o = audio.createOscillator(), g = audio.createGain(); o.type = 'sine'; o.frequency.value = 180 * Math.sqrt(n * n + m * m); g.gain.setValueAtTime(0, t0); g.gain.linearRampToValueAtTime(.12, t0 + .03); g.gain.exponentialRampToValueAtTime(.0001, t0 + SETTLE * 3); o.connect(g).connect(audio.destination); o.start(t0); o.stop(t0 + SETTLE * 3 + .1); } function go(next) { mode = (next + MODES.length) % MODES.length; [n, m, sign] = MODES[mode]; shake = 1; tapped = true; label.textContent = `Pattern ${n} × ${m}`; paintTicks(); ring(); } // the knob: a tap moves on by one mark, a drag around it picks the nearest mark let drag = null; const angleAt = e => { const r = knob.getBoundingClientRect(); return Math.atan2(e.clientX - (r.left + r.width / 2), -(e.clientY - (r.top + r.height / 2))) * 180 / PI; }; knob.addEventListener('pointerdown', e => { drag = { id: e.pointerId, a0: angleAt(e), moved: false }; try { knob.setPointerCapture(e.pointerId); } catch (err) { } }); knob.addEventListener('pointermove', e => { if (!drag || e.pointerId !== drag.id) return; const a = angleAt(e); if (Math.abs(a - drag.a0) > 14) drag.moved = true; if (drag.moved) { const k = Math.round((Math.max(-135, Math.min(135, a)) + 135) / (270 / MODES.length)); if (k - 1 !== mode && k > 0) go(k - 1); } }); const release = e => { if (!drag || e.pointerId !== drag.id) return; const moved = drag.moved; drag = null; if (!moved) go(mode + 1); }; knob.addEventListener('pointerup', release); knob.addEventListener('pointercancel', () => { drag = null; }); knob.addEventListener('keydown', e => { if (e.key === 'ArrowRight' || e.key === 'ArrowUp') { e.preventDefault(); go(mode + 1); } if (e.key === 'ArrowLeft' || e.key === 'ArrowDown') { e.preventDefault(); go(Math.max(0, mode - 1)); } }); knob.addEventListener('click', e => { if (e.detail === 0) go(mode + 1); }); soundBtn.addEventListener('click', () => { soundOn = !soundOn; soundBtn.setAttribute('aria-pressed', String(soundOn)); if (soundOn) { audio = audio || new (window.AudioContext || window.webkitAudioContext)(); audio.resume(); } }); function frame(now) { const dt = Math.min(.05, frame.t ? (now - frame.t) / 1000 : .016); frame.t = now; requestAnimationFrame(frame); if (!S) return; const still = calm.matches; // the knob turns to its mark on a spring and overshoots a little const goal = -135 + (mode + 1) * 270 / MODES.length; for (let s = 0; s < Math.max(1, Math.round(dt * 60)); s++) { spin = (spin + (goal - turn) * .12) * .78; turn += spin; } dial.style.transform = `rotate(${turn.toFixed(2)}deg)`; // after a tap the plate shakes and calms down; the sand hops less and less if (shake > .002) { const hops = Math.max(1, Math.round(dt * 210)); for (let k = 0; k < hops; k++) for (let i = 0; i < GRAINS; i++) { const f = Math.abs(wave(gx[i], gy[i])); const step = JUMP * shake * f; gx[i] += (rnd() - .5) * step; gy[i] += (rnd() - .5) * step; if (gx[i] < .01) gx[i] = .01; else if (gx[i] > .99) gx[i] = .99; if (gy[i] < .01) gy[i] = .01; else if (gy[i] > .99) gy[i] = .99; lift[i] = still ? 0 : step * 260 * rnd(); } shake *= Math.exp(-dt / SETTLE); } else if (shake) { shake = 0; lift.fill(0); } const jx = still ? 0 : (rnd() - .5) * shake * 3, jy = still ? 0 : (rnd() - .5) * shake * 3; plate.style.transform = shake && !still ? `translate(${jx.toFixed(2)}px, ${jy.toFixed(2)}px)` : ''; draw(); hint.style.opacity = tapped ? '0' : '1'; } // every grain is a warm speck of light; a hopping grain leaves its shadow on the steel function draw() { const w = sand.width, k = w; buf.fill(0); for (let i = 0; i < GRAINS; i++) { const x = gx[i] * k, y = gy[i] * k, l = lift[i] * DPR; const lit = .7 + .3 * (gx[i] - gy[i] + 1) / 2, b = tone[i] * lit * 255; if (l > .6) { const o = ((y | 0) * w + (x | 0)) * 4; buf[o + 3] = Math.max(buf[o + 3], 110); } const sx = x | 0, sy = (y - l) | 0, size = DPR > 1.4 ? 2 : 1; if (sx < 0 || sy < 0 || sx >= w - 2 || sy >= w - 2) continue; for (let oy = 0; oy < size; oy++) for (let ox = 0; ox < size; ox++) { const o = ((sy + oy) * w + sx + ox) * 4; buf[o] += b; buf[o + 1] += b * .87; buf[o + 2] += b * .7; buf[o + 3] = 255; } } pctx.putImageData(img, 0, 0); ctx.clearRect(0, 0, w, w); hctx.clearRect(0, 0, halo.width, halo.height); hctx.imageSmoothingQuality = 'high'; hctx.drawImage(page, 0, 0, halo.width, halo.height); ctx.globalCompositeOperation = 'lighter'; ctx.imageSmoothingQuality = 'high'; ctx.globalAlpha = .45; ctx.drawImage(halo, 0, 0, w, w); ctx.globalAlpha = 1; ctx.globalCompositeOperation = 'source-over'; ctx.drawImage(page, 0, 0); } new ResizeObserver(layout).observe(plate); layout(); paintTicks(); requestAnimationFrame(frame); })();
Connect it to your backend
Pure front end Canvas simulation with optional Web Audio tone.
Good to know
- Sound stays muted until the visitor turns it on.
Tilda, Webflow, WordPress
Press Copy all code at the top and paste it into one HTML block.
Add block, Other, T123 HTML code. Paste and save. Check the published page, the editor preview does not run scripts.
Add panel, Embed. Paste, then Save & Close. Custom code runs on the published site.
Add block, Custom HTML. Paste and press Update.



