3.8 KiB
Context Management
Provides functions to create and initialize a WebGL context, and to check for presence of WebGL and extensions.
- Provides the
createGLContextmethod which can create WebGLContexts both in browsers and under Node.js. - Also provides
getGLExtensionthat throws anErrorif the requested extension cannot be returned.
Note that the use of these functions is NOT required to use the remaining functions and classes in luma.gl.
You could e.g. manually create a WebGLContext by using canvas.getContext, or use a context created by another WebGL library. In fact, luma.gl is explicitly designed to work with any WebGL context, and in contrast to some other approaches, luma.gl maintains no "hidden state" that might complicate composing your code with other WebGL based modules.
Usage
import headlessGL from 'gl';
import {createGLContext} from 'luma.gl';
const gl = createGLContext({headlessGL})
Functions
createGLContext
Creates and returns a WebGL context, both in browsers and in Node.js.
const gl = createGLContext(options);
canvas(string|DOMElement, required for browser contexts, ignored for headless contexts) Can be a string with the canvas id or the canvas element itself.width(number, required for headless contexts, ignored for browser contexts) - width of the "virtual screen" render targetheight(number, required for headless contexts, ignored for browser contexts) - height of the "virtual screen" render targetdebug(boolean,true) - Unless set to false, all gl calls will beconsole.log-ged and errors thrown to the console. Note that catching webgl errors has a performance impact as it requires a GPU sync after each operation, so make sure to passfalsein production environments.webgl2(boolean,false) - If true, will attempt to create a WebGL2 context (not supported on headless environments). Will fall back to WebGL1 contexts. Usegl instanceof WebGL2RenderingContextto determine what type of context was actually returned.webgl2=false(boolean) - Iftrue, will attempt to create a WebGL2 context (not supported on headless environments). Will fall back to WebGL1 contexts ifwebgl1is true. UseisWebGL2orgetCapsto determine what type of context was actually returned.webgl1=true(boolean) - Iftrue, will attempt to create a WebGL1 context. Set to false ifwebgl2istrueto force failure onwebgl2contexts.throwOnError=true(boolean) - NormallycreateGLContextwill throw an error on failure. With this flag set, it will returnnullinstead.headlessGL(function, null) - In Node.js environments, contexts are created using headless-gl. To avoid including theheadless-glmodule in applications that don't use it, luma.gl requires the app to install and import headless-gl itself, and pass headless-gl as an argument tocreateGLContext.
getExtension
More info here.
getGLExtension(gl, name);
gl(WebGLRenderingContext) - gl contextname(String) - The name of the extension.
Returns the WebGL extension with the given name, for example OES_texture_float.
Throws an Error if the extension is not available.
clear
Clears the drawing buffer (the default framebuffer) or the currently bound framebuffer.
Remarks
- In browser environments, contexts are created via
HTMLCanvasElement.getContext, first usingwebgland then falls back toexperimental-webgl. Ifwebgl2option is set, this function will first trywebgl2and thenexperimental-webgl2, before falling back to webgl1. - In Node.js environments, the context is created using headless-gl. In this case width and height options must be supplied as there is no canvas element to use as reference.