Skip to content

pencil.js / container / Container

Class: Container<O> ​

Defined in: container.ts:121

Basic node in the scene graph, holding children and their common transformations

Extends ​

Extended by ​

Type Parameters ​

Type ParameterDefault type
O extends ContainerOptionsContainerOptions

Constructors ​

Constructor ​

ts
new Container<O>(positionDefinition?, options?): Container<O>;

Defined in: container.ts:161

Container constructor

Parameters ​

ParameterTypeDescription
positionDefinition?PositionDefinitionPosition in its container
options?Partial<O>Specific options

Returns ​

Container<O>

Overrides ​

EventEmitter.constructor

Properties ​

PropertyTypeDescriptionInherited fromDefined in
childrenContainer<ContainerOptions>[]List of children-container.ts:134
eventListenersRecord<string, EventListener[]>Set of event listeners of this componentEventEmitter.eventListenersevent-emitter.ts:33
frameCountnumberNumber of time this container has been rendered-container.ts:140
optionsOSet of options-container.ts:131
parent| Container<ContainerOptions> | nullParent container (null when not attached)-container.ts:137
positionPositionPosition in its container-container.ts:128

Accessors ​

isScene ​

Get Signature ​

ts
get isScene(): boolean;

Defined in: container.ts:143

Is this the root scene

Returns ​

boolean


className ​

Get Signature ​

ts
get static className(): string;

Defined in: container.ts:123

Name used when serializing (survive minification)

Returns ​

string


defaultOptions ​

Get Signature ​

ts
get static defaultOptions(): ContainerOptions;

Defined in: container.ts:831

Default options for this class

Returns ​

ContainerOptions


events ​

Get Signature ​

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

Defined in: container.ts:846

Set of events for easy access

Returns ​

Record<string, string>

Overrides ​

EventEmitter.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


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


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


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


clone() ​

ts
clone(): this;

Defined in: container.ts:788

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

Returns ​

this


delete() ​

ts
delete(): this;

Defined in: container.ts:259

Remove itself from its parent

Returns ​

this

Itself


empty() ​

ts
empty(): this;

Defined in: container.ts:251

Remove all its children

Returns ​

this

Itself


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

Overrides ​

EventEmitter.fire


getAbsoluteMatrix() ​

ts
getAbsoluteMatrix(): DOMMatrix;

Defined in: container.ts:330

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

Returns ​

DOMMatrix


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


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


getRoot() ​

ts
getRoot(): Container;

Defined in: container.ts:299

Return its highest parent

Returns ​

Container


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>>


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


hide() ​

ts
hide(): this;

Defined in: container.ts:718

Hide it

Returns ​

this

Itself


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


isHover() ​

ts
isHover(_positionDefinition?, _ctx?): boolean;

Defined in: container.ts:197

Container can't be hovered

Parameters ​

ParameterTypeDescription
_positionDefinition?PositionDefinitionAny position
_ctx?CanvasRenderingContext2DDrawing context

Returns ​

boolean


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


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


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 ​

EventEmitter.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 ​

EventEmitter.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


removeAllListener() ​

ts
removeAllListener(): this;

Defined in: event-emitter.ts:127

Remove all event listeners

Returns ​

this

Itself

Inherited from ​

EventEmitter.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 ​

EventEmitter.removeListener


render() ​

ts
render(ctx): this;

Defined in: container.ts:512

Draw itself and all its children

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context (its current transformation and opacity are used as starting point)

Returns ​

this

Itself


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


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


show() ​

ts
show(): this;

Defined in: container.ts:708

Display it

Returns ​

this

Itself


sortChildren() ​

ts
protected sortChildren(): void;

Defined in: container.ts:385

Sort children according to their zIndex

Returns ​

void


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


toJSON() ​

ts
toJSON(): ContainerJSON;

Defined in: container.ts:755

Return a json ready object

Returns ​

ContainerJSON


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


from() ​

ts
static from(definition): Container;

Defined in: container.ts:824

Return an instance from a generic object

Parameters ​

ParameterTypeDescription
definitionContainerJSONContainer definition

Returns ​

Container


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


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

Released under the MIT License.