// Canvas scenes driven by the click-step engine's `stepchange` event. Each scene eases every item // toward the targets for the current beat (exponential smoothing, so going back works the same as // going forward) and stops its animation loop once everything has arrived. ?static, arriving // backward, or a slide that isn't current jumps straight to the targets. Deterministic: seeded. ;(() => { const root = document.documentElement const css = (el, name) => getComputedStyle(el).getPropertyValue(name).trim() const rng = (seed) => () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 4294967296) window.__scenes = {} function scene(id, { items, targets, draw }) { const section = document.getElementById(id) if (!section) return const canvas = section.querySelector('canvas') const ctx = canvas.getContext('2d') const state = { step: 0, since: 0, raf: 0, running: false } const all = items() const settle = () => { for (const it of all) Object.assign(it.cur, targets(it, state.step, Infinity)) } const frame = (now) => { const el = now - state.since let moving = false for (const it of all) { const t = targets(it, state.step, state.wave ? el : Infinity) for (const k in t) { if (k[0] === '_') continue const d = t[k] - it.cur[k] if (Math.abs(d) > 0.002) { it.cur[k] += d * 0.085; moving = true } else it.cur[k] = t[k] } if (t.__wait) moving = true } render() if (moving) state.raf = requestAnimationFrame(frame) else state.running = false } const render = () => { ctx.clearRect(0, 0, canvas.width, canvas.height) draw(ctx, all, { ink: css(section, '--ink'), muted: css(section, '--muted'), accent: css(section, '--accent'), page: css(section, '--page') }) } const go = (instant) => { cancelAnimationFrame(state.raf) const prev = state.step state.step = Number(section.dataset.step ?? 0) // Staggered entry (a wave) only when moving forward from the beat before; any other change // (a later beat, going back) keeps everything where it is. state.wave = state.step === prev + 1 state.since = performance.now() const live = !instant && root.dataset.motion !== 'off' && window.__deck?.getCurrentSlide() === section && !section.classList.contains('m-instant') if (!live) { settle(); state.running = false; render(); return } state.running = true state.raf = requestAnimationFrame(frame) } section.addEventListener('stepchange', () => go(false)) window.__deck.on('slidechanged', (e) => { if (e.currentSlide === section || e.previousSlide === section) go(e.currentSlide !== section || section.classList.contains('m-instant')) }) new MutationObserver(render).observe(root, { attributes: true, attributeFilter: ['data-theme'] }) window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => setTimeout(render, 50)) document.fonts.ready.then(() => go(true)) window.__scenes[id] = { state, settled: () => !state.running } } // Title: a few people, then a dome full of them, each with a little code chip that fades away. scene('title', { items() { const r = rng(7) const out = [] const sp = 34 for (let y = sp / 2; y < 1080; y += sp) { for (let x = sp / 2; x < 1920; x += sp) { const px = x + (r() - 0.5) * sp * 0.7, py = y + (r() - 0.5) * sp * 0.7 const d = Math.hypot((px - 960) / 960, (py - 540) / 540) out.push({ x: px, y: py, d, hole: Math.hypot((px - 960) / 760, (py - 540) / 230), delay: d * 1500 + r() * 350, few: false, cur: { a: 0, s: 0.4, c: 1 } }) } } // The first few: scattered around the word. const few = out.filter((p) => p.d > 0.55 && p.d < 0.85) for (let i = 0; i < 26; i++) few[Math.floor(r() * few.length)].few = true return out }, targets(p, step, el) { // Beat 1 fills the dome in a wave; beat 2 (the title) holds the full crowd exactly as it is. const on = step >= 2 ? true : step === 1 ? el >= p.delay || p.few : p.few const wait = step === 1 && el < p.delay && !p.few // A clear oval around the words. Laptops (coders) only in the "millions" beat; the title beat // changes only the words, so the crowd holds exactly as it was. const hole = Math.min(1, Math.max(0, (p.hole - 0.75) / 0.35)) return { a: on ? hole : 0, s: on ? (step === 0 ? 2 : 1) : 0.4, c: step >= 1 ? 0 : 1, __wait: wait ? 1 : 0 } }, draw(ctx, all, col) { for (const p of all) { const { a, s, c } = p.cur if (a < 0.01) continue ctx.globalAlpha = a ctx.fillStyle = col.muted ctx.beginPath() ctx.arc(p.x, p.y - 5 * s, 4.6 * s, 0, Math.PI * 2) ctx.fill() ctx.beginPath() ctx.ellipse(p.x, p.y + 8 * s, 8 * s, 6 * s, 0, Math.PI, 0) ctx.fill() if (c > 0.01) { // A laptop in front of the person, seen from behind: the person sits at it, so we see the // back of the lid (solid, with a small logo dot) and the edge of the base. ctx.globalAlpha = a * c ctx.fillStyle = col.accent const w = 16 * s, h = 10 * s, x = p.x - w / 2, y = p.y + 1 * s ctx.beginPath() ctx.roundRect(x, y, w, h, 1.8 * s) ctx.fill() ctx.fillStyle = col.page ctx.beginPath() ctx.arc(p.x, y + h / 2, 1.5 * s, 0, Math.PI * 2) ctx.fill() ctx.fillStyle = col.accent ctx.beginPath() ctx.roundRect(x - 2 * s, y + h - 0.2 * s, w + 4 * s, 2 * s, 1 * s) ctx.fill() } } ctx.globalAlpha = 1 }, }) // Counters: . The number rolls to the value for the // slide's current beat (the first value on entry rolls up from 0). Static, backward or off-slide: it // jumps straight there. for (const el of document.querySelectorAll('.reveal .slides section [data-counter]')) { const section = el.closest('section') const values = el.dataset.counter.split(',').map(Number) const unit = el.dataset.unit ?? '' const fmt = (n) => (Math.round(n) === 0 ? '0' : Math.round(n).toLocaleString('en-US') + unit) let cur = values[0] let raf = 0 const show = (n) => { cur = n; el.textContent = fmt(n) } const go = (fromZero) => { cancelAnimationFrame(raf) const step = Number(section.dataset.step ?? 0) const target = values[Math.min(step, values.length - 1)] const live = root.dataset.motion !== 'off' && window.__deck?.getCurrentSlide() === section && !section.classList.contains('m-instant') if (!live) return show(target) const from = fromZero ? 0 : cur const t0 = performance.now() const tick = (now) => { const x = Math.min(1, (now - t0) / 1600) show(from + (target - from) * (1 - (1 - x) ** 3)) if (x < 1) raf = requestAnimationFrame(tick) } raf = requestAnimationFrame(tick) } section.addEventListener('stepchange', () => go(false)) window.__deck.on('slidechanged', (e) => { if (e.currentSlide !== section) return show(values[Math.min(Number(section.dataset.step ?? 0), values.length - 1)]) const back = section.classList.contains('m-instant') go(!back && Number(section.dataset.step ?? 0) === 0) }) window.__deck.on('ready', (e) => { if (e.currentSlide === section) go(true); else show(values[Math.min(Number(section.dataset.step ?? 0), values.length - 1)]) }) show(values[Math.min(Number(section.dataset.step ?? 0), values.length - 1)]) } // Problems: the code behind the camping-trip app (illustrative, never meant to be read). Filled // here so the slide source stays short; keywords and strings get a tint. { const pre = document.querySelector('#problems .code pre') if (pre) { const people = ['eli', 'priya', 'sam', 'jo', 'maya', 'you'] const kw = /\b(import|from|export|const|let|async|await|function|return|if|else|for|of|new|type|throw)\b/g const lines = [] const chunk = (n, who) => [ `import { z } from 'zod'`, `import { requestPayment } from './payments/${who}.ts'`, ``, `const Expense${n} = z.object({ payer: z.string(), amount: z.number(), note: z.string() })`, `type Split${n} = { from: string; to: string; cents: number }`, ``, `export async function settle${n}(trip: Trip, expenses: Expense[]) {`, ` const total = expenses.reduce((sum, e) => sum + Math.round(e.amount * 100), 0)`, ` const share = Math.round(total / trip.people.length)`, ` const balances = new Map(trip.people.map((p) => [p.id, -share]))`, ` for (const e of expenses) {`, ` balances.set(e.payer, (balances.get(e.payer) ?? 0) + Math.round(e.amount * 100))`, ` }`, ` const owed: Split${n}[] = []`, ` for (const [id, cents] of balances) {`, ` if (cents >= 0) continue`, ` owed.push({ from: id, to: trip.organizer, cents: -cents })`, ` }`, ` const results = await Promise.all(owed.map((o) => requestPayment(o, { memo: 'Camp trip' })))`, ` await db.insert('settlements', { trip: trip.id, results, at: Date.now() })`, ` if (results.some((r) => r.status === 'failed')) throw new Error('retry ${who}')`, ` return { share, owed, paid: results.filter((r) => r.status === 'paid').length }`, `}`, ``, `function formatCents${n}(cents: number) {`, ` return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(cents / 100)`, `}`, ``, ] for (let i = 0; i < 12; i++) lines.push(...chunk(i + 1, people[i % people.length])) const esc = (t) => t.replace(/&/g, '&').replace(/ esc(l).replace(/'[^']*'/g, (m) => `${m}`).replace(kw, '$1')).join('\n') } } })()