function animate() {
requestAnimationFrame(animate);
// do render stuff
chicken.position.x++;
}
animate();
However when you encapsulate your code into a class
animate() {
requestAnimationFrame(this.animate.bind(this));
// do render stuff
this.chicken.position.x++;
}
// somewhere else
this.animate.call(this);
I noticed you can get a measurable execution speedup by prebinding the animate function to do this
animate() {
requestAnimationFrame(this.animate_binded);
// do render stuff
this.chicken.position.x++;
}
// somewhere else
this.animate_binded = this.animate.bind(this);
this.animate();