-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Projectile Motion Simulation</title> | ||
<style> | ||
input { | ||
width: 1200px; | ||
} | ||
div { | ||
position: relative; | ||
} | ||
canvas { | ||
position: absolute; | ||
top: 0px; | ||
left: 3px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
Angle:<br> | ||
<input id="angle" type="range" min="0.07" max="1.5" value="1" step="0.01" autocomplete="off" /> <br> | ||
Speed:<br> | ||
<input id="speed" type="range" min="1" max="12" value="10" step="0.01" autocomplete="off" /> <br> | ||
<div> | ||
<canvas id="cvs2" width=1200 height=800></canvas> | ||
<canvas id="cvs1" width=1200 height=800></canvas> | ||
</div> | ||
<script> | ||
const angle = document.getElementById('angle'); | ||
const speed = document.getElementById('speed'); | ||
// For dots, clear every frame | ||
const cvs1 = document.getElementById('cvs1'); | ||
const ctx1 = cvs1.getContext('2d'); | ||
// For paths, clear on reset | ||
const cvs2 = document.getElementById('cvs2'); | ||
const ctx2 = cvs2.getContext('2d'); | ||
|
||
const startPosition = [0, 0]; | ||
let position; | ||
let velocity; | ||
|
||
function drawCircle(pos, radius) { | ||
ctx1.beginPath(); | ||
ctx1.arc(pos[0], 800 - pos[1], radius, 0, 2 * Math.PI, false); | ||
ctx1.fill(); | ||
ctx1.stroke(); | ||
} | ||
|
||
function reset() { | ||
position = startPosition; | ||
velocity = [speed.value * Math.cos(angle.value), speed.value * Math.sin(angle.value)]; | ||
|
||
ctx2.fillRect(0, 0, cvs2.width, cvs2.height); | ||
} | ||
angle.addEventListener('input', reset); | ||
speed.addEventListener('input', reset); | ||
|
||
|
||
function updatePos() { | ||
// Ball hit the ground | ||
if (position[0] < 0) return; | ||
const oldPos = position; | ||
|
||
velocity[1] -= 0.098; | ||
position = [position[0] + velocity[0], position[1] + velocity[1]]; | ||
|
||
// Draw | ||
ctx2.strokeStyle = 'red'; | ||
ctx2.beginPath(); | ||
ctx2.moveTo(oldPos[0], 800 - oldPos[1]); | ||
ctx2.lineTo(position[0], 800 - position[1]); | ||
ctx2.stroke(); | ||
} | ||
|
||
function step() { | ||
ctx1.clearRect(0, 0, cvs1.width, cvs1.height); | ||
|
||
updatePos(); | ||
|
||
ctx1.fillStyle = 'white'; | ||
drawCircle(position, 5); | ||
|
||
window.requestAnimationFrame(step); | ||
} | ||
|
||
reset(); | ||
window.requestAnimationFrame(step); | ||
</script> | ||
</body> | ||
</html> |