WebGL

LittleJS WebGL Interface

  • All webgl used by the engine is wrapped up here
  • For normal stuff you won't need to see or call anything in this file
  • For advanced stuff there are helper functions to create shaders, textures, etc
  • Can be disabled with glEnable to revert to 2D canvas rendering
  • Batches sprite rendering on GPU for incredibly fast performance
  • Sprite transform math is done in the shader where possible
  • Supports shadertoy style post processing shaders

Members

(static) glAntialias :Boolean

Shoule webgl be setup with antialiasing, must be set before calling engineInit

Type:
  • Boolean

(static) glCanvas :HTMLCanvasElement

The WebGL canvas which appears above the main canvas and below the overlay canvas

Type:
  • HTMLCanvasElement

(static) glContext :WebGL2RenderingContext

2d context for glCanvas

Type:
  • WebGL2RenderingContext

Methods

(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, forceDrawopt)

Draw any sprites still in the buffer, copy to main canvas and clear

Parameters:
NameTypeAttributesDefaultDescription
contextCanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
forceDrawBoolean<optional>
false

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

Create WebGL program with given shaders

Parameters:
NameTypeDescription
vsSourceString
fsSourceString
Returns:
Type: 
WebGLProgram

(static) glCreateTexture(image) → {WebGLTexture}

Create WebGL texture from an image and init the texture settings

Parameters:
NameTypeDescription
imageHTMLImageElement
Returns:
Type: 
WebGLTexture

(static) glDraw(x, y, sizeX, sizeY, angle, uv0X, uv0Y, uv1X, uv1Y, rgba, rgbaAdditiveopt)

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

Parameters:
NameTypeAttributesDefaultDescription
xNumber
yNumber
sizeXNumber
sizeYNumber
angleNumber
uv0XNumber
uv0YNumber
uv1XNumber
uv1YNumber
rgbaNumber
rgbaAdditiveNumber<optional>
0

(static) glFlush()

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

(static) glSetAntialias(antialiasopt)

Set antialiasing for webgl canvas

Parameters:
NameTypeAttributesDefaultDescription
antialiasBoolean<optional>
true

(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