How to call requestAnimationFrame from a class instance?

How to call requestAnimationFrame from a class instance?

Example:
class Foo() {
animation(){
window.requestAnimationFrame(this.animation);
}
}

new Foo().animation();
July 4th 19 at 22:44
2 answers
July 4th 19 at 22:46
Solution
If I understand you correctly, here is my September demo:
codepen.io/WispProxy/pen/YGGKdd
There is an example call.

In short, you want to pass the context.
Cool! Thank you very much for the example) - nikita.Stracke commented on July 4th 19 at 22:49
July 4th 19 at 22:48
Solution
1) you Have a syntax error in the class Declaration (parentheses not needed - this is not a function).
2) you Need to bandit the context, ie to use a lambda function or bind(this)
class Foo {
animation(){
window.requestAnimationFrame(this.animation.bind(this));
}
}

new Foo().animation();

Find more questions by tags Canvas