UNPKG

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.

91 lines (90 loc) 2.26 kB
export 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); } get angle() { return Math.atan2(this.y, this.x); } set angle(angle) { this.updateFromAngle(angle, this.length); } get length() { return Math.sqrt(Math.pow(this.x, 2) + Math.pow(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 Math.pow(this.x, 2) + Math.pow(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; } } Vector.origin = Vector.create(0, 0);