Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
67 lines (54 loc) · 2.17 KB
/
Copy pathhelpers.js
File metadata and controls
67 lines (54 loc) · 2.17 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
export const animate = (drawFunc) => {
let previousTimestamp = false;
const drawFuncContainer = (timestamp) => {
// Prevent deltaTime from producing huge values when e.g. user switches
// tabs and then switches back. 20 represents the number of milliseconds
// between frames when game is running at 50fps.
const deltaTime = Math.min(
20,
previousTimestamp
? timestamp - previousTimestamp
: performance.now() - timestamp
);
drawFunc(deltaTime);
window.requestAnimationFrame(drawFuncContainer);
previousTimestamp = timestamp;
};
window.requestAnimationFrame(drawFuncContainer);
};
export const progress = (start, end, current) =>
(current - start) / (end - start);
export const clampedProgress = (start, end, current) =>
Math.max(0, Math.min(1, (current - start) / (end - start)));
export const transition = (start, end, progress, easingFunc) => {
const easedProgress = easingFunc ? easingFunc(progress) : progress;
return start + Math.sign(end - start) * Math.abs(end - start) * easedProgress;
};
export const randomBool = (probability = 0.5) => Math.random() >= probability;
export const randomBetween = (min, max) => Math.random() * (max - min) + min;
export const degToRag = (degree) => (degree * Math.PI) / 180;
export const getHeadingInRadsFromTwoPoints = (a, b) =>
Math.atan2(a.y - b.y, a.x - b.x);
export const getVelocityFromSpeedAndHeading = (speed, headingInRads) => ({
x: speed * Math.cos(headingInRads),
y: speed * Math.sin(headingInRads),
});
export const getSpeedFromVelocity = ({ x, y }) => Math.sqrt(x * x + y * y);
export const findBallAtPoint = (balls, { x, y }) =>
balls.find((ball) => {
const dx = x - ball.getPosition().x;
const dy = y - ball.getPosition().y;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance < ball.getRadius();
});
export const getBoundedPosition = (canvasManager, { x, y }, padding = 16) => {
const paddedBoundedX = Math.min(
canvasManager.getWidth() - padding,
Math.max(padding, x)
);
const paddedBoundedY = Math.min(
canvasManager.getHeight() - padding,
Math.max(padding, y)
);
return { x: paddedBoundedX, y: paddedBoundedY };
};