diff --git a/src/renderers/webgl/WebGLAnimation.js b/src/renderers/webgl/WebGLAnimation.js index 6965bcf897abdb..e72bb5fed0b379 100644 --- a/src/renderers/webgl/WebGLAnimation.js +++ b/src/renderers/webgl/WebGLAnimation.js @@ -1,52 +1,55 @@ -function WebGLAnimation() { +class WebGLAnimation { - let context = null; - let isAnimating = false; - let animationLoop = null; - let requestId = null; + constructor() { - function onAnimationFrame( time, frame ) { + this._context = null; + this._isAnimating = false; + this._animationLoop = null; + this._requestId = null; - animationLoop( time, frame ); - - requestId = context.requestAnimationFrame( onAnimationFrame ); + // Re-bind the prototype function so that `this` can be used with rAF: + this._onAnimationFrame = this._onAnimationFrame.bind( this ); } - return { + _onAnimationFrame( time, frame ) { + + this._animationLoop( time, frame ); - start: function () { + this._requestId = this._context.requestAnimationFrame( this._onAnimationFrame ); + + } - if ( isAnimating === true ) return; - if ( animationLoop === null ) return; + start() { - requestId = context.requestAnimationFrame( onAnimationFrame ); + if ( this._isAnimating === true ) return; + if ( this._animationLoop === null ) return; - isAnimating = true; + this._requestId = this._context.requestAnimationFrame( this._onAnimationFrame ); - }, + this._isAnimating = true; - stop: function () { + } - context.cancelAnimationFrame( requestId ); + stop() { - isAnimating = false; + this._context.cancelAnimationFrame( this._requestId ); - }, + this._isAnimating = false; - setAnimationLoop: function ( callback ) { + } - animationLoop = callback; + setAnimationLoop( callback ) { - }, + this._animationLoop = callback; - setContext: function ( value ) { + } - context = value; + setContext( value ) { - } + this._context = value; - }; + } }