So I want to animate the drawing of a line on the HTML 5 canvas using requestAnimationFrame.
I've done this
function animateWrapper(centerX, centerY, xAngle, yAngle, width, height) {
var request = requestAnimationFrame(animate);
var canvas = document.getElementById("drawing");
var context = canvas.getContext('2d');
function animate (centerX, centerY, xAngle, width, height) {
drawLine(centerX,centerY, centerX, centerY - height, context, request);
}
}
function drawLine(startX, startY, endX, endY, context, request) {
context.beginPath();
context.moveTo(startX, startY);
step += 0.05;
// if (step > 1) step = 1;
var newX = startX + (endX - startX) * step;
var newY = startY + (endY - startY) * step;
context.lineTo(newX, newY);
context.stroke();
if (Math.abs(newX) >= Math.abs(endX) && Math.abs(newY) >= Math.abs(endY)) {
cancelAnimationFrame(request);
step = 0;
}
}
$(document).ready(function(){
animateWrapper(100, 100, 40, 40, 400, 400);
});
Right now centerY, xAngle, width, height are all undefined(which makes sense - they aren't being passed in). This causes endY to become a NaN, so the animation does not occur.
I'm not sure how to fix this - how do I pass in those arguments in the request animation frame?