/
210919.tsx
152 lines (123 loc) · 3.49 KB
/
210919.tsx
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
import React from "react";
import type p5 from "p5";
import { P5Renderer } from "Renderers/P5";
import { moveToVector } from "LibUtils/p5";
const sketch = (p: p5) => {
let shortestDimension: number;
let maxR: number;
let mouseLocation: p5.Vector;
let target: p5.Vector;
let resetMouseLocation: () => void;
let mouseHasMoved = false;
let lastMouseMovement = 0;
const balls: Ball[] = [];
class Ball {
xOff: number;
yOff: number;
zOff: number;
r: number;
x: number;
y: number;
z: number;
constructor(r?: number) {
this.xOff = p.random(200);
this.yOff = p.random(400, 600);
this.zOff = p.random(800, 1000);
this.r = r || 1;
}
update() {
this.xOff += 0.01;
this.yOff += 0.01;
this.zOff += 0.01;
this.x =
mouseLocation.x + p.map(p.noise(this.xOff), 0, 1, -p.width / 2, p.width / 2);
this.y =
mouseLocation.y +
p.map(p.noise(this.yOff), 0, 1, -p.height / 2, p.height / 2);
this.z =
mouseLocation.z +
p.map(
p.noise(this.zOff),
0,
1,
-shortestDimension / 2,
shortestDimension / 2
);
if (this.r < maxR) this.r += 1;
}
show() {
p.push();
p.translate(this.x, this.y, this.z);
p.sphere(this.r);
p.pop();
}
collides(sibling: { x: number; y: number; z: number; r: number }) {
const d = p.dist(this.x, this.y, this.z, sibling.x, sibling.y, sibling.z);
return d < this.r / 2 + sibling.r;
}
excited() {
this.xOff += 0.005;
this.yOff += 0.005;
}
}
p.setup = () => {
p.createCanvas(p.windowWidth, p.windowHeight, p.WEBGL);
shortestDimension = Math.min(p.width, p.height);
p.background(20);
p.noStroke();
maxR = p.width > 500 ? 60 : 36;
for (let i = 0; i < 3; i++) {
balls.push(new Ball(maxR));
}
resetMouseLocation = () => {
mouseLocation = p.createVector(
p.width / 2,
p.height / 2,
shortestDimension / 2
);
};
target = p.createVector(p.width / 2, p.height / 2, shortestDimension / 2);
resetMouseLocation();
};
p.draw = () => {
p.camera(0, 0, shortestDimension * 2, p.tan(p.PI / 6), 0, 0, 0, 1, 0);
p.background(20);
p.translate(-p.width / 2, -p.height / 2);
const dirY = (p.mouseY / p.height - 0.5) * 2;
const dirX = (p.mouseX / p.width - 0.5) * 2;
p.directionalLight(255, 255, 255, -dirX, -dirY, 0.1);
p.pointLight(255, 255, 255, 0, 0, 500);
p.ambientMaterial(255, 255, 255);
if (p.frameCount % 360 === 0) {
if (balls.length < p.width / 40) balls.push(new Ball());
}
balls.forEach(ball => {
ball.update();
ball.show();
balls.forEach(sibling => {
if (ball !== sibling && ball.collides(sibling)) {
ball.excited();
sibling.excited();
}
});
});
if (mouseHasMoved) {
target =
lastMouseMovement < p.frameCount - 100
? p.createVector(p.width / 2, p.height / 2, shortestDimension / 2)
: p.createVector(p.mouseX, p.mouseY, shortestDimension / 2);
} else {
resetMouseLocation();
}
mouseLocation = moveToVector(mouseLocation, target, p);
};
p.mouseMoved = () => {
mouseHasMoved = true;
lastMouseMovement = p.frameCount;
};
p.mousePressed = () => {
if (balls.length < p.width / 40) balls.push(new Ball());
};
};
const S210919 = () => <P5Renderer sketch={sketch} />;
export default S210919;