-
Notifications
You must be signed in to change notification settings - Fork 2
/
parameters.js
75 lines (67 loc) · 1.49 KB
/
parameters.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
var canvas = document.getElementById('canvas');
var c = canvas.getContext('2d');
var particles = [];
var tick = 0;
function loop() {
createParticles();
updateParticles();
killParticles();
drawParticles();
setTimeout(loop, 30);
}
setTimeout(loop, 30);
function randomBetween(min, max) {
return Math.random() * (max - min) + min;
}
function randomColor() {
return '#'+Math.round(randomBetween(0, 0xFFF)).toString(16)
}
function createParticles() {
//check on every 10th tick check
if(tick % 10 == 0) {
//add particle if fewer than 100
if(particles.length < 1) {
particles.push({
x: randomBetween(0, canvas.width),
y: 0,
speed: randomBetween(2, 5),
radius: randomBetween(5, 10),
color: "white",
});
}
}
++tick;
}
function updateParticles() {
for(var i in particles) {
var part = particles[i];
part.y += part.speed;
part.x = Math.sin(part.y / param('sin-factor', 5, 0)) *
param('scale', 10, 0) + 200;
}
}
function killParticles() {
for(var i in particles) {
var part = particles[i];
if(part.y > canvas.height) {
part.y = 0;
part.color = randomColor();
}
}
}
function drawParticles() {
var c = canvas.getContext('2d');
c.fillStyle = "black";
//c.fillRect(0,0,canvas.width,canvas.height);
for(var i in particles) {
var part = particles[i];
c.beginPath();
c.arc(part.x, part.y, part.radius, 0, Math.PI*2);
c.closePath();
c.fillStyle = part.color;
c.fill();
}
}
function clearCanvas() {
c.clearRect(0, 0, canvas.width, canvas.height);
}