UNPKG

@tsparticles/interaction-particles-attract

Version:

tsParticles attract particles interaction

114 lines (107 loc) 7.81 kB
(function(g){g.__tsParticlesInternals=g.__tsParticlesInternals||{};g.__tsParticlesInternals.bundles=g.__tsParticlesInternals.bundles||{};g.__tsParticlesInternals.effects=g.__tsParticlesInternals.effects||{};g.__tsParticlesInternals.engine=g.__tsParticlesInternals.engine||{};g.__tsParticlesInternals.interactions=g.__tsParticlesInternals.interactions||{};g.__tsParticlesInternals.palettes=g.__tsParticlesInternals.palettes||{};g.__tsParticlesInternals.paths=g.__tsParticlesInternals.paths||{};g.__tsParticlesInternals.plugins=g.__tsParticlesInternals.plugins||{};g.__tsParticlesInternals.plugins=g.__tsParticlesInternals.plugins||{};g.__tsParticlesInternals.plugins.emittersShapes=g.__tsParticlesInternals.plugins.emittersShapes||{};g.__tsParticlesInternals.presets=g.__tsParticlesInternals.presets||{};g.__tsParticlesInternals.shapes=g.__tsParticlesInternals.shapes||{};g.__tsParticlesInternals.updaters=g.__tsParticlesInternals.updaters||{};g.__tsParticlesInternals.utils=g.__tsParticlesInternals.utils||{};g.__tsParticlesInternals.canvas=g.__tsParticlesInternals.canvas||{};g.__tsParticlesInternals.canvas=g.__tsParticlesInternals.canvas||{};g.__tsParticlesInternals.canvas.utils=g.__tsParticlesInternals.canvas.utils||{};g.__tsParticlesInternals.path=g.__tsParticlesInternals.path||{};g.__tsParticlesInternals.path=g.__tsParticlesInternals.path||{};g.__tsParticlesInternals.path.utils=g.__tsParticlesInternals.path.utils||{};var __tsProxyFactory=typeof Proxy!=="undefined"?function(obj){return new Proxy(obj,{get:function(target,key){if(!(key in target)){target[key]={};}return target[key];}});}:function(obj){return obj;};g.__tsParticlesInternals.bundles=__tsProxyFactory(g.__tsParticlesInternals.bundles);g.__tsParticlesInternals.effects=__tsProxyFactory(g.__tsParticlesInternals.effects);g.__tsParticlesInternals.interactions=__tsProxyFactory(g.__tsParticlesInternals.interactions);g.__tsParticlesInternals.palettes=__tsProxyFactory(g.__tsParticlesInternals.palettes);g.__tsParticlesInternals.paths=__tsProxyFactory(g.__tsParticlesInternals.paths);g.__tsParticlesInternals.plugins=__tsProxyFactory(g.__tsParticlesInternals.plugins);g.__tsParticlesInternals.plugins.emittersShapes=__tsProxyFactory(g.__tsParticlesInternals.plugins.emittersShapes);g.__tsParticlesInternals.presets=__tsProxyFactory(g.__tsParticlesInternals.presets);g.__tsParticlesInternals.shapes=__tsProxyFactory(g.__tsParticlesInternals.shapes);g.__tsParticlesInternals.updaters=__tsProxyFactory(g.__tsParticlesInternals.updaters);g.__tsParticlesInternals.utils=__tsProxyFactory(g.__tsParticlesInternals.utils);g.__tsParticlesInternals.canvas=__tsProxyFactory(g.__tsParticlesInternals.canvas);g.__tsParticlesInternals.path=__tsProxyFactory(g.__tsParticlesInternals.path);g.tsparticlesInternalExports=g.tsparticlesInternalExports||{};})(typeof globalThis!=="undefined"?globalThis:typeof window!=="undefined"?window:this); /* Particles Interaction v4.1.0 */ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tsparticles/plugin-interactivity'), require('@tsparticles/engine')) : typeof define === 'function' && define.amd ? define(['exports', '@tsparticles/plugin-interactivity', '@tsparticles/engine'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.__tsParticlesInternals = global.__tsParticlesInternals || {}, global.__tsParticlesInternals.interactions = global.__tsParticlesInternals.interactions || {}, global.__tsParticlesInternals.interactions.particlesAttract = global.__tsParticlesInternals.interactions.particlesAttract || {}), global.__tsParticlesInternals.plugins.interactivity, global.__tsParticlesInternals.engine)); })(this, (function (exports, pluginInteractivity, engine) { 'use strict'; class Attract { distance; enable; rotate; constructor() { this.distance = 200; this.enable = false; this.rotate = { x: 3000, y: 3000, }; } load(data) { if (engine.isNull(data)) { return; } if (data.distance !== undefined) { this.distance = engine.setRangeValue(data.distance); } if (data.enable !== undefined) { this.enable = data.enable; } if (data.rotate) { const rotateX = data.rotate.x; if (rotateX !== undefined) { this.rotate.x = rotateX; } const rotateY = data.rotate.y; if (rotateY !== undefined) { this.rotate.y = rotateY; } } } } const attractFactor = 1000, identity = 1; class Attractor extends pluginInteractivity.ParticlesInteractorBase { #maxDistance; constructor(container) { super(container); this.#maxDistance = 0; } get maxDistance() { return this.#maxDistance; } clear() { } init() { } interact(p1) { if (!p1.options.attract?.enable) { return; } const container = this.container; if (engine.isNull(p1.attractDistance)) { const attractDistance = engine.getRangeValue(p1.options.attract.distance); if (attractDistance > this.#maxDistance) { this.#maxDistance = attractDistance; } p1.attractDistance = attractDistance * container.retina.pixelRatio; } const distance = p1.attractDistance, pos1 = p1.getPosition(), query = container.particles.grid.queryCircle(pos1, distance); for (const p2 of query) { if (p1 === p2 || !p2.options.attract?.enable || p2.destroyed || p2.spawning) { continue; } const pos2 = p2.getPosition(), { dx, dy } = engine.getDistances(pos1, pos2), rotate = p1.options.attract.rotate, ax = dx / (rotate.x * attractFactor), ay = dy / (rotate.y * attractFactor), p1Factor = p2.size.value / p1.size.value, p2Factor = identity / p1Factor; p1.velocity.x -= ax * p1Factor; p1.velocity.y -= ay * p1Factor; p2.velocity.x += ax * p2Factor; p2.velocity.y += ay * p2Factor; } } isEnabled(particle) { return particle.options.attract?.enable ?? false; } loadParticlesOptions(options, ...sources) { options.attract ??= new Attract(); for (const source of sources) { options.attract.load(source?.attract); } } reset() { } } async function loadParticlesAttractInteraction(engine) { engine.checkVersion("4.1.0"); await engine.pluginManager.register((e) => { pluginInteractivity.ensureInteractivityPluginLoaded(e); e.pluginManager.addInteractor?.("particlesAttract", container => { return Promise.resolve(new Attractor(container)); }); }); } const globalObject = globalThis; globalObject.__tsParticlesInternals = globalObject.__tsParticlesInternals ?? {}; globalObject.loadParticlesAttractInteraction = loadParticlesAttractInteraction; exports.loadParticlesAttractInteraction = loadParticlesAttractInteraction; })); Object.assign(globalThis.window || globalThis, { loadParticlesAttractInteraction: (globalThis.__tsParticlesInternals.interactions.particlesAttract || {}).loadParticlesAttractInteraction }); delete (globalThis.window || globalThis).tsparticlesInternalExports;