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>

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.