File

src/toolbox/shapes/Rectangle.ts

Extends

BaseShapeTool

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 draw
draw(options: RectangleOptions)
Parameters :
Name Type Optional
options RectangleOptions No
Returns : this
Public drawRounded
drawRounded(options: RoundedRectangleOptions)
Parameters :
Name Type Optional
options RoundedRectangleOptions No
Returns : this
Public addPoint
addPoint()
Inherited from BaseShapeTool
Defined in BaseShapeTool:62
Returns : this
Public clip
clip(options?: literal type)
Inherited from BaseShapeTool
Defined in BaseShapeTool:116
Parameters :
Name Type Optional
options literal type Yes
Returns : this
Public fill
fill(options?: literal type)
Inherited from BaseShapeTool
Defined in BaseShapeTool:86
Parameters :
Name Type Optional
options literal type Yes
Returns : this
Public move
move(x: number, y: number)
Inherited from BaseShapeTool
Defined in BaseShapeTool:78
Parameters :
Name Type Optional
x number No
y number No
Returns : this
Public removePoint
removePoint()
Inherited from BaseShapeTool
Defined in BaseShapeTool:70
Returns : this
Public setDashOffset
setDashOffset(offset: number)
Inherited from BaseShapeTool
Defined in BaseShapeTool:38
Parameters :
Name Type Optional
offset number No
Returns : this
Public setFillColor
setFillColor(color: string | CanvasGradient | CanvasPattern)
Inherited from BaseShapeTool
Defined in BaseShapeTool:6
Parameters :
Name Type Optional
color string | CanvasGradient | CanvasPattern No
Returns : this
Public setLineCap
setLineCap(lineCapStyle: CanvasLineCap)
Inherited from BaseShapeTool
Defined in BaseShapeTool:30
Parameters :
Name Type Optional
lineCapStyle CanvasLineCap No
Returns : this
Public setLineDash
setLineDash(segments: number[])
Inherited from BaseShapeTool
Defined in BaseShapeTool:108
Parameters :
Name Type Optional Default value
segments number[] No []
Returns : this
Public setLineJoinStyle
setLineJoinStyle(style: "round" | "bevel" | "miter")
Inherited from BaseShapeTool
Defined in BaseShapeTool:46
Parameters :
Name Type Optional
style "round" | "bevel" | "miter" No
Returns : this
Public setLineWidth
setLineWidth(width: number)
Inherited from BaseShapeTool
Defined in BaseShapeTool:22
Parameters :
Name Type Optional
width number No
Returns : this
Public setMiterLimit
setMiterLimit(limit: number)
Inherited from BaseShapeTool
Defined in BaseShapeTool:54
Parameters :
Name Type Optional
limit number No
Returns : this
Public setStrokeColor
setStrokeColor(color: string | CanvasGradient | CanvasPattern)
Inherited from BaseShapeTool
Defined in BaseShapeTool:14
Parameters :
Name Type Optional
color string | CanvasGradient | CanvasPattern No
Returns : this
Public stroke
stroke(options?: literal type)
Inherited from BaseShapeTool
Defined in BaseShapeTool:100
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 { BaseShapeTool } from "../base/BaseShapeTool";

export interface RectangleOptions {
    x: number;
    y: number;
    width: number;
    height: number;
}

export interface RoundedRectangleOptions {
    x: number;
    y: number;
    width: number;
    height: number;
    radius: number;
}

export class RectangleTool extends BaseShapeTool {
    public draw(options: RectangleOptions) {
        this.history.push((ctx) => {
            ctx.rect(options.x, options.y, options.width, options.height);
        });

        return this;
    }

    public drawRounded(options: RoundedRectangleOptions) {
        if (options.width < 2 * options.radius) options.radius = options.width / 2;
        if (options.height < 2 * options.radius) options.radius = options.height / 2;

        this.history.push((ctx) => {
            ctx.beginPath();
            ctx.moveTo(options.x + options.radius, options.y);
            ctx.arcTo(
                options.x + options.width,
                options.y,
                options.x + options.width,
                options.y + options.height,
                options.radius
            );
            ctx.arcTo(
                options.x + options.width,
                options.y + options.height,
                options.x,
                options.y + options.height,
                options.radius
            );
            ctx.arcTo(options.x, options.y + options.height, options.x, options.y, options.radius);
            ctx.arcTo(options.x, options.y, options.x + options.width, options.y, options.radius);
            ctx.closePath();
        });

        return this;
    }
}

results matching ""

    No results matching ""