@biunow/agent-biutalk 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/server.js +355 -0
- package/engine/AGENTS.md +1414 -0
- package/engine/CLAUDE.md +1 -0
- package/engine/LICENSE +21 -0
- package/engine/README.md +216 -0
- package/engine/bin/explainroo.js +19 -0
- package/engine/dev/audio-lab.mjs +268 -0
- package/engine/docs/media/contact-sheet.jpg +0 -0
- package/engine/docs/media/dns-poster.jpg +0 -0
- package/engine/docs/media/intro-poster.jpg +0 -0
- package/engine/docs/media/logo-dark.png +0 -0
- package/engine/docs/media/logo-light.png +0 -0
- package/engine/docs/media/looks-dark.webp +0 -0
- package/engine/docs/media/looks-light.webp +0 -0
- package/engine/docs/media/product-demo.webp +0 -0
- package/engine/docs/media/rebase-poster.jpg +0 -0
- package/engine/docs/media/sizes-dark.webp +0 -0
- package/engine/docs/media/sizes-light.webp +0 -0
- package/engine/engine/audio/dsp.js +69 -0
- package/engine/engine/audio/instruments.js +147 -0
- package/engine/engine/audio/music.js +576 -0
- package/engine/engine/audio/pcm.js +93 -0
- package/engine/engine/audio/rng.js +40 -0
- package/engine/engine/audio/sfx.js +287 -0
- package/engine/engine/audio/soundtrack.js +201 -0
- package/engine/engine/audio/synth.js +721 -0
- package/engine/engine/audio/theory.js +105 -0
- package/engine/engine/audio/wav.js +47 -0
- package/engine/engine/captions.js +131 -0
- package/engine/engine/clips.js +107 -0
- package/engine/engine/code.js +179 -0
- package/engine/engine/director.js +475 -0
- package/engine/engine/frame.html +135 -0
- package/engine/engine/icons/LICENSE-lucide.txt +43 -0
- package/engine/engine/icons/lucide-tags.json +1 -0
- package/engine/engine/icons/lucide.json +1 -0
- package/engine/engine/layout.js +69 -0
- package/engine/engine/pathdata.js +234 -0
- package/engine/engine/pen.js +240 -0
- package/engine/engine/player.html +67 -0
- package/engine/engine/player.js +219 -0
- package/engine/engine/readability.js +9 -0
- package/engine/engine/runtime.js +690 -0
- package/engine/engine/stage.js +2247 -0
- package/engine/engine/studio.html +216 -0
- package/engine/engine/text.js +81 -0
- package/engine/engine/themes.js +360 -0
- package/engine/engine/transitions.js +161 -0
- package/engine/engine/ui.js +1042 -0
- package/engine/engine/upload.js +32 -0
- package/engine/engine/util.js +204 -0
- package/engine/examples/callbacks-to-async-await/code-versions.js +34 -0
- package/engine/examples/callbacks-to-async-await/scenes.js +106 -0
- package/engine/examples/callbacks-to-async-await/script.md +22 -0
- package/engine/examples/callbacks-to-async-await/video.json +16 -0
- package/engine/examples/circle-area/scenes.js +228 -0
- package/engine/examples/circle-area/script.md +37 -0
- package/engine/examples/circle-area/video.json +14 -0
- package/engine/examples/explainroo-intro/scenes.js +133 -0
- package/engine/examples/explainroo-intro/script.md +27 -0
- package/engine/examples/explainroo-intro/video.json +8 -0
- package/engine/examples/how-dns-works/scenes.js +130 -0
- package/engine/examples/how-dns-works/script.md +28 -0
- package/engine/examples/how-dns-works/video.json +8 -0
- package/engine/examples/leap-years/scenes.js +156 -0
- package/engine/examples/leap-years/script.md +25 -0
- package/engine/examples/leap-years/video.json +16 -0
- package/engine/examples/leap-years-ja/scenes.js +194 -0
- package/engine/examples/leap-years-ja/script.md +32 -0
- package/engine/examples/leap-years-ja/video.json +17 -0
- package/engine/examples/merge-vs-rebase/scenes.js +129 -0
- package/engine/examples/merge-vs-rebase/script.md +22 -0
- package/engine/examples/merge-vs-rebase/video.json +8 -0
- package/engine/examples/password-storage-zh/scenes.js +185 -0
- package/engine/examples/password-storage-zh/script.md +25 -0
- package/engine/examples/password-storage-zh/video.json +19 -0
- package/engine/examples/sqlite-demo/README.md +19 -0
- package/engine/examples/sqlite-demo/scenes.js +423 -0
- package/engine/examples/sqlite-demo/script.md +35 -0
- package/engine/examples/sqlite-demo/video.json +17 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-400.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-500.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-600.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-700.woff2 +0 -0
- package/engine/examples/unspar-demo/assets/fonts/Figtree-OFL.txt +93 -0
- package/engine/examples/unspar-demo/assets/unspar-logo.svg +23 -0
- package/engine/examples/unspar-demo/scenes.js +367 -0
- package/engine/examples/unspar-demo/script.md +25 -0
- package/engine/examples/unspar-demo/video.json +21 -0
- package/engine/examples/why-is-the-sky-blue-es/scenes.js +160 -0
- package/engine/examples/why-is-the-sky-blue-es/script.md +22 -0
- package/engine/examples/why-is-the-sky-blue-es/video.json +15 -0
- package/engine/fonts/ArchitectsDaughter-400-latin-ext.woff2 +0 -0
- package/engine/fonts/ArchitectsDaughter-400-latin.woff2 +0 -0
- package/engine/fonts/CabinSketch-400-latin.woff2 +0 -0
- package/engine/fonts/CabinSketch-700-latin.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-italic-latin-ext.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-italic-latin.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-normal-latin-ext.woff2 +0 -0
- package/engine/fonts/InstrumentSerif-400-normal-latin.woff2 +0 -0
- package/engine/fonts/Inter-400-latin-ext.woff2 +0 -0
- package/engine/fonts/Inter-400-latin.woff2 +0 -0
- package/engine/fonts/JetBrainsMono-400-latin-ext.woff2 +0 -0
- package/engine/fonts/JetBrainsMono-400-latin.woff2 +0 -0
- package/engine/fonts/Kalam-400-latin-ext.woff2 +0 -0
- package/engine/fonts/Kalam-400-latin.woff2 +0 -0
- package/engine/fonts/Kalam-700-latin-ext.woff2 +0 -0
- package/engine/fonts/Kalam-700-latin.woff2 +0 -0
- package/engine/fonts/NotoSansDevanagari-400.ttf +0 -0
- package/engine/fonts/NotoSansDevanagari-700.ttf +0 -0
- package/engine/fonts/PatrickHand-400-latin-ext.woff2 +0 -0
- package/engine/fonts/PatrickHand-400-latin.woff2 +0 -0
- package/engine/fonts/PlusJakartaSans-600-latin-ext.woff2 +0 -0
- package/engine/fonts/PlusJakartaSans-600-latin.woff2 +0 -0
- package/engine/fonts/SpaceGrotesk-500-latin-ext.woff2 +0 -0
- package/engine/fonts/SpaceGrotesk-500-latin.woff2 +0 -0
- package/engine/fonts/fonts.json +216 -0
- package/engine/fonts/licenses/ArchitectsDaughter-OFL.txt +93 -0
- package/engine/fonts/licenses/CabinSketch-OFL.txt +93 -0
- package/engine/fonts/licenses/InstrumentSerif-OFL.txt +93 -0
- package/engine/fonts/licenses/Inter-OFL.txt +93 -0
- package/engine/fonts/licenses/JetBrainsMono-OFL.txt +93 -0
- package/engine/fonts/licenses/Kalam-OFL.txt +93 -0
- package/engine/fonts/licenses/NotoSansDevanagari-OFL.txt +118 -0
- package/engine/fonts/licenses/PatrickHand-OFL.txt +93 -0
- package/engine/fonts/licenses/PlusJakartaSans-OFL.txt +93 -0
- package/engine/fonts/licenses/SpaceGrotesk-OFL.txt +93 -0
- package/engine/package-lock.json +1201 -0
- package/engine/package.json +67 -0
- package/engine/scripts/build-icons.mjs +74 -0
- package/engine/scripts/fetch-fonts.mjs +68 -0
- package/engine/skills/explainroo/SKILL.md +51 -0
- package/engine/src/align.js +357 -0
- package/engine/src/browser.js +121 -0
- package/engine/src/capture.js +315 -0
- package/engine/src/chapters.js +32 -0
- package/engine/src/cli.js +421 -0
- package/engine/src/clips.js +19 -0
- package/engine/src/elevenlabs.js +90 -0
- package/engine/src/env.js +16 -0
- package/engine/src/export.js +187 -0
- package/engine/src/ffmpeg.js +103 -0
- package/engine/src/fonts.js +86 -0
- package/engine/src/g2p.py +76 -0
- package/engine/src/images.js +151 -0
- package/engine/src/licenses/misaki-Apache-2.0.txt +201 -0
- package/engine/src/math.js +83 -0
- package/engine/src/models.js +161 -0
- package/engine/src/phonemes.js +162 -0
- package/engine/src/pipeline.js +63 -0
- package/engine/src/project.js +331 -0
- package/engine/src/qa.js +186 -0
- package/engine/src/recordings.js +89 -0
- package/engine/src/render.js +156 -0
- package/engine/src/script.js +266 -0
- package/engine/src/server.js +288 -0
- package/engine/src/subtitles.js +78 -0
- package/engine/src/timeline.js +55 -0
- package/engine/src/voice.js +240 -0
- package/engine/src/wav.js +98 -0
- package/engine/templates/starter/scenes.js +31 -0
- package/engine/templates/starter/script.md +14 -0
- package/engine/test/align.test.js +108 -0
- package/engine/test/brand.test.js +41 -0
- package/engine/test/browser.test.js +89 -0
- package/engine/test/camera-browser.test.js +225 -0
- package/engine/test/caption-phrases.test.js +35 -0
- package/engine/test/capture-browser.test.js +90 -0
- package/engine/test/capture-fixture.js +46 -0
- package/engine/test/capture.test.js +62 -0
- package/engine/test/chapters.test.js +41 -0
- package/engine/test/cjk-browser.test.js +101 -0
- package/engine/test/cjk.test.js +232 -0
- package/engine/test/cli-exit.test.js +25 -0
- package/engine/test/cli.test.js +116 -0
- package/engine/test/clips-browser.test.js +125 -0
- package/engine/test/clips.test.js +67 -0
- package/engine/test/code-morph-browser.test.js +99 -0
- package/engine/test/code-morph.test.js +110 -0
- package/engine/test/contrast-browser.test.js +63 -0
- package/engine/test/contrast.test.js +80 -0
- package/engine/test/director.test.js +316 -0
- package/engine/test/engine-pure.test.js +29 -0
- package/engine/test/export-browser.test.js +224 -0
- package/engine/test/export.test.js +96 -0
- package/engine/test/fixtures/capture.html +55 -0
- package/engine/test/formats-pace.test.js +104 -0
- package/engine/test/languages-browser.test.js +78 -0
- package/engine/test/languages.test.js +140 -0
- package/engine/test/looks-browser.test.js +109 -0
- package/engine/test/looks-preview.test.js +57 -0
- package/engine/test/looks.test.js +130 -0
- package/engine/test/math-browser.test.js +114 -0
- package/engine/test/math.test.js +54 -0
- package/engine/test/multi-size-browser.test.js +133 -0
- package/engine/test/multi-size.test.js +116 -0
- package/engine/test/phonemes.test.js +92 -0
- package/engine/test/readability-browser.test.js +36 -0
- package/engine/test/readability.test.js +20 -0
- package/engine/test/render-options.test.js +52 -0
- package/engine/test/script.test.js +67 -0
- package/engine/test/server.test.js +256 -0
- package/engine/test/stills-browser.test.js +70 -0
- package/engine/test/subtitles.test.js +83 -0
- package/engine/test/timeline.test.js +31 -0
- package/engine/test/transparent-browser.test.js +123 -0
- package/engine/test/ui-kit.test.js +96 -0
- package/engine/test/upload-browser.test.js +55 -0
- package/engine/test/voice-providers.test.js +338 -0
- package/lib/host.mjs +480 -0
- package/manifest.json +54 -0
- package/package.json +8 -4
- package/skills/biutalk/SKILL.md +21 -0
- package/studio/app.js +209 -0
- package/studio/assert-panel-permit.mjs +67 -0
- package/studio/composer.css +354 -0
- package/studio/composer.js +169 -0
- package/studio/index.html +82 -0
- package/studio/looks/blueprint.svg +21 -0
- package/studio/looks/chalk.svg +15 -0
- package/studio/looks/clean.svg +20 -0
- package/studio/looks/editorial.svg +18 -0
- package/studio/looks/midnight.svg +17 -0
- package/studio/looks/paper.svg +11 -0
- package/studio/select-menu.js +268 -0
- package/studio/styles.css +4 -0
- package/README.md +0 -3
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Uploads from engine pages (render, still, sheet) to the local server.
|
|
2
|
+
//
|
|
3
|
+
// Two things keep large uploads such as a long soundtrack WAV working:
|
|
4
|
+
//
|
|
5
|
+
// - The body is always sent as a Blob. When a page sends an ArrayBuffer, Chrome
|
|
6
|
+
// copies the whole body into the DevTools Network.requestWillBeSent event,
|
|
7
|
+
// and Playwright reads that event as one string (about four characters per
|
|
8
|
+
// byte). A 16-bit stereo 48 kHz soundtrack is about 192 KB per second, so
|
|
9
|
+
// around ten minutes of audio passed Node's maximum string length and the
|
|
10
|
+
// render stopped with ERR_STRING_TOO_LONG before the first frame. Blob bodies
|
|
11
|
+
// are not copied into the event.
|
|
12
|
+
// - The body goes up in pieces of at most UPLOAD_CHUNK_BYTES, each with its
|
|
13
|
+
// byte offset, so no single request or server-side buffer grows with the
|
|
14
|
+
// length of the video. The server assembles the pieces of one upload (its
|
|
15
|
+
// random id) in order and moves the file into place when the last piece
|
|
16
|
+
// arrives (see /__upload in server.js).
|
|
17
|
+
export const UPLOAD_CHUNK_BYTES = 8 * 1024 * 1024;
|
|
18
|
+
|
|
19
|
+
export async function upload(path, body, { chunkBytes = UPLOAD_CHUNK_BYTES } = {}) {
|
|
20
|
+
if (!(chunkBytes > 0)) throw new Error(`upload ${path}: chunkBytes must be positive`);
|
|
21
|
+
const blob = body instanceof Blob ? body : new Blob([body]);
|
|
22
|
+
const id = crypto.randomUUID().replaceAll('-', '');
|
|
23
|
+
const base = `/__upload?path=${encodeURIComponent(path)}&upload=${id}`;
|
|
24
|
+
let offset = 0;
|
|
25
|
+
do {
|
|
26
|
+
const part = blob.slice(offset, offset + chunkBytes);
|
|
27
|
+
const last = offset + part.size >= blob.size;
|
|
28
|
+
const res = await fetch(`${base}&offset=${offset}${last ? '&last=1' : ''}`, { method: 'PUT', body: part });
|
|
29
|
+
if (!res.ok) throw new Error(`upload ${path} failed at byte ${offset}: ${await res.text()}`);
|
|
30
|
+
offset += part.size;
|
|
31
|
+
} while (offset < blob.size);
|
|
32
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Small math, easing, randomness and color helpers shared by the engine.
|
|
2
|
+
|
|
3
|
+
export const clamp = (v, a = 0, b = 1) => (v < a ? a : v > b ? b : v);
|
|
4
|
+
export const lerp = (a, b, p) => a + (b - a) * p;
|
|
5
|
+
|
|
6
|
+
export const ease = {
|
|
7
|
+
linear: (p) => p,
|
|
8
|
+
in: (p) => p * p * p,
|
|
9
|
+
out: (p) => 1 - Math.pow(1 - p, 3),
|
|
10
|
+
inOut: (p) => (p < 0.5 ? 4 * p * p * p : 1 - Math.pow(-2 * p + 2, 3) / 2),
|
|
11
|
+
outBack: (p) => {
|
|
12
|
+
const c1 = 1.70158;
|
|
13
|
+
const c3 = c1 + 1;
|
|
14
|
+
return 1 + c3 * Math.pow(p - 1, 3) + c1 * Math.pow(p - 1, 2);
|
|
15
|
+
},
|
|
16
|
+
outElastic: (p) => (p === 0 || p === 1 ? p : Math.pow(2, -10 * p) * Math.sin((p * 10 - 0.75) * ((2 * Math.PI) / 3)) + 1),
|
|
17
|
+
outQuart: (p) => 1 - Math.pow(1 - p, 4),
|
|
18
|
+
inOutSine: (p) => -(Math.cos(Math.PI * p) - 1) / 2,
|
|
19
|
+
// A lightly damped spring: quick, about 4% past the end, then settled.
|
|
20
|
+
spring: (p) => (p >= 1 ? 1 : 1 - Math.exp(-7.2 * p) * Math.cos(7.2 * p)),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function easeFn(name) {
|
|
24
|
+
if (typeof name === 'function') return name;
|
|
25
|
+
const f = ease[name || 'inOut'];
|
|
26
|
+
if (!f) throw new Error(`unknown ease "${name}". Use one of: ${Object.keys(ease).join(', ')}`);
|
|
27
|
+
return f;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function hashStr(s) {
|
|
31
|
+
let h = 2166136261 >>> 0;
|
|
32
|
+
const str = String(s);
|
|
33
|
+
for (let i = 0; i < str.length; i++) {
|
|
34
|
+
h ^= str.charCodeAt(i);
|
|
35
|
+
h = Math.imul(h, 16777619);
|
|
36
|
+
}
|
|
37
|
+
return h >>> 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function mulberry32(seed) {
|
|
41
|
+
let a = seed >>> 0;
|
|
42
|
+
return function rand() {
|
|
43
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
44
|
+
let t = a;
|
|
45
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
46
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
47
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Deterministic value in [0, 1) for an index and seed.
|
|
52
|
+
export function rand01(i, seed = 0) {
|
|
53
|
+
return mulberry32(hashStr(`${seed}:${i}`))();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Smooth 1D value noise in [-1, 1].
|
|
57
|
+
export function noise1(x, seed = 0) {
|
|
58
|
+
const i = Math.floor(x);
|
|
59
|
+
const f = x - i;
|
|
60
|
+
const a = rand01(i, seed) * 2 - 1;
|
|
61
|
+
const b = rand01(i + 1, seed) * 2 - 1;
|
|
62
|
+
const u = f * f * (3 - 2 * f);
|
|
63
|
+
return a + (b - a) * u;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const HEX = /^#([0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
67
|
+
export function parseColor(c) {
|
|
68
|
+
if (typeof c !== 'string') return null;
|
|
69
|
+
const m = HEX.exec(c.trim());
|
|
70
|
+
if (m) {
|
|
71
|
+
let h = m[1];
|
|
72
|
+
if (h.length === 3) h = h.split('').map((x) => x + x).join('');
|
|
73
|
+
return { r: parseInt(h.slice(0, 2), 16), g: parseInt(h.slice(2, 4), 16), b: parseInt(h.slice(4, 6), 16), a: h.length === 8 ? parseInt(h.slice(6, 8), 16) / 255 : 1 };
|
|
74
|
+
}
|
|
75
|
+
const rgb = /^rgba?\(([^)]+)\)$/i.exec(c.trim());
|
|
76
|
+
if (rgb) {
|
|
77
|
+
const [r, g, b, a = 1] = rgb[1].split(/[\s,/]+/).filter(Boolean).map(Number);
|
|
78
|
+
return { r, g, b, a };
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function rgba({ r, g, b, a = 1 }) {
|
|
84
|
+
return `rgba(${Math.round(r)},${Math.round(g)},${Math.round(b)},${Math.round(a * 1000) / 1000})`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function mixColor(c1, c2, p) {
|
|
88
|
+
const a = parseColor(c1);
|
|
89
|
+
const b = parseColor(c2);
|
|
90
|
+
if (!a || !b) return p < 0.5 ? c1 : c2;
|
|
91
|
+
return rgba({ r: lerp(a.r, b.r, p), g: lerp(a.g, b.g, p), b: lerp(a.b, b.b, p), a: lerp(a.a, b.a, p) });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function withAlpha(c, alpha) {
|
|
95
|
+
const p = parseColor(c);
|
|
96
|
+
if (!p) return c;
|
|
97
|
+
return rgba({ ...p, a: p.a * alpha });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// A translucent color as it looks over an opaque base color.
|
|
101
|
+
export function opaque(c, base) {
|
|
102
|
+
const p = parseColor(c);
|
|
103
|
+
if (!p || p.a >= 1) return c;
|
|
104
|
+
return mixColor(base, rgba({ ...p, a: 1 }), p.a);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function luminance(c) {
|
|
108
|
+
const p = parseColor(c);
|
|
109
|
+
if (!p) return 0.5;
|
|
110
|
+
const f = (v) => {
|
|
111
|
+
v /= 255;
|
|
112
|
+
return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, 2.4);
|
|
113
|
+
};
|
|
114
|
+
return 0.2126 * f(p.r) + 0.7152 * f(p.g) + 0.0722 * f(p.b);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function contrast(c1, c2) {
|
|
118
|
+
const a = luminance(c1);
|
|
119
|
+
const b = luminance(c2);
|
|
120
|
+
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function hsl(c) {
|
|
124
|
+
const { r, g, b } = parseColor(c);
|
|
125
|
+
const [R, G, B] = [r, g, b].map(v => v / 255);
|
|
126
|
+
const max = Math.max(R, G, B), min = Math.min(R, G, B);
|
|
127
|
+
const d = max - min, l = (max + min) / 2;
|
|
128
|
+
const h = !d ? 0 : max === R ? ((G - B) / d + 6) % 6 : max === G ? (B - R) / d + 2 : (R - G) / d + 4;
|
|
129
|
+
return { h: h * 60, s: d ? d / (1 - Math.abs(2 * l - 1)) : 0, l };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function hslHex({ h, s, l }) {
|
|
133
|
+
const a = s * Math.min(l, 1 - l);
|
|
134
|
+
const channel = n => {
|
|
135
|
+
const k = (n + h / 30) % 12;
|
|
136
|
+
const v = l - a * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
137
|
+
return Math.round(clamp(v) * 255).toString(16).padStart(2, '0');
|
|
138
|
+
};
|
|
139
|
+
return '#' + channel(0) + channel(8) + channel(4);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function suggestColor(fg, bg, size = 0, colors = {}) {
|
|
143
|
+
if (!parseColor(fg) || !parseColor(bg)) return null;
|
|
144
|
+
const min = size >= 48 ? 3 : 4.5;
|
|
145
|
+
const result = color => ({ color, ratio: contrast(colors[color] || color, bg) });
|
|
146
|
+
if (contrast(fg, bg) >= min) return { color: fg, ratio: contrast(fg, bg) };
|
|
147
|
+
const source = hsl(fg);
|
|
148
|
+
const named = Object.entries(colors).filter(([, c]) => parseColor(c) && contrast(c, bg) >= min)
|
|
149
|
+
.map(([name, c]) => {
|
|
150
|
+
const p = hsl(c);
|
|
151
|
+
const diff = Math.abs(source.h - p.h);
|
|
152
|
+
const hue = Math.min(diff, 360 - diff);
|
|
153
|
+
// Hue is undefined for grays, so match them only with other grays.
|
|
154
|
+
const gray = source.s <= 0.1 && p.s <= 0.1;
|
|
155
|
+
const close = gray || source.s > 0.1 && p.s > 0.1 && hue <= 15;
|
|
156
|
+
return { name, close, distance: (gray ? 0 : hue / 180) ** 2 + (source.s - p.s) ** 2 + (source.l - p.l) ** 2 };
|
|
157
|
+
}).filter(p => p.close).sort((a, b) => a.distance - b.distance);
|
|
158
|
+
if (named.length) return result(named[0].name);
|
|
159
|
+
const candidates = [];
|
|
160
|
+
for (const end of [0, 1]) {
|
|
161
|
+
if (contrast(hslHex({ ...source, l: end }), bg) < min) continue;
|
|
162
|
+
let fail = source.l, pass = end;
|
|
163
|
+
// Check rounded hex values so the suggested color itself meets the target.
|
|
164
|
+
for (let i = 0; i < 24; i++) {
|
|
165
|
+
const l = (fail + pass) / 2;
|
|
166
|
+
if (contrast(hslHex({ ...source, l }), bg) >= min) pass = l;
|
|
167
|
+
else fail = l;
|
|
168
|
+
}
|
|
169
|
+
candidates.push({ l: pass, color: hslHex({ ...source, l: pass }) });
|
|
170
|
+
}
|
|
171
|
+
candidates.sort((a, b) => Math.abs(a.l - source.l) - Math.abs(b.l - source.l));
|
|
172
|
+
return result(candidates[0].color);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function levenshtein(a, b) {
|
|
176
|
+
const m = a.length;
|
|
177
|
+
const n = b.length;
|
|
178
|
+
let prev = Array.from({ length: n + 1 }, (_, j) => j);
|
|
179
|
+
for (let i = 1; i <= m; i++) {
|
|
180
|
+
const cur = [i];
|
|
181
|
+
for (let j = 1; j <= n; j++) cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
182
|
+
prev = cur;
|
|
183
|
+
}
|
|
184
|
+
return prev[n];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function suggest(name, options, n = 5) {
|
|
188
|
+
const q = String(name).toLowerCase();
|
|
189
|
+
return options
|
|
190
|
+
.map((o) => ({ o, d: o.includes(q) || q.includes(o) ? 0 : levenshtein(q, o) }))
|
|
191
|
+
.sort((a, b) => a.d - b.d)
|
|
192
|
+
.slice(0, n)
|
|
193
|
+
.map((x) => x.o);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function normWord(w) {
|
|
197
|
+
return String(w)
|
|
198
|
+
.toLowerCase()
|
|
199
|
+
.normalize('NFKD')
|
|
200
|
+
.replace(/[̀-ͯ]/g, '')
|
|
201
|
+
.replace(/['’]/g, '')
|
|
202
|
+
.replace(/[^\p{L}\p{M}\p{N}%$€£+#@.]/gu, '')
|
|
203
|
+
.replace(/^\.+|\.+$/g, '');
|
|
204
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// getUser and getOrders are application helpers, not the native fetch API.
|
|
2
|
+
// The callback version shows the success path. The promise versions add errors.
|
|
3
|
+
export const callback = `getUser(7, user => {
|
|
4
|
+
getOrders(user.id, orders => {
|
|
5
|
+
showOrders(orders);
|
|
6
|
+
});
|
|
7
|
+
});`;
|
|
8
|
+
|
|
9
|
+
export const promise = `getUser(7)
|
|
10
|
+
.then(user => {
|
|
11
|
+
return getOrders(user.id);
|
|
12
|
+
})
|
|
13
|
+
.then(orders => showOrders(orders));`;
|
|
14
|
+
|
|
15
|
+
export const promiseErrors = `getUser(7)
|
|
16
|
+
.then(user => {
|
|
17
|
+
return getOrders(user.id);
|
|
18
|
+
})
|
|
19
|
+
.then(orders => showOrders(orders))
|
|
20
|
+
.catch(err => showError(err));`;
|
|
21
|
+
|
|
22
|
+
export const awaited = `async function loadOrders() {
|
|
23
|
+
const user = await getUser(7);
|
|
24
|
+
const orders = await getOrders(user.id);
|
|
25
|
+
showOrders(orders);
|
|
26
|
+
}`;
|
|
27
|
+
|
|
28
|
+
export const awaitedErrors = `async function loadOrders() {
|
|
29
|
+
try {
|
|
30
|
+
const user = await getUser(7);
|
|
31
|
+
const orders = await getOrders(user.id);
|
|
32
|
+
showOrders(orders);
|
|
33
|
+
} catch (err) { showError(err); }
|
|
34
|
+
}`;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { callback, promise, promiseErrors, awaited, awaitedErrors } from './code-versions.js';
|
|
2
|
+
|
|
3
|
+
// Every state is declared on every frame; markers drive both morphs and labels.
|
|
4
|
+
const quiet = { sfx: false };
|
|
5
|
+
function heading(s, title, section) {
|
|
6
|
+
s.text(section, { x: 192, y: 94, align: 'left', size: 32, font: 'mono', color: 'muted', at: -1, ...quiet });
|
|
7
|
+
s.text(title, { x: 192, y: 172, align: 'left', size: 88, font: 'display', bold: true, at: -1, ...quiet });
|
|
8
|
+
}
|
|
9
|
+
function editor(s, steps) {
|
|
10
|
+
s.code(steps, { x: 960, y: 570, w: 1550, size: 48, title: 'user-orders.js', lang: 'js', reveal: 'none', enter: 'none', morphDur: 0.8, ...quiet });
|
|
11
|
+
}
|
|
12
|
+
function notes(s, states) {
|
|
13
|
+
states.forEach(([text, at, color = 'ink'], i) => {
|
|
14
|
+
s.text(text, { x: 960, y: 956, size: 44, color, at,
|
|
15
|
+
out: states[i + 1]?.[1], enter: 'fade', dur: 0.18, exit: 'none', ...quiet });
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
function flow(s, times, y = 625) {
|
|
19
|
+
s.box('User 7', { id: 'user', x: 480, y, w: 310, h: 148, size: 52, color: 'blue', at: times[0], ...quiet });
|
|
20
|
+
s.box('Orders', { id: 'orders', x: 960, y, w: 310, h: 148, size: 52, color: 'purple', at: times[1], ...quiet });
|
|
21
|
+
s.box('Display', { id: 'display', x: 1440, y, w: 310, h: 148, size: 52, color: 'green', at: times[2], ...quiet });
|
|
22
|
+
s.arrow('user', 'orders', { at: times[1], color: 'blue', width: 4, ...quiet });
|
|
23
|
+
s.arrow('orders', 'display', { at: times[2], color: 'purple', width: 4, ...quiet });
|
|
24
|
+
}
|
|
25
|
+
export default {
|
|
26
|
+
intro(s) {
|
|
27
|
+
heading(s, 'Callbacks to async/await', 'JAVASCRIPT / ASYNCHRONOUS CODE');
|
|
28
|
+
s.text('Two requests, one dependency', { id: 'dependency', y: 350, size: 56, at: '#question', ...quiet });
|
|
29
|
+
s.annotate('dependency', { type: 'underline', color: 'accent', at: '#depends', ...quiet });
|
|
30
|
+
flow(s, ['#user', '#orders', '#display']);
|
|
31
|
+
s.text('user.id', { x: 720, y: 762, font: 'mono', size: 40, color: 'blue', at: '#orders', ...quiet });
|
|
32
|
+
},
|
|
33
|
+
callbacks(s) {
|
|
34
|
+
heading(s, 'Callbacks', '01 / FUNCTIONS THAT RUN LATER');
|
|
35
|
+
editor(s, [
|
|
36
|
+
{ code: callback, at: -1, highlight: [] },
|
|
37
|
+
{ code: callback, at: '#callback', highlight: [1] },
|
|
38
|
+
{ code: callback, at: '#later', highlight: [1] },
|
|
39
|
+
{ code: callback, at: '#nested', highlight: [2] },
|
|
40
|
+
{ code: callback, at: '#display', highlight: [3] },
|
|
41
|
+
]);
|
|
42
|
+
notes(s, [['Success path', -1, 'muted'], ['Pass a function', '#callback'], ['Run it when data arrives', '#later'], ['Start orders inside the callback', '#nested'], ['Display inside the next callback', '#display']]);
|
|
43
|
+
},
|
|
44
|
+
promises(s) {
|
|
45
|
+
heading(s, 'Promises', '02 / HELPERS NOW RETURN PROMISES');
|
|
46
|
+
editor(s, [
|
|
47
|
+
{ code: callback, at: -1, highlight: [] },
|
|
48
|
+
{ code: promise, at: '#promise', highlight: [1] },
|
|
49
|
+
{ code: promise, at: '#future', highlight: [1] },
|
|
50
|
+
{ code: promise, at: '#then', highlight: [2] },
|
|
51
|
+
{ code: promise, at: '#return', highlight: [3] },
|
|
52
|
+
]);
|
|
53
|
+
notes(s, [['A promise represents a future result', '#promise'], ['Result arrives later', '#future'], ['then receives the user', '#then'], ['Return the orders promise', '#return', 'accent']]);
|
|
54
|
+
s.sfx('whoosh', '#promise', { gain: 0.12 });
|
|
55
|
+
},
|
|
56
|
+
chain(s) {
|
|
57
|
+
heading(s, 'Connect the results', '02 / A SINGLE PROMISE CHAIN');
|
|
58
|
+
editor(s, [
|
|
59
|
+
{ code: promise, at: -1, highlight: [3] },
|
|
60
|
+
{ code: promise, at: '#wait', highlight: [3] },
|
|
61
|
+
{ code: promise, at: '#orders', highlight: [5] },
|
|
62
|
+
{ code: promiseErrors, at: '#catch', highlight: [6] },
|
|
63
|
+
{ code: promiseErrors, at: '#either', highlight: [1, 3, 6] },
|
|
64
|
+
]);
|
|
65
|
+
notes(s, [['Return connects the two requests', '#wait'], ['The next then receives the orders', '#orders'], ['catch handles rejection', '#catch'], ['Failures from either request', '#either']]);
|
|
66
|
+
s.sfx('click', '#catch', { gain: 0.1 });
|
|
67
|
+
},
|
|
68
|
+
asyncVersion(s) {
|
|
69
|
+
heading(s, 'async / await', '03 / THE SAME REQUEST ORDER');
|
|
70
|
+
editor(s, [
|
|
71
|
+
{ code: promiseErrors, at: -1, highlight: [] },
|
|
72
|
+
{ code: awaited, at: '#async', highlight: [1] },
|
|
73
|
+
{ code: awaited, at: '#syntax', highlight: [2, 3] },
|
|
74
|
+
{ code: awaited, at: '#first', highlight: [2] },
|
|
75
|
+
{ code: awaited, at: '#second', highlight: [3] },
|
|
76
|
+
]);
|
|
77
|
+
notes(s, [['async enables await', '#async'], ['Success path', '#syntax', 'muted'], ['The user result becomes a variable', '#first'], ['Then fetch orders with user.id', '#second']]);
|
|
78
|
+
s.sfx('whoosh', '#async', { gain: 0.12 });
|
|
79
|
+
},
|
|
80
|
+
waiting(s) {
|
|
81
|
+
heading(s, 'What await does', '03 / WAITING AND FAILURE');
|
|
82
|
+
editor(s, [
|
|
83
|
+
{ code: awaited, at: -1, highlight: [2] },
|
|
84
|
+
{ code: awaited, at: '#pause', highlight: [2] },
|
|
85
|
+
{ code: awaited, at: '#other', highlight: [2] },
|
|
86
|
+
{ code: awaited, at: '#throws', highlight: [2, 3] },
|
|
87
|
+
{ code: awaitedErrors, at: '#try', highlight: [2, 6] },
|
|
88
|
+
]);
|
|
89
|
+
notes(s, [['This function pauses at await', '#pause'], ['Other JavaScript can run', '#other', 'accent'], ['Rejection throws at await', '#throws'], ['try / catch handles the failure', '#try']]);
|
|
90
|
+
s.sfx('click', '#try', { gain: 0.1 });
|
|
91
|
+
},
|
|
92
|
+
summary(s) {
|
|
93
|
+
heading(s, 'One sequence, three styles', 'JAVASCRIPT / CALLBACKS TO ASYNC/AWAIT');
|
|
94
|
+
const styles = [
|
|
95
|
+
['Callbacks', 'Nested functions', 480, '#callbacks', 'blue'],
|
|
96
|
+
['Promises', 'Returned promises', 960, '#promises', 'purple'],
|
|
97
|
+
['async / await', 'Awaited results', 1440, '#await', 'accent'],
|
|
98
|
+
];
|
|
99
|
+
styles.forEach(([title, detail, x, at, color]) => {
|
|
100
|
+
s.text(title, { x, y: 404, size: 54, bold: true, color, at, ...quiet });
|
|
101
|
+
s.text(detail, { x, y: 482, size: 36, color: 'muted', at, ...quiet });
|
|
102
|
+
});
|
|
103
|
+
flow(s, [-1, -1, -1], 715);
|
|
104
|
+
s.text('Fetch user, then orders, then display', { y: 935, size: 48, at: '#read', ...quiet });
|
|
105
|
+
},
|
|
106
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# JavaScript: callbacks to async/await
|
|
2
|
+
|
|
3
|
+
## intro {chapter="Callbacks"}
|
|
4
|
+
[#question] How does JavaScript handle two requests that [#depends] depend on each other? Our helper functions [#user] fetch a user, then [#orders] that user's orders, and [#display] display them.
|
|
5
|
+
|
|
6
|
+
## callbacks {transition=cut}
|
|
7
|
+
[#callback] Here, a callback is a function that [#later] runs after a result arrives. [#nested] When the user arrives, its callback starts fetching orders. [#display] Another callback displays them.
|
|
8
|
+
|
|
9
|
+
## promises {chapter="Promises" transition=cut}
|
|
10
|
+
[#promise] Now use helpers that return promises, which represent [#future] future results. A then handler [#then] receives the user, and [#return] returns the promise for their orders.
|
|
11
|
+
|
|
12
|
+
## chain {transition=cut}
|
|
13
|
+
[#wait] Returning that promise makes the next handler [#orders] wait for the orders. A [#catch] catch handler handles failures from [#either] either request.
|
|
14
|
+
|
|
15
|
+
## asyncVersion {chapter="Async and await" transition=cut}
|
|
16
|
+
[#async] The {async|a sink} keyword lets a function use [#syntax] {await|a wait} expressions. Each [#first] {await|a wait} gives us the result when its [#second] promise succeeds.
|
|
17
|
+
|
|
18
|
+
## waiting {transition=cut}
|
|
19
|
+
[#pause] While this function is waiting, [#other] other JavaScript can keep running. A rejected promise [#throws] makes this function throw, so [#try] try and catch handle failures.
|
|
20
|
+
|
|
21
|
+
## summary {hold=1.1 transition=fade}
|
|
22
|
+
[#callbacks] Callbacks nest the steps, and [#promises] promises connect them through handlers. [#await] The {async|a sink} and {await|a wait} keywords make promise based code [#read] easier to read.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"title": "JavaScript: callbacks to async/await",
|
|
3
|
+
"theme": "midnight",
|
|
4
|
+
"size": "youtube",
|
|
5
|
+
"voice": "af_heart",
|
|
6
|
+
"music": {
|
|
7
|
+
"style": "tech",
|
|
8
|
+
"volume": 0.12
|
|
9
|
+
},
|
|
10
|
+
"captions": false,
|
|
11
|
+
"speed": 0.95,
|
|
12
|
+
"sfx": "minimal",
|
|
13
|
+
"transition": "fade",
|
|
14
|
+
"cover": "intro@end",
|
|
15
|
+
"end": 1.0
|
|
16
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// Geometry is in canvas pixels; every sector remains a circular sector.
|
|
2
|
+
// For n even: half-angle d=pi/n, projected half-chord a=R sin(d),
|
|
3
|
+
// distance between the alternating tips h=R cos(d).
|
|
4
|
+
// The n/2 arcs on EACH long edge have total length pi R.
|
|
5
|
+
const TAU = Math.PI * 2;
|
|
6
|
+
const R = 255;
|
|
7
|
+
const CX = 960;
|
|
8
|
+
const CY = 550;
|
|
9
|
+
|
|
10
|
+
function heading(s, text, at = -1) {
|
|
11
|
+
s.text(text, { x: 960, y: 155, size: 88, font: 'display', at, enter: at < 0 ? 'none' : 'write', sfx: false });
|
|
12
|
+
}
|
|
13
|
+
function label(s, text, x, y, at, extra = {}) {
|
|
14
|
+
s.text(text, { x, y, size: 48, at, sfx: false, ...extra });
|
|
15
|
+
}
|
|
16
|
+
function stroke(ctx, pts, color, width = 4) {
|
|
17
|
+
ctx.strokeStyle = color;
|
|
18
|
+
ctx.lineWidth = width;
|
|
19
|
+
ctx.lineCap = 'round';
|
|
20
|
+
ctx.lineJoin = 'round';
|
|
21
|
+
ctx.beginPath();
|
|
22
|
+
pts.forEach(([x, y], i) => i ? ctx.lineTo(x, y) : ctx.moveTo(x, y));
|
|
23
|
+
ctx.stroke();
|
|
24
|
+
}
|
|
25
|
+
function sector(ctx, radius, delta, color, ink, alpha = 1, inner = 1) {
|
|
26
|
+
ctx.save();
|
|
27
|
+
ctx.globalAlpha *= alpha;
|
|
28
|
+
ctx.beginPath();
|
|
29
|
+
ctx.moveTo(0, 0);
|
|
30
|
+
ctx.arc(0, 0, radius, -Math.PI / 2 - delta, -Math.PI / 2 + delta);
|
|
31
|
+
ctx.closePath();
|
|
32
|
+
ctx.fillStyle = color;
|
|
33
|
+
ctx.globalAlpha *= 0.12;
|
|
34
|
+
ctx.fill();
|
|
35
|
+
ctx.globalAlpha /= 0.12;
|
|
36
|
+
const a = radius * Math.sin(delta), h = radius * Math.cos(delta);
|
|
37
|
+
ctx.save();
|
|
38
|
+
ctx.globalAlpha *= inner;
|
|
39
|
+
stroke(ctx, [[-a, -h], [0, 0], [a, -h]], ink, 2.4);
|
|
40
|
+
ctx.restore();
|
|
41
|
+
ctx.beginPath();
|
|
42
|
+
ctx.arc(0, 0, radius, -Math.PI / 2 - delta, -Math.PI / 2 + delta);
|
|
43
|
+
ctx.strokeStyle = color;
|
|
44
|
+
ctx.lineWidth = 5;
|
|
45
|
+
ctx.stroke();
|
|
46
|
+
ctx.restore();
|
|
47
|
+
}
|
|
48
|
+
function row(ctx, s, n, radius = R, x = CX, y = CY, alpha = 1) {
|
|
49
|
+
const d = Math.PI / n, a = radius * Math.sin(d), h = radius * Math.cos(d);
|
|
50
|
+
for (let j = 0; j < n; j++) {
|
|
51
|
+
ctx.save();
|
|
52
|
+
ctx.translate(x + (j - (n - 1) / 2) * a, y + (j % 2 ? -h / 2 : h / 2));
|
|
53
|
+
ctx.rotate(j % 2 ? Math.PI : 0);
|
|
54
|
+
sector(ctx, radius, d, s.color(j % 2 ? 'blue' : 'yellow'), s.color('ink'), alpha, n >= 64 ? 0.2 : n >= 32 ? 0.35 : 0.65);
|
|
55
|
+
ctx.restore();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function circle(s, { x = 560, y = 550, r = 235, at = -1, out } = {}) {
|
|
59
|
+
// Exact circular geometry makes the radius endpoint land on the boundary.
|
|
60
|
+
// The chalk look's rough s.circle outline can wander outside its nominal r.
|
|
61
|
+
s.draw({ at, out, enter: at < 0 ? 'none' : 'fade' }, (ctx) => {
|
|
62
|
+
ctx.beginPath();
|
|
63
|
+
ctx.arc(x, y, r, 0, TAU);
|
|
64
|
+
ctx.save();
|
|
65
|
+
ctx.globalAlpha *= 0.12;
|
|
66
|
+
ctx.fillStyle = s.color('yellow'); ctx.fill();
|
|
67
|
+
ctx.restore();
|
|
68
|
+
ctx.strokeStyle = s.color('ink'); ctx.lineWidth = 5; ctx.stroke();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
function cutCircle(s, { at = -1, coloredAt = -1, cutAt = -1 } = {}) {
|
|
72
|
+
s.draw({ at, enter: 'none' }, (ctx) => {
|
|
73
|
+
const colorP = coloredAt < 0 ? 1 : s.p(coloredAt, 1.4);
|
|
74
|
+
const cutP = cutAt < 0 ? 1 : s.p(cutAt, 2.6, 'linear');
|
|
75
|
+
for (let i = 0; i < 8; i++) {
|
|
76
|
+
ctx.save();
|
|
77
|
+
ctx.translate(CX, CY);
|
|
78
|
+
ctx.rotate((i + 0.5) * TAU / 8);
|
|
79
|
+
const c = s.color(i < 4 ? 'yellow' : 'blue');
|
|
80
|
+
sector(ctx, R, Math.PI / 8, colorP > 0 ? c : s.color('ink'), s.color('ink'), 1, Math.min(1, Math.max(0, cutP * 8 - i)));
|
|
81
|
+
ctx.restore();
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
function rectangle(s, at = -1, { x = CX, y = CY, r = R, opacity = 1 } = {}) {
|
|
86
|
+
const w = Math.PI * r;
|
|
87
|
+
s.draw({ at, enter: at < 0 ? 'none' : 'fade', dur: 1.1, opacity }, (ctx) => {
|
|
88
|
+
ctx.save();
|
|
89
|
+
ctx.globalAlpha *= 0.1;
|
|
90
|
+
ctx.fillStyle = s.color('yellow');
|
|
91
|
+
ctx.fillRect(x - w / 2, y - r / 2, w, r);
|
|
92
|
+
ctx.restore();
|
|
93
|
+
stroke(ctx, [[x - w / 2, y - r / 2], [x + w / 2, y - r / 2]], s.color('yellow'), 6);
|
|
94
|
+
stroke(ctx, [[x - w / 2, y + r / 2], [x + w / 2, y + r / 2]], s.color('blue'), 6);
|
|
95
|
+
stroke(ctx, [[x - w / 2, y - r / 2], [x - w / 2, y + r / 2]], s.color('ink'), 4);
|
|
96
|
+
stroke(ctx, [[x + w / 2, y - r / 2], [x + w / 2, y + r / 2]], s.color('ink'), 4);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
function horizontalMeasure(s, x1, x2, y, at, color = 'yellow') {
|
|
100
|
+
s.line([[x1, y - 12], [x1, y + 12], [x1, y], [x2, y], [x2, y - 12], [x2, y + 12]], { color, at, width: 3, sfx: false });
|
|
101
|
+
}
|
|
102
|
+
function verticalMeasure(s, x, y1, y2, at) {
|
|
103
|
+
s.line([[x - 12, y1], [x + 12, y1], [x, y1], [x, y2], [x - 12, y2], [x + 12, y2]], { color: 'ink', at, width: 3, sfx: false });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default {
|
|
107
|
+
intro(s) {
|
|
108
|
+
heading(s, 'Why this circle formula?');
|
|
109
|
+
circle(s, { at: -1 });
|
|
110
|
+
s.math(String.raw`A = \pi r^2`, { x: 1280, y: 520, size: 118, at: '#question', color: 'yellow', sfx: false });
|
|
111
|
+
s.line([[560, 550], [795, 550]], { at: '#idea', color: 'yellow', sfx: false });
|
|
112
|
+
s.math('r', { x: 685, y: 495, size: 64, at: '#idea', color: 'yellow', sfx: false });
|
|
113
|
+
label(s, 'Cut it. Rearrange it.', 1280, 690, '#idea', { size: 56 });
|
|
114
|
+
},
|
|
115
|
+
measure(s) {
|
|
116
|
+
heading(s, 'Start with the radius');
|
|
117
|
+
circle(s);
|
|
118
|
+
s.line([[560, 550], [795, 550]], { at: '#radius', color: 'yellow', sfx: false });
|
|
119
|
+
s.math('r', { x: 685, y: 495, size: 64, at: '#radius', color: 'yellow', sfx: false });
|
|
120
|
+
label(s, 'Center to edge', 1280, 390, '#radius', { size: 56 });
|
|
121
|
+
s.draw({ at: '#around' }, (ctx) => {
|
|
122
|
+
ctx.beginPath();
|
|
123
|
+
ctx.arc(560, 550, 247, -Math.PI / 2, -Math.PI / 2 + TAU * s.p('#around', 2.7, 'linear'));
|
|
124
|
+
ctx.strokeStyle = s.color('blue'); ctx.lineWidth = 6; ctx.stroke();
|
|
125
|
+
});
|
|
126
|
+
label(s, 'Around the circle', 1280, 580, '#around');
|
|
127
|
+
s.math(String.raw`C = 2\pi r`, { x: 1280, y: 700, size: 86, color: 'blue', at: '#around', sfx: false });
|
|
128
|
+
},
|
|
129
|
+
slices(s) {
|
|
130
|
+
heading(s, 'Cut into equal slices', '#cut');
|
|
131
|
+
cutCircle(s, { at: '#cut', cutAt: '#cut', coloredAt: '#halves' });
|
|
132
|
+
label(s, '8 equal slices', CX, 865, '#cut');
|
|
133
|
+
// A trace of each semicircle links the two colors to the circumference.
|
|
134
|
+
s.draw({ at: '#halves' }, (ctx) => {
|
|
135
|
+
for (let i = 0; i < 2; i++) {
|
|
136
|
+
ctx.beginPath();
|
|
137
|
+
ctx.arc(CX, CY, R + 12, -Math.PI / 2 + i * Math.PI, -Math.PI / 2 + i * Math.PI + Math.PI * s.p('#halves', 1.7, 'linear'));
|
|
138
|
+
ctx.strokeStyle = s.color(i ? 'blue' : 'yellow'); ctx.lineWidth = 4; ctx.stroke();
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
arrange(s) {
|
|
143
|
+
heading(s, 'Alternate the directions');
|
|
144
|
+
s.draw({ at: -1 }, (ctx) => {
|
|
145
|
+
const n = 8, d = Math.PI / n, a = R * Math.sin(d), h = R * Math.cos(d);
|
|
146
|
+
// Draw unmoved sectors first, then traveling sectors, to keep motion legible.
|
|
147
|
+
const pieces = Array.from({ length: n }, (_, i) => {
|
|
148
|
+
const j = i < 4 ? i * 2 : (i - 4) * 2 + 1;
|
|
149
|
+
return { i, j, p: s.p(s.time('#move') + j * 0.24, 1.65) };
|
|
150
|
+
}).sort((a, b) => a.p - b.p);
|
|
151
|
+
for (const { i, j, p } of pieces) {
|
|
152
|
+
const targetX = (j - (n - 1) / 2) * a;
|
|
153
|
+
const targetY = j % 2 ? -h / 2 : h / 2;
|
|
154
|
+
const startAngle = (i + 0.5) * TAU / n;
|
|
155
|
+
const endAngle = j % 2 ? Math.PI : 0;
|
|
156
|
+
const turn = ((endAngle - startAngle + 3 * Math.PI) % TAU) - Math.PI;
|
|
157
|
+
ctx.save();
|
|
158
|
+
ctx.translate(CX + targetX * p, CY + targetY * p + Math.sin(p * Math.PI) * (j % 2 ? 115 : -115));
|
|
159
|
+
ctx.rotate(startAngle + turn * p);
|
|
160
|
+
sector(ctx, R, d, s.color(i < 4 ? 'yellow' : 'blue'), s.color('ink'));
|
|
161
|
+
ctx.restore();
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
label(s, 'Same pieces, same area', CX, 865, '#same', { size: 56 });
|
|
165
|
+
s.sfx('whoosh', '#move', { gain: 0.17, dur: 0.6 });
|
|
166
|
+
},
|
|
167
|
+
refine(s) {
|
|
168
|
+
heading(s, 'Thinner slices, flatter edges');
|
|
169
|
+
const stages = [
|
|
170
|
+
{ n: 8, at: -1, out: '#more' },
|
|
171
|
+
{ n: 16, at: '#more', out: 'flatter' },
|
|
172
|
+
{ n: 32, at: 'flatter', out: '#limit' },
|
|
173
|
+
{ n: 64, at: '#limit', out: 'rectangle' },
|
|
174
|
+
];
|
|
175
|
+
for (const st of stages) {
|
|
176
|
+
s.draw({ at: st.at, out: st.out, enter: st.at < 0 ? 'none' : 'fade', dur: 0.6 }, (ctx) => row(ctx, s, st.n));
|
|
177
|
+
}
|
|
178
|
+
label(s, '8 slices', CX, 865, -1, { out: '#more', exit: 'none' });
|
|
179
|
+
label(s, '16 slices', CX, 865, '#more', { out: 'flatter', exit: 'none' });
|
|
180
|
+
label(s, '32 slices', CX, 865, 'flatter', { out: '#limit', exit: 'none' });
|
|
181
|
+
label(s, '64 slices', CX, 865, '#limit', { out: 'rectangle', exit: 'none' });
|
|
182
|
+
rectangle(s, 'rectangle');
|
|
183
|
+
label(s, 'The limiting rectangle', CX, 865, 'rectangle', { size: 56 });
|
|
184
|
+
},
|
|
185
|
+
edges(s) {
|
|
186
|
+
heading(s, 'Half the circumference on each side');
|
|
187
|
+
s.draw({ at: -1 }, (ctx) => row(ctx, s, 64));
|
|
188
|
+
horizontalMeasure(s, 558, 1362, 355, '#top');
|
|
189
|
+
s.math(String.raw`\pi r`, { x: CX, y: 300, size: 68, color: 'yellow', at: '#top', sfx: false });
|
|
190
|
+
horizontalMeasure(s, 558, 1362, 748, 'bottom', 'blue');
|
|
191
|
+
s.math(String.raw`\pi r`, { x: CX, y: 800, size: 68, color: 'blue', at: 'bottom', sfx: false });
|
|
192
|
+
s.math(String.raw`\frac{1}{2}(2\pi r) = \pi r`, { x: CX, y: 915, size: 76, at: '#width', color: 'yellow', sfx: false });
|
|
193
|
+
},
|
|
194
|
+
height(s) {
|
|
195
|
+
heading(s, 'The height approaches the radius');
|
|
196
|
+
s.draw({ at: -1, out: '#height' }, (ctx) => row(ctx, s, 64));
|
|
197
|
+
const d = Math.PI / 64, a = R * Math.sin(d), h = R * Math.cos(d);
|
|
198
|
+
// This is an actual sector radius, not a finite-slice vertical height.
|
|
199
|
+
s.line([[CX + a / 2, CY + h / 2], [CX + a * 1.5, CY - h / 2]], { at: '#radius', out: '#height', color: 'yellow', width: 7, sfx: false });
|
|
200
|
+
s.math('r', { x: 1050, y: CY, size: 70, color: 'yellow', at: '#radius', out: '#height', sfx: false });
|
|
201
|
+
rectangle(s, '#height');
|
|
202
|
+
verticalMeasure(s, 1485, CY - R / 2, CY + R / 2, '#height');
|
|
203
|
+
s.math('r', { x: 1560, y: CY, size: 76, at: '#height', sfx: false });
|
|
204
|
+
label(s, 'In the limit', CX, 865, '#height', { size: 56 });
|
|
205
|
+
},
|
|
206
|
+
example(s) {
|
|
207
|
+
heading(s, 'Try a radius of two', '#two');
|
|
208
|
+
rectangle(s, '#two');
|
|
209
|
+
horizontalMeasure(s, 558, 1362, 355, '#two');
|
|
210
|
+
s.math(String.raw`2\pi`, { x: CX, y: 295, size: 76, color: 'yellow', at: '#two', sfx: false });
|
|
211
|
+
verticalMeasure(s, 1485, CY - R / 2, CY + R / 2, '#multiply');
|
|
212
|
+
s.math('2', { x: 1560, y: CY, size: 76, at: '#multiply', sfx: false });
|
|
213
|
+
s.math(String.raw`2\pi \times 2 = 4\pi`, { x: CX, y: 850, size: 88, color: 'yellow', at: '#multiply', sfx: false });
|
|
214
|
+
label(s, 'square units', CX, 950, 'square');
|
|
215
|
+
},
|
|
216
|
+
conclusion(s) {
|
|
217
|
+
heading(s, 'The area of a circle');
|
|
218
|
+
rectangle(s, -1, { y: 490, r: 220 });
|
|
219
|
+
horizontalMeasure(s, 614, 1306, 325, '#product');
|
|
220
|
+
s.math(String.raw`\pi r`, { x: CX, y: 270, size: 72, color: 'yellow', at: '#product', sfx: false });
|
|
221
|
+
verticalMeasure(s, 1420, 380, 600, '#product');
|
|
222
|
+
s.math('r', { x: 1500, y: 490, size: 78, at: '#product', sfx: false });
|
|
223
|
+
s.math(String.raw`A = \pi r \times r`, { x: CX, y: 785, size: 100, at: '#product', out: '#answer', exit: 'none', sfx: false });
|
|
224
|
+
s.math(String.raw`A = \pi r^2`, { id: 'answer', x: CX, y: 785, size: 128, at: '#answer', color: 'yellow', sfx: false });
|
|
225
|
+
s.annotate('answer', { type: 'box', padding: 26, color: 'yellow', at: s.time('#answer') + 0.9, dur: 1, sfx: false });
|
|
226
|
+
s.sfx('chime', '#answer', { gain: 0.15 });
|
|
227
|
+
},
|
|
228
|
+
};
|