Skip to content

pencil.js / scene / Scene

Class: Scene<O> ​

Defined in: scene.ts:28

Root of every drawing, linked to a canvas displayed on screen and handling user interactions

Extends ​

Type Parameters ​

Type ParameterDefault type
O extends SceneOptionsSceneOptions

Constructors ​

Constructor ​

ts
new Scene<O>(container?, options?): Scene<O>;

Defined in: scene.ts:68

Scene constructor

Parameters ​

ParameterTypeDefault valueDescription
container?HTMLElementwindow.document.bodyContainer of the renderer (or a canvas to use)
options?Partial<O>undefinedSpecific options

Returns ​

Scene<O>

Overrides ​

OffScreenCanvas.constructor

Properties ​

PropertyTypeDescriptionInherited fromDefined in
childrenContainer<ContainerOptions>[]List of childrenOffScreenCanvas.childrencontainer.ts:134
ctxCanvasRenderingContext2DDrawing contextOffScreenCanvas.ctxoffscreen-canvas.ts:25
cursorPositionPositionLast known pointer position-scene.ts:43
eventListenersRecord<string, EventListener[]>Set of event listeners of this componentOffScreenCanvas.eventListenersevent-emitter.ts:33
fpsnumberNumber of frame per seconds (only when looped)-scene.ts:52
frameCountnumberNumber of time this container has been renderedOffScreenCanvas.frameCountcontainer.ts:140
isClickedbooleanIs the scene clicked-scene.ts:49
isLoopedbooleanIs the scene rendered every frame-scene.ts:46
lastTicknumber | nullTimestamp of the last render-scene.ts:55
optionsOSet of optionsOffScreenCanvas.optionscontainer.ts:131
parent| Container<ContainerOptions> | nullParent container (null when not attached)OffScreenCanvas.parentcontainer.ts:137
positionPositionPosition in its containerOffScreenCanvas.positioncontainer.ts:128

Accessors ​

center ​

Get Signature ​

ts
get center(): Position;

Defined in: offscreen-canvas.ts:114

Return this canvas center point

Returns ​

Position

Inherited from ​

OffScreenCanvas.center


height ​

Get Signature ​

ts
get height(): number;

Defined in: offscreen-canvas.ts:96

Height of the canvas

Returns ​

number

Set Signature ​

ts
set height(height): void;

Defined in: offscreen-canvas.ts:100

Parameters ​
ParameterType
heightnumber
Returns ​

void

Inherited from ​

OffScreenCanvas.height


isScene ​

Get Signature ​

ts
get isScene(): boolean;

Defined in: scene.ts:33

Is this the root scene

Returns ​

boolean

Overrides ​

OffScreenCanvas.isScene


size ​

Get Signature ​

ts
get size(): Position;

Defined in: offscreen-canvas.ts:107

Return the whole canvas size

Returns ​

Position

Inherited from ​

OffScreenCanvas.size


width ​

Get Signature ​

ts
get width(): number;

Defined in: offscreen-canvas.ts:85

Width of the canvas

Returns ​

number

Set Signature ​

ts
set width(width): void;

Defined in: offscreen-canvas.ts:89

Parameters ​
ParameterType
widthnumber
Returns ​

void

Inherited from ​

OffScreenCanvas.width


className ​

Get Signature ​

ts
get static className(): string;

Defined in: scene.ts:29

Name used when serializing (survive minification)

Returns ​

string

Overrides ​

OffScreenCanvas.className


defaultOptions ​

Get Signature ​

ts
get static defaultOptions(): SceneOptions;

Defined in: scene.ts:364

Default options for this class

Returns ​

SceneOptions

Overrides ​

OffScreenCanvas.defaultOptions


events ​

Get Signature ​

ts
get static events(): Record<string, string>;

Defined in: scene.ts:374

Set of events for easy access

Returns ​

Record<string, string>

Overrides ​

OffScreenCanvas.events

Methods ​

add() ​

ts
add(...children): this;

Defined in: container.ts:206

Add other containers as children

Parameters ​

ParameterTypeDescription
...childrenContainer<ContainerOptions>[]Other containers

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.add


applyRotationAndScale() ​

ts
protected applyRotationAndScale(ctx): this;

Defined in: container.ts:467

Apply rotation and scaling to the context

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.applyRotationAndScale


applyTransform() ​

ts
applyTransform(ctx): this;

Defined in: container.ts:450

Apply position, clipping, rotation and scaling to the context

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.applyTransform


clear() ​

ts
clear(): this;

Defined in: offscreen-canvas.ts:52

Erase the canvas

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.clear


climbAncestry() ​

ts
climbAncestry(callback, until?): void;

Defined in: container.ts:744

Execute an action on itself and every ancestor

Parameters ​

ParameterTypeDescription
callback(ancestor) => voidFunction to execute on each ancestor
until?| Container<ContainerOptions> | nullDefine a ancestor where to stop the climbing (excluded)

Returns ​

void

Inherited from ​

OffScreenCanvas.climbAncestry


clone() ​

ts
clone(): this;

Defined in: container.ts:788

Create a deep copy of this container (and its children)

Returns ​

this

Inherited from ​

OffScreenCanvas.clone


delete() ​

ts
delete(): this;

Defined in: container.ts:259

Remove itself from its parent

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.delete


destroy() ​

ts
destroy(): this;

Defined in: scene.ts:189

Stop rendering, remove every listener and remove the canvas from the page (when created by the scene)

Returns ​

this

Itself


empty() ​

ts
empty(): this;

Defined in: container.ts:251

Remove all its children

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.empty


fire() ​

ts
fire(event): this;

Defined in: container.ts:374

Fire an event and bubble it to its parent

Parameters ​

ParameterTypeDescription
eventBaseEventEvent to fire

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.fire


getAbsoluteMatrix() ​

ts
getAbsoluteMatrix(): DOMMatrix;

Defined in: container.ts:330

Get the cumulated matrix of transformation from the root to this container

Returns ​

DOMMatrix

Inherited from ​

OffScreenCanvas.getAbsoluteMatrix


getAbsolutePosition() ​

ts
getAbsolutePosition(): Position;

Defined in: container.ts:338

Get this container's absolute position (up to it's utmost parent), taking into account every transformation

Returns ​

Position

Inherited from ​

OffScreenCanvas.getAbsolutePosition


getEventPosition() ​

ts
getEventPosition(event): Position;

Defined in: scene.ts:203

Convert a DOM event coordinates into the scene space

Parameters ​

ParameterTypeDescription
event{ clientX: number; clientY: number; }Any DOM mouse/pointer event
event.clientXnumber-
event.clientYnumber-

Returns ​

Position


getImageData() ​

ts
getImageData(vectorDefinition?): ImageData;

Defined in: offscreen-canvas.ts:129

Render and return the canvas content as data

Parameters ​

ParameterTypeDescription
vectorDefinition?VectorDefinitionBox of data to extract

Returns ​

ImageData

Inherited from ​

OffScreenCanvas.getImageData


getMatrix() ​

ts
getMatrix(): DOMMatrix;

Defined in: container.ts:310

Get the matrix of transformation applied by this container on its children (including its position)

Returns ​

DOMMatrix

Inherited from ​

OffScreenCanvas.getMatrix


getRandomPosition() ​

ts
getRandomPosition(): Position;

Defined in: offscreen-canvas.ts:121

Return a random position within the canvas

Returns ​

Position

Inherited from ​

OffScreenCanvas.getRandomPosition


getRoot() ​

ts
getRoot(): Container;

Defined in: container.ts:299

Return its highest parent

Returns ​

Container

Inherited from ​

OffScreenCanvas.getRoot


getScene() ​

ts
getScene(): Promise<Scene<SceneOptions>>;

Defined in: container.ts:267

Return a promise for the associated scene (resolved once attached to a scene)

Returns ​

Promise<Scene<SceneOptions>>

Inherited from ​

OffScreenCanvas.getScene


getTarget() ​

ts
getTarget(position, ctx): 
  | Container<ContainerOptions>
  | null;

Defined in: container.ts:401

Find the target at a position

Parameters ​

ParameterTypeDescription
positionPositionAny position
ctxCanvasRenderingContext2DDrawing context to apply paths

Returns ​

| Container<ContainerOptions> | null

Inherited from ​

OffScreenCanvas.getTarget


hide() ​

ts
hide(): this;

Defined in: scene.ts:175

Hide it

Returns ​

this

Itself

Overrides ​

OffScreenCanvas.hide


isAncestorOf() ​

ts
isAncestorOf(container?): boolean;

Defined in: container.ts:728

Define if this is an ancestor of another container

Parameters ​

ParameterTypeDescription
container?| Container<ContainerOptions> | nullAny container

Returns ​

boolean

Inherited from ​

OffScreenCanvas.isAncestorOf


isHover() ​

ts
isHover(): boolean;

Defined in: scene.ts:145

A scene is always hovered when shown

Returns ​

boolean

Overrides ​

OffScreenCanvas.isHover


isInClip() ​

ts
protected isInClip(position, ctx): boolean;

Defined in: container.ts:437

Tell if a position is inside the clipping region

Parameters ​

ParameterTypeDescription
positionPositionAny position (in the drawing context space)
ctxCanvasRenderingContext2DDrawing context with this container's transformations applied

Returns ​

boolean

Inherited from ​

OffScreenCanvas.isInClip


makePath() ​

ts
makePath(_ctx): this;

Defined in: container.ts:700

Do nothing on Container, override it to add behavior

Parameters ​

ParameterTypeDescription
_ctxCanvasRenderingContext2DDrawing context

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.makePath


on() ​

ts
on(
   eventName, 
   callback, 
   isTargeted?
): this;

Defined in: event-emitter.ts:51

Listen to an event or multiple events

Parameters ​

ParameterTypeDefault valueDescription
eventNamestring | string[]undefinedName of event (or a list of event names) to listen to. The event name can be followed by a modifier (separated by a ".")
callbackEventCallbackundefinedFunction to call when event fire
isTargeted?booleanfalseShould only listen to event targeting itself

Returns ​

this

Itself

Example ​

ts
component.on("event.modifier", () => console.log("Event fired with modifier"));

Inherited from ​

OffScreenCanvas.on


once() ​

ts
once(
   eventName, 
   callback, 
   isTargeted?
): this;

Defined in: event-emitter.ts:71

Listen to an event only once

Parameters ​

ParameterTypeDefault valueDescription
eventNamestring | string[]undefinedName of event (or a list of event names) to listen to.
callbackEventCallbackundefinedFunction to call when event fire
isTargeted?booleanfalseShould only listen to event targeting itself

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.once


remove() ​

ts
remove(...children): this;

Defined in: container.ts:234

Remove children from the list

Parameters ​

ParameterTypeDescription
...childrenContainer<ContainerOptions>[]Children to remove

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.remove


removeAllListener() ​

ts
removeAllListener(): this;

Defined in: event-emitter.ts:127

Remove all event listeners

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.removeAllListener


removeListener() ​

ts
removeListener(eventName, callback?): this;

Defined in: event-emitter.ts:105

Remove an event or multiple events

Parameters ​

ParameterTypeDescription
eventNamestring | string[]Name of event (or a list of event names) to remove
callback?EventCallback<any>Specify the callback to remove, if not defined remove all listeners

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.removeListener


render() ​

ts
render(): this;

Defined in: scene.ts:116

Draw the whole scene

Returns ​

this

Itself

Overrides ​

OffScreenCanvas.render


setContext() ​

ts
setContext(_ctx): this;

Defined in: container.ts:503

Set variables of the context according to specified options (override it to add behavior)

Parameters ​

ParameterTypeDescription
_ctxCanvasRenderingContext2DDrawing context

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.setContext


setCursor() ​

ts
setCursor(cursor?): this;

Defined in: scene.ts:107

Change the cursor displayed over the canvas

Parameters ​

ParameterTypeDefault valueDescription
cursor?string | nullComponent.cursors.defaultCursor string

Returns ​

this

Itself


setImageData() ​

ts
setImageData(imageData, positionDefinition?): this;

Defined in: offscreen-canvas.ts:144

Put data into the canvas

Parameters ​

ParameterTypeDescription
imageDataImageDataData to add to the canvas
positionDefinition?PositionDefinitionPosition of the data

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.setImageData


setImageSmoothing() ​

ts
setImageSmoothing(enable): this;

Defined in: offscreen-canvas.ts:43

Change the behavior for upscaled images, smoothing is good for pictures but bad for pixel-art

Parameters ​

ParameterTypeDescription
enablebooleanShould the smoothing be active or not

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.setImageSmoothing


setOptions() ​

ts
setOptions(options?): this;

Defined in: container.ts:177

Define options for this container

Parameters ​

ParameterTypeDescription
options?Partial<O>Options to override

Returns ​

this

Itself

Inherited from ​

OffScreenCanvas.setOptions


show() ​

ts
show(): this;

Defined in: scene.ts:180

Display it

Returns ​

this

Itself

Overrides ​

OffScreenCanvas.show


sortChildren() ​

ts
protected sortChildren(): void;

Defined in: container.ts:385

Sort children according to their zIndex

Returns ​

void

Inherited from ​

OffScreenCanvas.sortChildren


startLoop() ​

ts
startLoop(): this;

Defined in: scene.ts:153

Start to render the scene each frame

Returns ​

this

Itself


stopLoop() ​

ts
stopLoop(): this;

Defined in: scene.ts:165

Stop scene from being rendered

Returns ​

this

Itself


toGlobal() ​

ts
toGlobal(positionDefinition): Position;

Defined in: container.ts:360

Convert a position from this container's space into the root (scene) space

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionPosition relative to this container

Returns ​

Position

Inherited from ​

OffScreenCanvas.toGlobal


toImage() ​

ts
toImage(vectorDefinition?, type?): HTMLImageElement;

Defined in: offscreen-canvas.ts:155

Return an image composed of its content

Parameters ​

ParameterTypeDefault valueDescription
vectorDefinition?VectorDefinitionundefinedBox to restrict exported data
type?string"image/png"Data format. Supported format depend on the browser implementation (png or jpeg are the only safe choices)

Returns ​

HTMLImageElement

Inherited from ​

OffScreenCanvas.toImage


toJSON() ​

ts
toJSON(): object;

Defined in: offscreen-canvas.ts:176

Return a json ready object

Returns ​

children? ​
ts
children?: ContainerJSON[];

Children definitions

constructor ​
ts
constructor: string;

Name of the class

height ​
ts
height: number;
options? ​
ts
options?: Record<string, any>;

Options different from the default ones

position ​
ts
position: PositionDefinition;

Position in its parent

width ​
ts
width: number;

Inherited from ​

OffScreenCanvas.toJSON


toLocal() ​

ts
toLocal(positionDefinition): Position;

Defined in: container.ts:347

Convert a position from the root (scene) space into this container's space

Parameters ​

ParameterTypeDescription
positionDefinitionPositionDefinitionPosition relative to the root

Returns ​

Position

Inherited from ​

OffScreenCanvas.toLocal


from() ​

ts
static from(definition): Scene;

Defined in: scene.ts:341

Return an instance from a generic object

Parameters ​

ParameterTypeDescription
definitionContainerJSONScene definition

Returns ​

Scene

Overrides ​

OffScreenCanvas.from


getClipPath() ​

ts
static getClipPath(clip): Path2D;

Defined in: container.ts:489

Build the clipping path of a component, with its own transformations applied

Parameters ​

ParameterTypeDescription
clipComponentComponent used as clip

Returns ​

Path2D

Inherited from ​

OffScreenCanvas.getClipPath


getDrawingContext() ​

ts
static getDrawingContext(container?, height?): CanvasRenderingContext2D;

Defined in: scene.ts:350

Build a canvas filling the given element

Parameters ​

ParameterTypeDefault valueDescription
container?number | HTMLElementwindow.document.bodyElement holding the canvas
height?number1Canvas height (when the first argument is a width)

Returns ​

CanvasRenderingContext2D

Overrides ​

OffScreenCanvas.getDrawingContext


setOpacity() ​

ts
static setOpacity(ctx, opacity): void;

Defined in: container.ts:814

Define context opacity (null means no change)

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context
opacitynumber | nullOpacity value

Returns ​

void

Inherited from ​

OffScreenCanvas.setOpacity

Released under the MIT License.