WebGL

LittleJS WebGL Interface

  • WebGL2 rendering engine for high-performance graphics
  • Batched sprite rendering for drawing thousands of sprites efficiently
  • Instanced rendering using vertex array objects (VAOs)
  • Polygon rendering with triangle strip support
  • Shader system with custom vertex and fragment shaders
  • Texture management with automatic atlas support
  • Post-processing effects via framebuffer and shader plugins
  • Automatic fallback to Canvas2D if WebGL is unavailable
  • Context loss and restoration handling
  • Can be disabled with glEnable setting
  • Advanced users can create custom shaders and render targets

Members

(static) glAntialias :boolean

Should WebGL be setup with anti-aliasing? must be set before calling engineInit

Type:
  • boolean

(static) glCanvas :HTMLCanvasElement

The WebGL canvas which appears below the main canvas

Type:
  • HTMLCanvasElement

(static) glContext :WebGL2RenderingContext

WebGL2 context for glCanvas

Type:
  • WebGL2RenderingContext

Methods

(static) glClearCanvas()

Clear the canvas or render target to canvasClearColor

(static) glClearRect(x, y, width, height)

Clear out a rectangle area of the WebGL canvas or render target

  • In framebuffer pixels from the bottom left: backing store pixels on the canvas, texture pixels in a render target
Parameters:
NameTypeDescription
xnumber
ynumber
widthnumber
heightnumber

(static) glCompileShader(source, type) → {WebGLShader}

Compile WebGL shader of the given type, will throw errors if in debug mode

Parameters:
NameTypeDescription
sourcestring
typenumber
Returns:
Type: 
WebGLShader

(static) glCopyToContext(context)

Flush any sprites still in the buffer and copy the WebGL canvas to a 2D context

Parameters:
NameTypeDescription
contextCanvasRenderingContext2D | OffscreenCanvasRenderingContext2D

(static) glCreateProgram(vsSource, fsSource) → {WebGLProgram}

Create WebGL program with given shaders

Parameters:
NameTypeDescription
vsSourcestring
fsSourcestring
Returns:
Type: 
WebGLProgram

(static) glCreateTexture(imageopt, wrapopt) → {WebGLTexture}

Create WebGL texture from an image and init the texture settings Restores the active texture when done

Parameters:
NameTypeAttributesDefaultDescription
imageHTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap<optional>
wrapboolean | Array.<number><optional>
false

true for REPEAT, false for CLAMP_TO_EDGE, or the WebGL modes across and down

Returns:
Type: 
WebGLTexture

(static) glDeleteTexture(textureopt)

Deletes a WebGL texture

Parameters:
NameTypeAttributesDescription
textureWebGLTexture<optional>

(static) glDraw(x, y, sizeX, sizeY, angleopt, uv0Xopt, uv0Yopt, uv1Xopt, uv1Yopt, rgbaopt, rgbaAdditiveopt)

Add a sprite to the gl draw list, used by all gl draw functions

Parameters:
NameTypeAttributesDefaultDescription
xnumber
ynumber
sizeXnumber
sizeYnumber
anglenumber<optional>
0
uv0Xnumber<optional>
0
uv0Ynumber<optional>
0
uv1Xnumber<optional>
1
uv1Ynumber<optional>
1
rgbanumber<optional>
-1

white is -1

rgbaAdditivenumber<optional>
0

black is 0

(static) glDrawColoredPoints(points, pointColors)

Add a list of colored points to the gl draw list

Parameters:
NameTypeDescription
pointsArray.<Vector2>

Array of Vector2 points in tri strip order

pointColorsArray.<number>

Array of 32-bit integer colors

(static) glDrawOutlineTransform(points, rgba, lineWidth, x, y, sx, sy, angle, wrapopt)

Transform and add a polygon's outline to the gl draw list

Parameters:
NameTypeAttributesDefaultDescription
pointsArray.<Vector2>

Array of Vector2 points

rgbanumber

Color of the polygon as a 32-bit integer

lineWidthnumber

Width of the outline

xnumber
ynumber
sxnumber
synumber
anglenumber
wrapboolean<optional>
true

Should the outline connect the first and last points

(static) glDrawPoints(points, rgba)

Add a list of points to the gl draw list

Parameters:
NameTypeDescription
pointsArray.<Vector2>

Array of Vector2 points in tri strip order

rgbanumber

Color as a 32-bit integer

(static) glDrawPointsTransform(points, rgba, x, y, sx, sy, angle, tristripopt)

Transform and add a polygon to the gl draw list

Parameters:
NameTypeAttributesDefaultDescription
pointsArray.<Vector2>

Array of Vector2 points

rgbanumber

Color of the polygon as a 32-bit integer

xnumber
ynumber
sxnumber
synumber
anglenumber
tristripboolean<optional>
true

should tristrip algorithm be used

(static) glDrawUntextured(x, y, sizeX, sizeY, angle, rgba)

Add an untextured rect to the gl draw list Zeroes the uvs and rgba so the texture contribution multiplies to 0, then carries the real color in the additive slot. Works regardless of which texture is currently bound.

Parameters:
NameTypeDescription
xnumber
ynumber
sizeXnumber
sizeYnumber
anglenumber
rgbanumber

color as 32-bit integer

(static) glFlush()

Draw all sprites and clear out the buffer, called automatically by the system whenever necessary

(static) glSetAntialias(antialiasopt)

Set anti-aliasing for WebGL canvas Must be called before engineInit

Parameters:
NameTypeAttributesDefaultDescription
antialiasboolean<optional>
true

(static) glSetRenderTarget(textureopt, clearopt)

Set the WebGL render target to the given texture or back to the canvas

  • What is drawn into a texture is stored premultiplied, and draws of that texture blend it as such
Parameters:
NameTypeAttributesDefaultDescription
textureWebGLTexture<optional>

a texture or undefined to use normal glCanvas

clearboolean<optional>
false

should the render target be cleared, to canvasClearColor, CLEAR_BLACK for a transparent one

(static) glSetTexture(texture)

Set the WebGL texture, called automatically if using multiple textures

  • This may also flush the gl buffer resulting in more draw calls and worse performance
Parameters:
NameTypeDescription
textureWebGLTexture

(static) glSetTextureData(texture, image)

Set WebGL texture data from an image, restores the active texture when done

Parameters:
NameTypeDescription
textureWebGLTexture
imageHTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap

(static) glSetTextureWrap(texture, wrapopt)

Set the wrap mode on an existing WebGL texture Flushes the current batch only if the texture is the active one

Parameters:
NameTypeAttributesDefaultDescription
textureWebGLTexture
wrapboolean | Array.<number><optional>
true

true for REPEAT, false for CLAMP_TO_EDGE, or the WebGL modes across and down, like [gl.CLAMP_TO_EDGE, gl.MIRRORED_REPEAT]