GLContext

Creates a WebGLRenderingContext and augments it with extra methods and properties. All methods and properties of WebGLRenderingContext can be directly accessed from this class.

Constructor

new L3D.GLContext(options)

Parameters
options Object

supported options:

Properties
canvas Canvas

The canvas to use.

width Number

The width of the canvas to be created.

height Number

The height of the canvas to be created.

container Element | String

The containing element for the created canvas.

version Number

The version of WebGL to use, either 1 or 2. 0 or undefined will use the greatest available version.

Properties

currentShader GLShader

currently bound shader.

extensions Object

Stores common used extensions so they can be checked if they are supported.

height number

The canvas height, in pixels.

id number

Unique ID for this GLContext.

maxAnisotropy number
maxCombinedTextures number
maxCubeMapSize number
maxFragmentUniformVectors number
maxTextureSize number
maxTextureUnits number
maxVaryingVectors number
maxVertexAttribs number
maxVertexTextures number
maxVertexUniformVectors number
maxViewport Int32Array
onDestroy Signal

Emitted when the context is destroyed so objects can free their resources.

onDrawFrame Signal

Emitted so objects can draw into the current frame.

onLoseContext Signal

Emitted when the context has been lost so objects can free their resources and prepare for a new context being created.

onUpdate Signal

Emitted before rendering so listeners can do any updating for the next frame.

resourceManager GLResourceManager

Manages a cache of GL objects.

state GLState

The GLState object managing pushing and popping of state values.

vendor String
version String
viewportData Float32Array

The current viewport dimensions

webgl2 bool

True if this context is WebGL 2.

webglVersion number

The version of WebGL used by this context, either 2 or 1.

width number

The canvas width, in pixels.

Methods

static cloneCanvas(canvas) → Canvas

Clone a canvas element.

Parameters
canvas Canvas

The canvas to clone.

Returns
Canvas

The created canvas.

static createCanvas(width, height) → Canvas

Create a canvas element.

Parameters
width number

The width of the created canvas.

height number

The height of the created canvas.

Returns
Canvas

The created canvas.

animate()

Launch animation loop (calls gl.onUpdate and gl.onDrawFrame every frame) example: gl.onDrawFrame.addListener(function(){ ... }) or gl.onUpdate.addListener(function(dt) { ... })

destroy()

Destroy this WebGL context and removes the Canvas from the DOM.

drawFrame()

Request all objects listening to onDrawFrame to draw.

exitPointerLock()

Release the pointer lock from the canvas.

fullscreen()

Launches the canvas in fullscreen mode

getAspectRatio() → number

Get the aspect ratio of the canvas.

Returns
number

The aspect ratio, width / height.

getUniformFunction(data) → function

Get the setter function to use for a uniform given a WebGLActiveInfo.

Parameters
data WebGLActiveInfo

Info about the active uniform.

Returns
function

The uniform setter function, bound to the GL context, such as uniform1f.

getViewport(v)

Get the dimensions of the viewport

Parameters
v Array.<number>

Optional storage for the results.

requestFrame()

Request a frame to be drawn. This is done asynchrounously, and multiple calls to requestFrame called before the frame is drawn will be consolidated.

requestPointerLock()

Request pointer lock for the canvas. This needs to be done from a user event, such as mousedown or touchdown.

reset()

Reset the the initial gl state

setViewport(v, flipY)

Set the dimensions of the viewport.

Parameters
v array.<number>

Dimensions of the viewport

flipY bool

If true, flip the y coordinate of the viewport so the origin is the bottom-left corner of the viewport.

snapshot(startx, starty, areax, areay, skipReverse) → Canvas

Returns a canvas with a snapshot of an area. This is safer than using the canvas itself due to internals of WebGL.

Parameters
startx number

Viewport x coordinate

starty number

Viewport y coordinate, from bottom of the canvas.

areax number

Viewport area width.

areay number

Viewport area height.

skipReverse bool

If false, the canvas image will be flipped vertically.

Returns
Canvas

canvas

updateResolution()

Call this when the size of the canvas has changed to update the resolution of the canvas to match.

viewport(a, b, c, d)

Set the dimensions of the viewport.

Parameters
a number
b number
c number
d number