CanvasLayer

Canvas Layer - cached off screen rendering system

  • Contains an offscreen canvas that can be rendered to
  • WebGL rendering is optional, call useWebGL to enable

Constructor

new CanvasLayer(positionopt, sizeopt, angleopt, renderOrderopt, canvasSizeopt)

Create a canvas layer object

Parameters:
NameTypeAttributesDefaultDescription
positionVector2<optional>

World space position of the layer

sizeVector2<optional>

World space size of the layer

anglenumber<optional>
0

Angle the layer is rotated by

renderOrdernumber<optional>
0

Objects sorted by renderOrder

canvasSizeVector2<optional>

Default size of canvas, can be changed later

Example
const canvasLayer = new CanvasLayer(vec2(), vec2(200,100));

Extends

Members

additiveColor

Properties
TypeDescription
Color

Additive color to apply when rendered

angle

Properties
TypeDescription
number

Angle to rotate the object

angleDamping

Properties
NameTypeAttributesDefaultDescription
angleDampingnumber<optional>
objectDefaultAngleDamping

How much to slow down rotation each frame (0-1)

angleVelocity

Properties
TypeDescription
number

Angular velocity of the object

canvas

Properties
TypeDescription
HTMLCanvasElement

The canvas used by this layer

children

Properties
TypeDescription
Array.<EngineObject>

List of children of this object

clampSpeed

Properties
TypeDescription
boolean

Limit object speed along x and y axis

collideRaycast

Properties
TypeDescription
boolean

Object collides with raycasts

collideSolidObjects

Properties
TypeDescription
boolean

Object collides with solid objects

collideTiles

Properties
TypeDescription
boolean

Object collides with the tile collision

color

Properties
TypeDescription
Color

Color to apply when rendered

context

Properties
TypeDescription
OffscreenCanvasRenderingContext2D

The 2D canvas context used by this layer

damping

Properties
NameTypeAttributesDefaultDescription
dampingnumber<optional>
objectDefaultDamping

How much to slow down velocity each frame (0-1)

drawSize

Properties
TypeDescription
Vector2

Size of object used for drawing, uses size if not set

friction

Properties
NameTypeAttributesDefaultDescription
frictionnumber<optional>
objectDefaultFriction

How much friction to apply when sliding (0-1)

glTexture

Properties
TypeDescription
WebGLTexture

Texture if using WebGL for this layer, call useWebGL to enable

gravityScale

Properties
TypeDescription
number

How much to scale gravity by for this object

groundObject

Properties
TypeDescription
EngineObject

Object we are standing on, if any

isSolid

Properties
TypeDescription
boolean

Object collides with and blocks other objects

localAngle

Properties
TypeDescription
number

Local angle if child

localPos

Properties
TypeDescription
Vector2

Local position if child

mass

Properties
NameTypeAttributesDefaultDescription
massnumber<optional>
objectDefaultMass

How heavy the object is, static if 0

mirror

Properties
TypeDescription
boolean

Should it flip along y axis when rendered

parent

Properties
TypeDescription
EngineObject

Parent of object if in local space

pos

Properties
TypeDescription
Vector2

World space position of the object

renderOrder

Properties
TypeDescription
number

Objects are sorted by render order

restitution

Properties
NameTypeAttributesDefaultDescription
restitutionnumber<optional>
objectDefaultRestitution

How bouncy the object is when colliding (0-1)

size

Properties
TypeDescription
Vector2

World space width and height of the object

spawnTime

Properties
TypeDescription
number

Track when object was created

tileInfo

Properties
TypeDescription
TileInfo

Tile info to render object (undefined is untextured)

velocity

Properties
TypeDescription
Vector2

Velocity of the object

Methods

addChild(child, localPosopt, localAngleopt)

Attaches a child to this with a given local transform

Parameters:
NameTypeAttributesDefaultDescription
childEngineObject
localPosVector2<optional>
(0,0)
localAnglenumber<optional>
0

applyAcceleration(acceleration)

Apply acceleration to this object (adjust velocity, not affected by mass)

Parameters:
NameTypeDescription
accelerationVector2

applyAngularAcceleration(acceleration)

Apply angular acceleration to this object

Parameters:
NameTypeDescription
accelerationnumber

applyForce(force)

Apply force to this object (adjust velocity, affected by mass)

Parameters:
NameTypeDescription
forceVector2

collideWithObject(object) → {boolean}

Called to check if a object collision should be resolved

Parameters:
NameTypeDescription
objectEngineObject

the object to test against

Returns:
  • true if the collision should be resolved
Type: 
boolean

collideWithTile(tileData, pos) → {boolean}

Called to check if a tile collision should be resolved

Parameters:
NameTypeDescription
tileDatanumber

the value of the tile at the position

posVector2

tile where the collision occurred

Returns:
  • true if the collision should be resolved
Type: 
boolean

destroy()

Destroy this canvas layer

drawCanvas2D(pos, size, angle, mirror, drawFunction)

Draw onto the layer canvas in world space (bypass WebGL)

Parameters:
NameTypeDescription
posVector2
sizeVector2
anglenumber
mirrorboolean
drawFunctionfunction

drawRect(pos, sizeopt, coloropt, angleopt)

Draw a rectangle onto the layer canvas in world space

Parameters:
NameTypeAttributesDefaultDescription
posVector2
sizeVector2<optional>
(1,1)
colorColor<optional>
(1,1,1,1)
anglenumber<optional>
0

drawTile(pos, sizeopt, tileInfoopt, coloropt, angleopt, mirroropt)

Draw a tile onto the layer canvas in world space

Parameters:
NameTypeAttributesDefaultDescription
posVector2
sizeVector2<optional>
(1,1)
tileInfoTileInfo<optional>
colorColor<optional>
(1,1,1,1)
anglenumber<optional>
0
mirrorboolean<optional>
false

getAliveTime() → {number}

How long since the object was created

Returns:
Type: 
number

getMirrorSign() → {number}

Get the direction of the mirror

Returns:

-1 if this.mirror is true, or 1 if not mirrored

Type: 
number

localToWorld(pos)

Convert from local space to world space

Parameters:
NameTypeDescription
posVector2

local space point

localToWorldVector(vec)

Convert from local space to world space for a vector (rotation only)

Parameters:
NameTypeDescription
vecVector2

local space vector

removeChild(child)

Removes a child from this one

Parameters:
NameTypeDescription
childEngineObject

render()

Render the object, draws a tile by default, automatically called each frame, sorted by renderOrder

renderDebugInfo()

Render debug info for this object

setCollision(collideSolidObjectsopt, isSolidopt, collideTilesopt, collideRaycastopt)

Set how this object collides

Parameters:
NameTypeAttributesDefaultDescription
collideSolidObjectsboolean<optional>
true

Does it collide with solid objects?

isSolidboolean<optional>
true

Does it collide with and block other objects? (expensive in large numbers)

collideTilesboolean<optional>
true

Does it collide with the tile collision?

collideRaycastboolean<optional>
true

Does it collide with raycasts?

toString() → {string}

Returns string containing info about this object for debugging

Returns:
Type: 
string

update()

Update the object physics, called automatically by engine once each frame

updateTransforms()

Update the object transform, called automatically by engine even when paused

useWebGL(enableopt)

Create or update the WebGL texture for this layer

Parameters:
NameTypeAttributesDefaultDescription
enableboolean<optional>
true

enable WebGL rendering and update the texture

worldToLocal(pos)

Convert from world space to local space

Parameters:
NameTypeDescription
posVector2

world space point

worldToLocalVector(vec)

Convert from world space to local space for a vector (rotation only)

Parameters:
NameTypeDescription
vecVector2

world space vector