UNPKG

illustrator.js

Version:

JavaScript image processing library

52 lines (51 loc) 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ImageTool = void 0; const ImageLoader_1 = require("../../image/ImageLoader"); const ToolBox_1 = require("../base/ToolBox"); class ImageTool extends ToolBox_1.ToolBox { load(source) { return ImageLoader_1.ImageLoader.loadImage(source); } draw(image, dx, dy, dw, dh, circle) { // eslint-disable-next-line prefer-rest-params const args = arguments; this.history.push((ctx) => { ctx.save(); if (args.length === 4 && typeof dw === "boolean") { this.drawRounded(image, dx, dy, image.width, image.height, image.width / 2); return ctx.restore(); } if ([dw, dh].every((x) => typeof x === "number") && circle) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion this.drawRounded(image, dx, dy, dw, dh, dw / 2); return ctx.restore(); } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion if ([dw, dh].every((x) => typeof x === "number")) return ctx.drawImage(image, dx, dy, dw, dh); return ctx.drawImage(image, dx, dy); }); return this; } drawRounded(image, dx, dy, dw, dh, radius) { this.history.push((ctx) => { if (dw < 2 * radius) radius = dw / 2; if (dh < 2 * radius) radius = dh / 2; ctx.beginPath(); ctx.moveTo(dx + radius, dy); ctx.arcTo(dx + dw, dy, dx + dw, dy + dh, radius); ctx.arcTo(dx + dw, dy + dh, dx, dy + dh, radius); ctx.arcTo(dx, dy + dh, dx, dy, radius); ctx.arcTo(dx, dy, dx + dw, dy, radius); ctx.closePath(); ctx.clip(); ctx.drawImage(image, dx, dy, dw, dh); return ctx.restore(); }); return this; } } exports.ImageTool = ImageTool;