illustrator.js
Version:
JavaScript image processing library
68 lines (67 loc) • 2.88 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Illustrator = void 0;
const canvas_1 = require("@napi-rs/canvas");
const IllustratorAnimation_1 = require("../animation/IllustratorAnimation");
const Colors_1 = require("../colors/Colors");
const IllustratorImageManager_1 = require("../image/IllustratorImageManager");
const LayerManager_1 = require("../layer/LayerManager");
class Illustrator {
constructor(width, height) {
this.width = width;
this.height = height;
this.layers = new LayerManager_1.LayerManager(this);
this.animation = new IllustratorAnimation_1.IllustratorAnimation(this);
this.colors = new Colors_1.Colors(this);
this.image = new IllustratorImageManager_1.IllustratorImageManager();
this.layers
.createLayer({
name: "background"
})
.lock();
}
get backgroundLayer() {
return this.layers.getLayer("background");
}
async render() {
const canvas = (0, canvas_1.createCanvas)(this.width, this.height);
const ctx = canvas.getContext("2d");
// render background layer first
// eslint-disable-next-line
const data = (await this.backgroundLayer.render());
// draw rendered layer on main canvas
if (data != null)
ctx.drawImage(data, this.backgroundLayer.coordinates.x, this.backgroundLayer.coordinates.y, this.backgroundLayer.width, this.backgroundLayer.height);
// render from top to bottom
const layers = this.layers.getAllLayers(true);
for (const layerConfig of layers) {
// skip background layer
if (layerConfig.name === "background")
continue;
// don't render if the layer is hidden
if (layerConfig.layer.hidden)
continue;
// eslint-disable-next-line
const data = (await layerConfig.layer.render());
if (data == null)
continue;
// draw rendered layer on main canvas
ctx.drawImage(data, layerConfig.layer.coordinates.x, layerConfig.layer.coordinates.y, layerConfig.layer.width, layerConfig.layer.height);
}
return { canvas, ctx };
}
async export(config = {}) {
const output = await this.render();
if (config.encoding == null || config.encoding === "png") {
return output.canvas.encode("png");
}
if (config.encoding === "avif") {
return output.canvas.encode("avif", config.avifConfig);
}
if (config.encoding === "jpeg" || config.encoding === "webp") {
return output.canvas.encode(config.encoding, config.quality);
}
throw new Error(`unsupported export encoding "${config.encoding}"`);
}
}
exports.Illustrator = Illustrator;