@tsparticles/updater-tilt
Version:
tsParticles particles tilt updater
157 lines (148 loc) • 9.06 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);
/* Updater v4.1.3 */
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tsparticles/engine')) :
typeof define === 'function' && define.amd ? define(['exports', '@tsparticles/engine'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.__tsParticlesInternals = global.__tsParticlesInternals || {}, global.__tsParticlesInternals.updaters = global.__tsParticlesInternals.updaters || {}, global.__tsParticlesInternals.updaters.tilt = global.__tsParticlesInternals.updaters.tilt || {}), global.__tsParticlesInternals.engine));
})(this, (function (exports, engine) { 'use strict';
var TiltDirection;
(function (TiltDirection) {
TiltDirection["clockwise"] = "clockwise";
TiltDirection["counterClockwise"] = "counter-clockwise";
TiltDirection["random"] = "random";
})(TiltDirection || (TiltDirection = {}));
class TiltAnimation {
decay;
enable;
speed;
sync;
constructor() {
this.enable = false;
this.speed = 0;
this.decay = 0;
this.sync = false;
}
load(data) {
if (engine.isNull(data)) {
return;
}
if (data.enable !== undefined) {
this.enable = data.enable;
}
if (data.speed !== undefined) {
this.speed = engine.setRangeValue(data.speed);
}
if (data.decay !== undefined) {
this.decay = engine.setRangeValue(data.decay);
}
if (data.sync !== undefined) {
this.sync = data.sync;
}
}
}
class Tilt extends engine.ValueWithRandom {
animation;
direction;
enable;
constructor() {
super();
this.animation = new TiltAnimation();
this.direction = TiltDirection.clockwise;
this.enable = false;
this.value = 0;
}
load(data) {
super.load(data);
if (engine.isNull(data)) {
return;
}
this.animation.load(data.animation);
if (data.direction !== undefined) {
this.direction = data.direction;
}
if (data.enable !== undefined) {
this.enable = data.enable;
}
}
}
const maxAngle = 360;
class TiltUpdater {
#container;
constructor(container) {
this.#container = container;
}
getTransformValues(particle) {
const tilt = particle.tilt?.enable && particle.tilt;
return {
b: tilt ? Math.cos(tilt.value) * tilt.cosDirection : undefined,
c: tilt ? Math.sin(tilt.value) * tilt.sinDirection : undefined,
};
}
init(particle) {
const tiltOptions = particle.options.tilt;
if (!tiltOptions) {
return;
}
particle.tilt = {
enable: tiltOptions.enable,
value: engine.degToRad(engine.getRangeValue(tiltOptions.value)),
sinDirection: engine.getRandom() >= engine.half ? engine.identity : -engine.identity,
cosDirection: engine.getRandom() >= engine.half ? engine.identity : -engine.identity,
min: 0,
max: engine.doublePI,
};
let tiltDirection = tiltOptions.direction;
if (tiltDirection === TiltDirection.random) {
const index = Math.floor(engine.getRandom() * engine.double), minIndex = 0;
tiltDirection = index > minIndex ? TiltDirection.counterClockwise : TiltDirection.clockwise;
}
switch (tiltDirection) {
case TiltDirection.counterClockwise:
case "counterClockwise":
particle.tilt.status = engine.AnimationStatus.decreasing;
break;
case TiltDirection.clockwise:
particle.tilt.status = engine.AnimationStatus.increasing;
break;
}
const tiltAnimation = particle.options.tilt?.animation;
if (tiltAnimation?.enable) {
particle.tilt.decay = engine.identity - engine.getRangeValue(tiltAnimation.decay);
particle.tilt.velocity = (engine.getRangeValue(tiltAnimation.speed) / maxAngle) * this.#container.retina.reduceFactor;
if (!tiltAnimation.sync) {
particle.tilt.velocity *= engine.getRandom();
}
}
}
isEnabled(particle) {
const tiltAnimation = particle.options.tilt?.animation;
return !particle.destroyed && !particle.spawning && !!tiltAnimation?.enable;
}
loadOptions(options, ...sources) {
options.tilt ??= new Tilt();
for (const source of sources) {
options.tilt.load(source?.tilt);
}
}
update(particle, delta) {
if (!this.isEnabled(particle) || !particle.tilt) {
return;
}
engine.updateAnimation(particle, particle.tilt, false, engine.DestroyType.none, delta);
}
}
async function loadTiltUpdater(engine) {
engine.checkVersion("4.1.3");
await engine.pluginManager.register(e => {
e.pluginManager.addParticleUpdater("tilt", container => {
return Promise.resolve(new TiltUpdater(container));
});
});
}
const globalObject = globalThis;
globalObject.__tsParticlesInternals = globalObject.__tsParticlesInternals ?? {};
globalObject.loadTiltUpdater = loadTiltUpdater;
exports.loadTiltUpdater = loadTiltUpdater;
}));
Object.assign(globalThis.window || globalThis, { loadTiltUpdater: (globalThis.__tsParticlesInternals.updaters.tilt || {}).loadTiltUpdater });
delete (globalThis.window || globalThis).tsparticlesInternalExports;