Lava lamp v1 · Antigravity CLI19.11.2026 04:10
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover"><meta name="theme-color" content="#09070e"><title>Lava Lamp</title><link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' rx='14' fill='%23100c18'/%3E%3Cpath d='M25 8h14l4 8H21z' fill='%238c929f'/%3E%3Cpath d='M21 16h22l6 24-8 10H23l-8-10z' fill='%232e1047'/%3E%3Ccircle cx='33' cy='31' r='7' fill='%23ff5e1a'/%3E%3Ccircle cx='28' cy='43' r='6' fill='%23e82424'/%3E%3Cpath d='M23 50h18l5 9H18z' fill='%237d8390'/%3E%3C/svg%3E"><style> *, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; } html, body { width: 100%; height: 100%; height: 100dvh; overflow: hidden; background: #09070e; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; user-select: none; -webkit-user-select: none; touch-action: manipulation; } #stage { position: relative; width: 100%; height: 100%; overflow: hidden; } #lampCanvas { display: block; width: 100%; height: 100%; cursor: default; } /* Physical inline cord rocker switch resting on the wooden desk */ #powerSwitch { --glow: 1; position: absolute; left: 0; top: 0; transform: translate(-50%, -50%); width: 112px; height: 52px; border: none; padding: 0; background: transparent; cursor: pointer; outline: none; z-index: 10; touch-action: manipulation; -webkit-appearance: none; appearance: none; } #powerSwitch:focus-visible .sw-shell { box-shadow: 0 0 0 2px #09070e, 0 0 0 4px rgba(255, 162, 74, 0.88), 0 10px 22px rgba(0, 0, 0, 0.7); } .sw-shadow { position: absolute; inset: 4px -5px -8px -5px; border-radius: 17px; background: radial-gradient(ellipse at 50% 45%, rgba(0, 0, 0, 0.82) 0%, rgba(0, 0, 0, 0.42) 58%, transparent 82%); filter: blur(4px); pointer-events: none; } .sw-collar { position: absolute; left: -8px; top: 50%; transform: translateY(-50%); width: 11px; height: 14px; border-radius: 4px 0 0 4px; background: linear-gradient(180deg, #2d2a34 0%, #15131a 55%, #0a090e 100%); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), -2px 3px 4px rgba(0, 0, 0, 0.65); pointer-events: none; } .sw-shell { position: relative; width: 100%; height: 100%; border-radius: 14px; padding: 7px 10px 8px 11px; display: flex; align-items: center; justify-content: space-between; gap: 8px; background: linear-gradient( 180deg, rgba(54, 49, 58, 0.97) 0%, rgba(32, 28, 36, 0.99) 28%, rgba(20, 17, 24, 0.99) 82%, rgba(11, 9, 15, 1) 100% ); box-shadow: inset 0 1.5px 1px rgba(255, 214, 175, calc(0.08 + var(--glow) * 0.17)), inset 0 -3px 4px rgba(0, 0, 0, 0.72), inset 1px 0 1.5px rgba(255, 140, 62, calc(var(--glow) * 0.24)), 0 6px 12px rgba(0, 0, 0, 0.66), 0 2px 3px rgba(0, 0, 0, 0.78), 0 -1px 12px rgba(255, 96, 32, calc(var(--glow) * 0.16)); transition: transform 90ms ease, box-shadow 260ms ease; } #powerSwitch:active .sw-shell { transform: translateY(1.5px) scale(0.985); } .sw-pilot { flex: 0 0 auto; width: 9px; height: 9px; border-radius: 50%; position: relative; background: radial-gradient(circle at 38% 35%, #4f3a34 0%, #211718 70%, #110c0e 100%); box-shadow: inset 0 1px 1.5px rgba(0, 0, 0, 0.85), 0 0.5px 0 rgba(255, 255, 255, 0.10); transition: background 240ms ease, box-shadow 240ms ease; } #powerSwitch[aria-checked="true"] .sw-pilot { background: radial-gradient(circle at 36% 34%, #fff1be 0%, #ff8826 48%, #d42f12 100%); box-shadow: 0 0 6px 1.5px rgba(255, 128, 40, 0.88), 0 0 14px 3px rgba(255, 72, 22, 0.44), inset 0 0.5px 1px rgba(255, 255, 255, 0.75); } .sw-well { flex: 1 1 auto; height: 30px; border-radius: 7px; padding: 2.5px; background: #0a080e; box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.94), 0 1px 0 rgba(255, 255, 255, 0.08); display: flex; perspective: 220px; } .sw-rocker { width: 100%; height: 100%; border-radius: 5px; display: flex; align-items: center; justify-content: space-between; padding: 0 11px; position: relative; transition: transform 130ms cubic-bezier(0.22, 1, 0.36, 1), background 160ms ease, box-shadow 160ms ease; } #powerSwitch[aria-checked="false"] .sw-rocker { transform: rotateY(-12deg); background: linear-gradient(90deg, #17151d 0%, #221f29 46%, #35313e 54%, #433d4d 100%); box-shadow: inset 2px 1px 3px rgba(0, 0, 0, 0.70), inset -1px 1px 1px rgba(255, 255, 255, 0.14), -2px 0 3px rgba(0, 0, 0, 0.48); } #powerSwitch[aria-checked="true"] .sw-rocker { transform: rotateY(12deg); background: linear-gradient(90deg, #473f4e 0%, #393241 46%, #26212d 54%, #1a1720 100%); box-shadow: inset -2px 1px 3px rgba(0, 0, 0, 0.72), inset 1px 1px 1px rgba(255, 214, 178, 0.19), 2px 0 3px rgba(0, 0, 0, 0.48); } .sw-rocker::after { content: ""; position: absolute; left: 50%; top: 3px; bottom: 3px; width: 1px; transform: translateX(-50%); background: linear-gradient(180deg, rgba(255, 255, 255, 0.10), rgba(0, 0, 0, 0.48)); } .sw-glyph { font-size: 11px; font-weight: 700; letter-spacing: 0.02em; line-height: 1; transition: color 200ms ease, text-shadow 200ms ease; pointer-events: none; } .sw-glyph-off { color: rgba(205, 200, 218, 0.40); } .sw-glyph-on { color: rgba(205, 200, 218, 0.36); } #powerSwitch[aria-checked="false"] .sw-glyph-off { color: rgba(224, 220, 236, 0.80); } #powerSwitch[aria-checked="true"] .sw-glyph-on { color: #ffa952; text-shadow: 0 0 6px rgba(255, 126, 36, 0.78); } .sw-caption { position: absolute; left: 50%; top: calc(100% + 7px); transform: translateX(-50%); font-size: 10px; font-weight: 600; letter-spacing: 0.16em; text-transform: uppercase; color: rgba(212, 196, 182, 0.50); text-shadow: 0 1px 2px rgba(0, 0, 0, 0.82); white-space: nowrap; pointer-events: none; transition: color 220ms ease; } #powerSwitch[aria-checked="true"] .sw-caption { color: rgba(255, 198, 146, 0.78); } @media (max-width: 390px), (max-height: 620px) { #powerSwitch { width: 104px; height: 48px; } .sw-well { height: 27px; } }</style></head><body> <main id="stage"> <canvas id="lampCanvas" aria-label="Glowing glass lava lamp on a wooden desk"></canvas> <button id="powerSwitch" type="button" role="switch" aria-checked="true" aria-label="Turn lava lamp off"> <span class="sw-shadow" aria-hidden="true"></span> <span class="sw-collar" aria-hidden="true"></span> <span class="sw-shell"> <span class="sw-pilot" aria-hidden="true"></span> <span class="sw-well"> <span class="sw-rocker"> <span class="sw-glyph sw-glyph-off" aria-hidden="true">O</span> <span class="sw-glyph sw-glyph-on" aria-hidden="true">I</span> </span> </span> </span> <span class="sw-caption" aria-hidden="true">ON</span> </button> </main><script>(() => { 'use strict'; // ========================================================================= // 1. SHARED BOTTLE & LAMP GEOMETRY (identical in JS physics, WebGL, & 2D) // ========================================================================= const GLASS_Y0 = 0.255; const GLASS_Y1 = 0.865; const GLASS_H = GLASS_Y1 - GLASS_Y0; // 0.610 const WALL_T = 0.012; const MAX_BLOBS = 24; // C2-continuous Wyvill polynomial metaball kernel: // f_i(d) = max(0, 1 - d^2 / (META_REACH * r_i)^2)^3 // Zero creases, zero cutoff seams, exact visual radius = r_i at META_ISO, // silky liquid bridging right when two blobs approach & touch! const META_REACH = 2.08; const META_ISO = Math.pow(1.0 - 1.0 / (META_REACH * META_REACH), 3); // ~0.4547 function smin(a, b, k) { const h = Math.max(k - Math.abs(a - b), 0.0) / k; return Math.min(a, b) - h * h * k * 0.25; } function glassOuterW(by) { const wLow = 0.156 + 0.56 * by; const wHigh = 0.296 - 0.154 * by; return smin(wLow, wHigh, 0.055); } function glassInnerW(by) { return glassOuterW(by) - WALL_T; } function clamp(v, lo, hi) { return v < lo ? lo : (v > hi ? hi : v); } function lerp(a, b, t) { return a + (b - a) * t; } // ========================================================================= // 2. RESPONSIVE LAYOUT (Phone portrait, phone landscape, tablet, desktop) // ========================================================================= const layout = { vw: window.innerWidth, vh: window.innerHeight, isPortrait: true, lampCx: 0, lampBaseY: 0, lampH: 0, deskHorizonY: 0, swX: 0, swY: 0, swW: 112, swH: 52, cordP0x: 0, cordP0y: 0, cordP1x: 0, cordP1y: 0, cordP2x: 0, cordP2y: 0 }; function computeLayout(vw, vh) { layout.vw = vw; layout.vh = vh; const aspect = vw / Math.max(1, vh); layout.isPortrait = aspect < 0.86; const compact = vw <= 390 || vh <= 620; layout.swW = compact ? 104 : 112; layout.swH = compact ? 48 : 52; if (layout.isPortrait) { const bottomSafe = 24; layout.lampH = Math.min(vh * 0.665, vw * 1.48); layout.lampCx = vw * 0.50; layout.lampBaseY = clamp(vh * 0.742, layout.lampH + 26, vh - 118 - bottomSafe * 0.4); layout.deskHorizonY = layout.lampBaseY - layout.lampH * 0.088; const deskSpan = vh - layout.lampBaseY; layout.swX = vw * 0.50; layout.swY = clamp(layout.lampBaseY + deskSpan * 0.51, layout.lampBaseY + 56, vh - 46 - bottomSafe * 0.35); const baseFootHalfW = 0.146 * layout.lampH; layout.cordP0x = layout.lampCx - baseFootHalfW * 0.76; layout.cordP0y = layout.lampBaseY - layout.lampH * 0.016; layout.cordP2x = layout.swX - layout.swW * 0.50 - 3; layout.cordP2y = layout.swY + 1; layout.cordP1x = Math.max(20, Math.min(layout.cordP0x, layout.cordP2x) - layout.lampH * 0.13); layout.cordP1y = lerp(layout.cordP0y, layout.cordP2y, 0.74); } else { layout.lampH = Math.min(vh * 0.78, vw * 0.58); layout.lampCx = vw * 0.455; layout.lampBaseY = clamp(vh * 0.835, layout.lampH + 22, vh - 52); layout.deskHorizonY = layout.lampBaseY - layout.lampH * 0.095; const baseFootHalfW = 0.146 * layout.lampH; const minSwX = layout.lampCx + baseFootHalfW + layout.swW * 0.5 + Math.max(54, layout.lampH * 0.16); layout.swX = Math.min(vw - layout.swW * 0.5 - 26, Math.max(minSwX, layout.lampCx + layout.lampH * 0.44)); layout.swY = clamp(layout.lampBaseY + layout.lampH * 0.036, layout.deskHorizonY + 40, vh - 46); layout.cordP0x = layout.lampCx + baseFootHalfW * 0.78; layout.cordP0y = layout.lampBaseY - layout.lampH * 0.020; layout.cordP2x = layout.swX - layout.swW * 0.50 - 3; layout.cordP2y = layout.swY + 1; layout.cordP1x = lerp(layout.cordP0x, layout.cordP2x, 0.46); layout.cordP1y = Math.max(layout.cordP0y, layout.cordP2y) + Math.min(34, layout.lampH * 0.072); } const swEl = document.getElementById('powerSwitch'); if (swEl) { swEl.style.left = layout.swX.toFixed(1) + 'px'; swEl.style.top = layout.swY.toFixed(1) + 'px'; } } // ========================================================================= // 3. THERMAL CONVECTION & MERGE/SPLIT WAX PHYSICS ENGINE // ========================================================================= // Conserved total wax area (~26% of bottle interior). // Features desynchronized full-height Rayleigh-Bénard thermal convection: // - Bottom coil heats central resting wax -> stretches, splits & lifts // - Insulated mid-column lets hot blobs travel all the way up (y~0.88) // and cool blobs travel all the way down (y~0.06) without stalling // - Top cap cools hovering blobs -> deepen to ruby & sink back down // - Touching blobs smoothly bridge, coalesce, elongate & split! const TARGET_TOTAL_MASS = 0.031; const MIN_BLOB_MASS = 0.00075; // r ~ 0.027 const SPLIT_MIN_MASS = 0.00240; // r ~ 0.049 const MAX_BLOB_MASS_ON = 0.00580; // r ~ 0.076 const MAX_BLOB_MASS_OFF = 0.00440; // r ~ 0.066 let nextBlobId = 1; let simTime = 0; const state = { isOn: true, lampGlow: 1.0, heaterTemp: 1.0, blobs: [], mergeCount: 0, splitCount: 0 }; function makeBlob(x, y, mass, temp, vx = 0, vy = 0, neutralT = null) { const r = Math.sqrt(mass); return { id: nextBlobId++, x, y, vx, vy, mass, r, temp, // Slight natural wax-density variation keeps convection organically desynchronized! neutralT: neutralT !== null ? neutralT : (0.45 + Math.random() * 0.10), splitCooldown: 1.1 + Math.random() * 1.6, mergeCooldown: 0.0, siblingId: -1, siblingTimer: 0.0, strain: 0.0, phase: Math.random() * Math.PI * 2 }; } function initBlobs() { state.blobs = []; // Naturally distributed across the full bottle height on Frame 1: // molten base pool on the coil, rising golden-orange plumes, mid-bottle // passing & touching blobs, and cool ruby-crimson droplets drifting down from the cap! const seeds = [ // Molten resting base bed hugging the bottom heating coil { x: -0.062, y: 0.056, m: 0.0036, t: 0.44, vx: 0.004, vy: 0.000, nt: 0.54 }, { x: 0.060, y: 0.054, m: 0.0034, t: 0.48, vx: -0.004, vy: 0.000, nt: 0.55 }, { x: 0.002, y: 0.095, m: 0.0038, t: 0.78, vx: 0.002, vy: 0.024, nt: 0.49 }, // Rising warm blobs { x: -0.072, y: 0.285, m: 0.0030, t: 0.85, vx: 0.008, vy: 0.058, nt: 0.47 }, { x: 0.058, y: 0.455, m: 0.0025, t: 0.80, vx: -0.006, vy: 0.052, nt: 0.48 }, // Mid-bottle interacting & counter-flowing blobs { x: -0.036, y: 0.585, m: 0.0028, t: 0.74, vx: 0.006, vy: 0.042, nt: 0.49 }, { x: 0.076, y: 0.625, m: 0.0020, t: 0.25, vx: -0.006, vy: -0.044, nt: 0.51 }, // Upper cool hovering & descending droplets near top cap { x: -0.040, y: 0.805, m: 0.0028, t: 0.22, vx: 0.005, vy: -0.036, nt: 0.50 }, { x: 0.026, y: 0.885, m: 0.0025, t: 0.34, vx: -0.004, vy: -0.012, nt: 0.48 }, // Sinking cool satellite droplets along outer walls { x: 0.108, y: 0.275, m: 0.0016, t: 0.24, vx: -0.006, vy: -0.046, nt: 0.52 }, { x: -0.098, y: 0.435, m: 0.0017, t: 0.26, vx: 0.004, vy: -0.042, nt: 0.51 }, { x: 0.012, y: 0.725, m: 0.0015, t: 0.78, vx: -0.002, vy: 0.046, nt: 0.47 } ]; let sumM = 0; for (let i = 0; i < seeds.length; i++) sumM += seeds[i].m; const scale = TARGET_TOTAL_MASS / sumM; for (let i = 0; i < seeds.length; i++) { const s = seeds[i]; state.blobs.push(makeBlob(s.x, s.y, s.m * scale, s.t, s.vx, s.vy, s.nt)); } } function thermalNoiseX(x, y, t) { return Math.sin(y * 7.3 + t * 0.55) * 0.55 + Math.cos(x * 11.1 - y * 4.7 + t * 0.39) * 0.45; } function thermalNoiseY(x, y, t) { return Math.cos(x * 8.9 + y * 5.2 - t * 0.46) * 0.55 + Math.sin(y * 9.4 + t * 0.33) * 0.45; } function stepPhysics(dt) { simTime += dt; // 1. Electrical bulb brightness & heating coil thermal inertia const targetGlow = state.isOn ? 1.0 : 0.0; const glowRate = state.isOn ? 5.2 : 3.2; state.lampGlow += (targetGlow - state.lampGlow) * Math.min(1.0, dt * glowRate); if (Math.abs(targetGlow - state.lampGlow) < 0.0005) state.lampGlow = targetGlow; const heaterRate = state.isOn ? 0.88 : 0.72; state.heaterTemp += (targetGlow - state.heaterTemp) * Math.min(1.0, dt * heaterRate); if (Math.abs(targetGlow - state.heaterTemp) < 0.0005) state.heaterTemp = targetGlow; const H = state.heaterTemp; const blobs = state.blobs; const n = blobs.length; const maxMass = lerp(MAX_BLOB_MASS_OFF, MAX_BLOB_MASS_ON, H); // 2. Full-Height Desynchronized Thermal Convection for (let i = 0; i < n; i++) { const b = blobs[i]; if (b.splitCooldown > 0) b.splitCooldown = Math.max(0, b.splitCooldown - dt); if (b.mergeCooldown > 0) b.mergeCooldown = Math.max(0, b.mergeCooldown - dt); if (b.siblingTimer > 0) { b.siblingTimer = Math.max(0, b.siblingTimer - dt); if (b.siblingTimer <= 0) b.siblingId = -1; } const sizeFactor = clamp(0.050 / Math.max(0.026, b.r), 0.72, 1.42); if (H > 0.12) { // Sharp localized heating spot directly over the central bulb coil (y < 0.15) // Outer bottom lobes heat slowly, while the central lobe heats fast and lifts off! const radialCoil = 0.24 + 0.76 * Math.exp(-(b.x * b.x) / (0.075 * 0.075)); const bottomZone = Math.exp(-Math.pow(Math.max(0, b.y - 0.03) / 0.105, 2.0)); // Localized cooling sink right under the upper metal cap (y > 0.78) const topZone = Math.exp(-Math.pow(Math.max(0, 0.93 - b.y) / 0.135, 2.0)); // Mid-column (0.18 < y < 0.76) is thermally insulated so rising hot blobs // reach the very top and sinking cool blobs reach the very bottom! b.temp += (0.98 - b.temp) * (0.68 * bottomZone * radialCoil * H * sizeFactor) * dt; b.temp += (0.12 - b.temp) * (0.40 * topZone * sizeFactor) * dt; } else { // Lamp OFF: cool ambient liquid chills every blob down to heavy resting state b.temp += (0.05 - b.temp) * (0.46 * sizeFactor) * dt; } b.temp = clamp(b.temp, 0.03, 1.0); // Buoyancy relative to each blob's neutral density threshold const dT = b.temp - b.neutralT; const buoyancy = dT * (0.38 + 0.22 * (b.r / 0.055)); let ay = buoyancy; let ax = 0; if (H > 0.08) { const wHere = glassInnerW(clamp(b.y, 0, 1)); const normX = b.x / Math.max(0.08, wHere); // Gently draw bottom-resting cool blobs inward over the heating coil one by one if (b.y < 0.16 && b.temp < b.neutralT + 0.08) { const driftBias = Math.sin(simTime * 0.25 + b.phase) * 0.015; ax += (-b.x * 0.16 + driftBias) * H * (1.0 - b.y / 0.16); } // Rising hot plumes gently favor the warm central updraft if (dT > 0.08 && b.y > 0.15 && b.y < 0.74) { ax += -b.x * 0.045 * H; } // Cooling blobs near top gently peel slightly outward along the curved glass walls if (b.y > 0.72 && dT < 0.02) { const dir = Math.abs(b.x) > 0.014 ? (b.x > 0 ? 1 : -1) : (Math.sin(b.phase) >= 0 ? 1 : -1); ax += dir * 0.032 * H * ((b.y - 0.72) / 0.28); } // Gentle organic fluid meander const swirl = 0.018 * H; ax += thermalNoiseX(b.x * 2.8, b.y * 2.1, simTime + b.phase * 0.25) * swirl; ay += thermalNoiseY(b.x * 2.8, b.y * 2.1, simTime + b.phase * 0.25) * swirl * 0.28; // Mild cool downdraft along outer glass walls ay -= 0.012 * H * normX * normX; } else { if (b.y < 0.14 && b.temp < 0.30) { ax += -b.x * 0.04; } } b.vx += ax * dt; b.vy += ay * dt; // Viscous fluid drag for languid, velvety motion const drag = H < 0.15 ? 3.1 : 2.15; const damp = Math.exp(-drag * dt); b.vx *= damp; b.vy *= damp; const maxSpeed = 0.18; const sp2 = b.vx * b.vx + b.vy * b.vy; if (sp2 > maxSpeed * maxSpeed) { const inv = maxSpeed / Math.sqrt(sp2); b.vx *= inv; b.vy *= inv; } } // 3. Pairwise Interactions: Selective Coalescence, Slip-Past Weaving, Conduction & Shear const mergeQueue = []; for (let i = 0; i < n; i++) { const a = blobs[i]; for (let j = i + 1; j < n; j++) { const b = blobs[j]; const dx = b.x - a.x; const dy = b.y - a.y; const d2 = dx * dx + dy * dy; const sumR = a.r + b.r; const touchRange = sumR * 1.28; if (d2 >= touchRange * touchRange) continue; const d = Math.sqrt(Math.max(d2, 1e-8)); const nx = dx / d; const ny = dy / d; const overlapFrac = clamp(1.0 - d / touchRange, 0.0, 1.0); const tempDiff = Math.abs(a.temp - b.temp); // Mild heat conduction while surfaces touch (stronger only on deep overlap) const cond = (0.10 + 0.28 * overlapFrac) * overlapFrac * dt; const avgT = (a.temp * a.mass + b.temp * b.mass) / (a.mass + b.mass); a.temp += (avgT - a.temp) * cond; b.temp += (avgT - b.temp) * cond; const areFreshSiblings = (a.siblingId === b.id && a.siblingTimer > 0) || (b.siblingId === a.id && b.siblingTimer > 0); const combinedMass = a.mass + b.mass; // Blobs merge eagerly when compatible in temperature/buoyancy OR when colliding closely! const thermallyCompatible = tempDiff < 0.30 || H < 0.15 || (a.y < 0.16 && b.y < 0.16) || d < sumR * 0.72; const canMerge = !areFreshSiblings && a.mergeCooldown <= 0 && b.mergeCooldown <= 0 && combinedMass <= maxMass && thermallyCompatible; if (areFreshSiblings) { // Smooth sibling separation after splitting so the glowing neck stretches & pinches cleanly const sepStr = 0.095 * clamp(1.32 - d / (sumR * 1.22), 0, 1); a.vx -= nx * sepStr * dt; a.vy -= ny * sepStr * dt; b.vx += nx * sepStr * dt; b.vy += ny * sepStr * dt; } else if (canMerge) { // Cohesive surface tension pulls touching blobs smoothly into coalescence const pull = 0.068 * (0.28 + 0.72 * overlapFrac); const wA = b.mass / combinedMass; const wB = a.mass / combinedMass; a.vx += nx * pull * wA * dt; a.vy += ny * pull * wA * dt; b.vx -= nx * pull * wB * dt; b.vy -= ny * pull * wB * dt; const relVx = b.vx - a.vx; const relVy = b.vy - a.vy; const relDamp = Math.min(0.45, 1.8 * overlapFrac * dt); a.vx += relVx * relDamp * wA; a.vy += relVy * relDamp * wA; b.vx -= relVx * relDamp * wB; b.vy -= relVy * relDamp * wB; const mergeDist = 0.46 * Math.max(a.r, b.r) + 0.24 * Math.min(a.r, b.r); if (d < mergeDist) { mergeQueue.push([i, j]); } } else { // Counter-flowing hot/cool blobs glide past one another with a brief glossy kiss + sideways slip! const minSep = sumR * 0.96; if (d < minSep) { const push = (minSep - d) * 0.65; const invM = 1.0 / (a.mass + b.mass); const wA = b.mass * invM; const wB = a.mass * invM; // Slight lateral slip helps counter-flowing blobs weave smoothly past each other const slipX = (Math.abs(nx) < 0.25 ? (a.id % 2 === 0 ? 0.35 : -0.35) : nx * 0.35); a.vx -= (nx + slipX) * push * wA * dt * 5.0; a.vy -= ny * push * wA * dt * 3.2; b.vx += (nx + slipX) * push * wB * dt * 5.0; b.vy += ny * push * wB * dt * 3.2; // Grazing contact imparts shear strain that can cleave a large blob! const relSpeed = Math.hypot(b.vx - a.vx, b.vy - a.vy); if (H > 0.3 && relSpeed > 0.032) { a.strain += relSpeed * 2.4 * dt; b.strain += relSpeed * 2.4 * dt; } } } } } if (mergeQueue.length > 0) { const consumed = new Uint8Array(blobs.length); for (let k = 0; k < mergeQueue.length; k++) { const i = mergeQueue[k][0]; const j = mergeQueue[k][1]; if (consumed[i] || consumed[j]) continue; const a = blobs[i]; const b = blobs[j]; const totalM = a.mass + b.mass; if (totalM > maxMass) continue; a.x = (a.x * a.mass + b.x * b.mass) / totalM; a.y = (a.y * a.mass + b.y * b.mass) / totalM; a.vx = (a.vx * a.mass + b.vx * b.mass) / totalM; a.vy = (a.vy * a.mass + b.vy * b.mass) / totalM; a.temp = (a.temp * a.mass + b.temp * b.mass) / totalM; a.neutralT = (a.neutralT * a.mass + b.neutralT * b.mass) / totalM; a.mass = totalM; a.r = Math.sqrt(totalM); a.splitCooldown = Math.max(a.splitCooldown, 1.15); a.strain = Math.min(a.strain + 0.24, 1.0); consumed[j] = 1; state.mergeCount++; } for (let i = blobs.length - 1; i >= 0; i--) { if (consumed[i]) blobs.splice(i, 1); } } // 4. Integrate & strictly keep wax inside curved glass walls for (let i = 0; i < blobs.length; i++) { const b = blobs[i]; b.x += b.vx * dt; b.y += b.vy * dt; const floorY = b.r * 0.78 + 0.006; const ceilY = 0.946 - b.r * 0.86; if (b.y < floorY) { b.y = floorY; if (b.vy < 0) b.vy *= -0.10; b.vx *= 0.96; } else if (b.y > ceilY) { b.y = ceilY; if (b.vy > 0) b.vy *= -0.10; b.vx *= 0.97; } const halfW = glassInnerW(clamp(b.y, 0, 1)); const maxX = Math.max(0.004, halfW - b.r * 0.88); if (b.x > maxX) { b.x = maxX; if (b.vx > 0) b.vx *= -0.16; } else if (b.x < -maxX) { b.x = -maxX; if (b.vx < 0) b.vx *= -0.16; } } // 5. Buoyancy / Rayleigh-Taylor / Shear Splitting (active when lamp is ON) if (H > 0.25 && blobs.length < MAX_BLOBS - 1) { const newBlobs = []; for (let i = 0; i < blobs.length; i++) { if (blobs.length + newBlobs.length >= MAX_BLOBS) break; const b = blobs[i]; if (b.mass < SPLIT_MIN_MASS || b.splitCooldown > 0) { b.strain = Math.max(0, b.strain - 0.26 * dt); continue; } const sizeExcess = Math.max(0, (b.mass - SPLIT_MIN_MASS) / 0.0022); let strainRate = 0.0; if (b.y < 0.20 && b.temp > 0.62) { // Hot wax over the coil stretches upward and pinches off a buoyant rising teardrop! strainRate += (0.28 + 0.48 * sizeExcess) * H; } else if (b.y > 0.74 && b.temp < 0.42) { // Cool mass under the cap sags downward and drips off a sinking ruby droplet! strainRate += (0.24 + 0.44 * sizeExcess) * H; } else if (b.mass > 0.0038) { strainRate += (0.18 + 0.46 * sizeExcess) * H; } strainRate += Math.abs(b.vy) * 1.8 * sizeExcess; b.strain += strainRate * dt; if (strainRate < 0.05) { b.strain = Math.max(0, b.strain - 0.16 * dt); } const threshold = 1.12 + 0.30 * Math.sin(b.phase * 3.1); if (b.strain >= threshold) { const minFrac = Math.max(0.32, MIN_BLOB_MASS / b.mass); const maxFrac = 1.0 - minFrac; if (maxFrac <= minFrac) continue; const frac = minFrac + Math.random() * Math.min(0.50 - minFrac, maxFrac - minFrac); const m1 = b.mass * frac; const m2 = b.mass - m1; const tilt = (Math.random() - 0.5) * 0.65; const ux = Math.sin(tilt); const uy = Math.cos(tilt); // Tiny initial center offset -> completely seamless Wyvill metaball elongation! const sep = b.r * 0.28; const x1 = b.x + ux * sep * (m2 / b.mass); const y1 = b.y + uy * sep * (m2 / b.mass); const x2 = b.x - ux * sep * (m1 / b.mass); const y2 = b.y - uy * sep * (m1 / b.mass); // Conserve total thermal energy while slightly differentiating upper & lower daughters const dT = 0.12 * H; const t1 = clamp(b.temp + dT * (m2 / b.mass), 0.05, 1.0); const t2 = clamp((b.temp * b.mass - t1 * m1) / m2, 0.05, 1.0); const kick = 0.042; const vx1 = b.vx + ux * kick * (m2 / b.mass); const vy1 = b.vy + uy * kick * (m2 / b.mass); const vx2 = b.vx - ux * kick * (m1 / b.mass); const vy2 = b.vy - uy * kick * (m1 / b.mass); const ntShift = (Math.random() - 0.5) * 0.04; const nt1 = clamp(b.neutralT - ntShift, 0.45, 0.55); const nt2 = clamp(b.neutralT + ntShift, 0.45, 0.55); b.x = x1; b.y = y1; b.vx = vx1; b.vy = vy1; b.mass = m1; b.r = Math.sqrt(m1); b.temp = t1; b.neutralT = nt1; b.strain = 0; b.splitCooldown = 3.2 + Math.random() * 1.8; b.mergeCooldown = 0.70; const d2 = makeBlob(x2, y2, m2, t2, vx2, vy2, nt2); d2.splitCooldown = 3.2 + Math.random() * 1.8; d2.mergeCooldown = 0.70; d2.siblingId = b.id; d2.siblingTimer = 3.0; b.siblingId = d2.id; b.siblingTimer = 3.0; newBlobs.push(d2); state.splitCount++; } } for (let k = 0; k < newBlobs.length; k++) { blobs.push(newBlobs[k]); } } } function nudgeBottleAt(clientX, clientY) { const lx = (clientX - layout.lampCx) / layout.lampH; const ly = (layout.lampBaseY - clientY) / layout.lampH; const by = (ly - GLASS_Y0) / GLASS_H; const bx = lx / GLASS_H; if (by < 0.02 || by > 0.96) return; if (Math.abs(bx) > glassOuterW(by) * 1.08) return; for (let i = 0; i < state.blobs.length; i++) { const b = state.blobs[i]; const dx = b.x - bx; const dy = b.y - by; const d2 = dx * dx + dy * dy; const influence = Math.exp(-d2 / 0.042); if (influence > 0.02) { const d = Math.sqrt(d2 + 0.002); b.vx += (dx / d) * 0.062 * influence; b.vy += ((dy / d) * 0.042 + 0.026 * state.heaterTemp) * influence; b.strain += 0.35 * influence * state.heaterTemp; } } } // ========================================================================= // 4. PROCEDURAL MECHANICAL SWITCH SOUND (Zero External Assets) // ========================================================================= let audioCtx = null; function playSwitchClick(turningOn) { try { const AC = window.AudioContext || window.webkitAudioContext; if (!AC) return; if (!audioCtx) audioCtx = new AC(); if (audioCtx.state === 'suspended') audioCtx.resume(); const now = audioCtx.currentTime; const len = Math.floor(audioCtx.sampleRate * 0.026); const buf = audioCtx.createBuffer(1, len, audioCtx.sampleRate); const data = buf.getChannelData(0); for (let i = 0; i < len; i++) { const t = i / len; data[i] = (Math.random() * 2 - 1) * Math.pow(1 - t, 4.5); } const src = audioCtx.createBufferSource(); src.buffer = buf; const filter = audioCtx.createBiquadFilter(); filter.type = 'bandpass'; filter.frequency.value = turningOn ? 2300 : 1750; filter.Q.value = 1.6; const gain = audioCtx.createGain(); gain.gain.setValueAtTime(0.18, now); gain.gain.exponentialRampToValueAtTime(0.001, now + 0.025); src.connect(filter); filter.connect(gain); gain.connect(audioCtx.destination); src.start(now); const osc = audioCtx.createOscillator(); const oscGain = audioCtx.createGain(); osc.type = 'triangle'; osc.frequency.setValueAtTime(turningOn ? 320 : 240, now); osc.frequency.exponentialRampToValueAtTime(85, now + 0.022); oscGain.gain.setValueAtTime(0.08, now); oscGain.gain.exponentialRampToValueAtTime(0.0008, now + 0.024); osc.connect(oscGain); oscGain.connect(audioCtx.destination); osc.start(now); osc.stop(now + 0.026); } catch (_) {} } // ========================================================================= // 5. HIGH-CRAFT WEBGL1 FULL-SCENE RENDERER (60 FPS + Adaptive Resolution) // ========================================================================= const VERT_SRC = ` attribute vec2 aPos; void main() { gl_Position = vec4(aPos, 0.0, 1.0); } `; const FRAG_SRC = ` #ifdef GL_FRAGMENT_PRECISION_HIGH precision highp float; #else precision mediump float; #endif uniform vec2 uRes; uniform float uTime; uniform float uGlow; uniform float uHeat; uniform vec3 uLamp; uniform float uDeskY; uniform vec2 uCordP0; uniform vec2 uCordP1; uniform vec2 uCordP2; uniform int uBlobCount; uniform vec4 uBlobs[${MAX_BLOBS}]; const float GLASS_Y0 = 0.255; const float GLASS_Y1 = 0.865; const float GLASS_H = 0.610; const float WALL_T = 0.012; const float META_REACH = ${META_REACH.toFixed(4)}; const float META_ISO = ${META_ISO.toFixed(5)}; float hash21(vec2 p) { p = fract(p * vec2(123.34, 456.21)); p += dot(p, p + 45.32); return fract(p.x * p.y); } float vnoise(vec2 p) { vec2 i = floor(p); vec2 f = fract(p); f = f * f * (3.0 - 2.0 * f); float a = hash21(i); float b = hash21(i + vec2(1.0, 0.0)); float c = hash21(i + vec2(0.0, 1.0)); float d = hash21(i + vec2(1.0, 1.0)); return mix(mix(a, b, f.x), mix(c, d, f.x), f.y); } float smin(float a, float b, float k) { float h = max(k - abs(a - b), 0.0) / k; return min(a, b) - h * h * k * 0.25; } float glassOuterW(float by) { float wLow = 0.156 + 0.56 * by; float wHigh = 0.296 - 0.154 * by; return smin(wLow, wHigh, 0.055); } float baseHalfW(float ly) { float t = clamp(ly / 0.258, 0.0, 1.0); float w = mix(0.148, 0.099, pow(t, 0.92)); float footBevel = smoothstep(0.022, 0.0, ly); w -= footBevel * footBevel * 0.005; float collarLip = exp(-pow((ly - 0.254) / 0.007, 2.0)); w += collarLip * 0.0025; return w; } float capHalfW(float ly) { float t = clamp((ly - 0.860) / 0.140, 0.0, 1.0); float w = mix(0.090, 0.064, t); float crown = smoothstep(0.955, 1.005, ly); w -= crown * crown * 0.008; return w; } vec2 sdQuadBezier(vec2 p, vec2 p0, vec2 p1, vec2 p2) { float minD = 1.0e9; float bestOffset = 0.0; vec2 prev = p0; for (int i = 1; i <= 12; i++) { float t = float(i) / 12.0; float it = 1.0 - t; vec2 curr = it * it * p0 + 2.0 * it * t * p1 + t * t * p2; vec2 pa = p - prev; vec2 ba = curr - prev; float h = clamp(dot(pa, ba) / max(dot(ba, ba), 1.0e-5), 0.0, 1.0); vec2 diff = pa - ba * h; float d = length(diff); if (d < minD) { minD = d; vec2 tang = normalize(ba + vec2(1.0e-5, 0.0)); vec2 norm = vec2(-tang.y, tang.x); bestOffset = dot(diff, norm); } prev = curr; } return vec2(minD, bestOffset); } vec3 deskWood(vec2 uv, float depth) { float warp = vnoise(vec2(uv.x * 1.1, uv.y * 3.8)) * 0.95 + vnoise(vec2(uv.x * 3.2, uv.y * 9.5)) * 0.25; float grain1 = 0.5 + 0.5 * sin(uv.y * 28.0 + warp * 4.2); float grain2 = 0.5 + 0.5 * sin(uv.y * 64.0 + warp * 5.8 + uv.x * 1.2); float fibers = vnoise(vec2(uv.x * 26.0, uv.y * 210.0)); float subtleSeam = smoothstep(0.012, 0.0, abs(fract(uv.y * 1.6 + 0.22) - 0.5) - 0.494); float g = grain1 * 0.55 + grain2 * 0.25 + fibers * 0.20; vec3 deepWalnut = vec3(0.024, 0.012, 0.008); vec3 midWalnut = vec3(0.056, 0.027, 0.015); vec3 warmFiber = vec3(0.088, 0.044, 0.024); vec3 col = mix(deepWalnut, midWalnut, smoothstep(0.18, 0.78, g)); col = mix(col, warmFiber, smoothstep(0.72, 0.98, g) * 0.45); col *= (1.0 - subtleSeam * 0.30); col *= mix(0.62, 1.05, clamp(depth, 0.0, 1.0)); return col; } vec3 shadeMetal(float u, float ly, float pxNoise, float glow, float refWarm, float topMask, float botMask) { float su = clamp(u, -0.999, 0.999); float nz = sqrt(max(0.02, 1.0 - su * su)); float brush = (vnoise(vec2(su * 5.0, ly * 280.0)) - 0.5) * 0.11 + (vnoise(vec2(su * 12.0, ly * 620.0)) - 0.5) * 0.05; float bandKey = exp(-pow((su + 0.40) / 0.22, 2.0)); float bandSharp = exp(-pow((su + 0.46) / 0.075, 2.0)); float bandRimR = exp(-pow((su - 0.78) / 0.12, 2.0)); float bandDark = exp(-pow((su - 0.18) / 0.28, 2.0)); float fresnel = pow(1.0 - nz, 2.8); vec3 baseTone = mix(vec3(0.048, 0.052, 0.068), vec3(0.072, 0.068, 0.080), glow * 0.5); vec3 col = baseTone * (0.45 + 0.55 * nz); col *= (1.0 - bandDark * 0.46); vec3 hiTint = mix(vec3(0.46, 0.50, 0.62), vec3(0.64, 0.62, 0.68), glow * 0.45); col += hiTint * (bandKey * 0.36 + bandSharp * 0.52 + bandRimR * 0.22); col += vec3(0.10, 0.11, 0.16) * fresnel * 0.45; vec3 warmBounce = vec3(1.0, 0.34, 0.08); col += warmBounce * glow * refWarm * (0.28 + 0.72 * nz) * 0.55; col += vec3(0.36, 0.10, 0.52) * glow * (0.06 + 0.14 * fresnel) * (1.0 - botMask * 0.6); col = mix(col, vec3(0.025, 0.014, 0.010) + warmBounce * glow * 0.12, botMask * 0.58); col += vec3(0.12, 0.13, 0.16) * topMask * (0.4 + 0.6 * bandKey); col *= (1.0 + brush); col += (pxNoise - 0.5) / 255.0; return col; } void main() { vec2 frag = gl_FragCoord.xy; float pxNoise = hash21(frag); float lampH = uLamp.z; float aa = 1.35 / lampH; vec2 lp = (frag - uLamp.xy) / lampH; float lx = lp.x; float ly = lp.y; // ------------------------------------------------------------------- // A. COZY DARK ROOM WALL & SUBTLE ATMOSPHERIC BACKLIGHT HALO // ------------------------------------------------------------------- vec2 screenUV = frag / uRes; vec3 wallOff = mix(vec3(0.007, 0.008, 0.015), vec3(0.017, 0.019, 0.032), smoothstep(0.10, 0.94, screenUV.y)); vec3 wallOn = mix(vec3(0.012, 0.007, 0.020), vec3(0.022, 0.014, 0.036), smoothstep(0.10, 0.94, screenUV.y)); vec3 col = mix(wallOff, wallOn, uGlow); vec2 vigV = (screenUV - vec2(0.5, 0.52)) * vec2(1.15, 1.0); col *= (1.0 - 0.42 * dot(vigV, vigV)); vec2 hV = vec2(lx * 1.05, (ly - 0.52) * 0.85); vec2 hA = vec2(lx * 1.30, (ly - 0.36) * 1.20); vec3 haloViolet = vec3(0.24, 0.045, 0.34) * exp(-dot(hV, hV) * 3.2); vec3 haloAmber = vec3(0.62, 0.16, 0.04) * exp(-dot(hA, hA) * 5.4); col += (haloViolet * 0.42 + haloAmber * 0.34) * uGlow; col += (vnoise(frag * 0.09) - 0.5) * 0.0025; // ------------------------------------------------------------------- // B. PERSPECTIVE WALNUT WOODEN DESK & DYNAMIC LAMP LIGHT POOL // ------------------------------------------------------------------- float deskHorizonPx = uDeskY; if (frag.y <= deskHorizonPx + 3.0) { float deskMask = smoothstep(deskHorizonPx + 1.5, deskHorizonPx - 1.5, frag.y); float deskDepth = clamp((deskHorizonPx - frag.y) / max(deskHorizonPx, 1.0), 0.0, 1.0); float persp = 1.0 / (0.40 + deskDepth * 1.25); vec2 woodUV = vec2(lx * persp * 1.35, deskDepth * 1.25 + 0.10); vec3 wood = deskWood(woodUV, deskDepth); vec3 ambLight = mix(vec3(0.62, 0.68, 0.88), vec3(0.76, 0.64, 0.72), uGlow); vec3 deskCol = wood * ambLight; vec2 poolVec = vec2(lx * 1.05, (ly + 0.02) * 3.10); float poolD2 = dot(poolVec, poolVec); float innerPool = exp(-poolD2 * 5.0); float widePool = exp(-poolD2 * 1.25); float shimmer = 0.95 + 0.05 * sin(uTime * 1.2 + lx * 3.5); vec3 lightTint = mix(vec3(0.72, 0.14, 0.34), vec3(1.0, 0.42, 0.10), clamp(innerPool * 1.4, 0.0, 1.0)); deskCol += wood * lightTint * (widePool * 1.65 + innerPool * 2.40) * uGlow * shimmer; float glossRefl = exp(-pow(lx * 3.5, 2.0)) * smoothstep(0.0, 0.45, -ly) * exp(-max(0.0, -ly) * 4.0); deskCol += vec3(0.85, 0.28, 0.10) * glossRefl * uGlow * 0.12; float backEdge = exp(-pow((deskHorizonPx - frag.y) / 2.5, 2.0)); deskCol += mix(vec3(0.03, 0.035, 0.055), vec3(0.11, 0.045, 0.075), uGlow) * backEdge * 0.75; float frontBevel = smoothstep(14.0, 0.0, frag.y); deskCol *= (1.0 - frontBevel * 0.42); float shD = length(vec2(lx / 0.158, (ly + 0.008) / 0.034)); float contactAO = smoothstep(0.74, 1.24, shD); float broadShadow = smoothstep(0.65, 2.10, length(vec2((lx - 0.025) / 0.21, (ly + 0.015) / 0.055))); deskCol *= mix(0.10, 1.0, contactAO); deskCol *= mix(0.50, 1.0, broadShadow); // ----------------------------------------------------------------- // C. SMOOTH MATTE BLACK POWER CORD ON THE DESK // ----------------------------------------------------------------- float cordR = clamp(lampH * 0.0070, 2.4, 4.4); vec2 cordSh = sdQuadBezier(frag + vec2(0.0, cordR * 0.95), uCordP0, uCordP1, uCordP2); float cordShadow = smoothstep(cordR * 2.4, cordR * 0.35, cordSh.x); deskCol *= (1.0 - cordShadow * 0.56); vec2 cordRes = sdQuadBezier(frag, uCordP0, uCordP1, uCordP2); float cordMask = smoothstep(cordR + 1.0, cordR - 0.85, cordRes.x); if (cordMask > 0.001) { float cyl = clamp(cordRes.y / max(cordR, 1.0), -1.0, 1.0); float hi = pow(max(0.0, 1.0 - abs(cyl - 0.28) * 1.85), 2.5); vec3 cordCol = vec3(0.012, 0.011, 0.016) + vec3(0.055, 0.055, 0.068) * hi + vec3(0.24, 0.085, 0.03) * uGlow * hi * exp(-poolD2 * 1.4); deskCol = mix(deskCol, cordCol, cordMask); } col = mix(col, deskCol, deskMask); } // ------------------------------------------------------------------- // D. GLASS BOTTLE, JEWEL LIQUID, COIL & C2-SMOOTH WYVILL WAX BLOBS // ------------------------------------------------------------------- float by = (ly - GLASS_Y0) / GLASS_H; float bx = lx / GLASS_H; if (by >= -0.04 && by <= 1.04) { float byClamped = clamp(by, 0.0, 1.0); float outerW = glassOuterW(byClamped); float innerW = outerW - WALL_T; float glassAA = aa / GLASS_H; float uGlass = clamp(bx / max(outerW, 0.001), -1.0, 1.0); float cylZ = sqrt(max(0.0, 1.0 - uGlass * uGlass)); float botSeamBy = -0.015 * cylZ; float topSeamBy = 1.0 + 0.014 * cylZ; float sideMask = smoothstep(outerW + glassAA, outerW - glassAA, abs(bx)); float vertMask = smoothstep(botSeamBy - glassAA, botSeamBy + glassAA, by) * smoothstep(topSeamBy + glassAA, topSeamBy - glassAA, by); float glassMask = sideMask * vertMask; if (glassMask > 0.001) { vec3 bgCol = col; float nz = sqrt(max(0.02, 1.0 - uGlass * uGlass)); // 1. Deep jewel-toned liquid interior float liquidTop = 0.962 + 0.012 * cylZ; float inLiquid = smoothstep(liquidTop + 0.007, liquidTop - 0.006, by); vec3 liqOffTop = vec3(0.014, 0.018, 0.034); vec3 liqOffBot = vec3(0.028, 0.020, 0.038); vec3 liqOff = mix(liqOffBot, liqOffTop, byClamped); vec3 liqOnBot = vec3(0.38, 0.045, 0.072); vec3 liqOnMid = vec3(0.092, 0.014, 0.175); vec3 liqOnTop = vec3(0.022, 0.008, 0.092); vec3 liqOn = mix(liqOnBot, liqOnMid, smoothstep(0.0, 0.44, byClamped)); liqOn = mix(liqOn, liqOnTop, smoothstep(0.38, 1.0, byClamped)); float bulbDist = length(vec2(bx * 1.85, byClamped * 1.25)); float bulbBeam = exp(-bulbDist * 3.4) * (0.60 + 0.40 * nz); liqOn += vec3(0.95, 0.28, 0.05) * bulbBeam * 0.58; liqOn *= (0.72 + 0.34 * nz); vec3 liqCol = mix(liqOff, liqOn, uGlow); // Subtle suspended micro-bubbles float bubbleGlow = 0.0; for (int k = 0; k < 5; k++) { float fk = float(k); float bBy = fract(fk * 0.21 + uTime * (0.009 + 0.0025 * fk) * (0.20 + 0.80 * uGlow)); float bBx = sin(fk * 2.7 + uTime * 0.28 + bBy * 5.0) * (glassOuterW(bBy) * 0.58); float bd = length(vec2(bx - bBx, by - bBy)); bubbleGlow += smoothstep(0.0048, 0.0010, bd) * 0.16; } liqCol += mix(vec3(0.24, 0.28, 0.42), vec3(0.95, 0.58, 0.40), uGlow) * bubbleGlow * inLiquid; // Subtle recessed metallic heating coil band tucked right at the base collar if (by < 0.040) { float coilBand = exp(-pow((by - 0.012) / 0.008, 2.0)) * smoothstep(0.132, 0.095, abs(bx)); float coilRibs = 0.65 + 0.35 * sin(bx * 170.0); vec3 coilCol = mix(vec3(0.05, 0.05, 0.06), vec3(1.15, 0.46, 0.08), uGlow * 0.85 + uHeat * 0.15); liqCol = mix(liqCol, coilCol * coilRibs, coilBand * 0.55); } // 2. C2-SMOOTH WYVILL METABALL WAX FIELD (Zero creases + velvety 3D shading!) float field = 0.0; vec2 grad = vec2(0.0); float tempSum = 0.0; vec2 pGlass = vec2(bx, by); for (int i = 0; i < ${MAX_BLOBS}; i++) { if (i >= uBlobCount) break; vec4 b = uBlobs[i]; vec2 d = pGlass - b.xy; // Subtle bottom gravitational cushion when resting on the floor float floorFlat = smoothstep(0.12, 0.03, b.y) * (0.14 + 0.28 * (1.0 - uHeat)); float sx = 1.0 - floorFlat * 0.36; float sy = 1.0 + floorFlat * 0.58; vec2 ds = vec2(d.x * sx, d.y * sy); float R = b.z * META_REACH; float invR2 = 1.0 / (R * R); float q = 1.0 - dot(ds, ds) * invR2; if (q > 0.0) { float q2 = q * q; float contrib = q2 * q; field += contrib; tempSum += contrib * b.w; float k = -6.0 * q2 * invR2; grad += k * vec2(ds.x * sx, ds.y * sy); } } float innerMask = smoothstep(innerW + glassAA * 0.7, innerW - glassAA * 1.4, abs(bx)) * smoothstep(liquidTop + 0.004, liquidTop - 0.012, by); float effField = field * innerMask; float waxTemp = tempSum / max(field, 0.0001); // Soft luminous glow aura cast into surrounding violet liquid around molten blobs float haloField = smoothstep(0.06, META_ISO, effField); vec3 waxHaloCol = mix(vec3(0.42, 0.03, 0.12), vec3(0.92, 0.24, 0.03), waxTemp); liqCol += waxHaloCol * haloField * uGlow * (0.12 + 0.22 * waxTemp); float gLen = length(grad); float isoAA = max(glassAA * gLen * 1.05, 0.014); float waxMask = smoothstep(META_ISO - isoAA, META_ISO + isoAA, effField) * innerMask; if (waxMask > 0.001) { // Singularity-free smooth 3D dome normal: smoothly (0,0,1) at every blob apex & saddle, // curving organically toward the silhouette rim (zero cone tips or creases!) float excess = max(0.0, effField - META_ISO); float thickness = excess / (excess + 0.42); float edgeFactor = pow(1.0 - thickness, 1.15); vec2 tilt = (-grad / (gLen + 10.5)) * (0.58 + 0.62 * edgeFactor); float t2 = min(dot(tilt, tilt), 0.92); float wNz = sqrt(1.0 - t2); vec3 wN = vec3(tilt, wNz); vec3 bulbPos = vec3(0.0, -0.06, 0.18); vec3 toBulb = bulbPos - vec3(bx, by, 0.0); float distBulb = length(toBulb); vec3 Lbulb = toBulb / max(distBulb, 0.001); vec3 Lkey = normalize(vec3(-0.62, 0.48, 0.62)); vec3 V = vec3(0.0, 0.0, 1.0); float bulbAtten = exp(-distBulb * 1.45); float diffBulb = max(dot(wN, Lbulb), 0.0); float wrapBulb = max((dot(wN, Lbulb) + 0.50) / 1.50, 0.0); float diffKey = max(dot(wN, Lkey), 0.0); float specWax = pow(max(dot(wN, normalize(Lkey + V)), 0.0), 24.0); float specBulb = pow(max(dot(wN, normalize(Lbulb + V)), 0.0), 14.0); // Rich, velvety molten wax palette: // Cool upper wax = deep luscious ruby-crimson; Hot rising wax = glowing molten orange-amber! vec3 coreCool = vec3(0.28, 0.010, 0.044); vec3 coreHot = vec3(0.82, 0.130, 0.014); vec3 sssCool = vec3(0.84, 0.068, 0.115); vec3 sssHot = vec3(1.28, 0.440, 0.025); float tMix = smoothstep(0.20, 0.86, waxTemp); vec3 waxCore = mix(coreCool, coreHot, tMix); vec3 waxSSS = mix(sssCool, sssHot, tMix); // Smooth translucent subsurface scattering (brightest on thin necks, rims & hot bellies) float sss = (0.18 + 0.82 * edgeFactor) * (0.44 + 0.56 * bulbAtten) * (0.45 + 0.65 * tMix); vec3 waxOn = waxCore * (0.34 + 0.64 * wrapBulb * bulbAtten + 0.26 * diffKey) + waxSSS * sss * 0.72 + vec3(1.18, 0.50, 0.06) * diffBulb * bulbAtten * 0.46 + vec3(1.00, 0.46, 0.08) * specBulb * bulbAtten * 0.18 + vec3(1.00, 0.74, 0.60) * specWax * 0.24; // Gentle waxy depth darkening right in the thickest core waxOn *= mix(0.86, 1.10, edgeFactor * 0.52 + tMix * 0.48); // Lamp OFF: solid/matte deep burgundy-plum resting inside dim glass vec3 waxOff = vec3(0.085, 0.014, 0.032) * (0.32 + 0.68 * diffKey) + vec3(0.14, 0.06, 0.09) * pow(1.0 - wNz, 2.2) * 0.28 + vec3(0.32, 0.26, 0.34) * specWax * 0.18; vec3 waxCol = mix(waxOff, waxOn, uGlow); liqCol = mix(liqCol, waxCol, waxMask); } // 3. Air gap & curved top liquid meniscus beneath metal cap vec3 airGapCol = mix(vec3(0.015, 0.017, 0.028), vec3(0.032, 0.012, 0.056), uGlow); vec3 cavityCol = mix(airGapCol, liqCol, inLiquid); float meniscusLine = exp(-pow((by - liquidTop) / 0.005, 2.0)) * smoothstep(outerW, innerW - 0.01, abs(bx)); cavityCol += mix(vec3(0.10, 0.12, 0.18), vec3(0.55, 0.22, 0.48), uGlow) * meniscusLine * 0.62; // 4. Blown Glass Optical Layer float wallZone = smoothstep(innerW - 0.003, outerW, abs(bx)); float fresnel = pow(1.0 - nz, 3.4); vec3 glassRimOn = mix(vec3(0.48, 0.14, 0.44), vec3(0.95, 0.38, 0.12), smoothstep(0.62, 0.04, byClamped)); vec3 glassRimOff = vec3(0.15, 0.18, 0.26); vec3 glassRim = mix(glassRimOff, glassRimOn, uGlow); vec3 glassCol = mix(cavityCol, mix(bgCol, cavityCol, 0.58) + glassRim * 0.34, wallZone * 0.72); glassCol += glassRim * fresnel * (0.24 + 0.24 * uGlow); float reflL = exp(-pow((uGlass + 0.56) / 0.13, 2.0)) * smoothstep(0.04, 0.16, byClamped) * smoothstep(0.97, 0.84, byClamped); float reflLSharp = exp(-pow((uGlass + 0.76) / 0.034, 2.0)) * smoothstep(0.06, 0.22, byClamped) * smoothstep(0.95, 0.78, byClamped); float reflR = exp(-pow((uGlass - 0.81) / 0.042, 2.0)) * smoothstep(0.08, 0.24, byClamped) * smoothstep(0.94, 0.75, byClamped); vec3 reflTint = mix(vec3(0.45, 0.52, 0.68), vec3(1.0, 0.86, 0.78), uGlow * 0.42); glassCol += reflTint * (reflL * 0.14 + reflLSharp * 0.24 + reflR * 0.14); float outerEdge = smoothstep(outerW - glassAA * 2.5, outerW, abs(bx)); glassCol *= (1.0 - outerEdge * 0.26); float capAO = smoothstep(0.94, 1.01, by); float collarAO = smoothstep(0.032, -0.01, by); glassCol *= (1.0 - capAO * 0.34); glassCol = mix(glassCol, glassCol * 0.70 + vec3(0.45, 0.14, 0.03) * uGlow * 0.25, collarAO * 0.65); col = mix(bgCol, glassCol, glassMask); } } // ------------------------------------------------------------------- // E. BRUSHED ALUMINUM CONICAL BASE // ------------------------------------------------------------------- if (ly >= -0.03 && ly <= 0.270) { float w = baseHalfW(clamp(ly, 0.0, 0.260)); float u = clamp(lx / max(w, 0.001), -1.0, 1.0); float cz = sqrt(max(0.0, 1.0 - u * u)); float footY = -0.018 * cz; float collarY = 0.257 - 0.008 * cz; float mSide = smoothstep(w + aa, w - aa, abs(lx)); float mVert = smoothstep(footY - aa, footY + aa, ly) * smoothstep(collarY + aa, collarY - aa, ly); float baseMask = mSide * mVert; if (baseMask > 0.001) { float warmCollar = smoothstep(0.14, 0.255, ly); float botMask = smoothstep(0.055, 0.0, ly - footY); float topLip = exp(-pow((ly - collarY + 0.003) / 0.004, 2.0)); vec3 baseCol = shadeMetal(u, ly, pxNoise, uGlow, warmCollar * 0.95, topLip * 0.45, botMask); baseCol += vec3(1.0, 0.42, 0.10) * uGlow * topLip * (0.24 + 0.42 * cz); col = mix(col, baseCol, baseMask); } } // ------------------------------------------------------------------- // F. BRUSHED ALUMINUM CONICAL CAP // ------------------------------------------------------------------- if (ly >= 0.850 && ly <= 1.025) { float w = capHalfW(clamp(ly, 0.860, 1.005)); float u = clamp(lx / max(w, 0.001), -1.0, 1.0); float cz = sqrt(max(0.0, 1.0 - u * u)); float capBotY = 0.863 + 0.008 * cz; float capTopY = 0.996 + 0.009 * cz; float mSide = smoothstep(w + aa, w - aa, abs(lx)); float mVert = smoothstep(capBotY - aa, capBotY + aa, ly) * smoothstep(capTopY + aa, capTopY - aa, ly); float capMask = mSide * mVert; if (capMask > 0.001) { float warmUnder = smoothstep(0.92, 0.862, ly); float topMask = smoothstep(capTopY - 0.018, capTopY, ly); vec3 capCol = shadeMetal(u, ly, pxNoise, uGlow, warmUnder * 0.40, topMask, 0.0); col = mix(col, capCol, capMask); } } // ------------------------------------------------------------------- // G. FILMIC TONE MAPPING & GAMMA // ------------------------------------------------------------------- col = (col * (2.51 * col + 0.03)) / (col * (2.43 * col + 0.59) + 0.14); col = pow(clamp(col, 0.0, 1.0), vec3(1.0 / 2.2)); col += (pxNoise - 0.5) / 255.0; gl_FragColor = vec4(col, 1.0); } `; function createWebGLRenderer(canvas) { const opts = { alpha: false, antialias: false, depth: false, stencil: false, powerPreference: 'high-performance' }; const gl = canvas.getContext('webgl', opts) || canvas.getContext('experimental-webgl', opts); if (!gl) return null; function compile(type, src) { const sh = gl.createShader(type); gl.shaderSource(sh, src); gl.compileShader(sh); if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) { console.error('Shader error:', gl.getShaderInfoLog(sh)); gl.deleteShader(sh); return null; } return sh; } const vs = compile(gl.VERTEX_SHADER, VERT_SRC); const fs = compile(gl.FRAGMENT_SHADER, FRAG_SRC); if (!vs || !fs) return null; const prog = gl.createProgram(); gl.attachShader(prog, vs); gl.attachShader(prog, fs); gl.linkProgram(prog); if (!gl.getProgramParameter(prog, gl.LINK_STATUS)) { console.error('Program link error:', gl.getProgramInfoLog(prog)); return null; } gl.useProgram(prog); const buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW); const aPos = gl.getAttribLocation(prog, 'aPos'); gl.enableVertexAttribArray(aPos); gl.vertexAttribPointer(aPos, 2, gl.FLOAT, false, 0, 0); const loc = { res: gl.getUniformLocation(prog, 'uRes'), time: gl.getUniformLocation(prog, 'uTime'), glow: gl.getUniformLocation(prog, 'uGlow'), heat: gl.getUniformLocation(prog, 'uHeat'), lamp: gl.getUniformLocation(prog, 'uLamp'), deskY: gl.getUniformLocation(prog, 'uDeskY'), cordP0: gl.getUniformLocation(prog, 'uCordP0'), cordP1: gl.getUniformLocation(prog, 'uCordP1'), cordP2: gl.getUniformLocation(prog, 'uCordP2'), blobCount: gl.getUniformLocation(prog, 'uBlobCount'), blobs: gl.getUniformLocation(prog, 'uBlobs[0]') }; const blobUniformData = new Float32Array(MAX_BLOBS * 4); return { kind: 'webgl', render(scale) { const w = canvas.width; const h = canvas.height; gl.viewport(0, 0, w, h); const s = scale; gl.uniform2f(loc.res, w, h); gl.uniform1f(loc.time, simTime); gl.uniform1f(loc.glow, state.lampGlow); gl.uniform1f(loc.heat, state.heaterTemp); gl.uniform3f(loc.lamp, layout.lampCx * s, h - layout.lampBaseY * s, layout.lampH * s); gl.uniform1f(loc.deskY, h - layout.deskHorizonY * s); gl.uniform2f(loc.cordP0, layout.cordP0x * s, h - layout.cordP0y * s); gl.uniform2f(loc.cordP1, layout.cordP1x * s, h - layout.cordP1y * s); gl.uniform2f(loc.cordP2, layout.cordP2x * s, h - layout.cordP2y * s); const count = Math.min(state.blobs.length, MAX_BLOBS); for (let i = 0; i < count; i++) { const b = state.blobs[i]; const o = i * 4; blobUniformData[o] = b.x; blobUniformData[o + 1] = b.y; blobUniformData[o + 2] = b.r; blobUniformData[o + 3] = b.temp; } gl.uniform1i(loc.blobCount, count); gl.uniform4fv(loc.blobs, blobUniformData); gl.drawArrays(gl.TRIANGLES, 0, 3); } }; } // ========================================================================= // 6. CANVAS 2D FALLBACK RENDERER (if WebGL is unavailable) // ========================================================================= function createCanvas2DRenderer(canvas) { const ctx = canvas.getContext('2d', { alpha: false }); if (!ctx) return null; const metaW = 76; const metaH = 136; const metaCanvas = document.createElement('canvas'); metaCanvas.width = metaW; metaCanvas.height = metaH; const metaCtx = metaCanvas.getContext('2d'); const metaImg = metaCtx.createImageData(metaW, metaH); const metaData = metaImg.data; function glassPath(c) { const steps = 28; c.beginPath(); for (let i = 0; i <= steps; i++) { const by = i / steps; const w = glassOuterW(by) * GLASS_H * layout.lampH; const y = layout.lampBaseY - (GLASS_Y0 + by * GLASS_H) * layout.lampH; if (i === 0) c.moveTo(layout.lampCx - w, y); else c.lineTo(layout.lampCx - w, y); } for (let i = steps; i >= 0; i--) { const by = i / steps; const w = glassOuterW(by) * GLASS_H * layout.lampH; const y = layout.lampBaseY - (GLASS_Y0 + by * GLASS_H) * layout.lampH; c.lineTo(layout.lampCx + w, y); } c.closePath(); } return { kind: '2d', render(scale) { const vw = layout.vw; const vh = layout.vh; const g = state.lampGlow; const cx = layout.lampCx; const baseY = layout.lampBaseY; const lh = layout.lampH; ctx.setTransform(scale, 0, 0, scale, 0, 0); const wallGrad = ctx.createLinearGradient(0, 0, 0, vh); wallGrad.addColorStop(0, g > 0.1 ? '#181126' : '#0f101a'); wallGrad.addColorStop(1, g > 0.1 ? '#0e0817' : '#080910'); ctx.fillStyle = wallGrad; ctx.fillRect(0, 0, vw, vh); if (g > 0.01) { const halo = ctx.createRadialGradient(cx, baseY - lh * 0.46, lh * 0.04, cx, baseY - lh * 0.46, lh * 0.65); halo.addColorStop(0, `rgba(235, 92, 32, ${(0.24 * g).toFixed(3)})`); halo.addColorStop(0.45, `rgba(110, 28, 145, ${(0.18 * g).toFixed(3)})`); halo.addColorStop(1, 'rgba(14, 8, 22, 0)'); ctx.fillStyle = halo; ctx.fillRect(0, 0, vw, vh); } const deskGrad = ctx.createLinearGradient(0, layout.deskHorizonY, 0, vh); deskGrad.addColorStop(0, g > 0.1 ? '#26140f' : '#161115'); deskGrad.addColorStop(1, g > 0.1 ? '#361c12' : '#20161a'); ctx.fillStyle = deskGrad; ctx.fillRect(0, layout.deskHorizonY, vw, vh - layout.deskHorizonY); if (g > 0.01) { const pool = ctx.createRadialGradient(cx, baseY + lh * 0.02, lh * 0.03, cx, baseY + lh * 0.02, lh * 0.44); pool.addColorStop(0, `rgba(255, 118, 36, ${(0.34 * g).toFixed(3)})`); pool.addColorStop(0.55, `rgba(185, 42, 82, ${(0.14 * g).toFixed(3)})`); pool.addColorStop(1, 'rgba(0,0,0,0)'); ctx.fillStyle = pool; ctx.fillRect(0, layout.deskHorizonY, vw, vh - layout.deskHorizonY); } ctx.save(); ctx.fillStyle = 'rgba(0,0,0,0.70)'; ctx.beginPath(); ctx.ellipse(cx, baseY + lh * 0.008, lh * 0.162, lh * 0.030, 0, 0, Math.PI * 2); ctx.fill(); ctx.restore(); ctx.save(); ctx.lineCap = 'round'; ctx.strokeStyle = 'rgba(0,0,0,0.52)'; ctx.lineWidth = Math.max(5, lh * 0.014); ctx.beginPath(); ctx.moveTo(layout.cordP0x, layout.cordP0y + 4); ctx.quadraticCurveTo(layout.cordP1x, layout.cordP1y + 4, layout.cordP2x, layout.cordP2y + 4); ctx.stroke(); ctx.strokeStyle = '#15131c'; ctx.lineWidth = Math.max(4.5, lh * 0.012); ctx.beginPath(); ctx.moveTo(layout.cordP0x, layout.cordP0y); ctx.quadraticCurveTo(layout.cordP1x, layout.cordP1y, layout.cordP2x, layout.cordP2y); ctx.stroke(); ctx.restore(); ctx.save(); glassPath(ctx); ctx.clip(); const gTopY = baseY - GLASS_Y1 * lh; const gBotY = baseY - GLASS_Y0 * lh; const liqGrad = ctx.createLinearGradient(cx, gBotY, cx, gTopY); if (g > 0.05) { liqGrad.addColorStop(0, `rgb(${Math.round(lerp(20, 175, g))}, ${Math.round(lerp(16, 36, g))}, ${Math.round(lerp(30, 48, g))})`); liqGrad.addColorStop(0.45, `rgb(${Math.round(lerp(14, 52, g))}, ${Math.round(lerp(14, 14, g))}, ${Math.round(lerp(28, 92, g))})`); liqGrad.addColorStop(1, `rgb(${Math.round(lerp(12, 20, g))}, ${Math.round(lerp(14, 10, g))}, ${Math.round(lerp(24, 56, g))})`); } else { liqGrad.addColorStop(0, '#14111e'); liqGrad.addColorStop(1, '#0c0d18'); } ctx.fillStyle = liqGrad; ctx.fillRect(cx - lh * 0.22, gTopY, lh * 0.44, gBotY - gTopY); const blobs = state.blobs; const bCount = blobs.length; for (let py = 0; py < metaH; py++) { const by = 1.0 - (py + 0.5) / metaH; const innerW = glassInnerW(by); for (let px = 0; px < metaW; px++) { const bx = ((px + 0.5) / metaW - 0.5) * 0.56; const idx = (py * metaW + px) * 4; if (Math.abs(bx) > innerW || by > 0.96) { metaData[idx + 3] = 0; continue; } let f = 0; let tSum = 0; for (let i = 0; i < bCount; i++) { const b = blobs[i]; const dx = bx - b.x; const dy = by - b.y; const R = b.r * META_REACH; const q = 1.0 - (dx * dx + dy * dy) / (R * R); if (q > 0) { const c = q * q * q; f += c; tSum += c * b.temp; } } if (f > META_ISO - 0.08) { const alpha = clamp((f - (META_ISO - 0.08)) / 0.14, 0, 1); const wt = tSum / Math.max(f, 0.001); const edge = clamp(1.0 - (f - META_ISO) * 1.4, 0, 1); const rOn = lerp(165, 255, clamp(wt * 0.8 + edge * 0.45, 0, 1)); const gOn = lerp(18, 155, clamp(wt * 0.72 + edge * 0.30, 0, 1)); const bOn = lerp(34, 32, clamp(wt * 0.55, 0, 1)); metaData[idx] = Math.round(lerp(52, rOn, g)); metaData[idx + 1] = Math.round(lerp(16, gOn, g)); metaData[idx + 2] = Math.round(lerp(28, bOn, g)); metaData[idx + 3] = Math.round(alpha * 255); } else { metaData[idx + 3] = 0; } } } metaCtx.putImageData(metaImg, 0, 0); const drawW = 0.56 * GLASS_H * lh; const drawH = GLASS_H * lh; ctx.imageSmoothingEnabled = true; ctx.drawImage(metaCanvas, cx - drawW * 0.5, gTopY, drawW, drawH); const specGrad = ctx.createLinearGradient(cx - lh * 0.14, 0, cx - lh * 0.05, 0); specGrad.addColorStop(0, 'rgba(255,255,255,0)'); specGrad.addColorStop(0.5, 'rgba(255,244,236,0.20)'); specGrad.addColorStop(1, 'rgba(255,255,255,0)'); ctx.fillStyle = specGrad; ctx.fillRect(cx - lh * 0.15, gTopY + 10, lh * 0.11, drawH - 20); ctx.restore(); glassPath(ctx); ctx.strokeStyle = g > 0.1 ? 'rgba(255, 175, 148, 0.34)' : 'rgba(130, 146, 180, 0.22)'; ctx.lineWidth = 1.6; ctx.stroke(); function drawCone(y0, y1, w0, w1) { const py0 = baseY - y0 * lh; const py1 = baseY - y1 * lh; const mg = ctx.createLinearGradient(cx - w0 * lh, 0, cx + w0 * lh, 0); mg.addColorStop(0, '#1e2028'); mg.addColorStop(0.28, '#8a8e9c'); mg.addColorStop(0.54, '#3d3f4a'); mg.addColorStop(0.84, '#545764'); mg.addColorStop(1, '#181a22'); ctx.fillStyle = mg; ctx.beginPath(); ctx.moveTo(cx - w0 * lh, py0); ctx.lineTo(cx - w1 * lh, py1); ctx.lineTo(cx + w1 * lh, py1); ctx.lineTo(cx + w0 * lh, py0); ctx.closePath(); ctx.fill(); } drawCone(0.0, 0.258, 0.148, 0.099); drawCone(0.862, 1.00, 0.090, 0.064); } }; } // ========================================================================= // 7. BOOTSTRAP, INTERACTION & ADAPTIVE 60 FPS LOOP // ========================================================================= const canvas = document.getElementById('lampCanvas'); const powerSwitch = document.getElementById('powerSwitch'); const captionEl = powerSwitch.querySelector('.sw-caption'); initBlobs(); const renderer = createWebGLRenderer(canvas) || createCanvas2DRenderer(canvas); let nativeDpr = Math.min(window.devicePixelRatio || 1, 2); let renderScale = nativeDpr; let avgFrameMs = 16.0; let perfSampleCount = 0; function resizeCanvas() { const vw = window.innerWidth; const vh = window.innerHeight; nativeDpr = Math.min(window.devicePixelRatio || 1, 2); renderScale = Math.min(renderScale, nativeDpr); computeLayout(vw, vh); const targetW = Math.max(1, Math.round(vw * renderScale)); const targetH = Math.max(1, Math.round(vh * renderScale)); if (canvas.width !== targetW || canvas.height !== targetH) { canvas.width = targetW; canvas.height = targetH; } } function updateSwitchUI() { powerSwitch.setAttribute('aria-checked', state.isOn ? 'true' : 'false'); powerSwitch.setAttribute('aria-label', state.isOn ? 'Turn lava lamp off' : 'Turn lava lamp on'); captionEl.textContent = state.isOn ? 'ON' : 'OFF'; } function toggleLamp() { state.isOn = !state.isOn; updateSwitchUI(); playSwitchClick(state.isOn); } powerSwitch.addEventListener('click', toggleLamp); canvas.addEventListener('pointerdown', (e) => { nudgeBottleAt(e.clientX, e.clientY); }, { passive: true }); window.addEventListener('resize', resizeCanvas, { passive: true }); window.addEventListener('orientationchange', () => setTimeout(resizeCanvas, 80), { passive: true }); resizeCanvas(); updateSwitchUI(); window.__lavaLamp = { getState: () => ({ isOn: state.isOn, lampGlow: state.lampGlow, heaterTemp: state.heaterTemp, blobCount: state.blobs.length, totalMass: state.blobs.reduce((s, b) => s + b.mass, 0), avgY: state.blobs.reduce((s, b) => s + b.y * b.mass, 0) / Math.max(1e-6, state.blobs.reduce((s, b) => s + b.mass, 0)), avgTemp: state.blobs.reduce((s, b) => s + b.temp * b.mass, 0) / Math.max(1e-6, state.blobs.reduce((s, b) => s + b.mass, 0)), mergeCount: state.mergeCount, splitCount: state.splitCount, renderer: renderer ? renderer.kind : 'none', renderScale, avgFrameMs }), toggle: toggleLamp }; let lastTs = performance.now(); let lastCssGlow = -1; function frame(now) { const rawMs = now - lastTs; lastTs = now; const dt = Math.min(Math.max(rawMs * 0.001, 0.001), 0.05); stepPhysics(dt * 0.5); stepPhysics(dt * 0.5); const qGlow = Math.round(state.lampGlow * 100) / 100; if (qGlow !== lastCssGlow) { lastCssGlow = qGlow; powerSwitch.style.setProperty('--glow', qGlow.toFixed(2)); } if (renderer) { renderer.render(renderScale); } if (rawMs > 2 && rawMs < 80) { avgFrameMs = avgFrameMs * 0.92 + rawMs * 0.08; perfSampleCount++; if (perfSampleCount >= 35) { perfSampleCount = 0; if (avgFrameMs > 18.2 && renderScale > 0.72) { renderScale = Math.max(0.70, +(renderScale - 0.20).toFixed(2)); resizeCanvas(); } else if (avgFrameMs < 11.5 && renderScale < nativeDpr) { renderScale = Math.min(nativeDpr, +(renderScale + 0.15).toFixed(2)); resizeCanvas(); } } } requestAnimationFrame(frame); } requestAnimationFrame(frame);})();</script></body></html>