LittleJS 1.30.1

TileLayers

CanvasLayer

Canvas Layer - a picture placed in the world, drawn into once and shown as one image

  • drawRect and drawTile draw into it in world space, where they show in the world, on the GPU into its WebGL texture, or onto its canvas when WebGL is off or useWebGL is false; drawLayerRect and drawLayerTile draw in its pixels inside redrawStart/End
  • A world unit is as many pixels as its canvas has across its size, so a bigger canvas is a sharper picture
  • A draw turned in the world turns in those pixels, so it keeps its shape only when the canvas has the shape of the size, as many pixels a unit up as across; on any other layer it comes out skewed, which a debug build warns of
  • Its pos is its middle, it turns by its angle and flips with mirror
  • Drawing into its WebGL texture leaves its canvas blank, so drawing on its context only shows on a layer made with useWebGL=false (or with WebGL off), and updateWebGL copies the canvas into the texture
  • What it holds on the GPU is lost with the WebGL context, onContextRestored is the place to draw it again

Extends EngineObject

Extended by ParallaxLayer, TileLayer

Constructor

new CanvasLayer(pos, size, angle, renderOrder, canvasSize, useWebGL)engineTileLayer.js:764

Create a canvas layer object

pos Vector2 optional

World space position of the layer

size Vector2 optional

World space size of the layer

angle number optional, default 0

Angle the layer is rotated by

renderOrder number optional, default 0

Objects sorted by renderOrder

canvasSize Vector2 optional

Default size of canvas, can be changed later

useWebGL boolean optional, default true

Should this layer use WebGL for rendering

const canvasLayer = new CanvasLayer(vec2(), vec2(200,100), 0, 0, vec2(400,200)); // 2 pixels a unit both ways

Members

Methods

Inherited from EngineObject

Members

canvas : OffscreenCanvasengineTileLayer.js:782

The canvas used by this layer

context : OffscreenCanvasRenderingContext2DengineTileLayer.js:780

The 2D canvas context used by this layer

isUsingWebGL : booleanengineTileLayer.js:795

Is this layer drawing into its WebGL texture, set by redrawStart

savedRenderSettings : [CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D, Vector2, Vector2, number, number, Color, Shader|undefined]engineTileLayer.js:896
savedRenderTarget : [WebGLTexture|undefined, number, number, boolean, WebGLTexture|undefined, boolean]engineTileLayer.js:901
scrollTextureInfo : TextureInfo|undefinedengineTileLayer.js:798

Texture info to use for this object rendering

Methods

clearLayerRect(pos, size)engineTileLayer.js:1060

Clear a rectangle in layer space

pos Vector2

position in pixel coordinates

size Vector2
destroy(immediate)engineTileLayer.js:812

Destroy this canvas layer

immediate boolean optional, default false

Remove it now, as EngineObject.destroy does, children included

draw(pos, size, color, angle, mirror, additiveColor, screenSpace, context)engineTileLayer.js:836

Draw this canvas layer centered in world space

pos Vector2

Center in world space

size Vector2 optional

Size in world space

color Color optional

Color to modulate with

angle number optional, default 0

Angle to rotate by

mirror boolean optional, default false

If true the image is flipped left to right

additiveColor Color optional

Additive color to be applied if any

screenSpace boolean optional, default drawScreenSpace

If true the pos and size are in screen space

context CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D optional

Canvas 2D context to draw to

drawLayerRect(pos, size, color, angle)engineTileLayer.js:978

Draw a rectangle in layer space

pos Vector2
size Vector2
color Color optional, default WHITE

Color to modulate with

angle number optional, default 0

Angle to rotate by

drawLayerTile(pos, size, tileInfo, color, angle, mirror, additiveColor)engineTileLayer.js:965

Draw textured tile in layer space

pos Vector2

Position in pixel coordinates

size Vector2 optional, default vec2(1)

Size of the tile

tileInfo TileInfo optional

Tile info to use, untextured if undefined

color Color optional, default WHITE

Color to modulate with

angle number optional, default 0

Angle to rotate by

mirror boolean optional

Is the image flipped left to right?

additiveColor Color optional

Additive color to be applied if any

drawRect(pos, size, color, angle)engineTileLayer.js:1053

Draw a rectangle onto the layer in world space, where it shows in the world

  • Like drawTile, many go between one redrawStart() and redrawEnd()
pos Vector2
size Vector2 optional, default vec2(1)
color Color optional, default WHITE
angle number optional
drawTile(pos, size, tileInfo, color, angle, mirror)engineTileLayer.js:991

Draw a tile onto the layer in world space, where it shows in the world, whatever the layer's place, turn and mirror

  • Each call outside redrawStart and redrawEnd switches to the layer and back, a draw of its own; many in one frame, like a splat for each particle, are drawn between one redrawStart() and redrawEnd()
pos Vector2
size Vector2 optional, default vec2(1)
tileInfo TileInfo optional
color Color optional, default WHITE
angle number optional, default 0
mirror boolean optional, default false
hasWebGL() → booleanengineTileLayer.js:858

Check if this layer is using WebGL

onContextRestored()engineTileLayer.js:864

Called when the WebGL context comes back after it was lost, does nothing by default

  • What was drawn into the layer's texture is gone with the context, draw it again here to keep it; a TileLayer draws its tiles again on its own before this

Call to end the redraw process

redrawStart(clear)engineTileLayer.js:889

Call to start the redraw process, drawing into the layer until redrawEnd

clear boolean optional, default false

Should it clear the canvas before drawing

Move the layer through the world by offset while what is drawn in it stays where it is in the world, for a picture that follows something across a world bigger than it, like decals around a car

  • The offset is snapped to whole pixels of the layer, so the picture never blurs, and the snapped one is returned
  • What scrolls off an edge is gone, and the strip that comes into view is empty for the game to draw
  • With WebGL it is one draw on the GPU, into a second texture as big as the layer's, kept from its first scroll; with WebGL off or useWebGL false the canvas is drawn onto itself, which is slower
  • A picture kept on the canvas and copied up with updateWebGL is not moved on a WebGL layer, only its texture
  • A TileLayer can not scroll, its grid would draw over the picture
offset Vector2

World units to move the layer by

Returns
  • The offset it moved by, snapped to its pixels

Create WebGL texture if necessary and copy layer canvas to it