-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_walkers.html
84 lines (72 loc) · 1.58 KB
/
random_walkers.html
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
<!DOCTYPE html>
<html>
<head>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<script>
var w = [];
var array = [];
function setup() {
createCanvas(500, 400);
for (let i = 0; i < 10; i++) {
w[i] = new Walker();
w[i].num = i;
array.push([]);
}
}
function draw() {
background(200);
for (let i = 0; i < w.length; i++) {
w[i].update();
w[i].display();
}
for (let i = 0; i < array.length; i++) {
noFill();
beginShape();
for (let j = 0; j < array[i].length; j++) {
if (array[i].length > 1000) {
array[i].splice(0, 1);
}
stroke(array[i][j].r, array[i][j].g, array[i][j].b);
vertex(array[i][j].x, array[i][j].y);
}
endShape();
}
}
class Walker {
constructor() {
this.pos = createVector(random(50, width - 50), random(50, height - 50));
this.step = random(0.01, 0.02);
this.num = 0;
this.r = random(10, 255);
this.g = random(10, 255);
this.b = random(10, 255);
}
update() {
this.vel = createVector(random(-5, 5), random(-5, 5));
this.pos.add(this.vel);
array[this.num].push({
x: this.pos.x,
y: this.pos.y,
step: this.step,
r: this.r,
g: this.g,
b: this.b,
});
}
display() {
stroke(0);
fill(this.r, this.g, this.b);
ellipse(this.pos.x, this.pos.y, 10, 10);
}
}
</script>
</body>
</html>