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>Adding Positive and Negative Numbers — 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: 4, title: 'Adding positive and negative numbers', minutes: 7, next: '../ch05/' };
AXIS.o = 800; AXIS.u = 82; // number line −8…8
/* ---------- chapter drawing helpers ---------- */
const showAll = (els, t0, step = 0) => els.forEach((e, i) => show(e, t0 + i * step, .45));
const row = (p, items, y = 230, size = 64, x = 800) => tokens(p, items, { x, y, size, anchor: 'middle', o: 0 });
const caption = (p, str, t0, y = 170, fill = COL.dim, size = 30) => show(T(p, str, { x: 800, y, size, fill, weight: 600, anchor: 'middle', o: 0 }), t0);
/* ---------- beats ---------- */
const BEATS = [
['intro', 'Adding positive and negative numbers', (m, D) => {
const g = G(scene, { o: 0, y: 24 });
T(g, 'Unit 2, Chapter 4', { x: 800, y: 330, size: 28, fill: COL.dim, anchor: 'middle' });
T(g, 'Adding positive and negative numbers', { x: 800, y: 420, size: 68, weight: 600, anchor: 'middle' });
tw(g, { o: 1, y: 0 }, .2, 1.1, out);
const ex = G(scene);
[['−12 °C', 440, .9], ['−$40', 800, 2.4], ['−30 m', 1160, 3.9]].forEach(([s, x, t]) => show(M(ex, [s], { x, y: 580, size: 48, fill: NEG, o: 0 }), t));
show(M(ex, ['+ ?'], { x: 800, y: 680, size: 40, fill: POS, o: 0 }), m('q'));
tw(g, { o: 0, y: -24 }, D - .9, .8); hide(ex, D - .9, .8);
}],
['signs', 'Positive and negative', (m) => {
const p = panel(0);
numberLine(.1);
for (const [a, b, c, name] of [[0, 8.6, POS, 'positive'], [0, -8.6, NEG, 'negative']]) {
const g = G(p, { o: 0 });
mk('rect', { x: Math.min(X(a), X(b)), y: Y0 - 26, width: Math.abs(X(b) - X(a)), height: 52, rx: 8, fill: c, 'fill-opacity': .16 }, g);
T(g, name, { x: X((a + b) / 2), y: Y0 - 60, size: 30, fill: c, weight: 600, anchor: 'middle' });
show(g, .9);
}
landDot(p, 2, m('plus'), POS);
show(M(p, ['+2 = 2'], { x: X(2), y: 330, size: 60, fill: POS, o: 0 }), m('plus', .3));
landDot(p, -6, m('neg'), NEG);
show(M(p, ['−6'], { x: X(-6), y: 330, size: 60, fill: NEG, o: 0 }), m('neg', .3));
}],
['lineRule', 'Adding on a number line', (m) => {
const p = panel(0);
caption(p, 'Start at zero.', .3, 170, COL.chalk);
landDot(p, 0, .5);
caption(p, 'Positive: move right', m('pos'), 240, POS);
move(p, 0, 3, Y0 - 90, m('pos', .4));
caption(p, 'Negative: move left', m('neg'), 300, NEG);
move(p, 0, -4, Y0 - 160, m('neg', .4));
caption(p, 'Where you stop is the sum.', m('end'), 380, COL.chalk);
}],
['ex1', '5 + (−3)', (m) => {
const p = panel(0);
const E = row(p, [{ t: '5', fill: POS }, ' + ', { t: '(−3)', fill: NEG }, ' = ', '2']);
showAll(E.slice(0, 3), .2); show(E[3], m('land')); show(E[4], m('land'));
move(p, 0, 5, Y0 - 90, m('m1'));
move(p, 5, 2, Y0 - 170, m('m2'));
landDot(p, 2, m('land'));
}],
['ex2', '(−1) + (−4)', (m) => {
const p = panel(0);
const E = row(p, [{ t: '(−1)', fill: NEG }, ' + ', { t: '(−4)', fill: NEG }, ' = ', '−5']);
showAll(E.slice(0, 3), .2); show(E[3], m('land')); show(E[4], m('land'));
move(p, 0, -1, Y0 - 90, m('m1'));
move(p, -1, -5, Y0 - 170, m('m2'));
landDot(p, -5, m('land'));
}],
['ex3', '5 + (−7)', (m) => {
const p = panel(0);
const E = row(p, [{ t: '5', fill: POS }, ' + ', { t: '(−7)', fill: NEG }, ' = ', '−2']);
showAll(E.slice(0, 3), .2); show(E[3], m('land')); show(E[4], m('land'));
move(p, 0, 5, Y0 - 90, m('m1'));
move(p, 5, -2, Y0 - 170, m('m2'));
const ring = G(p, { x: X(0), y: Y0, o: 0 });
mk('circle', { r: 22, fill: 'none', stroke: COL.task, 'stroke-width': 3 }, ring);
show(ring, m('cross'), .3); pulse(ring, m('cross', .3), 1.4); hide(ring, m('land'), .3);
landDot(p, -2, m('land'));
}],
['q1', 'Quick check', () => {
const p = panel(0);
showAll(row(p, [{ t: '−2', fill: NEG }, ' + ', { t: '6', fill: POS }]), .2);
}, { ask: done => quiz(TOPR, [{
id: 'c-line', prompt: ['Tap the point where ', $m('−2 + 6'), ' lands.'],
build: tap(Array.from({ length: 17 }, (_, i) => i - 8), 4, 'Start at 0, move 2 left to −2, then 6 right. You stop at 4.',
v => `You picked ${num(v)}. Start at 0, move 2 to the left, then 6 to the right.`,
() => { move(S.q, 0, -2, Y0 - 90, 0, fx); move(S.q, -2, 4, Y0 - 170, .8, fx); }),
}], done) }],
['opposites', 'Opposites', (m) => {
const p = panel(0);
const E = row(p, [{ t: '8', fill: POS }, ' + ', { t: '(−8)', fill: NEG }, ' = ', '0']);
landDot(p, 8, m('ex'), POS); landDot(p, -8, m('ex'), NEG);
brace(p, 0, 8, Y0 + 90, '8 units', POS, m('ex', .6)); brace(p, -8, 0, Y0 + 90, '8 units', NEG, m('ex', .6));
showAll(E.slice(0, 3), m('m1'));
move(p, 0, 8, Y0 - 90, m('m1'));
move(p, 8, 0, Y0 - 170, m('m2'));
landDot(p, 0, m('land'));
show(E[3], m('land')); show(E[4], m('land'));
caption(p, 'A number plus its opposite is 0.', m('rule'), 340, COL.chalk);
}],
['tiles', 'Plus and minus tiles', (m) => {
hide(S.line, 0, .5);
const p = panel(0);
const a = tile(p, 1, 620, 300), b = tile(p, -1, 980, 300);
tw(a, { o: 1, s: 1 }, m('show'), .4, back); tw(b, { o: 1, s: 1 }, m('show', .5), .4, back);
const la = M(p, ['+1'], { x: 620, y: 380, size: 34, fill: POS, o: 0 }), lb = M(p, ['−1'], { x: 980, y: 380, size: 34, fill: NEG, o: 0 });
show(la, m('show', .3)); show(lb, m('show', .8));
tw(a, { x: 764 }, m('pair'), .8); tw(b, { x: 836 }, m('pair'), .8); hide(la, m('pair'), .3); hide(lb, m('pair'), .3);
const zero = M(p, ['= 0'], { x: 800, y: 385, size: 40, o: 0 });
show(zero, m('pair', 1));
for (const e of [a, b, zero]) hide(e, m('ex'), .4);
const E = row(p, [{ t: '5', fill: POS }, ' + ', { t: '(−3)', fill: NEG }, ' = ', '2'], 200, 56);
showAll(E.slice(0, 3), m('ex'));
const plus = tiles(p, 5, 1, 520, 420), minus = tiles(p, 3, -1, 520, 520);
popAll(plus, m('ex', .8)); popAll(minus, m('ex', 1.6));
cancelPairs(p, plus.slice(0, 3), minus, m('cancel'));
for (const e of plus.slice(3)) pulse(e, m('left'), 1.25);
show(E[3], m('left')); show(E[4], m('left'));
}],
['absValue', 'Absolute value', (m) => {
show(S.line, 0, .5);
const p = panel(0);
caption(p, 'Absolute value = distance from zero', m('dist'), 170, COL.chalk);
landDot(p, -6, m('ex1'), NEG);
brace(p, -6, 0, Y0 + 90, '6 units', NEG, m('ex1', .4));
show(M(p, ['|−6| = 6'], { x: X(-3.5), y: 360, size: 60, fill: NEG, o: 0 }), m('bars'));
landDot(p, 6, m('ex2'), POS);
brace(p, 0, 6, Y0 + 90, '6 units', POS, m('ex2', .4));
show(M(p, ['|6| = 6'], { x: X(3.5), y: 360, size: 60, fill: POS, o: 0 }), m('ex2', .6));
caption(p, 'Never negative: |0| = 0', m('never'), 250, COL.dim, 28);
}],
['sameSign', 'Same signs', (m) => {
hide(S.line, 0, .5);
const p = panel(0);
caption(p, 'Same signs', m('rule'), 150, COL.chalk, 36);
caption(p, 'Add the absolute values. Keep the sign.', m('do'), 205, COL.dim, 28);
const E = row(p, [{ t: '(−6)', fill: NEG }, ' + ', { t: '(−8)', fill: NEG }, ' = ', { t: '−14', fill: NEG }], 330, 60);
showAll(E.slice(0, 3), m('ex'));
const a = tiles(p, 6, -1, 300, 470, 64), b = tiles(p, 8, -1, 720, 470, 64);
popAll(a, m('tiles')); popAll(b, m('tiles', .6));
const bl = (x1, x2, lab, t0) => {
draw(path(p, `M${x1} 520V530H${x2}V520`, { stroke: NEG, 'stroke-width': 3 }, { d: 0 }), t0, .5);
show(T(p, lab, { x: (x1 + x2) / 2, y: 568, size: 28, fill: NEG, font: MATH, anchor: 'middle', o: 0 }), t0 + .3);
};
bl(273, 647, '6', m('tiles', 1.4)); bl(693, 1195, '8', m('tiles', 1.8));
show(E[3], m('ans')); show(E[4], m('ans'));
show(M(p, ['6 + 8 = 14, keep the −'], { x: 800, y: 650, size: 36, fill: COL.dim, o: 0 }), m('ans', 1));
}],
['diffSign', 'Different signs', (m) => {
const p = panel(0);
caption(p, 'Different signs', .2, 130, COL.chalk, 36);
caption(p, 'Subtract the absolute values.', m('do'), 180, COL.dim, 28);
caption(p, 'Use the sign of the number farther from zero.', m('sign'), 225, COL.dim, 28);
const E = row(p, [{ t: '(−11)', fill: NEG }, ' + ', { t: '5', fill: POS }, ' = ', { t: '−6', fill: NEG }], 320, 60);
showAll(E.slice(0, 3), m('ex'));
const minus = tiles(p, 11, -1, 450, 520, 64), plus = tiles(p, 5, 1, 450, 430, 64);
popAll(minus, m('ex', .5), .05); popAll(plus, m('ex', 1.2));
cancelPairs(p, plus, minus.slice(0, 5), m('tiles'));
for (const e of minus.slice(5)) pulse(e, m('left', .3), 1.2);
show(M(p, ['|−11| − |5| = 11 − 5 = 6'], { x: 800, y: 650, size: 38, fill: COL.dim, o: 0 }), m('calc'));
show(E[3], m('calc', 2.5)); show(E[4], m('calc', 2.5));
}],
['q2', 'Quick check', () => {
const p = panel(0);
showAll(row(p, [{ t: '−3', fill: NEG }, ' + ', { t: '10', fill: POS }], 340, 80), .2);
}, { ask: done => quiz(BAND, [{
id: 'c-sign', prompt: ['Is ', $m('−3 + 10'), ' positive, negative, or zero?'],
build: choice([['Positive'], ['Negative'], ['Zero']], 0, '10 is farther from zero than −3, so the sum is positive.',
[null, 'Which number is farther from zero: −3 or 10?', 'Only opposites add to zero, and −3 and 10 are not opposites.']),
}, {
id: 'c-diff', prompt: ['Now find the sum.'],
build: blanks([{ parts: ['−3 + 10 = ', { box: '7' }], why: '10 − 3 = 7, and the sign is positive.', hint: 'Subtract the absolute values: 10 − 3.' }]),
}], done) }],
['story', 'An elevator', (m) => {
const p = panel(0);
const Yf = v => 520 - 40 * v, ax = 360;
vAxis(p, { x: ax, Yf, lo: -5, hi: 8 });
show(path(p, `M${ax - 70} ${Yf(0)}H${ax + 300}`, { stroke: COL.faint, 'stroke-width': 2, 'stroke-dasharray': '8 8' }, { o: 0 }), .6);
show(T(p, 'ground floor', { x: ax + 300, y: Yf(0) - 10, size: 22, fill: COL.dim, anchor: 'end', o: 0 }), .6);
const lift = G(p, { x: ax, y: Yf(0), o: 0 });
mk('rect', { x: -30, y: -20, width: 60, height: 40, rx: 8, fill: mix(COL.board, COL.chalk, .25), stroke: COL.chalk, 'stroke-width': 2.5 }, lift);
show(lift, .8);
const vmove = (a, b, x, t0) => {
const d = Math.sign(b - a), c = d > 0 ? POS : NEG;
draw(path(p, `M${x} ${Yf(a)}V${Yf(b)}`, { stroke: c, 'stroke-width': 5 }, { d: 0 }), t0, .8);
show(path(p, `M${x - 10} ${Yf(b) + 14 * d}L${x} ${Yf(b)}L${x + 10} ${Yf(b) + 14 * d}`, { stroke: c, 'stroke-width': 5 }, { o: 0 }), t0 + .7, .15);
show(T(p, (d > 0 ? '+' : '−') + Math.abs(b - a), { x: x + 16, y: (Yf(a) + Yf(b)) / 2 + 9, size: 26, fill: c, font: MATH, o: 0 }), t0 + .3);
tw(lift, { y: Yf(b) }, t0, .8 + Math.abs(b - a) * .08);
};
vmove(0, 5, ax + 60, m('up5')); vmove(5, 7, ax + 110, m('up2')); vmove(7, -4, ax + 160, m('down'));
let E = row(p, [{ t: '5', fill: POS }, ' + ', { t: '2', fill: POS }, ' + ', { t: '(−11)', fill: NEG }], 330, 56, 1080);
showAll(E, m('expr'), .5);
E = collapse(E, 0, 2, ['7'], m('s1'), { color: POS });
E = collapse(E, 0, 2, ['−4'], m('s2'), { color: NEG });
show(T(p, '4 floors below the ground floor', { x: 1080, y: 440, size: 30, fill: COL.good, weight: 600, anchor: 'middle', o: 0 }), m('ans'));
}],
['q3', 'Quick check', () => {
const p = panel(0);
S.th = thermometer(p, { x: 800, value: -5 });
const Yt = S.th.Yt, x = 800;
show(T(p, '6 a.m.', { x: x + 40, y: Yt(-5) + 9, size: 24, fill: NEG }), .3);
}, { ask: done => quiz(TOPR, [{
id: 'c-temp', prompt: ['At 6 a.m. it is −5 °C. By noon it is 12 degrees warmer. What is the temperature at noon?'],
build: blanks([{ parts: ['−5 + 12 = ', { box: '7' }], why: '12 − 5 = 7, and 12 is farther from zero, so it is 7 °C.',
hint: 'The signs are different: subtract the absolute values, then choose the sign.',
fx: () => S.th.set(7, 0, fx, 1.4) }]),
}], done) }],
['wrap', 'Summary', (m) => {
const p = panel(0);
[['Same signs: add, and keep the sign.', '(−6) + (−8) = −14', 'w1'],
['Different signs: subtract, and use the sign of the number farther from zero.', '(−11) + 5 = −6', 'w2'],
['A number plus its opposite is zero.', '8 + (−8) = 0', 'w3']].forEach(([str, ex, mark], i) => {
const g = G(p, { x: -16, y: 220 + i * 170, o: 0 });
T(g, str, { x: 200, size: 34, weight: 600 });
M(g, [ex], { x: 200, y: 60, size: 40, fill: COL.dim, anchor: 'start' });
tw(g, { o: 1, x: 0 }, m(mark), .6);
});
}],
['final1', 'Chapter practice', () => { panel(0); hide(S.line, 0, .3); }, { ask: done => quiz(SCREEN, [{
id: 'p-sums', prompt: ['Find each sum.'], build: blanks(SUMS),
}], done, 'Chapter practice 1 of 3') }],
['final2', 'Chapter practice', () => {}, { ask: done => quiz(SCREEN, [{
id: 'p-sign', prompt: ['Is each sum positive, negative, or zero?'],
build: grid(SIGNQ, [['p', 'Positive', POS], ['n', 'Negative', NEG], ['z', 'Zero', COL.chalk]], { 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 SUMS = [
{ parts: ['9 + (−4) = ', { box: '5' }], why: '9 − 4 = 5, and 9 is farther from zero.' },
{ parts: ['−8 + 3 = ', { box: '-5' }], why: '8 − 3 = 5, and −8 is farther from zero, so −5.' },
{ parts: ['−7 + (−6) = ', { box: '-13' }], why: 'Same signs: 7 + 6 = 13, keep the −.' },
{ parts: ['−4 + 11 = ', { box: '7' }], why: '11 − 4 = 7, and 11 is farther from zero.' },
{ parts: ['−12 + (−9) = ', { box: '-21' }], why: 'Same signs: 12 + 9 = 21, keep the −.' },
{ parts: ['6 + (−15) = ', { box: '-9' }], why: '15 − 6 = 9, and −15 is farther from zero, so −9.' },
{ parts: ['14 + (−14) = ', { box: '0' }], why: 'A number plus its opposite is zero.' },
];
const SIGNQ = [
{ parts: [$m('−20 + 7')], ans: ['n'], why: '−20 is farther from zero.' },
{ parts: [$m('15 + (−3)')], ans: ['p'], why: '15 is farther from zero.' },
{ parts: [$m('−6 + (−1)')], ans: ['n'], why: 'Two negatives make a negative.' },
{ parts: [$m('−9 + 9')], ans: ['z'], why: '−9 and 9 are opposites.' },
{ parts: [$m('−2 + 30')], ans: ['p'], why: '30 is farther from zero.' },
{ parts: [$m('11 + (−13)')], ans: ['n'], why: '−13 is farther from zero.' },
];
const TFQ = [
{ parts: ['The sum of two negative numbers is always negative.'], ans: ['t'], why: 'Both move left, so you end up left of zero.' },
{ parts: [$m('−8 + 5 = 3')], ans: ['f'], why: '−8 is farther from zero, so the sum is −3.' },
{ parts: ['A number plus its opposite is zero.'], ans: ['t'], why: 'You move out and come straight back to zero.' },
{ parts: [$m('|−7| = −7')], ans: ['f'], why: 'An absolute value is a distance, so |−7| = 7.' },
{ parts: ['With different signs, the sum takes the sign of the number farther from zero.'], ans: ['t'], why: 'That number pulls harder.' },
{ parts: [$m('−4 + (−4) = 0')], ans: ['f'], why: '−4 and −4 are not opposites: the sum is −8.' },
];
boot();
</script>
</body>
</html>