<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"><meta name="theme-color" content="#08070f"><title>Lava Lamp</title><link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M6 1h4l1.2 9H4.8z' fill='%238a4df0'/%3E%3Ccircle cx='8' cy='6.2' r='2' fill='%23ff8a3d'/%3E%3Cpath d='M4.8 10h6.4L12.6 15H3.4z' fill='%23b9b9c6'/%3E%3C/svg%3E"><style> *, *::before, *::after { box-sizing: border-box; } html, body { margin: 0; height: 100%; background: #08070f; overflow: hidden; overscroll-behavior: none; } #stage { position: fixed; top: 0; right: 0; bottom: 0; left: 0; inset: 0; overflow: hidden; background: #08070f; touch-action: manipulation; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; } #stage canvas { position: absolute; display: block; pointer-events: none; } #scene { left: 0; top: 0; width: 100%; height: 100%; } /* Inline rocker switch on the lamp cord. */ #switch { position: absolute; left: 0; top: 0; width: 80px; height: 50px; margin: 0; padding: 0; border: 0; background: none; cursor: pointer; outline: none; -webkit-appearance: none; appearance: none; touch-action: manipulation; } #switch svg { display: block; width: 100%; height: 100%; overflow: visible; pointer-events: none; } #switch:active svg { transform: translateY(1px); } #switch:focus-visible { outline: 2px solid #ffd9a8; outline-offset: 3px; border-radius: 14px; } #switch .rk { fill: #a39c87; transition: fill .15s; } #switch .half .up { display: none; } #switch[aria-checked="true"] .half.off .up, #switch[aria-checked="false"] .half.on .up { display: inline; } #switch[aria-checked="true"] .half.off .rk, #switch[aria-checked="false"] .half.on .rk { fill: #f7f2e4; } #switch .mk { transition: transform .15s; } #switch[aria-checked="true"] .half.off .mk { transform: translateY(-1.5px); } #switch[aria-checked="true"] .half.on .mk { transform: translateY(1px); } #switch[aria-checked="false"] .half.off .mk { transform: translateY(1px); } #switch[aria-checked="false"] .half.on .mk { transform: translateY(-1.5px); } #switch .led { fill: #5a4a3a; transition: fill .2s; } #switch[aria-checked="true"] .led { fill: #ff8a2a; filter: drop-shadow(0 0 3px #ff6a00); } noscript { position: absolute; left: 0; right: 0; top: 45%; text-align: center; color: #cfc8e6; font: 16px system-ui, sans-serif; }</style></head><body><div id="stage"> <canvas id="scene"></canvas> <canvas id="glass"></canvas> <canvas id="front"></canvas> <button id="switch" type="button" role="switch" aria-checked="true" aria-label="Lamp power" title="Turn the lamp off or on"> <svg viewBox="0 0 120 76" aria-hidden="true" focusable="false"> <defs> <linearGradient id="swBody" x1="0" y1="0" x2="0" y2="1"> <stop offset="0" stop-color="#f3eddd"/> <stop offset="0.55" stop-color="#cdc6b0"/> <stop offset="1" stop-color="#8e8772"/> </linearGradient> </defs> <ellipse cx="60" cy="67" rx="54" ry="8" fill="#000" opacity=".5"/> <rect x="0" y="33" width="120" height="9" rx="4.5" fill="#0c0c10"/> <rect x="13" y="12" width="94" height="48" rx="11" fill="url(#swBody)" stroke="#1c1a16" stroke-width="1.5"/> <path d="M19 16.5 H101" stroke="#fff" stroke-opacity=".55" stroke-width="1.5" stroke-linecap="round"/> <rect x="21" y="22" width="78" height="35" rx="6" fill="#17150f"/> <g class="half off"> <path class="rk up" d="M24 25 L60 28 L60 51 L24 54 Z"/> <path class="rk" d="M24 31 L60 28 L60 51 L24 52 Z"/> <ellipse class="mk" cx="42" cy="39.5" rx="6" ry="5.5" fill="none" stroke="#2e2b25" stroke-width="2.2"/> </g> <g class="half on"> <path class="rk up" d="M60 28 L96 25 L96 54 L60 51 Z"/> <path class="rk" d="M60 28 L96 31 L96 52 L60 51 Z"/> <path class="mk" d="M78 34 V45" fill="none" stroke="#2e2b25" stroke-width="2.6" stroke-linecap="round"/> </g> <circle class="led" cx="60" cy="18.5" r="2.4"/> </svg> </button> <noscript>This lava lamp needs JavaScript.</noscript></div><script>(function () { 'use strict'; const stage = document.getElementById('stage'); const scene = document.getElementById('scene'); const front = document.getElementById('front'); let glass = document.getElementById('glass'); const swBtn = document.getElementById('switch'); const sctx = scene.getContext('2d'); const fctx = front.getContext('2d'); // Lamp proportions, as fractions of the lamp's total height (0 = desk, 1 = top of cap). const COLLAR_TOP = 0.266, GLASS_BOTTOM = 0.262, GLASS_TOP = 0.862, CAP_BOTTOM = 0.856, CAP_TOP = 0.975; const GLASS_H = GLASS_TOP - GLASS_BOTTOM; // Glass interior, measured in glass heights: y runs 0 (bottom) to 1 (top). const GL_B = 0.18, GL_A = 0.10; // half-width at the bottom / top const XH = 0.2, Y_BOT = -0.02, Y_SPAN = 1.04; // rectangle covered by the glass canvas let W = 0, H = 0, S = 0, cx = 0, by = 0, ty = 0, horizon = 0, gH = 0, dpr2 = 1, dprG = 1, quality = 0; let fX = 0, fY = 0; // top-left corner of the front canvas, in CSS px let swX = 0, swY = 0, swW = 0, swH = 0; let lampOn = true, light = 1, heat = 1, simT = 0, drawnLight = -1; const sstep = (a, b, v) => { let t = (v - a) / (b - a); t = t < 0 ? 0 : t > 1 ? 1 : t; return t * t * (3 - 2 * t); }; const clamp01 = v => v < 0 ? 0 : v > 1 ? 1 : v; const rgba = (r, g, b, a) => 'rgba(' + r + ',' + g + ',' + b + ',' + a.toFixed(4) + ')'; const rnd = x => { const s = Math.sin(x * 127.1 + 311.7) * 43758.5453; return s - Math.floor(s); }; /* ------------------------------------------------------------------ Wax simulation The wax is a set of equal "motes". Motes that touch bond together, so a blob is simply a connected cluster. Each mote has its own temperature: warm motes are buoyant, cool ones heavy. Heat enters a blob through the surfaces exposed to the hot liquid near the bulb and leaves it near the top, so blobs swell off the pool at the bottom, shed their cool upper parts on the way, and sink back down to merge into the pool again. ------------------------------------------------------------------ */ const N = 56; const D0 = 0.036; // rest spacing of bonded motes const DBOND = 0.047; // motes closer than this bond (touching) const DCUT = 0.067; // beyond this a bond cannot survive const DBOND2 = DBOND * DBOND, DCUT2 = DCUT * DCUT; const KATT = 10; // cohesion between bonded motes const KREP = 500, FMAX = 6; // short-range repulsion keeps motes from collapsing const KC = 1.2; // pull toward the cluster centre (surface tension) const KB = 2.2; // buoyancy per unit temperature const TN = 0.45; // neutral temperature const CD = 2.2; // drag const KAPPA = 0.12; // heat conduction between bonded motes const H0 = 0.12, HY2 = 0.16; // surface heating near the bulb: H0 * exp(-y^2 / HY2) const C0 = 0.05, CTOP = 1.2; // cooling toward the liquid const TLIQ = 0.35, TTOP = 0.1, TROOM = 0.1; const WALL = 0.012, FLOOR = 0.014, CEIL = 0.968; const RK = 0.052; // radius over which one mote contributes to the wax field const px = new Float64Array(N), py = new Float64Array(N), vx = new Float64Array(N), vy = new Float64Array(N); const temp = new Float64Array(N), expo = new Float64Array(N), cond = new Float64Array(N); const fx = new Float64Array(N), fy = new Float64Array(N), phase = new Float64Array(N); const bond = new Uint8Array(N * N); const comp = new Int16Array(N), csize = new Int16Array(N); const ccx = new Float64Array(N), ccy = new Float64Array(N), stack = new Int16Array(N); function findClusters() { comp.fill(-1); let nc = 0; for (let s = 0; s < N; s++) { if (comp[s] >= 0) continue; let top = 0, n = 0, sx = 0, sy = 0; stack[top++] = s; comp[s] = nc; while (top > 0) { const a = stack[--top], row = a * N; n++; sx += px[a]; sy += py[a]; for (let b = 0; b < N; b++) if (bond[row + b] && comp[b] < 0) { comp[b] = nc; stack[top++] = b; } } csize[nc] = n; ccx[nc] = sx / n; ccy[nc] = sy / n; nc++; } } // How exposed a mote is to the liquid above it (1 = open surface, 0 = buried in wax). function exposure() { const inv = 1 / (RK * RK); for (let i = 0; i < N; i++) { const x = px[i], y = py[i] + 0.04; let f = 0; for (let j = 0; j < N; j++) { const dx = px[j] - x, dy = py[j] - y, u2 = (dx * dx + dy * dy) * inv; if (u2 < 1) { const a = 1 - u2; f += a * a * a; } } expo[i] = clamp01(1 - f * 2); } } function step(h, t) { findClusters(); for (let i = 0; i < N; i++) { fx[i] = 0; fy[i] = 0; cond[i] = 0; } for (let i = 0; i < N; i++) { const xi = px[i], yi = py[i], Ti = temp[i], row = i * N; const n = csize[comp[i]], sizef = n > 12 ? 12 / n : 1; // big blobs hold together less firmly for (let j = i + 1; j < N; j++) { const dx = px[j] - xi, dy = py[j] - yi, d2 = dx * dx + dy * dy; if (d2 >= DCUT2) { if (bond[row + j]) bond[row + j] = bond[j * N + i] = 0; continue; } const d = Math.sqrt(d2) + 1e-9; let b = bond[row + j]; if (!b && d < DBOND) b = bond[row + j] = bond[j * N + i] = 1; // touching: merge const dT = Ti - temp[j]; let f = 0; if (d < D0) f = -Math.min(KREP * (D0 - d), FMAX); else if (b) { // A bond between motes of very different temperature is weak, so a blob // whose top has cooled (or whose underside is hot) tears itself apart. const q = dT < 0 ? -dT : dT; const s = (q >= 0.35 ? 0.08 : 1 - (q / 0.35) * (q / 0.35)) * sizef; if (d > D0 * (1 + 0.85 * s)) bond[row + j] = bond[j * N + i] = 0; // stretched too far: split else f = KATT * s * (d - D0); } if (f !== 0) { const ux = f * dx / d, uy = f * dy / d; fx[i] += ux; fy[i] += uy; fx[j] -= ux; fy[j] -= uy; } if (b) { cond[i] -= dT; cond[j] += dT; } } } const damp = Math.exp(-CD * h); for (let i = 0; i < N; i++) { const c = comp[i], n = csize[c]; // Surface tension rounds a blob up; in a big pile it is negligible. if (n > 1) { const kc = n > 8 ? KC * 8 / n : KC; fx[i] += kc * (ccx[c] - px[i]); fy[i] += kc * (ccy[c] - py[i]); } const y = py[i], T = temp[i]; let ay = KB * (T - TN); // warm rises, cool sinks fx[i] += 0.05 * Math.sin(0.9 * t + phase[i]); // a little drift so nothing stays symmetric ay += 0.05 * Math.cos(0.7 * t + phase[i] * 1.7); const up = y > 0.93 ? sstep(0.93, 0.955, y) : 0; // only the very top of the glass is cold const rate = C0 + CTOP * up + 0.12 * (1 - heat); const tliq = TROOM + heat * (TLIQ - (TLIQ - TTOP) * up - TROOM); let dT = rate * (tliq - T) + KAPPA * (n > 10 ? 10 / n : 1) * cond[i]; if (y < 0.6 && heat > 0) { dT += H0 * heat * expo[i] * Math.exp(-y * y / HY2) * (0.8 + 0.2 * Math.sin(9 * px[i] + 0.35 * t)) * (1 - T); } const Tn = T + dT * h; temp[i] = Tn < 0 ? 0 : Tn > 1 ? 1 : Tn; let nvx = (vx[i] + fx[i] * h) * damp, nvy = (vy[i] + ay * h) * damp; if (nvx > 1.2) nvx = 1.2; else if (nvx < -1.2) nvx = -1.2; if (nvy > 1.2) nvy = 1.2; else if (nvy < -1.2) nvy = -1.2; px[i] += nvx * h; py[i] += nvy * h; // Stay inside the tapered glass. const yy = py[i], lim = GL_B + (GL_A - GL_B) * (yy < 0 ? 0 : yy > 1 ? 1 : yy) - WALL; if (px[i] > lim) { px[i] = lim; if (nvx > 0) nvx = 0; } else if (px[i] < -lim) { px[i] = -lim; if (nvx < 0) nvx = 0; } if (yy < FLOOR) { py[i] = FLOOR; if (nvy < 0) nvy = 0; } else if (yy > CEIL) { py[i] = CEIL; if (nvy > 0) nvy = 0; } vx[i] = nvx; vy[i] = nvy; } } function initParticles() { // A settled pile of wax on the floor of the glass, ready to be warmed up. let i = 0, row = 0; while (i < N) { const y = FLOOR + 0.018 + row * D0 * 0.866; const lim = GL_B + (GL_A - GL_B) * y - WALL; const cap = row & 1 ? 2 * Math.floor(lim / D0) : 2 * Math.floor(lim / D0) + 1; const n = Math.min(cap, N - i); for (let k = 0; k < n; k++, i++) { px[i] = (k - (n - 1) / 2) * D0 + (Math.random() - 0.5) * 0.004; py[i] = y + (Math.random() - 0.5) * 0.004; vx[i] = vy[i] = 0; temp[i] = TLIQ; phase[i] = Math.random() * 6.2832; } row++; } } function warmUp(seconds) { const h = 1 / 120; for (let k = 0, n = Math.round(seconds * 120); k < n; k++) { if ((k & 1) === 0) exposure(); simT += h; step(h, simT); } } /* ------------------------------------------------------------------ Wax field: every mote splats a smooth kernel into a small grid. The grid stores, per cell, the signed distance to the wax surface, the direction of the surface normal and the wax temperature. A shader turns that into shaded wax; a canvas-2D path does the same if WebGL is unavailable. ------------------------------------------------------------------ */ const GW = 123, GH = 320; const CELLX = 2 * XH / GW, CELLY = Y_SPAN / GH; const RC = RK / CELLX; // kernel radius in cells const TH = 0.25; // wax surface where the field crosses this const DMAX = 14; // distance range stored, in cells const GREF = 0.2 / RC, DOME = 0.75 * RC; const F = new Float32Array(GW * GH), GX = new Float32Array(GW * GH), GY = new Float32Array(GW * GH); const TS = new Float32Array(GW * GH), WS = new Float32Array(GW * GH); const data = new Uint8Array(GW * GH * 4); const NA = new Float32Array(GW), NC = new Float32Array(GW), NB = new Float32Array(GH), ND = new Float32Array(GH); function buildField(t) { F.fill(0); GX.fill(0); GY.fill(0); TS.fill(0); WS.fill(0); const invR2 = 1 / (RC * RC); for (let i = 0; i < N; i++) { const sx = (px[i] + XH) / CELLX - 0.5, sy = (Y_BOT + Y_SPAN - py[i]) / CELLY - 0.5; let x0 = Math.floor(sx - RC), x1 = Math.ceil(sx + RC), y0 = Math.floor(sy - RC), y1 = Math.ceil(sy + RC); if (x0 < 0) x0 = 0; if (y0 < 0) y0 = 0; if (x1 > GW - 1) x1 = GW - 1; if (y1 > GH - 1) y1 = GH - 1; const Ti = temp[i]; for (let j = y0; j <= y1; j++) { const dy = j - sy; let o = j * GW + x0; for (let k = x0; k <= x1; k++, o++) { const dx = k - sx, u2 = (dx * dx + dy * dy) * invR2; if (u2 < 1) { const a = 1 - u2, w = a * a * a, g = -6 * a * a * invR2; // kernel and its gradient F[o] += w; GX[o] += g * dx; GY[o] += g * dy; TS[o] += w * Ti; WS[o] += w; } } } } // A slow drifting ripple so that outlines never look machine-perfect. for (let i = 0; i < GW; i++) { NA[i] = Math.sin(0.16 * i + 1.1 * t); NC[i] = Math.sin(0.12 * i - 0.8 * t + 1.7); } for (let j = 0; j < GH; j++) { NB[j] = Math.sin(0.11 * j + 0.5); ND[j] = Math.sin(0.17 * j + 2.3); } let o = 0; for (let j = 0; j < GH; j++) { const nb = NB[j], nd = ND[j]; for (let i = 0; i < GW; i++, o++) { const o4 = o * 4, f = F[o]; if (f < 0.004) { data[o4] = 0; data[o4 + 1] = 128; data[o4 + 2] = 128; data[o4 + 3] = 128; continue; } const gx = GX[o], gy = -GY[o]; // y is up for the renderer const gm = Math.sqrt(gx * gx + gy * gy); const th = TH * (1 + 0.25 * (NA[i] * nb + NC[i] * nd)); let d = (f - th) / (gm > 1e-4 ? gm : 1e-4); if (d > DMAX) d = DMAX; else if (d < -DMAX) d = -DMAX; const k = (gm < GREF ? gm / GREF : 1) / (gm > 1e-6 ? gm : 1e-6); data[o4] = (d / DMAX * 0.5 + 0.5) * 255 + 0.5; data[o4 + 1] = (gx * k * 0.5 + 0.5) * 255 + 0.5; data[o4 + 2] = (gy * k * 0.5 + 0.5) * 255 + 0.5; data[o4 + 3] = TS[o] / WS[o] * 255 + 0.5; } } } /* ---------- WebGL renderer ---------- */ const VS = `attribute vec2 aPos;varying vec2 vUv;void main() { vUv = aPos * 0.5 + 0.5; gl_Position = vec4(aPos, 0.0, 1.0);}`; const FS = `#ifdef GL_FRAGMENT_PRECISION_HIGHprecision highp float;#elseprecision mediump float;#endifuniform sampler2D uField;uniform vec2 uRes;uniform float uCell;uniform float uDome;uniform float uLight;varying vec2 vUv;const vec3 WAX_COLD = vec3(0.70, 0.07, 0.25);const vec3 WAX_MID = vec3(1.00, 0.30, 0.33);const vec3 WAX_HOT = vec3(1.00, 0.76, 0.28);const vec3 LIQ_TOP = vec3(0.16, 0.10, 0.46);const vec3 LIQ_BOT = vec3(0.50, 0.22, 0.92);float smax(float a, float b, float k) { float h = clamp(0.5 + 0.5 * (a - b) / k, 0.0, 1.0); return mix(b, a, h) + k * h * (1.0 - h);}void main() { // Position in glass heights: x across, y up. vec2 p = vec2((vUv.x - 0.5) * 0.4, vUv.y * 1.04 - 0.02); float yc = clamp(p.y, 0.0, 1.0); float hw = mix(0.18, 0.10, yc); float sd = smax(abs(p.x) - hw, max(-p.y, p.y - 1.0), 0.03); // < 0 inside the glass float ins = -sd; float glassA = clamp(ins * uRes.y / 1.04 + 0.5, 0.0, 1.0); if (glassA < 0.004) { gl_FragColor = vec4(0.0); return; } vec4 t = texture2D(uField, vec2(vUv.x, 1.0 - vUv.y)); float d = (t.r - 0.5) * 28.0; // cells inside (+) or outside (-) the wax vec2 g = t.gb * 2.0 - 1.0; // points into the wax float T = t.a; // Wax: treat the blob as a dome rising from its outline. float waxA = clamp(d * uCell + 0.5, 0.0, 1.0); float dc = clamp(d, 0.35, uDome); float slope = (uDome - dc) / sqrt(2.0 * uDome * dc - dc * dc); vec3 n = normalize(vec3(-g * slope, 1.0)); float difB = clamp(dot(n, normalize(vec3(0.12, -1.0, 0.5))), 0.0, 1.0); // bulb, from below float difR = clamp(dot(n, normalize(vec3(-0.45, 0.55, 0.7))), 0.0, 1.0); // room, from the upper left float spec = pow(clamp(dot(n, normalize(vec3(-0.24, 0.30, 0.92))), 0.0, 1.0), 18.0); float bulb = mix(1.0, 0.45, yc); vec3 wc = mix(WAX_COLD, WAX_MID, smoothstep(0.12, 0.45, T)); wc = mix(wc, WAX_HOT, smoothstep(0.45, 0.8, T)); float rim = smoothstep(0.0, 2.5, d); vec3 wax = wc * (0.42 + 0.95 * difB * bulb + 0.25 * difR) * mix(0.6, 1.0, rim) + vec3(1.0, 0.96, 0.9) * spec * 0.2; // Liquid: lit from the bulb below, with a soft shadow and glow around the wax. vec3 liq = mix(LIQ_TOP, LIQ_BOT, pow(1.0 - yc, 1.6)); liq += vec3(1.0, 0.45, 0.55) * 0.45 * exp(-yc * 7.0); liq *= 1.0 - 0.3 * smoothstep(-4.0, 0.0, d); liq += wc * 0.10 * smoothstep(-10.0, 0.0, d); // The same scene with the bulb off. vec3 waxOff = vec3(0.085, 0.06, 0.12) * (0.55 + 0.9 * difR) * mix(0.7, 1.0, rim); vec3 liqOff = mix(vec3(0.035, 0.035, 0.075), vec3(0.06, 0.05, 0.12), 1.0 - yc); liqOff *= 1.0 - 0.35 * smoothstep(-4.0, 0.0, d); vec3 col = mix(mix(liqOff, liq, uLight), mix(waxOff, wax, uLight), waxA); // Thickness of the glass seen at its edge. col *= mix(0.45, 1.0, smoothstep(0.0, 0.02, ins)); col += vec3(0.75, 0.8, 1.0) * 0.07 * (1.0 - smoothstep(0.0, 0.006, abs(ins - 0.012))); gl_FragColor = vec4(col * glassA, glassA);}`; let mode = '2d', gl = null, tex = null, uRes = null, uCell = null, uDome = null, uLight = null, ctx2 = null, img2 = null; function setupGL() { const opts = { alpha: true, premultipliedAlpha: true, antialias: false, depth: false, stencil: false, powerPreference: 'high-performance' }; gl = glass.getContext('webgl', opts) || glass.getContext('experimental-webgl', opts); if (!gl) return false; const compile = (type, src) => { const s = gl.createShader(type); gl.shaderSource(s, src); gl.compileShader(s); return gl.getShaderParameter(s, gl.COMPILE_STATUS) ? s : null; }; const vs = compile(gl.VERTEX_SHADER, VS), fs = compile(gl.FRAGMENT_SHADER, FS); if (!vs || !fs) return false; const prog = gl.createProgram(); gl.attachShader(prog, vs); gl.attachShader(prog, fs); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) return false; gl.useProgram(prog); gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); const loc = gl.getAttribLocation(prog, 'aPos'); gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.pixelStorei(gl.UNPACK_ALIGNMENT, 1); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, GW, GH, 0, gl.RGBA, gl.UNSIGNED_BYTE, data); gl.uniform1i(gl.getUniformLocation(prog, 'uField'), 0); uRes = gl.getUniformLocation(prog, 'uRes'); uCell = gl.getUniformLocation(prog, 'uCell'); uDome = gl.getUniformLocation(prog, 'uDome'); uLight = gl.getUniformLocation(prog, 'uLight'); gl.disable(gl.DEPTH_TEST); gl.disable(gl.BLEND); return true; } function use2D() { // A canvas that has been given a WebGL context cannot become a 2D one, so swap in a fresh element. const fresh = glass.cloneNode(false); glass.parentNode.replaceChild(fresh, glass); glass = fresh; ctx2 = glass.getContext('2d'); img2 = ctx2.createImageData(GW, GH); mode = '2d'; } /* ---------- canvas-2D fallback: the shader above, in JavaScript ---------- */ const WC = [0.70, 0.07, 0.25], WM = [1.00, 0.30, 0.33], WH = [1.00, 0.76, 0.28], LT = [0.16, 0.10, 0.46], LB = [0.50, 0.22, 0.92]; function shade2D() { const out = img2.data, pxu = GH / Y_SPAN, L = light; let o = 0, o4 = 0; for (let j = 0; j < GH; j++) { const y = Y_BOT + Y_SPAN - (j + 0.5) * CELLY; const yc = clamp01(y); const hw = GL_B + (GL_A - GL_B) * yc; const vb = -y > y - 1 ? -y : y - 1; const gb = Math.pow(1 - yc, 1.6), bulb = 1 - 0.55 * yc, hot = 0.45 * Math.exp(-yc * 7); const l0 = LT[0] + (LB[0] - LT[0]) * gb, l1 = LT[1] + (LB[1] - LT[1]) * gb, l2 = LT[2] + (LB[2] - LT[2]) * gb; for (let i = 0; i < GW; i++, o++, o4 += 4) { const a = Math.abs((i + 0.5) * CELLX - XH) - hw; let hh = clamp01(0.5 + 0.5 * (a - vb) / 0.03); const ins = -(vb + (a - vb) * hh + 0.03 * hh * (1 - hh)); const ga = ins * pxu + 0.5; if (ga <= 0) { out[o4 + 3] = 0; continue; } const d = (data[o4] / 255 - 0.5) * 2 * DMAX; const gx = data[o4 + 1] / 127.5 - 1, gy = data[o4 + 2] / 127.5 - 1, T = data[o4 + 3] / 255; const waxA = clamp01(d + 0.5); const dc = d < 0.35 ? 0.35 : d > DOME ? DOME : d; const slope = (DOME - dc) / Math.sqrt(2 * DOME * dc - dc * dc); const nl = 1 / Math.sqrt(gx * gx * slope * slope + gy * gy * slope * slope + 1); const nx = -gx * slope * nl, ny = -gy * slope * nl, nz = nl; const difB = clamp01(nx * 0.1067 - ny * 0.8893 + nz * 0.4446); const difR = clamp01(nx * -0.4511 + ny * 0.5514 + nz * 0.7018); const spec = Math.pow(clamp01(nx * -0.2407 + ny * 0.3009 + nz * 0.9228), 18); const m1 = sstep(0.12, 0.45, T), m2 = sstep(0.45, 0.8, T); const rim = sstep(0, 2.5, d), near = sstep(-4, 0, d), halo = sstep(-10, 0, d); const w0 = WC[0] + (WM[0] - WC[0]) * m1, w1 = WC[1] + (WM[1] - WC[1]) * m1, w2 = WC[2] + (WM[2] - WC[2]) * m1; const r = w0 + (WH[0] - w0) * m2, g = w1 + (WH[1] - w1) * m2, b = w2 + (WH[2] - w2) * m2; const shade = (0.42 + 0.95 * difB * bulb + 0.25 * difR) * (0.6 + 0.4 * rim); const edge = 0.45 + 0.55 * sstep(0, 0.02, ins); const line = 0.07 * (1 - sstep(0, 0.006, Math.abs(ins - 0.012))); const sh = 1 - 0.3 * near, so = 1 - 0.35 * near, off = 0.55 + 0.9 * difR, ro = 0.7 + 0.3 * rim; // liquid, lit and unlit const lr = (l0 + hot) * sh + r * 0.1 * halo, lg = (l1 + hot * 0.45) * sh + g * 0.1 * halo, lb = (l2 + hot * 0.55) * sh + b * 0.1 * halo; const qr = (0.035 + 0.025 * (1 - yc)) * so, qg = (0.035 + 0.015 * (1 - yc)) * so, qb = (0.075 + 0.045 * (1 - yc)) * so; // wax, lit and unlit const xr = r * shade + spec * 0.2, xg = g * shade + spec * 0.192, xb = b * shade + spec * 0.18; const yr = 0.085 * off * ro, yg = 0.06 * off * ro, yb = 0.12 * off * ro; let cr = qr + (lr - qr) * L, cg = qg + (lg - qg) * L, cb = qb + (lb - qb) * L; cr += (yr + (xr - yr) * L - cr) * waxA; cg += (yg + (xg - yg) * L - cg) * waxA; cb += (yb + (xb - yb) * L - cb) * waxA; out[o4] = (cr * edge + 0.75 * line) * 255; out[o4 + 1] = (cg * edge + 0.8 * line) * 255; out[o4 + 2] = (cb * edge + line) * 255; out[o4 + 3] = (ga > 1 ? 1 : ga) * 255; } } } function drawGlass() { if (mode === 'gl') { if (!gl || gl.isContextLost()) return; gl.viewport(0, 0, glass.width, glass.height); gl.uniform2f(uRes, glass.width, glass.height); gl.uniform1f(uCell, glass.width / GW); gl.uniform1f(uDome, DOME); gl.uniform1f(uLight, light); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, GW, GH, gl.RGBA, gl.UNSIGNED_BYTE, data); gl.drawArrays(gl.TRIANGLES, 0, 3); } else { shade2D(); ctx2.putImageData(img2, 0, 0); } } /* ---------- layout ---------- */ function layout() { W = stage.clientWidth; H = stage.clientHeight; const dpr = window.devicePixelRatio || 1; dprG = Math.min(dpr, [3, 2, 1.5, 1][quality]); dpr2 = Math.min(dpr, [2, 2, 1.5, 1][quality]); S = Math.max(150, Math.min(H * 0.74, W * 1.5)); // lamp height in px by = H - Math.max(S * 0.14, Math.min(58, H * 0.12)) - 14; // where the base meets the desk ty = by - S; cx = W / 2; horizon = by - 0.055 * S; gH = GLASS_H * S; scene.width = Math.max(1, Math.round(W * dpr2)); scene.height = Math.max(1, Math.round(H * dpr2)); // The front canvas only has to cover the lamp, not the whole room. fX = Math.floor(cx - 0.16 * S); fY = Math.floor(ty - 0.01 * S); const fw = Math.ceil(0.32 * S), fh = Math.ceil(1.04 * S); front.width = Math.max(1, Math.round(fw * dpr2)); front.height = Math.max(1, Math.round(fh * dpr2)); front.style.left = fX + 'px'; front.style.top = fY + 'px'; front.style.width = fw + 'px'; front.style.height = fh + 'px'; // Glass canvas, snapped to whole device pixels so its edges stay crisp. const gw = Math.max(2, Math.round(2 * XH * gH * dprG)), gh = Math.max(2, Math.round(Y_SPAN * gH * dprG)); glass.width = mode === 'gl' ? gw : GW; glass.height = mode === 'gl' ? gh : GH; glass.style.left = Math.round((cx - XH * gH) * dprG) / dprG + 'px'; glass.style.top = Math.round((by - GLASS_BOTTOM * S - (Y_BOT + Y_SPAN) * gH) * dprG) / dprG + 'px'; glass.style.width = gw / dprG + 'px'; glass.style.height = gh / dprG + 'px'; // Switch: on the desk, in front of the lamp and a little to the right. swW = Math.max(62, Math.min(92, S * 0.125)); swH = swW * 76 / 120; swX = Math.min(cx + S * 0.2, W - swW * 0.5 - 10); swY = Math.min(by + Math.max(S * 0.105, swH * 0.5 + 22), H - swH * 0.5 - 8); swBtn.style.left = swX - swW * 0.5 + 'px'; swBtn.style.top = swY - swH * 0.5 + 'px'; swBtn.style.width = swW + 'px'; swBtn.style.height = swH + 'px'; drawScene(); drawFront(); drawnLight = light; drawGlass(); // the canvas was just resized, so refill it } /* ---------- the room: wall, desk, lamp cord (behind the glass) ---------- */ function cordPath(c) { const x0 = cx + 0.125 * S, y0 = by - 0.014 * S; // out from behind the base const x1 = swX - swW * 0.5 + 3, y1 = swY + swH * 0.03; // into the switch c.beginPath(); c.moveTo(x0, y0); c.bezierCurveTo(x0 + 0.1 * S, y0 + 0.01 * S, x1 - 0.07 * S, y1 + 0.012 * S, x1, y1); const x2 = swX + swW * 0.5 - 3; c.moveTo(x2, y1); c.bezierCurveTo(x2 + 0.12 * S, y1, W - 0.04 * S, y1 + 0.05 * S, W + 20, y1 + 0.07 * S); } function drawScene() { const c = sctx; c.setTransform(dpr2, 0, 0, dpr2, 0, 0); // Wall. let g = c.createLinearGradient(0, 0, 0, horizon); g.addColorStop(0, '#08070f'); g.addColorStop(1, '#15112a'); c.fillStyle = g; c.fillRect(0, 0, W, horizon + 1); if (light > 0.004) { // the lamp's glow on the wall const gy = ty + 0.56 * S, r = 0.95 * S; g = c.createRadialGradient(cx, gy, 0, cx, gy, r); g.addColorStop(0, rgba(255, 120, 160, 0.36 * light)); g.addColorStop(0.22, rgba(205, 90, 235, 0.24 * light)); g.addColorStop(0.55, rgba(115, 62, 215, 0.10 * light)); g.addColorStop(1, rgba(115, 62, 215, 0)); c.fillStyle = g; c.fillRect(0, 0, W, horizon + 1); } // Desk. g = c.createLinearGradient(0, horizon, 0, H); g.addColorStop(0, '#170d0a'); g.addColorStop(0.3, '#24140e'); g.addColorStop(1, '#3a2218'); c.fillStyle = g; c.fillRect(0, horizon, W, H - horizon); // Skirting board where the wall meets the desk. const sk = Math.max(6, 0.028 * S); c.fillStyle = 'rgba(8,6,18,0.45)'; c.fillRect(0, horizon - sk, W, sk); c.fillStyle = rgba(255, 200, 220, 0.04 + 0.05 * light); c.fillRect(0, horizon - sk, W, 1); c.lineWidth = 1; for (let i = 0; i < 18; i++) { // wood grain const t = (i + 0.5 + 0.4 * (rnd(i * 7.13) - 0.5)) / 18; const y = horizon + 3 + (H - horizon) * t * t * 0.92; const amp = (2 + 5 * rnd(i * 3.7)) * t, ph = rnd(i * 1.9) * 6.283, k = 0.004 + 0.004 * rnd(i * 5.1); c.strokeStyle = i % 3 ? 'rgba(0,0,0,0.16)' : 'rgba(255,190,140,0.035)'; c.beginPath(); c.moveTo(-10, y); for (let x = 0; x <= W + 40; x += 40) c.lineTo(x, y + amp * Math.sin(x * k + ph)); c.stroke(); } c.fillStyle = 'rgba(255,210,180,0.07)'; c.fillRect(0, horizon, W, 1.5); if (light > 0.004) { // pool of light on the desk c.save(); c.beginPath(); c.rect(0, horizon, W, H - horizon); c.clip(); c.translate(cx, by + 0.01 * S); c.scale(1, 0.26); const r = 0.62 * S; g = c.createRadialGradient(0, 0, 0, 0, 0, r); g.addColorStop(0, rgba(255, 120, 170, 0.34 * light)); g.addColorStop(0.5, rgba(200, 80, 210, 0.14 * light)); g.addColorStop(1, rgba(200, 80, 210, 0)); c.fillStyle = g; c.fillRect(-r, -r, 2 * r, 2 * r); c.restore(); } // Contact shadow under the base. c.save(); c.translate(cx, by - 0.004 * S); c.scale(1, 0.2); let r = 0.23 * S; g = c.createRadialGradient(0, 0, r * 0.45, 0, 0, r); g.addColorStop(0, 'rgba(0,0,0,0.6)'); g.addColorStop(1, 'rgba(0,0,0,0)'); c.fillStyle = g; c.fillRect(-r, -r, 2 * r, 2 * r); c.restore(); // Top face of the collar that holds the glass. c.fillStyle = '#34343e'; c.beginPath(); c.ellipse(cx, by - COLLAR_TOP * S, 0.112 * S, 0.017 * S, 0, 0, Math.PI * 2); c.fill(); // Cord. const cw = Math.max(3, 0.011 * S); c.lineCap = 'round'; c.lineJoin = 'round'; c.save(); c.translate(0, cw * 0.45); cordPath(c); c.strokeStyle = 'rgba(0,0,0,0.35)'; c.lineWidth = cw + 3; c.stroke(); c.restore(); cordPath(c); c.strokeStyle = '#0b0b10'; c.lineWidth = cw; c.stroke(); c.save(); c.translate(0, -cw * 0.22); cordPath(c); c.strokeStyle = 'rgba(255,255,255,0.08)'; c.lineWidth = Math.max(1, cw * 0.3); c.stroke(); c.restore(); // Vignette. g = c.createRadialGradient(W / 2, H * 0.45, Math.min(W, H) * 0.35, W / 2, H * 0.45, Math.hypot(W, H) * 0.62); g.addColorStop(0, 'rgba(0,0,0,0)'); g.addColorStop(1, 'rgba(0,0,0,0.5)'); c.fillStyle = g; c.fillRect(0, 0, W, H); } /* ---------- metal base and cap, and the shine on the glass (in front of the wax) ---------- */ function drawFront() { const c = fctx, X = u => cx + u * S, Y = v => by - v * S; c.setTransform(dpr2, 0, 0, dpr2, -fX * dpr2, -fY * dpr2); c.clearRect(0, 0, W, H); const metal = (x0, x1) => { const g = c.createLinearGradient(x0, 0, x1, 0); g.addColorStop(0, '#14141a'); g.addColorStop(0.10, '#4d4d5a'); g.addColorStop(0.27, '#e9e9f2'); g.addColorStop(0.40, '#a3a3b2'); g.addColorStop(0.62, '#3a3a46'); g.addColorStop(0.84, '#6c6c7b'); g.addColorStop(1, '#121218'); return g; }; // Top face of the collar, in front of the glass. const facePath = () => { c.beginPath(); c.moveTo(X(-0.112), Y(COLLAR_TOP)); c.ellipse(X(0), Y(COLLAR_TOP), 0.112 * S, 0.017 * S, 0, Math.PI, 0, true); c.closePath(); }; facePath(); let g = c.createLinearGradient(X(-0.112), 0, X(0.112), 0); g.addColorStop(0, '#2a2a33'); g.addColorStop(0.3, '#c9c9d6'); g.addColorStop(0.6, '#6a6a78'); g.addColorStop(1, '#22222a'); c.fillStyle = g; c.fill(); if (light > 0.004) { c.fillStyle = rgba(255, 150, 150, 0.4 * light); c.fill(); } else { c.fillStyle = 'rgba(5,5,12,0.6)'; c.fill(); } // Base: a cone flaring down to the desk, with a collar at the top. const basePath = () => { c.beginPath(); c.moveTo(X(-0.150), Y(0.012)); c.lineTo(X(-0.098), Y(0.228)); c.lineTo(X(-0.112), Y(0.238)); c.lineTo(X(-0.112), Y(COLLAR_TOP)); c.ellipse(X(0), Y(COLLAR_TOP), 0.112 * S, 0.017 * S, 0, Math.PI, 0, true); c.lineTo(X(0.112), Y(0.238)); c.lineTo(X(0.098), Y(0.228)); c.lineTo(X(0.150), Y(0.012)); c.ellipse(X(0), Y(0.012), 0.150 * S, 0.024 * S, 0, 0, Math.PI); c.closePath(); }; basePath(); c.fillStyle = metal(X(-0.15), X(0.15)); c.fill(); c.save(); basePath(); c.clip(); g = c.createLinearGradient(0, Y(COLLAR_TOP), 0, Y(-0.012)); g.addColorStop(0, rgba(255, 130, 150, 0.36 * light)); // warm light spilling down from the glass g.addColorStop(0.45, rgba(255, 130, 150, 0.05 * light)); g.addColorStop(1, 'rgba(0,0,0,0.4)'); c.fillStyle = g; c.fillRect(X(-0.16), Y(COLLAR_TOP + 0.01), 0.32 * S, 0.3 * S); if (light < 0.996) { c.fillStyle = rgba(5, 5, 12, 0.6 * (1 - light)); c.fillRect(X(-0.16), Y(COLLAR_TOP + 0.01), 0.32 * S, 0.3 * S); } c.restore(); c.lineWidth = Math.max(1, 0.004 * S); c.strokeStyle = 'rgba(0,0,0,0.45)'; c.beginPath(); c.ellipse(X(0), Y(0.233), 0.105 * S, 0.016 * S, 0, 0, Math.PI); c.stroke(); c.lineWidth = 1; c.strokeStyle = rgba(255, 255, 255, 0.2 + 0.15 * light); c.beginPath(); c.ellipse(X(0), Y(COLLAR_TOP), 0.112 * S, 0.017 * S, 0, 0, Math.PI); c.stroke(); // Cap. const capPath = () => { c.beginPath(); c.moveTo(X(-0.066), Y(CAP_BOTTOM)); c.lineTo(X(-0.050), Y(CAP_TOP)); c.ellipse(X(0), Y(CAP_TOP), 0.050 * S, 0.022 * S, 0, Math.PI, Math.PI * 2); c.lineTo(X(0.066), Y(CAP_BOTTOM)); c.ellipse(X(0), Y(CAP_BOTTOM), 0.066 * S, 0.010 * S, 0, 0, Math.PI, true); c.closePath(); }; capPath(); c.fillStyle = metal(X(-0.066), X(0.066)); c.fill(); c.save(); capPath(); c.clip(); g = c.createLinearGradient(0, Y(CAP_BOTTOM - 0.01), 0, Y(CAP_BOTTOM + 0.1)); g.addColorStop(0, rgba(255, 140, 160, 0.45 * light)); g.addColorStop(1, rgba(255, 140, 160, 0)); c.fillStyle = g; c.fillRect(X(-0.07), Y(CAP_BOTTOM - 0.012), 0.14 * S, 0.12 * S); if (light < 0.996) { c.fillStyle = rgba(5, 5, 12, 0.6 * (1 - light)); c.fillRect(X(-0.07), Y(CAP_BOTTOM - 0.012), 0.14 * S, 0.12 * S); } c.restore(); // Reflections on the glass. const gby = by - GLASS_BOTTOM * S, gty = by - GLASS_TOP * S, wb = GL_B * gH, wt = GL_A * gH, sa = 0.3 + 0.7 * light; c.lineCap = 'round'; c.strokeStyle = rgba(255, 255, 255, 0.10 * sa); c.lineWidth = 0.05 * gH; c.beginPath(); c.moveTo(cx - 0.66 * wb, gby - 0.05 * gH); c.lineTo(cx - 0.66 * wt, gty + 0.04 * gH); c.stroke(); c.strokeStyle = rgba(255, 255, 255, 0.22 * sa); c.lineWidth = 0.014 * gH; c.beginPath(); c.moveTo(cx - 0.70 * wb, gby - 0.07 * gH); c.lineTo(cx - 0.70 * wt, gty + 0.05 * gH); c.stroke(); c.strokeStyle = rgba(255, 255, 255, 0.09 * sa); c.lineWidth = 0.02 * gH; c.beginPath(); c.moveTo(cx + 0.74 * wb, gby - 0.08 * gH); c.lineTo(cx + 0.74 * wt, gty + 0.06 * gH); c.stroke(); c.lineCap = 'butt'; c.lineWidth = 1; c.strokeStyle = 'rgba(210,220,255,0.12)'; c.beginPath(); c.moveTo(cx - wb, gby); c.lineTo(cx - wt, gty); c.moveTo(cx + wb, gby); c.lineTo(cx + wt, gty); c.stroke(); } /* ---------- switch ---------- */ swBtn.addEventListener('click', () => { lampOn = !lampOn; swBtn.setAttribute('aria-checked', lampOn ? 'true' : 'false'); }); /* ---------- main loop ---------- */ let last = 0, slowFrames = 0, workAcc = 0, workFrames = 0; function frame(now) { requestAnimationFrame(frame); const t0 = performance.now(); if (!last) last = now; const raw = (now - last) / 1000; last = now; const dt = raw > 0.05 ? 0.05 : raw < 0 ? 0 : raw; // The bulb brightens and dims quickly; the heat that drives the wax lags behind. const lt = lampOn ? 1 : 0; if (light < lt) light = Math.min(lt, light + dt * 1.3); else if (light > lt) light = Math.max(lt, light - dt * 2.4); const ht = lampOn ? 1 : 0; if (heat < ht) heat = Math.min(ht, heat + dt * 0.4); else if (heat > ht) heat = Math.max(ht, heat - dt * 1.5); // Advance the wax with a step of about 1/120 s, however fast the display refreshes. exposure(); const steps = Math.max(1, Math.min(5, Math.round(dt * 120))), h = dt / steps; for (let s = 0; s < steps; s++) { simT += h; step(h, simT); } buildField(simT); drawGlass(); if (Math.abs(light - drawnLight) > 0.002) { drawScene(); drawFront(); drawnLight = light; } // If this device cannot keep up, trade resolution for frame rate. workAcc += performance.now() - t0; workFrames++; if (workFrames >= 90) { if (workAcc / workFrames > 7 && quality < 3) { quality++; layout(); } workAcc = 0; workFrames = 0; } if (raw > 0.25) slowFrames = 0; // the tab was paused, not struggling else if (raw > 0.024) { if (++slowFrames > 60 && quality < 3) { quality++; slowFrames = 0; layout(); } } else if (slowFrames > 0) slowFrames--; } let resizeQueued = false; const onResize = () => { if (resizeQueued) return; resizeQueued = true; requestAnimationFrame(() => { resizeQueued = false; layout(); }); }; window.addEventListener('resize', onResize); window.addEventListener('orientationchange', onResize); /* ---------- start ---------- */ initParticles(); if (setupGL()) { mode = 'gl'; glass.addEventListener('webglcontextlost', e => e.preventDefault()); glass.addEventListener('webglcontextrestored', () => { if (!setupGL()) { use2D(); layout(); } }); } else { use2D(); } layout(); warmUp(18); // start with the lamp already going, not cold requestAnimationFrame(frame);})();</script></body></html>