Util class for simplify the use of canvas with nodejs.
Import the module :
CommonJS syntax :
const { CanvasUtil } = require("canvas-util.js");
EcmaScript syntax :
import { CanvasUtil } from "canvas-util.js";
Create a new canvas with the width and height as a parameters (default 930x280 pixels) :
const canvas = new CanvasUtil(1000, 800);
Set an image or a color as the background of the canvas.
Params :
- type - The type of the background ("IMAGE" or "COLOR" : String).
- value - The value of the background (String for COLOR and Buffer or String for Image).
canvas.setBackground("COLOR", "blue");
Set a font and size of text.
Params :
- name - The name of the font (string).
- size - The size of the font (number).
canvas.setFont("Arial", 12);
Set a font and size of text.
Params :
- transparancy - The value of the transparancy (number).
canvas.setTransparancy(0.4);
Write text to canvas.
Params :
- text - The text to write (string).
- color - The color of the text (string).
- x - The x axis offset (number).
- y - The y axis offset (number).
canvas.addText("Hello World", "#ffffff", 200, 200);
Add an image to the canvas.
Params :
- img - The image (Buffer or link).
- x - The x axis offset (number).
- y - The y axis offset (number).
- sx - The x axis size (number).
- sy - The y axis size (number).
canvas.addImage("https://website.com/image.png", 200, 200, 200, 150);
Add an circular image to the canvas. Note : For a good result, sx and sy should be same value
Params :
- img - The image (Buffer or link).
- x - The x axis offset (number).
- y - The y axis offset (number).
- sx - The x axis size (number).
- sy - The y axis size (number).
canvas.addImage("https://website.com/image.png", 200, 200, 150, 150);
Build the canvas and return a buffer of the image.
canvas.build();