-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalternating-spirals.js
More file actions
189 lines (177 loc) · 5.02 KB
/
alternating-spirals.js
File metadata and controls
189 lines (177 loc) · 5.02 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
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
// Initializing various things
let pos;
let prev;
let walker;
let center;
let resetCounter = 1;
// Creating array that will hold lines
let lines = [];
// Parameters for sliders/adjustments
let newSize = 1; // stroke size
let newOpacity = 100; // opacity of lines
let newBranch = 7; // chance of branching
let newTerm = 3; // chance of terminiating
let newDense = 1; // multiplying velocity magnitude
let newFade = 5; // how quickly old stalks fade
let newSat = 5; // how quickly saturation rises
let newStarSize = 11; // starburst size
let newStarPts = 11; // starburst points
// Creates variables for the viewport w/h
const vw = Math.max(
document.documentElement.clientWidth || 0,
window.innerWidth || 0
);
const vh = Math.max(
document.documentElement.clientHeight || 0,
window.innerHeight || 0
);
// p5 setup, runs once when page is loaded
function setup() {
createCanvas(vw, vh);
walker = new Walker(vw / 2, vh / 2);
lines.push(walker);
background(0);
noLoop();
setInterval(redraw, 10); // where 10 is the minimum time between frames in ms
}
// p5 draw, loops infinitely
function draw() {
// // This part makes it go super fast, comment out to go back to normal
// noLoop();
// setInterval(redraw, 0);
for (walker of lines) {
walker.update();
walker.show();
}
colorMode(RGB, 100, 100, 100, 100);
stroke(color(100, 100, 100, 100));
background(0, 0, 0, 1 + lines.length / 1000);
text(`${lines.length * Math.pow(-1, resetCounter + 1)}`, vw / 2, vh / 2);
}
class Walker {
constructor(x, y) {
this.pos = createVector(x, y);
this.in = 1;
}
update() {
// creates vector pointing in random direction
this.vel = p5.Vector.random2D();
this.vel.normalize();
this.vel.setMag(1);
// multiplies vel length/magnitude
this.vel.mult(newDense);
// sets a vector located at the middle of the screen
center = createVector(vw / 2, vh / 2);
// try to create vector pointing from center to current position
this.outgrowth = center.sub(this.pos);
this.outgrowth.mult(-0.007);
this.outgrowth.mult(this.in);
const roll = random(100);
if (roll > 5) {
this.vel.add(this.outgrowth);
if (roll > 50) {
this.vel.rotate(
90 * Math.pow(-1, 1 + resetCounter) * Math.pow(this.in, resetCounter)
);
}
this.pos.add(this.vel);
} else {
const reroll = random(100);
if (reroll < newBranch) {
const newWalker = new Walker(this.pos.x, this.pos.y);
lines.push(newWalker);
console.log("new walker attempted");
if (lines.length > 1000) {
lines = [];
background(0);
resetCounter++;
const nextWalker = new Walker(vw / 2, vh / 2);
lines.push(nextWalker);
}
}
if (reroll > 100 - newTerm) {
colorMode(RGB, 100, 100, 100, 1);
stroke(color(100, 100, 100, 0.01));
star(
this.pos.x, // x location
this.pos.y, // y location
1, // inner radius
random(newStarSize) + lines.length / 10, // outer radius
random(newStarPts) + lines.length / 20 // number of points
);
lines.pop(this);
console.log("pop");
if (lines.length === 0) {
background(0, 0, 0, newFade / 20);
const nextWalker = new Walker(vw / 2, vh / 2);
lines.push(nextWalker);
}
}
}
// if (
// this.pos.y > height ||
// this.pos.y < 0 ||
// this.pos.x > width ||
// this.pos.x < 0
// ) {
// lines = [];
// background(0, 0, 0, newFade / 20);
// const nextWalker = new Walker(vw / 2, vh / 2);
// lines.push(nextWalker);
// }
if (this.pos.y > height) {
this.pos.y = 0;
this.in = -1;
}
if (this.pos.y < 0) {
this.pos.y = height;
this.in = -1;
}
if (this.pos.x > width) {
this.pos.x = 0;
this.in = -1;
}
if (this.pos.x < 0) {
this.pos.x = width;
this.in = -1;
}
if (this.in === -1) {
if (
this.pos.y < height / 2 + height / 25 &&
this.pos.y > height / 2 - height / 25 &&
this.pos.x < width / 2 + width / 25 &&
this.pos.x > width / 2 - width / 25
) {
this.in = 1;
}
}
}
show() {
colorMode(HSB, 100, 100, 100, 100);
let hue = floor(100 * ((frameCount / 5) % 100)) / 100;
let saturation = 20 + (lines.length * newSat) / 2;
if (saturation > 100) {
saturation = 100;
}
let brightness = 100;
let opacity = newOpacity;
let c = color(hue, saturation, brightness, opacity);
stroke(c);
strokeWeight(newSize);
point(this.pos.x, this.pos.y);
}
}
function star(x, y, radius1, radius2, npoints) {
let angle = TWO_PI / npoints;
let halfAngle = angle / 2.0;
beginShape();
for (let a = 0; a < TWO_PI; a += angle) {
let sx = x + cos(a) * radius2;
let sy = y + sin(a) * radius2;
vertex(sx, sy);
sx = x + cos(a + halfAngle) * radius1;
sy = y + sin(a + halfAngle) * radius1;
vertex(sx, sy);
}
endShape(CLOSE);
}