Repository navigation
Expand file tree
/
Copy pathtrajectory.js
More file actions
52 lines (46 loc) · 1.5 KB
/
Copy pathtrajectory.js
File metadata and controls
52 lines (46 loc) · 1.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
export const drawTrajectory = (
canvasManager,
currentPosition,
currentVelocity,
gravity
) => {
const CTX = canvasManager.getContext();
// Iterate on ballistic properties until hitting the ground, and push these
// points into an array to draw later
const generateTrajectoryPoints = () => {
let projectedXPosition = currentPosition.x;
let projectedYPosition = currentPosition.y;
let projectedYVelocity = currentVelocity.y;
const segments = [];
while (
projectedYPosition <= canvasManager.getHeight() + 100 &&
projectedYPosition >= -100 &&
projectedXPosition <= canvasManager.getWidth() + 100 &&
projectedXPosition >= -100
) {
segments.push({ x: projectedXPosition, y: projectedYPosition });
projectedYPosition += projectedYVelocity;
projectedXPosition += currentVelocity.x;
projectedYVelocity += gravity;
}
return segments;
};
const segments = generateTrajectoryPoints();
if (segments.length > 10) {
const gradientLength = Math.floor(segments.length / 4);
CTX.save();
segments.forEach(({ x, y }, index) => {
if (segments.length > index + 1) {
const nextSegment = segments[index + 1];
CTX.globalAlpha = index % 2 ? 0 : 0.6;
CTX.beginPath();
CTX.moveTo(x, y);
CTX.lineTo(nextSegment.x, nextSegment.y);
CTX.lineWidth = 1;
CTX.strokeStyle = `rgba(255, 255, 255, ${index / gradientLength})`;
CTX.stroke();
}
});
CTX.restore();
}
};