UNPKG

@tsparticles/updater-out-modes

Version:

tsParticles particles out modes updater

35 lines (34 loc) 1.4 kB
import { OutMode, calculateBounds, } from "@tsparticles/engine"; import { bounceHorizontal, bounceVertical } from "./Utils.js"; export class BounceOutMode { modes; #container; #particleBouncePlugins; constructor(container) { this.#container = container; this.modes = [ OutMode.bounce, OutMode.split, ]; this.#particleBouncePlugins = container.plugins.filter(p => p.particleBounce !== undefined); } update(particle, direction, delta, outMode) { if (!this.modes.includes(outMode)) { return; } const container = this.#container; let handled = false; for (const plugin of this.#particleBouncePlugins) { handled = plugin.particleBounce?.(particle, delta, direction) ?? false; if (handled) { break; } } if (handled) { return; } const pos = particle.getPosition(), offset = particle.offset, size = particle.getRadius(), bounds = calculateBounds(pos, size), canvasSize = container.canvas.size, outOfCanvas = !particle.isInsideCanvasForOutMode(outMode, direction); bounceHorizontal({ particle, outMode, direction, bounds, canvasSize, offset, outOfCanvas, size }); bounceVertical({ particle, outMode, direction, bounds, canvasSize, offset, outOfCanvas, size }); } }