CHAPTER 4
Code Explanation
function init(){
ctx = document.getElementById
('canvas').getContext('2d');
drawall();
}
function fire() {
var angle = Number(document.f
.ang.value);
Extract angle from form, convert to numbervar outofcannon = Number
(document.f.vo.value);
Extract velocity out of cannon from form,
convert to numbervar angleradians = angle*Math
.PI/180;
Convert to radianshorvelocity = outofcannon*Math
.cos(angleradians);
Compute horizontal velocityverticalvel1 = - outofcannon*Math
.sin(angleradians);
Compute initial vertical velocityeverything[cannonindex][2]=
- angleradians;
Set information to rotate cannoncball.sx = cannonx +
cannonlength*Math.cos(angleradians);
Set x for cball at mouth of what will be rotated
cannoncball.sy = cannony+cannonht*.5
- cannonlength*Math.sin(angleradians);
Set y for cball at mouth of what will be rotated
cannondrawall();
tid = setInterval(change,100);
return false;