Skip to content
← Public packages

@kentcdodds/cloudflare-connect-pse-hyperframes

I Built Your Personal Software Ecosystem: Kent C. Dodds's Cloudflare Connect 2026 talk deck, built with HyperFrames. How Kody gives every user one of everything on Cloudflare.

FollowForkOpen app

Files

runtime.js

364 lines · 12.7 KB · JavaScript
// Runs inside the HyperFrames composition. The slideshow player seeks
// window.__timelines.deck to a scene's hold-point; this script turns that playhead into
// "which scene is up, and which fragment step it is on", then plays that scene's
// entrance, its fragment reveals and its looping flow on GSAP's own ticker.
;(function () {
	'use strict'
	var params = new URLSearchParams(location.search)
	// ?still: every flow shows one finished frame (screenshots, print).
	var STILL = params.has('still')
	// Reduced motion softens: fades in place instead of travel, slower loops. It never stops motion.
	var SOFT = params.has('soft') || window.matchMedia('(prefers-reduced-motion: reduce)').matches
	document.documentElement.toggleAttribute('data-soft', SOFT)

	var island = JSON.parse(document.querySelector('script[type="application/hyperframes-slideshow+json"]').textContent)
	var fragmentsById = {}
	island.slides.forEach(function (s) {
		fragmentsById[s.sceneId] = s.fragments || []
	})

	var scenes = Array.prototype.map.call(document.querySelectorAll('.scene-frame'), function (el) {
		var start = Number(el.getAttribute('data-start'))
		var id = el.getAttribute('data-composition-id')
		return { id: id, el: el, start: start, end: start + Number(el.getAttribute('data-duration')), fragments: fragmentsById[id] || [], initialHTML: el.innerHTML }
	})
	var total = scenes[scenes.length - 1].end

	function resetScene(scene) {
		gsap.killTweensOf(scene.el.querySelectorAll('*'))
		scene.el.innerHTML = scene.initialHTML
	}

	var EASE = 'power3.out'
	var LIFT = SOFT ? 0 : 28

	function sceneAt(t) {
		for (var i = 0; i < scenes.length; i++) if (t >= scenes[i].start && t < scenes[i].end) return scenes[i]
		return scenes[scenes.length - 1]
	}
	function stepAt(scene, t) {
		var n = 0
		scene.fragments.forEach(function (f) {
			if (t + 1e-4 >= f) n++
		})
		return Math.max(0, n - 1)
	}

	// ---------- Flow engine ----------
	// A stage holds absolutely positioned .node[data-node] elements (left/top are their
	// centers) and an <svg class="wires"> of <path class="wire" data-from data-to>. Wires are
	// routed edge to edge from the nodes' layout boxes, so transforms never skew them.

	function box(node) {
		var w = node.offsetWidth
		var h = node.offsetHeight
		var cx = node.offsetLeft
		var cy = node.offsetTop
		return { cx: cx, cy: cy, l: cx - w / 2, r: cx + w / 2, t: cy - h / 2, b: cy + h / 2 }
	}

	function route(a, b, kind, lane, out) {
		lane = lane || 0
		var pts
		if (kind === 'v') {
			var x = (Math.max(a.l, b.l) + Math.min(a.r, b.r)) / 2 + lane
			pts = b.cy > a.cy ? [[x, a.b], [x, b.t]] : [[x, a.t], [x, b.b]]
		} else if (kind === 'vh') {
			var sx = a.cx + lane
			var ty = b.cy
			pts = [[sx, b.cy > a.cy ? a.b : a.t], [sx, ty], [b.cx > sx ? b.l : b.r, ty]]
		} else if (kind === 'hv') {
			var sy = a.cy + lane
			var tx = b.cx + lane
			pts = [[b.cx > a.cx ? a.r : a.l, sy], [tx, sy], [tx, b.cy > sy ? b.t : b.b]]
		} else if (kind === 'hvh') {
			var x1 = b.cx > a.cx ? a.r : a.l
			var x2 = b.cx > a.cx ? b.l : b.r
			var mx = (x1 + x2) / 2
			pts = [[x1, a.cy], [mx, a.cy], [mx, b.cy + lane], [x2, b.cy + lane]]
		} else if (kind === 'loop-left') {
			var lx = Math.min(a.l, b.l) - (out || 90)
			pts = [[a.l, a.cy + lane], [lx, a.cy + lane], [lx, b.cy + lane], [b.l, b.cy + lane]]
		} else {
			var y = (Math.max(a.t, b.t) + Math.min(a.b, b.b)) / 2 + lane
			pts = b.cx > a.cx ? [[a.r, y], [b.l, y]] : [[a.l, y], [b.r, y]]
		}
		return pts
	}

	function pathD(pts) {
		var radius = 26
		var d = 'M' + pts[0][0] + ' ' + pts[0][1]
		for (var i = 1; i < pts.length; i++) {
			var p = pts[i]
			if (i < pts.length - 1) {
				var prev = pts[i - 1]
				var next = pts[i + 1]
				var r1 = Math.min(radius, Math.hypot(p[0] - prev[0], p[1] - prev[1]) / 2)
				var r2 = Math.min(radius, Math.hypot(next[0] - p[0], next[1] - p[1]) / 2)
				var ax = p[0] - Math.sign(p[0] - prev[0]) * r1
				var ay = p[1] - Math.sign(p[1] - prev[1]) * r1
				var bx = p[0] + Math.sign(next[0] - p[0]) * r2
				var by = p[1] + Math.sign(next[1] - p[1]) * r2
				d += ' L' + ax + ' ' + ay + ' Q' + p[0] + ' ' + p[1] + ' ' + bx + ' ' + by
			} else {
				d += ' L' + p[0] + ' ' + p[1]
			}
		}
		return d
	}

	function Flow(stage) {
		this.stage = stage
		this.packets = []
		this.tl = gsap.timeline({ paused: true, repeat: 200, repeatDelay: SOFT ? 2 : 1.4 })
		var nodes = stage.querySelectorAll('.node')
		this.tl.set(nodes, { '--glow': 0 }, 0)
		this.layout()
	}
	Flow.prototype.layout = function () {
		var stage = this.stage
		stage.querySelectorAll('.wire[data-from]').forEach(function (w) {
			var a = stage.querySelector('[data-node="' + w.getAttribute('data-from') + '"]')
			var b = stage.querySelector('[data-node="' + w.getAttribute('data-to') + '"]')
			if (!a || !b) return
			var pts = route(box(a), box(b), w.getAttribute('data-route'), Number(w.getAttribute('data-lane') || 0), Number(w.getAttribute('data-out') || 0))
			w.setAttribute('d', pathD(pts))
		})
	}
	Flow.prototype.node = function (name) {
		return this.stage.querySelector('[data-node="' + name + '"]')
	}
	Flow.prototype.wire = function (name) {
		return this.stage.querySelector('.wire[data-name="' + name + '"]')
	}
	// Light a node at `at`, and let it settle back `hold` seconds later.
	Flow.prototype.lit = function (name, at, hold) {
		var n = this.node(name)
		this.tl.to(n, { '--glow': 1, duration: SOFT ? 0.5 : 0.3, ease: 'power2.out' }, at)
		if (!SOFT) this.tl.fromTo(n, { scale: 1 }, { scale: 1.04, duration: 0.18, yoyo: true, repeat: 1, ease: 'power1.inOut' }, at)
		if (hold != null) this.tl.to(n, { '--glow': 0, duration: 0.6, ease: 'power2.inOut' }, at + hold)
		return this
	}
	Flow.prototype.dim = function (name, at) {
		this.tl.to(this.node(name), { '--glow': 0, duration: 0.5 }, at)
		return this
	}
	// A labelled packet travels the wire. Returns the arrival time.
	Flow.prototype.send = function (wireName, label, at, opts) {
		opts = opts || {}
		var w = this.wire(wireName)
		var dur = opts.dur || 1
		var pk = document.createElement('span')
		pk.className = 'packet' + (opts.cls ? ' ' + opts.cls : '')
		pk.innerHTML = label
		w.ownerSVGElement.parentNode.appendChild(pk)
		this.packets.push(pk)
		var len = w.getTotalLength()
		var reverse = !!opts.reverse
		var hot = opts.cls === 'err' ? '#ff7a6b' : opts.cls === 'back' ? '#7cc7ff' : opts.cls === 'key' ? '#f2c77a' : opts.cls === 'cf' ? '#ff8a45' : '#4fd27a'
		var place = function (v) {
			var p = w.getPointAtLength((reverse ? 1 - v : v) * len)
			gsap.set(pk, { x: p.x, y: p.y })
		}
		this.tl.set(pk, { opacity: 0 }, 0)
		this.tl.to(w, { stroke: hot, duration: 0.25 }, at)
		this.tl.to(w, { stroke: getComputedStyle(w).stroke, duration: 0.6 }, at + dur + 0.3)
		if (SOFT) {
			this.tl.call(place, [0.5], at)
			this.tl.fromTo(pk, { opacity: 0 }, { opacity: 1, duration: 0.5 }, at)
			this.tl.to(pk, { opacity: 0, duration: 0.5 }, at + dur + 0.2)
		} else {
			var prog = { v: 0 }
			this.tl.fromTo(prog, { v: 0 }, { v: 1, duration: dur, ease: 'power1.inOut', onUpdate: function () { place(prog.v) } }, at)
			this.tl.fromTo(pk, { opacity: 0, scale: 0.7 }, { opacity: 1, scale: 1, duration: 0.2 }, at)
			this.tl.to(pk, { opacity: 0, scale: 0.8, duration: 0.25 }, at + dur)
		}
		return at + dur
	}
	// Swap an element's text at a time in the loop (reset at the loop start).
	Flow.prototype.text = function (el, value, at, initial) {
		if (typeof el === 'string') el = this.stage.querySelector(el)
		if (initial != null) this.tl.call(function () { el.innerHTML = initial }, null, 0.001)
		this.tl.call(function () { el.innerHTML = value }, null, at)
		return this
	}
	Flow.prototype.attr = function (el, name, value, at, initial) {
		if (typeof el === 'string') el = this.stage.querySelector(el)
		if (initial != null) this.tl.call(function () { el.setAttribute(name, initial) }, null, 0.001)
		this.tl.call(function () { el.setAttribute(name, value) }, null, at)
		return this
	}
	Flow.prototype.still = function (at) {
		this.tl.addLabel('still', at)
		return this
	}
	Flow.prototype.start = function (delay) {
		if (SOFT) this.tl.timeScale(0.8)
		if (STILL) {
			this.tl.seek(this.tl.labels.still != null ? this.tl.labels.still : this.tl.duration() * 0.6, false)
			return
		}
		this.tl.delay(delay || 0).play(0)
	}
	Flow.prototype.kill = function () {
		this.tl.kill()
		this.packets.forEach(function (p) {
			p.remove()
		})
		this.stage.querySelectorAll('.wire').forEach(function (w) {
			w.style.stroke = ''
		})
		this.stage.querySelectorAll('.node').forEach(function (n) {
			gsap.set(n, { '--glow': 0, scale: 1 })
		})
	}

	// ---------- Per-slide flows and step hooks ----------
	var FLOWS = window.__deckFlows || {}
	var STEPS = window.__deckSteps || {}

	// ---------- Scene controller ----------
	var active = null
	var activeStep = -1
	var flow = null

	function stepEls(scene) {
		return scene.el.querySelectorAll('[data-step], [data-until]')
	}
	function shownAt(el, step) {
		var from = el.hasAttribute('data-step') ? Number(el.getAttribute('data-step')) : 0
		var until = el.hasAttribute('data-until') ? Number(el.getAttribute('data-until')) : Infinity
		return step >= from && step <= until
	}

	function enter(scene, prev) {
		if (prev) {
			gsap.killTweensOf(prev.el)
			gsap.to(prev.el, {
				opacity: 0,
				duration: 0.25,
				onComplete: function () {
					prev.el.style.visibility = 'hidden'
				},
			})
			prev.el.style.pointerEvents = 'none'
		}
		gsap.killTweensOf(scene.el)
		scene.el.style.visibility = 'visible'
		scene.el.style.pointerEvents = 'auto'
		gsap.fromTo(scene.el, { opacity: prev ? 0 : 1 }, { opacity: 1, duration: SOFT ? 0.6 : 0.35, ease: 'power1.out' })
	}

	function applyStep(scene, step, entering) {
		// The last visit leaves inline positions and text in place. Restore the
		// markup so entrance, fragments, and flows start from the first frame.
		if (entering) resetScene(scene)
		var revealed = []
		stepEls(scene).forEach(function (el) {
			var show = shownAt(el, step)
			var wasShown = el.getAttribute('data-shown') === '1'
			el.setAttribute('data-shown', show ? '1' : '0')
			if (show && (entering || !wasShown)) {
				gsap.killTweensOf(el)
				gsap.set(el, { opacity: 0, y: LIFT * 0.7 })
				revealed.push(el)
			} else if (!show) {
				gsap.killTweensOf(el)
				if (entering || !wasShown) gsap.set(el, { opacity: 0, y: 0 })
				else gsap.to(el, { opacity: 0, duration: 0.25 })
			}
			el.style.pointerEvents = show ? '' : 'none'
		})
		if (entering) {
			var enterEls = scene.el.querySelectorAll('[data-enter]')
			gsap.fromTo(enterEls, { opacity: 0, y: LIFT }, { opacity: 1, y: 0, duration: SOFT ? 0.8 : 0.6, stagger: 0.08, ease: EASE, overwrite: 'auto' })
		}
		// One tween per element, so a quick step change can cancel each reveal on its own.
		revealed.forEach(function (el, i) {
			gsap.to(el, { opacity: 1, y: 0, duration: SOFT ? 0.8 : 0.5, ease: EASE, delay: (entering ? 0.25 : 0) + i * 0.1 })
		})
		var hook = STEPS[scene.id]
		if (hook) hook(scene.el, step, { entering: entering, soft: SOFT, still: STILL })
		if (flow) flow.kill()
		flow = null
		var build = FLOWS[scene.id]
		if (build) {
			var stage = scene.el.querySelector('.stage')
			flow = new Flow(stage)
			build(flow, step, { soft: SOFT })
			flow.start(entering ? 0.5 : 0.2)
		}
	}

	function update(t) {
		var scene = sceneAt(t)
		var step = stepAt(scene, t)
		if (scene !== active) {
			var prev = active
			if (flow) flow.kill()
			flow = null
			active = scene
			activeStep = -1
			enter(scene, prev)
		}
		if (step !== activeStep) {
			var entering = activeStep === -1
			activeStep = step
			applyStep(scene, step, entering)
		}
	}

	var root = gsap.timeline({ paused: true })
	root.to({}, { duration: total })
	root.eventCallback('onUpdate', function () {
		update(root.time())
	})
	window.__timelines = window.__timelines || {}
	window.__timelines.deck = root
	window.__deck = {
		get scene() {
			return active && active.id
		},
		get step() {
			return activeStep
		},
		get soft() {
			return SOFT
		},
		get flowActive() {
			return !!(flow && flow.tl.isActive())
		},
		scenes: scenes.map(function (s) {
			return { id: s.id, start: s.start, end: s.end, fragments: s.fragments }
		}),
	}

	function boot() {
		update(root.time())
	}
	if (document.fonts && document.fonts.ready) document.fonts.ready.then(boot)
	else boot()

	// The player learns the scene list from the runtime's "timeline" message; without the
	// engine, the composition posts it itself.
	function postTimeline() {
		if (window.parent === window) return
		window.parent.postMessage(
			{
				source: 'hf-preview',
				type: 'timeline',
				durationInFrames: Math.round(total * 30),
				durationSeconds: total,
				compositionWidth: 1920,
				compositionHeight: 1080,
				scenes: scenes.map(function (s) {
					return { id: s.id, start: s.start, duration: s.end - s.start }
				}),
			},
			'*',
		)
	}
	if (document.readyState === 'complete') postTimeline()
	else window.addEventListener('load', postTimeline)
})()