<html>
<head>
<title>Simple Animation</title>
<script type="text/javascript">
var can, ctx, puzzle,
x, y, xVec, yVec,
direc, interval,
rot = 0,
gravity = 1,
left = 150,
right = 1050,
bottom = 650,
centerOffset = -150;
function init() {
puzzle = document.getElementById("puzzle");
can = document.getElementById("can");
ctx = can.getContext("2d");
ctx.strokeStyle = "black";
// initialize position, speed, spin direction
x = 196;
y = 150;
xVec = 1.5;
yVec = 0;
direc = 1;
// draw lines for the puzzle to bounce off of
ctx.moveTo(0, bottom + 75);
ctx.lineTo(600, bottom+ 75);
ctx.lineTo(600, 0)
ctx.stroke();
// set animation to repeat every 40 ms
interval = setInterval(animate, 40);
}
function animate() {
model();
// clear canvas except for lines at edge
ctx.clearRect(0, 0, can.width - 1 , can.height - 1);
draw();
}
function model() {
rot += .1 * direc;
x += xVec;
yVec += gravity;
y += yVec;
bounceIf();
}
function bounceIf() {
if (y >= bottom) {
y = bottom;
yVec = -1 * yVec - gravity
}
}
function draw() {
ctx.save();
ctx.translate(x, y);
ctx.drawImage(puzzle, centerOffset,centerOffset);
ctx.restore();
}
</script>
</head>
<body onload="init()" style="background-color:#ffffff">
<img id="puzzle" src="ball.gif" style="display:none" />
<canvas id="can" height="800" width="1200" style="position:relative;top:-50">
</canvas>
</body>
</html>
// set animation to repeat every 40 ms
interval = setInterval(animate, 40);
Time to create page: 0.055 seconds