Render in Node.js
Put the assets in card-assets/ next to your script. Install yugioh-card and skia-canvas, save the example as render.mjs, and run node render.mjs. The PNG files appear in output/ next to the script.
mjs
import { mkdir, writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import skia from 'skia-canvas';
import { YugiohCard } from 'yugioh-card';
const resourcePath = fileURLToPath(new URL('./card-assets', import.meta.url));
const outputDir = new URL('./output/', import.meta.url);
await mkdir(outputDir, { recursive: true });
const card = new YugiohCard({
skia,
resourcePath,
data: {
language: 'en',
attribute: 'light',
level: 8,
monsterTypeLine: 'Dragon / Normal',
cardText: 'A dragon drawn on Canvas.',
def: 2500,
},
});
const cards = [
{ name: 'Demo Dragon A', atk: 3000 },
{ name: 'Demo Dragon B', atk: 2600 },
];
try {
for (const [index, data] of cards.entries()) {
card.setData(data);
await card.ready;
const { data: png } = await card.leafer.export('png', { screenshot: true, blob: true });
await writeFile(new URL(`card-${index + 1}.png`, outputDir), png);
}
} finally {
card.leafer.destroy(true);
}You can reuse an instance for cards of the same type. Call setData(), read and await ready, then export and write each card before advancing to the next one. In Node.js, the example uses blob: true to get a PNG Buffer.
setData() merges the supplied fields into the current data. Provide every field that changes from one card to the next. For example, pass artwork: '' to clear the previous card's artwork. To change card type, instantiate the corresponding class.
See Export and cleanup with Leafer for the available options.