-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpendulum.html
87 lines (74 loc) · 2.28 KB
/
pendulum.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pendulum Simulation</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}
canvas {
background-color: #ffffff;
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="pendulumCanvas" width="600" height="400"></canvas>
<script>
const canvas = document.getElementById("pendulumCanvas");
const ctx = canvas.getContext("2d");
const width = canvas.width;
const height = canvas.height;
// Pendulum parameters
const originX = width / 2;
const originY = 50;
let length = 200;
let angle = Math.PI / 4; // Initial angle (45 degrees)
let angleVelocity = 0; // Initial angular velocity
let angleAcceleration = 0; // Initial angular acceleration
const damping = 0.995; // Damping factor
const gravity = 0.4; // Gravitational constant
function drawPendulum() {
ctx.clearRect(0, 0, width, height);
// Calculate position of the pendulum bob
const bobX = originX + length * Math.sin(angle);
const bobY = originY + length * Math.cos(angle);
// Draw the pendulum rod
ctx.beginPath();
ctx.moveTo(originX, originY);
ctx.lineTo(bobX, bobY);
ctx.strokeStyle = "#000";
ctx.lineWidth = 2;
ctx.stroke();
// Draw the pendulum bob
ctx.beginPath();
ctx.arc(bobX, bobY, 20, 0, Math.PI * 2);
ctx.fillStyle = "#000";
ctx.fill();
ctx.stroke();
}
function updatePendulum() {
// Calculate angular acceleration
angleAcceleration = (-gravity / length) * Math.sin(angle);
// Update angular velocity and angle
angleVelocity += angleAcceleration;
angleVelocity *= damping; // Apply damping
angle += angleVelocity;
drawPendulum();
}
function animate() {
updatePendulum();
requestAnimationFrame(animate);
}
// Start the animation
animate();
</script>
</body>
</html>