Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

Drawing rectangles with Jimp

node v6.17.1
endpointsharetweet
const Jimp = require('jimp'); const canvas = new Jimp(512, 256, 0xFFFFFFFF); // Promise-based wrapper for Jimp#getBuffer function encode(image) { return new Promise((fulfill, reject) => { canvas.getBuffer(Jimp.MIME_PNG, (err, img) => err ? reject(err) : fulfill(img)); }); } function makeIteratorThatFillsWithColor(color) { return function (x, y, offset) { this.bitmap.data.writeUInt32BE(color, offset, true); } }; // fill canvas.scan(32, 32, 256, 128, makeIteratorThatFillsWithColor(0x00000040)); // border const fillCrimson = makeIteratorThatFillsWithColor(0xED143DFF); canvas.scan(236 , 100 , 240, 1, fillCrimson); canvas.scan(236 , 100 + 110, 240, 1, fillCrimson); canvas.scan(236 , 100 , 1 , 110, fillCrimson); canvas.scan(236 + 240, 100 , 1 , 110, fillCrimson); // displaying await encode(canvas);
function scanCircle(image, x, y, radius, iterator) { return image.scan(x - radius, y - radius, radius*2, radius*2, function (pixX, pixY, idx) { if (Math.pow(pixX - x, 2) + Math.pow(pixY - y, 2) < radius*radius) { iterator.call(this, pixX, pixY, idx); } }); } scanCircle(canvas, 92, 170, 43, makeIteratorThatFillsWithColor(0x000000FF)); scanCircle(canvas, 92, 170, 42, makeIteratorThatFillsWithColor(0xFFCC00FF)); await encode(canvas);
Loading…

no comments

    sign in to comment