yugioh-card
Version:
<h1 align="center">🎉 游戏王卡片 - Yugioh Card 🎉</h1>
76 lines (75 loc) • 2.24 kB
JavaScript
import { isBrowser as e, isNode as t, loadFontBrowser as n, loadFontNode as r } from "../utils/index.js";
import i from "../svg/loader.js";
import a from "../svg/image.js";
import { Image as o, ImageEvent as s, Leafer as c, Text as l, useCanvas as u } from "leafer-unified";
//#region packages/src/card/index.js
var d = {
YugiohCard: "/yugioh/font",
YugiohSeries2Card: "/yugioh/font",
RushDuelCard: "/rush-duel/font"
}, f = () => {
l.changeAttr("lineHeight", {
type: "percent",
value: 1.15
});
}, p = class {
leafer = null;
imageStatusLeaf = null;
cardWidth = 100;
cardHeight = 100;
data = {};
view = null;
resourcePath = null;
skia = null;
constructor(e = {}) {
if (this.view = e.view, this.resourcePath = e.resourcePath, this.skia = e.skia, f(), t) {
if (!this.skia) throw Error("skia-canvas is required in Node environment");
u("skia", this.skia);
}
let i = d[this.tag];
i && (t ? r(`${this.resourcePath}${i}`, this.skia) : n(`${this.resourcePath}${i}`).then(() => {
this.draw();
}));
}
setData(e = {}) {
Object.assign(this.data, e), this.draw();
}
initLeafer() {
this.leafer = new c({
view: this.view,
width: this.cardWidth,
height: this.cardHeight
});
}
draw() {}
listenImageStatus(e) {
t || (e.on(s.LOAD, () => {
this.drawImageStatus(e, s.LOAD);
}), e.on(s.LOADED, () => {
this.drawImageStatus(e, s.LOADED);
}), e.on(s.ERROR, () => {
this.drawImageStatus(e, s.ERROR);
}));
}
drawImageStatus(e, t) {
let { url: n, width: r, height: c, x: l, y: u, zIndex: d } = e;
this.imageStatusLeaf || (this.imageStatusLeaf = new o(), this.leafer.add(this.imageStatusLeaf));
let f = "";
t === s.LOAD ? f = i : t === s.ERROR && (f = a), this.imageStatusLeaf.set({
url: f,
width: 120,
height: 120,
around: "center",
x: l + r / 2,
y: u + c / 2,
visible: [s.LOAD, s.ERROR].includes(t) && n,
zIndex: d + 1
});
}
updateScale() {
let t = e ? devicePixelRatio : 1;
this.leafer.pixelRatio = t, this.leafer.width = this.cardWidth * this.data.scale / t, this.leafer.height = this.cardHeight * this.data.scale / t, this.leafer.scaleX = this.data.scale / t, this.leafer.scaleY = this.data.scale / t;
}
};
//#endregion
export { p as Card };