-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
189 lines (165 loc) · 5.87 KB
/
script.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
const canvas = document.getElementById("canvas1");
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
//let particlesArray;
let particlesSet;
let newparticlesize;
let DISTORTMOUSE = 3;
let PARTICLESPEED = 3;
let COLOR = '#dddddd'; //'#ffffff';
let SIZE = 8;
let MAXSIZE = 18;
let LIVELENGTH = 1000;
let CELLDIVPROP = 0.0025;
let OVERPOP = false;
let UNDERPOPLIMIT = 12;
let UNDERPOPMULT = 8;
let NUMBEROFPARTICLES = Math.min(5, (canvas.height * canvas.width) / 40000);
let MAXNUMBEROFPARTICLES = Math.min(125, (canvas.height * canvas.width) / 10000);
// Mouse position
let mouse = {
x: null,
y: null,
radius: (canvas.height/ 120) * (canvas.width / 120)
}
window.addEventListener('mousemove',
function(event){
mouse.x = event.x;
mouse.y = event.y;
}
);
function randomBinary(p = 0.5) {
return 2 * (Math.ceil(Math.random() - (1-p))) - 1;
}
class Particle {
constructor(x, y, directionX, directionY, size, COLOR, length){
this.x = x;
this.y = y;
this.directionX = directionX;
this.directionY = directionY;
this.COLOR = COLOR;
this.lifelength = length;
this.timer = 0;
if (size <= MAXSIZE){
this.size = size;
} else {
this.size = MAXSIZE;
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2, false);
ctx.fillStyle = COLOR;
ctx.fill();
}
increasesize() {
if (this.size <= MAXSIZE -1){
this.size += 1;
}
}
update() {
// Check if particle still within canvas, else change direction
if(this.x > canvas.width || this.x < 0) {
this.directionX = - this.directionX;
}
if (this.y > canvas.height || this.y < 0){
this.directionY = - this.directionY;
}
// chechk for coluision detecion - mouse
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx*dx + dy*dy);
// Change direction if the mouse is close
if (distance < mouse.radius + this.size){
if(mouse.x < this.x && this.x < canvas.width - this.size * DISTORTMOUSE){
this.x += DISTORTMOUSE;
this.directionY = - this.directionY;
}
if(mouse.x > this.x && this.x > this.size * DISTORTMOUSE){
this.x -= DISTORTMOUSE;
this.directionY = -this.directionY;
}
if(mouse.y < this.y && this.y < canvas.height - this.size * DISTORTMOUSE){
this.y += DISTORTMOUSE;
this.directionX = - this.directionX;
}
if(mouse.y > this.y && this.y > this.size * DISTORTMOUSE){
this.y -= DISTORTMOUSE;
this.directionX = - this.directionX;
}
// this.directionX = - this.directionX;
// this.directionY = - this.directionY;
}
// Introduce random movement changes
this.directionX *= (- randomBinary(0.01));
this.directionY *= (- randomBinary(0.01));
// Update position
this.x += this.directionX ;
this.y += this.directionY;
// Update the timer how long the particle already lives
this.timer += 1;
this.draw();
}
}
function init() {
//particlesArray = [];
particlesSet = new Set();
for (let i = 0; i < NUMBEROFPARTICLES; i++){
let size = (Math.random() * SIZE) + 1;
let x = (Math.random() * ((innerWidth - size * 2) - (size * 2)) + size * 2);
let y = (Math.random() * ((innerHeight - size * 2) - (size * 2)) + size * 2);
let directionX = (Math.random() * PARTICLESPEED) - PARTICLESPEED/2;
let directionY = (Math.random() * PARTICLESPEED) - PARTICLESPEED/2;
let length = LIVELENGTH * Math.random();
//particlesArray.push(new Particle(x, y, directionX, directionY, size, COLOR));
particlesSet.add(new Particle(x, y, directionX, directionY, size, COLOR, length));
}
}
function animate() {
requestAnimationFrame(animate);
ctx.clearRect(0, 0, innerWidth, innerHeight);
// Check for overpopulation
if (particlesSet.size > MAXNUMBEROFPARTICLES){
OVERPOP = true;
} else {
OVERPOP = false;
}
for (let particle of particlesSet){
// Cell division if random event happens
if (!OVERPOP && // Only create new cells if there is no overpopulation
(Math.random() < CELLDIVPROP ||
(particlesSet.size < UNDERPOPLIMIT && Math.random < UNDERPOPMULT * CELLDIVPROP))) // If there are very few cells, than increase the propulation of cell division
{
// Create new cell
if(particlesSet.size < UNDERPOPLIMIT){
newparticlesize = Math.ceil(particle.size * 1.8);
} else {
newparticlesize = particle.size;
}
particlesSet.add(new Particle(particle.x, particle.y,
randomBinary() * particle.directionX, randomBinary() * particle.directionY, newparticlesize,
particle.COLOR, particle.length));
} else {
// If there is no cell division, update the particle and shrink it if
// the timer is longer than the lifelength
particle.update();
if(particle.timer > particle.lifelength){
if(particle.timer % 20 == 0){
particle.size -= 1;
}
}
// If the size is 1, then delete it
if(particle.size <= 1){
particlesSet.delete(particle);
}
}
if (OVERPOP){
particle.lifelength = 100 // particle.lifelength * 0.25;
}
}
// console.log("Animate finished!")
}
console.log("start");
init();
animate();