Files
uni/year4/semester1/CT404: Graphics & Image Processing/materials/week3/examples/canvasAnimationExample1_withSmootherAnimation.html

54 lines
1.4 KiB
HTML

<html>
<head>
<script>
var x = 0, y = 0;
var dx = 4, dy = 5;
var now = Date.now();
function draw() {
// do it all again in 1/60th of a second
window.requestAnimationFrame(draw);
var elapsedMs = Date.now() - now;
now = Date.now();
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
// remove previous translation if any
context.save();
// over-write previous content, with a grey rectangle
context.fillStyle = "#DDDDDD";
context.fillRect(0, 0, 600, 600);
// perform movement, and translate to position
x += dx * elapsedMs / 16.7;
y += dy * elapsedMs / 16.7;
if (x <= 0)
dx = 4;
else if (x >= 550)
dx = -4;
if (y <= 0)
dy = 5;
else if (y >= 550)
dy = -5;
context.translate(x, y);
// purple rectangle
context.fillStyle = "#CC00FF";
context.fillRect(0, 0, 50, 50);
context.restore();
}
</script>
</head>
<body onload="draw();">
<canvas id="canvas" width="600" height="600"></canvas>
</body>
</html>