-
Notifications
You must be signed in to change notification settings - Fork 39
/
ChatRoomConfetti.user.js
417 lines (389 loc) · 18.1 KB
/
ChatRoomConfetti.user.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// ==UserScript==
// @name Confetti
// @namespace https://github.com/SO-Close-Vote-Reviewers/UserScripts
// @version 0.1
// @description Shows confetti when a certain type of message shows in chat
// @author You
// @match http://chat.stackoverflow.com/rooms/41570/*
// @grant none
// ==/UserScript==
/*
* Note: this script is still in a conceptual phase.
* For the moment, you need to click a message (any message)
* and confetti will appear.
*
* Things to do next:
*
* 1 - make the confetti show when a message is added (and only allowed types through regex)
* 2 - simulate the confettie longer before showing. See (http://codepen.io/GhostRider/pen/Lzfrd) for the full effect
*
*/
(function() {
$('body').append("<div id='confetti'></div>");
//$('body').append('<styles>#confetti{ background: #000; height: 100%; left: 0; position: fixed; top: 0; width: 100%; z-index: 10;}</styles>');
//$('body div#confetti').css('background', '#000');
$('body div#confetti').css('height', '100%');
$('body div#confetti').css('left', '0');
$('body div#confetti').css('position', 'fixed');
$('body div#confetti').css('top', '0');
$('body div#confetti').css('width', '100%');
$('body div#confetti').css('z-index', '10');
$('body div#confetti').css('display', 'none');
$(document).on('click', 'div#chat div.content', function() {
//$(this).attr('style', 'background-color: red');
//alert('click!');
//$('body div#confetti').css('display', 'block');
$('body div#confetti canvas').remove();
//===============================
//http://codepen.io/GhostRider/pen/Lzfrd
/* Confetti by Patrik Svensson (http://metervara.net) */
var frameRate = 30;
var dt = 1.0 / frameRate;
var DEG_TO_RAD = Math.PI / 180;
var RAD_TO_DEG = 180 / Math.PI;
var colors = [
["#df0049", "#660671"],
["#00e857", "#005291"],
["#2bebbc", "#05798a"],
["#ffd200", "#b06c00"]
];
function Vector2(_x, _y) {
this.x = _x, this.y = _y;
this.Length = function() {
return Math.sqrt(this.SqrLength());
}
this.SqrLength = function() {
return this.x * this.x + this.y * this.y;
}
this.Equals = function(_vec0, _vec1) {
return _vec0.x == _vec1.x && _vec0.y == _vec1.y;
}
this.Add = function(_vec) {
this.x += _vec.x;
this.y += _vec.y;
}
this.Sub = function(_vec) {
this.x -= _vec.x;
this.y -= _vec.y;
}
this.Div = function(_f) {
this.x /= _f;
this.y /= _f;
}
this.Mul = function(_f) {
this.x *= _f;
this.y *= _f;
}
this.Normalize = function() {
var sqrLen = this.SqrLength();
if (sqrLen != 0) {
var factor = 1.0 / Math.sqrt(sqrLen);
this.x *= factor;
this.y *= factor;
}
}
this.Normalized = function() {
var sqrLen = this.SqrLength();
if (sqrLen != 0) {
var factor = 1.0 / Math.sqrt(sqrLen);
return new Vector2(this.x * factor, this.y * factor);
}
return new Vector2(0, 0);
}
}
Vector2.Lerp = function(_vec0, _vec1, _t) {
return new Vector2((_vec1.x - _vec0.x) * _t + _vec0.x, (_vec1.y - _vec0.y) * _t + _vec0.y);
}
Vector2.Distance = function(_vec0, _vec1) {
return Math.sqrt(Vector2.SqrDistance(_vec0, _vec1));
}
Vector2.SqrDistance = function(_vec0, _vec1) {
var x = _vec0.x - _vec1.x;
var y = _vec0.y - _vec1.y;
return (x * x + y * y + z * z);
}
Vector2.Scale = function(_vec0, _vec1) {
return new Vector2(_vec0.x * _vec1.x, _vec0.y * _vec1.y);
}
Vector2.Min = function(_vec0, _vec1) {
return new Vector2(Math.min(_vec0.x, _vec1.x), Math.min(_vec0.y, _vec1.y));
}
Vector2.Max = function(_vec0, _vec1) {
return new Vector2(Math.max(_vec0.x, _vec1.x), Math.max(_vec0.y, _vec1.y));
}
Vector2.ClampMagnitude = function(_vec0, _len) {
var vecNorm = _vec0.Normalized;
return new Vector2(vecNorm.x * _len, vecNorm.y * _len);
}
Vector2.Sub = function(_vec0, _vec1) {
return new Vector2(_vec0.x - _vec1.x, _vec0.y - _vec1.y, _vec0.z - _vec1.z);
}
function EulerMass(_x, _y, _mass, _drag) {
this.position = new Vector2(_x, _y);
this.mass = _mass;
this.drag = _drag;
this.force = new Vector2(0, 0);
this.velocity = new Vector2(0, 0);
this.AddForce = function(_f) {
this.force.Add(_f);
}
this.Integrate = function(_dt) {
var acc = this.CurrentForce(this.position);
acc.Div(this.mass);
var posDelta = new Vector2(this.velocity.x, this.velocity.y);
posDelta.Mul(_dt);
this.position.Add(posDelta);
acc.Mul(_dt);
this.velocity.Add(acc);
this.force = new Vector2(0, 0);
}
this.CurrentForce = function(_pos, _vel) {
var totalForce = new Vector2(this.force.x, this.force.y);
var speed = this.velocity.Length();
var dragVel = new Vector2(this.velocity.x, this.velocity.y);
dragVel.Mul(this.drag * this.mass * speed);
totalForce.Sub(dragVel);
return totalForce;
}
}
function ConfettiPaper(_x, _y) {
this.pos = new Vector2(_x, _y);
this.rotationSpeed = Math.random() * 600 + 800;
this.angle = DEG_TO_RAD * Math.random() * 360;
this.rotation = DEG_TO_RAD * Math.random() * 360;
this.cosA = 1.0;
this.size = 5.0;
this.oscillationSpeed = Math.random() * 1.5 + 0.5;
this.xSpeed = 40.0;
this.ySpeed = Math.random() * 60 + 50.0;
this.corners = new Array();
this.time = Math.random();
var ci = Math.round(Math.random() * (colors.length - 1));
this.frontColor = colors[ci][0];
this.backColor = colors[ci][1];
for (var i = 0; i < 4; i++) {
var dx = Math.cos(this.angle + DEG_TO_RAD * (i * 90 + 45));
var dy = Math.sin(this.angle + DEG_TO_RAD * (i * 90 + 45));
this.corners[i] = new Vector2(dx, dy);
}
this.Update = function(_dt) {
this.time += _dt;
this.rotation += this.rotationSpeed * _dt;
this.cosA = Math.cos(DEG_TO_RAD * this.rotation);
this.pos.x += Math.cos(this.time * this.oscillationSpeed) * this.xSpeed * _dt
this.pos.y += this.ySpeed * _dt;
if (this.pos.y > ConfettiPaper.bounds.y) {
this.pos.x = Math.random() * ConfettiPaper.bounds.x;
this.pos.y = 0;
}
}
this.Draw = function(_g) {
if (this.cosA > 0) {
_g.fillStyle = this.frontColor;
} else {
_g.fillStyle = this.backColor;
}
_g.beginPath();
_g.moveTo(this.pos.x + this.corners[0].x * this.size, this.pos.y + this.corners[0].y * this.size * this.cosA);
for (var i = 1; i < 4; i++) {
_g.lineTo(this.pos.x + this.corners[i].x * this.size, this.pos.y + this.corners[i].y * this.size * this.cosA);
}
_g.closePath();
_g.fill();
}
}
ConfettiPaper.bounds = new Vector2(0, 0);
function ConfettiRibbon(_x, _y, _count, _dist, _thickness, _angle, _mass, _drag) {
this.particleDist = _dist;
this.particleCount = _count;
this.particleMass = _mass;
this.particleDrag = _drag;
this.particles = new Array();
var ci = Math.round(Math.random() * (colors.length - 1));
this.frontColor = colors[ci][0];
this.backColor = colors[ci][1];
this.xOff = Math.cos(DEG_TO_RAD * _angle) * _thickness;
this.yOff = Math.sin(DEG_TO_RAD * _angle) * _thickness;
this.position = new Vector2(_x, _y);
this.prevPosition = new Vector2(_x, _y);
this.velocityInherit = Math.random() * 2 + 4;
this.time = Math.random() * 100;
this.oscillationSpeed = Math.random() * 2 + 2;
this.oscillationDistance = Math.random() * 40 + 40;
this.ySpeed = Math.random() * 40 + 80;
for (var i = 0; i < this.particleCount; i++) {
this.particles[i] = new EulerMass(_x, _y - i * this.particleDist, this.particleMass, this.particleDrag);
}
this.Update = function(_dt) {
var i = 0;
this.time += _dt * this.oscillationSpeed;
this.position.y += this.ySpeed * _dt;
this.position.x += Math.cos(this.time) * this.oscillationDistance * _dt;
this.particles[0].position = this.position;
var dX = this.prevPosition.x - this.position.x;
var dY = this.prevPosition.y - this.position.y;
var delta = Math.sqrt(dX * dX + dY * dY);
this.prevPosition = new Vector2(this.position.x, this.position.y);
for (i = 1; i < this.particleCount; i++) {
var dirP = Vector2.Sub(this.particles[i - 1].position, this.particles[i].position);
dirP.Normalize();
dirP.Mul((delta / _dt) * this.velocityInherit);
this.particles[i].AddForce(dirP);
}
for (i = 1; i < this.particleCount; i++) {
this.particles[i].Integrate(_dt);
}
for (i = 1; i < this.particleCount; i++) {
var rp2 = new Vector2(this.particles[i].position.x, this.particles[i].position.y);
rp2.Sub(this.particles[i - 1].position);
rp2.Normalize();
rp2.Mul(this.particleDist);
rp2.Add(this.particles[i - 1].position);
this.particles[i].position = rp2;
}
if (this.position.y > ConfettiRibbon.bounds.y + this.particleDist * this.particleCount) {
this.Reset();
}
}
this.Reset = function() {
this.position.y = -Math.random() * ConfettiRibbon.bounds.y;
this.position.x = Math.random() * ConfettiRibbon.bounds.x;
this.prevPosition = new Vector2(this.position.x, this.position.y);
this.velocityInherit = Math.random() * 2 + 4;
this.time = Math.random() * 100;
this.oscillationSpeed = Math.random() * 2.0 + 1.5;
this.oscillationDistance = Math.random() * 40 + 40;
this.ySpeed = Math.random() * 40 + 80;
var ci = Math.round(Math.random() * (colors.length - 1));
this.frontColor = colors[ci][0];
this.backColor = colors[ci][1];
this.particles = new Array();
for (var i = 0; i < this.particleCount; i++) {
this.particles[i] = new EulerMass(this.position.x, this.position.y - i * this.particleDist, this.particleMass, this.particleDrag);
}
}
this.Draw = function(_g) {
for (var i = 0; i < this.particleCount - 1; i++) {
var p0 = new Vector2(this.particles[i].position.x + this.xOff, this.particles[i].position.y + this.yOff);
var p1 = new Vector2(this.particles[i + 1].position.x + this.xOff, this.particles[i + 1].position.y + this.yOff);
if (this.Side(this.particles[i].position.x, this.particles[i].position.y, this.particles[i + 1].position.x, this.particles[i + 1].position.y, p1.x, p1.y) < 0) {
_g.fillStyle = this.frontColor;
_g.strokeStyle = this.frontColor;
} else {
_g.fillStyle = this.backColor;
_g.strokeStyle = this.backColor;
}
if (i == 0) {
_g.beginPath();
_g.moveTo(this.particles[i].position.x, this.particles[i].position.y);
_g.lineTo(this.particles[i + 1].position.x, this.particles[i + 1].position.y);
_g.lineTo((this.particles[i + 1].position.x + p1.x) * 0.5, (this.particles[i + 1].position.y + p1.y) * 0.5);
_g.closePath();
_g.stroke();
_g.fill();
_g.beginPath();
_g.moveTo(p1.x, p1.y);
_g.lineTo(p0.x, p0.y);
_g.lineTo((this.particles[i + 1].position.x + p1.x) * 0.5, (this.particles[i + 1].position.y + p1.y) * 0.5);
_g.closePath();
_g.stroke();
_g.fill();
} else if (i == this.particleCount - 2) {
_g.beginPath();
_g.moveTo(this.particles[i].position.x, this.particles[i].position.y);
_g.lineTo(this.particles[i + 1].position.x, this.particles[i + 1].position.y);
_g.lineTo((this.particles[i].position.x + p0.x) * 0.5, (this.particles[i].position.y + p0.y) * 0.5);
_g.closePath();
_g.stroke();
_g.fill();
_g.beginPath();
_g.moveTo(p1.x, p1.y);
_g.lineTo(p0.x, p0.y);
_g.lineTo((this.particles[i].position.x + p0.x) * 0.5, (this.particles[i].position.y + p0.y) * 0.5);
_g.closePath();
_g.stroke();
_g.fill();
} else {
_g.beginPath();
_g.moveTo(this.particles[i].position.x, this.particles[i].position.y);
_g.lineTo(this.particles[i + 1].position.x, this.particles[i + 1].position.y);
_g.lineTo(p1.x, p1.y);
_g.lineTo(p0.x, p0.y);
_g.closePath();
_g.stroke();
_g.fill();
}
}
}
this.Side = function(x1, y1, x2, y2, x3, y3) {
return ((x1 - x2) * (y3 - y2) - (y1 - y2) * (x3 - x2));
}
}
ConfettiRibbon.bounds = new Vector2(0, 0);
confetti = {};
confetti.Context = function(parent) {
var i = 0;
var canvasParent = document.getElementById(parent);
var canvas = document.createElement('canvas');
canvas.width = canvasParent.offsetWidth;
canvas.height = canvasParent.offsetHeight;
canvasParent.appendChild(canvas);
var context = canvas.getContext('2d');
var interval = null;
var confettiRibbonCount = 7;
var rpCount = 30;
var rpDist = 8.0;
var rpThick = 8.0;
var confettiRibbons = new Array();
ConfettiRibbon.bounds = new Vector2(canvas.width, canvas.height);
for (i = 0; i < confettiRibbonCount; i++) {
confettiRibbons[i] = new ConfettiRibbon(Math.random() * canvas.width, -Math.random() * canvas.height * 2, rpCount, rpDist, rpThick, 45, 1, 0.05);
}
var confettiPaperCount = 25;
var confettiPapers = new Array();
ConfettiPaper.bounds = new Vector2(canvas.width, canvas.height);
for (i = 0; i < confettiPaperCount; i++) {
confettiPapers[i] = new ConfettiPaper(Math.random() * canvas.width, Math.random() * canvas.height);
}
this.resize = function() {
canvas.width = canvasParent.offsetWidth;
canvas.height = canvasParent.offsetHeight;
ConfettiPaper.bounds = new Vector2(canvas.width, canvas.height);
ConfettiRibbon.bounds = new Vector2(canvas.width, canvas.height);
}
this.start = function() {
this.stop()
var context = this
this.interval = setInterval(function() {
confetti.update();
}, 1000.0 / frameRate)
}
this.stop = function() {
clearInterval(this.interval);
}
this.update = function() {
var i = 0;
context.clearRect(0, 0, canvas.width, canvas.height);
for (i = 0; i < confettiPaperCount; i++) {
confettiPapers[i].Update(dt);
confettiPapers[i].Draw(context);
}
for (i = 0; i < confettiRibbonCount; i++) {
confettiRibbons[i].Update(dt);
confettiRibbons[i].Draw(context);
}
}
}
$('body div#confetti').fadeIn('fast');
var confetti = new confetti.Context('confetti');
confetti.start();
$(window).resize(function() {
confetti.resize();
});
setTimeout(function()
{
$('body div#confetti').fadeOut('fast');
}, 1000);
//===============================
});
})();