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>Ratios — Elementary Mathematics</title><link rel="stylesheet" href="../lib/engine.css"><link rel="expect" href="#bar" blocking="render"><style>.ratio-inputs .bline{gap:12px}.ratio-inputs .rlabel{font-size:27px;min-width:310px}.ratio-inputs .box{width:6ch!important}.ratio-inputs .why{font-size:20px}.band .prompt{font-size:27px}.side .keys{font-size:19px}</style></head><body>
<script src="../lib/engine.js"></script><script src="audio/en/timings.js"></script><script>
'use strict';
const CHAPTER={number:15,title:'Ratios',minutes:9,language:'en', next: '../ch16/' };
const rgC={blue:COL.whole,coral:COL.nat,white:COL.chalk};
const rgText=(p,s,x,y,t=0,size=30,col=COL.chalk,anchor='start')=>{const e=T(p,s,{x,y,size,fill:col,anchor,o:0});show(e,t);return e;};
const rgMath=(p,s,x,y,t=0,size=52,col=COL.chalk)=>{const e=M(p,Array.isArray(s)?s:[s],{x,y,size,fill:col,anchor:'middle',o:0});show(e,t);return e;};
const rgTitle=(p,s,sub='')=>{rgText(p,s,100,110,.1,43);if(sub)rgText(p,sub,100,165,.15,27,COL.dim);};
function rgBead(p,color,id,x,y){const g=G(p,{x,y});g.dataset.beadId=id;g.dataset.kind=color;mk('circle',{r:19,fill:rgC[color],stroke:COL.board,'stroke-width':2},g);mk('circle',{r:5,fill:COL.board},g);return g;}
const rgData=[...Array(6).fill('blue'),...Array(9).fill('coral'),...Array(3).fill('white')];
function rgBag(p,layout='rows'){return rgData.map((kind,i)=>{const n=kind==='blue'?i:kind==='coral'?i-6:i-15;const [x,y]=layout==='bag'?[592+i%6*100,330+Math.floor(i/6)*110]:[560+n*70,300+['blue','coral','white'].indexOf(kind)*135];return rgBead(p,kind,'bag-'+i,x,y);});}
function rgRowLabels(p,t=0,kinds=['blue','coral','white']){kinds.forEach((kind,i)=>{rgText(p,kind+' beads',490,310+135*i,t,31,rgC[kind],'end');rgText(p,String({blue:6,coral:9,white:3}[kind]),1230,315+135*i,t,46,rgC[kind]);});}
// A drawn bag: a sack outline gathered and tied at the top centre.
function rgSack(p,L,top,R,bottom,stroke=COL.dim){const cx=(L+R)/2,g=G(p),a={stroke,'stroke-width':3};path(g,`M${cx-45} ${top}C${cx-130} ${top+10} ${L} ${top+5} ${L} ${top+70}L${L} ${bottom-50}Q${L} ${bottom} ${L+50} ${bottom}L${R-50} ${bottom}Q${R} ${bottom} ${R} ${bottom-50}L${R} ${top+70}C${R} ${top+5} ${cx+130} ${top+10} ${cx+45} ${top}`,a);path(g,`M${cx-45} ${top}Q${cx} ${top-14} ${cx+45} ${top}M${cx-6} ${top-8}Q${cx-40} ${top-48} ${cx-58} ${top-30}M${cx+6} ${top-8}Q${cx+40} ${top-48} ${cx+58} ${top-30}`,a);return g;}
const rgPair=(p,parts,x,y,t=0,size=36,anchor='middle')=>{const e=T(p,'',{x,y,size,anchor,o:0});parts.forEach(([s,c])=>{mk('tspan',{fill:c},e).textContent=s;});show(e,t);return e;};
function rgRegroup(p,t=0){const beads=rgBag(p);const trays=[];for(let j=0;j<3;j++){const x=330+j*450,g=G(p,{o:0});mk('rect',{x:x-135,y:255,width:270,height:290,rx:20,fill:'none',stroke:COL.dim,'stroke-width':2},g);T(g,'tray '+(j+1),{x,y:595,size:26,anchor:'middle',fill:COL.dim});show(g,t,.5);trays.push(g);}beads.forEach((b,i)=>{const kind=rgData[i],n=kind==='blue'?i:kind==='coral'?i-6:i-15,per=kind==='blue'?2:kind==='coral'?3:1,j=Math.floor(n/per),k=n%per;tw(b,{x:330+j*450+(k-(per-1)/2)*65,y:kind==='blue'?310:kind==='coral'?410:500},t,2,io);});return {beads,trays};}
function rgPattern(p,x,y,b,c,id){const g=G(p);for(let i=0;i<b;i++)rgBead(g,'blue',id+'-b'+i,x+(i-(b-1)/2)*50,y);for(let i=0;i<c;i++)rgBead(g,'coral',id+'-c'+i,x+(i-(c-1)/2)*50,y+95);rgText(g,b+' blue : '+c+' coral',x,y+175,0,31,COL.chalk,'middle');return g;}
function rgMap(p,x,y,n,key=4){const g=G(p),w=80;g.dataset.mapUnits=n;g.dataset.realUnits=n*key;for(let j=0;j<n;j++){mk('rect',{x:x+j*w,y,width:w,height:65,fill:mix(COL.board,COL.whole,.3),stroke:COL.whole,'stroke-width':2},g);T(g,'1 cm',{x:x+(j+.5)*w,y:y+40,size:22,anchor:'middle',fill:COL.whole});mk('rect',{x:x+j*w,y:y+140,width:w,height:65,fill:mix(COL.board,COL.nat,.3),stroke:COL.nat,'stroke-width':2},g);T(g,key+' km',{x:x+(j+.5)*w,y:y+180,size:22,anchor:'middle',fill:COL.nat});}rgText(g,'map',x+n*w/2,y-25,0,25,COL.whole,'middle');rgText(g,'real distance',x+n*w/2,y+250,0,25,COL.nat,'middle');return g;}
// Format-aware exact grading: a fraction must be n/d; ratio counts must be positive integers.
function rgInputOK(values,answers,kind){const re=kind==='fraction'?/^\s*(\d+)\s*\/\s*(\d+)\s*$/:/^\s*\d+\s*$/;let ns;if(kind==='fraction'){const q=String(values[0]).match(re);if(!q)return false;ns=[+q[1],+q[2]];}else{if(!values.every(v=>re.test(String(v))))return false;ns=values.map(Number);}if(!ns.every(n=>Number.isSafeInteger(n)&&n>0)||gcd(...ns)!==1)return false;return BigInt(ns[0])*BigInt(answers[1])===BigInt(answers[0])*BigInt(ns[1]);}
const rgInputs=rows=>(body,api)=>{body.classList.add('blanks','ratio-inputs');const built=rows.map(it=>{const line=h('div','bline'),lab=h('span','rlabel',[it.label]),mark=h('span','mark'),why=h('div','why');line.append(lab);const boxes=(it.kind==='fraction'?[it.answers.join('/')]:it.answers.map(String)).map((ans,i)=>{if(i)line.append(mathEl([' : '],34));const b=h('input','box');Object.assign(b,{type:'text',inputMode:'text',autocomplete:'off',spellcheck:false});b.dataset.ans=ans;b.setAttribute('aria-label',it.label+(it.kind==='fraction'?' fraction':i?' second count':' first count'));line.append(b);return b;});line.append(mark);body.append(h('div','brow',[line,why]));return {it,boxes,mark,why};});const judge=r=>{const ok=rgInputOK(r.boxes.map(b=>b.value),r.it.answers,r.it.kind);r.boxes.forEach(b=>{b.classList.toggle('good',ok);b.classList.toggle('bad',!ok)});r.mark.className='mark '+(ok?'good':'bad');r.mark.textContent=ok?'✓':'✗';r.why.replaceChildren(ok?'':r.it.hint);return ok;};const all=built.flatMap(r=>r.boxes);requestAnimationFrame(()=>all[0]?.focus({preventScroll:true}));return {check(){const right=built.filter(judge).length;api.grade(right===built.length,right===built.length?'The order, quantities, and lowest terms are correct.':`${right} of ${built.length} right. Read the row hints.`,{right,total:built.length});},reveal(){built.forEach(r=>{r.boxes.forEach(b=>b.value=b.dataset.ans);judge(r);r.why.replaceChildren(r.it.why)});return 'The ratios in lowest terms are shown.';},lock(){all.forEach(b=>b.disabled=true)},hint:['type ',rows[0].kind==='fraction'?'a fraction like 2/5':'positive whole-number counts',', ',kbd('Tab'),' next box, ',kbd('Enter'),' check, ',kbd('Esc'),' leave box']};};
const BEATS=[
['intro','Ratios',(m,D)=>{const g=G(scene,{o:0,y:24});T(g,'Unit 2 · Ratios, Proportions, and Percents · Chapter 15',{x:800,y:225,size:28,fill:COL.dim,anchor:'middle'});T(g,'Ratios',{x:800,y:370,size:90,weight:600,anchor:'middle'});M(g,['6 : 9'],{x:800,y:540,size:90,anchor:'middle',fill:COL.task});tw(g,{o:1,y:0},.2,1,out);rgText(g,'Compare two quantities · keep their order',800,700,m('sub'),36,COL.dim,'middle');hide(g,D-.8,.7);}],
['compare','An ordered comparison',m=>{const p=panel(0);rgTitle(p,'One bag · eighteen beads');rgSack(p,515,235,1170,640);const beads=rgBag(p,'bag');beads.forEach((b,i)=>{const k=rgData[i],n=k==='blue'?i:k==='coral'?i-6:i-15;tw(b,{x:560+n*70,y:300+['blue','coral','white'].indexOf(k)*135},m('separate'),2,io);});rgRowLabels(p,m('counts'));rgMath(p,'blue : coral = 6 : 9',842,750,m('ratio'),57);rgText(p,'The white beads stay in the bag but are not compared.',842,835,m('ratio',9.7),29,COL.dim,'middle');S.rgBag=beads;}],
['order','Order names the quantities',m=>{const p=panel(0);rgTitle(p,'Blue to coral ≠ coral to blue');const a=G(p,{x:500,y:345}),b=G(p,{x:1100,y:345});M(a,['6'],{size:88,anchor:'middle',fill:rgC.blue});T(a,'blue beads',{y:80,size:34,anchor:'middle',fill:rgC.blue});M(b,['9'],{size:88,anchor:'middle',fill:rgC.coral});T(b,'coral beads',{y:80,size:34,anchor:'middle',fill:rgC.coral});rgMath(p,':',800,345,0,88);rgText(p,'first quantity',500,220,m('first'),29,COL.dim,'middle');rgText(p,'second quantity',1100,220,m('first'),29,COL.dim,'middle');tw(a,{y:285},m('swap'),.5);tw(b,{y:465},m('swap'),.5);tw(a,{x:1100},m('swap',.5),1.5);tw(b,{x:500},m('swap',.5),1.5);tw(a,{y:345},m('swap',2),.5);tw(b,{y:345},m('swap',2),.5);rgMath(p,'coral : blue = 9 : 6',800,705,m('reverse'),57);rgText(p,'Same bag · different ordered comparison',800,815,m('reverse',2),34,COL.dim,'middle');S.rgSwap={a,b};}],
['notation','Three notations, one comparison',m=>{const p=panel(0);rgTitle(p,'Blue beads compared with coral beads');['blue','coral'].forEach((k,j)=>{for(let n=0;n<(j?9:6);n++)rgBead(p,k,'notation-'+k+n,560+n*70,300+j*135);});rgRowLabels(p,0,['blue','coral']);rgMath(p,'6 : 9',350,590,m('colon'),66);rgMath(p,'6 to 9',800,590,m('words'),66);rgMath(p,[F(6,9)],1250,600,m('fraction'),100);rgText(p,'first: blue count',1250,725,m('roles'),31,rgC.blue,'middle');rgText(p,'second: coral count',1250,772,m('roles',1),31,rgC.coral,'middle');rgText(p,'Denominator = coral count, not total',100,845,m('roles',6.7),33,COL.task);}],
['whole','Part to part; part to whole',m=>{const p=panel(0);rgTitle(p,'Which second quantity?','Same beads · a different comparison');const sack=rgSack(p,515,235,1170,640);S.rgBag=rgBag(p);rgRowLabels(p);sack.querySelectorAll('path').forEach(e=>tw(e,{c_stroke:COL.task,'a_stroke-width':5},m('all'),.6));rgText(p,'ALL = 6 + 9 + 3 = 18',842,692,m('all'),34,COL.task,'middle');rgMath(p,[F(6,9)],330,815,m('difference'),100);rgPair(p,[['blue',rgC.blue],[' : ',COL.chalk],['coral',rgC.coral]],400,797,m('difference'),38,'start');rgMath(p,[F(6,18)],1130,815,m('whole'),100);rgPair(p,[['blue',rgC.blue],[' : ',COL.chalk],['all',COL.task]],1215,797,m('whole'),38,'start');}],
['q1','Choose the denominator',()=>{const p=panel(0);rgTitle(p,'Blue beads : ALL beads','Choose the second quantity.');S.rgChoices=[[rgData.slice(0,6),3,'6 blue beads'],[rgData.slice(6,15),3,'9 coral beads'],[rgData,6,'all 18 beads']].map(([kinds,cols,label],i)=>{const cx=350+i*450,g=G(p),rows=Math.ceil(kinds.length/cols);kinds.forEach((k,j)=>rgBead(g,k,'q1-'+i+'-'+j,cx+(j%cols-(cols-1)/2)*58,410+(Math.floor(j/cols)-(rows-1)/2)*62));if(i===2)rgSack(g,cx-190,290,cx+190,530);rgText(g,kinds.length+' beads',cx,610,0,40,i===2?COL.chalk:rgC[kinds[0]],'middle');return {el:g,box:[cx-205,235,410,410],label};});},{ask:done=>quiz(BAND,[{id:'c-denominator',prompt:'For blue : all, which group supplies the denominator?',build:pickEls(S.rgChoices,2,'All eighteen beads. The denominator is the second named quantity.',i=>i===0?'Blue is the first quantity: it belongs in the numerator.':'Nine counts only coral beads. The question names ALL beads as its second quantity.')}],done)}],
['group','Find a repeated pattern',m=>{const p=panel(0);rgTitle(p,'Regroup the same eighteen beads','No beads added · no beads removed');S.rgGroups=rgRegroup(p,m('trays'));rgText(p,'Each tray: 2 blue + 3 coral + 1 white',800,680,m('each'),36,COL.chalk,'middle');rgMath(p,'blue : coral = 2 : 3',800,770,m('same'),57);rgText(p,'Three copies give the full bag: 6 blue : 9 coral.',800,845,m('same',2),29,COL.dim,'middle');}],
['simplify','Divide both counts',m=>{const p=panel(0);rgTitle(p,'Same repeated pattern · smaller counts');S.rgGroups=rgRegroup(p,-3);rgMath(p,'6 ÷ 3 : 9 ÷ 3',800,670,m('divide'),61);rgMath(p,'2 : 3',800,770,m('reduced'),65,COL.task);rgText(p,'GCF(6, 9) = 3 · 2 and 3 share only factor 1',800,855,m('lowest'),31,COL.dim,'middle');}],
['mistake','One divisor for both quantities',m=>{const p=panel(0);rgTitle(p,'Simplify BOTH counts; keep their order');[[400,9,'wrong'],[1200,3,'both']].forEach(([x,c,k])=>{const g=rgPattern(p,x,280,2,c,k+'-pattern');put(g,{o:0});show(g,m(k));});rgText(p,'6 ÷ 3 : 9 → 2 : 9',430,600,m('wrong'),40,COL.chalk,'middle');rgText(p,'✗',185,604,m('wrong',.6),56,COL.bad,'middle');rgText(p,'6 ÷ 3 : 9 ÷ 3 → 2 : 3',1230,600,m('both'),40,COL.task,'middle');rgText(p,'✓',945,604,m('both',.6),56,COL.good,'middle');rgText(p,'blue stays first · coral stays second',800,790,m('order'),39,COL.chalk,'middle');}],
['q2','Choose the same comparison',()=>{const p=panel(0);rgTitle(p,'Original bag: 6 blue beads : 9 coral beads','Choose the repeated pattern with the same ordered comparison.');S.rgChoices=[[2,3],[2,9],[3,2]].map(([b,c],i)=>{const x=300+i*500,g=rgPattern(p,x,300,b,c,'choice-'+i);return {el:g,box:[x-230,230,460,290],label:b+' blue beads to '+c+' coral beads'};});},{ask:done=>quiz(BAND,[{id:'c-pattern',prompt:'Which picture preserves blue : coral = 6 : 9?',build:pickEls(S.rgChoices,0,'Two blue to three coral. Three copies give six blue to nine coral.',i=>i===1?'Only the blue count was divided. Nine coral needs to be divided by three too.':'The counts were reversed. Blue comes first, so we need two blue to three coral.')}],done)}],
['scale','A map compares labelled distances',m=>{const p=panel(0);rgTitle(p,'A map key');const key=G(p);rgText(key,'Map key: 1 cm on the map means 4 km in the real world',800,250,m('key'),38,COL.task,'middle');const map=rgMap(p,0,0,2);put(map,{x:300,y:390,s:1.6,o:0});show(map,m('pair'));rgPair(p,[['2 cm',rgC.blue],[' : ',COL.chalk],['8 km',rgC.coral]],1110,480,m('units'),54);rgPair(p,[['= ',COL.chalk],['1 cm',rgC.blue],[' : ',COL.chalk],['4 km',rgC.coral]],1110,575,m('units',3.5),54);rgPair(p,[['map distance',rgC.blue],[' : ',COL.chalk],['real distance',rgC.coral]],1110,690,m('units',7.1),34);S.rgMap=map;}],
['q3','Read the map key',()=>{const p=panel(0);rgTitle(p,'Key: 1 cm on the map means 4 km in the real world','Which picture shows 3 cm on the map?');S.rgChoices=[4,3,1].map((key,i)=>{const x=230+i*500,g=rgMap(p,x,290,3,key);rgText(g,'3 cm : '+(3*key)+' km',x+120,595,0,35,COL.chalk,'middle');return {el:g,box:[x-40,230,320,410],label:'3 centimetres on map to '+(3*key)+' kilometres in real world'};});},{ask:done=>quiz(BAND,[{id:'c-map',prompt:'Choose the picture that matches a 3 cm map distance.',build:pickEls(S.rgChoices,0,'Three map segments each represent four kilometres: 3 cm : 12 km.',i=>i===1?'Each centimetre must represent FOUR kilometres. Three lots of four gives twelve.':'Equal numeric counts would ignore the map key. Each map centimetre represents four real kilometres.')}],done)}],
['wrap','Ratios with clear meaning',m=>{const p=panel(0);rgTitle(p,'Let the labels lead');[['Name first quantity, then second; order matters.','w1'],['6 : 9, 6 to 9, and 6/9 express the same ratio.','w2'],['Second quantity: another part, the whole, or a labelled unit.','w3'],['Divide BOTH counts by the same factor; retain their order.','w4']].forEach(([s,k],i)=>{const g=G(p,{x:-16,y:285+i*145,o:0});mk('circle',{cx:130,cy:-10,r:26,fill:COL.task},g);T(g,String(i+1),{x:130,y:0,size:30,anchor:'middle',fill:COL.board});T(g,s,{x:195,size:32});tw(g,{x:0,o:1},m(k),.6);});}],
['final1','Chapter practice',()=>panel(0),{ask:done=>quiz(SCREEN,[{id:'p-fractions',prompt:'Write each ratio as a fraction in LOWEST TERMS.',build:rgInputs([{label:'8 : 20',kind:'fraction',answers:[2,5],hint:'Write numerator/denominator. Divide BOTH 8 and 20 by 4.',why:'8/20=2/5: divide both counts by 4.'},{label:'27 to 18',kind:'fraction',answers:[3,2],hint:'Keep 27 first and 18 second; divide both by 9.',why:'27/18=3/2. A ratio need not be less than 1.'},{label:'14 : 35',kind:'fraction',answers:[2,5],hint:'The common factor 7 gives 2/5.',why:'14/35=2/5.'},{label:'15 red : 40 total',kind:'fraction',answers:[3,8],hint:'Total is already 40. Divide 15 and 40 by 5.',why:'15/40=3/8; the denominator counts all 40.'}])}],done,'Chapter practice · set 1 of 3')}],
['final2','Chapter practice',()=>panel(0),{ask:done=>quiz(SCREEN,[{id:'p-supplies',prompt:'6 pens, 10 pencils, 4 erasers. Enter each ratio in LOWEST TERMS.',build:rgInputs([{label:'pens : pencils',kind:'pair',answers:[3,5],hint:'6:10 reduces by 2 to 3:5; use whole-number counts.',why:'Pens:pencils=6:10=3:5.'},{label:'pencils : pens',kind:'pair',answers:[5,3],hint:'Pencils come first: 10:6 reduces to 5:3.',why:'Pencils:pens=10:6=5:3.'},{label:'pens : all items',kind:'pair',answers:[3,10],hint:'Total 6+10+4=20, not 16. Simplify 6:20.',why:'Pens:all=6:20=3:10.'},{label:'erasers : all items',kind:'pair',answers:[1,5],hint:'Four erasers, twenty items: 4:20 reduces to 1:5.',why:'Erasers:all=4:20=1:5.'}])}],done,'Chapter practice · set 2 of 3')}],
['final3','Chapter practice',()=>panel(0),{ask:done=>quiz(SCREEN,[{id:'p-denominator-part',prompt:['Blue : coral = ',$m(F(2,3)),'. What does the denominator describe?'],build:choice(['The coral count','All beads','The blue count'],0,'The second quantity is coral. This part-to-part ratio does not say that two thirds of ALL beads are blue.',['','The denominator is the second named quantity, coral. The bag also has white beads.','Blue is the first quantity, in the numerator.'])},{id:'p-denominator-whole',prompt:['Blue : all = ',$m(F(1,3)),'. What does the denominator describe?'],build:choice(['Only coral beads','All beads','Only white beads'],1,'All beads. This comparison DOES describe the blue fraction of the whole bag.',['The word ALL makes the second quantity the whole bag.','','The second quantity includes every colour.'])},{id:'p-map-units',prompt:'A map key says “2 cm on map represents 10 km in real world.” What does 2 cm : 10 km compare?',build:choice(['Map centimetres to real kilometres','Two real centimetres to ten real kilometres','Two kilometres to ten centimetres'],0,'Map distance in centimetres compared with real distance in kilometres. Both units belong with their quantities.',['','The first distance is measured on the map.','Order matters: the map distance, 2 cm, comes first; the real distance, 10 km, comes second.'])}],done,'Chapter practice · set 3 of 3')}],
['finish','Finished',()=>panel(0),{ask:finishCard}]
].map(([id,title,run,extra])=>({id,title,run,...extra}));
boot();
</script></body></html>