File

src/toolbox/base/BaseShapeTool.ts

Extends

ToolBox

Index

Properties
Methods

Properties

Public autoClear
Default value : false
Inherited from ToolBox
Defined in ToolBox:9
Public history
Type : HistoryCallback[]
Default value : []
Inherited from ToolBox
Defined in ToolBox:8
Public Readonly layer
Type : Layer
Inherited from ToolBox
Defined in ToolBox:9

Methods

Public addPoint
addPoint()
Returns : this
Public clip
clip(options?: literal type)
Parameters :
Name Type Optional
options literal type Yes
Returns : this
Public fill
fill(options?: literal type)
Parameters :
Name Type Optional
options literal type Yes
Returns : this
Public move
move(x: number, y: number)
Parameters :
Name Type Optional
x number No
y number No
Returns : this
Public removePoint
removePoint()
Returns : this
Public setDashOffset
setDashOffset(offset: number)
Parameters :
Name Type Optional
offset number No
Returns : this
Public setFillColor
setFillColor(color: string | CanvasGradient | CanvasPattern)
Parameters :
Name Type Optional
color string | CanvasGradient | CanvasPattern No
Returns : this
Public setLineCap
setLineCap(lineCapStyle: CanvasLineCap)
Parameters :
Name Type Optional
lineCapStyle CanvasLineCap No
Returns : this
Public setLineDash
setLineDash(segments: number[])
Parameters :
Name Type Optional Default value
segments number[] No []
Returns : this
Public setLineJoinStyle
setLineJoinStyle(style: "round" | "bevel" | "miter")
Parameters :
Name Type Optional
style "round" | "bevel" | "miter" No
Returns : this
Public setLineWidth
setLineWidth(width: number)
Parameters :
Name Type Optional
width number No
Returns : this
Public setMiterLimit
setMiterLimit(limit: number)
Parameters :
Name Type Optional
limit number No
Returns : this
Public setStrokeColor
setStrokeColor(color: string | CanvasGradient | CanvasPattern)
Parameters :
Name Type Optional
color string | CanvasGradient | CanvasPattern No
Returns : this
Public stroke
stroke(options?: literal type)
Parameters :
Name Type Optional
options literal type Yes
Returns : this
Public render
render()
Inherited from ToolBox
Defined in ToolBox:27
Returns : void
Public restore
restore()
Inherited from ToolBox
Defined in ToolBox:19
Returns : this
Public save
save()
Inherited from ToolBox
Defined in ToolBox:11
Returns : this
import { Path2D } from "@napi-rs/canvas";
import { makeArgs } from "../../utils/makeArgs";
import { ToolBox } from "./ToolBox";

export class BaseShapeTool extends ToolBox {
    public setFillColor(color: string | CanvasGradient | CanvasPattern) {
        this.history.push((ctx) => {
            ctx.fillStyle = color;
        });

        return this;
    }

    public setStrokeColor(color: string | CanvasGradient | CanvasPattern) {
        this.history.push((ctx) => {
            ctx.strokeStyle = color;
        });

        return this;
    }

    public setLineWidth(width: number) {
        this.history.push((ctx) => {
            ctx.lineWidth = width;
        });

        return this;
    }

    public setLineCap(lineCapStyle: CanvasLineCap) {
        this.history.push((ctx) => {
            ctx.lineCap = lineCapStyle;
        });

        return this;
    }

    public setDashOffset(offset: number) {
        this.history.push((ctx) => {
            ctx.lineDashOffset = offset;
        });

        return this;
    }

    public setLineJoinStyle(style: "round" | "bevel" | "miter") {
        this.history.push((ctx) => {
            ctx.lineJoin = style;
        });

        return this;
    }

    public setMiterLimit(limit: number) {
        this.history.push((ctx) => {
            ctx.miterLimit = limit;
        });

        return this;
    }

    public addPoint() {
        this.history.push((ctx) => {
            ctx.beginPath();
        });

        return this;
    }

    public removePoint() {
        this.history.push((ctx) => {
            ctx.closePath();
        });

        return this;
    }

    public move(x: number, y: number) {
        this.history.push((ctx) => {
            ctx.moveTo(x, y);
        });

        return this;
    }

    public fill(options?: { fillRule?: "evenodd" | "nonzero"; path: Path2D }) {
        this.history.push((ctx) => {
            if (!options) return ctx.fill();
            ctx.fill(
                ...makeArgs(
                    (arg, idx) => (idx === 0 ? arg instanceof Path2D : idx === 1 ? typeof arg === "string" : false),
                    [options.path, options.fillRule]
                )
            );
        });

        return this;
    }

    public stroke(options?: { path: Path2D }) {
        this.history.push((ctx) => {
            options?.path ? ctx.stroke(options.path) : ctx.stroke();
        });

        return this;
    }

    public setLineDash(segments: number[] = []) {
        this.history.push((ctx) => {
            ctx.setLineDash(segments);
        });

        return this;
    }

    public clip(options?: { fillRule?: "evenodd" | "nonzero"; path?: Path2D }) {
        this.history.push((ctx) => {
            if (!options) return ctx.clip();
            ctx.clip(
                ...makeArgs(
                    (arg, idx) => (idx === 0 ? arg instanceof Path2D : idx === 1 ? typeof arg === "string" : false),
                    [options.path, options.fillRule]
                )
            );
        });

        return this;
    }
}

results matching ""

    No results matching ""