o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
Write a pendulum simulator in HTML canvas without external assets
- User
Write a pendulum simulator in HTML canvas without external assets
- Assistant
Write a pendulum simulator in HTML canvas without external assets
Sure! Below is a simple pendulum simulator implemented using HTML5 Canvas and JavaScript. It simulates the motion of a pendulum based on the equations of motion and animates it on the canvas.
HTML and JavaScript Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pendulum Simulator</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #f0f0f0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="pendulumCanvas"></canvas>
<script>
// Get the canvas and context
const canvas = document.getElementById('pendulumCanvas');
const ctx = canvas.getContext('2d');
// Resize the canvas to fill browser window dynamically
window.addEventListener('resize', resizeCanvas, false);
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
// Pendulum properties
const originX = canvas.width / 2;
const originY = 100;
let length = 300; // Length of the pendulum
let angle = Math.PI / 4; // Initial angle (from vertical)
let angleVelocity = 0.0;
let angleAcceleration = 0.0;
const gravity = 0.8;
function drawPendulum() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Calculate position of the pendulum bob
const bobX = originX + length * Math.sin(angle);
const bobY = originY + length * Math.cos(angle);
// Draw the rod
ctx.beginPath();
ctx.moveTo(originX, originY);
ctx.lineTo(bobX, bobY);
ctx.lineWidth = 2;
ctx.strokeStyle = '#000';
ctx.stroke();
// Draw the bob
ctx.beginPath();
ctx.arc(bobX, bobY, 20, 0, 2 * Math.PI);
ctx.fillStyle = '#007acc';
ctx.fill();
ctx.strokeStyle = '#000';
ctx.stroke();
}
function updatePendulum() {
// Equation of motion for pendulum (without damping)
angleAcceleration = (-gravity / length) * Math.sin(angle);
// Update the angular velocity and angle
angleVelocity += angleAcceleration;
angle += angleVelocity;
// Damping effect (uncomment to add damping)
// angleVelocity *= 0.99;
}
function animate() {
updatePendulum();
drawPendulum();
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
Explanation:
Canvas Setup:
ctx is obtained using getContext('2d').Pendulum Properties:
originX and originY represent the pivot point of the pendulum.length is the length of the pendulum rod.angle is the current angle from the vertical (in radians).angleVelocity and angleAcceleration are used to calculate the motion.gravity simulates the acceleration due to gravity.Drawing the Pendulum:
drawPendulum function calculates the position of the pendulum bob using trigonometry.Updating the Pendulum:
updatePendulum function uses the physics of a simple pendulum to calculate the angular acceleration.
angleAcceleration = (-gravity / length) * Math.sin(angle);angleVelocity *= 0.99; would add damping, causing the pendulum to slow down over time.Animation Loop:
animate function updates the pendulum's state and redraws it.requestAnimationFrame(animate); is used to create a smooth animation loop.How to Run the Simulator:
Save the Code:
.html extension, for example, pendulum.html.Open in Browser:
Interact:
Customization:
Adjusting Pendulum Properties:
length, angle, and gravity variables to change the pendulum's behavior.angleVelocity *= 0.99; in the updatePendulum function.Styling:
drawPendulum function to customize the appearance.Note:
Enjoy experimenting with the pendulum simulator!
Not yet included in a published release.