@tsparticles/path-curves
Version:
tsParticles curves path
64 lines (63 loc) • 2.4 kB
JavaScript
import { Vector, deepExtend, doublePI, getRandom, isFunction, isString } from "@tsparticles/engine";
import { CurvesPathGen } from "./Curves.js";
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 getRandom() * factor + offset;
}
export class CurvesPathGenerator {
options;
#container;
constructor(container) {
this.#container = container;
this.options = 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()) % doublePI;
}
else {
particle.curveVelocity = Vector.origin;
particle.curveVelocity.length = randomVelocity();
particle.curveVelocity.angle = getRandom() * doublePI;
}
particle.velocity.x = 0;
particle.velocity.y = 0;
return particle.curveVelocity;
}
init() {
const sourceOptions = this.#container.actualOptions.particles.move.path.options;
if (isFunction(sourceOptions["rndFunc"])) {
this.options.rndFunc = sourceOptions["rndFunc"];
}
else if (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() {
}
}