Merge pull request #11 from SimonLeclere/patch-1

Improving blink generation, now allows as many images as the user wants. ^^ Thanks to @SimonLeclere
main
Killian' Dal-Cin 4 years ago committed by GitHub
commit f91f9cff78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,35 +1,29 @@
const Canvas = require(`canvas`);
const GIFEncoder = require(`gifencoder`);
module.exports = class Blink {
/**
* Blink
* @param {image} image1
* @param {image} image2
* @param {number} timeout
*/
async getImage(image1, image2, timeout = 1000) {
if (!image1) throw new Error(`You must provide an image as a first argument.`);
if (!image2) throw new Error(`You must provide an image as a second argument.`);
if (isNaN(timeout)) throw new Error(`The timeout argument must be a number.`);
const img = await Canvas.loadImage(image1);
const base = await Canvas.loadImage(image2);
const GIF = new GIFEncoder(480, 480);
async getImage( ...images) {
console.log(images)
if (!images || images.length < 2) throw new Error(`You must provide an image as a first argument.`);
const GIF = new GIFEncoder(480, 480)
GIF.start();
GIF.setRepeat(0);
GIF.setDelay(timeout);
GIF.setDelay(1000);
GIF.setTransparent();
const canvas = Canvas.createCanvas(480, 480);
const ctx = canvas.getContext(`2d`);
ctx.clearRect(0, 0, 480, 480);
ctx.drawImage(img, 0, 0, 480, 480);
GIF.addFrame(ctx);
const ctx2 = canvas.getContext(`2d`);
ctx2.clearRect(0, 0, 480, 480);
ctx2.drawImage(base, 0, 0, 480, 480);
GIF.addFrame(ctx2);
for (const image of images) {
const base = await Canvas.loadImage(image);
const ctx2 = canvas.getContext(`2d`);
ctx2.clearRect(0, 0, 480, 480);
ctx2.drawImage(base, 0, 0, 480, 480);
GIF.addFrame(ctx2);
}
GIF.finish();
return GIF.out.getData();
}
};
};

Loading…
Cancel
Save