File

src/toolbox/filters/Filter.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 applyFilter
applyFilter(filters: FilterArgs[])
Parameters :
Name Type Optional
filters FilterArgs[] No
Returns : this
Public brightness
brightness(amount: number)
Parameters :
Name Type Optional
amount number No
Returns : this
Public contrast
contrast(amount: number)
Parameters :
Name Type Optional
amount number No
Returns : this
Public dropShadow
dropShadow(x: string | number, y: string | number, radius: number, color: string)
Parameters :
Name Type Optional
x string | number No
y string | number No
radius number No
color string No
Returns : this
Public gaussianBlur
gaussianBlur(amount: number)
Parameters :
Name Type Optional Default value
amount number No 1
Returns : this
Public grayscale
grayscale(amount: number)
Parameters :
Name Type Optional Default value
amount number No 100
Returns : this
Public hueRotate
hueRotate(angle: number)
Parameters :
Name Type Optional
angle number No
Returns : this
Public invert
invert(amount: number)
Parameters :
Name Type Optional Default value
amount number No 100
Returns : this
Public opacity
opacity(amount: number)
Parameters :
Name Type Optional
amount number No
Returns : this
Public saturate
saturate(amount: number)
Parameters :
Name Type Optional
amount number No
Returns : this
Public sepia
sepia(amount: number)
Parameters :
Name Type Optional Default value
amount number No 100
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 { ToolBox } from "../base/ToolBox";

export type FilterArgs = { name: string; value: string | number };

export class FilterTool extends ToolBox {
    public applyFilter(filters: FilterArgs[]) {
        if (!Array.isArray(filters) || !filters.length) return this;
        this.history.push((ctx) => {
            ctx.filter = filters.map((m) => `${m.name}(${m.value})`).join(" ");
        });
        return this;
    }

    public gaussianBlur(amount = 1) {
        if (typeof amount !== "number") throw new TypeError("gaussian blur amount must be a number");
        return this.applyFilter([
            {
                name: "blur",
                value: amount
            }
        ]);
    }

    public brightness(amount: number) {
        if (typeof amount !== "number") throw new TypeError("brightness amount must be a number");
        return this.applyFilter([
            {
                name: "brightness",
                value: `${amount}%`
            }
        ]);
    }

    public contrast(amount: number) {
        if (typeof amount !== "number") throw new TypeError("contrast amount must be a number");
        return this.applyFilter([
            {
                name: "contrast",
                value: `${amount}%`
            }
        ]);
    }

    public dropShadow(x: string | number, y: string | number, radius: number, color: string) {
        return this.applyFilter([
            {
                name: "drop-shadow",
                value: `${x} ${y} ${radius} ${color}`
            }
        ]);
    }

    public grayscale(amount = 100) {
        if (typeof amount !== "number") throw new TypeError("grayscale amount must be a number");
        return this.applyFilter([
            {
                name: "grayscale",
                value: `${amount}%`
            }
        ]);
    }

    public hueRotate(angle: number) {
        if (typeof angle !== "number") throw new TypeError("hue rotate angle must be a number");
        return this.applyFilter([
            {
                name: "hue-rotate",
                value: `${angle}deg`
            }
        ]);
    }

    public invert(amount = 100) {
        if (typeof amount !== "number") throw new TypeError("invert amount must be a number");
        return this.applyFilter([
            {
                name: "invert",
                value: `${amount}%`
            }
        ]);
    }

    public opacity(amount: number) {
        if (typeof amount !== "number") throw new TypeError("opacity amount must be a number");
        return this.applyFilter([
            {
                name: "opacity",
                value: `${amount}%`
            }
        ]);
    }

    public saturate(amount: number) {
        if (typeof amount !== "number") throw new TypeError("saturation amount must be a number");
        return this.applyFilter([
            {
                name: "saturate",
                value: `${amount}%`
            }
        ]);
    }

    public sepia(amount = 100) {
        if (typeof amount !== "number") throw new TypeError("sepia amount must be a number");
        return this.applyFilter([
            {
                name: "sepia",
                value: `${amount}%`
            }
        ]);
    }
}

results matching ""

    No results matching ""