UNPKG

4.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.calcEasing = exports.collisionVelocity = exports.getParticleBaseVelocity = exports.getParticleDirectionAngle = exports.getDistance = exports.getDistances = exports.getValue = exports.setRangeValue = exports.getRangeMax = exports.getRangeMin = exports.getRangeValue = exports.randomInRange = exports.mix = exports.clamp = void 0;
4const Enums_1 = require("../Enums");
5const Vector_1 = require("../Core/Particle/Vector");
6function clamp(num, min, max) {
7 return Math.min(Math.max(num, min), max);
8}
9exports.clamp = clamp;
10function mix(comp1, comp2, weight1, weight2) {
11 return Math.floor((comp1 * weight1 + comp2 * weight2) / (weight1 + weight2));
12}
13exports.mix = mix;
14function randomInRange(r) {
15 const max = getRangeMax(r);
16 let min = getRangeMin(r);
17 if (max === min) {
18 min = 0;
19 }
20 return Math.random() * (max - min) + min;
21}
22exports.randomInRange = randomInRange;
23function getRangeValue(value) {
24 return typeof value === "number" ? value : randomInRange(value);
25}
26exports.getRangeValue = getRangeValue;
27function getRangeMin(value) {
28 return typeof value === "number" ? value : value.min;
29}
30exports.getRangeMin = getRangeMin;
31function getRangeMax(value) {
32 return typeof value === "number" ? value : value.max;
33}
34exports.getRangeMax = getRangeMax;
35function setRangeValue(source, value) {
36 if (source === value || (value === undefined && typeof source === "number")) {
37 return source;
38 }
39 const min = getRangeMin(source), max = getRangeMax(source);
40 return value !== undefined
41 ? {
42 min: Math.min(min, value),
43 max: Math.max(max, value),
44 }
45 : setRangeValue(min, max);
46}
47exports.setRangeValue = setRangeValue;
48function getValue(options) {
49 const random = options.random;
50 const { enable, minimumValue } = typeof random === "boolean" ? { enable: random, minimumValue: 0 } : random;
51 return enable ? getRangeValue(setRangeValue(options.value, minimumValue)) : getRangeValue(options.value);
52}
53exports.getValue = getValue;
54function getDistances(pointA, pointB) {
55 const dx = pointA.x - pointB.x;
56 const dy = pointA.y - pointB.y;
57 return { dx: dx, dy: dy, distance: Math.sqrt(dx * dx + dy * dy) };
58}
59exports.getDistances = getDistances;
60function getDistance(pointA, pointB) {
61 return getDistances(pointA, pointB).distance;
62}
63exports.getDistance = getDistance;
64function getParticleDirectionAngle(direction) {
65 if (typeof direction === "number") {
66 return (direction * Math.PI) / 180;
67 }
68 else {
69 switch (direction) {
70 case Enums_1.MoveDirection.top:
71 return -Math.PI / 2;
72 case Enums_1.MoveDirection.topRight:
73 return -Math.PI / 4;
74 case Enums_1.MoveDirection.right:
75 return 0;
76 case Enums_1.MoveDirection.bottomRight:
77 return Math.PI / 4;
78 case Enums_1.MoveDirection.bottom:
79 return Math.PI / 2;
80 case Enums_1.MoveDirection.bottomLeft:
81 return (3 * Math.PI) / 4;
82 case Enums_1.MoveDirection.left:
83 return Math.PI;
84 case Enums_1.MoveDirection.topLeft:
85 return (-3 * Math.PI) / 4;
86 case Enums_1.MoveDirection.none:
87 default:
88 return Math.random() * Math.PI * 2;
89 }
90 }
91}
92exports.getParticleDirectionAngle = getParticleDirectionAngle;
93function getParticleBaseVelocity(direction) {
94 const baseVelocity = Vector_1.Vector.origin;
95 baseVelocity.length = 1;
96 baseVelocity.angle = direction;
97 return baseVelocity;
98}
99exports.getParticleBaseVelocity = getParticleBaseVelocity;
100function collisionVelocity(v1, v2, m1, m2) {
101 return Vector_1.Vector.create((v1.x * (m1 - m2)) / (m1 + m2) + (v2.x * 2 * m2) / (m1 + m2), v1.y);
102}
103exports.collisionVelocity = collisionVelocity;
104function calcEasing(value, type) {
105 switch (type) {
106 case Enums_1.EasingType.easeOutQuad:
107 return 1 - (1 - value) ** 2;
108 case Enums_1.EasingType.easeOutCubic:
109 return 1 - (1 - value) ** 3;
110 case Enums_1.EasingType.easeOutQuart:
111 return 1 - (1 - value) ** 4;
112 case Enums_1.EasingType.easeOutQuint:
113 return 1 - (1 - value) ** 5;
114 case Enums_1.EasingType.easeOutExpo:
115 return value === 1 ? 1 : 1 - Math.pow(2, -10 * value);
116 case Enums_1.EasingType.easeOutSine:
117 return Math.sin((value * Math.PI) / 2);
118 case Enums_1.EasingType.easeOutBack: {
119 const c1 = 1.70158;
120 const c3 = c1 + 1;
121 return 1 + c3 * Math.pow(value - 1, 3) + c1 * Math.pow(value - 1, 2);
122 }
123 case Enums_1.EasingType.easeOutCirc:
124 return Math.sqrt(1 - Math.pow(value - 1, 2));
125 default:
126 return value;
127 }
128}
129exports.calcEasing = calcEasing;