1// Drawing helpers shared by the diagrams, and the Start / Stop animation loop.
3export function color(name) {
4 return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
5}
7// Slides are laid out at 1920 x 1080 and the whole deck is scaled to fit the
8// window (deck.js sets the scale). Diagrams are drawn in units of DRAW_SCALE
9// slide pixels, so their geometry and fonts read well on a full-size slide.
10export const DRAW_SCALE = 1.5;
11let deckScale = 1;
13export function setDeckScale(scale) {
14 deckScale = scale;
15}
17// Size a canvas's backing store to its on-screen pixels (slide size x deck
18// scale x device pixel ratio) and return a context and size in drawing units.
19export function prepare(canvas) {
20 const ctx = canvas.getContext('2d');
21 const k = deckScale * (window.devicePixelRatio || 1);
22 const bw = Math.max(1, Math.round(canvas.clientWidth * k));
23 const bh = Math.max(1, Math.round(canvas.clientHeight * k));
24 if (canvas.width !== bw || canvas.height !== bh) {
25 canvas.width = bw;
26 canvas.height = bh;
27 }
28 ctx.setTransform(k * DRAW_SCALE, 0, 0, k * DRAW_SCALE, 0, 0);
29 const w = canvas.clientWidth / DRAW_SCALE;
30 const h = canvas.clientHeight / DRAW_SCALE;
31 ctx.clearRect(0, 0, w, h);
32 return { ctx, w, h };
33}
35// A horizontal arrow at height y from x = from to x = to, labeled above or below.
36export function arrow(ctx, from, to, y, col, label, side) {
37 const dir = Math.sign(to - from);
38 ctx.strokeStyle = col;
39 ctx.fillStyle = col;
40 ctx.lineWidth = 1.5;
41 ctx.beginPath();
42 ctx.moveTo(from, y);
43 ctx.lineTo(to, y);
44 ctx.stroke();
45 if (Math.abs(to - from) > 8) {
46 ctx.beginPath();
47 ctx.moveTo(to, y);
48 ctx.lineTo(to - dir * 8, y - 4);
49 ctx.lineTo(to - dir * 8, y + 4);
50 ctx.closePath();
51 ctx.fill();
52 }
53 ctx.textBaseline = side === 'above' ? 'bottom' : 'top';
54 ctx.fillText(label, (from + to) / 2, side === 'above' ? y - 4 : y + 4);
55}
57// Animation driven by a Start / Stop / Continue button. step(dt) advances the
58// simulation; speed is a number, or a function read every frame so it can
59// change while running.
60export function createLoop({ button, step, draw, speed = 1 }) {
61 let running = false;
62 let lastTime = null;
63 let rafId = null;
65 function frame(t) {
66 if (!running) return;
67 if (lastTime !== null) {
68 const dt = (typeof speed === 'function' ? speed() : speed) * Math.min((t - lastTime) / 1000, 0.05);
69 const n = 4;
70 for (let i = 0; i < n; i++) step(dt / n);
71 }
72 lastTime = t;
73 draw();
74 rafId = requestAnimationFrame(frame);
75 }
77 function setRunning(value) {
78 running = value;
79 cancelAnimationFrame(rafId);
80 if (running) {
81 lastTime = null;
82 rafId = requestAnimationFrame(frame);
83 }
84 if (running || button.textContent !== 'Start') {
85 button.textContent = running ? 'Stop' : 'Continue';
86 }
87 }
89 button.addEventListener('click', () => setRunning(!running));
91 return {
92 isRunning: () => running,
93 start: () => running || setRunning(true),
94 pause: () => running && setRunning(false),
95 reset: () => {
96 setRunning(false);
97 button.textContent = 'Start';
98 draw();
99 },
100 };
101}