-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
170 lines (144 loc) · 3.59 KB
/
sketch.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Description: Main file for the collision simulation
// * Global variables
let circles = [];
let circlesNum = 0;
let inputs = [];
let walls;
let play = false;
let elasticity = 90; // Elastic collision
let grabRadius = 40;
let grabbing = false;
let globalGrabDir = false;
let simSpeed = 1;
let lastTime = 0;
let deltaTime = 0;
let font;
let particles = [];
// * Functions
// Particle system
function updateParticles() {
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].update();
if (!particles[i].isAlive()) {
particles.splice(i, 1); // Remove dead particles
}
}
}
function displayParticles() {
for (let particle of particles) {
particle.display();
}
}
function preload() {
// import sharetechmono font
font = loadFont('assets/ShareTechMono-Regular.ttf');
}
function setup() {
// Set up the canvas
walls = [15, 15, 585, windowHeight-15];
createCanvas(walls[2]+15, windowHeight);
// Create the circles
circles.push(
new Circle(
50,
walls[3] * 0.5 + 50,
5,
2,
1
)
);
circles.push(
new Circle(
walls[2] - 50,
walls[3] * 0.5,
-2,
4,
3
)
);
circles.push(
new Circle(
walls[2] * 0.5,
walls[3] * 0.5 - 50,
0,
10,
5
)
);
// Create the data panels
circles.forEach((circle) => {
UIEvents.updateDataPanel(circle);
});
// Set up the animation
lastTime = performance.now();
requestAnimationFrame(updateAnimation);
circlesNum = circles.length;
}
function draw() { } // Not using draw()
// Animation loop
function updateAnimation(currentTime) {
// Set up the canvas
createCanvas(walls[2]+15, windowHeight);
walls[3] = windowHeight-15;
// Match background to system theme
if (window.matchMedia('(prefers-color-scheme: light)').matches) {
background('#ECECED');
} else {
background('#121212');
}
// Update the simulation speed
deltaTime = (currentTime - lastTime) / 10;
lastTime = currentTime;
// Call the updateAnimation function again on the next frame
requestAnimationFrame(updateAnimation);
// Get the simulation speed
let outputSimSpeedParagraph = document.getElementById("outputSimSpeed");
outputSimSpeedParagraph.textContent = 'Simulation Speed (x' + simSpeed + ')';
// Get the elasticity value
let outputElasticityParagraph = document.getElementById("outputElasticity");
outputElasticityParagraph.textContent = 'Elasticity Value (' + elasticity + '%)';
display();
}
// Display
function display() {
// User interface
userIntf();
updateParticles(); // Update particles
displayParticles(); // Display particles
UIEvents.updateDataPage(); // Update circles
// Update the circles
for (let i = 0; i < circles.length; i++) {
circles[i].draw();
circles[i].displayLabels();
// Check for collisions only if the simulation is playing
if (play) {
for (let j = i + 1; j < circles.length; j++) {
if (circles[i].checkCollision(circles[j])) {
circles[i].handleCollision(circles[j]);
}
}
circles[i].updatePosition();
} else {
// If the simulation is paused, check if the user is grabbing a circle
circles[i].grabChange();
}
}
}
// User interface
function userIntf() {
// Collision container
if (window.matchMedia('(prefers-color-scheme: light)').matches) {
fill('#FFFFFF');
} else {
fill('#1e2329');
}
rect(walls[0], walls[1], walls[2] - walls[0], walls[3] - walls[1], 15);
}
// Mouse events
function mousePressed() {
grabbing = true;
}
function mouseReleased() {
grabbing = false;
globalGrabDir = false;
}