I am looking to create a function that scrolls an image element x pixels over y time on an HTML5 canvas, using requestAnimationFrame and delta time. What I can't figure out is how to add more arguments to my function, when requestAnimationFrame allready calls back my function with one argument (a DOMHighResTimeStamp). I am pretty sure the following code doesn't work:
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)
My question is I have no idea how to force requestAnimationFrame to continute to call my scroll function with my additional parameters, when all it does by default is pass just one argument (a timestamp) on callback. So how do I go about adding more parameters (or forcing rAF to put the timestamp in my 'timestamp' argument)?
I am looking to create a function that scrolls an image element x pixels over y time on an HTML5 canvas, using requestAnimationFrame and delta time. What I can't figure out is how to add more arguments to my function, when requestAnimationFrame allready calls back my function with one argument (a DOMHighResTimeStamp). I am pretty sure the following code doesn't work:
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)
My question is I have no idea how to force requestAnimationFrame to continute to call my scroll function with my additional parameters, when all it does by default is pass just one argument (a timestamp) on callback. So how do I go about adding more parameters (or forcing rAF to put the timestamp in my 'timestamp' argument)?
Share Improve this question edited Feb 27, 2014 at 6:53 Danneh asked Feb 27, 2014 at 6:46 DannehDanneh 2011 gold badge4 silver badges10 bronze badges 2- From what I can muster with my limited knowledge, I think the solution lies either in .call()/.apply() or by passing an anonymous function that contains scroll in it to rAF, but I can't wrap my head around accessing rAF's timestamp if I do either. – Danneh Commented Feb 27, 2014 at 6:54
- Possible duplicate of How can i pass argument with requestAnimationFrame? – Magnus Lind Oxlund Commented Nov 19, 2018 at 15:26
2 Answers
Reset to default 12What your requestAnimationFrame
statement evaluates to:
scroll(timestamp, distanceToScroll, secondsToScroll)
, where timestamp is undefined. It throws an error or returns undefinedwindow.requestAnimationFrame
is executed without parameters, thus no callback
Passing an anonymous function that calls scroll
with the desired parameters should do the trick:
requestAnimationFrame(function(timestamp) {
scroll(timestamp, distanceToScroll, secondsToScroll));
});
What this evaluates to:
window.requestAnimationFrame
is called with anonymous function as callback- anonymous function is called with
timestamp
as first parameter scroll
is called with currenttimestamp
,distanceToScroll
andsecondsToScroll
as parameters
Pure JavaScript
function scrollIntoViewSmooth(elem) {
var move = elem.offsetTop - (elem.offsetTop - elem.parentElement.scrollTop) * 0.25;
if (Math.abs(elem.offsetTop - move) <= 2) {
elem.parentElement.scrollTop = elem.offsetTop;
} else {
elem.parentElement.scrollTop = move;
setTimeout(scrollIntoViewSmooth, 33, elem);
}
}
Example
scrollIntoViewSmooth(document.getElementById('stuff'));