Valter
Valter

Reputation: 83

Function with arguments in requestAnimationFrame

I'm trying to define a recursive function with arguments using "requestAnimationFrame".

animate(frameElapsed,points,canvas){
        console.log("test");
        if(frameElapsed<points.length-1){ 
            requestAnimationFrame(this.animate(frameElapsed+1,points,canvas));
        }
        ...
} 

When I do this, i get the error "Argument of type 'void' is not assignable to parameter of type 'FrameRequestCallback'. So I tried to define an anonymous function that calls animate to solve this problem.

animate(frameElapsed,points,canvas){
        console.log("test");
        if(frameElapsed<points.length-1){ 
            requestAnimationFrame(function() {
                this.animate(frameElapsed+1,points,canevas);
            }); 
        }
        ...
} 

Then, I get "ERROR TypeError: Cannot read property 'animate' of undefined" in my web console. I can't manage to solve this problem.

Is there a way to pass arguments to af function called back by requestAnimationFrame ?

Thank you for your help !

Upvotes: 0

Views: 1050

Answers (1)

Valter
Valter

Reputation: 83

user120242's solution worked for me !

    animate(frameElapsed, points, canvas) {
        console.log("test");
        if(frameElapsed < points.length - 1) { 
            requestAnimationFrame(() => {
                this.animate(frameElapsed + 1, points, canvas);
            }); 
        }
        ...
    } 

Thank you for your help !

Upvotes: 3

Related Questions