Skip to content

Component ​

Abstract class for visual component of a scene.

Examples ​

js
import { Component } from "pencil.js";

class FunnyShape extends Component {
    render (ctx) {
        // Do crazy stuff here
    }
}

ComponentOptions ​

Inherit from ContainerOptions.

NameTypeDefaultComment
fillColor or String"#000"Background Color used to fill, set to null for transparent
strokeColor or StringnullColor used to stroke, set to null for transparent
strokeWidthNumber2Stroke line thickness in pixels
dashedBoolean or ArrayfalseShould the line be dashed, you can also specify the dash pattern (ex: [4, 4] or Component.dashes.dots)
cursorStringComponent.cursors.defaultCursor to use when hover
joinStringComponent.joins.miterHow lines join between them
originPositionnew Position()Relative offset
shadowShadowOptions(see below)Set of options to set a shadow

ShadowOptions ​

NameTypeDefaultComment
blurNumber0Spread of the shadow around the component
positionPositionnew Position()Position of the shadow relative to the component
colorColor or StringnullColor of the shadow

Custom shapes ​

Extend Component and implement trace. By default, the path is traced again at every render. When the path only depends on a few values, declare them with pathDependencies: the path is then kept between renders until one of them changes.

js
import { Component } from "pencil.js";

class Cross extends Component {
    constructor (position, size, options) {
        super(position, options);
        this.size = size;
    }

    trace (path) {
        path.moveTo(0, 0);
        path.lineTo(this.size, this.size);
        path.moveTo(this.size, 0);
        path.lineTo(0, this.size);
        return this;
    }

    get pathDependencies () {
        return [this.size];
    }
}

A subclass redefining trace without its own pathDependencies is always traced again, so extending a shape never shows an outdated path.

Released under the MIT License.