tsparticles-engine
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.
119 lines (118 loc) • 3.7 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./Constants", "../../Utils/Utils"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Vector3d = void 0;
const Constants_1 = require("./Constants");
const Utils_1 = require("../../Utils/Utils");
class Vector3d {
constructor(xOrCoords, y, z) {
this._updateFromAngle = (angle, length) => {
this.x = Math.cos(angle) * length;
this.y = Math.sin(angle) * length;
};
if (!(0, Utils_1.isNumber)(xOrCoords) && xOrCoords) {
this.x = xOrCoords.x;
this.y = xOrCoords.y;
const coords3d = xOrCoords;
this.z = coords3d.z ? coords3d.z : 0;
}
else if (xOrCoords !== undefined && y !== undefined) {
this.x = xOrCoords;
this.y = y;
this.z = z ?? 0;
}
else {
throw new Error(`${Constants_1.errorPrefix} Vector3d not initialized correctly`);
}
}
static get origin() {
return Vector3d.create(0, 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.getLengthSq());
}
set length(length) {
this._updateFromAngle(this.angle, length);
}
static clone(source) {
return Vector3d.create(source.x, source.y, source.z);
}
static create(x, y, z) {
return new Vector3d(x, y, z);
}
add(v) {
return Vector3d.create(this.x + v.x, this.y + v.y, this.z + v.z);
}
addTo(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
}
copy() {
return Vector3d.clone(this);
}
distanceTo(v) {
return this.sub(v).length;
}
distanceToSq(v) {
return this.sub(v).getLengthSq();
}
div(n) {
return Vector3d.create(this.x / n, this.y / n, this.z / n);
}
divTo(n) {
this.x /= n;
this.y /= n;
this.z /= n;
}
getLengthSq() {
return this.x ** 2 + this.y ** 2;
}
mult(n) {
return Vector3d.create(this.x * n, this.y * n, this.z * n);
}
multTo(n) {
this.x *= n;
this.y *= n;
this.z *= n;
}
normalize() {
const length = this.length;
if (length != 0) {
this.multTo(1.0 / length);
}
}
rotate(angle) {
return Vector3d.create(this.x * Math.cos(angle) - this.y * Math.sin(angle), this.x * Math.sin(angle) + this.y * Math.cos(angle), 0);
}
setTo(c) {
this.x = c.x;
this.y = c.y;
const v3d = c;
this.z = v3d.z ? v3d.z : 0;
}
sub(v) {
return Vector3d.create(this.x - v.x, this.y - v.y, this.z - v.z);
}
subFrom(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
}
}
exports.Vector3d = Vector3d;
});