UNPKG

10 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.rectBounce = exports.circleBounce = exports.circleBounceDataFromParticle = exports.divMode = exports.singleDivModeExecute = exports.divModeExecute = exports.isDivModeEnabled = exports.deepExtend = exports.calculateBounds = exports.areBoundsInside = exports.isPointInside = exports.itemFromArray = exports.arrayRandomIndex = exports.loadFont = exports.isInArray = exports.cancelAnimation = exports.animate = exports.isSsr = void 0;
4const Enums_1 = require("../Enums");
5const NumberUtils_1 = require("./NumberUtils");
6const Vector_1 = require("../Core/Particle/Vector");
7function rectSideBounce(pSide, pOtherSide, rectSide, rectOtherSide, velocity, factor) {
8 const res = { bounced: false };
9 if (pOtherSide.min >= rectOtherSide.min &&
10 pOtherSide.min <= rectOtherSide.max &&
11 pOtherSide.max >= rectOtherSide.min &&
12 pOtherSide.max <= rectOtherSide.max) {
13 if ((pSide.max >= rectSide.min && pSide.max <= (rectSide.max + rectSide.min) / 2 && velocity > 0) ||
14 (pSide.min <= rectSide.max && pSide.min > (rectSide.max + rectSide.min) / 2 && velocity < 0)) {
15 res.velocity = velocity * -factor;
16 res.bounced = true;
17 }
18 }
19 return res;
20}
21function checkSelector(element, selectors) {
22 if (selectors instanceof Array) {
23 for (const selector of selectors) {
24 if (element.matches(selector)) {
25 return true;
26 }
27 }
28 return false;
29 }
30 else {
31 return element.matches(selectors);
32 }
33}
34function isSsr() {
35 return typeof window === "undefined" || !window || typeof window.document === "undefined" || !window.document;
36}
37exports.isSsr = isSsr;
38function animate() {
39 return isSsr()
40 ? (callback) => setTimeout(callback)
41 : (callback) => (window.requestAnimationFrame ||
42 window.webkitRequestAnimationFrame ||
43 window.mozRequestAnimationFrame ||
44 window.oRequestAnimationFrame ||
45 window.msRequestAnimationFrame ||
46 window.setTimeout)(callback);
47}
48exports.animate = animate;
49function cancelAnimation() {
50 return isSsr()
51 ? (handle) => clearTimeout(handle)
52 : (handle) => (window.cancelAnimationFrame ||
53 window.webkitCancelRequestAnimationFrame ||
54 window.mozCancelRequestAnimationFrame ||
55 window.oCancelRequestAnimationFrame ||
56 window.msCancelRequestAnimationFrame ||
57 window.clearTimeout)(handle);
58}
59exports.cancelAnimation = cancelAnimation;
60function isInArray(value, array) {
61 return value === array || (array instanceof Array && array.indexOf(value) > -1);
62}
63exports.isInArray = isInArray;
64async function loadFont(character) {
65 var _a, _b;
66 try {
67 await document.fonts.load(`${(_a = character.weight) !== null && _a !== void 0 ? _a : "400"} 36px '${(_b = character.font) !== null && _b !== void 0 ? _b : "Verdana"}'`);
68 }
69 catch (_c) {
70 }
71}
72exports.loadFont = loadFont;
73function arrayRandomIndex(array) {
74 return Math.floor(Math.random() * array.length);
75}
76exports.arrayRandomIndex = arrayRandomIndex;
77function itemFromArray(array, index, useIndex = true) {
78 const fixedIndex = index !== undefined && useIndex ? index % array.length : arrayRandomIndex(array);
79 return array[fixedIndex];
80}
81exports.itemFromArray = itemFromArray;
82function isPointInside(point, size, radius, direction) {
83 return areBoundsInside(calculateBounds(point, radius !== null && radius !== void 0 ? radius : 0), size, direction);
84}
85exports.isPointInside = isPointInside;
86function areBoundsInside(bounds, size, direction) {
87 let inside = true;
88 if (!direction || direction === Enums_1.OutModeDirection.bottom) {
89 inside = bounds.top < size.height;
90 }
91 if (inside && (!direction || direction === Enums_1.OutModeDirection.left)) {
92 inside = bounds.right > 0;
93 }
94 if (inside && (!direction || direction === Enums_1.OutModeDirection.right)) {
95 inside = bounds.left < size.width;
96 }
97 if (inside && (!direction || direction === Enums_1.OutModeDirection.top)) {
98 inside = bounds.bottom > 0;
99 }
100 return inside;
101}
102exports.areBoundsInside = areBoundsInside;
103function calculateBounds(point, radius) {
104 return {
105 bottom: point.y + radius,
106 left: point.x - radius,
107 right: point.x + radius,
108 top: point.y - radius,
109 };
110}
111exports.calculateBounds = calculateBounds;
112function deepExtend(destination, ...sources) {
113 for (const source of sources) {
114 if (source === undefined || source === null) {
115 continue;
116 }
117 if (typeof source !== "object") {
118 destination = source;
119 continue;
120 }
121 const sourceIsArray = Array.isArray(source);
122 if (sourceIsArray && (typeof destination !== "object" || !destination || !Array.isArray(destination))) {
123 destination = [];
124 }
125 else if (!sourceIsArray && (typeof destination !== "object" || !destination || Array.isArray(destination))) {
126 destination = {};
127 }
128 for (const key in source) {
129 if (key === "__proto__") {
130 continue;
131 }
132 const sourceDict = source;
133 const value = sourceDict[key];
134 const isObject = typeof value === "object";
135 const destDict = destination;
136 destDict[key] =
137 isObject && Array.isArray(value)
138 ? value.map((v) => deepExtend(destDict[key], v))
139 : deepExtend(destDict[key], value);
140 }
141 }
142 return destination;
143}
144exports.deepExtend = deepExtend;
145function isDivModeEnabled(mode, divs) {
146 return divs instanceof Array ? !!divs.find((t) => t.enable && isInArray(mode, t.mode)) : isInArray(mode, divs.mode);
147}
148exports.isDivModeEnabled = isDivModeEnabled;
149function divModeExecute(mode, divs, callback) {
150 if (divs instanceof Array) {
151 for (const div of divs) {
152 const divMode = div.mode;
153 const divEnabled = div.enable;
154 if (divEnabled && isInArray(mode, divMode)) {
155 singleDivModeExecute(div, callback);
156 }
157 }
158 }
159 else {
160 const divMode = divs.mode;
161 const divEnabled = divs.enable;
162 if (divEnabled && isInArray(mode, divMode)) {
163 singleDivModeExecute(divs, callback);
164 }
165 }
166}
167exports.divModeExecute = divModeExecute;
168function singleDivModeExecute(div, callback) {
169 const selectors = div.selectors;
170 if (selectors instanceof Array) {
171 for (const selector of selectors) {
172 callback(selector, div);
173 }
174 }
175 else {
176 callback(selectors, div);
177 }
178}
179exports.singleDivModeExecute = singleDivModeExecute;
180function divMode(divs, element) {
181 if (!element || !divs) {
182 return;
183 }
184 if (divs instanceof Array) {
185 return divs.find((d) => checkSelector(element, d.selectors));
186 }
187 else if (checkSelector(element, divs.selectors)) {
188 return divs;
189 }
190}
191exports.divMode = divMode;
192function circleBounceDataFromParticle(p) {
193 return {
194 position: p.getPosition(),
195 radius: p.getRadius(),
196 mass: p.getMass(),
197 velocity: p.velocity,
198 factor: Vector_1.Vector.create((0, NumberUtils_1.getValue)(p.options.bounce.horizontal), (0, NumberUtils_1.getValue)(p.options.bounce.vertical)),
199 };
200}
201exports.circleBounceDataFromParticle = circleBounceDataFromParticle;
202function circleBounce(p1, p2) {
203 const xVelocityDiff = p1.velocity.x;
204 const yVelocityDiff = p1.velocity.y;
205 const pos1 = p1.position;
206 const pos2 = p2.position;
207 const xDist = pos2.x - pos1.x;
208 const yDist = pos2.y - pos1.y;
209 if (xVelocityDiff * xDist + yVelocityDiff * yDist >= 0) {
210 const angle = -Math.atan2(pos2.y - pos1.y, pos2.x - pos1.x);
211 const m1 = p1.mass;
212 const m2 = p2.mass;
213 const u1 = p1.velocity.rotate(angle);
214 const u2 = p2.velocity.rotate(angle);
215 const v1 = (0, NumberUtils_1.collisionVelocity)(u1, u2, m1, m2);
216 const v2 = (0, NumberUtils_1.collisionVelocity)(u2, u1, m1, m2);
217 const vFinal1 = v1.rotate(-angle);
218 const vFinal2 = v2.rotate(-angle);
219 p1.velocity.x = vFinal1.x * p1.factor.x;
220 p1.velocity.y = vFinal1.y * p1.factor.y;
221 p2.velocity.x = vFinal2.x * p2.factor.x;
222 p2.velocity.y = vFinal2.y * p2.factor.y;
223 }
224}
225exports.circleBounce = circleBounce;
226function rectBounce(particle, divBounds) {
227 const pPos = particle.getPosition();
228 const size = particle.getRadius();
229 const bounds = calculateBounds(pPos, size);
230 const resH = rectSideBounce({
231 min: bounds.left,
232 max: bounds.right,
233 }, {
234 min: bounds.top,
235 max: bounds.bottom,
236 }, {
237 min: divBounds.left,
238 max: divBounds.right,
239 }, {
240 min: divBounds.top,
241 max: divBounds.bottom,
242 }, particle.velocity.x, (0, NumberUtils_1.getValue)(particle.options.bounce.horizontal));
243 if (resH.bounced) {
244 if (resH.velocity !== undefined) {
245 particle.velocity.x = resH.velocity;
246 }
247 if (resH.position !== undefined) {
248 particle.position.x = resH.position;
249 }
250 }
251 const resV = rectSideBounce({
252 min: bounds.top,
253 max: bounds.bottom,
254 }, {
255 min: bounds.left,
256 max: bounds.right,
257 }, {
258 min: divBounds.top,
259 max: divBounds.bottom,
260 }, {
261 min: divBounds.left,
262 max: divBounds.right,
263 }, particle.velocity.y, (0, NumberUtils_1.getValue)(particle.options.bounce.vertical));
264 if (resV.bounced) {
265 if (resV.velocity !== undefined) {
266 particle.velocity.y = resV.velocity;
267 }
268 if (resV.position !== undefined) {
269 particle.position.y = resV.position;
270 }
271 }
272}
273exports.rectBounce = rectBounce;