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>Unit Rate — 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: 12, title: 'Unit rate', minutes: 5, next: '../ch13/' };
const A = COL.whole, B = COL.nat; // first quantity (top line), second quantity (bottom line)
/* ---------- chapter drawing helpers ---------- */
const caption = (p, str, t0, y = 140, fill = COL.dim, size = 30, x = 800) => show(T(p, str, { x, y, size, fill, weight: 600, anchor: 'middle', o: 0 }), t0);
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; };
// Double number line: two parallel scales whose marks line up. top/bottom: [[value, label]].
function doubleLine(p, { x0 = 220, x1 = 1380, yA = 330, yB = 470, max, labelA, labelB, t0 = .2 }) {
const g = G(p, { o: 0 });
path(g, `M${x0} ${yA}H${x1}`, { stroke: A, 'stroke-width': 5 });
path(g, `M${x0} ${yB}H${x1}`, { stroke: B, 'stroke-width': 5 });
T(g, labelA, { x: x0 - 20, y: yA + 9, size: 26, fill: A, weight: 600, anchor: 'end' });
T(g, labelB, { x: x0 - 20, y: yB + 9, size: 26, fill: B, weight: 600, anchor: 'end' });
show(g, t0);
const X = v => x0 + (x1 - x0) * v / max;
return {
X,
mark(v, la, lb, t, { color = COL.task, strong = false, run = tw } = {}) {
const m = G(p, { o: 0 });
path(m, `M${X(v)} ${yA - 14}V${yB + 14}`, { stroke: color, 'stroke-width': strong ? 3 : 2, 'stroke-dasharray': strong ? '' : '6 6' });
if (la !== null) T(m, la, { x: X(v), y: yA - 26, size: 26, fill: A, font: MATH, anchor: 'middle' });
if (lb !== null) T(m, lb, { x: X(v), y: yB + 44, size: 26, fill: B, font: MATH, anchor: 'middle' });
run(m, { o: 1 }, t, .35);
return m;
},
};
}
function bottle(p, x, y, color) {
const g = G(p, { x, y, o: 0, s: .6 });
mk('path', { d: 'M-8 -60H8V-44Q24 -36 24 -18V40Q24 48 16 48H-16Q-24 48 -24 40V-18Q-24 -36 -8 -44Z', fill: mix(COL.board, color, .45), stroke: color, 'stroke-width': 2.5 }, g);
return g;
}
function notebook(p, x, y, color) {
const g = G(p, { x, y, o: 0, s: .6 });
mk('rect', { x: -20, y: -28, width: 40, height: 56, rx: 4, fill: mix(COL.board, color, .45), stroke: color, 'stroke-width': 2.5 }, g);
path(g, 'M-12 -28V28', { stroke: color, 'stroke-width': 2 });
return g;
}
/* ---------- beats ---------- */
const BEATS = [
['intro', 'Unit rate', (m, D) => {
const g = G(scene, { o: 0, y: 24 });
T(g, 'Unit 3, Chapter 12', { x: 800, y: 330, size: 28, fill: COL.dim, anchor: 'middle' });
T(g, 'Unit rate', { x: 800, y: 430, size: 100, weight: 600, anchor: 'middle' });
tw(g, { o: 1, y: 0 }, .2, 1.1, out);
const r = mline(scene, ['… per 1'], 800, 580, m('q'), COL.dim, 56);
tw(g, { o: 0, y: -24 }, D - .9, .8); hide(r, D - .9, .8);
}],
['rate', 'Rates', (m) => {
const p = panel(0);
mline(p, ['300 kilometers : 20 liters'], 800, 170, m('ex'), COL.chalk, 48);
caption(p, 'different units: a rate', m('units'), 230, COL.task, 28);
const L = doubleLine(p, { max: 20, labelA: 'km', labelB: 'liters', t0: m('bars') });
L.mark(0, '0', '0', m('bars'), { strong: true }); L.mark(20, '300', '20', m('bars', .3), { strong: true });
for (let k = 1; k < 20; k++) L.mark(k, null, null, m('split', k * .08), { color: COL.faint });
const one = L.mark(1, '15', '1', m('one'), { color: COL.good, strong: true });
const hl = rect(p, L.X(0), 300, L.X(1) - L.X(0), 200, COL.good);
hl.setAttribute('fill-opacity', .15); show(hl, m('one'), .4);
mline(p, [F(300, 20), ' = ', F(15, 1), ' = 15 kilometers per liter'], 800, 640, m('def'), COL.good, 46);
caption(p, 'unit rate: first quantity ÷ second quantity', m('how'), 750, COL.task);
}],
['q1', 'Quick check', () => {
const p = panel(0);
S.L = doubleLine(p, { max: 4, labelA: 'pages', labelB: 'minutes', t0: .1 });
S.L.mark(0, '0', '0', .2, { strong: true }); S.L.mark(4, '120', '4', .3, { strong: true });
caption(p, 'A printer prints 120 pages in 4 minutes.', .2, 180, COL.chalk);
}, { ask: done => quiz(BAND, [{
id: 'c-rate', prompt: ['How many pages per minute?'],
build: blanks([{ parts: ['120 ÷ 4 = ', { box: '30' }, ' pages per minute'], why: '120 ÷ 4 = 30.', hint: 'Divide the pages by the minutes.',
fx: () => { for (let k = 1; k < 4; k++) S.L.mark(k, String(30 * k), String(k), (k - 1) * .25, { run: fx, color: COL.good }); } }]),
}], done) }],
['fraction', 'Fractions in a rate', (m) => {
const p = panel(0);
mline(p, [F(1, 2), ' kilometer in ', F(1, 3), ' hour'], 800, 160, m('q'), COL.chalk, 50);
caption(p, 'How far in 1 hour?', m('ask'), 240, COL.dim);
const L = doubleLine(p, { max: 3, labelA: 'km', labelB: 'hours', t0: m('bar') });
L.mark(0, '0', '0', m('bar'), { strong: true });
[1, 2, 3].forEach(k => L.mark(k, ['½', '1', '1½'][k - 1], ['⅓', '⅔', '1'][k - 1], m(k === 1 ? 'fill' : 'three', (k - 2) * .6), { strong: k === 3, color: k === 3 ? COL.good : COL.task }));
mline(p, [F(1, 2), ' ÷ ', F(1, 3), ' = ', F(1, 2), ' × 3 = ', F(3, 2)], 800, 640, m('calc'), COL.chalk, 50);
mline(p, ['= 1', F(1, 2), ' kilometers per hour'], 800, 750, m('ans'), COL.good, 50);
}],
['price', 'Unit price', (m) => {
const p = panel(0);
const bs = [0, 1, 2].map(i => bottle(p, 520 + i * 110, 380, COL.int));
popIn(bs, m('q'));
const tag = G(p, { x: 900, y: 290, o: 0 });
mk('path', { d: 'M0 0H140L160 30L140 60H0Z', fill: COL.chalk }, tag);
T(tag, '$2.70', { x: 70, y: 42, size: 32, fill: COL.board, weight: 700, anchor: 'middle' });
show(tag, m('q', .5));
mline(p, ['$2.70 ÷ 3 = $0.90 per bottle'], 800, 600, m('div'), COL.chalk, 50);
bs.forEach((b, i) => show(T(p, '$0.90', { x: 520 + i * 110, y: 470, size: 26, fill: COL.good, weight: 600, anchor: 'middle', o: 0 }), m('ans', i * .2)));
}],
['compare', 'The better deal', (m) => {
const p = panel(0);
const a = Array.from({ length: 8 }, (_, i) => notebook(p, 220 + (i % 4) * 60, 300 + Math.floor(i / 4) * 80, A));
const b = Array.from({ length: 5 }, (_, i) => notebook(p, 1000 + (i % 3) * 60, 300 + Math.floor(i / 3) * 80, B));
popIn(a, m('q'), .06); popIn(b, m('q2'), .06);
show(T(p, '8 for $40', { x: 310, y: 470, size: 32, fill: A, weight: 600, anchor: 'middle', o: 0 }), m('q', .5));
show(T(p, '5 for $30', { x: 1060, y: 470, size: 32, fill: B, weight: 600, anchor: 'middle', o: 0 }), m('q2', .5));
caption(p, 'Lower total price, but is it the better deal?', m('claim'), 170, COL.chalk);
mline(p, ['40 ÷ 8 = $5 each'], 310, 580, m('u1'), A, 44);
mline(p, ['30 ÷ 5 = $6 each'], 1060, 580, m('u2'), B, 44);
const ring = put(mk('rect', { x: 110, y: 240, width: 400, height: 380, rx: 24, fill: 'none', stroke: COL.good, 'stroke-width': 4 }, p), { o: 0 });
show(ring, m('ans'));
caption(p, '$5 < $6: the pack of 8 is the better deal', m('ans', .5), 720, COL.good, 32);
}],
['q2', 'Quick check', () => { panel(0); }, { ask: done => quiz(BAND, [{
id: 'c-price', prompt: ['6 muffins cost $7.50. What is the unit price?'],
build: blanks([{ parts: ['$7.50 ÷ 6 = $', { box: '1.25' }, ' per muffin'], why: '7.50 ÷ 6 = 1.25.', hint: 'Divide the price by the number of muffins.' }]),
}, {
id: 'c-deal', prompt: ['Which is the better deal?'],
build: choice([['4 muffins for $6'], ['6 muffins for $7.50']], 1, '$6 ÷ 4 = $1.50 each, and $7.50 ÷ 6 = $1.25 each. $1.25 is lower.',
['4 for $6 is $1.50 each. Compare it with $1.25 each.', null]),
}], done) }],
['table', 'Predicting with a unit rate', (m) => {
const p = panel(0);
mline(p, ['18 pages in 3 minutes'], 800, 170, m('q'), COL.chalk, 48);
const L = doubleLine(p, { max: 10, labelA: 'pages', labelB: 'minutes', t0: m('q', .5) });
L.mark(0, '0', '0', m('q', .5), { strong: true });
L.mark(3, '18', '3', m('q', 1), { strong: true });
L.mark(1, '6', '1', m('unit'), { color: COL.good, strong: true });
L.mark(5, '30', '5', m('t'));
L.mark(10, '60', '10', m('t2'));
mline(p, ['pages = 6 × minutes'], 800, 660, m('rule'), COL.task, 46);
}],
['q3', 'Quick check', () => { panel(0); }, { ask: done => quiz(BAND, [{
id: 'c-predict', prompt: ['A car uses 1 liter of fuel every 16 km. How many liters for 240 km?'],
build: blanks([{ parts: ['240 ÷ 16 = ', { box: '15' }, ' liters'], why: 'Each liter covers 16 km, and 240 ÷ 16 = 15.', hint: 'How many 16s fit in 240?' }]),
}], done) }],
['wrap', 'Summary', (m) => {
const p = panel(0);
[['A rate compares quantities with different units.', 'w1'], ['Unit rate: divide to find how much for 1.', 'w1'],
['A unit price is a unit rate for money.', 'w2'], ['Compare unit prices; multiply a unit rate to predict.', 'w3']].forEach(([str, mark], i) => {
const g = G(p, { x: -16, y: 230 + i * 120, o: 0 });
mk('circle', { cx: 200, cy: -12, r: 26, fill: COL.task }, g);
T(g, String(i + 1), { x: 200, y: -1, size: 30, fill: COL.board, weight: 700, anchor: 'middle' });
T(g, str, { x: 260, size: 34, weight: 600 });
tw(g, { o: 1, x: 0 }, m(mark, i === 1 ? 1.5 : 0), .6);
});
}],
['final1', 'Chapter practice', () => { panel(0); }, { ask: done => quiz(SCREEN, [{
id: 'p-rate', prompt: ['Find each unit rate or unit price.'], build: blanks(RATES),
}], done, 'Chapter practice 1 of 3') }],
['final2', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-deal', prompt: ['Which is the better deal?'], build: grid(DEALS, [['a', 'First'], ['b', 'Second']], { multi: false, text: true }),
}], done, 'Chapter practice 2 of 3') }],
['final3', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-tf', prompt: ['True or false?'], build: grid(TFQ, TF, { multi: false, text: true }),
}], done, 'Chapter practice 3 of 3') }],
['finish', 'Finished', () => {}, { ask: finishCard }],
].map(([id, title, run, extra]) => ({ id, title, run, ...extra }));
/* ---------- chapter practice data ---------- */
const RATES = [
{ parts: ['72 liters in 9 minutes: ', { box: '8' }, ' liters per minute'], why: '72 ÷ 9 = 8.' },
{ parts: ['210 meters in 6 seconds: ', { box: '35' }, ' meters per second'], why: '210 ÷ 6 = 35.' },
{ parts: ['$4.80 for 12 pencils: $', { box: '0.4' }, ' per pencil'], why: '4.80 ÷ 12 = 0.40.' },
{ parts: ['45 km in ', F(3, 4), ' hour: ', { box: '60' }, ' km per hour'], why: '45 ÷ 3/4 = 45 × 4/3 = 60.' },
{ parts: [F(2, 3), ' cup of flour for ', F(1, 2), ' batch: ', { box: '4/3' }, ' cups per batch'], why: '2/3 ÷ 1/2 = 2/3 × 2 = 4/3 = 1 1/3.' },
{ parts: ['$39.60 for 12 liters: $', { box: '3.3' }, ' per liter'], why: '39.60 ÷ 12 = 3.30.' },
];
const DEALS = [
{ parts: ['3 kg for $7.50 or 5 kg for $11'], ans: ['b'], why: '$2.50 per kg vs $2.20 per kg.' },
{ parts: ['12 eggs for $3.60 or 18 eggs for $5.94'], ans: ['a'], why: '$0.30 per egg vs $0.33 per egg.' },
{ parts: ['20 balls for $70 or 50 balls for $157'], ans: ['b'], why: '$3.50 per ball vs $3.14 per ball.' },
{ parts: ['2 liters for $3.20 or 5 liters for $8.50'], ans: ['a'], why: '$1.60 per liter vs $1.70 per liter.' },
];
const TFQ = [
{ parts: ['A unit rate compares a quantity to one unit of another quantity.'], ans: ['t'], why: 'Like 15 kilometers per 1 liter.' },
{ parts: ['The pack with the lower total price is always the better deal.'], ans: ['f'], why: 'Compare the price for one item.' },
{ parts: ['If 4 tickets cost $30, one ticket costs $7.50.'], ans: ['t'], why: '30 ÷ 4 = 7.50.' },
{ parts: ['To find kilometers per hour, divide the hours by the kilometers.'], ans: ['f'], why: 'Divide kilometers by hours.' },
{ parts: ['At 6 pages per minute, a printer prints 45 pages in 7.5 minutes.'], ans: ['t'], why: '6 × 7.5 = 45.' },
];
boot();
</script>
</body>
</html>