-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjectileMotion.js
More file actions
88 lines (80 loc) · 1.92 KB
/
Copy pathProjectileMotion.js
File metadata and controls
88 lines (80 loc) · 1.92 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
const g = 9.81;
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");
const canvasWidth = canvas.width;
const canvasHeight = canvas.height;
const vel = document.getElementById("vel");
const ang = document.getElementById("ang");
let Angle,
timer,
velocity,
x = 40,
y = 460,
radius = 5;
let frameCount = 0;
let v0x = velocity * Math.cos((Angle * Math.PI) / 180);
let v0y = velocity * Math.sin((Angle * Math.PI) / 180);
let startX = x;
let startY = y;
const make_base = () => {
base_image = new Image(10, 10);
base_image.src = "pall.png";
base_image.onload = function () {
ctx.drawImage(base_image, 0, 460);
};
};
make_base();
const init = () => {
velocity = vel.value;
Angle = ang.value;
frameCount = 0;
v0x = velocity * Math.cos((Angle * Math.PI) / 180);
v0y = velocity * Math.sin((Angle * Math.PI) / 180);
};
const draw = () => {
ctx.save();
ctx.restore();
if (y < canvasHeight - radius + 1 && x < canvasWidth - radius + 1) {
y = startY - (v0y * frameCount - (1 / 2) * g * Math.pow(frameCount, 2));
x = startX + v0x * frameCount;
} else {
clearInterval(timer);
}
ctx.save();
ctx.beginPath();
ctx.fillStyle = "red";
ctx.arc(x, y, radius, 0, Math.PI * 2, true);
ctx.fill();
ctx.stroke();
ctx.closePath();
ctx.restore();
frameCount += 0.15;
};
const checkS = () => {
if (isNaN(vel.value) || vel.value === "") {
alert("velocity: " + vel.value + "not a number");
return 0;
}
if (isNaN(ang.value) || ang.value === "") {
alert("Angle: " + ang.value + "not a number");
return 0;
}
return 1;
};
const reset = () => {
clearInterval(timer);
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
make_base();
x = 40;
y = 460;
startX = x;
startY = y;
};
const start = () => {
reset();
init();
if (checkS()) timer = setInterval(draw, 10);
};
const checkNumber = (val) => {
if (isNaN(val)) alert("Please enter a number.");
};