An AI skill that turns books into animated, narrated, interactive web experiences
JavaScript ai-agentsai-educationanimationclaudeclaude-codeclaude-skillsedtecheducationinteractive-textbookscormtext-to-video
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Relations and Functions — Elementary Algebra</title>
<link rel="stylesheet" href="../lib/engine.css">
<link rel="expect" href="#bar" blocking="render"><!-- hold the first frame until the player exists, so the page transition can run -->
</head>
<body>
<script src="../lib/engine.js"></script>
<script src="../lib/math.js"></script>
<script src="audio/en/timings.js"></script>
<script>
'use strict';
const CHAPTER = { number: 45, title: 'Relations and Functions', minutes: 8, next: '../ch46/' };
const DOM = COL.whole, RAN = COL.nat, OK = COL.good, NO = COL.bad;
/* ---------- chapter drawing helpers ---------- */
const mline = (p, parts, x, y, t0, fill = COL.chalk, size = 48, anchor = 'middle') => { const e = M(p, [].concat(parts), { x, y, size, fill, anchor, o: 0 }); show(e, t0); return e; };
const text = (p, str, x, y, t0, { size = 30, fill = COL.chalk, anchor = 'start', weight = 500 } = {}) => { const e = T(p, str, { x, y, size, fill, anchor, weight, o: 0 }); show(e, t0); return e; };
// Mapping diagram: domain oval at x1, range oval at x2, centred at height cy. pairs: [[input, output]].
function mapping(p, x1, x2, cy, ins, outs, pairs, t0, { gap = 66, arrowsAt, labels = true, size = 34, vals = true } = {}) {
const h = Math.max(ins.length, outs.length) * gap + 50, g = G(p, { o: 0 });
mk('ellipse', { cx: x1, cy, rx: 62, ry: h / 2, fill: mix(COL.board, DOM, .1), stroke: DOM, 'stroke-width': 3 }, g);
mk('ellipse', { cx: x2, cy, rx: 62, ry: h / 2, fill: mix(COL.board, RAN, .1), stroke: RAN, 'stroke-width': 3 }, g);
if (labels) { T(g, 'domain', { x: x1, y: cy - h / 2 - 16, size: 22, fill: DOM, anchor: 'middle', weight: 600 }); T(g, 'range', { x: x2, y: cy - h / 2 - 16, size: 22, fill: RAN, anchor: 'middle', weight: 600 }); }
show(g, t0);
const iy = i => cy + (i - (ins.length - 1) / 2) * gap, oy = j => cy + (j - (outs.length - 1) / 2) * gap;
const inEl = ins.map((v, i) => { const e = M(p, [num(v)], { x: x1, y: iy(i) + size * .34, size, o: 0 }); if (vals) show(e, t0 + .2); return e; });
const outEl = outs.map((v, j) => { const e = M(p, [num(v)], { x: x2, y: oy(j) + size * .34, size, o: 0 }); if (vals) show(e, t0 + .2); return e; });
const arrows = pairs.map(([a, b], k) => {
const ya = iy(ins.indexOf(a)), yb = oy(outs.indexOf(b)), xa = x1 + 36, xb = x2 - 40, L = Math.hypot(xb - xa, yb - ya), ux = (xb - xa) / L, uy = (yb - ya) / L;
const ar = path(p, `M${xa} ${ya}L${xb} ${yb}M${xb - 13 * ux - 8 * uy} ${yb - 13 * uy + 8 * ux}L${xb} ${yb}L${xb - 13 * ux + 8 * uy} ${yb - 13 * uy - 8 * ux}`, { stroke: COL.chalk, 'stroke-width': 3 }, { d: 0 });
draw(ar, (arrowsAt ?? t0 + .5) + k * .3, .5);
return ar;
});
return { g, inEl, outEl, arrows, iy, oy, box: [x1 - 90, cy - h / 2 - 50, x2 - x1 + 180, h + 70] };
}
// Recolour arrows.
const tintArrows = (arrs, color, t0, run = tw) => arrs.forEach((a, i) => { put(a, { c_stroke: COL.chalk }); run(a, { c_stroke: color, 'a_stroke-width': 5 }, t0 + i * .15, .3); });
// A vertical scanning line across plane P, moving from x = a to x = b; returns the time it reaches x.
function sweep(p, P, a, b, t0, dur, color = COL.task) {
const l = path(p, `M${P.PX(a)} ${P.PY(P.y1 + .4)}V${P.PY(P.y0 - .4)}`, { stroke: color, 'stroke-width': 4, 'stroke-opacity': .8 }, { o: 0 });
show(l, t0, .2);
tw(l, { x: P.PX(b) - P.PX(a) }, t0, dur, lin);
hide(l, t0 + dur, .3);
return x => t0 + dur * (x - a) / (b - a);
}
// Curve through x(t), y(t) on plane P, t from 0 to 1.
const curve = (P, fx, fy, n = 80) => Array.from({ length: n + 1 }, (_, i) => `${i ? 'L' : 'M'}${P.PX(fx(i / n)).toFixed(1)} ${P.PY(fy(i / n)).toFixed(1)}`).join('');
const CURVES = {
para: [t => -2.6 + 5.2 * t, t => { const x = -2.6 + 5.2 * t; return x * x / 2 - 2; }],
circle: [t => 2.6 * Math.cos(t * 2 * Math.PI), t => 2.6 * Math.sin(t * 2 * Math.PI)],
side: [t => { const y = -2.6 + 5.2 * t; return y * y / 2 - 2; }, t => -2.6 + 5.2 * t],
vee: [t => -3.4 + 6.8 * t, t => Math.abs(-3.4 + 6.8 * t) - 1.5],
wave: [t => -3.6 + 7.2 * t, t => 1.6 * Math.sin((-3.6 + 7.2 * t) * 1.4)],
sideL: [t => { const y = -2.6 + 5.2 * t; return 2 - y * y / 2; }, t => -2.6 + 5.2 * t],
line: [t => -3.5 + 7 * t, t => .6 * (-3.5 + 7 * t) + .5],
vert: [t => 1.2, t => -3.6 + 7.2 * t],
};
// A small inline graph (HTML) for practice rows.
function miniSvg(kind) {
const svg = mk('svg', { viewBox: '-50 -38 100 76', width: 124, height: 94, class: 'm' });
svg.style.verticalAlign = 'middle';
mk('path', { d: 'M-46 0H46M0 -35V35', stroke: COL.dim, 'stroke-width': 1.5, fill: 'none' }, svg);
const [fx, fy] = CURVES[kind], P = { PX: x => x * 10, PY: y => -y * 10 };
mk('path', { d: curve(P, fx, fy), stroke: COL.task, 'stroke-width': 3, fill: 'none', 'stroke-linecap': 'round' }, svg);
return svg;
}
/* ---------- beats ---------- */
const PAIRS = [[-2, 1], [0, 3], [1, 3], [4, 6]];
const BEATS = [
['intro', 'Relations and functions', (m, D) => {
const g = G(scene, { o: 0, y: 24 });
T(g, 'Unit 8, Chapter 45', { x: 800, y: 300, size: 28, fill: COL.dim, anchor: 'middle' });
T(g, 'Relations and Functions', { x: 800, y: 410, size: 84, weight: 600, anchor: 'middle' });
tw(g, { o: 1, y: 0 }, .2, 1.1, out);
const r = text(scene, 'one input → exactly one output', 800, 560, m('sub'), { size: 36, fill: COL.dim, anchor: 'middle' });
tw(g, { o: 0, y: -24 }, D - .9, .8); hide(r, D - .9, .8);
}],
['relation', 'Domain and range', (m) => {
const p = panel(0), X1 = 560, X2 = 1040, CY = 560;
text(p, 'relation: a set of ordered pairs', 800, 100, .2, { size: 28, fill: COL.dim, anchor: 'middle' });
const cx = i => 440 + i * 240;
PAIRS.forEach(([a, b], i) => pop(chip(p, [`(${num(a)}, ${num(b)})`], COL.chalk, { x: cx(i), y: 190, size: 38, s: 0, o: 0 }), m('pairs', i * .2)));
const ins = [-2, 0, 1, 4], outs = [1, 3, 6];
const D = mapping(p, X1, X2, CY, ins, outs, PAIRS, m('dom'), { arrowsAt: m('map'), vals: false });
// Inputs fly from the chips into the domain; outputs into the range (the two 3s land on one spot).
PAIRS.forEach(([a, b], i) => {
const ca = M(p, [num(a)], { x: cx(i) - 38, y: 202, size: 34, fill: DOM, o: 0 }), cb = M(p, [num(b)], { x: cx(i) + 38, y: 202, size: 34, fill: RAN, o: 0 });
const ta = m('dom', .6 + i * .35), tb = m('ran', .4 + i * .35);
show(ca, ta, .1); tw(ca, { x: X1, y: D.iy(ins.indexOf(a)) + 12 }, ta, .7); hide(ca, ta + .8, .2); show(D.inEl[ins.indexOf(a)], ta + .7, .2);
show(cb, tb, .1); tw(cb, { x: X2, y: D.oy(outs.indexOf(b)) + 12 }, tb, .7); hide(cb, tb + .8, .2); show(D.outEl[outs.indexOf(b)], tb + .7, .2);
});
mline(p, 'domain = {−2, 0, 1, 4}', 120, 420, m('dom', 2.4), DOM, 36, 'start');
mline(p, 'range = {1, 3, 6}', 1480, 420, m('ran', 2.2), RAN, 36, 'end');
text(p, '3 is listed once', 1480, 465, m('ran', 3.4), { size: 22, fill: COL.dim, anchor: 'end' });
}],
['function', 'Functions', (m) => {
const p = panel(0);
text(p, 'function: each input has exactly one output', 800, 110, m('one'), { size: 32, fill: COL.task, anchor: 'middle', weight: 600 });
const A = mapping(p, 300, 620, 480, [-2, 0, 1, 4], [1, 3, 6], PAIRS, .2, { arrowsAt: .3 });
A.inEl.forEach((e, i) => pulse(e, m('check', i * .4), 1.4));
tintArrows(A.arrows, OK, m('check'));
tintArrows([A.arrows[1], A.arrows[2]], COL.int, m('share'));
text(p, 'outputs may repeat', 460, 770, m('share', .5), { size: 26, fill: COL.int, anchor: 'middle', weight: 600 });
text(p, 'a function', 460, 820, m('check', 2), { size: 30, fill: OK, anchor: 'middle', weight: 700 });
const B = mapping(p, 980, 1300, 480, [3, 5], [1, 5, 2], [[3, 1], [3, 5], [5, 2]], m('bad'), { arrowsAt: m('bad', .5) });
tintArrows([B.arrows[0], B.arrows[1]], NO, m('split'));
pulse(B.inEl[0], m('split'), 1.6);
text(p, 'one input, two outputs', 1140, 770, m('split', .8), { size: 26, fill: NO, anchor: 'middle', weight: 600 });
text(p, 'not a function', 1140, 820, m('not'), { size: 30, fill: NO, anchor: 'middle', weight: 700 });
}],
['q1', 'Quick check', () => {
const p = panel(0);
S.maps = [[[1, 2, 3], [4, 7], [[1, 4], [2, 4], [3, 7]]], [[-1, 3], [0, 2, 5], [[-1, 0], [-1, 2], [3, 5]]], [[5, 6, 7], [1, 2, 3], [[5, 1], [6, 2], [7, 3]]]]
.map(([ins, outs, pr], i) => { text(p, 'ABC'[i], 290 + i * 510, 170, .2, { size: 32, anchor: 'middle', weight: 700 }); return mapping(p, 180 + i * 510, 400 + i * 510, 400, ins, outs, pr, .3 + i * .2, { labels: false }); });
}, { ask: done => quiz(BAND, [{
id: 'c-map', prompt: ['Click the relation that is not a function.'],
build: pickEls(S.maps.map((d, i) => ({ el: d.g, box: d.box, label: 'Relation ' + 'ABC'[i] })), 1, 'In B, the input −1 goes to both 0 and 2.',
i => i === 0 ? 'In A, 1 and 2 share the output 4, which is allowed. Each input still has one arrow.' : 'In C, each input has exactly one arrow.',
() => tintArrows(S.maps[1].arrows.slice(0, 2), NO, 0, fx)),
}], done) }],
['forms', 'Four views', (m) => {
const p = panel(0);
text(p, 'x: input, the independent variable', 160, 130, m('vars'), { size: 28, fill: DOM, weight: 600 });
text(p, 'y: output, the dependent variable', 160, 180, m('vars', 1.5), { size: 28, fill: RAN, weight: 600 });
text(p, 'words', 300, 290, m('rule'), { size: 24, fill: COL.dim, anchor: 'middle' });
text(p, 'y is twice x', 300, 350, m('rule'), { size: 36, anchor: 'middle', weight: 600 });
mline(p, 'y = 2x', 300, 420, m('rule', .8), COL.task, 46);
text(p, 'table', 640, 290, m('table'), { size: 24, fill: COL.dim, anchor: 'middle' });
const rows = [[-1, -2], [0, 0], [1, 2], [2, 4]];
const tg = G(p, { o: 0 });
path(tg, 'M560 320H720M640 300V560', { stroke: COL.faint, 'stroke-width': 2 });
M(tg, ['x'], { x: 600, y: 315, size: 30, fill: DOM }); M(tg, ['y'], { x: 680, y: 315, size: 30, fill: RAN });
show(tg, m('table'));
rows.forEach(([x, y], i) => { mline(p, num(x), 600, 370 + i * 55, m('table', .3 + i * .15), COL.chalk, 32); mline(p, num(y), 680, 370 + i * 55, m('table', .3 + i * .15), COL.chalk, 32); });
const P = plane(p, { cx: 1160, cy: 500, u: 44, x0: -4, x1: 4, y0: -5, y1: 5, t0: m('graph') });
P.layer = p;
rows.forEach(([x, y], i) => {
const c = M(p, [`(${num(x)}, ${num(y)})`], { x: 640, y: 370 + i * 55, size: 28, fill: COL.task, o: 0 });
const t = m('graph', .4 + i * .35);
show(c, t, .1); tw(c, { x: P.PX(x) + 60, y: P.PY(y) - 14 }, t, .6); hide(c, t + .7, .2);
P.dot(x, y, COL.task, t + .5);
});
const fr = put(mk('rect', { x: 120, y: 250, width: 1360, height: 560, rx: 24, fill: 'none', stroke: COL.task, 'stroke-width': 3, 'stroke-dasharray': '10 8' }, p), { o: 0 });
show(fr, m('same'));
text(p, 'one function, several views', 800, 850, m('same', .3), { size: 28, fill: COL.task, anchor: 'middle', weight: 600 });
}],
['vlt', 'Vertical line test', (m) => {
const p = panel(0);
const A = plane(p, { cx: 430, cy: 470, u: 36, x0: -6, x1: 6, y0: -9, y1: 9, t0: m('pts'), labels: false, step: 2 });
const B = plane(p, { cx: 1170, cy: 470, u: 36, x0: -6, x1: 6, y0: -9, y1: 9, t0: m('other'), labels: false, step: 2 });
A.layer = B.layer = p;
const ptsA = [[-4, 8], [-2, 4], [0, 0], [2, -4], [4, -8]], ptsB = [[-5, 3], [-5, 6], [-2, 3], [1, 5], [4, 2], [4, 6]];
const dA = ptsA.map(([x, y], i) => A.dot(x, y, COL.whole, m('pts', .3 + i * .1)));
const at = sweep(p, A, -6.5, 6.5, m('sweep'), 3.4);
ptsA.forEach(([x], i) => { tw(dA[i], { s: 1.6 }, at(x), .15); tw(dA[i], { s: 1 }, at(x) + .15, .3); });
text(p, 'one point at a time: function', 430, 860, m('pass'), { size: 26, fill: OK, anchor: 'middle', weight: 600 });
const dB = ptsB.map(([x, y], i) => B.dot(x, y, COL.whole, m('other', .3 + i * .1)));
const bt = sweep(p, B, -6.5, -5, m('hit', -.8), .8, NO);
const stop = path(p, `M${B.PX(-5)} ${B.PY(9.4)}V${B.PY(-9.4)}`, { stroke: NO, 'stroke-width': 4 }, { o: 0 });
show(stop, bt(-5), .1);
[0, 1].forEach(i => { tw(dB[i], { s: 1.8 }, bt(-5), .2); tw(dB[i], { s: 1.2 }, bt(-5) + .2, .3); });
const s2 = path(p, `M${B.PX(4)} ${B.PY(9.4)}V${B.PY(-9.4)}`, { stroke: NO, 'stroke-width': 4 }, { o: 0 });
show(s2, m('fail', -1));
[4, 5].forEach(i => tw(dB[i], { s: 1.4 }, m('fail', -1), .3));
text(p, 'two points on one vertical line: not a function', 1170, 860, m('fail'), { size: 26, fill: NO, anchor: 'middle', weight: 600 });
}],
['curves', 'Curves', (m) => {
const p = panel(0);
[['para', 'passes', OK, [[1, .5 - 2]]], ['circle', 'fails', NO, [[1, Math.sqrt(2.6 * 2.6 - 1)], [1, -Math.sqrt(2.6 * 2.6 - 1)]]], ['side', 'fails', NO, [[1, Math.sqrt(6)], [1, -Math.sqrt(6)]]]]
.forEach(([k, verdict, col, hits], i) => {
const t0 = m(['para', 'circ', 'side'][i]), P = plane(p, { cx: 300 + i * 500, cy: 430, u: 44, x0: -4, x1: 4, y0: -4, y1: 4, t0, labels: false });
P.layer = p;
draw(path(p, curve(P, ...CURVES[k]), { stroke: COL.task, 'stroke-width': 4 }, { d: 0 }), t0 + .2, 1);
const at = sweep(p, P, -4.3, 1, t0 + 1.2, 1.6, col);
const keep = path(p, `M${P.PX(1)} ${P.PY(4.4)}V${P.PY(-4.4)}`, { stroke: col, 'stroke-width': 4 }, { o: 0 });
show(keep, at(1), .1);
hits.forEach(([x, y]) => P.dot(x, y, col, at(1) + .1));
text(p, verdict, 300 + i * 500, 690, at(1) + .3, { size: 32, fill: col, anchor: 'middle', weight: 700 });
});
text(p, 'Any vertical line crossing twice means not a function.', 800, 800, m('rule'), { size: 30, fill: COL.task, anchor: 'middle', weight: 600 });
}],
['q2', 'Quick check', () => {
const p = panel(0);
S.gr = ['vee', 'wave', 'sideL'].map((k, i) => {
const P = plane(p, { cx: 300 + i * 500, cy: 380, u: 40, x0: -4, x1: 4, y0: -4, y1: 4, t0: .2, labels: false });
draw(path(p, curve(P, ...CURVES[k]), { stroke: COL.task, 'stroke-width': 4 }, { d: 0 }), .4 + i * .2, .8);
text(p, 'ABC'[i], 300 + i * 500, 150, .2, { size: 32, anchor: 'middle', weight: 700 });
return { P, box: [300 + i * 500 - 200, 170, 400, 400] };
});
}, { ask: done => quiz(BAND, [{
id: 'c-vlt', prompt: ['Click the graph that is not a function.'],
build: pickEls(S.gr.map((g, i) => ({ el: null, box: g.box, label: 'Graph ' + 'ABC'[i] })), 2, 'A vertical line through C crosses it twice.',
i => (i === 0 ? 'The V shape passes: every vertical line crosses it once.' : 'The wave goes up and down, but every vertical line still crosses it once.'),
() => { const P = S.gr[2].P, L = S.q || qlayer(); const l = path(L, `M${P.PX(0)} ${P.PY(4.4)}V${P.PY(-4.4)}`, { stroke: NO, 'stroke-width': 4 }, { o: 0 }); fx(l, { o: 1 }, 0, .3); [2, -2].forEach(y => P.dot(0, y, NO, .2, { run: fx, into: L })); }),
}], done) }],
['wrap', 'Summary', (m) => {
const p = panel(0);
[['A relation is a set of ordered pairs: inputs (domain), outputs (range).', 'w1', DOM], ['Function: each input has exactly one output.', 'w2', OK],
['Vertical line test: no vertical line crosses the graph twice.', 'w3', COL.task]].forEach(([str, mark, c], i) => {
const g = G(p, { x: -16, y: 260 + i * 140, o: 0 });
mk('circle', { cx: 160, cy: -12, r: 26, fill: c }, g);
T(g, String(i + 1), { x: 160, y: -1, size: 30, fill: COL.board, weight: 700, anchor: 'middle' });
T(g, str, { x: 220, size: 32, weight: 600 });
tw(g, { o: 1, x: 0 }, m(mark), .6);
});
}],
['final1', 'Chapter practice', () => { panel(0); }, { ask: done => quiz(SCREEN, [{
id: 'p-rel', prompt: ['Is the relation a function?'],
build: grid(RELQ, [['y', 'Function', OK, 'Y'], ['n', 'Not a function', NO, 'N']], { multi: false, text: true }),
}], done, 'Chapter practice 1 of 3') }],
['final2', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-dr', prompt: ['Relation: ', $m('(2, 5), (3, 7), (−1, 5), (0, 4)'), '. List each set from least to greatest.'], build: blanks([
{ parts: ['domain = { ', { box: '-1' }, ', ', { box: '0' }, ', ', { box: '2' }, ', ', { box: '3' }, ' }'], why: 'The inputs are 2, 3, −1 and 0.', hint: 'The domain holds the first number of each pair.' },
{ parts: ['range = { ', { box: '4' }, ', ', { box: '5' }, ', ', { box: '7' }, ' }'], why: 'The outputs are 5, 7, 5 and 4; 5 is listed once.', hint: 'The range holds the second numbers. List a repeated value once.' },
]),
}], done, 'Chapter practice 2 of 3') }],
['final3', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-graph', prompt: ['Does the graph show a function?'],
build: grid(GRAPHQ.map(([k, a, why]) => ({ parts: [miniSvg(k)], ans: [a], why })), [['y', 'Function', OK, 'Y'], ['n', 'Not a function', NO, 'N']], { multi: false, text: true }),
}], done, 'Chapter practice 3 of 3') }],
['finish', 'Finished', () => { panel(0); }, { ask: finishCard }],
].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));
/* ---------- chapter practice data ---------- */
const RELQ = [
{ parts: [$m('(3, 5), (2, 0), (7, 8), (12, 1)')], ans: ['y'], why: 'No x-value repeats.' },
{ parts: [$m('(4, −9), (8, 7), (−5, 2), (8, 0)')], ans: ['n'], why: 'The input 8 has two outputs.' },
{ parts: [$m('(1, 4), (2, 4), (3, 4), (4, 4)')], ans: ['y'], why: 'Outputs may repeat; each input has one output.' },
{ parts: [$m('(0, −5), (8, −7), (0, 5)')], ans: ['n'], why: 'The input 0 has two outputs.' },
{ parts: [$m('(−0.6, 3.7), (4.1, 5.9), (5.9, −2.8)')], ans: ['y'], why: 'No x-value repeats.' },
{ parts: [$m('(2, 1), (1, 2), (2, 2)')], ans: ['n'], why: 'The input 2 has two outputs.' },
];
const GRAPHQ = [
['para', 'y', 'Every vertical line crosses it once.'],
['circle', 'n', 'Vertical lines cross a circle twice.'],
['wave', 'y', 'It goes up and down, but every vertical line crosses it once.'],
['vert', 'n', 'A vertical line has many y-values for one x.'],
['side', 'n', 'A sideways U is crossed twice.'],
];
boot();
</script>
</body>
</html>