Lava lamp v1 · OpenCode21.11.2026 08:02
<!doctype html><html lang="en"><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover, user-scalable=no" /><meta name="theme-color" content="#160f18" /><title>Lava Lamp</title><link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect x='8' y='20' width='16' height='8' rx='3' fill='%23d9b77a'/%3E%3Cpath d='M11 20V8a5 5 0 0 1 10 0v12z' fill='%23ff9a3c'/%3E%3Ccircle cx='16' cy='14' r='4' fill='%23c23c0e'/%3E%3C/svg%3E" /><style> :root { --amber: #ffca7a; } * { box-sizing: border-box; margin: 0; padding: 0; } html, body { height: 100%; } body { min-height: 100vh; min-height: 100dvh; background: radial-gradient(1100px 780px at 50% -12%, #2b2033 0%, #191222 55%, #0e0a14 100%); font-family: ui-rounded, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif; color: #f2e7d5; display: flex; align-items: center; justify-content: center; overflow: hidden; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; touch-action: manipulation; } main { display: flex; flex-direction: column; align-items: center; justify-content: center; width: 100%; padding: max(10px, env(safe-area-inset-top)) 14px max(12px, env(safe-area-inset-bottom)); gap: 12px; } h1 { font-size: clamp(15px, 4vw, 20px); font-weight: 800; letter-spacing: .24em; text-transform: uppercase; color: var(--amber); text-shadow: 0 0 22px rgba(255,150,60,.35); } .stage { display: flex; align-items: center; justify-content: center; width: 100%; min-height: 0; } canvas { width: min(94vw, 400px, calc((100vh - 196px) * 0.6)); width: min(94vw, 400px, calc((100dvh - 196px) * 0.6)); aspect-ratio: 3 / 5; display: block; border-radius: 12px; box-shadow: 0 18px 50px rgba(0,0,0,.55), 0 0 0 1px rgba(255,205,140,.08); } .power { display: flex; align-items: center; gap: 12px; padding: 10px 20px; border-radius: 999px; cursor: pointer; border: 1px solid rgba(255,220,160,.22); background: linear-gradient(180deg, #2c2333, #1a1422); color: #e8dcc8; font: inherit; box-shadow: inset 0 1px 0 rgba(255,255,255,.08), 0 6px 16px rgba(0,0,0,.45); transition: box-shadow .25s ease, transform .1s ease; touch-action: manipulation; } .power:active { transform: scale(.97); } .power:focus-visible { outline: 2px solid rgba(255,205,140,.8); outline-offset: 3px; } .track { width: 46px; height: 26px; border-radius: 999px; position: relative; flex: none; background: #3a2e38; box-shadow: inset 0 2px 6px rgba(0,0,0,.55); transition: background .25s ease, box-shadow .25s ease; } .knob { position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, #fff, #cfc4b4 60%, #8d7f6f); box-shadow: 0 1px 3px rgba(0,0,0,.6); transition: transform .28s cubic-bezier(.3,1.35,.45,1); } .power.on .track { background: linear-gradient(90deg, #b3541e, #ffb347); box-shadow: inset 0 1px 3px rgba(0,0,0,.35), 0 0 16px rgba(255,150,60,.55); } .power.on .knob { transform: translateX(20px); } .state { font-weight: 700; letter-spacing: .1em; text-transform: uppercase; font-size: .82rem; min-width: 2.4em; text-align: left; color: #ffd489; transition: color .25s ease; } .power:not(.on) .state { color: #8f8397; } .hint { font-size: .68rem; letter-spacing: .06em; color: #7d7386; text-align: center; text-transform: uppercase; font-weight: 600; } @media (max-height: 560px) { h1 { display: none; } main { gap: 8px; } .hint { display: none; } canvas { width: min(96vw, 380px, calc((100vh - 110px) * 0.6)); width: min(96vw, 380px, calc((100dvh - 110px) * 0.6)); } }</style></head><body> <main> <h1>Lava Lamp</h1> <div class="stage"> <canvas id="lamp" width="420" height="700" aria-label="Animated lava lamp"></canvas> </div> <button id="power" class="power on" role="switch" aria-checked="true" aria-label="Toggle lamp power"> <span class="track"><span class="knob"></span></span> <span class="state">On</span> </button> <p class="hint">Tap the switch or press L</p> </main><script>(() => { 'use strict'; // ---------- setup ---------- const W = 420, H = 700, DESK_Y = 648; const TUBE = { cx: 210, cy: 170, domeR: 88, x0: 122, x1: 298, yTop: 82, yBottom: 556 }; const canvas = document.getElementById('lamp'); const ctx = canvas.getContext('2d', { alpha: false }); const reduced = window.matchMedia('(prefers-reduced-motion: reduce)').matches; function resize() { const dpr = Math.min(window.devicePixelRatio || 1, 2); canvas.width = Math.round(W * dpr); canvas.height = Math.round(H * dpr); } window.addEventListener('resize', resize); resize(); // ---------- helpers ---------- const clamp01 = v => v < 0 ? 0 : v > 1 ? 1 : v; const mixc = (a, b, k) => [a[0]+(b[0]-a[0])*k, a[1]+(b[1]-a[1])*k, a[2]+(b[2]-a[2])*k]; const rgba = (c, a) => `rgba(${c[0]|0},${c[1]|0},${c[2]|0},${a})`; const rnd = (a, b) => a + Math.random() * (b - a); const PAL = { liquidTop: [[255, 224, 138], [66, 51, 72]], liquidBottom: [[255, 146, 58], [33, 26, 42]], blobLight: [[255, 226, 160], [118, 84, 70]], blobMid: [[255, 150, 50], [88, 62, 54]], blobEdge: [[226, 80, 20], [52, 38, 36]] }; // ---------- state ---------- const lamp = { on: true, power: 1 }; let t = 0; let blobs = []; let splitCd = 0; function makeBlob(x, y, r, heat) { return { x, y, r, heat, vx: 0, vy: 0, phase: Math.random() * Math.PI * 2 }; } function initBlobs() { blobs = []; for (let i = 0; i < 10; i++) { const r = rnd(15, 24); blobs.push(makeBlob( rnd(TUBE.x0 + r + 6, TUBE.x1 - r - 6), rnd(110, 540 - r), r, rnd(0.25, 0.8) )); } } initBlobs(); // ---------- physics ---------- function update(dt) { splitCd -= dt; for (const b of blobs) { // warmth gradient: bottom is hot, top is cold const bottomness = (b.y - TUBE.yTop) / (TUBE.yBottom - TUBE.yTop); const targetHeat = lamp.power * clamp01(bottomness * 1.35 - 0.26); const rate = targetHeat > b.heat ? 1.7 : 0.95; b.heat += (targetHeat - b.heat) * Math.min(1, dt * rate); // warm rises, cool sinks const vyT = (0.5 - b.heat) * 155; b.vy += (vyT - b.vy) * Math.min(1, dt * 2.2); // gentle lateral drift b.vx += (Math.sin(t * 1.35 + b.phase * 2.17) * 30 - b.vx) * Math.min(1, dt * 0.85); b.x += b.vx * dt; b.y += b.vy * dt; // rounded dome (only applies inside dome region) const dx = b.x - TUBE.cx; const Rdome = TUBE.domeR - b.r; if (b.y < TUBE.cy && Rdome > 0) { const dy = b.y - TUBE.cy; const dd = Math.hypot(dx, dy); if (dd > Rdome) { const s = Rdome / dd; b.x = TUBE.cx + dx * s; b.y = TUBE.cy + dy * s; const nx = dx / dd, ny = dy / dd; const vn = b.vx * nx + b.vy * ny; if (vn > 0) { b.vx -= 1.35 * vn * nx; b.vy -= 1.35 * vn * ny; } } } // side walls const minX = TUBE.x0 + b.r, maxX = TUBE.x1 - b.r; if (b.x < minX) { b.x = minX; if (b.vx < 0) b.vx = -b.vx * 0.55; } else if (b.x > maxX) { b.x = maxX; if (b.vx > 0) b.vx = -b.vx * 0.55; } // floor const maxY = TUBE.yBottom - b.r - 1; if (b.y > maxY) { b.y = maxY; if (b.vy > 0) b.vy = 0; } } mergePass(); // oversized wax splits back apart if (splitCd <= 0) { const idx = blobs.findIndex(b => b.r > 52); if (idx >= 0) { splitBlob(idx); splitCd = 0.4; } } } function mergePass() { for (let iter = 0; iter < 4; iter++) { let merged = false; outer: for (let i = 0; i < blobs.length; i++) { for (let j = i + 1; j < blobs.length; j++) { const a = blobs[i], b = blobs[j]; const dx = a.x - b.x, dy = a.y - b.y; const d = Math.hypot(dx, dy); if (d >= (a.r + b.r) * 0.72) continue; const na = a.r * a.r, nb = b.r * b.r, A = na + nb, r = Math.sqrt(A); if (r > 62) { // too big to fuse: push apart along their normal const nx = dx / (d || 1), ny = dy / (d || 1); const rel = (a.vx - b.vx) * nx + (a.vy - b.vy) * ny; if (rel < 0) { const imp = -rel * 0.55; a.vx += nx * imp; a.vy += ny * imp; b.vx -= nx * imp; b.vy -= ny * imp; } continue; } const heat = (a.heat * na + b.heat * nb) / A; const x = (a.x * na + b.x * nb) / A; const y = (a.y * na + b.y * nb) / A; const vx = (a.vx * na + b.vx * nb) / A; const vy = (a.vy * na + b.vy * nb) / A; blobs.splice(j, 1); blobs.splice(i, 1); const np = makeBlob(x, y, r, heat); np.vx = vx; np.vy = vy; blobs.push(np); merged = true; break outer; } } if (!merged) break; } } function splitBlob(idx) { const b = blobs[idx]; const ang = Math.random() * Math.PI * 2; const r2 = b.r / Math.sqrt(2); const cx = Math.cos(ang), sy = Math.sin(ang); const off = r2 * 0.85; const a = makeBlob(b.x - cx * off, b.y - sy * off, r2, b.heat); const c = makeBlob(b.x + cx * off, b.y + sy * off, r2, b.heat); a.vx = b.vx - cx * 55; a.vy = b.vy - sy * 45; c.vx = b.vx + cx * 55; c.vy = b.vy + sy * 45; blobs.splice(idx, 1, a, c); } // ---------- drawing ---------- function rr(x, y, w, h, r) { ctx.beginPath(); ctx.moveTo(x + r, y); ctx.arcTo(x + w, y, x + w, y + h, r); ctx.arcTo(x + w, y + h, x, y + h, r); ctx.arcTo(x, y + h, x, y, r); ctx.arcTo(x, y, x + w, y, r); ctx.closePath(); } function liquidPath() { ctx.beginPath(); ctx.moveTo(TUBE.x0, TUBE.yBottom); ctx.lineTo(TUBE.x0, TUBE.cy); ctx.arc(TUBE.cx, TUBE.cy, TUBE.domeR, Math.PI, 0, false); ctx.lineTo(TUBE.x1, TUBE.yBottom); ctx.closePath(); } function outerGlassPath(inset) { const R = 100 - inset, yBottom = 566 - inset * 0.4; ctx.beginPath(); ctx.moveTo(TUBE.cx - R, yBottom); ctx.lineTo(TUBE.cx - R, TUBE.cy); ctx.arc(TUBE.cx, TUBE.cy, R, Math.PI, 0, false); ctx.lineTo(TUBE.cx + R, yBottom); ctx.closePath(); } function drawBlob(b, p) { const light = mixc(PAL.blobLight[1], PAL.blobLight[0], p); const mid = mixc(PAL.blobMid[1], PAL.blobMid[0], p); const edge = mixc(PAL.blobEdge[1], PAL.blobEdge[0], p); const amp = (0.045 + 0.075 * p) * (reduced ? 0.35 : 1); const tt = t * (1.5 + b.phase * 0.35); const pts = 14; ctx.beginPath(); for (let i = 0; i <= pts; i++) { const a = (i / pts) * Math.PI * 2; const rad = b.r * (1 + amp * (Math.sin(a * 3 + tt + b.phase) + 0.6 * Math.sin(a * 5 - tt * 1.3 + b.phase * 2))); const px = b.x + Math.cos(a) * rad; const py = b.y + Math.sin(a) * rad; if (i) ctx.lineTo(px, py); else ctx.moveTo(px, py); } ctx.closePath(); const g = ctx.createRadialGradient(b.x - b.r * 0.28, b.y - b.r * 0.32, b.r * 0.12, b.x, b.y, b.r * 1.05); g.addColorStop(0, rgba(light, 1)); g.addColorStop(0.55, rgba(mid, 1)); g.addColorStop(1, rgba(edge, 1)); ctx.fillStyle = g; ctx.fill(); ctx.strokeStyle = `rgba(40,16,8,${0.35 * (0.4 + 0.6 * p)})`; ctx.lineWidth = 1.2; ctx.stroke(); ctx.fillStyle = `rgba(255,255,255,${0.16 + 0.22 * p})`; ctx.beginPath(); ctx.ellipse(b.x - b.r * 0.3, b.y - b.r * 0.42, b.r * 0.34, b.r * 0.18, -0.5, 0, Math.PI * 2); ctx.fill(); } function drawBase() { // foot shadow on desk ctx.fillStyle = 'rgba(0,0,0,0.4)'; ctx.beginPath(); ctx.ellipse(210, 650, 118, 10, 0, 0, Math.PI * 2); ctx.fill(); // chrome body const g = ctx.createLinearGradient(0, 566, 0, 652); g.addColorStop(0, '#f3e6cf'); g.addColorStop(0.35, '#d3bf9e'); g.addColorStop(0.8, '#7d6e58'); g.addColorStop(1, '#443a2e'); rr(96, 566, 228, 86, 20); ctx.fillStyle = g; ctx.fill(); ctx.fillStyle = 'rgba(255,255,255,0.45)'; rr(102, 569, 216, 4, 2); ctx.fill(); ctx.fillStyle = 'rgba(0,0,0,0.35)'; rr(96, 646, 228, 6, 3); ctx.fill(); ctx.fillStyle = 'rgba(60,45,30,0.5)'; rr(120, 616, 180, 3, 1.5); ctx.fill(); } function drawCollar() { const g = ctx.createLinearGradient(0, 546, 0, 574); g.addColorStop(0, '#ffe1a2'); g.addColorStop(0.45, '#c9903a'); g.addColorStop(1, '#5f3f14'); rr(148, 546, 124, 28, 9); ctx.fillStyle = g; ctx.fill(); ctx.strokeStyle = 'rgba(20,10,4,0.55)'; ctx.lineWidth = 1.6; ctx.stroke(); ctx.fillStyle = 'rgba(255,255,255,0.5)'; rr(152, 549, 116, 3, 1.5); ctx.fill(); } function draw() { const p = lamp.power; const flick = reduced ? p : p * (0.93 + 0.07 * Math.sin(t * 11.3) * Math.sin(t * 5.7)); const dpr = canvas.width / W; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); // room let g = ctx.createLinearGradient(0, 0, 0, H); g.addColorStop(0, '#241b2d'); g.addColorStop(0.6, '#181124'); g.addColorStop(1, '#0d0a12'); ctx.fillStyle = g; ctx.fillRect(0, 0, W, H); if (p > 0.01) { ctx.fillStyle = `rgba(255,140,60,${0.05 * p})`; ctx.fillRect(0, 0, W, H); } // desk g = ctx.createLinearGradient(0, DESK_Y, 0, H); g.addColorStop(0, '#3b2c20'); g.addColorStop(0.18, '#2b1f18'); g.addColorStop(1, '#150f0b'); ctx.fillStyle = g; ctx.fillRect(0, DESK_Y, W, H - DESK_Y); ctx.fillStyle = 'rgba(255,210,160,0.16)'; ctx.fillRect(0, DESK_Y, W, 1.5); // warm reflection on desk ctx.save(); ctx.translate(210, 658); ctx.scale(1, 0.12); const rg = ctx.createRadialGradient(0, 0, 0, 0, 0, 135); rg.addColorStop(0, `rgba(255,155,75,${0.20 * flick + 0.02})`); rg.addColorStop(1, 'rgba(255,155,75,0)'); ctx.fillStyle = rg; ctx.beginPath(); ctx.arc(0, 0, 135, 0, Math.PI * 2); ctx.fill(); ctx.restore(); // ambient glow behind lamp ctx.save(); ctx.translate(210, 320); const ag = ctx.createRadialGradient(0, 0, 10, 0, 0, 300); ag.addColorStop(0, `rgba(255,160,80,${0.28 * flick})`); ag.addColorStop(0.55, `rgba(255,130,55,${0.10 * flick})`); ag.addColorStop(1, 'rgba(255,120,50,0)'); ctx.fillStyle = ag; ctx.fillRect(-300, -300, 600, 600); ctx.restore(); // lamp drawBase(); // liquid + wax, clipped to glass const liquidTop = mixc(PAL.liquidTop[1], PAL.liquidTop[0], p); const liquidBottom = mixc(PAL.liquidBottom[1], PAL.liquidBottom[0], p); ctx.save(); liquidPath(); ctx.clip(); const lg = ctx.createLinearGradient(0, TUBE.yTop, 0, TUBE.yBottom); lg.addColorStop(0, rgba(liquidTop, 1)); lg.addColorStop(1, rgba(liquidBottom, 1)); ctx.fillStyle = lg; ctx.fillRect(TUBE.x0 - 2, TUBE.yTop - 2, TUBE.x1 - TUBE.x0 + 4, TUBE.yBottom - TUBE.yTop + 4); // bulb glow rising from the base const bg = ctx.createRadialGradient(210, 545, 0, 210, 545, 120); bg.addColorStop(0, `rgba(255,222,150,${0.55 * flick})`); bg.addColorStop(1, 'rgba(255,222,150,0)'); ctx.fillStyle = bg; ctx.fillRect(96, 430, 228, 132); for (const b of blobs) drawBlob(b, p); ctx.restore(); // glass highlight streaks ctx.save(); outerGlassPath(2); ctx.clip(); const hl = ctx.createLinearGradient(126, 0, 154, 0); hl.addColorStop(0, 'rgba(255,255,255,0.30)'); hl.addColorStop(1, 'rgba(255,255,255,0)'); ctx.fillStyle = hl; ctx.fillRect(126, 66, 30, 500); const hl2 = ctx.createLinearGradient(294, 0, 280, 0); hl2.addColorStop(0, 'rgba(255,255,255,0.16)'); hl2.addColorStop(1, 'rgba(255,255,255,0)'); ctx.fillStyle = hl2; ctx.fillRect(280, 66, 16, 500); ctx.restore(); // glass outline outerGlassPath(0); ctx.strokeStyle = 'rgba(255,240,220,0.28)'; ctx.lineWidth = 2.4; ctx.stroke(); drawCollar(); // vignette const vg = ctx.createRadialGradient(210, 310, 250, 210, 330, 530); vg.addColorStop(0, 'rgba(0,0,0,0)'); vg.addColorStop(1, 'rgba(0,0,0,0.42)'); ctx.fillStyle = vg; ctx.fillRect(0, 0, W, H); } // ---------- loop ---------- let last = performance.now(); function frame(now) { let dt = (now - last) / 1000; last = now; if (dt > 0.05) dt = 0.05; t += dt * (reduced ? 0.4 : 1); const target = lamp.on ? 1 : 0; lamp.power += (target - lamp.power) * Math.min(1, dt * 3.0); if (Math.abs(target - lamp.power) < 0.003) lamp.power = target; update(dt); draw(); requestAnimationFrame(frame); } requestAnimationFrame(frame); // ---------- switch ---------- const btn = document.getElementById('power'); const stateEl = btn.querySelector('.state'); function setPower(on) { lamp.on = on; btn.classList.toggle('on', on); btn.setAttribute('aria-checked', String(on)); stateEl.textContent = on ? 'On' : 'Off'; } btn.addEventListener('click', () => setPower(!lamp.on)); window.addEventListener('keydown', e => { if (e.key === 'l' || e.key === 'L') setPower(!lamp.on); if (e.key === ' ' && e.target === document.body) { e.preventDefault(); setPower(!lamp.on); } });})();</script></body></html>