// Living Dough v4: inside one real 500 g dough, ingredient by ingredient, from mixing to cooling. // The numbers live in model.js. This file draws the magnified inside view (true scale, about 0.4 mm across), // the whole dough in 3D in the corner, the step-by-step panel and the walk-through. import * as THREE from '../vendor/three/three.module.min.js'; import { SRC, STEPS, stateAt, condition, ingredients, practical, extra, ING, fmtClock } from './model.js'; const $ = id => document.getElementById(id); const clamp = (x, a = 0, b = 1) => Math.min(b, Math.max(a, x)); const lerp = (a, b, t) => a + (b - a) * t; const smooth = t => { t = clamp(t); return t * t * (3 - 2 * t); }; const reduced = matchMedia('(prefers-reduced-motion: reduce)').matches; const SHORT = { P92: "Puhr & D’Appolonia 1992", TO15: "Turbin-Orger et al. 2015", TO12: "Turbin-Orger et al. 2012", K16: "Koksel et al. 2016", K15: "Koksel et al. 2015", J13: "Jayaram et al. 2013", J14: "Jayaram et al. 2014", PY: "Pyler (Baking Science)", ASPH: "American Society of Baking, pH", ASTP: "American Society of Baking, thermal profiling", PS1: "Purlis & Salvadori 2009, part 1", PS2: "Purlis & Salvadori 2009, part 2", J22: "Jha et al. 2022", FG: "Farinograph notes (NDSU)", TI: "Techniques de l’Ingénieur, baker’s yeast", PO94: "Potus, Poiffait & Drapron 1994", BJ: "Bakers Journal, yeast and sugar", KK17: "Kumar & Khatkar 2017", HBW: "Figoni, How Baking Works", CG3: "Cereals & Grains Assoc., Wheat Flour ch. 3", R10: "Rouillé et al. 2010", MD22: "Foods 2022 (flour sugars)" }; const TAGN = { M: "measured", L: "lab study", C: "calculated", E: "estimate", N: "not measured" }; const COL = Object.fromEntries(ING.map(i => [i.k, i.col])); /* ---------------- renderer, scene, lights ---------------- */ const stage = $('stage'), inset = $('inset'), cv = $('scene'), hud = $('hud'), hx = hud.getContext('2d'); let renderer; try { renderer = new THREE.WebGLRenderer({ canvas: cv, antialias: true, alpha: true }); } catch (e) { $('insetCap').textContent = 'This browser could not start 3D graphics.'; } const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(34, 1, 0.5, 400); if (renderer) { renderer.setPixelRatio(Math.min(devicePixelRatio || 1, 2)); renderer.toneMapping = THREE.ACESFilmicToneMapping; renderer.toneMappingExposure = 1.05; renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFShadowMap; } scene.add(new THREE.HemisphereLight(0xfff1dc, 0x241a28, 1.15)); const sun = new THREE.DirectionalLight(0xfff0dd, 2.4); sun.position.set(-14, 30, 18); sun.castShadow = true; sun.shadow.mapSize.set(1024, 1024); Object.assign(sun.shadow.camera, { left: -25, right: 25, top: 25, bottom: -25, near: 1, far: 90 }); sun.shadow.bias = -0.0006; sun.shadow.normalBias = 0.05; scene.add(sun); const fill = new THREE.DirectionalLight(0xbcd3ff, 0.55); fill.position.set(-20, 8, -14); scene.add(fill); const ovenLight = new THREE.PointLight(0xff7a2e, 0, 60, 1.4); ovenLight.position.set(5, 18, 2); scene.add(ovenLight); const bench = new THREE.Mesh(new THREE.CircleGeometry(60, 48), new THREE.MeshStandardMaterial({ color: 0x3a2c2a, roughness: 0.9 })); bench.rotation.x = -Math.PI / 2; bench.receiveShadow = true; scene.add(bench); /* ---------------- props: bowl, pan, oven, rack ---------------- */ const bowl = new THREE.Mesh(new THREE.LatheGeometry([ [0, 0], [6.6, 0], [8.7, 1.6], [10.4, 5], [11.4, 8.6], [11.7, 8.9], [11.0, 8.8], [10.0, 5.3], [8.2, 2.1], [6.2, 0.45], [0, 0.45] ].map(p => new THREE.Vector2(p[0], p[1])), 64, 0, Math.PI), new THREE.MeshStandardMaterial({ color: 0xd8dde3, roughness: 0.32, metalness: 0.05, side: THREE.DoubleSide })); bowl.castShadow = true; bowl.receiveShadow = true; scene.add(bowl); const pan = new THREE.Group(); { const mat = new THREE.MeshStandardMaterial({ color: 0x9aa0a8, roughness: 0.38, metalness: 0.45, side: THREE.DoubleSide }); const part = (sx, sy, sz, x, y, z) => { const m = new THREE.Mesh(new THREE.BoxGeometry(sx, sy, sz), mat); m.position.set(x, y, z); m.castShadow = m.receiveShadow = true; pan.add(m); }; part(10.5, 0.2, 10.8, 5.25, 0.1, 0); part(10.5, 7, 0.2, 5.25, 3.5, 5.3); part(10.5, 7, 0.2, 5.25, 3.5, -5.3); part(0.2, 7, 10.8, 10.4, 3.5, 0); // rolled rim part(10.5, 0.3, 0.5, 5.25, 7, 5.45); part(10.5, 0.3, 0.5, 5.25, 7, -5.45); } scene.add(pan); const oven = new THREE.Group(); const heatMat = new THREE.MeshStandardMaterial({ color: 0x3a1a10, emissive: 0xff5a1e, emissiveIntensity: 0, roughness: 0.6 }); for (const y of [19, 21]) { const rod = new THREE.Mesh(new THREE.CylinderGeometry(0.35, 0.35, 26, 12), heatMat); rod.rotation.x = Math.PI / 2; rod.position.set(y === 19 ? 3 : 9, 18, 0); oven.add(rod); } const ovenWall = new THREE.Mesh(new THREE.PlaneGeometry(60, 40), new THREE.MeshStandardMaterial({ color: 0x24140f, roughness: 0.95, emissive: 0x3a1206, emissiveIntensity: 0 })); ovenWall.position.set(22, 12, 0); ovenWall.rotation.y = -Math.PI / 2; oven.add(ovenWall); scene.add(oven); const rack = new THREE.Group(); { const mat = new THREE.MeshStandardMaterial({ color: 0x6f747c, roughness: 0.4, metalness: 0.6 }); for (let z = -7; z <= 7; z += 1.4) { const c = new THREE.Mesh(new THREE.CylinderGeometry(0.08, 0.08, 15, 6), mat); c.rotation.z = Math.PI / 2; c.position.set(5, 1.05, z); rack.add(c); } for (const x of [-1.5, 11.5]) { const c = new THREE.Mesh(new THREE.CylinderGeometry(0.12, 0.12, 15, 6), mat); c.rotation.x = Math.PI / 2; c.position.set(x, 0.95, 0); rack.add(c); } rack.children.forEach(c => { c.castShadow = true; }); } scene.add(rack); /* ---------------- the dough: a lofted half-solid plus its cut face ---------------- */ const NX = 34, NP = 72; const geo = new THREE.BufferGeometry(); const pos = new Float32Array((NX + 1) * NP * 3), col = new Float32Array((NX + 1) * NP * 3); { const idx = []; for (let i = 0; i < NX; i++) for (let j = 0; j < NP; j++) { const a = i * NP + j, b = i * NP + (j + 1) % NP, c = (i + 1) * NP + j, d = (i + 1) * NP + (j + 1) % NP; idx.push(a, b, c, b, d, c); } geo.setIndex(idx); geo.setAttribute('position', new THREE.BufferAttribute(pos, 3)); geo.setAttribute('color', new THREE.BufferAttribute(col, 3)); } const doughMat = new THREE.MeshPhysicalMaterial({ vertexColors: true, roughness: 0.62, sheen: 0.5, sheenRoughness: 0.8, sheenColor: new THREE.Color(0xfff4e0), side: THREE.DoubleSide }); const dough = new THREE.Mesh(geo, doughMat); dough.castShadow = true; const capGeo = new THREE.BufferGeometry(); const capPos = new Float32Array((NP + 1) * 3), capUv = new Float32Array((NP + 1) * 2), capNrm = new Float32Array((NP + 1) * 3); for (let j = 0; j <= NP; j++) capNrm[j * 3] = -1; { const idx = []; for (let j = 0; j < NP; j++) idx.push(NP, (j + 1) % NP, j); capGeo.setIndex(idx); } capGeo.setAttribute('position', new THREE.BufferAttribute(capPos, 3)); capGeo.setAttribute('uv', new THREE.BufferAttribute(capUv, 2)); capGeo.setAttribute('normal', new THREE.BufferAttribute(capNrm, 3)); const capCanvas = document.createElement('canvas'); capCanvas.width = 1024; capCanvas.height = 1024; let capTex = new THREE.CanvasTexture(capCanvas); capTex.colorSpace = THREE.SRGBColorSpace; capTex.anisotropy = 4; const capMat = new THREE.MeshStandardMaterial({ map: capTex, emissive: 0xffffff, emissiveMap: capTex, emissiveIntensity: 0.5, roughness: 0.92, side: THREE.DoubleSide }); const cap = new THREE.Mesh(capGeo, capMat); cap.receiveShadow = true; const doughG = new THREE.Group(); doughG.add(dough, cap); scene.add(doughG); // cross-section and taper const ring = new Float32Array(NP * 2); function profile(p) { // fills ring[] with (z, yRel) for the full cross-section for (let j = 0; j < NP; j++) { const th = j / NP * Math.PI * 2, c = Math.cos(th), s = Math.sin(th), n = s >= 0 ? p.nt : p.nb; ring[j * 2] = p.W * Math.sign(c) * Math.pow(Math.abs(c), 2 / n); ring[j * 2 + 1] = (s >= 0 ? p.Ht : p.Hb) * Math.sign(s) * Math.pow(Math.abs(s), 2 / n); } } const tau = (u, ne) => Math.pow(Math.max(0, 1 - Math.pow(u, ne)), 1 / ne); function volumeOf(p) { // full (both halves) volume, cm³ profile(p); let A = 0; for (let j = 0; j < NP; j++) { const k = (j + 1) % NP; A += ring[j * 2] * ring[k * 2 + 1] - ring[k * 2] * ring[j * 2 + 1]; } A = Math.abs(A) / 2; let I = 0; const n = 60; for (let i = 0; i < n; i++) { const u = (i + 0.5) / n; I += tau(u, p.ne) ** 2 / n; } return 2 * p.L * A * I; } const SHAPES = { round: { L: 1, W: 1, Hb: 0.3, Ht: 0.62, nb: 3.4, nt: 2.1, ne: 2.1 }, pan: { L: 9.8, W: 5.0, Hb: 0.6, Ht: 0.4, nb: 12, nt: 2.6, ne: 8 } }; function fitAll(base, V) { const v = volumeOf(base), k = Math.cbrt(V / v); return { ...base, L: base.L * k, W: base.W * k, Hb: base.Hb * k, Ht: base.Ht * k }; } function fitY(base, V) { const v = volumeOf(base), k = V / v; return { ...base, Hb: base.Hb * k, Ht: base.Ht * k }; } function mix(a, b, t) { const o = {}; for (const k in a) o[k] = lerp(a[k], b[k], t); return o; } function shapeFor(s) { if (s.id === 'mix' || s.id === 'rise') return { p: fitAll(SHAPES.round, s.V), y: 0.5, where: 'bowl' }; const LOG = { L: 9.2, W: 4.4, Hb: 4.0, Ht: 3.9, nb: 2.6, nt: 2.2, ne: 3.2 }; if (s.id === 'shape') { const t = smooth(s.f / 0.7); const p = fitAll(mix(fitAll(SHAPES.round, s.V), fitAll(LOG, s.V), t), s.V); return { p, y: lerp(0.5, 0.2, t), where: t < 0.25 ? 'bowl' : 'pan' }; } if (s.id === 'proof' && s.f < 0.3) { const t = smooth(s.f / 0.3); return { p: fitAll(mix(fitAll(LOG, s.V), fitY(SHAPES.pan, s.V), t), s.V), y: 0.2, where: 'pan' }; } const p = fitY(SHAPES.pan, s.V); return { p, y: s.id === 'cool' ? 1.2 : 0.2, where: s.id === 'cool' ? 'rack' : 'pan' }; } const noise = (x, y, z) => Math.sin(x * 1.7 + 1.3) * Math.sin(y * 2.1 + 0.7) * Math.sin(z * 1.9 + 2.1) + 0.55 * Math.sin(x * 4.3 + y * 3.1 + 0.2) * Math.sin(z * 3.9 - y * 2.7) + 0.3 * Math.sin(x * 8.9 - z * 7.7 + y * 6.1) + 0.2 * Math.sin(x * 15.1 + z * 13.3 - y * 11.9); const cDough = new THREE.Color('#e7d2ab'), cFlour = new THREE.Color('#f5efe3'), cCrustA = new THREE.Color('#bf7a35'), cCrustB = new THREE.Color('#7a3a14'), cPale = new THREE.Color('#e9cf9b'); const tmpC = new THREE.Color(); let dims = { W: 5, H: 5, L: 5, Hb: 1, Ht: 1 }; function buildDough(s) { const sh = shapeFor(s), p = sh.p; profile(p); const rough = s.rough, H = p.Hb + p.Ht, roughAmp = rough * Math.max(p.W, H) * 0.16; for (let i = 0; i <= NX; i++) { const u = Math.sin(Math.PI / 2 * i / NX), x = p.L * u, t = tau(u, p.ne); for (let j = 0; j < NP; j++) { const z0 = ring[j * 2] * t, yr = ring[j * 2 + 1] * t; let y = p.Hb + yr, z = z0, xx = x; if (roughAmp > 0.002) { const n = noise(x * 0.9, y * 0.9, z * 0.9) * roughAmp + Math.abs(noise(x * 2.6 + 3, y * 2.6, z * 2.6)) * roughAmp * 0.6; const L = Math.hypot(z0, yr, u * p.L * 0.3) || 1; z += z0 / L * n; y += yr / L * n; if (i > 0) xx += (u * p.L * 0.3) / L * n; } y = Math.max(0, y); const o = (i * NP + j) * 3; pos[o] = xx; pos[o + 1] = y; pos[o + 2] = z; // colour const top = clamp(yr / (p.Ht || 1)), inPan = sh.where === 'pan' || s.id === 'cool'; tmpC.copy(cDough); if (s.id === 'mix') tmpC.lerp(cFlour, (1 - s.gl) * 0.85 * clamp(0.5 + noise(x * 3, y * 3, z * 3))); if (s.crust > 0) { const k = s.crust * (inPan ? lerp(0.55, 1, top) : 1); tmpC.lerp(cPale, clamp(k * 1.6)).lerp(cCrustA, clamp(k * 1.4 - 0.25)).lerp(cCrustB, clamp(k * top * 1.5 - 0.35) * s.crust); } col[o] = tmpC.r; col[o + 1] = tmpC.g; col[o + 2] = tmpC.b; } } geo.attributes.position.needsUpdate = true; geo.attributes.color.needsUpdate = true; geo.computeVertexNormals(); geo.computeBoundingSphere(); // cut face from ring i = 0 const m = 0.4, w = 2 * p.W + 2 * m, h = H + 2 * m; let cy = 0, cz = 0; for (let j = 0; j < NP; j++) { const zz = pos[j * 3 + 2], yy = pos[j * 3 + 1]; capPos[j * 3] = 0; capPos[j * 3 + 1] = yy; capPos[j * 3 + 2] = zz; cy += yy; cz += zz; capUv[j * 2] = (zz + p.W + m) / w; capUv[j * 2 + 1] = (yy + m) / h; } cy /= NP; cz /= NP; capPos[NP * 3 + 1] = cy; capPos[NP * 3 + 2] = cz; capUv[NP * 2] = (cz + p.W + m) / w; capUv[NP * 2 + 1] = (cy + m) / h; capGeo.attributes.position.needsUpdate = true; capGeo.attributes.uv.needsUpdate = true; capGeo.computeBoundingSphere(); doughG.position.y = sh.y; dims = { W: p.W, H, L: p.L, Hb: p.Hb, Ht: p.Ht, m, w, h, where: sh.where, y: sh.y }; bowl.visible = sh.where === 'bowl'; pan.visible = sh.where === 'pan'; rack.visible = sh.where === 'rack'; } /* ---------------- crumb drawing (cut face and lens) ---------------- */ /* ---------------- cut face of the 3D dough ---------------- */ let seed = 7; const rnd = () => (seed = (seed * 16807) % 2147483647) / 2147483647; const POOL = 26000, SIG = 0.5, EF2 = Math.exp(SIG * SIG); const px = new Float32Array(POOL), py = new Float32Array(POOL), pf = new Float32Array(POOL); for (let i = 0; i < POOL; i++) { px[i] = rnd(); py[i] = rnd(); const z = Math.sqrt(-2 * Math.log(rnd() + 1e-9)) * Math.cos(2 * Math.PI * rnd()); pf[i] = Math.exp(SIG * z - SIG * SIG / 2); } const grain = document.createElement('canvas'); grain.width = grain.height = 128; { const g = grain.getContext('2d'), d = g.createImageData(128, 128); for (let i = 0; i < d.data.length; i += 4) { const v = 120 + rnd() * 135; d.data[i] = v; d.data[i + 1] = v * 0.93; d.data[i + 2] = v * 0.8; d.data[i + 3] = 26; } g.putImageData(d, 0, 0); } function palette(s) { const baked = s.id === 'cool' ? 1 : s.id === 'bake' ? smooth((s.m - 10) / 10) : 0; return { wall: `rgb(${lerp(234, 243, baked) | 0},${lerp(219, 231, baked) | 0},${lerp(186, 198, baked) | 0})`, hole: `rgba(${lerp(170, 196, baked) | 0},${lerp(140, 168, baked) | 0},${lerp(98, 118, baked) | 0},0.92)`, deep: `rgba(${lerp(122, 150, baked) | 0},${lerp(96, 118, baked) | 0},${lerp(64, 76, baked) | 0},0.95)`, rim: `rgba(${lerp(126, 150, baked) | 0},${lerp(98, 116, baked) | 0},${lerp(62, 74, baked) | 0},0.55)`, lit: 'rgba(255,250,236,0.55)' }; } function drawCells(ctx, w, h, mmPerPx, s, offset = 0) { const pal = palette(s); ctx.fillStyle = pal.wall; ctx.fillRect(0, 0, w, h); ctx.fillStyle = ctx.createPattern(grain, 'repeat'); ctx.fillRect(0, 0, w, h); if (s.id === 'mix' && s.gl < 0.5) { // dry flour pockets early in mixing ctx.fillStyle = 'rgba(250,246,238,0.8)'; for (let i = 0; i < 400 * (1 - s.gl * 2); i++) { const r = (0.5 + pf[i] * 1.6) * 0.25 / mmPerPx; ctx.beginPath(); ctx.arc(px[(i + 999) % POOL] * w, py[(i + 999) % POOL] * h, r, 0, 7); ctx.fill(); } } const Dpx = s.D / 1000 / mmPerPx, meanA = Math.PI / 4 * Dpx * Dpx * EF2; // overlapping circles cover 1 − e^(−sum) of the face; choose the count so the holes cover exactly the gas share (a flat cut shows the same share as the volume) const N = Math.min(POOL - offset, Math.round(-Math.log(1 - Math.min(s.P, 0.97)) * w * h / meanA)); if (N <= 0) return; ctx.fillStyle = pal.hole; ctx.beginPath(); for (let i = 0; i < N; i++) { const k = i + offset, r = pf[k] * Dpx / 2; if (r < 0.7) continue; const x = px[k] * w, y = py[k] * h; ctx.moveTo(x + r, y); ctx.arc(x, y, r, 0, Math.PI * 2); } ctx.fill(); if (N < 900) { // few, large cells (the lens): shade each cavity, dark under its top edge, lit at the bottom for (let i = 0; i < N; i++) { const k = i + offset, r = pf[k] * Dpx / 2; if (r < 5) continue; const x = px[k] * w, y = py[k] * h; const g = ctx.createLinearGradient(x, y - r, x, y + r); g.addColorStop(0, pal.deep); g.addColorStop(0.55, pal.hole); g.addColorStop(1, pal.wall); ctx.fillStyle = g; ctx.beginPath(); ctx.arc(x, y, r * 0.96, 0, Math.PI * 2); ctx.fill(); } } // sub-pixel bubbles: specks whose darkness carries their area ctx.fillStyle = pal.rim; for (let i = 0; i < N; i++) { const k = i + offset, r = pf[k] * Dpx / 2; if (r >= 0.7) continue; ctx.globalAlpha = clamp(r * r * 2.2, 0.08, 0.9); ctx.fillRect(px[k] * w, py[k] * h, 1, 1); } ctx.globalAlpha = 1; ctx.strokeStyle = pal.rim; ctx.lineWidth = Math.max(0.6, Dpx * 0.05); ctx.beginPath(); for (let i = 0; i < N; i++) { const k = i + offset, r = pf[k] * Dpx / 2; if (r < 1.5) continue; const x = px[k] * w, y = py[k] * h; ctx.moveTo(x + r * Math.cos(3.6), y + r * Math.sin(3.6)); ctx.arc(x, y, r, 3.6, 6.0); } ctx.stroke(); ctx.strokeStyle = pal.lit; ctx.lineWidth = Math.max(0.5, Dpx * 0.035); ctx.beginPath(); for (let i = 0; i < N; i++) { const k = i + offset, r = pf[k] * Dpx / 2; if (r < 2.5) continue; const x = px[k] * w, y = py[k] * h; ctx.moveTo(x + r * Math.cos(0.5), y + r * Math.sin(0.5)); ctx.arc(x, y, r, 0.5, 2.6); } ctx.stroke(); } let capKey = '', capAt = 0; function drawCap(s, force) { const key = [s.si, Math.round(s.P * 150), Math.round(s.D / 8), Math.round(s.crust * 30), Math.round(dims.w * 4), Math.round(dims.h * 4), Math.round(s.gl * 8)].join('|'); const now = performance.now(); if (!force && (key === capKey || now - capAt < 120)) return; capKey = key; capAt = now; const W = 1024, H = clamp(Math.round(W * dims.h / dims.w), 160, 1400); if (capCanvas.height !== H) { capCanvas.height = H; capTex.dispose(); capTex = new THREE.CanvasTexture(capCanvas); capTex.colorSpace = THREE.SRGBColorSpace; capTex.anisotropy = 4; capMat.map = capTex; capMat.emissiveMap = capTex; capMat.needsUpdate = true; } const ctx = capCanvas.getContext('2d'), mmPerPx = dims.w * 10 / W; drawCells(ctx, W, H, mmPerPx, s); if (s.crust > 0.01) { // 5 mm crust (Purlis & Salvadori), following the outline ctx.save(); ctx.beginPath(); for (let j = 0; j < NP; j++) { const X = capUv[j * 2] * W, Y = (1 - capUv[j * 2 + 1]) * H; j ? ctx.lineTo(X, Y) : ctx.moveTo(X, Y); } ctx.closePath(); ctx.clip(); const cpx = 5 * s.crust / mmPerPx; ctx.lineJoin = 'round'; ctx.strokeStyle = 'rgba(214,160,92,0.45)'; ctx.lineWidth = cpx * 3.4; ctx.stroke(); const g = s.crust; ctx.strokeStyle = `rgb(${lerp(214, 132, g) | 0},${lerp(160, 70, g) | 0},${lerp(92, 28, g) | 0})`; ctx.lineWidth = cpx * 2; ctx.stroke(); ctx.restore(); } capTex.needsUpdate = true; } /* ---------------- camera ---------------- */ let yaw = 0.68, pitch = 0.38, zoom = 1; const target = new THREE.Vector3(), camGoal = new THREE.Vector3(), tgtGoal = new THREE.Vector3(); let camInit = false; function placeCamera(dt) { const D = dims, size = Math.max(D.W * 2 + 4, D.H * 1.6 + 3, D.L + 6, D.where === 'bowl' ? 22 : 16); const r = size / (2 * Math.tan(camera.fov * Math.PI / 360)) * 1.0 * zoom; tgtGoal.set(D.L * 0.42, D.y + D.H * 0.48, 0); camGoal.set(-Math.cos(pitch) * Math.cos(yaw), Math.sin(pitch), Math.cos(pitch) * Math.sin(yaw)).multiplyScalar(r).add(tgtGoal); const k = camInit ? 1 - Math.exp(-dt * 4) : 1; camInit = true; target.lerp(tgtGoal, k); camera.position.lerp(camGoal, k); camera.lookAt(target); } const ptrs = new Map(); let pinch0 = 0, zoom0 = 1; cv.addEventListener('pointerdown', e => { cv.setPointerCapture(e.pointerId); ptrs.set(e.pointerId, { x: e.clientX, y: e.clientY }); if (ptrs.size === 2) { const [a, b] = [...ptrs.values()]; pinch0 = Math.hypot(a.x - b.x, a.y - b.y); zoom0 = zoom; } }); cv.addEventListener('pointermove', e => { const p = ptrs.get(e.pointerId); if (!p) return; if (ptrs.size === 1) { yaw = clamp(yaw + (e.clientX - p.x) * 0.006, -0.35, 1.35); pitch = clamp(pitch + (e.clientY - p.y) * 0.005, 0.04, 1.25); } p.x = e.clientX; p.y = e.clientY; if (ptrs.size === 2) { const [a, b] = [...ptrs.values()]; const d = Math.hypot(a.x - b.x, a.y - b.y); if (pinch0) zoom = clamp(zoom0 * pinch0 / d, 0.45, 2.2); } }); const up = e => { ptrs.delete(e.pointerId); pinch0 = 0; }; cv.addEventListener('pointerup', up); cv.addEventListener('pointercancel', up); cv.addEventListener('wheel', e => { e.preventDefault(); zoom = clamp(zoom * Math.exp(e.deltaY * 0.0012), 0.45, 2.2); }, { passive: false }); $('reset').addEventListener('click', () => { yaw = 0.68; pitch = 0.38; zoom = 1; }); /* ---------------- HUD: ruler, temperature tag, lens leader ---------------- */ /* ---------------- the close-up: one patch of dough, few items, drawn large, labelled in place ---------------- */ // Not to scale on purpose: each item is drawn big enough to read, and its real size is on its label. // What IS to scale: the share of the patch taken by gas equals the dough's real gas share. const mc = $('micro'), mx = mc.getContext('2d'); let sel = null, mW = 0, mH = 0, hits = []; const hash = (i, j, k = 0) => { let h = (i * 374761393 + j * 668265263 + k * 1274126177) | 0; h = (h ^ (h >>> 13)) * 1274126177 | 0; return ((h ^ (h >>> 16)) >>> 0) / 4294967296; }; let rs = 11; const R = () => (rs = (rs * 16807) % 2147483647) / 2147483647; /* Sprites: each ingredient is painted once, shaded, at 128 px, then stamped scaled and rotated. */ function sprite(size, paint) { const c = document.createElement('canvas'); c.width = c.height = size; paint(c.getContext('2d'), size); return c; } const SP = {}; SP.starch = sprite(128, (g, n) => { // a lens-shaped granule lit from the top left: rim, growth rings, equatorial furrow, highlight const c = n / 2, r = n / 2 - 3; let gr = g.createRadialGradient(c - r * 0.35, c - r * 0.4, r * 0.05, c, c, r); gr.addColorStop(0, '#fffdf7'); gr.addColorStop(0.55, '#f6eedb'); gr.addColorStop(0.9, '#e2d2ad'); gr.addColorStop(1, '#c9b386'); g.fillStyle = gr; g.beginPath(); g.arc(c, c, r, 0, 7); g.fill(); g.strokeStyle = 'rgba(205,186,146,.55)'; g.lineWidth = 1.4; for (const q of [0.72, 0.5, 0.3]) { g.beginPath(); g.arc(c + r * 0.04, c + r * 0.05, r * q, 0, 7); g.stroke(); } g.strokeStyle = 'rgba(170,146,104,.55)'; g.lineWidth = 2; g.beginPath(); g.moveTo(c - r * 0.8, c + r * 0.08); g.quadraticCurveTo(c, c - r * 0.06, c + r * 0.8, c + r * 0.08); g.stroke(); g.strokeStyle = 'rgba(140,114,74,.85)'; g.lineWidth = 3; g.beginPath(); g.arc(c, c, r - 1.5, 0, 7); g.stroke(); gr = g.createRadialGradient(c - r * 0.42, c - r * 0.45, 0, c - r * 0.42, c - r * 0.45, r * 0.38); gr.addColorStop(0, 'rgba(255,255,255,.95)'); gr.addColorStop(1, 'rgba(255,255,255,0)'); g.fillStyle = gr; g.beginPath(); g.arc(c - r * 0.42, c - r * 0.45, r * 0.38, 0, 7); g.fill(); }); SP.gel = sprite(128, (g, n) => { // swollen, gelatinised: soft-edged, translucent, no rim const c = n / 2, gr = g.createRadialGradient(c - 10, c - 12, 4, c, c, n / 2 - 2); gr.addColorStop(0, 'rgba(255,250,236,.85)'); gr.addColorStop(0.6, 'rgba(248,236,206,.6)'); gr.addColorStop(1, 'rgba(236,220,186,0)'); g.fillStyle = gr; g.beginPath(); g.arc(c, c, n / 2 - 2, 0, 7); g.fill(); g.strokeStyle = 'rgba(196,170,122,.5)'; g.lineWidth = 3; g.setLineDash([7, 6]); g.beginPath(); g.arc(c, c, n / 2 - 14, 0, 7); g.stroke(); }); SP.shadow = sprite(128, (g, n) => { const c = n / 2, gr = g.createRadialGradient(c, c, n * 0.18, c, c, n / 2); gr.addColorStop(0, 'rgba(40,52,60,.45)'); gr.addColorStop(1, 'rgba(40,52,60,0)'); g.fillStyle = gr; g.fillRect(0, 0, n, n); }); SP.blur = sprite(64, (g, n) => { const c = n / 2, gr = g.createRadialGradient(c - 6, c - 6, 2, c, c, n / 2); gr.addColorStop(0, 'rgba(250,244,228,.75)'); gr.addColorStop(0.65, 'rgba(236,226,200,.45)'); gr.addColorStop(1, 'rgba(236,226,200,0)'); g.fillStyle = gr; g.fillRect(0, 0, n, n); }); const yeastSprite = (dead) => sprite(128, (g, n) => { // ovoid cell: cell wall, cytoplasm, vacuole, granules, highlight const c = n / 2, r = n / 2 - 4; let gr = g.createRadialGradient(c - r * 0.3, c - r * 0.35, r * 0.1, c, c, r); if (dead) { gr.addColorStop(0, '#d6d1c8'); gr.addColorStop(0.7, '#a39d93'); gr.addColorStop(1, '#6e6a63'); } else { gr.addColorStop(0, '#ffe0a3'); gr.addColorStop(0.6, '#e9a948'); gr.addColorStop(1, '#a96a1c'); } g.fillStyle = gr; g.beginPath(); g.arc(c, c, r, 0, 7); g.fill(); g.strokeStyle = dead ? 'rgba(80,76,70,.9)' : 'rgba(110,64,14,.9)'; g.lineWidth = 4; g.stroke(); g.fillStyle = dead ? 'rgba(235,232,226,.35)' : 'rgba(255,240,205,.55)'; g.beginPath(); g.ellipse(c + r * 0.15, c + r * 0.1, r * 0.42, r * 0.36, 0.4, 0, 7); g.fill(); g.fillStyle = dead ? 'rgba(70,66,60,.45)' : 'rgba(120,70,16,.55)'; for (const [dx, dy, q] of [[-0.45, 0.2, 0.08], [-0.2, 0.5, 0.06], [0.4, -0.45, 0.07], [-0.5, -0.15, 0.05], [0.55, 0.3, 0.05]]) { g.beginPath(); g.arc(c + dx * r, c + dy * r, q * r, 0, 7); g.fill(); } gr = g.createRadialGradient(c - r * 0.45, c - r * 0.5, 0, c - r * 0.45, c - r * 0.5, r * 0.35); gr.addColorStop(0, 'rgba(255,255,255,.85)'); gr.addColorStop(1, 'rgba(255,255,255,0)'); g.fillStyle = gr; g.beginPath(); g.arc(c - r * 0.45, c - r * 0.5, r * 0.35, 0, 7); g.fill(); }); SP.yeast = yeastSprite(false); SP.dead = yeastSprite(true); SP.fat = sprite(64, (g, n) => { const c = n / 2, gr = g.createRadialGradient(c - 8, c - 9, 2, c, c, n / 2 - 2); gr.addColorStop(0, '#fffbe0'); gr.addColorStop(0.45, '#f3e07e'); gr.addColorStop(1, 'rgba(214,180,60,.0)'); g.fillStyle = gr; g.beginPath(); g.arc(c, c, n / 2 - 2, 0, 7); g.fill(); }); SP.sugar = sprite(32, (g, n) => { const c = n / 2, gr = g.createRadialGradient(c, c, 0, c, c, c); gr.addColorStop(0, 'rgba(255,190,222,1)'); gr.addColorStop(0.35, 'rgba(239,111,174,.85)'); gr.addColorStop(1, 'rgba(239,111,174,0)'); g.fillStyle = gr; g.fillRect(0, 0, n, n); }); SP.co2 = sprite(32, (g, n) => { const c = n / 2, gr = g.createRadialGradient(c - 3, c - 3, 0, c, c, c - 1); gr.addColorStop(0, 'rgba(255,255,255,.95)'); gr.addColorStop(0.5, 'rgba(140,200,250,.75)'); gr.addColorStop(1, 'rgba(70,140,220,0)'); g.fillStyle = gr; g.fillRect(0, 0, n, n); }); const NOISE = sprite(256, (g, n) => { // soft caustic texture for the water const d = g.createImageData(n, n); for (let y = 0; y < n; y++) for (let x = 0; x < n; x++) { const v = Math.sin(x * 0.049 + Math.sin(y * 0.031) * 2.1) * Math.sin(y * 0.043 + Math.sin(x * 0.027) * 1.7) + 0.5 * Math.sin((x + y) * 0.11) * Math.sin((x - y) * 0.083); const o = (y * n + x) * 4, t = 128 + v * 60; d.data[o] = t; d.data[o + 1] = t; d.data[o + 2] = t; d.data[o + 3] = 255; } g.putImageData(d, 0, 0); }); function stamp(ctx, img, x, y, rx, ry, rot) { const c = Math.cos(rot), sn = Math.sin(rot), d = mc.width / (mW || 1); ctx.setTransform(c * rx * d, sn * rx * d, -sn * ry * d, c * ry * d, x * d, y * d); ctx.drawImage(img, -1, -1, 2, 2); } // the cast, in patch coordinates (0..1); jittered grid so nothing piles up const grid = (n, seed) => Array.from({ length: n }, (_, i) => { const c = Math.ceil(Math.sqrt(n * 1.6)), gx = i % c, gy = Math.floor(i / c), r = Math.ceil(n / c); return { u: (gx + 0.5 + (hash(i, seed) - 0.5) * 0.7) / c, v: (gy + 0.5 + (hash(i, seed, 1) - 0.5) * 0.7) / r }; }); const CAST = { starch: grid(16, 1).map((p, i) => ({ ...p, big: i % 4 !== 3, rot: hash(i, 7) * Math.PI, e: 0.55 + 0.3 * hash(i, 8) })), yeast: grid(7, 2).map((p, i) => ({ ...p, rot: hash(i, 9) * Math.PI, bud: i % 3 === 0, ph: hash(i, 10) * 6.28 })), fat: grid(4, 3).map((p, i) => ({ ...p, rot: hash(i, 11) * Math.PI })), sugar: grid(28, 4).map((p, i) => ({ ...p, ph: hash(i, 12) * 6.28 })) }; const BUBS = [{ u: 0.2, v: 0.36, w: 1.25 }, { u: 0.66, v: 0.27, w: 1 }, { u: 0.43, v: 0.8, w: 0.9 }, { u: 0.9, v: 0.76, w: 0.7 }]; function layout(s, W, H) { // scale the cells until the share of the patch they cover equals the dough's real gas share (checked on a 48 × 32 grid) const P = Math.min(s.P, 0.88), sw = BUBS.reduce((a, b) => a + b.w, 0); const at = k => BUBS.map(b => ({ x: b.u * W, y: b.v * H, r: k * Math.sqrt(b.w / sw) })); const cover = bubs => { let n = 0; for (let i = 0; i < 48; i++) for (let j = 0; j < 32; j++) { const x = (i + 0.5) / 48 * W, y = (j + 0.5) / 32 * H; if (bubs.some(b => (x - b.x) ** 2 + (y - b.y) ** 2 < b.r * b.r)) n++; } return n / 1536; }; if (P < 0.003) return at(0); let lo = 0, hi = Math.hypot(W, H); for (let it = 0; it < 16; it++) { const m = (lo + hi) / 2; if (cover(at(m)) < P) lo = m; else hi = m; } return at((lo + hi) / 2); } // push a point out of every gas cell: growing bubbles squeeze the ingredients into the walls function squeeze(x, y, bubs, pad) { for (let it = 0; it < 3; it++) for (const b of bubs) { const dx = x - b.x, dy = y - b.y, d = Math.hypot(dx, dy) || 1; if (d < b.r + pad) { x = b.x + dx / d * (b.r + pad); y = b.y + dy / d * (b.r + pad); } } return [x, y]; } const HEAD = s => ({ mix: s.f < 0.3 ? 'Flour meets water: starch soaks some up, gluten proteins wake up.' : 'Mixing builds a gluten net and traps air as tiny bubbles.', rise: s.m > 95 && s.m < 100 || s.m > 145 && s.m < 150 ? 'Knock-back: gas is pressed out, big cells split into small ones.' : 'Yeast eat sugar and breathe out CO₂, which inflates the bubbles.', shape: 'Shaping presses gas out and splits the cells: this sets how fine the crumb is.', proof: 'Bubbles grow until the walls between them are thin films.', bake: s.core < 56 ? 'Heat: gas expands and steam forms, so the loaf springs up.' : s.core < 70 ? 'Yeast are dead. Starch swells into gel: dough is turning into crumb.' : 'The crumb is set. The core holds near 98 °C while water leaves as steam.', cool: 'Steam keeps leaving. The bubble pattern you see is the crumb you slice.' }[s.id]); function drawMicro(s, e, tnow) { const dpr = Math.min(devicePixelRatio || 1, 2), W = mc.clientWidth, H = mc.clientHeight; if (!W || !H) return; if (mc.width !== Math.round(W * dpr) || mc.height !== Math.round(H * dpr)) { mc.width = Math.round(W * dpr); mc.height = Math.round(H * dpr); } mW = W; mH = H; const ctx = mx, T = tnow / 1000, reset = () => ctx.setTransform(dpr, 0, 0, dpr, 0, 0); reset(); const U = Math.min(W, H) / 10; // drawing unit: a big starch granule is ~1 unit across const al = key => (sel && sel !== key) ? 0.14 : 1, gel = e.gel, wet = s.id === 'mix' ? clamp(s.m / 3) : 1; const bubs = layout(s, W, H); hits = []; ctx.globalAlpha = 1; ctx.globalCompositeOperation = 'source-over'; // the liquid const bg = ctx.createLinearGradient(0, 0, W, H); if (gel > 0) { bg.addColorStop(0, `rgb(${lerp(214, 247, gel) | 0},${lerp(232, 235, gel) | 0},${lerp(238, 208, gel) | 0})`); bg.addColorStop(1, `rgb(${lerp(190, 236, gel) | 0},${lerp(214, 218, gel) | 0},${lerp(226, 184, gel) | 0})`); } else { bg.addColorStop(0, '#e3eff2'); bg.addColorStop(1, '#c3dbe5'); } ctx.fillStyle = bg; ctx.fillRect(0, 0, W, H); if (wet < 1) { ctx.fillStyle = `rgba(240,230,206,${(1 - wet) * 0.9})`; ctx.fillRect(0, 0, W, H); } ctx.globalAlpha = 0.14; ctx.globalCompositeOperation = 'overlay'; ctx.save(); ctx.translate(Math.sin(T * 0.25) * 8, 0); ctx.fillStyle = ctx.createPattern(NOISE, 'repeat'); ctx.fillRect(-20, -20, W + 40, H + 40); ctx.restore(); ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 1; if (sel === 'water') { ctx.fillStyle = 'rgba(80,160,230,.28)'; ctx.fillRect(0, 0, W, H); } // positions after the squeeze const pos = (p, pad) => squeeze(p.u * W, p.v * H, bubs, pad); const sp = CAST.starch.map(g => { const r = (g.big ? 0.55 : 0.22) * U * (1 + 0.35 * gel); const [x, y] = pos(g, r * 0.9); return { ...g, x, y, r }; }); // gluten: links between neighbouring granules, joined as mixing develops it; films around the gas cells ctx.globalAlpha = al('gluten'); const links = []; sp.forEach((a, i) => { const nb = sp.map((b, j) => [j, Math.hypot(a.x - b.x, a.y - b.y)]).filter(([j]) => j !== i).sort((p, q) => p[1] - q[1]).slice(0, 3); nb.forEach(([j], n) => { if (i < j || n > 1) links.push([a, sp[j], hash(i, j)]); }); }); const nL = Math.round(links.length * (0.15 + 0.85 * e.dev)); ctx.lineCap = 'round'; for (let pass = 0; pass < 2; pass++) { ctx.strokeStyle = pass ? (gel > 0.5 ? 'rgba(112,84,186,.95)' : 'rgba(128,96,232,.95)') : 'rgba(140,110,240,.22)'; ctx.lineWidth = pass ? Math.max(2, U * 0.06) : Math.max(6, U * 0.2); ctx.beginPath(); for (let i = 0; i < nL; i++) { const [a, b, h] = links[i], mxp = (a.x + b.x) / 2 + (h - 0.5) * U * 0.6, myp = (a.y + b.y) / 2 + (hash(i, 3) - 0.5) * U * 0.6; ctx.moveTo(a.x, a.y); ctx.quadraticCurveTo(mxp, myp, b.x, b.y); if (pass && i === 1) hits.push({ k: 'gluten', x: mxp, y: myp, r: U * 0.3 }); } ctx.stroke(); } if (e.dev < 0.35) { // loose protein pieces before they join ctx.strokeStyle = 'rgba(128,96,232,.9)'; ctx.lineWidth = Math.max(2, U * 0.07); for (let i = 0; i < 14; i++) { const x = hash(i, 21) * W, y = hash(i, 22) * H, a = hash(i, 23) * 6.28; ctx.beginPath(); ctx.moveTo(x, y); ctx.quadraticCurveTo(x + Math.cos(a) * U * 0.5, y + Math.sin(a + 1) * U * 0.5, x + Math.cos(a) * U, y + Math.sin(a) * U); ctx.stroke(); if (i === 0) hits.push({ k: 'gluten', x, y, r: U * 0.4 }); } } // starch granules for (const g of sp) { ctx.globalAlpha = al('starch') * (1 - gel) * 0.8; stamp(ctx, SP.shadow, g.x + g.r * 0.2, g.y + g.r * 0.25, g.r * 1.25, g.r * g.e * 1.35, g.rot); if (gel < 1) { ctx.globalAlpha = al('starch') * (1 - gel); stamp(ctx, SP.starch, g.x, g.y, g.r, g.r * (g.big ? g.e : 1), g.rot); } if (gel > 0) { ctx.globalAlpha = al('starch') * gel; stamp(ctx, SP.gel, g.x, g.y, g.r * 1.15, g.r * 1.1, g.rot); } hits.push({ k: 'starch', x: g.x, y: g.y, r: g.r, big: g.big }); } reset(); // fat for (const f of CAST.fat) { const r = 0.32 * U; const [x, y] = pos(f, r); ctx.globalAlpha = al('fat') * (0.95 - 0.3 * e.fatMelt); stamp(ctx, SP.fat, x, y, r * (1 + 1.3 * e.fatMelt), r * (0.8 - 0.4 * e.fatMelt), f.rot); hits.push({ k: 'fat', x, y, r }); } reset(); // dissolved sugar: as many dots as sugar is left const nS = Math.round(CAST.sugar.length * clamp(e.left / 14.8) * (1 - e.sucrose)), sd = Math.max(8, U * 0.2); ctx.globalAlpha = al('sugar'); for (let i = 0; i < nS; i++) { const p = CAST.sugar[i]; const [x0, y0] = pos(p, sd); const x = x0 + Math.sin(T * 0.9 + p.ph) * U * 0.06, y = y0 + Math.cos(T * 0.8 + p.ph) * U * 0.06; ctx.drawImage(SP.sugar, x - sd / 2, y - sd / 2, sd, sd); if (i === 0) hits.push({ k: 'sugar', x, y, r: sd }); } // yeast: each drawn cell stands for about 10 billion real ones const yp = CAST.yeast.map(c => { const r = 0.3 * U; const [x, y] = pos(c, r * 1.1); return { ...c, x, y, r }; }); for (const c of yp) { const j = e.alive ? U * 0.025 : 0, x = c.x + Math.sin(T * 2.1 + c.ph) * j, y = c.y + Math.cos(T * 1.7 + c.ph) * j, img = e.alive ? SP.yeast : SP.dead; ctx.globalAlpha = al('yeast') * 0.5; stamp(ctx, SP.shadow, x + c.r * 0.25, y + c.r * 0.3, c.r * 1.4, c.r * 1.2, c.rot); ctx.globalAlpha = al('yeast'); stamp(ctx, img, x, y, c.r, c.r * 0.8, c.rot); if (c.bud) stamp(ctx, img, x + Math.cos(c.rot) * c.r * 1.15, y + Math.sin(c.rot) * c.r * 1.15, c.r * 0.45, c.r * 0.4, c.rot); hits.push({ k: 'yeast', x, y, r: c.r }); } reset(); // CO₂ leaving each live yeast cell and flying into the nearest bubble if (e.alive && e.rate > 0 && bubs[0].r > 2) { for (const c of yp) { let nb = bubs[0], bd = 1e9; for (const b of bubs) { const d = Math.hypot(b.x - c.x, b.y - c.y) - b.r; if (d < bd) { bd = d; nb = b; } } for (let q = 0; q < 3; q++) { const t = (T * 0.35 * e.rate + c.ph + q / 3) % 1, ex = nb.x + (c.x - nb.x) / (Math.hypot(c.x - nb.x, c.y - nb.y) || 1) * nb.r * 0.9, ey = nb.y + (c.y - nb.y) / (Math.hypot(c.x - nb.x, c.y - nb.y) || 1) * nb.r * 0.9; const x = lerp(c.x, ex, t), y = lerp(c.y, ey, t) - Math.sin(t * Math.PI) * U * 0.25, sz = Math.max(6, U * (0.1 + 0.08 * t)); ctx.globalAlpha = al('gas') * Math.min(1, e.rate) * (t < 0.85 ? 1 : (1 - t) / 0.15); ctx.drawImage(SP.co2, x - sz / 2, y - sz / 2, sz, sz); } } } // crystals at the start of mixing [['sugar', e.sucrose, 0.8, 0.5], ['salt', e.saltXtal, 0.3, 0.62]].forEach(([t, left, u, v]) => { if (left <= 0) return; const sz = U * 0.9 * Math.sqrt(left), x = u * W, y = v * H; ctx.globalAlpha = al(t) * Math.min(1, left * 1.5); ctx.save(); ctx.translate(x, y); ctx.rotate(0.4); const gr = ctx.createLinearGradient(-sz / 2, -sz / 2, sz / 2, sz / 2); gr.addColorStop(0, '#fff'); gr.addColorStop(1, t === 'sugar' ? '#f0b8d4' : '#b9c9db'); ctx.fillStyle = gr; ctx.fillRect(-sz / 2, -sz / 2, sz, sz); ctx.strokeStyle = t === 'sugar' ? '#d9579a' : '#7f97b0'; ctx.lineWidth = 2; ctx.strokeRect(-sz / 2, -sz / 2, sz, sz); ctx.restore(); hits.push({ k: t, x, y, r: sz / 2, xtal: true }); }); // gas cells: neighbours meet at flat walls (each cell clipped by the half-planes it shares) const film = Math.max(2, (s.id === 'proof' || s.id === 'bake' || s.id === 'cool' ? 0.05 : 0.09) * U), steam = s.id === 'bake' ? clamp((s.core - 80) / 18) : 0; ctx.globalAlpha = al('gas'); bubs.forEach((b, i) => { if (b.r < 1) return; ctx.save(); bubs.forEach((o, j) => { if (j === i || o.r < 1) return; const dx = o.x - b.x, dy = o.y - b.y, d = Math.hypot(dx, dy) || 1; if (d > b.r + o.r) return; const m = (d * d + b.r * b.r - o.r * o.r) / (2 * d) - film / 2, nx = dx / d, ny = dy / d, px = b.x + nx * m, py = b.y + ny * m, L = 4000; ctx.beginPath(); ctx.moveTo(px - ny * L, py + nx * L); ctx.lineTo(px + ny * L, py - nx * L); ctx.lineTo(px + ny * L - nx * L, py - nx * L - ny * L); ctx.lineTo(px - ny * L - nx * L, py + nx * L - ny * L); ctx.closePath(); ctx.clip(); }); // a hollow seen in cross-section: shadow under the near rim, the far wall lit, cool CO₂ tint in dough, warm crumb tones once baked const bk = Math.max(gel, s.id === 'cool' ? 1 : 0), mixc = (c1, c2) => c1.map((v, n) => Math.round(lerp(v, c2[n], bk))); const rim = mixc([96, 128, 158], [138, 98, 56]), mid = mixc([170, 199, 220], [214, 182, 136]), far = mixc([222, 236, 244], [246, 230, 200]); ctx.beginPath(); ctx.arc(b.x, b.y, b.r + film * 3, 0, 7); ctx.fillStyle = 'rgba(40,34,30,.16)'; ctx.fill(); let g = ctx.createRadialGradient(b.x + b.r * 0.18, b.y + b.r * 0.22, b.r * 0.05, b.x, b.y, b.r); g.addColorStop(0, `rgb(${far})`); g.addColorStop(0.62, `rgb(${mid})`); g.addColorStop(1, `rgb(${rim})`); ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, 7); ctx.fillStyle = g; ctx.fill(); // shadow cast by the upper-left rim into the hollow ctx.save(); ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, 7); ctx.clip(); g = ctx.createRadialGradient(b.x + b.r * 0.32, b.y + b.r * 0.38, b.r * 0.78, b.x + b.r * 0.32, b.y + b.r * 0.38, b.r * 1.32); g.addColorStop(0, 'rgba(20,28,40,0)'); g.addColorStop(1, `rgba(${bk ? '70,42,16' : '20,34,58'},${bk ? 0.7 : 0.55})`); ctx.fillStyle = g; ctx.fillRect(b.x - b.r, b.y - b.r, b.r * 2, b.r * 2); // rising CO₂ specks while yeast work, steam wisps in the oven if (e.alive && e.rate > 0) { for (let n = 0; n < 9; n++) { const ph = hash(i, n, 5), t = (T * 0.12 * e.rate + ph) % 1, x = b.x + (hash(i, n, 6) - 0.5) * b.r * 1.3, y = b.y + b.r * 0.7 - t * b.r * 1.4, rr = Math.max(1.5, b.r * 0.025 * (1 + t)); ctx.globalAlpha = al('gas') * 0.55 * Math.sin(t * Math.PI); ctx.fillStyle = '#ffffff'; ctx.beginPath(); ctx.arc(x, y, rr, 0, 7); ctx.fill(); } ctx.globalAlpha = al('gas'); } if (steam > 0) { ctx.strokeStyle = `rgba(255,255,255,${0.35 * steam})`; ctx.lineWidth = Math.max(2, b.r * 0.05); ctx.lineCap = 'round'; for (let n = 0; n < 3; n++) { const x0 = b.x + (n - 1) * b.r * 0.35, t = (T * 0.25 + n / 3) % 1; ctx.globalAlpha = al('gas') * Math.sin(t * Math.PI); ctx.beginPath(); ctx.moveTo(x0, b.y + b.r * 0.6 - t * b.r * 0.6); ctx.bezierCurveTo(x0 + b.r * 0.15, b.y + b.r * 0.3 - t * b.r * 0.6, x0 - b.r * 0.15, b.y - t * b.r * 0.6, x0, b.y - b.r * 0.3 - t * b.r * 0.6); ctx.stroke(); } ctx.globalAlpha = al('gas'); } ctx.restore(); // the wall: a thin gluten film with a bright lip on the lit side ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, 7); ctx.strokeStyle = bk > 0.5 ? 'rgba(122,84,48,.95)' : 'rgba(146,118,240,.95)'; ctx.lineWidth = Math.max(film, bk > 0.5 ? 2.5 : 0); ctx.stroke(); ctx.strokeStyle = 'rgba(255,255,255,.75)'; ctx.lineWidth = Math.max(1.2, film * 0.5); ctx.beginPath(); ctx.arc(b.x, b.y, b.r - film * 0.6, 0.15, 1.9); ctx.stroke(); ctx.restore(); { const cx2 = W / 2, cy2 = H / 2, dx = cx2 - b.x, dy = cy2 - b.y, d = Math.hypot(dx, dy) || 1, q = Math.min(b.r * 0.6, d); hits.push({ k: 'gas', x: b.x + dx / d * q, y: b.y + dy / d * q, r: 8, inner: true }); } }); // labels, placed next to the item they name; no two may overlap const LB = { starch: gel > 0.5 ? 'Starch · swollen into gel' : 'Starch granule · real size 2–35 µm', gluten: e.dev < 0.35 ? 'Gluten proteins · not yet joined' : s.id === 'bake' && s.core > 60 ? 'Gluten · setting' : 'Gluten network', yeast: e.alive ? 'Yeast cell · real size 4–10 µm' : 'Yeast · dead at 56 °C', sugar: 'Sugar dissolved · ' + r1(e.left) + ' g left', fat: e.fatMelt > 0.5 ? 'Fat · melted' : 'Fat (shortening)', gas: (s.id === 'bake' && s.core > 80 ? 'Gas cell · CO₂ + steam' : 'Gas cell · CO₂') + ' · ≈ ' + Math.round(s.D / 10) * 10 + ' µm', water: gel > 0.5 ? 'Water + gelatinised starch' : 'Water (with salt, sugar)' }; if (e.sucrose > 0) LB.sugar = 'Sugar crystal · splitting'; if (e.saltXtal > 0) LB.salt = 'Salt crystal · dissolving'; const ibr = inset.getBoundingClientRect(), mbr = mc.getBoundingClientRect(); const taken = [{ x: ibr.left - mbr.left - 8, y: ibr.top - mbr.top - 8, w: ibr.width + 16, h: ibr.height + 16 }, { x: W - (W < 480 ? 230 : 340), y: H - 40, w: W < 480 ? 230 : 340, h: 40 }], order = sel ? [sel] : W < 520 ? ['gas', 'starch', 'yeast', 'gluten'] : ['gas', 'starch', 'gluten', 'yeast', 'sugar', 'fat', 'salt', 'water']; ctx.font = '700 ' + (W < 520 ? 11 : 12.5) + 'px system-ui,sans-serif'; reset(); const free = r => r.x > 6 && r.y > 6 && r.x + r.w < W - 6 && r.y + r.h < H - 6 && !taken.some(t => r.x < t.x + t.w + 6 && r.x + r.w + 6 > t.x && r.y < t.y + t.h + 4 && r.y + r.h + 4 > t.y); for (const k of order) { if (!LB[k]) continue; let cands = k === 'water' ? [{ x: W * 0.5, y: H * 0.5, r: 0 }] : hits.filter(h => h.k === k); if (k === 'water') { for (let i = 0; i < 30; i++) { const x = hash(i, 31) * W, y = hash(i, 32) * H; if (!bubs.some(b => Math.hypot(x - b.x, y - b.y) < b.r + 20) && !hits.some(h => Math.hypot(x - h.x, y - h.y) < h.r + 14)) cands.push({ x, y, r: 0 }); } cands = cands.slice(1); } if (k === 'starch') cands = cands.filter(h => h.big); if (k !== 'gas' && k !== 'water') cands = cands.filter(h => !bubs.some(b => Math.hypot(h.x - b.x, h.y - b.y) < b.r - 1)); const txt = LB[k], tw = ctx.measureText(txt).width + 28, th = 26; let placed = false; for (const h of cands) { const off = (h.r || 0) + 10; for (const [dx, dy] of [[1, 0], [-1, 0], [0, -1], [0, 1], [1, -1], [-1, 1]]) { const r = { x: h.x + dx * off - (dx < 0 ? tw : dx === 0 ? tw / 2 : 0), y: h.y + dy * off - (dy < 0 ? th : dy === 0 ? th / 2 : 0), w: tw, h: th }; if (!free(r)) continue; taken.push(r); placed = true; const col = COL[k]; ctx.globalAlpha = 1; const rr = Math.max(6, Math.min(h.r || 6, 40)) + 3, ax = clamp(h.x, r.x, r.x + r.w), ay = clamp(h.y, r.y, r.y + r.h), dd = Math.hypot(ax - h.x, ay - h.y) || 1; ctx.strokeStyle = 'rgba(16,12,20,.5)'; ctx.lineWidth = 4; ctx.beginPath(); ctx.arc(h.x, h.y, rr, 0, 7); ctx.moveTo(ax, ay); ctx.lineTo(h.x + (ax - h.x) / dd * rr, h.y + (ay - h.y) / dd * rr); ctx.stroke(); ctx.strokeStyle = 'rgba(255,255,255,.95)'; ctx.lineWidth = 2; ctx.stroke(); ctx.shadowColor = 'rgba(0,0,0,.35)'; ctx.shadowBlur = 8; ctx.shadowOffsetY = 2; ctx.fillStyle = 'rgba(16,12,20,.92)'; ctx.beginPath(); ctx.roundRect(r.x, r.y, r.w, r.h, 13); ctx.fill(); ctx.shadowColor = 'transparent'; ctx.shadowBlur = 0; ctx.shadowOffsetY = 0; ctx.fillStyle = col; ctx.beginPath(); ctx.arc(r.x + 13, r.y + 13, 4.5, 0, 7); ctx.fill(); ctx.fillStyle = '#f3ede4'; ctx.textAlign = 'left'; ctx.fillText(txt, r.x + 23, r.y + 17.5); break; } if (placed) break; } } // corner note: what is and is not to scale ctx.font = '600 11px system-ui,sans-serif'; const note = (W < 480 ? 'Drawn large · gas to scale (' : 'Close-up, drawn large to read · gas share to scale (') + Math.round(s.P * 100) + '%)'; const nw = ctx.measureText(note).width + 16; ctx.fillStyle = 'rgba(16,12,20,.78)'; ctx.beginPath(); ctx.roundRect(W - nw - 8, H - 30, nw, 22, 11); ctx.fill(); ctx.fillStyle = '#d8cfdd'; ctx.fillText(note, W - nw, H - 15); ctx.globalAlpha = 1; } const r1 = x => x.toFixed(x < 10 ? 1 : 0); mc.addEventListener('click', ev => { const r = mc.getBoundingClientRect(), x = ev.clientX - r.left, y = ev.clientY - r.top; let hit = 'water', bd = 1e9; for (const h of hits) { const d = Math.hypot(h.x - x, h.y - y) - (h.r || 8); if (d < 6 && d < bd) { bd = d; hit = h.k; } } selectIng(sel === hit ? null : hit); }); /* ---------------- ingredient meters under the close-up ---------------- */ const metersEl = $('meters'), meterEls = {}; ING.forEach(i => { const b = document.createElement('button'); b.type = 'button'; b.className = 'meter'; b.setAttribute('aria-pressed', 'false'); b.innerHTML = `${i.label}`; b.querySelector('i').style.background = i.col; b.querySelector('.mb b').style.background = i.col === '#2c4a6e' ? '#5b86b8' : i.col; b.addEventListener('click', () => selectIng(sel === i.k ? null : i.k)); metersEl.appendChild(b); meterEls[i.k] = b; }); function renderMeters(s, e) { const M = { starch: [e.gel > 0 ? (e.gel < 1 ? 'Gel ' + Math.round(e.gel * 100) + '%' : 'Gel (crumb)') : 'Raw', e.gel || 0.04], gluten: [s.id === 'mix' ? 'Forming ' + Math.round(e.dev * 100) + '%' : s.id === 'bake' ? (s.core > 60 ? 'Setting' : 'Stretching') : s.id === 'cool' ? 'Set' : s.id === 'proof' ? 'Thin films' : 'Stretched', e.dev], water: [Math.round(e.water) + ' g', e.water / 234], yeast: [e.alive ? (e.rate > 1.2 ? 'Fast' : 'Working') : 'Dead', e.alive ? Math.min(1, e.rate / 1.8) : 0], sugar: [r1(e.left) + ' g left', e.left / 14.8], salt: [e.saltXtal > 0 ? 'Dissolving' : 'Dissolved', 1 - e.saltXtal], fat: [e.fatMelt > 0 ? (e.fatMelt < 1 ? 'Melting' : 'Melted') : 'Solid', e.fatMelt || 0.04], gas: [Math.round(s.P * 100) + '%', s.P] }; for (const k in M) { const el = meterEls[k]; el.querySelector('.mv').textContent = M[k][0]; el.querySelector('.mb b').style.width = Math.round(clamp(M[k][1]) * 100) + '%'; el.setAttribute('aria-pressed', sel === k); } const hl = HEAD(s), he = $('headline'); if (he.textContent !== hl) { he.textContent = hl; he.classList.remove('in'); void he.offsetWidth; he.classList.add('in'); } } /* ---------------- the bread: a tag, the spot the close-up shows, and zoom lines to the close-up ---------------- */ let marker = null; const zc = $('zoom'), zx = zc.getContext('2d'), viz = $('viz'); function drawZoom() { if (zc.width) { zc.width = 0; zc.height = 0; } return; const W = viz.clientWidth, H = viz.clientHeight, dpr = Math.min(devicePixelRatio || 1, 2); if (zc.width !== Math.round(W * dpr) || zc.height !== Math.round(H * dpr)) { zc.width = Math.round(W * dpr); zc.height = Math.round(H * dpr); } zx.setTransform(dpr, 0, 0, dpr, 0, 0); zx.clearRect(0, 0, W, H); if (!marker) return; const vb = viz.getBoundingClientRect(), ib = inset.getBoundingClientRect(), sb = mc.getBoundingClientRect(); const mx0 = ib.left - vb.left + marker[0], my0 = ib.top - vb.top + marker[1], q = 9; if (mx0 < ib.left - vb.left || mx0 > ib.right - vb.left || my0 < ib.top - vb.top || my0 > ib.bottom - vb.top) return; const L = sb.left - vb.left, T = sb.top - vb.top, R = sb.right - vb.left, B = sb.bottom - vb.top; const side = L > mx0 + q; // close-up to the right (desktop) or below (phone) const pairs = side ? [[mx0 + q, my0 - q, L, T], [mx0 + q, my0 + q, L, B]] : [[mx0 - q, my0 + q, L, T], [mx0 + q, my0 + q, R, T]]; zx.fillStyle = 'rgba(242,177,63,.07)'; zx.beginPath(); zx.moveTo(pairs[0][0], pairs[0][1]); zx.lineTo(pairs[0][2], pairs[0][3]); zx.lineTo(pairs[1][2], pairs[1][3]); zx.lineTo(pairs[1][0], pairs[1][1]); zx.closePath(); zx.fill(); zx.setLineDash([5, 5]); zx.strokeStyle = 'rgba(242,177,63,.85)'; zx.lineWidth = 1.5; for (const [a, b, c, d] of pairs) { zx.beginPath(); zx.moveTo(a, b); zx.lineTo(c, d); zx.stroke(); } zx.setLineDash([]); zx.fillStyle = '#f2b13f'; zx.font = '800 11px system-ui,sans-serif'; zx.textAlign = 'center'; const lx = side ? (mx0 + L) / 2 : (mx0 + (L + R) / 2) / 2, ly = side ? (my0 + (T + B) / 2) / 2 - 8 : (my0 + T) / 2; const tw = zx.measureText('close-up of this spot').width + 16; zx.fillStyle = 'rgba(16,12,20,.9)'; zx.beginPath(); zx.roundRect(lx - tw / 2, ly - 14, tw, 20, 10); zx.fill(); zx.fillStyle = '#f2b13f'; zx.fillText('close-up of this spot', lx, ly); } const v3 = new THREE.Vector3(); function proj(x, y, z) { v3.set(x, y, z).project(camera); return [(v3.x + 1) / 2 * hud.clientWidth, (1 - v3.y) / 2 * hud.clientHeight]; } function drawHud(s) { const W = hud.clientWidth, H = hud.clientHeight, dpr = Math.min(devicePixelRatio || 1, 2); if (hud.width !== Math.round(W * dpr)) { hud.width = Math.round(W * dpr); hud.height = Math.round(H * dpr); } hx.setTransform(dpr, 0, 0, dpr, 0, 0); hx.clearRect(0, 0, W, H); const p = proj(0, dims.y + dims.Hb * 0.9, 0), q = Math.max(4, W * 0.035); const cap = W < 160 ? 'The bread · ▢ close-up' : 'The bread · ▢ = this close-up'; if ($('insetCap').textContent !== cap) $('insetCap').textContent = cap; hx.fillStyle = 'rgba(242,177,63,.18)'; hx.fillRect(p[0] - q, p[1] - q, 2 * q, 2 * q); hx.strokeStyle = 'rgba(16,12,20,.6)'; hx.lineWidth = 4; hx.strokeRect(p[0] - q, p[1] - q, 2 * q, 2 * q); hx.strokeStyle = '#f2b13f'; hx.lineWidth = 2; hx.strokeRect(p[0] - q, p[1] - q, 2 * q, 2 * q); marker = p; let t1 = s.id === 'bake' ? 'Core ' + Math.round(s.core) + ' °C' : s.id === 'mix' ? (s.f > 0.97 ? '25 °C' : 'Mixing') : s.id === 'rise' ? '30 °C' : s.id === 'proof' ? '43 °C' : s.id === 'shape' ? '500 g' : (s.f > 0.98 ? '423 g' : 'Cooling'); if (W > 180) t1 += ' · ' + Math.round(s.V / 10) * 10 + ' cm³'; hx.font = '800 10.5px system-ui,sans-serif'; const tw = hx.measureText(t1).width + 14; hx.fillStyle = 'rgba(18,14,22,.9)'; hx.strokeStyle = s.id === 'bake' && s.core >= 56 ? '#f26a4f' : '#f2b13f'; hx.lineWidth = 1.2; hx.beginPath(); hx.roundRect(6, 6, tw, 19, 9.5); hx.fill(); hx.stroke(); hx.fillStyle = '#f3ede4'; hx.textAlign = 'left'; hx.fillText(t1, 13, 19.5); } /* ---------------- panel: now, ingredients, the whole dough ---------------- */ const esc = t => String(t).replace(/[&<>"]/g, c => ({ '&': '&', '<': '<', '>': '>', '"': '"' }[c])); function tagHTML(tags) { const by = {}; for (const t of tags) (by[t.t] || (by[t.t] = [])).push(...(t.src || [])); return ['M', 'L', 'C', 'E', 'N'].filter(k => by[k]).map(k => `${TAGN[k]}`).join(''); } let open = null, hiKey = null; function makeList(el, map) { return rows => { const seen = new Set(); rows.forEach(r => { seen.add(r.k); let row = map.get(r.k); if (!row) { row = document.createElement('div'); row.className = 'row'; row.setAttribute('role', 'listitem'); row.innerHTML = ''; row.firstChild.addEventListener('click', () => { open = open === r.k ? null : r.k; if (ING.some(i => i.k === r.k)) sel = open === r.k ? r.k : (sel === r.k ? null : sel); syncOpen(); dirty = true; }); map.set(r.k, row); row._c = {}; } const c = row._c, b = row.firstChild; const lh = (COL[r.k] ? `` : '') + esc(r.label); if (c.l !== lh) { b.querySelector('.k').innerHTML = c.l = lh; const sw = b.querySelector('.sw'); if (sw) sw.style.background = COL[r.k]; } const th = tagHTML(r.tags); if (c.t !== th) b.querySelector('.tags').innerHTML = c.t = th; const vh = esc(r.v) + (r.sub ? ' · ' + esc(r.sub) + '' : ''); if (c.v !== vh) b.querySelector('.v').innerHTML = c.v = vh; const srcs = [...new Set(r.tags.flatMap(t => t.src || []))]; const wh = '

' + esc(r.why) + '

' + srcs.map(k => `

${esc(SHORT[k])} · ${esc(SRC[k].read)} · open the source

`).join(''); if (c.w !== wh) row.lastChild.innerHTML = c.w = wh; row.classList.toggle('hi', r.k === hiKey); row.classList.toggle('sel', r.k === sel); el.appendChild(row); }); for (const [k, row] of map) if (!seen.has(k) && row.parentNode) row.remove(); }; } const ingMap = new Map(), condMap = new Map(), ALL = () => [...ingMap, ...condMap]; const renderIng = makeList($('ing'), ingMap), renderCond = makeList($('cond'), condMap); function syncOpen() { for (const [k, row] of ALL()) { const on = k === open; row.firstChild.setAttribute('aria-expanded', on); row.lastChild.hidden = !on; row.classList.toggle('sel', k === sel); } } function selectIng(k) { sel = k; open = k; syncOpen(); dirty = true; const row = ingMap.get(k); if (row && k && innerWidth > 860) row.scrollIntoView({ block: 'nearest', behavior: 'smooth' }); } const WHOLE = ['temp', 'surf', 'size', 'ph', 'wt']; let lastNow = ''; function renderPanel(s) { renderIng(ingredients(s)); renderCond(condition(s).filter(r => WHOLE.includes(r.k))); const pr = practical(s), key = s.id; if (key !== lastNow) { lastNow = key; $('nowH').textContent = (s.si + 1) + ' · ' + STEPS[s.si].name; $('nowT').textContent = pr.what; $('nowC').innerHTML = pr.check.map(c => `
  • ${esc(c.t)} ${tagHTML(c.tags)}
  • `).join(''); } syncOpen(); } $('srcList').innerHTML = Object.keys(SRC).map(k => `
  • ${esc(SRC[k].c)} Link
    Read: ${esc(SRC[k].read)}. Used for: ${esc(SRC[k].what)}
  • `).join(''); /* ---------------- steps, timeline, playback, walk-through ---------------- */ const stepsEl = $('steps'), timeEl = $('time'); const JUMP = [999, 1526, 2999, 3999, 4700, 5999]; STEPS.forEach((st, i) => { const b = document.createElement('button'); b.type = 'button'; b.innerHTML = `${i + 1} · ${esc(st.name)}${esc(st.sub)}`; b.addEventListener('click', () => { stopTour(); setPlaying(false); setU(JUMP[i]); }); stepsEl.appendChild(b); }); let u = 999, playing = false, dirty = true; function setU(v) { u = clamp(v, 0, 5999.999); timeEl.value = Math.round(u); dirty = true; } timeEl.addEventListener('input', () => { stopTour(); setPlaying(false); u = +timeEl.value; dirty = true; }); function setPlaying(on) { playing = on; $('play').textContent = on ? '❚❚' : '▶'; $('play').setAttribute('aria-label', on ? 'Pause' : 'Play'); } $('play').addEventListener('click', () => { stopTour(); if (!playing && u >= 5999) setU(0); setPlaying(!playing); }); const TOUR = [ { a: 0, b: 300, ms: 6500, k: 'water', t: 'This is a close-up of the inside of the dough, drawn large so you can read it. Mixing has just started: flour is getting wet, sugar and salt crystals are dissolving. The bars below show each ingredient right now.' }, { a: 300, b: 999, ms: 7000, k: 'gluten', t: 'Mixing joins the wet gluten proteins (purple) into a net that ties the starch granules together, and traps air as tiny bubbles.' }, { a: 1000, b: 1526, ms: 8000, k: 'yeast', t: 'First rise at 30 °C. Each yeast cell eats the dissolved sugar (pink) and breathes out CO₂ (the little blue bubbles), which flies into the gas cells and makes them grow. Watch the Sugar bar drop.' }, { a: 1527, b: 1999, ms: 6500, k: 'gas', t: 'Knock-back at 95 and 145 min: gas is pressed out and the big cells split into small ones. The Gas bar falls, then climbs again.' }, { a: 3000, b: 3999, ms: 7500, k: 'gas', t: 'Proof at 43 °C. The cells grow until they press against each other and the walls between them are thin films. The ingredients get squeezed into those walls.' }, { a: 4000, b: 4800, ms: 9000, k: 'starch', t: 'Bake. Gas expands and the loaf springs. At 56 °C the yeast die (grey). Between 55 and 70 °C the starch swells into gel and the dough becomes crumb.' }, { a: 5000, b: 5999, ms: 6000, k: 'water', t: 'Cool: 423 g from 500 g, the difference mostly water gone as steam. Tap any ingredient, in the picture or the bars, to follow it; the panel on the right shows where every number comes from.' } ]; let tour = null; function startTour() { setPlaying(false); open = null; sel = null; tour = { i: 0, t0: performance.now() }; $('tourBox').hidden = false; showBeat(); } function showBeat() { const b = TOUR[tour.i]; $('tourT').textContent = b.t; $('tourN').textContent = (tour.i + 1) + ' of ' + TOUR.length; hiKey = b.k; sel = b.k; tour.t0 = performance.now(); setU(b.a); } function stopTour() { if (!tour) return; tour = null; hiKey = null; sel = null; $('tourBox').hidden = true; dirty = true; } $('tour').addEventListener('click', () => tour ? stopTour() : startTour()); $('tourSkip').addEventListener('click', stopTour); function resize() { const w = inset.clientWidth, h = inset.clientHeight; if (renderer) renderer.setSize(w, h, false); camera.aspect = w / Math.max(1, h); camera.updateProjectionMatrix(); dirty = true; } addEventListener('resize', resize); resize(); let last = performance.now(), sNow = stateAt(u), eNow = extra(sNow), lastSi = -1, lastMicro = 0; function frame(now) { const dt = Math.min(0.05, (now - last) / 1000); last = now; if (tour) { const b = TOUR[tour.i], k = (now - tour.t0) / b.ms; if (k >= 1.2) { if (tour.i + 1 < TOUR.length) { tour.i++; showBeat(); } else stopTour(); } else { u = lerp(b.a, b.b, smooth(k)); timeEl.value = Math.round(u); dirty = true; } } else if (playing) { u += dt * 1000 / 10; if (u >= 5999.999) { u = 5999.999; setPlaying(false); } timeEl.value = Math.round(u); dirty = true; } if (dirty) { sNow = stateAt(u); eNow = extra(sNow); buildDough(sNow); drawCap(sNow, sNow.si !== lastSi); renderPanel(sNow); ui(sNow); lastSi = sNow.si; const heat = sNow.id === 'bake' ? smooth(sNow.f * 4) * (1 - smooth((sNow.f - 0.985) * 60)) : 0; oven.visible = heat > 0.01; heatMat.emissiveIntensity = heat * 2.2; ovenWall.material.emissiveIntensity = heat * 0.8; ovenLight.intensity = heat * 170; inset.classList.toggle('hot', heat > 0.01); } if (dirty || (!reduced && now - lastMicro > 33)) { drawMicro(sNow, eNow, now); renderMeters(sNow, eNow); lastMicro = now; } dirty = false; placeCamera(dt); if (renderer) renderer.render(scene, camera); drawHud(sNow); drawZoom(); requestAnimationFrame(frame); } function ui(s) { const st = STEPS[s.si]; $('capN').textContent = s.si + 1; $('capH').textContent = st.name; $('capC').textContent = fmtClock(s.clock); $('clock').textContent = fmtClock(s.clock); [...stepsEl.children].forEach((b, i) => { if (i === s.si) b.setAttribute('aria-current', 'step'); else b.removeAttribute('aria-current'); }); } requestAnimationFrame(frame); new ResizeObserver(() => { dirty = true; }).observe(stage); new ResizeObserver(resize).observe(inset); let seen = false; try { seen = localStorage.getItem('living-dough-v4') === '1'; localStorage.setItem('living-dough-v4', '1'); } catch (e) { } if (!seen && !reduced) setTimeout(startTour, 900);