我希望创建一个函数,使用 requestAnimationFrame 和增量时间在 HTML5 画布上将图像元素滚动 x 像素超过 y 时间。我不知道的是,当 requestAnimationFrame allready 使用一个参数(DOMHighResTimeStamp)回调我的函数时,如何向我的函数添加更多参数。我很确定以下代码不起作用:
function scroll(timestamp, distanceToScroll, secondsToScroll) {
//delta = how many milliseconds have passed between this and last draw
if (!lastDraw) {var lastDraw = timestamp;};
delta = (0.5 + (timestamp - lastDraw)) << 0; //bitwise hack for rounding integers
lastDraw = timestamp;
//speed (pixels per millisecond) = amount of pixels to move / length of animation (in milliseconds)
speed = distanceToScroll / secondsToScroll;
//new position = current position + (speed * delta)
position += (speed * delta);
context.drawImage(myImage,0,position,50,50/*of 200*/,0,0,100,100);
requestAnimationFrame(scroll(timestamp, distanceToScroll, secondsToScroll));
};
//later...
scroll(timestamp, 100, 5)
scroll(timestamp, 10, 20)
我的问题是,我不知道如何强制 requestAnimationFrame 继续使用附加参数调用我的滚动函数,而默认情况下它所做的只是在回调时仅传递一个参数(时间戳)。那么我该如何添加更多参数(或强制 rAF 将时间戳放入我的“时间戳”参数中)?