@nmmty/lazycanvas
Version:
A simple way to interact with @napi-rs/canvas in an advanced way!
77 lines (76 loc) • 2.48 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pattern = void 0;
const enum_1 = require("../../types/enum");
const LazyCanvas_1 = require("../LazyCanvas");
const canvas_1 = require("@napi-rs/canvas");
const jimp = __importStar(require("jimp"));
class Pattern {
type;
src;
constructor() {
this.type = enum_1.PatternType.Repeat;
this.src = '';
}
/**
* Set the type of the pattern
* @param type {AnyPatternType} - The `type` of the pattern
*/
setType(type) {
this.type = type;
return this;
}
/**
* Set the source of the pattern
* @param src {string | LazyCanvas} - The `src` of the pattern
*/
setSrc(src) {
this.src = src;
return this;
}
async draw(ctx) {
if (this.src instanceof LazyCanvas_1.LazyCanvas) {
let jmp = await this.src.render.render();
let image = await (0, canvas_1.loadImage)(jmp);
return ctx.createPattern(image, this.type);
}
else {
let jmp = await jimp.read(this.src);
let image = await (0, canvas_1.loadImage)(await jmp.getBufferAsync('image/png'));
return ctx.createPattern(image, this.type);
}
}
/**
* @returns {IPattern}
*/
toJSON() {
return {
type: this.type,
src: this.src
};
}
}
exports.Pattern = Pattern;