/
waltz_of_the_circles.pde
68 lines (56 loc) · 2.01 KB
/
waltz_of_the_circles.pde
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
/*
based on: https://observablehq.com/@rreusser/instanced-webgl-circles
*/
final int MAX_CIRCLE_CNT = 2500, MIN_CIRCLE_CNT = 100,
MAX_VERTEX_CNT = 30, MIN_VERTEX_CNT = 3;
int circleCnt, vertexCnt;
void setup() {
size(700, 700);
}
void draw() {
background(0);
translate(width / 2, height / 2);
updateCntByMouse();
for (int ci = 0; ci < circleCnt; ci++) {
float time = float(frameCount) / 20;
float thetaC = map(ci, 0, circleCnt, 0, TAU);
float scale = 300;
PVector circleCenter = getCenterByTheta(thetaC, time, scale);
float circleSize = getSizeByTheta(thetaC, time, scale);
color c = getColorByTheta(thetaC, time);
stroke(c);
noFill();
beginShape();
for (int vi = 0; vi < vertexCnt; vi++) {
float thetaV = map(vi, 0, vertexCnt, 0, TAU);
float x = circleCenter.x + cos(thetaV) * circleSize;
float y = circleCenter.y + sin(thetaV) * circleSize;
vertex(x, y);
}
endShape(CLOSE);
}
}
void updateCntByMouse() {
float xoffset = abs(mouseX - width / 2), yoffset = abs(mouseY - height / 2);
circleCnt = int(map(xoffset, 0, width / 2, MAX_CIRCLE_CNT, MIN_CIRCLE_CNT));
vertexCnt = int(map(yoffset, 0, height / 2, MAX_VERTEX_CNT, MIN_VERTEX_CNT));
}
PVector getCenterByTheta(float theta, float time, float scale) {
PVector direction = new PVector(cos(theta), sin(theta));
float distance = 0.6 + 0.2 * cos(theta * 6.0 + cos(theta * 8.0 + time));
PVector circleCenter = PVector.mult(direction, distance * scale);
return circleCenter;
}
float getSizeByTheta(float theta, float time, float scale) {
float offset = 0.2 + 0.12 * cos(theta * 9.0 - time * 2.0);
float circleSize = scale * offset;
return circleSize;
}
color getColorByTheta(float theta, float time) {
float th = 8.0 * theta + time * 2.0;
float r = 0.6 + 0.4 * cos(th),
g = 0.6 + 0.4 * cos(th - PI / 3),
b = 0.6 + 0.4 * cos(th - PI * 2.0 / 3.0),
alpha = map(circleCnt, MIN_CIRCLE_CNT, MAX_CIRCLE_CNT, 150, 30);
return color(r * 255, g * 255, b * 255, alpha);
}