UNPKG

monafy

Version:

Make configurable canvas easily with Monafy

26 lines (20 loc) 787 B
const { createCanvas, loadImage } = require("canvas"); module.exports = async (template_image, amount) => { let image; try { image = await loadImage(template_image); } catch { throw new Error("The image given in the argument of the invert method is not valid or you are not connected to the internet."); } const canvas = await createCanvas(image.width, image.height); const ctx = canvas.getContext("2d"); ctx.drawImage(image, 0, 0); const image_data = ctx.getImageData(0, 0, canvas.width, canvas.height); for (let i = 0; i < image_data.data.length; i += 4) { image_data.data[i] -= amount; image_data.data[i + 1] -= amount; image_data.data[i + 2] -= amount; } ctx.putImageData(image_data, 0, 0); return canvas; };