-
Notifications
You must be signed in to change notification settings - Fork 4
/
generateParrots.js
84 lines (65 loc) 路 2.34 KB
/
generateParrots.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"use strict"
const fs = require('fs')
const GIFEncoder = require('gifencoder')
const Canvas = require('canvas')
const Image = Canvas.Image
const countryColors = require('./data/countryColors.js').getAll()
const CONFIG = {
source: './framePngs/',
destination: './parrots/',
gifFrames: [
'0.png',
'1.png',
'2.png',
'3.png',
'4.png',
'5.png',
'6.png',
'7.png',
'8.png',
'9.png',
],
gifSize: {
w: 35,
h: 25
}
}
function renderParrot(colorObject, progress) {
let encoder = new GIFEncoder(CONFIG.gifSize.w, CONFIG.gifSize.h)
const {name, colors} = colorObject
encoder.createReadStream()
.pipe(fs.createWriteStream(`${CONFIG.destination}${name.toLowerCase().split(' ').join('-')}-parrot.gif`))
encoder.start()
encoder.setRepeat(0)
encoder.setDelay(35)
encoder.setQuality(10)
encoder.setTransparent('#FFFFFF');
for(var colorIndex = 0; colorIndex < colors.length; colorIndex++) {
CONFIG.gifFrames.map((frame, frameIndex) => {
let frameCanvas = new Canvas(CONFIG.gifSize.w, CONFIG.gifSize.h)
let frameCtx = frameCanvas.getContext('2d');
let buffer = new Canvas(CONFIG.gifSize.w, CONFIG.gifSize.h)
let bufferCtx = buffer.getContext('2d')
let img = new Canvas.Image
img.src = fs.readFileSync(`${CONFIG.source}${frame}`);
let imgOutline = new Image()
imgOutline.src = fs.readFileSync(`${CONFIG.source}${frame.replace('.png', '_o.png')}`);
bufferCtx.drawImage(img,0,0)
bufferCtx.globalCompositeOperation = "source-atop"
bufferCtx.globalAlpha = ((CONFIG.gifFrames.length - frameIndex) / CONFIG.gifFrames.length) - 0.01
bufferCtx.fillStyle = colors[colorIndex]
bufferCtx.fillRect(0,0, CONFIG.gifSize.w, CONFIG.gifSize.h)
bufferCtx.globalAlpha = 1.01 - ((CONFIG.gifFrames.length - frameIndex) / CONFIG.gifFrames.length)
bufferCtx.fillStyle = (colors[colorIndex+1] || colors[0])
bufferCtx.fillRect(0,0,CONFIG.gifSize.w, CONFIG.gifSize.h)
bufferCtx.drawImage(imgOutline,0,0)
frameCtx.drawImage(buffer, 0,0);
frameCtx.drawImage(imgOutline,0,0)
encoder.addFrame(frameCtx);
})
}
process.stdout.write("\r\x1b[K")
process.stdout.write(`${progress+1} of ${countryColors.length} parrots generated [${colorObject.name}]`);
}
countryColors.map(renderParrot)
console.log('\ndone!');