UNPKG

@tsparticles/path-curl-noise

Version:

tsParticles curl noise path

40 lines (39 loc) 1.55 kB
import { Vector, deepExtend, double, getRandom } from "@tsparticles/engine"; import { SimplexNoise } from "@tsparticles/simplex-noise"; const defaultOptions = { speed: 0.2, step: 250, }; export class CurlNoiseGenerator { options; #container; #res; #simplex; constructor(container) { this.#container = container; this.#res = Vector.origin; const simplex = new SimplexNoise(); this.#simplex = simplex.noise2d; this.options = deepExtend({}, defaultOptions); } generate(particle) { const pos = particle.getPosition(), { speed, step } = this.options, x = pos.x / step, y = pos.y / step, eps = 0.001, n1a = this.#simplex.noise(x, y + eps), n2a = this.#simplex.noise(x, y - eps), a = (n1a - n2a) / (double * eps), n1b = this.#simplex.noise(x + eps, y), n2b = this.#simplex.noise(x - eps, y), b = (n1b - n2b) / (double * eps); particle.velocity.x = 0; particle.velocity.y = 0; this.#res.x = speed * a; this.#res.y = speed * -b; return this.#res; } init() { const container = this.#container, sourceOptions = container.actualOptions.particles.move.path.options; this.options.seed = sourceOptions["seed"]; this.options.speed = (sourceOptions["speed"] ?? defaultOptions.speed) * container.retina.pixelRatio; this.options.step = sourceOptions["step"] ?? defaultOptions.step; this.#simplex.seed(this.options.seed ?? getRandom()); } reset() { } update() { } }