tsparticles
Version:
Easily create highly customizable particle animations and use them as animated backgrounds for your website. Ready to use components available also for React, Vue.js (2.x and 3.x), Angular, Svelte, jQuery, Preact, Riot.js, Inferno.
103 lines (102 loc) • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberUtils = void 0;
const Directions_1 = require("../Enums/Directions");
const Vector_1 = require("../Core/Particle/Vector");
class NumberUtils {
static clamp(num, min, max) {
return Math.min(Math.max(num, min), max);
}
static mix(comp1, comp2, weight1, weight2) {
return Math.floor((comp1 * weight1 + comp2 * weight2) / (weight1 + weight2));
}
static randomInRange(r) {
const max = NumberUtils.getRangeMax(r);
let min = NumberUtils.getRangeMin(r);
if (max === min) {
min = 0;
}
return Math.random() * (max - min) + min;
}
static getRangeValue(value) {
return typeof value === "number" ? value : NumberUtils.randomInRange(value);
}
static getRangeMin(value) {
return typeof value === "number" ? value : value.min;
}
static getRangeMax(value) {
return typeof value === "number" ? value : value.max;
}
static setRangeValue(source, value) {
if (source === value || (value === undefined && typeof source === "number")) {
return source;
}
const min = NumberUtils.getRangeMin(source), max = NumberUtils.getRangeMax(source);
return value !== undefined
? {
min: Math.min(min, value),
max: Math.max(max, value),
}
: NumberUtils.setRangeValue(min, max);
}
static getValue(options) {
const random = options.random;
const { enable, minimumValue } = typeof random === "boolean" ? { enable: random, minimumValue: 0 } : random;
return enable
? NumberUtils.getRangeValue(NumberUtils.setRangeValue(options.value, minimumValue))
: NumberUtils.getRangeValue(options.value);
}
static getDistances(pointA, pointB) {
const dx = pointA.x - pointB.x;
const dy = pointA.y - pointB.y;
return { dx: dx, dy: dy, distance: Math.sqrt(dx * dx + dy * dy) };
}
static getDistance(pointA, pointB) {
return NumberUtils.getDistances(pointA, pointB).distance;
}
static getParticleBaseVelocity(direction) {
const baseVelocity = Vector_1.Vector.origin;
baseVelocity.length = 1;
switch (direction) {
case Directions_1.MoveDirection.top:
baseVelocity.angle = -Math.PI / 2;
break;
case Directions_1.MoveDirection.topRight:
baseVelocity.angle = -Math.PI / 4;
break;
case Directions_1.MoveDirection.right:
baseVelocity.angle = 0;
break;
case Directions_1.MoveDirection.bottomRight:
baseVelocity.angle = Math.PI / 4;
break;
case Directions_1.MoveDirection.bottom:
baseVelocity.angle = Math.PI / 2;
break;
case Directions_1.MoveDirection.bottomLeft:
baseVelocity.angle = (3 * Math.PI) / 4;
break;
case Directions_1.MoveDirection.left:
baseVelocity.angle = Math.PI;
break;
case Directions_1.MoveDirection.topLeft:
baseVelocity.angle = (-3 * Math.PI) / 4;
break;
case Directions_1.MoveDirection.none:
default:
baseVelocity.angle = Math.random() * Math.PI * 2;
break;
}
return baseVelocity;
}
static rotateVelocity(velocity, angle) {
return {
horizontal: velocity.horizontal * Math.cos(angle) - velocity.vertical * Math.sin(angle),
vertical: velocity.horizontal * Math.sin(angle) + velocity.vertical * Math.cos(angle),
};
}
static collisionVelocity(v1, v2, m1, m2) {
return Vector_1.Vector.create((v1.x * (m1 - m2)) / (m1 + m2) + (v2.x * 2 * m2) / (m1 + m2), v1.y);
}
}
exports.NumberUtils = NumberUtils;