91 lines
2.6 KiB
JavaScript
91 lines
2.6 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.BaseClass = void 0;
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const utils_1 = require("./utils");
|
|
class BaseClass {
|
|
constructor() {
|
|
this.options = {};
|
|
this._renderedTime = 0;
|
|
this._before = [0, 0];
|
|
}
|
|
get hasRendered() {
|
|
return !!this._canvas;
|
|
}
|
|
get canvas() {
|
|
this._checkHasRendered();
|
|
return this._canvas;
|
|
}
|
|
get renderedTime() {
|
|
this._checkHasRendered();
|
|
return this._renderedTime;
|
|
}
|
|
get width() {
|
|
this._checkHasRendered();
|
|
return this.canvas.width;
|
|
}
|
|
get height() {
|
|
this._checkHasRendered();
|
|
return this.canvas.height;
|
|
}
|
|
render() {
|
|
return this;
|
|
}
|
|
toDataUrl(mineType = "image/png", options) {
|
|
this._checkHasRendered();
|
|
if (mineType === "image/png") {
|
|
return this.canvas.toDataURL("image/png");
|
|
}
|
|
else {
|
|
return this.canvas.toDataURL("image/jpeg", options === null || options === void 0 ? void 0 : options.quality);
|
|
}
|
|
}
|
|
toBuffer(mineType = "image/png", options) {
|
|
this._checkHasRendered();
|
|
if (mineType === "image/png") {
|
|
return this.canvas.toBuffer("image/png", options);
|
|
}
|
|
else {
|
|
return this.canvas.toBuffer("image/jpeg", options);
|
|
}
|
|
}
|
|
toFile(filename, mineType = "", options) {
|
|
this._checkHasRendered();
|
|
if (!mineType) {
|
|
if (filename.match(/\.jpg$|\.jpeg$/i)) {
|
|
mineType = "image/jpeg";
|
|
}
|
|
else {
|
|
mineType = "image/png";
|
|
}
|
|
}
|
|
const buffer = this.toBuffer(mineType, options);
|
|
fs_1.default.writeFileSync(filename, buffer);
|
|
}
|
|
toStream(mineType = "image/png", options) {
|
|
this._checkHasRendered();
|
|
if (mineType === "image/png") {
|
|
return this.canvas.createPNGStream(options);
|
|
}
|
|
else {
|
|
return this.canvas.createJPEGStream(options);
|
|
}
|
|
}
|
|
_checkHasRendered() {
|
|
if (!this.hasRendered) {
|
|
throw new Error(`Please run render() first!`);
|
|
}
|
|
}
|
|
_startTimer() {
|
|
this._before = (0, utils_1.hrtime)();
|
|
}
|
|
_endTimer() {
|
|
const diff = (0, utils_1.hrtime)(this._before);
|
|
this._renderedTime = diff[0] * 1000 + (diff[1] / 1000000);
|
|
}
|
|
}
|
|
exports.BaseClass = BaseClass;
|