Skip to main content

Get Started with Node.js

You can use Spacha even from server side.

caution

If you use Spacha from Node.js, only SpachaImage(Canvas) is available. If yo want to use Spacha(HTML) then you should use from HTML or React

Quick Start

  1. Prepare

    Download installer from nodejs.org

  2. Prepare sample-spc-node directory.

    mkdir -p sample-spc-node
    cd sample-spc-node
  3. Generate package.json

    npm init --yes
  4. Install Spacha and necessary package

    npm i spacha canvas
  5. Prepare index.js

    index.js
    const Canvas = require("canvas");
    const Image = Canvas.Image;
    const { SpachaImage } = require("spacha");
    const fs = require("fs");

    (async () => {
    const canvas = new Canvas.Canvas(600, 300);
    const ctx = canvas.getContext("2d");
    const iconImg = new Image;
    iconImg.src = "https://github.com/bonychops.png";
    await new Promise((resolve) => { iconImg.onload = resolve });
    new SpachaImage(ctx, {
    user: {
    name: "Bony_Chops",
    img: iconImg
    },
    price: -31117,
    theme: "red"
    });

    saveImg(canvas);
    })();

    const saveImg = (canvas) => {
    const b64 = canvas.toDataURL().split(",")[1];
    const buf = Buffer.from(b64, 'base64');
    fs.writeFileSync("spc.png", buf);
    }
  6. Execute it to generate spc.png.

    node index

    Example banner

    This is the real total balance of Bony_Chops in 09/17/2022

Samples

See samples at here