var g,my={}
function calcrectMain(mode){my.version='0.80';w=300;h=360;var id="rect";s="";s+='
';s+='';my.topClr='purple'
my.sideClr='lightpurple'
my.sideClr='blue'
my.periClr='red'
my.diagClr='darkorange'
my.flds=[{id:'wide',title:'Width',lt:30,tp:22,clr:my.topClr,fn:'chgSide'},{id:'side',title:'Height',lt:165,tp:120,clr:my.sideClr,fn:'chgSide'},{id:'area',title:'Area',lt:30,tp:165,clr:'#ce5c00',fn:'chgArea'},{id:'diag',title:'Diagonal',lt:30,tp:235,clr:my.diagClr,fn:'chgDiag'},{id:'peri',title:'Perimeter',lt:30,tp:310,clr:my.periClr,fn:'chgPeri'},]
for(var i=0;i'+fld.title+':
';s+='';}
s+='© 2018 MathsIsFun.com v'+my.version+'
';s+='';document.write(s);var el=document.getElementById('canvas'+id);ratio=2;el.width=w*ratio;el.height=h*ratio;el.style.width=w+"px";el.style.height=h+"px";g=el.getContext("2d");g.setTransform(ratio,0,0,ratio,0,0);bgDraw()
my.unit=''
my.hist=[0]
divNum('wide',1)
divNum('side',1)
chg(1)}
function bgDraw(){my.rect={x:20,y:70,wd:150,ht:200}
var rect=my.rect
g.strokeStyle=my.topClr
g.fillStyle=my.topClr
g.beginPath()
g.moveTo(rect.x,rect.y-15)
g.lineTo(rect.x+rect.wd,rect.y-15)
g.drawArrow(rect.x,rect.y-15,15,2,20,10,Math.PI,10,false)
g.drawArrow(rect.x+rect.wd,rect.y-15,15,2,20,10,0,10,false)
g.stroke()
g.fill()
g.strokeStyle=my.sideClr
g.fillStyle=my.sideClr
g.beginPath()
g.moveTo(rect.x+rect.wd+20,rect.y)
g.lineTo(rect.x+rect.wd+20,rect.y+rect.ht)
g.drawArrow(rect.x+rect.wd+20,rect.y,15,2,20,10,Math.PI/2,10,false)
g.drawArrow(rect.x+rect.wd+20,rect.y+rect.ht,15,2,20,10,Math.PI*3/2,10,false)
g.stroke()
g.fill()
g.strokeStyle=my.periClr
g.fillStyle='hsla(240,100%,94%,1)'
g.lineWidth=1.2
g.beginPath()
g.rect(rect.x,rect.y,rect.wd,rect.ht)
g.fill()
g.stroke()
g.strokeStyle=my.periClr
g.fillStyle=my.periClr
g.lineWidth=1
g.beginPath()
g.drawArrow(90,270,25,1,20,10,Math.PI/2,10,false)
g.stroke()
g.fill()
g.strokeStyle=my.diagClr
g.setLineDash([5,5])
g.setLineDash([5,5])
g.beginPath()
g.moveTo(rect.x,rect.y)
g.lineTo(rect.x+rect.wd,rect.y+rect.ht)
g.stroke()
g.setLineDash([])}
function chg(n){if(my.hist[my.hist.length-1]!=n)my.hist.push(n)
while(my.hist.length>2){my.hist.shift();}
console.log('chg',my.hist)
for(var i=0;i=48&&charCode<=57)isNumQ=true;if(!isNumQ){isAllNumQ=false;splitCol=i;break;}}
if(wantNumQ){if(isAllNumQ){return text;}else{return text.substr(0,splitCol);}}else{if(isAllNumQ){return "";}else{return text.substr(splitCol).trim();}}}
function fmtNum(val,unit,exp){exp=typeof exp!=='undefined'?exp:1;var s="";if(unit.length>0){if(unit.charAt(unit.length-1)=="²"){unit=unit.substr(0,unit.length-1);}
s=fmt(val,7)+" "+unit;if(exp==2)
s+="²";}else{s=fmt(val,10);}
return s;}
function fmt(val,len){return val.toPrecision(len);}
CanvasRenderingContext2D.prototype.drawArrow=function(x0,y0,totLen,shaftHt,headLen,headHt,angle,sweep,invertQ){var g=this;var pts=[[0,0],[-headLen,-headHt/2],[-headLen+sweep,-shaftHt/2],[-totLen,-shaftHt/2],[-totLen,shaftHt/2],[-headLen+sweep,shaftHt/2],[-headLen,headHt/2],[0,0]];if(invertQ){pts.push([0,-headHt/2],[-totLen,-headHt/2],[-totLen,headHt/2],[0,headHt/2]);}
for(var i=0;i