-
Notifications
You must be signed in to change notification settings - Fork 3
/
Weeb.js
59 lines (51 loc) · 1.85 KB
/
Weeb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const { createCanvas, Image, loadImage } = require('canvas');
const GIFEncoder = require('gif-encoder-2');
const MyBufferAccumulator = require('../WorkerHelpers/BufferAccumulator');
const imageD = 256;
module.exports = {
/**
* gets the buffer from canvas
* @param images the image buffers to hold
* @param image the user's image
* @returns {Promise<*>}
*/
execute: async (images, image) => {
try {
// get their image.
const theirImage = await loadImage(image);
// use gifencoder
// create our new accumulator
const myAccumulator = new MyBufferAccumulator();
const encoder = new GIFEncoder(imageD, imageD, 'neuquant', false);
encoder.createReadStream().pipe(myAccumulator);
encoder.start();
// encoder.setQuality(30);
// encoder.setDelay(90);
// get our canvas and buffer
const canvas = createCanvas(imageD, imageD);
const ctx = canvas.getContext('2d');
// resize their image so we don't have to do it each time.
const ctxResize = canvas.getContext('2d');
ctxResize.drawImage(theirImage, 0, 0, imageD, imageD);
const buffer = canvas.toBuffer('image/jpeg', undefined);
theirImage.src = Buffer.from(buffer);
// loop over all images.
for (let i = 0; i < images.length; i += 1) {
ctx.globalAlpha = 1;
const background = new Image();
background.src = Buffer.from(images[i]);
ctx.drawImage(background, 0, 0);
// get buffer and resize our image to our length
ctx.globalAlpha = 0.6;
ctx.drawImage(theirImage, 0, 0);
encoder.addFrame(ctx);
}
encoder.finish();
return await myAccumulator.getBuffer();
} catch (error) {
console.error(error);
}
return undefined;
},
};
// worker doesn't work with canvas, send buffer and apply other methods if needed.