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.

81 lines (80 loc) • 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Rectangle = exports.Circle = exports.BaseRange = void 0; const RangeType_js_1 = require("../../Enums/RangeType.js"); const MathUtils_js_1 = require("../../Utils/MathUtils.js"); 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; } } exports.BaseRange = BaseRange; class Circle extends BaseRange { radius; constructor(x, y, radius) { super(x, y, RangeType_js_1.RangeType.circle); this.radius = radius; } contains(point) { return (0, MathUtils_js_1.checkDistance)(point, this.position, this.radius); } intersects(range) { const pos1 = this.position, pos2 = range.position, r = this.radius, dx = pos2.x - pos1.x, dy = pos2.y - pos1.y; if (range instanceof Circle || range.type === RangeType_js_1.RangeType.circle) { const circleRange = range, rSum = r + circleRange.radius; return rSum > 0 && dx * dx + dy * dy < rSum * rSum; } else if (range instanceof Rectangle || range.type === RangeType_js_1.RangeType.rectangle) { const rectRange = range, { width, height } = rectRange.size, absX = Math.abs(dx), absY = Math.abs(dy), xEdge = absX - width, yEdge = absY - height, edges = xEdge * xEdge + yEdge * yEdge; return edges <= r * r || (absX <= r + width && absY <= r + height) || absX <= width || absY <= height; } return false; } reset(x, y, radius) { this._resetPosition(x, y); this.radius = radius; return this; } } exports.Circle = Circle; class Rectangle extends BaseRange { size; constructor(x, y, width, height) { super(x, y, RangeType_js_1.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; } } exports.Rectangle = Rectangle;