-
Notifications
You must be signed in to change notification settings - Fork 27
/
ParrotConstructor.js
125 lines (104 loc) · 4.36 KB
/
ParrotConstructor.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const GIFEncoder = require('gifencoder');
const ParrotFramesReader = require("./ParrotFramesReader");
const ParrotFrameHandler = require("./ParrotFrameHandler");
const ParrotConfig = require("./ParrotConfig");
const ImageFactory = require("./ImageFactory");
const config = require("./config");
function ParrotConstructor() {
this.imageFactory = new ImageFactory();
this.setBaseParrot("parrot");
}
ParrotConstructor.prototype.start = function(writeStream, configuration) {
this.encoder = new GIFEncoder(this.parrotConfig.getWidth(), this.parrotConfig.getHeight());
this.encoder.createReadStream().pipe(writeStream);
this.encoder.start();
this.encoder.setTransparent("#000000");
this.encoder.setRepeat(0);
this.encoder.setDelay(configuration.delay || 40);
this.numberOfLoops = Math.ceil((configuration.colors ? configuration.colors.length : 1) / this.parrotConfig.getNumberOfFrames());
this.colors = configuration.colors;
}
ParrotConstructor.prototype.setBaseParrot = function(parrotType) {
this.parrotConfig = new ParrotConfig(parrotType);
}
ParrotConstructor.prototype.getFramesHandlers = function() {
if(!this.parrotFrameHandlers) {
this.initializeFramesHandlers();
}
return this.parrotFrameHandlers;
}
ParrotConstructor.prototype.initializeFramesHandlers = function() {
const loadWhiteParrot = !!this.colors;
const framesReader = new ParrotFramesReader(this.parrotConfig, loadWhiteParrot);
const mapImageToFrameHandler = (image) => {
var frameHandler = new ParrotFrameHandler(this.parrotConfig);
frameHandler.addImage(image);
return frameHandler;
};
const allImages = framesReader.getFrames().map((file) => {
console.log(file);
return this.imageFactory.fromFileSync(file);
});
let allFrameHandlers = [];
for (let i=0; i<this.numberOfLoops; i++) {
allFrameHandlers = allFrameHandlers.concat(allImages.map(mapImageToFrameHandler));
}
if (this.colors && this.colors.length > 0) {
allFrameHandlers.forEach((frameHandler, i) => {
frameHandler.applyColor(this.colors[i % this.colors.length]);
})
}
this.parrotFrameHandlers = allFrameHandlers;
}
ParrotConstructor.prototype.addOverlayImage = function(overlay) {
return this.imageFactory.get(overlay).then((image) => {
this.getFramesHandlers().map(handler => {
handler.addImage(image);
});
});
}
ParrotConstructor.prototype.addFollowingOverlayImage = function(overlay, offsetX, offsetY, width, height, flipX, flipY) {
let followingFrames = this.parrotConfig.getFollowingFrames();
if(this.parrotConfig.shouldFlipX()) {
flipX = !flipX;
}
if(this.parrotConfig.shouldFlipY()) {
flipY = !flipY;
}
return this.imageFactory.get(overlay).then((image) => {
let imageHeight = parseInt(height || image.height);
let imageWidth = parseInt(width || image.width);
let frameHandler = function(handler, frame) {
let shouldFlipX = frame.flipX ? !flipX : flipX;
let shouldFlipY = frame.flipY ? !flipY : flipY;
handler.addResizedImage(image,
flipPositionIfActivated(frame.x, imageWidth, shouldFlipY) + (offsetX || 0),
flipPositionIfActivated(frame.y, imageHeight, shouldFlipX) + (offsetY || 0),
flipSizeIfActivated(imageWidth, shouldFlipY),
flipSizeIfActivated(imageHeight, shouldFlipX));
}
this.getFramesHandlers().map((handler, index) => {
let currentFrame = followingFrames[index];
if (currentFrame.multiple) {
currentFrame.multiple.forEach(frame => {
frameHandler(handler, frame);
})
} else {
frameHandler(handler, currentFrame);
}
});
});
}
function flipPositionIfActivated(currentPosition, size, flip) {
return flip ? (currentPosition + size) : currentPosition;
}
function flipSizeIfActivated(currentSize, flip) {
return flip ? currentSize * -1 : currentSize;
}
ParrotConstructor.prototype.finish = function() {
this.getFramesHandlers().forEach(handler => {
this.encoder.addFrame(handler.getFrame());
});
this.encoder.finish();
}
module.exports = ParrotConstructor;