@tsparticles/path-curves
Version:
tsParticles curves path
122 lines (115 loc) • 8.49 kB
JavaScript
(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);
/* Path v4.1.0 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tsparticles/plugin-move'), require('@tsparticles/engine')) :
typeof define === 'function' && define.amd ? define(['exports', '@tsparticles/plugin-move', '@tsparticles/engine'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.__tsParticlesInternals = global.__tsParticlesInternals || {}, global.__tsParticlesInternals.paths = global.__tsParticlesInternals.paths || {}, global.__tsParticlesInternals.paths.curves = global.__tsParticlesInternals.paths.curves || {}), global.__tsParticlesInternals.plugins.move, global.__tsParticlesInternals.engine));
})(this, (function (exports, pluginMove, engine) { 'use strict';
function CurvesPathGen(rndFunc, period, nbHarmonics, attenHarmonics, lowValue = 0, highValue = 1) {
const arP0 = [], arP1 = [], amplitudes = [], increments = [], phases = [], randomFunc = rndFunc ?? engine.getRandom;
let globAmplitude = 0;
if (nbHarmonics < 1)
nbHarmonics = 1;
for (let kh = 1; kh <= nbHarmonics; ++kh) {
arP0[kh] = randomFunc();
arP1[kh] = randomFunc();
amplitudes[kh] = kh === 1 ? 1 : amplitudes[kh - 1] * attenHarmonics;
globAmplitude += amplitudes[kh];
increments[kh] = kh / period;
phases[kh] = randomFunc();
}
amplitudes.forEach((value, kh) => (amplitudes[kh] = (value / globAmplitude) * (highValue - lowValue)));
return () => {
let pf, pfl, signal = 0;
for (let kh = nbHarmonics; kh >= 1; --kh) {
pf = phases[kh] += increments[kh];
if (phases[kh] >= 1) {
pf = phases[kh] -= 1;
arP0[kh] = arP1[kh];
arP1[kh] = randomFunc();
}
pfl = pf ** 2 * (3 - 2 * pf);
signal += (arP0[kh] * (1 - pfl) + arP1[kh] * pfl) * amplitudes[kh];
}
return signal + lowValue;
};
}
const defaultOptions = {
rndFunc: null,
period: 100,
nbHarmonics: 2,
attenHarmonics: 0.8,
lowValue: -0.03,
highValue: 0.03,
};
function randomVelocity() {
const offset = 0.8, factor = 0.6;
return engine.getRandom() * factor + offset;
}
class CurvesPathGenerator {
options;
#container;
constructor(container) {
this.#container = container;
this.options = engine.deepExtend({}, defaultOptions);
}
generate(particle) {
if (!particle.pathGen) {
const options = this.options;
particle.pathGen = CurvesPathGen(options.rndFunc, options.period, options.nbHarmonics, options.attenHarmonics, options.lowValue, options.highValue);
}
if (particle.curveVelocity) {
particle.curveVelocity.length += 0.01;
particle.curveVelocity.angle = (particle.curveVelocity.angle + particle.pathGen()) % engine.doublePI;
}
else {
particle.curveVelocity = engine.Vector.origin;
particle.curveVelocity.length = randomVelocity();
particle.curveVelocity.angle = engine.getRandom() * engine.doublePI;
}
particle.velocity.x = 0;
particle.velocity.y = 0;
return particle.curveVelocity;
}
init() {
const sourceOptions = this.#container.actualOptions.particles.move.path.options;
if (engine.isFunction(sourceOptions["rndFunc"])) {
this.options.rndFunc = sourceOptions["rndFunc"];
}
else if (engine.isString(sourceOptions["rndFunc"])) {
this.options.rndFunc =
globalThis[sourceOptions["rndFunc"]] ??
this.options.rndFunc;
}
this.options.period = sourceOptions["period"] ?? this.options.period;
this.options.nbHarmonics = sourceOptions["nbHarmonics"] ?? this.options.nbHarmonics;
this.options.attenHarmonics =
sourceOptions["attenHarmonics"] ?? this.options.attenHarmonics;
this.options.lowValue = sourceOptions["lowValue"] ?? this.options.lowValue;
this.options.highValue = sourceOptions["highValue"] ?? this.options.highValue;
}
reset(particle) {
delete particle.pathGen;
delete particle.curveVelocity;
}
update() {
}
}
const curvesPathName = "curvesPathGenerator";
async function loadCurvesPath(engine) {
engine.checkVersion("4.1.0");
await engine.pluginManager.register((e) => {
pluginMove.ensureBaseMoverLoaded(e);
e.pluginManager.addPathGenerator?.(curvesPathName, container => {
return Promise.resolve(new CurvesPathGenerator(container));
});
});
}
const globalObject = globalThis;
globalObject.__tsParticlesInternals = globalObject.__tsParticlesInternals ?? {};
globalObject.loadCurvesPath = loadCurvesPath;
exports.curvesPathName = curvesPathName;
exports.loadCurvesPath = loadCurvesPath;
}));
Object.assign(globalThis.window || globalThis, { loadCurvesPath: (globalThis.__tsParticlesInternals.paths.curves || {}).loadCurvesPath });
delete (globalThis.window || globalThis).tsparticlesInternalExports;