-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMSP.html
More file actions
720 lines (633 loc) · 35.5 KB
/
CMSP.html
File metadata and controls
720 lines (633 loc) · 35.5 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centre of Mass Interactive Lab</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Outfit:wght@400;700&family=Inter:wght@300;400;600;700&display=swap" rel="stylesheet">
<!-- MathJax for LaTeX rendering -->
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<style>
/* Custom scrollbar for webkit browsers */
::-webkit-scrollbar {
width: 8px;
height: 8px;
}
::-webkit-scrollbar-track {
background: var(--bg-color-dark);
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
background: var(--primary-light);
border-radius: 10px;
}
::-webkit-scrollbar-thumb:hover {
background: var(--primary-color);
}
/* Basic CSS variables for theme, will be mapped to Tailwind config */
:root {
--primary-color: #0d9488; /* Teal 600 */
--primary-light: #2dd4bf; /* Teal 400 */
--secondary-color: #f59e0b; /* Amber 500 */
--accent-color: #ec4899; /* Pink 500 */
--bg-color-dark: #1f2937; /* Gray 800 */
--bg-color-medium: #374151; /* Gray 700 */
--card-bg-light: rgba(55, 65, 81, 0.7); /* Gray 700 with transparency */
--text-color-light: #e5e7eb; /* Gray 200 */
--text-color-dark: #d1d5db; /* Gray 300 */
}
body {
font-family: 'Inter', sans-serif;
background-color: var(--bg-color-dark);
color: var(--text-color-light);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 1rem;
overflow-x: hidden; /* Prevent horizontal scroll */
}
h1, h2, h3, h4, h5, h6 {
font-family: 'Outfit', sans-serif;
color: var(--primary-light);
}
.card {
background: var(--card-bg-light);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 1.5rem; /* rounded-xl */
box-shadow: 0 10px 15px rgba(0, 0, 0, 0.2); /* shadow-lg */
padding: 1.5rem;
margin-bottom: 1.5rem;
display: flex;
flex-direction: column;
gap: 1rem;
transition: all 0.3s ease-in-out;
background-image: linear-gradient(135deg, rgba(55, 65, 81, 0.7) 0%, rgba(31, 41, 55, 0.7) 100%);
}
.card:hover {
transform: translateY(-5px);
box-shadow: 0 15px 25px rgba(0, 0, 0, 0.3);
}
/* Custom range slider styling */
input[type="range"] {
-webkit-appearance: none;
width: 100%;
height: 8px;
background: var(--bg-color-medium);
outline: none;
border-radius: 5px;
opacity: 0.8;
transition: opacity .2s;
}
input[type="range"]:hover {
opacity: 1;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 20px;
height: 20px;
border-radius: 50%;
background: var(--primary-light);
cursor: pointer;
border: 2px solid var(--primary-color);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
transition: background 0.3s ease;
}
input[type="range"]::-webkit-slider-thumb:hover {
background: var(--accent-color);
}
/* Confetti animation */
.confetti-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
overflow: hidden;
z-index: 9999;
display: none;
}
.confetti {
position: absolute;
width: 10px;
height: 10px;
background-color: var(--primary-light);
animation: confetti-fall 3s linear forwards;
opacity: 0;
}
@keyframes confetti-fall {
0% {
transform: translateY(-100px) rotate(0deg);
opacity: 1;
}
100% {
transform: translateY(100vh) rotate(720deg);
opacity: 0;
}
}
/* For syntax highlighting (basic example, can be extended) */
pre code {
display: block;
padding: 1em;
background: var(--bg-color-medium);
color: var(--text-color-light);
border-radius: 0.5rem;
overflow-x: auto;
border: 1px solid rgba(255, 255, 255, 0.15);
font-family: 'Fira Code', 'Monaco', 'Courier New', monospace; /* Fira Code is good for programming */
font-size: 0.875rem;
line-height: 1.4;
}
.token.keyword { color: #8BE9FD; } /* Cyan */
.token.function { color: #50FA7B; } /* Green */
.token.string { color: #F1FA8C; } /* Yellow */
.token.number { color: #BD93F9; } /* Purple */
.token.comment { color: #6272A4; font-style: italic; } /* Gray Blue */
.token.operator { color: #FF79C6; } /* Pink */
.token.punctuation { color: #FF79C6; } /* Pink */
.token.variable { color: #FFB86C; } /* Orange */
.token.property { color: #FFB86C; } /* Orange */
</style>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
primary: {
DEFAULT: 'var(--primary-color)',
light: 'var(--primary-light)',
},
secondary: 'var(--secondary-color)',
accent: 'var(--accent-color)',
dark: 'var(--bg-color-dark)',
medium: 'var(--bg-color-medium)',
card: 'var(--card-bg-light)',
'text-light': 'var(--text-color-light)',
'text-dark': 'var(--text-color-dark)',
},
fontFamily: {
display: ['Outfit', 'sans-serif'],
body: ['Inter', 'sans-serif'],
},
}
}
}
</script>
</head>
<body class="bg-dark text-text-light font-body p-4 lg:p-8">
<main class="container mx-auto grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 max-w-7xl">
<!-- 1. Visual Interactive Demo Explanations -->
<section class="card lg:col-span-2">
<h2 class="text-3xl font-display font-bold text-primary-light mb-4">Theory Explained: Centre of Mass</h2>
<div class="space-y-4 text-text-dark text-lg">
<p>The <strong class="text-primary-light">Centre of Mass (CoM)</strong> of a system of particles is a unique point where the entire mass of the system is assumed to be concentrated. It's a crucial concept for understanding the translational motion of a system as a whole, simplifying complex dynamics.</p>
<p>A <strong class="text-primary-light">System of Particles</strong> refers to any collection of two or more interacting particles, rigid bodies, or objects considered together for analysis.</p>
<h3 class="text-xl font-display font-semibold mt-6 mb-2 text-primary-light">General Formula for CoM:</h3>
<div class="overflow-x-auto p-2 bg-medium rounded-lg border border-gray-700">
\[
\mathbf{R}_{CM} = \frac{\sum_{i=1}^{n} m_i \mathbf{r}_i}{\sum_{i=1}^{n} m_i}
\]
<p class="text-sm italic text-gray-400 mt-2">where \( \mathbf{R}_{CM} = (X_{CM}, Y_{CM}, Z_{CM}) \) is the position vector of the Centre of Mass.</p>
</div>
<h3 class="text-xl font-display font-semibold mt-6 mb-2 text-primary-light">2D Formulas for CoM:</h3>
<div class="overflow-x-auto p-2 bg-medium rounded-lg border border-gray-700">
\[
X_{CM} = \frac{\sum m_i x_i}{\sum m_i} \quad ; \quad Y_{CM} = \frac{\sum m_i y_i}{\sum m_i}
\]
</div>
<div class="mt-8 pt-4 border-t border-gray-700">
<h3 class="text-xl font-display font-semibold mb-3 text-primary-light">Interactive Mass Controls:</h3>
<p class="text-text-dark mb-4">Adjust the mass of each particle using the sliders below. Then, drag the particles on the simulation canvas to observe how the Centre of Mass shifts!</p>
<div class="space-y-4">
<div>
<label for="mass1" class="block text-text-light font-medium">Particle 1 Mass (<span id="mass1Value" class="font-bold text-secondary">5.0</span> kg)</label>
<input type="range" id="mass1" min="1" max="10" value="5" step="0.1" class="w-full">
</div>
<div>
<label for="mass2" class="block text-text-light font-medium">Particle 2 Mass (<span id="mass2Value" class="font-bold text-secondary">5.0</span> kg)</label>
<input type="range" id="mass2" min="1" max="10" value="5" step="0.1" class="w-full">
</div>
<div>
<label for="mass3" class="block text-text-light font-medium">Particle 3 Mass (<span id="mass3Value" class="font-bold text-secondary">5.0</span> kg)</label>
<input type="range" id="mass3" min="1" max="10" value="5" step="0.1" class="w-full">
</div>
</div>
</div>
</div>
</section>
<!-- 2. Interactive Visual Simulations -->
<section class="card md:col-span-1">
<h2 class="text-3xl font-display font-bold text-primary-light mb-4">Interactive CoM Simulation</h2>
<div class="flex flex-col items-center justify-center gap-4">
<canvas id="comCanvas" width="400" height="400" class="bg-medium rounded-lg shadow-inner border border-gray-700 cursor-grab"></canvas>
<div class="text-lg text-center font-bold">
CoM: X = <span id="comXDisplay" class="text-secondary">0.00</span>, Y = <span id="comYDisplay" class="text-secondary">0.00</span>
</div>
</div>
</section>
<!-- 3. Interactive Input Field (Problem Solver) -->
<section class="card lg:col-span-2">
<h2 class="text-3xl font-display font-bold text-primary-light mb-4">Problem Solver: Find the CoM</h2>
<form id="problemSolverForm" class="space-y-6">
<div class="grid grid-cols-1 md:grid-cols-3 gap-6">
<!-- Particle 1 Inputs -->
<fieldset class="border border-gray-700 p-4 rounded-lg bg-medium/50 hover:bg-medium/70 transition-colors">
<legend class="text-lg font-semibold text-primary-light px-2">Particle 1</legend>
<div class="space-y-3 mt-2">
<div>
<label for="p1_m" class="block text-text-light text-sm font-medium">Mass (kg):</label>
<input type="number" id="p1_m" value="2" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
<div>
<label for="p1_x" class="block text-text-light text-sm font-medium">X-coord:</label>
<input type="number" id="p1_x" value="1" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
<div>
<label for="p1_y" class="block text-text-light text-sm font-medium">Y-coord:</label>
<input type="number" id="p1_y" value="1" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
</div>
</fieldset>
<!-- Particle 2 Inputs -->
<fieldset class="border border-gray-700 p-4 rounded-lg bg-medium/50 hover:bg-medium/70 transition-colors">
<legend class="text-lg font-semibold text-primary-light px-2">Particle 2</legend>
<div class="space-y-3 mt-2">
<div>
<label for="p2_m" class="block text-text-light text-sm font-medium">Mass (kg):</label>
<input type="number" id="p2_m" value="3" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
<div>
<label for="p2_x" class="block text-text-light text-sm font-medium">X-coord:</label>
<input type="number" id="p2_x" value="5" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
<div>
<label for="p2_y" class="block text-text-light text-sm font-medium">Y-coord:</label>
<input type="number" id="p2_y" value="2" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
</div>
</fieldset>
<!-- Particle 3 Inputs -->
<fieldset class="border border-gray-700 p-4 rounded-lg bg-medium/50 hover:bg-medium/70 transition-colors">
<legend class="text-lg font-semibold text-primary-light px-2">Particle 3</legend>
<div class="space-y-3 mt-2">
<div>
<label for="p3_m" class="block text-text-light text-sm font-medium">Mass (kg):</label>
<input type="number" id="p3_m" value="1" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
<div>
<label for="p3_x" class="block text-text-light text-sm font-medium">X-coord:</label>
<input type="number" id="p3_x" value="2" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
<div>
<label for="p3_y" class="block text-text-light text-sm font-medium">Y-coord:</label>
<input type="number" id="p3_y" value="6" step="0.1" class="w-full p-2 bg-dark rounded-md border border-gray-600 focus:ring-primary focus:border-primary text-secondary" required>
</div>
</div>
</fieldset>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mt-8">
<div>
<label for="user_com_x" class="block text-text-light text-lg font-medium mb-1">Your X<sub class="text-sm">CM</sub>:</label>
<input type="number" id="user_com_x" step="0.01" class="w-full p-3 bg-dark rounded-md border border-gray-600 focus:ring-accent focus:border-accent text-accent text-lg font-bold" placeholder="Enter X_CM" required>
</div>
<div>
<label for="user_com_y" class="block text-text-light text-lg font-medium mb-1">Your Y<sub class="text-sm">CM</sub>:</label>
<input type="number" id="user_com_y" step="0.01" class="w-full p-3 bg-dark rounded-md border border-gray-600 focus:ring-accent focus:border-accent text-accent text-lg font-bold" placeholder="Enter Y_CM" required>
</div>
</div>
<button type="submit" class="w-full py-3 px-6 bg-primary hover:bg-primary-light text-white font-bold rounded-lg transition-all duration-300 ease-in-out transform hover:scale-105 shadow-md hover:shadow-lg focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-light">
Check Answer
</button>
</form>
<div id="feedback" class="mt-8 p-4 text-center text-lg font-semibold rounded-lg hidden transition-opacity duration-300 ease-in-out opacity-0"></div>
<div class="confetti-container"></div>
</section>
<!-- 4. Simulated Developer Console -->
<section class="card lg:col-span-1">
<h2 class="text-3xl font-display font-bold text-primary-light mb-4">Dev Console: CoM Algorithm</h2>
<div class="text-text-dark text-lg mb-4">
<p>This section outlines the pseudocode logic behind the Centre of Mass calculation used in the interactive simulation.</p>
</div>
<pre class="overflow-x-auto text-sm"><code><span class="token comment">// Function to calculate the Centre of Mass for a system of particles</span>
<span class="token function">function</span> <span class="token function">calculateCentreOfMass</span>(<span class="token variable">particles</span>) {
<span class="token keyword">let</span> <span class="token variable">totalMass</span> = <span class="token number">0</span>;
<span class="token keyword">let</span> <span class="token variable">sumMx</span> = <span class="token number">0</span>; <span class="token comment">// Sum of (mass * x-coordinate)</span>
<span class="token keyword">let</span> <span class="token variable">sumMy</span> = <span class="token number">0</span>; <span class="token comment">// Sum of (mass * y-coordinate)</span>
<span class="token keyword">for</span> (<span class="token keyword">const</span> <span class="token variable">particle</span> <span class="token keyword">of</span> <span class="token variable">particles</span>) {
<span class="token variable">totalMass</span> += <span class="token variable">particle</span>.<span class="token property">mass</span>;
<span class="token variable">sumMx</span> += <span class="token variable">particle</span>.<span class="token property">mass</span> * <span class="token variable">particle</span>.<span class="token property">x</span>;
<span class="token variable">sumMy</span> += <span class="token variable">particle</span>.<span class="token property">mass</span> * <span class="token variable">particle</span>.<span class="token property">y</span>;
}
<span class="token comment">// Handle division by zero if total mass is 0</span>
<span class="token keyword">if</span> (<span class="token variable">totalMass</span> === <span class="token number">0</span>) {
<span class="token keyword">return</span> { <span class="token property">x</span>: <span class="token number">0</span>, <span class="token property">y</span>: <span class="token number">0</span> }; <span class="token comment">// Or throw an error, depending on requirements</span>
}
<span class="token keyword">const</span> <span class="token variable">comX</span> = <span class="token variable">sumMx</span> / <span class="token variable">totalMass</span>;
<span class="token keyword">const</span> <span class="token variable">comY</span> = <span class="token variable">sumMy</span> / <span class="token variable">totalMass</span>;
<span class="token keyword">return</span> { <span class="token property">x</span>: <span class="token variable">comX</span>, <span class="token property">y</span>: <span class="token variable">comY</span> };
}
<span class="token comment">// Example Usage:</span>
<span class="token keyword">const</span> <span class="token variable">sampleParticles</span> = [
{ <span class="token property">mass</span>: <span class="token number">2</span>, <span class="token property">x</span>: <span class="token number">1</span>, <span class="token property">y</span>: <span class="token number">1</span> },
{ <span class="token property">mass</span>: <span class="token number">3</span>, <span class="token property">x</span>: <span class="token number">5</span>, <span class="token property">y</span>: <span class="token number">2</span> },
{ <span class="token property">mass</span>: <span class="token number">1</span>, <span class="token property">x</span>: <span class="token number">2</span>, <span class="token property">y</span>: <span class="token number">6</span> }
];
<span class="token keyword">const</span> <span class="token variable">com</span> = <span class="token function">calculateCentreOfMass</span>(<span class="token variable">sampleParticles</span>);
<span class="token comment">// Expected X_CM: (2*1 + 3*5 + 1*2) / (2+3+1) = (2 + 15 + 2) / 6 = 19 / 6 = 3.166...</span>
<span class="token comment">// Expected Y_CM: (2*1 + 3*2 + 1*6) / (2+3+1) = (2 + 6 + 6) / 6 = 14 / 6 = 2.333...</span>
<span class="token console">console</span>.<span class="token function">log</span>(<span class="token string">"Calculated CoM:"</span>, <span class="token variable">com</span>);
</code></pre>
</section>
</main>
<script>
// --- Confetti Animation Logic ---
function createConfetti() {
const container = document.querySelector('.confetti-container');
container.style.display = 'block';
for (let i = 0; i < 50; i++) {
const confetti = document.createElement('div');
confetti.className = 'confetti';
confetti.style.left = Math.random() * 100 + 'vw';
confetti.style.backgroundColor = `hsl(${Math.random() * 360}, 70%, 60%)`;
confetti.style.animationDelay = Math.random() * 2 + 's';
confetti.style.animationDuration = (2 + Math.random() * 2) + 's';
confetti.style.width = (5 + Math.random() * 10) + 'px';
confetti.style.height = (5 + Math.random() * 10) + 'px';
container.appendChild(confetti);
}
setTimeout(() => {
container.innerHTML = '';
container.style.display = 'none';
}, 4000);
}
// --- CoM Calculation Function (Centralized) ---
function calculateCentreOfMass(particles) {
let totalMass = 0;
let sumMx = 0;
let sumMy = 0;
for (const p of particles) {
totalMass += p.mass;
sumMx += p.mass * p.x;
sumMy += p.mass * p.y;
}
if (totalMass === 0) {
return { x: 0, y: 0 };
}
return {
x: sumMx / totalMass,
y: sumMy / totalMass
};
}
// --- Section 1 & 2: Interactive Simulation ---
const canvas = document.getElementById('comCanvas');
const ctx = canvas.getContext('2d');
const comXDisplay = document.getElementById('comXDisplay');
const comYDisplay = document.getElementById('comYDisplay');
const massSliders = [
{ slider: document.getElementById('mass1'), valueDisplay: document.getElementById('mass1Value') },
{ slider: document.getElementById('mass2'), valueDisplay: document.getElementById('mass2Value') },
{ slider: document.getElementById('mass3'), valueDisplay: document.getElementById('mass3Value') },
];
let particles = [
{ mass: 5, x: -5, y: -5, color: '#03a9f4', radius: 15, isDragging: false }, // Blue
{ mass: 5, x: 5, y: -5, color: '#e53935', radius: 15, isDragging: false }, // Red
{ mass: 5, x: 0, y: 5, color: '#9c27b0', radius: 15, isDragging: false }, // Purple
];
const canvasSize = 400;
const conceptualRange = 20; // -10 to 10
const scale = canvasSize / conceptualRange;
const originOffset = canvasSize / 2;
function conceptualToCanvas(coord) {
return coord * scale + originOffset;
}
function canvasToConceptual(coord) {
return (coord - originOffset) / scale;
}
function draw() {
ctx.clearRect(0, 0, canvasSize, canvasSize);
// Draw grid (optional, but good for context)
ctx.strokeStyle = '#374151'; // Gray-700
ctx.lineWidth = 0.5;
for (let i = -10; i <= 10; i += 2) {
const x = conceptualToCanvas(i);
const y = conceptualToCanvas(i);
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, canvasSize);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(canvasSize, y);
ctx.stroke();
}
// Draw axes
ctx.strokeStyle = '#4b5563'; // Gray-600
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(0, originOffset);
ctx.lineTo(canvasSize, originOffset);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(originOffset, 0);
ctx.lineTo(originOffset, canvasSize);
ctx.stroke();
// Draw particles
particles.forEach((p, index) => {
const canvasX = conceptualToCanvas(p.x);
const canvasY = conceptualToCanvas(p.y);
const radius = 10 + (p.mass - 1) * 1.5; // Radius from 10 to 25px
p.radius = radius; // Update radius in particle object
ctx.beginPath();
ctx.arc(canvasX, canvasY, radius, 0, Math.PI * 2);
ctx.fillStyle = p.color;
ctx.shadowColor = p.color;
ctx.shadowBlur = 10;
ctx.fill();
ctx.strokeStyle = 'rgba(255,255,255,0.4)';
ctx.lineWidth = 2;
ctx.stroke();
ctx.shadowBlur = 0;
// Draw labels
ctx.fillStyle = 'white';
ctx.font = '10px Inter';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(`P${index + 1}`, canvasX, canvasY - radius - 5);
ctx.fillText(`m:${p.mass.toFixed(1)}`, canvasX, canvasY + radius + 10);
ctx.fillText(`(${p.x.toFixed(1)}, ${p.y.toFixed(1)})`, canvasX, canvasY + radius + 25);
});
// Calculate and draw CoM
const com = calculateCentreOfMass(particles);
const comCanvasX = conceptualToCanvas(com.x);
const comCanvasY = conceptualToCanvas(com.y);
ctx.beginPath();
ctx.arc(comCanvasX, comCanvasY, 8, 0, Math.PI * 2);
ctx.fillStyle = 'var(--primary-light)'; // Primary color for CoM
ctx.shadowColor = 'var(--primary-light)';
ctx.shadowBlur = 15;
ctx.fill();
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.stroke();
ctx.shadowBlur = 0;
comXDisplay.textContent = com.x.toFixed(2);
comYDisplay.textContent = com.y.toFixed(2);
}
// Event listeners for mass sliders
massSliders.forEach((item, index) => {
item.slider.addEventListener('input', (e) => {
const newMass = parseFloat(e.target.value);
particles[index].mass = newMass;
item.valueDisplay.textContent = newMass.toFixed(1);
draw();
});
});
let draggingParticle = null;
let startX, startY;
function getMousePos(canvas, event) {
const rect = canvas.getBoundingClientRect();
const clientX = event.clientX || event.touches[0].clientX;
const clientY = event.clientY || event.touches[0].clientY;
return {
x: clientX - rect.left,
y: clientY - rect.top
};
}
canvas.addEventListener('mousedown', (e) => {
const mousePos = getMousePos(canvas, e);
for (let i = 0; i < particles.length; i++) {
const p = particles[i];
const canvasX = conceptualToCanvas(p.x);
const canvasY = conceptualToCanvas(p.y);
const dist = Math.sqrt(Math.pow(mousePos.x - canvasX, 2) + Math.pow(mousePos.y - canvasY, 2));
if (dist < p.radius) {
draggingParticle = p;
p.isDragging = true;
startX = mousePos.x - canvasX;
startY = mousePos.y - canvasY;
canvas.style.cursor = 'grabbing';
break;
}
}
});
canvas.addEventListener('mousemove', (e) => {
if (draggingParticle) {
e.preventDefault();
const mousePos = getMousePos(canvas, e);
const newCanvasX = mousePos.x - startX;
const newCanvasY = mousePos.y - startY;
draggingParticle.x = canvasToConceptual(newCanvasX);
draggingParticle.y = canvasToConceptual(newCanvasY);
// Clamp to conceptual range -10 to 10
draggingParticle.x = Math.max(-10, Math.min(10, draggingParticle.x));
draggingParticle.y = Math.max(-10, Math.min(10, draggingParticle.y));
draw();
} else {
const mousePos = getMousePos(canvas, e);
let hover = false;
for (let i = 0; i < particles.length; i++) {
const p = particles[i];
const canvasX = conceptualToCanvas(p.x);
const canvasY = conceptualToCanvas(p.y);
const dist = Math.sqrt(Math.pow(mousePos.x - canvasX, 2) + Math.pow(mousePos.y - canvasY, 2));
if (dist < p.radius) {
hover = true;
break;
}
}
canvas.style.cursor = hover ? 'grab' : 'default';
}
});
canvas.addEventListener('mouseup', () => {
if (draggingParticle) {
draggingParticle.isDragging = false;
draggingParticle = null;
canvas.style.cursor = 'grab';
}
});
canvas.addEventListener('mouseout', () => {
if (draggingParticle) {
draggingParticle.isDragging = false;
draggingParticle = null;
canvas.style.cursor = 'grab';
}
});
// Touch event handling for mobile
canvas.addEventListener('touchstart', (e) => {
e.preventDefault(); // Prevent scrolling
const touch = e.touches[0];
const mousePos = getMousePos(canvas, touch);
for (let i = 0; i < particles.length; i++) {
const p = particles[i];
const canvasX = conceptualToCanvas(p.x);
const canvasY = conceptualToCanvas(p.y);
const dist = Math.sqrt(Math.pow(mousePos.x - canvasX, 2) + Math.pow(mousePos.y - canvasY, 2));
if (dist < p.radius) {
draggingParticle = p;
p.isDragging = true;
startX = mousePos.x - canvasX;
startY = mousePos.y - canvasY;
break;
}
}
});
canvas.addEventListener('touchmove', (e) => {
if (draggingParticle) {
e.preventDefault(); // Prevent scrolling
const touch = e.touches[0];
const mousePos = getMousePos(canvas, touch);
const newCanvasX = mousePos.x - startX;
const newCanvasY = mousePos.y - startY;
draggingParticle.x = canvasToConceptual(newCanvasX);
draggingParticle.y = canvasToConceptual(newCanvasY);
// Clamp to conceptual range -10 to 10
draggingParticle.x = Math.max(-10, Math.min(10, draggingParticle.x));
draggingParticle.y = Math.max(-10, Math.min(10, draggingParticle.y));
draw();
}
});
canvas.addEventListener('touchend', () => {
if (draggingParticle) {
draggingParticle.isDragging = false;
draggingParticle = null;
}
});
draw(); // Initial draw
// --- Section 3: Problem Solver ---
const problemSolverForm = document.getElementById('problemSolverForm');
const feedbackDiv = document.getElementById('feedback');
const tolerance = 0.05; // Tolerance for checking answers
problemSolverForm.addEventListener('submit', (e) => {
e.preventDefault();
const inputParticles = [
{ mass: parseFloat(document.getElementById('p1_m').value), x: parseFloat(document.getElementById('p1_x').value), y: parseFloat(document.getElementById('p1_y').value) },
{ mass: parseFloat(document.getElementById('p2_m').value), x: parseFloat(document.getElementById('p2_x').value), y: parseFloat(document.getElementById('p2_y').value) },
{ mass: parseFloat(document.getElementById('p3_m').value), x: parseFloat(document.getElementById('p3_x').value), y: parseFloat(document.getElementById('p3_y').value) },
];
const correctCoM = calculateCentreOfMass(inputParticles);
const userCoMX = parseFloat(document.getElementById('user_com_x').value);
const userCoMY = parseFloat(document.getElementById('user_com_y').value);
const isCorrectX = Math.abs(userCoMX - correctCoM.x) < tolerance;
const isCorrectY = Math.abs(userCoMY - correctCoM.y) < tolerance;
feedbackDiv.classList.remove('hidden', 'opacity-0');
feedbackDiv.classList.add('opacity-100');
if (isCorrectX && isCorrectY) {
feedbackDiv.textContent = 'Correct! Fantastic job!';
feedbackDiv.className = 'mt-8 p-4 text-center text-lg font-semibold rounded-lg bg-emerald-700/50 border border-emerald-600 text-emerald-100 transition-opacity duration-300 ease-in-out opacity-100';
createConfetti();
} else {
feedbackDiv.innerHTML = `Incorrect. The correct CoM is X = <span class="text-secondary">${correctCoM.x.toFixed(2)}</span>, Y = <span class="text-secondary">${correctCoM.y.toFixed(2)}</span>. Keep practicing!`;
feedbackDiv.className = 'mt-8 p-4 text-center text-lg font-semibold rounded-lg bg-red-700/50 border border-red-600 text-red-100 transition-opacity duration-300 ease-in-out opacity-100';
}
});
// Initial MathJax rendering (important for dynamic content)
MathJax.typesetPromise();
</script>
</body>
</html>