Example snippet showing how to dynamically generate images using node-canvas and return them. After adding this snippet to a project, call it from your browser via your project's URL + "/somevalue/" at the end. This snippet will use final part of the URL path to create an image and will return it as a .png file.
const Canvas = require('canvas');
const width = 960;
const height = 540;
/* Load the font */
Canvas.registerFont(require('@canvas-fonts/arial-bold'), {
family: 'Arial Bold',
});
let canvas = Canvas.createCanvas(width, height);
let ctx = canvas.getContext('2d');
ctx.fillStyle = "#FFFFFF";
ctx.fillRect(0, 0, width, height);
let name = context.path[context.path.length - 1] || 'No name';
ctx.font = `24px Arial Bold`;
ctx.fillStyle = '#000000';
ctx.textAlign = 'center';
ctx.fillText(`${name}`, 128, 128);
return {
statusCode: 200,
body: await canvas.toBuffer(),
headers: {
'Content-Type': 'image/png'
}
}