-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_walkers_perlin_noise.html
108 lines (90 loc) · 2.09 KB
/
random_walkers_perlin_noise.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!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 = [];
let xoff = 0;
let yoff = 0;
function setup() {
createCanvas(500, 400);
for (let i = 0; i < 10; i++) {
w[i] = new Walker();
w[i].num = i;
array.push([]);
}
// frameRate(10);
}
function draw() {
background(200);
for (let i = 0; i < w.length; i++) {
let nx = noise(w[i].xoff);
let x = map(nx, 0, 1, -1, 1);
w[i].xoff += w[i].step;
let ny = noise(w[i].yoff);
let y = map(ny, 0, 1, -1, 1);
w[i].yoff += w[i].step;
// console.log("x: " + x + "\ny: " + y)
w[i].update(x, y);
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();
}
}
function mousePressed() {
noLoop();
}
class Walker {
constructor() {
this.pos = createVector(random(50, width - 50), random(50, height - 50));
this.xoff = random(-1000, 1000);
this.yoff = random(-1000, 1000);
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(x, y) {
// this.vel = createVector(random(-5, 5), random(-5, 5));
this.vel = createVector(x, y);
this.pos.add(this.vel);
array[this.num].push({
x: this.pos.x,
y: this.pos.y,
step: this.step,
xoff: this.xoff,
yoff: this.yoff,
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>