-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_0.js
150 lines (117 loc) · 3.67 KB
/
run_0.js
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
// Options
let numParticles = 2000;
// Setup a simulation
const canvas = document.getElementById("simCanvas");
const ctx = canvas.getContext("2d");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let simulator = new Simulator(canvas.width, canvas.height, numParticles);
simulator.running = true;
const fpsMonitor = new FPSMonitor();
function loop() {
simulator.update();
fpsMonitor.update();
ctx.clearRect(0, 0, canvas.width, canvas.height);
simulator.draw(ctx);
requestAnimationFrame(loop);
}
loop();
// Event listeners
const materialSliders = ["restDensity", "stiffness", "nearStiffness", "springStiffness", "plasticity", "yieldRatio", "minDistRatio", "linViscosity", "quadViscosity", "kernelRadius", "pointSize", "gravX", "gravY", "dt"];
for (let sliderId of materialSliders) {
let slider = document.getElementById(sliderId);
if (slider) {
slider.addEventListener("input", (e) => {
simulator.material[sliderId] = e.target.value;
});
}
}
document.getElementById("startButton").addEventListener("click", () => {
for (let sliderId of materialSliders) {
let slider = document.getElementById(sliderId);
if (slider) {
simulator.material[sliderId] = slider.value;
}
}
simulator.start();
});
document.getElementById("pauseButton").addEventListener("click", () => {
simulator.pause();
});
document.getElementById("stepButton").addEventListener("click", () => {
simulator.running = true;
simulator.update();
simulator.running = false;
});
document.getElementById("resetButton").addEventListener("click", () => {
simulator = new Simulator(canvas.width, canvas.height, numParticles);
});
let collapseButton = document.getElementById("collapseButton");
if (collapseButton) {
document.getElementById("collapseButton").addEventListener("click", () => {
let controls = document.getElementById("controls");
if (controls.style.display == "none") {
controls.style.display = "block";
collapseButton.innerText = "Collapse";
} else {
controls.style.display = "none";
collapseButton.innerText = "Expand";
}
});
}
document.getElementById("numParticles").addEventListener("input", (e) => {
if (e.target.value == numParticles) {
return;
}
numParticles = e.target.value;
simulator = new Simulator(canvas.width, canvas.height, numParticles);
});
{
let useSpatialHash = document.getElementById("useSpatialHash")
if (useSpatialHash) {
useSpatialHash.addEventListener("change", (e) => {
simulator.useSpatialHash = e.target.checked;
});
}
}
window.addEventListener("resize", () => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
simulator.resize(canvas.width, canvas.height);
});
window.addEventListener("pointermove", (e) => {
simulator.mouseX = e.clientX;
simulator.mouseY = e.clientY;
});
window.addEventListener("pointerdown", (e) => {
// Account for first-frame drags (mobile primarily)
simulator.mouseX = e.clientX;
simulator.mouseY = e.clientY;
simulator.mousePrevX = e.clientX;
simulator.mousePrevY = e.clientY;
if (e.button == 0) {
simulator.drag = true;
}
});
window.addEventListener("pointerup", (e) => {
if (e.button == 0) {
simulator.drag = false;
}
});
const actionKeys = { "e": "emit", "d": "drain", "a": "attract", "r": "repel" };
window.addEventListener("keydown", (e) => {
if (actionKeys[e.key]) {
simulator[actionKeys[e.key]] = true;
}
});
window.addEventListener("keyup", (e) => {
if (actionKeys[e.key]) {
simulator[actionKeys[e.key]] = false;
}
});
window.addEventListener("blur", () => {
for (let key in actionKeys) {
simulator[actionKeys[key]] = false;
}
simulator.drag = false;
});