LittleJS Drawing System
- Hybrid rendering with both Canvas2D and WebGL support
- Optimized tile sheet sprite rendering using WebGL batching
- Primitive drawing for polygons, ellipses, and lines
- Tile-based rendering with TileInfo and TextureInfo classes
- Text rendering with custom fonts and ImageFont support
- Color and additive color blending for effects
- Rotation, mirroring, and scaling transformations
- Camera system with position, scale, and rotation
- Multiple canvas support (main, WebGL, work canvases)
- Gradient fills and outlined shapes
- Image manipulation and color tinting
Rendering Architecture:
- glCanvas: WebGL canvas for accelerated sprite batch rendering
- mainCanvas: Canvas2D overlay for text, UI, and custom drawing
- All draw functions default to WebGL when enabled, can force Canvas2D with useWebGL parameter
- Source
Classes
Members
(static) backgroundCanvas :HTMLCanvasElement
Extra canvas to composite behind the engine canvases when combining canvases Set by plugins that render to their own canvas below the LittleJS canvases
- HTMLCanvasElement
- Source
(static) drawContext :CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D
The default 2d context to use for drawing, usually mainContext
- CanvasRenderingContext2D |
OffscreenCanvasRenderingContext2D
- Source
(static) drawCount :number
Keeps track of how many draw calls there were each frame for debugging
- number
- Source
(static) engineImageFont :ImageFont
Engine font image, 8x8 font provided by the engine
- ImageFont
- Source
(static) mainCanvas :HTMLCanvasElement
The primary 2D canvas visible to the user
- HTMLCanvasElement
- Source
(static) mainCanvasSize :Vector2
The size of the main canvas (and other secondary canvases) in css pixels
- This is the screen space coordinate system, matching mousePos
- With canvasPixelRatio set the backing store is larger than this
- Vector2
- Source
(static) mainContext :CanvasRenderingContext2D
2d context for mainCanvas
- Scaled by canvasPixelRatio, so drawing to it is in css pixels
- getImageData and putImageData ignore that scale and work in backing store pixels, so use workReadCanvas to read pixels back instead of this
- CanvasRenderingContext2D
- Source
(static) primitiveCount :number
Keeps track of how many primitives were drawn each frame for debugging A single draw call can render many primitives (e.g. a WebGL sprite batch).
- number
- Source
(static) textureInfos :Array.<TextureInfo>
Array containing texture info for batch rendering system
- Array.<TextureInfo>
- Source
(static) workCanvas :OffscreenCanvas
Offscreen canvas that can be used for image processing
- OffscreenCanvas
- Source
(static) workContext :OffscreenCanvasRenderingContext2D
Offscreen canvas that can be used for image processing
- OffscreenCanvasRenderingContext2D
- Source
(static) workReadCanvas :OffscreenCanvas
Offscreen canvas with willReadFrequently that can be used for image processing
- OffscreenCanvas
- Source
(static) workReadContext :OffscreenCanvasRenderingContext2D
Offscreen canvas with willReadFrequently that can be used for image processing
- OffscreenCanvasRenderingContext2D
- Source
Methods
(static) cameraFit(center, size, worldMarginopt, screenInsetopt) → {number}
Fit the camera to a rectangle in world space by setting cameraPos and cameraScale
- worldMargin pads the content rectangle in world units, so the gap scales with the content on resize
- screenInset reserves space in screen pixels on each viewport edge (for example a HUD band) and re-centers the content away from that edge, so the reserved band stays a fixed pixel size on resize
- worldMargin and screenInset may each be a number for all sides, a Vector2 (x=left/right, y=top/bottom), or an object with any of {top, right, bottom, left}
| Name | Type | Attributes | Description |
|---|---|---|---|
center | Vector2 | Center of the rectangle in world space | |
size | Vector2 | Size of the rectangle in world space | |
worldMargin | number | | <optional> | World space padding added around the content rectangle |
screenInset | number | | <optional> | Screen space padding in pixels reserved on each viewport edge |
- Source
- The new camera scale
- Type:
- number
(static) combineCanvases()
Combines LittleJS canvases onto the main canvas This is necessary for things like screenshots and video
- Source
(static) drawCanvas2D(pos, size, angleopt, mirroropt, drawFunctionopt, screenSpaceopt, contextopt)
Draw directly to a 2d canvas context in world space. The Y axis is flipped so world-Y-up coordinates render right-side up (matches the WebGL path). Callers whose drawing depends on Y direction (e.g. linear gradients) should flip their own Y endpoints accordingly.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | Vector2 | |||
angle | number | <optional> | 0 | |
mirror | boolean | <optional> | false | |
drawFunction | Canvas2DDrawFunction | <optional> | Needed, marked optional only because the ones before it are | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> | drawContext |
- Source
(static) drawCircle(pos, sizeopt, coloropt, lineWidthopt, lineColoropt, useWebGLopt, screenSpaceopt, contextopt)
Draw colored circle using passed in point
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | number | <optional> | 1 | Diameter |
color | Color | <optional> | WHITE | |
lineWidth | number | <optional> | 0 | |
lineColor | Color | <optional> | BLACK | |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawCircleGradient(pos, sizeopt, colorInneropt, colorOuteropt, useWebGLopt, screenSpaceopt, contextopt)
Draw a circle filled with a radial gradient from the center to the rim
- Best when batched with other untextured polys
- If drawing mostly textured sprites, bake the gradient into a texture and use drawTile instead
- Stacking gradients at the exact same position may show a faint vertical artifact
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | number | <optional> | 1 | Diameter |
colorInner | Color | <optional> | WHITE | |
colorOuter | Color | <optional> | CLEAR_WHITE | |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawEllipse(pos, sizeopt, coloropt, angleopt, lineWidthopt, lineColoropt, useWebGLopt, screenSpaceopt, contextopt)
Draw colored ellipse using passed in point
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | Vector2 | <optional> | vec2(1) | Width and height diameter |
color | Color | <optional> | WHITE | |
angle | number | <optional> | 0 | |
lineWidth | number | <optional> | 0 | |
lineColor | Color | <optional> | BLACK | |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawEllipseGradient(pos, sizeopt, colorInneropt, colorOuteropt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw an ellipse filled with a radial gradient from the center to the rim
- Best when batched with other untextured polys
- If drawing mostly textured sprites, bake the gradient into a texture and use drawTile instead
- Stacking gradients at the exact same position may show a faint vertical artifact
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | Vector2 | <optional> | vec2(1) | Width and height diameter |
colorInner | Color | <optional> | WHITE | |
colorOuter | Color | <optional> | CLEAR_WHITE | |
angle | number | <optional> | 0 | |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawLine(posA, posB, widthopt, coloropt, posopt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw colored line between two points
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
posA | Vector2 | |||
posB | Vector2 | |||
width | number | <optional> | 0.1 | |
color | Color | <optional> | WHITE | |
pos | Vector2 | <optional> | vec2() | Offset to apply |
angle | number | <optional> | 0 | Angle to rotate by |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawLineList(points, widthopt, coloropt, wrapopt, posopt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw connected lines between a series of points
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
points | Array.<Vector2> | |||
width | number | <optional> | 0.1 | |
color | Color | <optional> | WHITE | |
wrap | boolean | <optional> | false | Should the last point connect to the first? |
pos | Vector2 | <optional> | vec2() | Offset to apply |
angle | number | <optional> | 0 | Angle to rotate by |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawPoly(points, coloropt, lineWidthopt, lineColoropt, posopt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw colored polygon using passed in points
- WebGL fills a polygon whose edges do not cross, concave or not; a self crossing one, like a star through its outer points, fills wrong there, so draw it as its simple outline or in parts
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
points | Array.<Vector2> | Array of Vector2 points | ||
color | Color | <optional> | WHITE | |
lineWidth | number | <optional> | 0 | |
lineColor | Color | <optional> | BLACK | |
pos | Vector2 | <optional> | vec2() | Offset to apply |
angle | number | <optional> | 0 | Angle to rotate by |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawRect(pos, sizeopt, coloropt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw colored rect centered on pos
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | Vector2 | <optional> | vec2(1) | |
color | Color | <optional> | WHITE | |
angle | number | <optional> | ||
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawRectGradient(pos, sizeopt, colorTopopt, colorBottomopt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw a rect centered on pos with a gradient from top to bottom
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | Vector2 | <optional> | vec2(1) | |
colorTop | Color | <optional> | WHITE | |
colorBottom | Color | <optional> | CLEAR_WHITE | |
angle | number | <optional> | 0 | |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawRegularPoly(pos, sizeopt, sidesopt, coloropt, lineWidthopt, lineColoropt, angleopt, useWebGLopt, screenSpaceopt, contextopt)
Draw colored regular polygon using passed in number of sides
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | |||
size | Vector2 | <optional> | vec2(1) | |
sides | number | <optional> | 3 | |
color | Color | <optional> | WHITE | |
lineWidth | number | <optional> | 0 | |
lineColor | Color | <optional> | BLACK | |
angle | number | <optional> | 0 | |
useWebGL | boolean | <optional> | glEnable | |
screenSpace | boolean | <optional> | drawScreenSpace | |
context | CanvasRenderingContext2D | | <optional> |
- Source
(static) drawText(text, pos, sizeopt, coloropt, lineWidthopt, lineColoropt, textAlignopt, fontopt, fontStyleopt, maxWidthopt, angleopt, contextopt)
Draw text on main canvas in world space Automatically splits new lines into rows
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
text | string | | |||
pos | Vector2 | |||
size | number | <optional> | 1 | |
color | Color | <optional> | WHITE | |
lineWidth | number | <optional> | 0 | |
lineColor | Color | <optional> | BLACK | |
textAlign | 'left' | | <optional> | 'center' | |
font | string | <optional> | fontDefault | |
fontStyle | string | <optional> | ||
maxWidth | number | <optional> | ||
angle | number | <optional> | 0 | |
context | CanvasRenderingContext2D | | <optional> | drawContext |
- Source
(static) drawTextScreen(text, pos, size, coloropt, lineWidthopt, lineColoropt, textAlignopt, fontopt, fontStyleopt, maxWidthopt, angleopt, contextopt)
Draw text in screen space Automatically splits new lines into rows
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
text | string | | |||
pos | Vector2 | |||
size | number | |||
color | Color | <optional> | WHITE | |
lineWidth | number | <optional> | 0 | |
lineColor | Color | <optional> | BLACK | |
textAlign | 'left' | | <optional> | center | |
font | string | <optional> | fontDefault | |
fontStyle | string | <optional> | ||
maxWidth | number | <optional> | ||
angle | number | <optional> | 0 | Clockwise, like the other screen space draws |
context | CanvasRenderingContext2D | | <optional> | drawContext |
- Source
(static) drawTextureWrapped(pos, size, wrapCount, textureopt, coloropt, angleopt, additiveColoropt, useWebGLopt, screenSpaceopt, contextopt)
Draw a texture tiled (wrapped) across a rectangle in world space. Useful for backgrounds, repeating patterns, and seamless fills. The whole texture is tiled — sub-region (TileInfo) wrapping is not supported.
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | Center of the rect in world space | ||
size | Vector2 | Size of the rect in world space | ||
wrapCount | Vector2 | How many times the texture repeats (x, y) | ||
texture | TextureInfo | | <optional> | 0 | TextureInfo or texture index into textureInfos |
color | Color | <optional> | WHITE | Color to modulate with |
angle | number | <optional> | 0 | Angle to rotate by |
additiveColor | Color | <optional> | Additive color to be applied if any | |
useWebGL | boolean | <optional> | glEnable | Use accelerated WebGL rendering? |
screenSpace | boolean | <optional> | drawScreenSpace | Are pos and size in screen space? |
context | CanvasRenderingContext2D | | <optional> | Canvas 2D context to draw to |
- Source
(static) drawTile(pos, sizeopt, tileInfoopt, coloropt, angleopt, mirroropt, additiveColoropt, useWebGLopt, screenSpaceopt, contextopt)
Draw textured tile centered in world space
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | Center of the tile in world space | ||
size | Vector2 | <optional> | vec2(1) | Size of the tile in world space |
tileInfo | TileInfo | <optional> | Tile info to use, untextured if undefined | |
color | Color | <optional> | WHITE | Color to modulate with |
angle | number | <optional> | 0 | Angle to rotate by |
mirror | boolean | <optional> | Is image flipped along the Y axis? | |
additiveColor | Color | <optional> | Additive color to be applied if any | |
useWebGL | boolean | <optional> | glEnable | Use accelerated WebGL rendering? |
screenSpace | boolean | <optional> | drawScreenSpace | Are the pos and size are in screen space? |
context | CanvasRenderingContext2D | | <optional> | Canvas 2D context to draw to |
- Source
(static) getCameraSize() → {Vector2}
Get the size of the camera window in world space
- Source
- Type:
- Vector2
(static) isFullscreen() → {boolean}
Returns true if fullscreen mode is active
- Source
- Type:
- boolean
(static) isOnScreen(pos, sizeopt) → {boolean}
Check if a box, point, or circle is on screen with a circle test If size is a Vector2, uses the length as diameter This can be used to cull offscreen objects from render or update
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | world space position | ||
size | Vector2 | | <optional> | 0 | world space size or diameter |
- Source
- Type:
- boolean
(async, static) loadTexture(textureIndex, srcopt) → {Promise.<TextureInfo>}
Load a texture at a specific index after engineInit, the images passed to engineInit load this way
| Name | Type | Attributes | Description |
|---|---|---|---|
textureIndex | number | Index to store the texture at, an unused one | |
src | string | <optional> | Image source path |
- Source
Resolves to the texture info once the image loads, or fails to with a warning
- Type:
- Promise.<TextureInfo>
(static) screenToWorld(screenPos) → {Vector2}
Convert from screen to world space coordinates
| Name | Type | Description |
|---|---|---|
screenPos | Vector2 |
- Source
- Type:
- Vector2
(static) screenToWorldDelta(screenDelta) → {Vector2}
Convert from screen to world space coordinates for a directional vector (no translation)
| Name | Type | Description |
|---|---|---|
screenDelta | Vector2 |
- Source
- Type:
- Vector2
(static) screenToWorldTransform(screenPos, screenSize, screenAngleopt)
Convert screen space transform to world space
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
screenPos | Vector2 | |||
screenSize | Vector2 | |||
screenAngle | number | <optional> | 0 |
- Source
(static) setAdditiveBlendMode(additiveopt)
Enable additive blending
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
additive | boolean | <optional> | true |
- Source
(static) setBackgroundCanvas(canvasopt)
Set an extra canvas to composite behind the engine canvases when combining Plugins that insert their own canvas below the LittleJS canvases should set this so it appears in screenshots and video capture
| Name | Type | Attributes | Description |
|---|---|---|---|
canvas | HTMLCanvasElement | <optional> |
- Source
(static) setCursor(cursorStyleopt)
Set the cursor style
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
cursorStyle | string | <optional> | auto | CSS cursor style (auto, none, crosshair, etc) |
- Source
(static) setShader(shaderopt)
Set the Shader that 2D draws use from now on, none for the engine's own
- The object render loop sets each object's own shader, so this is for draws in gameRender and gameRenderPost
| Name | Type | Attributes | Description |
|---|---|---|---|
shader | Shader | <optional> |
- Source
(static) tile(indexopt, sizeopt, textureopt, paddingopt, bleedopt) → {TileInfo}
Create a tile info object using a grid based system
- This can take vecs or floats for easier use and conversion
- If an index is passed in, the tile size and index will determine the position
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
index | Vector2 | | <optional> | 0 | Index of the tile in 1d or 2d form |
size | Vector2 | | <optional> | Size of tile in pixels | |
texture | TextureInfo | | <optional> | 0 | Texture index or info to use |
padding | number | <optional> | How many pixels padding around tiles | |
bleed | number | <optional> | How many pixels smaller to draw tiles |
- Source
- Type:
- TileInfo
tile(2) // a tile at index 2 using the default tile size of 16
tile(5, 8) // a tile at index 5 using a tile size of 8
tile(1, 16, 3) // a tile at index 1 of size 16 on texture 3
tile(vec2(4,8), vec2(30,10)) // a tile at index (4,8) with a size of (30,10)(static) toggleFullscreen()
Toggle fullscreen mode
- Source
(static) worldToScreen(worldPos) → {Vector2}
Convert from world to screen space coordinates
| Name | Type | Description |
|---|---|---|
worldPos | Vector2 |
- Source
- Type:
- Vector2
(static) worldToScreenDelta(worldDelta) → {Vector2}
Convert from world to screen space coordinates for a directional vector (no translation)
| Name | Type | Description |
|---|---|---|
worldDelta | Vector2 |
- Source
- Type:
- Vector2
Type Definitions
CameraFitSides
Padding for each side of a rectangle, the sides left out are 0
- Object
| Name | Type | Attributes | Description |
|---|---|---|---|
top | number | <optional> | |
right | number | <optional> | |
bottom | number | <optional> | |
left | number | <optional> |
- Source
Canvas2DDrawFunction(context)
| Name | Type | Description |
|---|---|---|
context | CanvasRenderingContext2D | |
- Source
SpriteAnimationEndCallback() → {void}
- Source
- Type:
- void