Constructor
new Engine(canvas)
Create the engine controlling a canvas
Parameters:
| Name | Type | Description |
|---|---|---|
canvas |
object | DOM element containing the canvas |
Members
canvas
Get the current drawing canvas
ctx
Get the current drawing context
deltaTime
Get the current framerate as milliseconds per frame (mspf)
deltaTimeAverage
Get the average framerate as milliseconds per frame (mspf)
frameCount
Get the count of frames since the start
frameRate
Get the current framerate as frames per second (fps)
frameRateAverage
Get the average framerate as frames per second (fps)
height
Get the drawing area height
is_recording
Get the current recording state
(readonly) mousePosition
Get the current mouse position
(readonly) prevMousePosition
Get the previous mouse position
width
Get the drawing area width
Methods
background(color)
Set the background color for the canvas
Parameters:
| Name | Type | Description |
|---|---|---|
color |
string | number | Color can be a CSS< RGB, RGBA, HEX, HEAX, HSL, HSLA string, a Color object, or a monochrome value (number) |
click(x, y)
Public callback for mouse click and touchscreen tap
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | coordinate of the click/tap location |
y |
number | coordinate of the click/tap location |
clickHandler(e)
Handler for mouse click/touchscreen tap
Parameters:
| Name | Type | Description |
|---|---|---|
e |
MouseEvent | event |
draw(dt)
Main sketch function, will be run continuously unless noLoop() is called
Parameters:
| Name | Type | Description |
|---|---|---|
dt |
number | Delta time in milliseconds since last frame |
keyDown(key, code)
Public callback for key down
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string | pressed key |
code |
number | code of the pressed key |
keyDownHandler(e)
Handler for key pressed event
Parameters:
| Name | Type | Description |
|---|---|---|
e |
KeyboardEvent | event |
keyPress(key, code)
Public callback for key press
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string | pressed key |
code |
string | code of the pressed key |
keyPressHandler(e)
Handler for key press event
Parameters:
| Name | Type | Description |
|---|---|---|
e |
KeyboardEvent | event |
keyUp(key, code)
Public callback for key up
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string | pressed key |
code |
number | code of the pressed key |
keyUpHandler(e)
Handler for key up event
Parameters:
| Name | Type | Description |
|---|---|---|
e |
KeyboardEvent | event |
loop()
Starts looping the script
mouseDown(x, y)
Public callback for mouse and touchscreen pressed
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | coordinate of the click/tap location |
y |
number | coordinate of the click/tap location |
mouseDownHandler(e)
Handler for mouse down
Parameters:
| Name | Type | Description |
|---|---|---|
e |
MouseEvent | event |
mouseDragged(x, y)
Public callback for mouse and touchscreen drag
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | coordinate of the click/tap location |
y |
number | coordinate of the click/tap location |
mouseMoveHandler(e)
Handler for moved mouse
Parameters:
| Name | Type | Description |
|---|---|---|
e |
MouseEvent | event |
mouseMoved(x, y)
Public callback for mouse and touchscreen moved
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | coordinate of the click/tap location |
y |
number | coordinate of the click/tap location |
mouseUp(x, y)
Public callback for mouse and touchscreen up
Parameters:
| Name | Type | Description |
|---|---|---|
x |
number | coordinate of the click/tap location |
y |
number | coordinate of the click/tap location |
mouseUpHandler(e)
Handler for mouse up
Parameters:
| Name | Type | Description |
|---|---|---|
e |
MouseEvent | event |
noLoop()
Stops looping the script
preload()
Function ran once, before the sketch is actually loaded
saveFrame(titleopt)
Save current frame
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
title |
string |
<optional> |
null | The image filename (optional). |
saveRecording(filename)
Save the recording as a series of frames in a zip file
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
filename |
string | frames.zip | of the file to download |
scaleFromCenter(x, y)
Scale the canvas by a factor from the center.
If only one parameter is passed, the canvas will be scaled uniformly.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
x |
number | scaling factor in the x direction | |
y |
number | null | scaling factor in the y direction |
setup()
Function ran once
startRecording()
Start recording frames
stop()
Completely stop the engine.
Note that this step is final and there's no way to programatically restart it after this call.
stopRecording()
Stop recording frames