【发布时间】:2014-06-29 10:57:43
【问题描述】:
所以我想使用 requestAnimationFrame 在 HTML 5 画布上绘制一条线的动画。
我已经做到了
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);
});
现在 centerY、xAngle、width、height 都是未定义的(这是有道理的——它们没有被传入)。这会导致 endY 变为 NaN,因此不会发生动画。
我不确定如何解决这个问题 - 如何在请求动画帧中传递这些参数?
【问题讨论】:
标签: javascript jquery html animation canvas