mirror of
https://github.com/visgl/luma.gl.git
synced 2026-01-25 14:08:58 +00:00
3.9 KiB
3.9 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.
- Makes it easy to wait for the HTML page to load before creating resources.
- Provides a number of commonly needed variables to the renderFrame method as part of the
contextobject. - By default, manages resizing of canvas and viewport which is usually all that simpler apps and demos need. Advanced apps can override.
Usage
Short example:
new AnimationLoop({
onCreateContext() {
return createGLContext({canvas: 'canvas-0'}))
},
onInitialize({gl}) {
return {
clipSpaceQuad: new ClipSpaceQuad({gl, fs: CONTEXT_0_FRAGMENT_SHADER})
};
},
onRenderFrame({tick, clipSpaceQuad}) {
clipSpaceQuad.render({uTime: tick * 0.01});
}
});
To avoid problems with page load timing, move context creation to the initContext method. Also the setupFrame method enables the app to control the resizing of canvas, framebuffer and viewport.
new AnimationLoop()
onCreateContext() {
return createGLContext({canvas: 'canvas-0'});
},
onInit(({gl}) => ({
clipSpaceQuad: new ClipSpaceQuad({gl, fs: CONTEXT_0_FRAGMENT_SHADER})
}.
onInitFrame({gl, canvas}) {
canvas.width = canvas.clientWidth;
canvas.style.height = `${canvas.width}px`;
canvas.height = canvas.width;
gl.viewport(0, 0, canvas.width, canvas.height);
},
onDrawFrame({tick, clipSpaceQuad}) {
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 parametre 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 matricespixelRatio: Pixel ratio of the canvas drawingBuffer.time: A monotonic value in milliseconds sinceAnimationLoopwas created.tick: A monotonic counter that updates 60 times per second.tock: A monotonic counter that updates for every frame rendered.
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)onSetupFrame(callback) - Supplying this callback overrides the default frame setup code, which resizes the canvas and the viewport after the window size. For applications that use a single full screen canvas, this function is usually not needed.onRenderFrame(callback) - Callingframewill automatically start the animation. If this is not desired, follow immediately with astop().onCreateContext(callback) - function without parameters that returns aWebGLRenderingContext. This callback will be called exactly once, after page load completes.
Remarks:
- 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.
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.