Skip to content
← Public packages

@kentcdodds/the-last-software-engineer

The Last Software Engineer: Kent C. Dodds's React Summit US 2026 talk deck. Judgment and product engineering in an AI era.

FollowForkOpen app

Files

plank.js

435 lines · 15.0 KB · JavaScript
// "The work": a small, deterministic rigid-body simulation drawn on canvas.
//
// The metaphor (Kent): the work is a bar that must span from one stilt to the other. Who does the
// work doesn't matter; it just has to get done. If any of it is missing, the bar no longer reaches
// the right stilt, so it tips off its left support and falls until its free end hits the ground.
// Once the missing work is filled in, the bar is lifted back onto both stilts.
//
// Model: the bar is a rigid body pivoting about the inner edge of the left stilt's pad. Its filled
// length carries uniform mass (60 slices; every part of the work weighs the same). Gravity gives
// the tipping torque. The right pad (only when the bar is complete) and the ground are stiff,
// damped stops, so it lands with a small bounce. A complete bar is lifted back to level by a
// damped spring. A sag mode bends it slightly under its own weight. Integration is semi-implicit
// Euler at a fixed 240 Hz. Each beat's settled pose is precomputed (simulate to rest) and the bar
// always snaps to exactly that pose at the end, so forward, back, ?static, the overview and the
// speaker preview all show the same settled state for a beat.
;(() => {
	const deck = window.__deck
	const root = document.documentElement
	// Geometry in slide px (the slide canvas is 1920x1080). The canvas spans the dome-safe width.
	const W = 1520
	const HGT = 680
	const PX = 760 // centre of the bar
	const PY = 345 // bar bottom (and the stilt pad tops) when level
	const H = 689 // bar half length
	const T = 195 // bar thickness
	const PAD_W = 104
	const PAD_H = 29
	const PAD_OUT = 16 // pads stick out past the bar ends, so the bar never overhangs a stilt
	const PIVOT = -H + PAD_W - PAD_OUT // inner edge of the left pad: the bar tips about it
	const GROUND = PY + 325
	const N = 60
	const DT = 1 / 240
	// Physics constants (lengths normalized by the bar length 2H; full bar mass = 1).
	const G = 1
	const I0 = 0.02
	const C_AIR = 0.03
	const K_STOP = 260 // right pad (complete bar only) and the ground
	const C_STOP = 3.2
	const K_LIFT = 6 // a complete bar is lifted back onto both stilts
	const C_LIFT = 1.1
	const SAG_PX = 12 // midspan sag of a full bar at rest
	const K_SAG = 150
	const C_SAG = 7.5

	const ease = (x) => (x < 0.5 ? 4 * x * x * x : 1 - (-2 * x + 2) ** 3 / 2)

	// Beats. a = end of the first segment, b = end of the work actually done (fractions of the
	// span, 0 = left end, 1 = right end). b < 1 leaves a gap and the bar falls. ghost shows the
	// missing work as a dashed outline where it should be.
	const scenes = {
		'the-work': {
			beats: [
				{ a: 0.24, b: 1, agents: 0, arrow: 0, almost: 0, ghost: 0 },
				{ a: 0.24, b: 0.64, agents: 0, arrow: 0, almost: 0, ghost: 1 },
				{ a: 0.74, b: 1, agents: 1, arrow: 1, almost: 0, ghost: 0 },
			],
			labelA: 'Your Tools',
			labelB: 'You',
		},
		agi: {
			beats: [
				{ a: 1, b: 1, agents: 0, arrow: 0, almost: 0, ghost: 0 },
				{ a: 0.9, b: 0.9, agents: 0, arrow: 0, almost: 1, ghost: 1 },
				{ a: 0.9, b: 1, agents: 0, arrow: 0, almost: 1, ghost: 0 },
			],
			labelA: 'Artificial General Intelligence',
			labelB: 'You',
		},
	}
	const complete = (p) => p.b >= 0.999

	// Slices of done work: [distance from the pivot (normalized), mass].
	function slices(p) {
		const out = []
		for (let i = 0; i < N; i++) {
			const f = (i + 0.5) / N
			if (f > p.b) break
			out.push([(f * 2 * H - H - PIVOT) / (2 * H), 1 / N])
		}
		return out
	}
	const reach = (p) => (p.b * 2 * H - H - PIVOT) // px from the pivot to the end of the done work

	function stepSim(s, p) {
		let torque = 0
		let inertia = I0
		let mass = 0
		for (const [d, m] of slices(p)) {
			torque += G * m * d * Math.cos(s.theta) // positive: the free end goes down
			inertia += m * d * d
			mass += m
		}
		torque -= C_AIR * s.omega
		if (complete(p)) {
			// Resting on both stilts: the right pad stops it at level; a damped lift brings it back up.
			torque += -K_LIFT * s.theta - C_LIFT * s.omega
			if (s.theta > 0) torque += -K_STOP * s.theta - C_STOP * Math.max(0, s.omega)
		}
		// The ground stops the free end (its bottom corner).
		const L = reach(p)
		const ground = Math.asin(Math.min(1, (GROUND - PY) / Math.max(L, GROUND - PY + 1)))
		if (s.theta > ground) torque += -K_STOP * (s.theta - ground) - C_STOP * Math.max(0, s.omega)
		s.omega += (torque / inertia) * DT
		s.theta += s.omega * DT
		const fa = SAG_PX * mass * Math.cos(s.theta) - s.droop
		s.dv += (K_SAG * fa - C_SAG * s.dv) * DT
		s.droop += s.dv * DT
	}

	const equilibria = {}
	function equilibrium(id, step) {
		const key = `${id}:${step}`
		if (equilibria[key]) return equilibria[key]
		const p = scenes[id].beats[step]
		const s = { theta: 0, omega: 0, droop: 0, dv: 0 }
		for (let i = 0; i < 240 * 30; i++) stepSim(s, p)
		return (equilibria[key] = { theta: s.theta, omega: 0, droop: s.droop, dv: 0 })
	}

	const css = (el, name) => getComputedStyle(el).getPropertyValue(name).trim()

	function makePlank(section) {
		const id = section.id
		const scene = scenes[id]
		const canvas = section.querySelector('canvas.plank')
		const ctx = canvas.getContext('2d')
		let step = Number(section.dataset.step ?? 0)
		let params = { ...scene.beats[step] }
		let state = { ...equilibrium(id, step) }
		let tweens = []
		let raf = 0
		let last = 0
		let acc = 0
		let started = 0
		let lift = null // { from, dur }: filling a gap, the bar first eases back to horizontal
		const LIFT_MS = 1000

		function resize() {
			const box = canvas.getBoundingClientRect()
			const k = box.width > 0 ? (box.width / W) * (window.devicePixelRatio || 1) : 1
			const w = Math.max(1, Math.round(W * k))
			const h = Math.max(1, Math.round(HGT * k))
			if (canvas.width !== w || canvas.height !== h) {
				canvas.width = w
				canvas.height = h
			}
			draw()
		}

		// Bar frame: x along the bar (px from its centre), y up is negative. Rotates about the pivot.
		const sag = (x) => state.droop * Math.sin((Math.PI * (x + H)) / (2 * H))
		const at = (x, y, theta = state.theta, bend = true) => {
			const dx = x - PIVOT
			const yy = y + (bend ? sag(x) : 0)
			const c = Math.cos(theta)
			const s = Math.sin(theta)
			return [PX + PIVOT + dx * c - yy * s, PY + dx * s + yy * c]
		}
		function segPath(x0, x1, theta, bend) {
			ctx.beginPath()
			const n = Math.max(2, Math.ceil((x1 - x0) / 40))
			for (let i = 0; i <= n; i++) ctx.lineTo(...at(x0 + ((x1 - x0) * i) / n, -T, theta, bend))
			for (let i = n; i >= 0; i--) ctx.lineTo(...at(x0 + ((x1 - x0) * i) / n, 0, theta, bend))
			ctx.closePath()
		}
		function label(text, x0, x1, font, color, dy, alpha) {
			if (alpha <= 0.01 || x1 - x0 < 2) return
			ctx.save()
			// Shrink to fit with 36px of inner padding each side, never below 40px (dome-readable).
			const size = Number(font.match(/(\d+)px/)[1])
			ctx.font = font
			const room = x1 - x0
			const fit = Math.max(40, Math.min(size, ((room - 72) / ctx.measureText(text).width) * size))
			ctx.font = font.replace(/\d+px/, `${Math.floor(fit)}px`)
			const tw = ctx.measureText(text).width
			const a = alpha * Math.min(1, Math.max(0, (room - tw - 12) / 16)) // fully on or off at rest
			if (a > 0.01) {
				segPath(x0, x1)
				ctx.clip()
				const xc = (x0 + x1) / 2
				const [sx, sy] = at(xc, -T / 2 + dy)
				ctx.translate(sx, sy)
				ctx.rotate(state.theta)
				ctx.globalAlpha = a
				ctx.fillStyle = color
				ctx.textAlign = 'center'
				ctx.textBaseline = 'middle'
				ctx.fillText(text, 0, 0)
			}
			ctx.restore()
		}

		function draw() {
			const k = canvas.width / W
			ctx.setTransform(k, 0, 0, k, 0, 0)
			ctx.clearRect(0, 0, W, HGT)
			const ink = css(section, '--ink')
			const soft = css(section, '--soft')
			const surface = css(section, '--surface')
			const surface2 = css(section, '--surface-2')
			const muted = css(section, '--muted')
			const accent = css(section, '--accent')
			const line = css(section, '--line')
			ctx.lineJoin = 'round'
			ctx.lineCap = 'round'

			// Ground and the two stilts (pads under the bar ends, legs splayed out to the ground).
			ctx.strokeStyle = line
			ctx.lineWidth = 6
			ctx.beginPath()
			ctx.moveTo(PX - 740, GROUND)
			ctx.lineTo(PX + 740, GROUND)
			ctx.stroke()
			ctx.strokeStyle = ink
			ctx.lineWidth = 7
			for (const side of [-1, 1]) {
				const x0 = side < 0 ? PX - H - PAD_OUT : PX + H + PAD_OUT - PAD_W
				const cx = x0 + PAD_W / 2
				ctx.beginPath()
				ctx.moveTo(cx - side * 22, PY + PAD_H)
				ctx.lineTo(cx + side * 30, GROUND)
				ctx.moveTo(cx + side * 30, PY + PAD_H)
				ctx.lineTo(cx + side * 64, GROUND)
				ctx.stroke()
				ctx.fillStyle = surface2
				ctx.beginPath()
				ctx.roundRect(x0, PY, PAD_W, PAD_H, 8)
				ctx.fill()
				ctx.stroke()
			}

			const xa = -H + params.a * 2 * H
			const xb = -H + params.b * 2 * H
			// The missing work: a dashed outline where it should be, level between the bar and the stilt.
			if (params.ghost > 0.01 && xb < H - 1) {
				ctx.save()
				ctx.globalAlpha = params.ghost
				ctx.setLineDash([18, 18])
				ctx.lineWidth = 7
				ctx.strokeStyle = muted
				ctx.beginPath()
				ctx.rect(PX + xb, PY - T, H - xb, T)
				ctx.stroke()
				ctx.restore()
			}
			// The work that is done.
			segPath(-H, Math.min(xa, xb))
			ctx.fillStyle = soft
			ctx.fill()
			if (xb > xa + 0.5) {
				segPath(xa, xb)
				ctx.fillStyle = surface
				ctx.fill()
			}
			segPath(-H, xb)
			ctx.lineWidth = 7
			ctx.strokeStyle = ink
			ctx.stroke()
			if (xa > -H && xa < xb - 0.5) {
				ctx.beginPath()
				ctx.moveTo(...at(xa, -T))
				ctx.lineTo(...at(xa, 0))
				ctx.stroke()
			}

			// Labels.
			const hasSub = params.agents > 0.01 || params.almost > 0.01
			const subA = id === 'the-work' ? '(agents)' : '(Almost)'
			const subAlpha = id === 'the-work' ? params.agents : params.almost
			const mainDy = hasSub ? (id === 'the-work' ? -21 : 23) * Math.max(params.agents, params.almost) : 0
			const aEnd = Math.min(xa, xb)
			label(scene.labelA, -H, aEnd, `700 72px ${css(section, '--font-display')}`, ink, mainDy, 1)
			if (hasSub) label(subA, -H, aEnd, `600 56px ${css(section, '--font-body')}`, muted, id === 'the-work' ? 47 : -49, subAlpha)
			if (xb > xa + 0.5) {
				const room = xb - xa
				if (room > 260) label(scene.labelB, xa, xb, `700 72px ${css(section, '--font-display')}`, ink, 0, 1)
				else {
					// A sliver: "Y o u" stacked.
					const a = Math.min(1, Math.max(0, (room - 39) / 20))
					if (a > 0.01) {
						ctx.save()
						segPath(xa, xb)
						ctx.clip()
						const [sx, sy] = at((xa + xb) / 2, -T / 2)
						ctx.translate(sx, sy)
						ctx.rotate(state.theta)
						ctx.globalAlpha = a
						ctx.fillStyle = ink
						ctx.font = `700 50px ${css(section, '--font-display')}`
						ctx.textAlign = 'center'
						ctx.textBaseline = 'middle'
						;['Y', 'o', 'u'].forEach((ch, i) => ctx.fillText(ch, 0, (i - 1) * 50))
						ctx.restore()
					}
				}
			}

			// The push arrow, riding above the bar.
			if (params.arrow > 0.01) {
				ctx.save()
				ctx.globalAlpha = params.arrow
				ctx.strokeStyle = accent
				ctx.lineWidth = 13
				const x0 = -H + 78
				const x1 = x0 + (xa - 52 - x0) * params.arrow
				// A normal arrow: the shaft stops inside a filled triangular head that points along the bar.
				const yA = -T - 60
				const headLen = 54
				const shaftEnd = Math.max(x0, x1 - headLen * 0.65)
				ctx.beginPath()
				for (let i = 0; i <= 12; i++) ctx.lineTo(...at(x0 + ((shaftEnd - x0) * i) / 12, yA))
				ctx.stroke()
				ctx.fillStyle = accent
				ctx.lineWidth = 4
				ctx.beginPath()
				ctx.moveTo(...at(x1, yA))
				ctx.lineTo(...at(x1 - headLen, yA - 30))
				ctx.lineTo(...at(x1 - headLen, yA + 30))
				ctx.closePath()
				ctx.fill()
				ctx.stroke()
				ctx.restore()
			}
		}

		function snap(to) {
			cancelAnimationFrame(raf)
			raf = 0
			tweens = []
			lift = null
			step = to
			params = { ...scene.beats[to] }
			state = { ...equilibrium(id, to) }
			draw()
		}

		function frame(now) {
			if (!last) last = now
			acc += Math.min(0.1, (now - last) / 1000)
			last = now
			const t = now - started
			for (const tw of tweens) {
				const x = Math.min(1, Math.max(0, (t - tw.delay) / tw.dur))
				params[tw.key] = tw.from + (tw.to - tw.from) * ease(x)
			}
			while (acc >= DT) {
				stepSim(state, params)
				acc -= DT
			}
			// Filling a gap: ease the fallen bar up to horizontal first, hold it there while the work
			// extends to the right stilt, then let the physics take over (it is complete and level).
			if (lift) {
				const x = Math.min(1, t / lift.dur)
				state.theta = lift.from * (1 - ease(x))
				state.omega = 0
				if (x >= 1 && complete(params)) lift = null
			}
			draw()
			const eq = equilibrium(id, step)
			const tweening = tweens.some((tw) => t < tw.delay + tw.dur)
			const still = Math.abs(state.omega) < 0.003 && Math.abs(state.theta - eq.theta) < 0.002 && Math.abs(state.droop - eq.droop) < 0.4
			if ((!tweening && still) || t > 9000) return snap(step)
			raf = requestAnimationFrame(frame)
		}

		function go(to) {
			const current = deck.getCurrentSlide() === section
			if (!current || root.dataset.motion !== 'full') return snap(to)
			cancelAnimationFrame(raf)
			const target = scene.beats[to]
			const filling = target.b > params.b
			// Filling a gap: ease into horizontal first, then extend. Opening one: it falls as it goes.
			lift = filling && state.theta > 0.005 ? { from: state.theta, dur: LIFT_MS } : null
			const wait = lift ? LIFT_MS : 0
			tweens = Object.keys(target).map((key) => ({
				key,
				from: params[key],
				to: target[key],
				delay: wait + (key === 'arrow' ? 700 : key === 'ghost' && filling ? 500 : 0),
				dur: key === 'a' || key === 'b' ? 1100 : 700,
			}))
			step = to
			started = performance.now()
			last = 0
			acc = 0
			raf = requestAnimationFrame(frame)
		}

		section.addEventListener('stepchange', (event) => go(event.detail.step))
		new MutationObserver(draw).observe(root, { attributes: true, attributeFilter: ['data-theme'] })
		return {
			resize,
			draw,
			snapToCurrent: () => snap(Number(section.dataset.step ?? 0)),
			get step() { return step },
			get state() { return { ...state } },
			get settled() { return !raf },
			equilibrium: (k) => ({ ...equilibrium(id, k) }),
			// Geometry for tests (slide px): corners of the done work, the stilt pads, the canvas size.
			geometry() {
				const xb = -H + params.b * 2 * H
				return {
					corners: [at(-H, -T), at(xb, -T), at(xb, 0), at(-H, 0)],
					pads: [[PX - H - PAD_OUT, PX - H - PAD_OUT + PAD_W], [PX + H + PAD_OUT - PAD_W, PX + H + PAD_OUT]],
					complete: complete(params),
					thetaDeg: (state.theta * 180) / Math.PI,
					droop: state.droop,
					size: [W, HGT],
				}
			},
		}
	}

	const planks = {}
	for (const id of Object.keys(scenes)) {
		const section = document.getElementById(id)
		if (section?.querySelector('canvas.plank')) planks[id] = makePlank(section)
	}
	window.__plank = planks
	const resizeAll = () => Object.values(planks).forEach((p) => p.resize())
	const onReady = () => {
		Object.values(planks).forEach((p) => p.snapToCurrent())
		resizeAll()
	}
	if (deck.isReady()) onReady()
	else deck.on('ready', onReady)
	deck.on('resize', resizeAll)
	deck.on('slidechanged', (event) => {
		for (const [id, p] of Object.entries(planks)) {
			if (event.currentSlide?.id === id || event.previousSlide?.id === id) p.snapToCurrent()
		}
		resizeAll()
	})
	document.fonts?.ready.then(resizeAll)
})()