Represents an AbstractComponent made up of one or more Paths.

For some Editor editor and Stroke stroke,

Restyling:

editor.dispatch(stroke.updateStyle({ color: Color4.red }));

Transforming:

editor.dispatch(stroke.transformBy(Mat33.translation(Vec2.of(10, 0))));

Adding:

import {
	Editor, EditorImage, Stroke, pathToRenderable.
	Path, Color4,
} from 'js-draw';

const editor = new Editor(document.body);

const stroke = new Stroke([
	pathToRenderable(Path.fromString('m0,0 l100,100 l0,-10 z'), { fill: Color4.red }),
]);
editor.dispatch(EditorImage.addElement(stroke));

Hierarchy (view full)

Implements

Constructors

  • Creates a Stroke from the given parts. All parts should have the same color.

    Parameters

    Returns Stroke

    // A path that starts at (1,1), moves to the right by (2, 0),
    // then moves down and right by (3, 3)
    const path = Path.fromString('m1,1 2,0 3,3');

    const stroke = new Stroke([
    // Fill with red
    pathToRenderable(path, { fill: Color4.red })
    ]);

Properties

contentBBox: Rect2

The bounding box of this component. getBBox, by default, returns contentBBox. This must be set by components.

If this changes, EditorImage.queueRerenderOf should be called for this object (provided that this object has been added to the editor.)

Note: This value is ignored if getSizingMode returns FillScreen or FillImage.

lastChangedTime: number

The timestamp (milliseconds) at which the component was last changed (i.e. created/translated).

Methods

  • Returns Readonly<RenderablePathSpecWithPath>[]

    A list of the parts that make up this path. Many paths only have one part.

    Each part (a RenderablePathSpec) contains information about the style and geometry of that part of the stroke. Use the .path property to do collision detection and other operations involving the stroke's geometry.

    Note that many of Path's methods (e.g. Path.intersection) take a strokeWidth parameter that can be gotten from RenderablePathSpec.style .stroke.width.

  • Parameters

    Returns boolean

    true if this component intersects rect -- it is entirely contained within the rectangle or one of the rectangle's edges intersects this component.

    The default implementation assumes that this.getExactBBox() returns a tight bounding box -- that any horiziontal/vertical line that intersects this' boounding box also intersects a point in this component. If this is not the case, components must override this function.

  • Renders this component onto the given canvas.

    If visibleRect is given, it should be the region of canvas that is visible -- rendering anything outside of visibleRect should have no visible effect on the resultant image.

    For optimal performance, implementers should call canvas.startObject and canvas.endObject before and after rendering.

    Parameters

    Returns void

  • Convert the component to an object that can be passed to JSON.stringify.

    Do not rely on the output of this function to take a particular form — this function's output can change form without a major version increase.

    Returns {
        data:
            | string
            | number
            | any[]
            | Record<string, any>;
        id: string;
        loadSaveData: LoadSaveDataTable;
        name: string;
        zIndex: number;
    }

    • data:
          | string
          | number
          | any[]
          | Record<string, any>
    • id: string
    • loadSaveData: LoadSaveDataTable
    • name: string
    • zIndex: number
  • Return null iff this object cannot be safely serialized/deserialized.

    Returns {
        path: string;
        style: {
            fill: string;
            stroke: undefined | {
                color: string;
                width: number;
            };
        };
    }[]

OpenSource licenses