Skip to content

Render in the browser ​

Start with a Vite project using these commands. In an existing ESM frontend project, install yugioh-card and use your own HTML container.

bash
npm create vite@latest card-preview -- --template vanilla
cd card-preview
npm install
npm install yugioh-card

Prepare public/card-assets as described in Static assets. Keep the template's #app container, replace the entry file with this code, and run npm run dev.

js
import { YugiohCard } from 'yugioh-card';

const app = document.querySelector('#app');
const view = document.createElement('div');
const save = document.createElement('button');
const status = document.createElement('p');

save.type = 'button';
save.textContent = 'Download PNG';
save.disabled = true;
status.setAttribute('role', 'status');
app.replaceChildren(view, save, status);

const card = new YugiohCard({
  view,
  resourcePath: `${import.meta.env.BASE_URL}card-assets`,
  data: {
    language: 'en',
    name: 'Demo Dragon',
    attribute: 'light',
    level: 8,
    monsterTypeLine: 'Dragon / Normal',
    cardText: 'A dragon drawn on Canvas.',
    atk: 3000,
    def: 2500,
    renderScale: 0.4,
  },
});

try {
  await card.ready;
  save.disabled = false;
} catch (error) {
  status.textContent = error.message;
}

save.addEventListener('click', async () => {
  save.disabled = true;
  status.textContent = '';
  try {
    await card.ready;
    await card.leafer.export('card.png', { screenshot: true, pixelRatio: window.devicePixelRatio });
  } catch (error) {
    status.textContent = error.message;
  } finally {
    save.disabled = false;
  }
});

To add artwork, put an image in the public directory and pass its URL as data.artwork. Call card.setData({ name: 'Another Dragon' }) to update the name. After updates, read card.ready again and wait for rendering to finish before exporting.

renderScale controls the rendered dimensions. To enlarge or shrink the preview on screen, scale its outer container. Call card.leafer.destroy(true) when unmounting the component. See the API reference for card types and fields.

See Export and cleanup with Leafer for the available options.