HTML and CSS Reference
In-Depth Information
ball.x = 0;
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
ball.x += xspeed;
ball.y = centerY / 2 + Math.sin(angle) * range;
ball.draw(context);
}());
};
</script>
</body>
</html>
Pulsing motion
One important thing to keep in mind is that you can apply sine values to other properties besides the
position. In the 05-pulse.html example, we used the values to affect the scale of the ball instead. This
gives it a pulsating appearance. Here's the code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pulse</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="utils.js"></script>
<script src="ball.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
ball = new Ball(),
angle = 0,
centerScale = 1,
range = 0.5,
speed = 0.05;
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
ball.scaleX = ball.scaleY = centerScale + Math.sin(angle) * range;
angle += speed;
ball.draw(context);
}());
 
Search WWH ::




Custom Search