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.

76 lines (75 loc) 2.64 kB
import { RangeType } from "../../Enums/RangeType.js"; import { checkDistance } from "../../Utils/MathUtils.js"; import { squareExp } from "./Constants.js"; export class BaseRange { position; type; constructor(x, y, type) { this.position = { x: x, y: y, }; this.type = type; } _resetPosition(x, y) { this.position.x = x; this.position.y = y; } } export class Circle extends BaseRange { radius; constructor(x, y, radius) { super(x, y, RangeType.circle); this.radius = radius; } contains(point) { return checkDistance(point, this.position, this.radius); } intersects(range) { const pos1 = this.position, pos2 = range.position, r = this.radius, dx = Math.abs(pos2.x - pos1.x), dy = Math.abs(pos2.y - pos1.y); if (range instanceof Circle || range.type === RangeType.circle) { const circleRange = range, rSum = r + circleRange.radius, dist = Math.hypot(dx, dy); return rSum > dist; } else if (range instanceof Rectangle || range.type === RangeType.rectangle) { const rectRange = range, { width, height } = rectRange.size, edges = Math.pow(dx - width, squareExp) + Math.pow(dy - height, squareExp); return edges <= r ** squareExp || (dx <= r + width && dy <= r + height) || dx <= width || dy <= height; } return false; } reset(x, y, radius) { this._resetPosition(x, y); this.radius = radius; return this; } } export class Rectangle extends BaseRange { size; constructor(x, y, width, height) { super(x, y, RangeType.rectangle); this.size = { height: height, width: width, }; } contains(point) { const w = this.size.width, h = this.size.height, pos = this.position; return point.x >= pos.x && point.x <= pos.x + w && point.y >= pos.y && point.y <= pos.y + h; } intersects(range) { if (range instanceof Circle) { return range.intersects(this); } if (!(range instanceof Rectangle)) { return false; } const w = this.size.width, h = this.size.height, pos1 = this.position, pos2 = range.position, size2 = range.size, w2 = size2.width, h2 = size2.height; return pos2.x < pos1.x + w && pos2.x + w2 > pos1.x && pos2.y < pos1.y + h && pos2.y + h2 > pos1.y; } reset(x, y, width, height) { this._resetPosition(x, y); this.size.width = width; this.size.height = height; return this; } }