forked from RossLitzenberger/KappaGen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
animations.js
336 lines (320 loc) · 11.2 KB
/
animations.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Returns a random position within a spawnzone.
Takes one param:
spawnzone - the spawnzone as a string, comma seperated integers, like "x1,y1,x2,y2"
*/
function getSpawn(ctx, spawnzone) {
var m = spawnzone.match(/\d+/g);
var x1,x2,y1,y2;
var w = ctx.canvas.width;
var h = ctx.canvas.height;
if (m && m.length == 4) {
var split = spawnzone.split(",");
x1 = parseFloat(m[0])/100.0;
y1 = parseFloat(m[1])/100.0;
x2 = parseFloat(m[2])/100.0;
y2 = parseFloat(m[3])/100.0;
}
return [Math.round(w*(x1+Math.random()*(x2-x1))), Math.round(h*(y1+Math.random()*(y2-y1)))];
}
function zigZag(x){return 1-Math.abs(2*x-1)}
function clamp(val, min, max) {
return Math.min(Math.max(val, min), max);
};
function basicAnimationParams(ctx, settings, type) {
var pos = getSpawn(ctx, settings.spawnzone[type]);
return {"type": type, x: pos[0], y: pos[1], duration: settings.duration}
}
/*
these functions return an emote object.
they take one param:
settings - the whole settings object
*/
initializers = {
"zoom": function(ctx, settings){
return basicAnimationParams(ctx, settings, "zoom");
},
"blur": function(ctx, settings){
return basicAnimationParams(ctx, settings, "blur");
},
"bubble": function(ctx, settings){
var ap = basicAnimationParams(ctx, settings, "bubble");
ap.vy = -0.5*ctx.canvas.height*(1+Math.random());
ap.amp = settings.size*(1+Math.random())*0.5;
ap.freq = Math.PI*(1+Math.random());
ap.phase = Math.random()*Math.PI*2;
return ap;
},
"bounce": function(ctx, settings){
var ap = basicAnimationParams(ctx, settings, "bounce");
ap.vy = Math.random()*settings.size-2*ap.y;
ap.goleft = Math.random()>0.5;
ap.canbounce = true;
return ap;
},
"random": function(ctx, settings) {
var animations = ["bubble","blur","zoom","bounce"];
return initializers[animations[Math.floor(Math.random()*animations.length)]](ctx, settings);
}
}
/*
these functions draw an animated emote.
they take 4 params:
settings - the whole settings object
frameTime - time since the last frame, as a fraction of the total duration
age - time since animation start, as a fraction of the total duration
emote - the emote object that was used to create the emote
*/
animations = {
"bubble": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var x = ap.x + Math.sin(ap.phase + age * ap.freq) * ap.amp;
var y = ap.y + ap.vy * age;
var h = settings.size;
if(age < 0.10) {
h *= age*12;
} else if(age < 0.125) {
h *= 1.2-(age-0.1)*8;
}
var w = emote.w * h/emote.h;
if(age > 0.75) ctx.globalAlpha = clamp(4-4*age,0,1);
ctx.drawImage(emote.img, x-w/2, y-h/2, w, h);
ctx.globalAlpha = 1;
},
"blur": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var w = emote.w * settings.size/emote.h;
if(age < 0.25) {
ctx.globalAlpha = clamp(age*4,0,1);
} else if(age > 0.75) {
ctx.globalAlpha = clamp(4-age*4,0,1);
}
ctx.drawImage(emote.img, ap.x-w/2, ap.y-settings.size/2, w, settings.size);
ctx.globalAlpha = 1;
},
"zoom": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var zoom = 1;
if(age < 0.25) {
zoom = age*4;
} else if(age > 0.75) {
zoom = 4-age*4;
}
var w = emote.w * settings.size/emote.h*zoom;
ctx.drawImage(emote.img, ap.x-w/2, ap.y-settings.size/2*zoom, w, settings.size*zoom);
},
"bounce": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var sh = ctx.canvas.height;
var gravity = 20*sh;
var w = emote.w * settings.size/emote.h;
var dx1 = ap.x+w;
var dx2 = ctx.canvas.width-ap.x+w;
var vx = (ap.goleft?-dx1:dx2);
ap.vy += gravity*frameTime;
ap.y += ap.vy*frameTime;
var x = ap.x+vx*age;
if((ap.y+settings.size/2) >= sh && ap.canbounce) {
if(Math.abs(ap.vy) < (gravity*frameTime+100)) ap.canbounce = false;
else {
ap.vy = -Math.abs(ap.vy)*0.75;
ap.y = sh-settings.size/2;
}
}
ctx.drawImage(emote.img, x-w/2, ap.y-settings.size/2, w, settings.size);
},
"fireworkrocket": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var t = age;
var x = t*ap.targetx+(1-t)*ap.x;
var y = t*ap.targety+(1-t)*ap.y;
var w = emote.w * settings.size/emote.h;
ctx.drawImage(emote.img, x-w/2, y-settings.size/2, w, settings.size);
},
"fireworkspark": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var t = age;
var x = ap.x+age*ap.vx;
var y = ap.y+age*ap.vy;
var agesq = age * age;
var intensity = 1-agesq*agesq;
var w = emote.w * settings.size/emote.h*intensity;
ctx.globalAlpha = intensity;
ctx.drawImage(emote.img, x-w/2, y-settings.size*intensity/2, w, settings.size * intensity);
ctx.globalAlpha = 1;
},
"fountain": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var sh = ctx.canvas.height;
ap.vy += ap.gravity*frameTime;
ap.y += ap.vy*frameTime;
ap.x += ap.vx*frameTime;
var w = emote.w * settings.size/emote.h;
ctx.drawImage(emote.img, ap.x-w/2, ap.y-settings.size/2, w, settings.size);
},
"explosion": function(ctx, settings, frameTime, age, emote) {
var ap = emote.animation;
var sh = ctx.canvas.height;
var y = ap.y + ap.vy*age;
var x = ap.x + ap.vx*age;
var w = emote.w * settings.size/emote.h;
if(age > 0.75) ctx.globalAlpha = clamp(4-4*age,0,1);
ctx.drawImage(emote.img, x-w/2, y-settings.size/2, w, settings.size);
ctx.globalAlpha = 1;
}
}
emotesplosions = {
random: function(ctx, settings, allowedEmotes, animatedemotes) {
var est = ["explosion","fountain","firework","bubbles"];
emotesplosions[est[Math.floor(Math.random()*est.length)]](ctx, settings, allowedEmotes, animatedemotes);
},
explosion: function (ctx, settings, allowedEmotes, animatedemotes) {
var pos = getSpawn(ctx, settings.emotesplosionzone["firework"]);
var fdx = Math.max(pos[0], ctx.canvas.width-pos[0]);
var fdy = Math.max(pos[1], ctx.canvas.height-pos[1]);
var initialspeed = Math.max(fdx, fdy);
for(var i=1;i<settings.emotesplosion;++i) {
emote = allowedEmotes[Math.floor(Math.random()*allowedEmotes.length)];
var velocity = initialspeed * (2+Math.random())/2; // randomize initial velocity
// choose some initial direction
var vx = Math.random()-0.5;
var vy = Math.random()-0.5;
// normalize it and bring it to the velocity
var nf = velocity/Math.sqrt(vx*vx+vy*vy);
vx *= nf;
vy *= nf;
var ap = {"type": "explosion", x: pos[0], y: pos[1], vx: vx, vy: vy, duration: settings.duration, id: i}
var img = new ImageEx(emote.url, emote.type == "gif");
img.onload = function(that) {
return function() {
animatedemotes.push({
animation: that,
img: this,
type: emote.type,
w: this.width, h: this.height,
start: performance.now()+that.id*settings.emotesplosionduration*1000/settings.emotesplosion
});
}
}(ap);
}
},
firework: function (ctx, settings, allowedEmotes, animatedemotes) {
var sparks = [];
var emote;
var pos = getSpawn(ctx, settings.emotesplosionzone["firework"]);
var fdx = Math.max(pos[0], ctx.canvas.width-pos[0]);
var fdy = Math.max(pos[1], ctx.canvas.height-pos[1]);
var initialspeed = Math.max(fdx, fdy);
for(var i=1;i<settings.emotesplosion;++i) {
emote = allowedEmotes[Math.floor(Math.random()*allowedEmotes.length)];
var velocity = initialspeed * Math.random(); // randomize initial velocity
// choose some direction
var vx = Math.random()-0.5;
var vy = Math.random()-0.5;
// normalize it and bring it to the velocity
var nf = velocity/Math.sqrt(vx*vx+vy*vy);
vx *= nf;
vy *= nf;
var ap = {"type": "fireworkspark", x: pos[0], y: pos[1], vx: vx, vy: vy, duration: 0.8*settings.emotesplosionduration, id: i}
var img = new ImageEx(emote.url, emote.type == "gif");
img.onload = function(that) {
return function() {
sparks.push({url: this.src, animation: that, img: this, w: this.width, h: this.height, type: emote.type});
}
}(ap);
}
emote = allowedEmotes[Math.floor(Math.random()*allowedEmotes.length)];
var projap = {
type: "fireworkrocket",
x: ctx.canvas.width/2,
y: ctx.canvas.height+settings.size,
targetx: pos[0],
targety: pos[1],
duration: 0.2*settings.emotesplosionduration,
sparks: sparks,
id: -1
};
var img = new ImageEx(emote.url, emote.type == "gif");
img.onload = function() {
animatedemotes.push({
animation: projap,
start: performance.now(),
img: this,
type: emote.type,
w: this.width, h: this.height,
oncomplete: function() {
for(var i=0;i<this.animation.sparks.length;++i) {
this.animation.sparks[i].start = performance.now();
animatedemotes.push(this.animation.sparks[i]);
}
}
});
}
},
fountain: function (ctx, settings, allowedEmotes, animatedemotes) {
var pos = getSpawn(ctx, settings.emotesplosionzone["fountain"]);
var y0 = pos[1]; // start y value
var y1 = 0; // peak y value (yes, it is zero, since its the highest an emote can apex at)
var y2 = ctx.canvas.height+settings.size; // final y value (below the lower edge of the screen)
var v0 = -2*(y0+Math.sqrt(y2*y0)); // initial maximum velocity
var gravity = 2*(y2-v0-y0);//2*y2 - 2*v0;
for(var i=0;i<settings.emotesplosion;++i) {
// choose some initial direction (4 times as much vertical component)
var vx = Math.random()-0.5;
var vy = 1+Math.random();
// normalize it and bring it to the velocity
var nf = 1/Math.sqrt(vx*vx+vy*vy);
vy *= v0 * 0.5 * (1+Math.random()) * nf;
vx *= ctx.canvas.width * nf * (1+Math.random());
var ap = {
type: "fountain",
x: pos[0], y: pos[1],
vx: vx, vy: vy,
gravity: gravity,
duration: settings.duration,
id: i
}
var emote = allowedEmotes[Math.floor(Math.random()*allowedEmotes.length)];
var img = new ImageEx(emote.url, emote.type == "gif");
img.onload = function(that) {
return function() {
animatedemotes.push({
animation: that,
start: performance.now()+that.id*settings.emotesplosionduration*1000/settings.emotesplosion,
img: this,
type: emote.type,
w: this.width, h: this.height
});
}
}(ap);
}
},
bubbles: function (ctx, settings, allowedEmotes, animatedemotes) {
for(var i=0;i<settings.emotesplosion;++i) {
var pos = getSpawn(ctx, settings.emotesplosionzone["bubbles"]);
var ap = {
type: "bubble",
x: pos[0], y: pos[1],
duration: settings.duration,
vy: -0.5*ctx.canvas.height*(1+Math.random()),
amp: settings.size*(1+Math.random())*0.5,
freq: Math.PI*(1+Math.random()),
phase: Math.random()*Math.PI*2,
id: i
}
var emote = allowedEmotes[Math.floor(Math.random()*allowedEmotes.length)];
var img = new ImageEx(emote.url, emote.type == "gif");
img.onload = function(that) {
return function() {
animatedemotes.push({
animation: that,
start: performance.now()+that.id*settings.emotesplosionduration*1000/settings.emotesplosion,
img: this,
type: emote.type,
w: this.width, h: this.height
});
}
}(ap);
}
}
}