mirror of
https://github.com/visgl/luma.gl.git
synced 2025-12-08 17:36:19 +00:00
* Remove 'pickingSelectedColorValid' module setting * Rename 'useDevicePixelRatio' to 'useDevicePixels'
3.5 KiB
3.5 KiB
AnimationLoop
While this class is named to suggest that it is a wrapper for requestAnimationFrame, it provides a number of features related to initialization and animation of a WebGLRenderingContext or WebGL2RenderingContext.
- Makes it easy to wait for the HTML page to load before creating resources.
- Provides a number of commonly needed variables as part of the
contextobject which is passed toonRenderandonFinalizecallbacks. - Objects returned by
onInitailizewill be appended tocontextobject hence available toonRenderandonFinalize. - To avoid problems with page load timing, move context creation to the
onCreateContextmethod. - By default,
onRendermethod manages resizing of canvas, viewport and framebuffer.
Usage
Short example:
new AnimationLoop({
onCreateContext() {
return createGLContext({canvas: 'canvas-0'}))
},
onInitialize({gl}) {
return {
clipSpaceQuad: new ClipSpaceQuad({gl, fs: CONTEXT_0_FRAGMENT_SHADER})
};
},
onRender(context) {
const {tick, clipSpaceQuad} = context;
clipSpaceQuad.render({uTime: tick * 0.01});
}
});
Callback Parameters
The callbacks that the app supplies to the AnimationLoop will be called with an object containing named parameters. The parameter object will contain the following values:
gl- ThisAnimationLoop'sWebGLRenderingContext.canvas- The canvas associated with the rendering context.width: The canvas width, in device pixels.height: The canvas height, in device pixels.aspect: The canvas aspect ratio (width/height) to update projection matricesuseDevicePixels: A boolean value, indicates whether canvas is created to utilize full resolution of Retina/HD displays.time: A monotonic value in milliseconds sinceAnimationLoopwas created.tick: A monotonic counter that updates for every frame rendered.- And objects that are returned by
onInitializemethod.
Methods
constructor
Parameters:
onInitialize(callback) - function that will be called exactly once afterstart()has been called, after page load completes and a context has been created. The callback will be called with an initial object containing a gl context object. Can return a promise (e.g. for texture or model loads)onCreateContext(callback) - function without parameters that returns aWebGLRenderingContext. This callback will be called exactly once, after page load completes.onRenderFrame(callback) - Callingframewill automatically start the animation. If this is not desired, follow immediately with astop().onFinalize(callback) - Called once when animation is stopped. Can be used to delete objects or free any resources created duringonInitialize.
start
Restarts the animation
stop
Stops the animation
Remarks
- You can instantiate multiple
AnimationLoopclasses in parallel, rendering into the same or differentWebGLRenderingContexts. - Works both in browser and under Node.js.
- All
AnimationLoopmethods can be chained. - Postpones context creation until the page (i.e. all HTML) has been loaded. At this time it is safe to specify canvas ids when calling
createGLContext. - The supplied callback function must return a WebGLRenderingContext or an error will be thrown.
- This callback registration function should not be called if a
WebGLRenderingContextwas supplied to the AnimationLoop constructor.