0

I am using canvas and webkitrequestAnimation frame for my animation.. However, the animation is instant and not smooth.. It is so fast, i cant see any animation.. How do i make the animation smooth ?

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Wave 2</title>

</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>

<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
angle = 0,
range = 50,
centerY = canvas.height / 2,
xspeed = 1,
yspeed = 0.05,
xpos = 0,
ypos = centerY;
context.lineWidth = 2;
(function drawFrame () {
window.webkitrequestAnimationFrame(drawFrame, canvas);

context.beginPath();
context.moveTo(xpos, ypos);
//Calculate the new position.
xpos += xspeed;
angle += yspeed;
ypos = centerY + Math.sin(angle) * range;
context.lineTo(xpos, ypos);
context.stroke();
}());
};
</script>
</body>
</html>
Manish Basdeo
  • 6,139
  • 22
  • 68
  • 102

1 Answers1

1

That works as expected for me apart from a syntax error on line 24:

window.webkitrequestAnimationFrame(drawFrame, canvas);

window.webkitRequestAnimationFrame(drawFrame, canvas);

Check out the answer on this page: How to use requestAnimationFrame

It will guide you in writing a request animation function that works across all compatible web browsers not just those built on webkit. If you were using a browser other than Safari or Chrome that might explain any erratic behaviour.

I would like to advocate createjs.com here because they provide great API for working with the HTML5 canvas and animation. Check out the demo's and source!

Community
  • 1
  • 1
LoneWolf
  • 176
  • 1
  • 9