Skip to content

pencil.js / path / Path

Class: Path<O> ​

Defined in: path.ts:151

Free shape following a list of instructions (or a SVG path string)

Extends ​

Type Parameters ​

Type ParameterDefault type
O extends ComponentOptionsComponentOptions

Constructors ​

Constructor ​

ts
new Path<O>(
   positionDefinition?, 
   instructions?, 
   isClosed?, 
   options?
): Path<O>;

Defined in: path.ts:175

Path constructor

Parameters ​

ParameterTypeDefault valueDescription
positionDefinition?PositionDefinitionundefinedStarting position of the Path
instructions?string | Instruction[][]Set of instructions to follow to draw it (or a SVG path string)
isClosed?booleantrueShould the path close itself (add a line to the starting position)
options?Partial<LineOptions | O>undefinedDrawing options

Returns ​

Path<O>

Example ​

ts
new Path([0, 0], [
    Path.lineTo([100, 0]),
    Path.quarterTo([100, 100]),
]);
new Path([0, 0], "l100 0 a50 50 0 0 1 0 100");

Overrides ​

Component.constructor

Properties ​

PropertyTypeDescriptionInherited fromDefined in
childrenContainer<ContainerOptions>[]List of childrenComponent.childrencontainer.ts:134
eventListenersRecord<string, EventListener[]>Set of event listeners of this componentComponent.eventListenersevent-emitter.ts:33
frameCountnumberNumber of time this container has been renderedComponent.frameCountcontainer.ts:140
instructionsstring | Instruction[]Set of instructions (or a SVG path string)-path.ts:157
isClickedbooleanIs currently clickedComponent.isClickedcomponent.ts:177
isClosedbooleanShould the path close itself-path.ts:160
isDraggable?booleanIs the component draggableComponent.isDraggabledraggable.ts:37
isDragged?booleanIs the component currently draggedComponent.isDraggeddraggable.ts:39
isHoveredbooleanIs currently hoveredComponent.isHoveredcomponent.ts:180
isRotatable?booleanIs the component rotatableComponent.isRotatablerotatable.ts:18
isRotated?booleanIs the component currently rotatedComponent.isRotatedrotatable.ts:20
optionsOSet of optionsComponent.optionscontainer.ts:131
parent| Container<ContainerOptions> | nullParent container (null when not attached)Component.parentcontainer.ts:137
pathPath2D | nullLast traced pathComponent.pathcomponent.ts:174
positionPositionPosition in its containerComponent.positioncontainer.ts:128

Accessors ​

isScene ​

Get Signature ​

ts
get isScene(): boolean;

Defined in: container.ts:143

Is this the root scene

Returns ​

boolean

Inherited from ​

Component.isScene


pathDependencies ​

Get Signature ​

ts
get protected pathDependencies(): readonly unknown[] | null;

Defined in: component.ts:328

Values the path depends on (the path is kept between renders while they don't change). Return null when the path must be traced at every render (the default). Override it alongside "trace" when the traced path only depends on a few values.

Example ​
ts
protected override get pathDependencies () {
    return [this.width, this.height];
}
Returns ​

readonly unknown[] | null

Inherited from ​

Component.pathDependencies


willFill ​

Get Signature ​

ts
get willFill(): boolean;

Defined in: component.ts:231

Tell if a component will need to be filled

Returns ​

boolean

Inherited from ​

Component.willFill


willStroke ​

Get Signature ​

ts
get willStroke(): boolean;

Defined in: component.ts:238

Tell if a component will need to be stroked

Returns ​

boolean

Inherited from ​

Component.willStroke


className ​

Get Signature ​

ts
get static className(): string;

Defined in: path.ts:152

Name used when serializing (survive minification)

Returns ​

string

Overrides ​

Component.className


cursors ​

Get Signature ​

ts
get static cursors(): object;

Defined in: component.ts:475

All available cursors https://developer.mozilla.org/docs/Web/CSS/cursor

Returns ​

object

alias ​
ts
alias: string = "alias";
allScroll ​
ts
allScroll: string = "all-scroll";
bottomResize ​
ts
bottomResize: string = cssCursors.sResize;
cell ​
ts
cell: string = "cell";
colResize ​
ts
colResize: string = "col-resize";
contextMenu ​
ts
contextMenu: string = "context-menu";
copy ​
ts
copy: string = "copy";
crosshair ​
ts
crosshair: string = "crosshair";
default ​
ts
default: string = "default";
eResize ​
ts
eResize: string = "e-resize";
ewResize ​
ts
ewResize: string = "ew-resize";
grab ​
ts
grab: string = "grab";
grabbing ​
ts
grabbing: string = "grabbing";
help ​
ts
help: string = "help";
horizontalResize ​
ts
horizontalResize: string = cssCursors.colResize;
leftResize ​
ts
leftResize: string = cssCursors.wResize;
ts
link: string = cssCursors.alias;
move ​
ts
move: string = "move";
neResize ​
ts
neResize: string = "ne-resize";
neswResize ​
ts
neswResize: string = "nesw-resize";
noDrop ​
ts
noDrop: string = "no-drop";
none ​
ts
none: string = "none";
notAllowed ​
ts
notAllowed: string = "not-allowed";
nResize ​
ts
nResize: string = "n-resize";
nsResize ​
ts
nsResize: string = "ns-resize";
nwResize ​
ts
nwResize: string = "nw-resize";
nwseResize ​
ts
nwseResize: string = "nwse-resize";
pointer ​
ts
pointer: string = "pointer";
progress ​
ts
progress: string = "progress";
rightResize ​
ts
rightResize: string = cssCursors.eResize;
rowResize ​
ts
rowResize: string = "row-resize";
seResize ​
ts
seResize: string = "se-resize";
sResize ​
ts
sResize: string = "s-resize";
swResize ​
ts
swResize: string = "sw-resize";
text ​
ts
text: string = "text";
textVertical ​
ts
textVertical: string = "vertical-text";
topResize ​
ts
topResize: string = cssCursors.nResize;
verticalResize ​
ts
verticalResize: string = cssCursors.rowResize;
wait ​
ts
wait: string = "wait";
wResize ​
ts
wResize: string = "w-resize";
zoomIn ​
ts
zoomIn: string = "zoom-in";
zoomOut ​
ts
zoomOut: string = "zoom-out";

Inherited from ​

Component.cursors


dashes ​

Get Signature ​

ts
get static dashes(): object;

Defined in: component.ts:489

Dash patterns

Returns ​
default ​
ts
default: number[];

Simple dash

dots ​
ts
dots: number[];

Simple dots

long ​
ts
long: number[];

Longer dash

sewing ​
ts
sewing: number[];

Alternating pattern of short and long dash

Inherited from ​

Component.dashes


defaultOptions ​

Get Signature ​

ts
get static defaultOptions(): ComponentOptions;

Defined in: component.ts:452

Default options for this class

Returns ​

ComponentOptions

Inherited from ​

Component.defaultOptions


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>

Inherited from ​

Component.events


joins ​

Get Signature ​

ts
get static joins(): object;

Defined in: component.ts:482

How lines join between them

Returns ​
bevel ​
ts
readonly bevel: "bevel" = "bevel";

Join with a straight line between the line edges

miter ​
ts
readonly miter: "miter" = "miter";

Join segment by extending the line edges until they meet

round ​
ts
readonly round: "round" = "round";

Join with a circle tangent to line edges

Inherited from ​

Component.joins

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 ​

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

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

Component.applyTransform


buildPath() ​

ts
protected buildPath(): Path2D;

Defined in: component.ts:312

Trace and return a new path

Returns ​

Path2D

Inherited from ​

Component.buildPath


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 ​

Component.climbAncestry


clone() ​

ts
clone(): this;

Defined in: container.ts:788

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

Returns ​

this

Inherited from ​

Component.clone


delete() ​

ts
delete(): this;

Defined in: container.ts:259

Remove itself from its parent

Returns ​

this

Itself

Inherited from ​

Component.delete


draggable() ​

ts
draggable(options?): DraggableAPI;

Defined in: draggable.ts:44

Make the component draggable (need to import "pencil.js/draggable")

Parameters ​

ParameterTypeDescription
options?DraggableOptionsAdditional options

Returns ​

DraggableAPI

Inherited from ​

Component.draggable


empty() ​

ts
empty(): this;

Defined in: container.ts:251

Remove all its children

Returns ​

this

Itself

Inherited from ​

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

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

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

Component.getAbsolutePosition


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 ​

Component.getMatrix


getOrigin() ​

ts
getOrigin(): Position;

Defined in: component.ts:201

Return the relative origin position of draw

Returns ​

Position

Inherited from ​

Component.getOrigin


getPath() ​

ts
getPath(): Path2D;

Defined in: component.ts:355

Return the path of this component, traced again only when needed

Returns ​

Path2D

Inherited from ​

Component.getPath


getRoot() ​

ts
getRoot(): Container;

Defined in: container.ts:299

Return its highest parent

Returns ​

Container

Inherited from ​

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

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

Component.getTarget


hide() ​

ts
hide(): this;

Defined in: container.ts:718

Hide it

Returns ​

this

Itself

Inherited from ​

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

Component.isAncestorOf


isHover() ​

ts
isHover(positionDefinition?, ctx?): boolean;

Defined in: component.ts:425

Define if is hover a position

Parameters ​

ParameterTypeDescription
positionDefinition?PositionDefinitionAny position
ctx?CanvasRenderingContext2DContext use for calculation (with transformations already applied)

Returns ​

boolean

Inherited from ​

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

Component.isInClip


isPathDirty() ​

ts
protected isPathDirty(): boolean;

Defined in: component.ts:335

Tell if the path needs to be traced again

Returns ​

boolean

Inherited from ​

Component.isPathDirty


makePath() ​

ts
makePath(ctx): this;

Defined in: component.ts:393

Set the context and draw the component

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context

Returns ​

this

Itself

Inherited from ​

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

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

Component.once


paint() ​

ts
protected paint(ctx): void;

Defined in: component.ts:366

Fill and stroke the component (the context is already set)

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context

Returns ​

void

Inherited from ​

Component.paint


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 ​

Component.remove


removeAllListener() ​

ts
removeAllListener(): this;

Defined in: event-emitter.ts:127

Remove all event listeners

Returns ​

this

Itself

Inherited from ​

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

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

Inherited from ​

Component.render


rotatable() ​

ts
rotatable(): RotatableAPI;

Defined in: rotatable.ts:24

Make the component rotatable (need to import "pencil.js/rotatable")

Returns ​

RotatableAPI

Inherited from ​

Component.rotatable


setContext() ​

ts
setContext(ctx): this;

Defined in: component.ts:274

Set drawing variables of the context according to specified options

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context

Returns ​

this

Itself

Inherited from ​

Component.setContext


setLineContext() ​

ts
protected setLineContext(ctx): void;

Defined in: component.ts:246

Set line related variables of the context

Parameters ​

ParameterTypeDescription
ctxCanvasRenderingContext2DDrawing context

Returns ​

void

Inherited from ​

Component.setLineContext


setOptions() ​

ts
setOptions(options?): this;

Defined in: component.ts:211

Define options for this component

Parameters ​

ParameterTypeDescription
options?Partial<O>Options to override

Returns ​

this

Itself

Inherited from ​

Component.setOptions


show() ​

ts
show(): this;

Defined in: container.ts:708

Display it

Returns ​

this

Itself

Inherited from ​

Component.show


sortChildren() ​

ts
protected sortChildren(): void;

Defined in: container.ts:385

Sort children according to their zIndex

Returns ​

void

Inherited from ​

Component.sortChildren


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 ​

Component.toGlobal


toJSON() ​

ts
toJSON(): ContainerJSON;

Defined in: path.ts:221

Return a json ready object

Returns ​

ContainerJSON

Overrides ​

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

Component.toLocal


trace() ​

ts
trace(path): this;

Defined in: path.ts:201

Draw the path

Parameters ​

ParameterTypeDescription
pathPath2DDrawing path

Returns ​

this

Itself

Overrides ​

Component.trace


arcTo() ​

ts
static arcTo(
   position, 
   angle, 
   magicRatio, 
   clockwise?
): Instruction;

Defined in: path.ts:286

Try to approximate an arc between two points

Parameters ​

ParameterTypeDefault valueDescription
positionPositionDefinitionundefinedAny position
anglenumberundefinedArc angle in ratio of a full circle (should be less than 0.5)
magicRationumberundefinedControl points "openness" ratio
clockwise?booleantrueShould the arc be clockwise or not

Returns ​

Instruction


bezierTo() ​

ts
static bezierTo(
   position, 
   firstControlPoint, 
   secondControlPoint
): Instruction;

Defined in: path.ts:310

Draw a bezier curve to a position

Parameters ​

ParameterTypeDescription
positionPositionDefinitionAny position
firstControlPointPositionDefinitionFirst point to control the curve
secondControlPointPositionDefinitionSecond point to control the curve

Returns ​

Instruction


from() ​

ts
static from(definition): Path;

Defined in: path.ts:234

Return an instance from a generic object

Parameters ​

ParameterTypeDescription
definitionContainerJSONPath definition

Returns ​

Path

Overrides ​

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

Component.getClipPath


halfTo() ​

ts
static halfTo(position, clockwise?): Instruction;

Defined in: path.ts:273

Draw a half circle to a position

Parameters ​

ParameterTypeDefault valueDescription
positionPositionDefinitionundefinedAny position
clockwise?booleantrueShould the arc be clockwise or not

Returns ​

Instruction


lineTo() ​

ts
static lineTo(position): Instruction;

Defined in: path.ts:245

Draw a line to a position

Parameters ​

ParameterTypeDescription
positionPositionDefinitionAny position

Returns ​

Instruction


moveTo() ​

ts
static moveTo(position): Instruction;

Defined in: path.ts:253

Move the pencil without drawing

Parameters ​

ParameterTypeDescription
positionPositionDefinitionAny position

Returns ​

Instruction


quadTo() ​

ts
static quadTo(position, controlPoint): Instruction;

Defined in: path.ts:297

Draw a quadratic curve to a position

Parameters ​

ParameterTypeDescription
positionPositionDefinitionAny position
controlPointPositionDefinitionPoint that control the curve

Returns ​

Instruction


quarterTo() ​

ts
static quarterTo(position, clockwise?): Instruction;

Defined in: path.ts:262

Draw a quarter circle to a position

Parameters ​

ParameterTypeDefault valueDescription
positionPositionDefinitionundefinedAny position
clockwise?booleantrueShould the arc be clockwise or not

Returns ​

Instruction


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 ​

Component.setOpacity


sinTo() ​

ts
static sinTo(
   position, 
   nbSins?, 
   sinsHeight?
): Instruction;

Defined in: path.ts:375

Draw a sinusoid to a position

Parameters ​

ParameterTypeDefault valueDescription
positionPositionDefinitionundefinedAny position
nbSins?number1Number of sinusoid to draw
sinsHeight?number10Height of the sinusoid

Returns ​

Instruction


splineThrough() ​

Call Signature ​

ts
static splineThrough(points, tension?): Instruction;

Defined in: path.ts:327

Draw a smooth curve going through a set of positions

Parameters ​
ParameterTypeDescription
pointsPositionDefinition[]Any set of positions (the last one being the target)
tension?numberRatio of tension
Returns ​

Instruction

Call Signature ​

ts
static splineThrough(
   target, 
   points, 
   tension?
): Instruction;

Defined in: path.ts:334

Draw a smooth curve going through a set of positions (serialized form)

Parameters ​
ParameterTypeDescription
targetPositionDefinitionLast position
pointsPositionDefinition[]Every positions to go through before the target
tension?numberRatio of tension
Returns ​

Instruction


waveTo() ​

ts
static waveTo(position, nbWaves?): Instruction;

Defined in: path.ts:356

Draw a series of half circles to a position

Parameters ​

ParameterTypeDefault valueDescription
positionPositionDefinitionundefinedAny position
nbWaves?number1Number of waves to draw

Returns ​

Instruction

Released under the MIT License.