illustrator.js
Version:
JavaScript image processing library
125 lines (124 loc) • 6.67 kB
JavaScript
"use strict";
var _IllustratorAnimation_instances, _IllustratorAnimation_frames, _IllustratorAnimation_encoderConfig, _IllustratorAnimation_renderEachLayer, _IllustratorAnimation_addFrame;
Object.defineProperty(exports, "__esModule", { value: true });
exports.IllustratorAnimation = void 0;
const tslib_1 = require("tslib");
const Layer_1 = require("../layer/Layer");
const gifenc_1 = require("@skyra/gifenc");
class IllustratorAnimation {
constructor(illustrator) {
this.illustrator = illustrator;
_IllustratorAnimation_instances.add(this);
_IllustratorAnimation_frames.set(this, null);
_IllustratorAnimation_encoderConfig.set(this, {});
}
clearFrames() {
tslib_1.__classPrivateFieldSet(this, _IllustratorAnimation_frames, null, "f");
}
addFrame(layer, duration) {
if (!arguments.length || layer == null)
throw new Error("frame args required");
let frame = null;
if (layer instanceof Layer_1.Layer)
frame = {
frame: layer,
duration: duration ?? 1000
};
if (typeof layer === "object")
frame = {
duration: layer.duration ?? 1000,
frame: layer.frame
};
if (!frame)
throw new Error("missing frame data");
return this.addFrames([frame]);
}
addFrames(layers) {
if (!layers.length)
throw new Error("Frames required");
if (!layers.every((l) => l.frame instanceof Layer_1.Layer))
throw new TypeError("frame must be a Layer instance");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_instances, "m", _IllustratorAnimation_addFrame).call(this, layers);
return this;
}
setFrameRate(frameRate) {
if (typeof frameRate !== "number")
throw new TypeError("frame rate value must be a number");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").framerate = frameRate;
return this;
}
setTransparency(transparent) {
if (typeof transparent !== "number")
throw new TypeError("transparency value must be a number");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").transparent = transparent;
return this;
}
setFrameDelay(delay) {
if (typeof delay !== "number")
throw new TypeError("delay value must be a number");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").delay = delay;
return this;
}
setDisposalCode(code) {
if (typeof code !== "number")
throw new TypeError("disposal code value must be a number");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").dispose = code;
return this;
}
setQuality(quality) {
if (typeof quality !== "number")
throw new TypeError("quality value must be a number");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").quality = quality;
return this;
}
setRepeat(repeats) {
if (typeof repeats !== "number")
throw new TypeError("repeats value must be a number");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").repeat = repeats;
return this;
}
async createAnimation() {
if (!tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_frames, "f")?.length)
throw new Error("missing animation frames data");
const encoder = new gifenc_1.GifEncoder(this.illustrator.width, this.illustrator.height);
const frames = await tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_instances, "m", _IllustratorAnimation_renderEachLayer).call(this);
const gifStream = encoder.createReadStream();
if (tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").dispose != null)
encoder.setDispose(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").dispose);
if (tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").framerate != null)
encoder.setFramerate(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").framerate);
if (tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").quality != null)
encoder.setQuality(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").quality);
if (tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").repeat != null)
encoder.setRepeat(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").repeat);
if (tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").transparent != null)
encoder.setTransparent(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").transparent);
encoder.start();
for (const frame of frames) {
encoder.setDelay(frame.duration ?? tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_encoderConfig, "f").delay);
encoder.addFrame(frame.ctx.getImageData(0, 0, frame.ctx.canvas.width, frame.ctx.canvas.height).data);
}
encoder.finish();
return gifStream;
}
}
exports.IllustratorAnimation = IllustratorAnimation;
_IllustratorAnimation_frames = new WeakMap(), _IllustratorAnimation_encoderConfig = new WeakMap(), _IllustratorAnimation_instances = new WeakSet(), _IllustratorAnimation_renderEachLayer = async function _IllustratorAnimation_renderEachLayer() {
if (!tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_frames, "f"))
return [];
const canvasArray = await Promise.all(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_frames, "f")
.filter((frame) => !frame.frame.hidden)
.map(async (m) => ({
canvas: await m.frame.render(),
duration: m.duration
})));
return canvasArray.map((m) => ({
duration: m.duration,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
ctx: m.canvas.getContext("2d")
}));
}, _IllustratorAnimation_addFrame = function _IllustratorAnimation_addFrame(frames) {
if (!Array.isArray(tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_frames, "f")))
tslib_1.__classPrivateFieldSet(this, _IllustratorAnimation_frames, [], "f");
tslib_1.__classPrivateFieldGet(this, _IllustratorAnimation_frames, "f").push(...frames);
};