UNPKG

@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.

112 lines (111 loc) 2.98 kB
import { inverseFactorNumerator, none, originPoint, squareExp } from "./Constants.js"; function getZ(source) { return "z" in source ? source.z : originPoint.z; } export class Vector3d { x; y; z; constructor(x = originPoint.x, y = originPoint.y, z = originPoint.z) { this.x = x; this.y = y; this.z = z; } static get origin() { return Vector3d.create(originPoint.x, originPoint.y, originPoint.z); } 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, getZ(source)); } static create(x, y, z) { if (typeof x === "number") { return new Vector3d(x, y ?? originPoint.y, z ?? originPoint.z); } return new Vector3d(x.x, x.y, getZ(x)); } add(v) { return Vector3d.create(this.x + v.x, this.y + v.y, this.z + getZ(v)); } addTo(v) { this.x += v.x; this.y += v.y; this.z += getZ(v); } copy() { return Vector3d.clone(this); } 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 ** squareExp + this.y ** squareExp; } 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 != none) { this.multTo(inverseFactorNumerator / 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), originPoint.z); } setTo(c) { this.x = c.x; this.y = c.y; this.z = getZ(c); } sub(v) { return Vector3d.create(this.x - v.x, this.y - v.y, this.z - getZ(v)); } subFrom(v) { this.x -= v.x; this.y -= v.y; this.z -= getZ(v); } #updateFromAngle(angle, length) { this.x = Math.cos(angle) * length; this.y = Math.sin(angle) * length; } } export class Vector extends Vector3d { constructor(x = originPoint.x, y = originPoint.y) { super(x, y, originPoint.z); } static get origin() { return Vector.create(originPoint.x, originPoint.y); } static clone(source) { return Vector.create(source.x, source.y); } static create(x, y) { if (typeof x === "number") { return new Vector(x, y ?? originPoint.y); } return new Vector(x.x, x.y); } }