tsparticles
Version:
Easily create highly customizable particle, confetti and fireworks 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.
97 lines (96 loc) • 2.38 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vector = void 0;
class Vector {
constructor(x, y) {
let defX, defY;
if (y === undefined) {
if (typeof x === "number") {
throw new Error("tsParticles - Vector not initialized correctly");
}
const coords = x;
[defX, defY] = [coords.x, coords.y];
}
else {
[defX, defY] = [x, y];
}
this.x = defX;
this.y = defY;
}
static clone(source) {
return Vector.create(source.x, source.y);
}
static create(x, y) {
return new Vector(x, y);
}
static get origin() {
return Vector.create(0, 0);
}
get angle() {
return Math.atan2(this.y, this.x);
}
set angle(angle) {
this.updateFromAngle(angle, this.length);
}
get length() {
return Math.sqrt(this.x ** 2 + this.y ** 2);
}
set length(length) {
this.updateFromAngle(this.angle, length);
}
add(v) {
return Vector.create(this.x + v.x, this.y + v.y);
}
addTo(v) {
this.x += v.x;
this.y += v.y;
}
sub(v) {
return Vector.create(this.x - v.x, this.y - v.y);
}
subFrom(v) {
this.x -= v.x;
this.y -= v.y;
}
mult(n) {
return Vector.create(this.x * n, this.y * n);
}
multTo(n) {
this.x *= n;
this.y *= n;
}
div(n) {
return Vector.create(this.x / n, this.y / n);
}
divTo(n) {
this.x /= n;
this.y /= n;
}
distanceTo(v) {
return this.sub(v).length;
}
getLengthSq() {
return this.x ** 2 + this.y ** 2;
}
distanceToSq(v) {
return this.sub(v).getLengthSq();
}
manhattanDistanceTo(v) {
return Math.abs(v.x - this.x) + Math.abs(v.y - this.y);
}
copy() {
return Vector.clone(this);
}
setTo(velocity) {
this.x = velocity.x;
this.y = velocity.y;
}
rotate(angle) {
return Vector.create(this.x * Math.cos(angle) - this.y * Math.sin(angle), this.x * Math.sin(angle) + this.y * Math.cos(angle));
}
updateFromAngle(angle, length) {
this.x = Math.cos(angle) * length;
this.y = Math.sin(angle) * length;
}
}
exports.Vector = Vector;