-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript_results.js
More file actions
67 lines (50 loc) · 1.49 KB
/
script_results.js
File metadata and controls
67 lines (50 loc) · 1.49 KB
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
const canvasEl = document.querySelector('#canvas');
const w = canvasEl.width = window.innerWidth;
const h = canvasEl.height = window.innerHeight * 2;
function loop() {
requestAnimationFrame(loop);
ctx.clearRect(0,0,w,h);
confs.forEach((conf) => {
conf.update();
conf.draw();
})
}
function Confetti () {
//construct confetti
const colours = ['#fde132', '#009bde', '#ff6b00'];
this.x = Math.round(Math.random() * w);
this.y = Math.round(Math.random() * h)-(h/2);
this.rotation = Math.random()*360;
const size = Math.random()*(w/60);
this.size = size < 15 ? 15 : size;
this.color = colours[Math.floor(colours.length * Math.random())];
this.speed = this.size/7;
this.opacity = Math.random();
this.shiftDirection = Math.random() > 0.5 ? 1 : -1;
}
Confetti.prototype.border = function() {
if (this.y >= h) {
this.y = h;
}
}
Confetti.prototype.update = function() {
this.y += this.speed;
if (this.y <= h) {
this.x += this.shiftDirection/3;
this.rotation += this.shiftDirection*this.speed/100;
}
if (this.y > h) this.border();
};
Confetti.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, this.rotation, this.rotation+(Math.PI/2));
ctx.lineTo(this.x, this.y);
ctx.closePath();
ctx.globalAlpha = this.opacity;
ctx.fillStyle = this.color;
ctx.fill();
};
const ctx = canvasEl.getContext('2d');
const confNum = Math.floor(w / 4);
const confs = new Array(confNum).fill().map(_ => new Confetti());
loop();