@tsparticles/plugin-infection 3.0.0-alpha.1 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@
4
4
  * Demo / Generator : https://particles.js.org/
5
5
  * GitHub : https://www.github.com/matteobruni/tsparticles
6
6
  * How to use? : Check the GitHub README
7
- * v3.0.0-alpha.1
7
+ * v3.0.0-beta.0
8
8
  */
9
9
  (function webpackUniversalModuleDefinition(root, factory) {
10
10
  if(typeof exports === 'object' && typeof module === 'object')
@@ -91,7 +91,7 @@ __webpack_require__.r(__webpack_exports__);
91
91
 
92
92
  // EXPORTS
93
93
  __webpack_require__.d(__webpack_exports__, {
94
- "loadInfectionPlugin": () => (/* binding */ loadInfectionPlugin)
94
+ loadInfectionPlugin: () => (/* binding */ loadInfectionPlugin)
95
95
  });
96
96
 
97
97
  // EXTERNAL MODULE: external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"}
@@ -161,93 +161,105 @@ class Infection {
161
161
  ;// CONCATENATED MODULE: ./dist/browser/Infecter.js
162
162
  class Infecter {
163
163
  constructor(container) {
164
- this.container = container;
164
+ this._nextInfectionStage = particle => {
165
+ const infectionOptions = this._container.actualOptions.infection,
166
+ {
167
+ infection
168
+ } = particle;
169
+ if (!infectionOptions || !infection) {
170
+ return;
171
+ }
172
+ const stagesCount = infectionOptions.stages.length;
173
+ if (stagesCount <= 0 || infection.stage === undefined) {
174
+ return;
175
+ }
176
+ infection.time = 0;
177
+ if (stagesCount <= ++infection.stage) {
178
+ if (infectionOptions.cure) {
179
+ delete infection.stage;
180
+ delete infection.time;
181
+ return;
182
+ } else {
183
+ infection.stage = 0;
184
+ infection.time = 0;
185
+ }
186
+ }
187
+ };
188
+ this._container = container;
165
189
  }
166
190
  startInfection(particle, stage) {
167
- const options = this.container.actualOptions;
168
- if (!options.infection || !particle.infection) {
191
+ const infectionOptions = this._container.actualOptions.infection,
192
+ {
193
+ infection
194
+ } = particle;
195
+ if (!infectionOptions || !infection) {
169
196
  return;
170
197
  }
171
- const stages = options.infection.stages,
198
+ const stages = infectionOptions.stages,
172
199
  stagesCount = stages.length;
173
200
  if (stage > stagesCount || stage < 0) {
174
201
  return;
175
202
  }
176
- particle.infection.delay = 0;
177
- particle.infection.delayStage = stage;
203
+ infection.delay = 0;
204
+ infection.delayStage = stage;
178
205
  }
179
206
  updateInfection(particle, delta) {
180
- const infection = this.container.actualOptions.infection;
181
- if (!infection || !particle.infection) {
207
+ const infectionOptions = this._container.actualOptions.infection,
208
+ {
209
+ infection
210
+ } = particle;
211
+ if (!infectionOptions || !infection) {
182
212
  return;
183
213
  }
184
- const stages = infection.stages,
214
+ const stages = infectionOptions.stages,
185
215
  stagesCount = stages.length;
186
- if (particle.infection.delay !== undefined && particle.infection.delayStage !== undefined) {
187
- const stage = particle.infection.delayStage;
216
+ if (infection.delay !== undefined && infection.delayStage !== undefined) {
217
+ const stage = infection.delayStage;
188
218
  if (stage > stagesCount || stage < 0) {
189
219
  return;
190
220
  }
191
- if (particle.infection.delay >= infection.delay * 1000) {
192
- particle.infection.stage = stage;
193
- particle.infection.time = 0;
194
- delete particle.infection.delay;
195
- delete particle.infection.delayStage;
221
+ if (infection.delay >= infectionOptions.delay * 1000) {
222
+ infection.stage = stage;
223
+ infection.time = 0;
224
+ delete infection.delay;
225
+ delete infection.delayStage;
196
226
  } else {
197
- particle.infection.delay += delta;
227
+ infection.delay += delta;
198
228
  }
199
229
  } else {
200
- delete particle.infection.delay;
201
- delete particle.infection.delayStage;
230
+ delete infection.delay;
231
+ delete infection.delayStage;
202
232
  }
203
- if (particle.infection.stage !== undefined && particle.infection.time !== undefined) {
204
- const infectionStage = stages[particle.infection.stage];
233
+ if (infection.stage !== undefined && infection.time !== undefined) {
234
+ const infectionStage = stages[infection.stage];
205
235
  if (infectionStage.duration !== undefined && infectionStage.duration >= 0) {
206
- if (particle.infection.time > infectionStage.duration * 1000) {
207
- this.nextInfectionStage(particle);
236
+ if (infection.time > infectionStage.duration * 1000) {
237
+ this._nextInfectionStage(particle);
208
238
  } else {
209
- particle.infection.time += delta;
239
+ infection.time += delta;
210
240
  }
211
241
  } else {
212
- particle.infection.time += delta;
242
+ infection.time += delta;
213
243
  }
214
244
  } else {
215
- delete particle.infection.stage;
216
- delete particle.infection.time;
245
+ delete infection.stage;
246
+ delete infection.time;
217
247
  }
218
248
  }
219
249
  updateInfectionStage(particle, stage) {
220
- const options = this.container.actualOptions;
221
- if (!options.infection || !particle.infection) {
222
- return;
223
- }
224
- const stagesCount = options.infection.stages.length;
225
- if (stage > stagesCount || stage < 0 || particle.infection.stage !== undefined && particle.infection.stage > stage) {
226
- return;
227
- }
228
- particle.infection.stage = stage;
229
- particle.infection.time = 0;
230
- }
231
- nextInfectionStage(particle) {
232
- const options = this.container.actualOptions;
233
- if (!options.infection || !particle.infection) {
250
+ const options = this._container.actualOptions,
251
+ {
252
+ infection
253
+ } = particle;
254
+ if (!options.infection || !infection) {
234
255
  return;
235
256
  }
236
257
  const stagesCount = options.infection.stages.length;
237
- if (stagesCount <= 0 || particle.infection.stage === undefined) {
258
+ if (stage > stagesCount || stage < 0 || infection.stage !== undefined && infection.stage > stage) {
238
259
  return;
239
260
  }
240
- particle.infection.time = 0;
241
- if (stagesCount <= ++particle.infection.stage) {
242
- if (options.infection.cure) {
243
- delete particle.infection.stage;
244
- delete particle.infection.time;
245
- return;
246
- } else {
247
- particle.infection.stage = 0;
248
- particle.infection.time = 0;
249
- }
250
- }
261
+ infection.stage = stage;
262
+ infection.time = 0;
251
263
  }
252
264
  }
253
265
  ;// CONCATENATED MODULE: ./dist/browser/InfectionInstance.js
@@ -255,11 +267,11 @@ class Infecter {
255
267
 
256
268
  class InfectionInstance {
257
269
  constructor(container) {
258
- this.container = container;
259
- this.container.infecter = new Infecter(this.container);
270
+ this._container = container;
271
+ this._container.infecter = new Infecter(this._container);
260
272
  }
261
273
  particleFillColor(particle) {
262
- const options = this.container.actualOptions;
274
+ const options = this._container.actualOptions;
263
275
  if (!particle.infection || !options.infection) {
264
276
  return;
265
277
  }
@@ -272,13 +284,12 @@ class InfectionInstance {
272
284
  return this.particleFillColor(particle);
273
285
  }
274
286
  particlesSetup() {
275
- var _a;
276
- const options = this.container.actualOptions;
287
+ const options = this._container.actualOptions;
277
288
  if (!options.infection) {
278
289
  return;
279
290
  }
280
291
  for (let i = 0; i < options.infection.infections; i++) {
281
- const notInfected = this.container.particles.array.filter(p => {
292
+ const notInfected = this._container.particles.filter(p => {
282
293
  const infP = p;
283
294
  if (!infP.infection) {
284
295
  infP.infection = {};
@@ -286,7 +297,7 @@ class InfectionInstance {
286
297
  return infP.infection.stage === undefined;
287
298
  });
288
299
  const infected = (0,engine_root_window_.itemFromArray)(notInfected);
289
- (_a = this.container.infecter) === null || _a === void 0 ? void 0 : _a.startInfection(infected, 0);
300
+ this._container.infecter?.startInfection(infected, 0);
290
301
  }
291
302
  }
292
303
  }
@@ -299,50 +310,46 @@ class ParticlesInfecter extends engine_root_window_.ParticlesInteractorBase {
299
310
  clear() {}
300
311
  init() {}
301
312
  async interact(p1, delta) {
302
- var _a, _b, _c, _d, _e;
303
313
  const infecter = this.container.infecter;
304
314
  if (!infecter) {
305
315
  return;
306
316
  }
307
317
  infecter.updateInfection(p1, delta.value);
308
- if (((_a = p1.infection) === null || _a === void 0 ? void 0 : _a.stage) === undefined) {
318
+ if (p1.infection?.stage === undefined) {
309
319
  return;
310
320
  }
311
321
  const container = this.container,
312
322
  options = container.actualOptions,
313
323
  infectionOptions = options.infection;
314
- if (!(infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.enable) || infectionOptions.stages.length < 1) {
324
+ if (!infectionOptions?.enable || infectionOptions.stages.length < 1) {
315
325
  return;
316
326
  }
317
327
  const infectionStage1 = infectionOptions.stages[p1.infection.stage],
318
328
  pxRatio = container.retina.pixelRatio,
319
329
  radius = p1.getRadius() * 2 + infectionStage1.radius * pxRatio,
320
330
  pos = p1.getPosition(),
321
- infectedStage1 = (_b = infectionStage1.infectedStage) !== null && _b !== void 0 ? _b : p1.infection.stage,
331
+ infectedStage1 = infectionStage1.infectedStage ?? p1.infection.stage,
322
332
  query = container.particles.quadTree.queryCircle(pos, radius),
323
333
  infections = infectionStage1.rate,
324
334
  neighbors = query.length;
325
335
  for (const p2 of query) {
326
336
  const infP2 = p2;
327
- if (infP2 === p1 || infP2.destroyed || infP2.spawning || !(((_c = infP2.infection) === null || _c === void 0 ? void 0 : _c.stage) === undefined || infP2.infection.stage !== p1.infection.stage)) {
337
+ if (infP2 === p1 || infP2.destroyed || infP2.spawning || !(infP2.infection?.stage === undefined || infP2.infection.stage !== p1.infection.stage) || (0,engine_root_window_.getRandom)() >= infections / neighbors) {
328
338
  continue;
329
339
  }
330
- if ((0,engine_root_window_.getRandom)() < infections / neighbors) {
331
- if (((_d = infP2.infection) === null || _d === void 0 ? void 0 : _d.stage) === undefined) {
332
- infecter.startInfection(infP2, infectedStage1);
333
- } else if (infP2.infection.stage < p1.infection.stage) {
334
- infecter.updateInfectionStage(infP2, infectedStage1);
335
- } else if (infP2.infection.stage > p1.infection.stage) {
336
- const infectionStage2 = infectionOptions.stages[infP2.infection.stage];
337
- const infectedStage2 = (_e = infectionStage2 === null || infectionStage2 === void 0 ? void 0 : infectionStage2.infectedStage) !== null && _e !== void 0 ? _e : infP2.infection.stage;
338
- infecter.updateInfectionStage(p1, infectedStage2);
339
- }
340
+ if (infP2.infection?.stage === undefined) {
341
+ infecter.startInfection(infP2, infectedStage1);
342
+ } else if (infP2.infection.stage < p1.infection.stage) {
343
+ infecter.updateInfectionStage(infP2, infectedStage1);
344
+ } else if (infP2.infection.stage > p1.infection.stage) {
345
+ const infectionStage2 = infectionOptions.stages[infP2.infection.stage];
346
+ const infectedStage2 = infectionStage2?.infectedStage ?? infP2.infection.stage;
347
+ infecter.updateInfectionStage(p1, infectedStage2);
340
348
  }
341
349
  }
342
350
  }
343
351
  isEnabled() {
344
- var _a, _b, _c;
345
- return (_c = (_b = (_a = this.container.actualOptions) === null || _a === void 0 ? void 0 : _a.infection) === null || _b === void 0 ? void 0 : _b.enable) !== null && _c !== void 0 ? _c : false;
352
+ return this.container.actualOptions?.infection?.enable ?? false;
346
353
  }
347
354
  reset() {}
348
355
  }
@@ -358,24 +365,23 @@ class InfectionPlugin {
358
365
  return new InfectionInstance(container);
359
366
  }
360
367
  loadOptions(options, source) {
361
- if (!this.needsPlugin(source)) {
368
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
362
369
  return;
363
370
  }
364
371
  let infectionOptions = options.infection;
365
- if ((infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.load) === undefined) {
372
+ if (infectionOptions?.load === undefined) {
366
373
  options.infection = infectionOptions = new Infection();
367
374
  }
368
- infectionOptions.load(source === null || source === void 0 ? void 0 : source.infection);
375
+ infectionOptions.load(source?.infection);
369
376
  }
370
377
  needsPlugin(options) {
371
- var _a, _b;
372
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.infection) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
378
+ return options?.infection?.enable ?? false;
373
379
  }
374
380
  }
375
- async function loadInfectionPlugin(engine) {
381
+ async function loadInfectionPlugin(engine, refresh = true) {
376
382
  const plugin = new InfectionPlugin();
377
- await engine.addPlugin(plugin);
378
- await engine.addInteractor("particlesInfection", container => new ParticlesInfecter(container));
383
+ await engine.addPlugin(plugin, refresh);
384
+ await engine.addInteractor("particlesInfection", container => new ParticlesInfecter(container), refresh);
379
385
  }
380
386
 
381
387
 
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see tsparticles.plugin.infection.min.js.LICENSE.txt */
2
- !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],t);else{var i="object"==typeof exports?t(require("@tsparticles/engine")):t(e.window);for(var n in i)("object"==typeof exports?exports:e)[n]=i[n]}}(this,(e=>(()=>{"use strict";var t={533:t=>{t.exports=e}},i={};function n(e){var o=i[e];if(void 0!==o)return o.exports;var c=i[e]={exports:{}};return t[e](c,c.exports,n),c.exports}n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{loadInfectionPlugin:()=>l});var e=n(533);class t{constructor(){this.color=new e.OptionsColor,this.color.value="#ff0000",this.radius=0,this.rate=1}load(t){void 0!==t&&(void 0!==t.color&&(this.color=e.OptionsColor.create(this.color,t.color)),this.duration=t.duration,this.infectedStage=t.infectedStage,void 0!==t.radius&&(this.radius=t.radius),void 0!==t.rate&&(this.rate=t.rate))}}class i{constructor(){this.cure=!1,this.delay=0,this.enable=!1,this.infections=0,this.stages=[]}load(e){void 0!==e&&(void 0!==e.cure&&(this.cure=e.cure),void 0!==e.delay&&(this.delay=e.delay),void 0!==e.enable&&(this.enable=e.enable),void 0!==e.infections&&(this.infections=e.infections),void 0!==e.stages&&(this.stages=e.stages.map((e=>{const i=new t;return i.load(e),i}))))}}class c{constructor(e){this.container=e}startInfection(e,t){const i=this.container.actualOptions;if(!i.infection||!e.infection)return;t>i.infection.stages.length||t<0||(e.infection.delay=0,e.infection.delayStage=t)}updateInfection(e,t){const i=this.container.actualOptions.infection;if(!i||!e.infection)return;const n=i.stages,o=n.length;if(void 0!==e.infection.delay&&void 0!==e.infection.delayStage){const n=e.infection.delayStage;if(n>o||n<0)return;e.infection.delay>=1e3*i.delay?(e.infection.stage=n,e.infection.time=0,delete e.infection.delay,delete e.infection.delayStage):e.infection.delay+=t}else delete e.infection.delay,delete e.infection.delayStage;if(void 0!==e.infection.stage&&void 0!==e.infection.time){const i=n[e.infection.stage];void 0!==i.duration&&i.duration>=0&&e.infection.time>1e3*i.duration?this.nextInfectionStage(e):e.infection.time+=t}else delete e.infection.stage,delete e.infection.time}updateInfectionStage(e,t){const i=this.container.actualOptions;if(!i.infection||!e.infection)return;t>i.infection.stages.length||t<0||void 0!==e.infection.stage&&e.infection.stage>t||(e.infection.stage=t,e.infection.time=0)}nextInfectionStage(e){const t=this.container.actualOptions;if(!t.infection||!e.infection)return;const i=t.infection.stages.length;if(!(i<=0||void 0===e.infection.stage)&&(e.infection.time=0,i<=++e.infection.stage)){if(t.infection.cure)return delete e.infection.stage,void delete e.infection.time;e.infection.stage=0,e.infection.time=0}}}class a{constructor(e){this.container=e,this.container.infecter=new c(this.container)}particleFillColor(e){const t=this.container.actualOptions;if(!e.infection||!t.infection)return;const i=e.infection.stage,n=t.infection.stages;return void 0!==i?n[i].color:void 0}particleStrokeColor(e){return this.particleFillColor(e)}particlesSetup(){var t;const i=this.container.actualOptions;if(i.infection)for(let n=0;n<i.infection.infections;n++){const i=this.container.particles.array.filter((e=>{const t=e;return t.infection||(t.infection={}),void 0===t.infection.stage})),n=(0,e.itemFromArray)(i);null===(t=this.container.infecter)||void 0===t||t.startInfection(n,0)}}}class s extends e.ParticlesInteractorBase{constructor(e){super(e)}clear(){}init(){}async interact(t,i){var n,o,c,a,s;const r=this.container.infecter;if(!r)return;if(r.updateInfection(t,i.value),void 0===(null===(n=t.infection)||void 0===n?void 0:n.stage))return;const l=this.container,f=l.actualOptions.infection;if(!(null==f?void 0:f.enable)||f.stages.length<1)return;const d=f.stages[t.infection.stage],u=l.retina.pixelRatio,g=2*t.getRadius()+d.radius*u,v=t.getPosition(),p=null!==(o=d.infectedStage)&&void 0!==o?o:t.infection.stage,h=l.particles.quadTree.queryCircle(v,g),y=d.rate,S=h.length;for(const i of h){const n=i;if(!(n===t||n.destroyed||n.spawning||void 0!==(null===(c=n.infection)||void 0===c?void 0:c.stage)&&n.infection.stage===t.infection.stage)&&(0,e.getRandom)()<y/S)if(void 0===(null===(a=n.infection)||void 0===a?void 0:a.stage))r.startInfection(n,p);else if(n.infection.stage<t.infection.stage)r.updateInfectionStage(n,p);else if(n.infection.stage>t.infection.stage){const e=f.stages[n.infection.stage],i=null!==(s=null==e?void 0:e.infectedStage)&&void 0!==s?s:n.infection.stage;r.updateInfectionStage(t,i)}}}isEnabled(){var e,t,i;return null!==(i=null===(t=null===(e=this.container.actualOptions)||void 0===e?void 0:e.infection)||void 0===t?void 0:t.enable)&&void 0!==i&&i}reset(){}}class r{constructor(){this.id="infection"}getPlugin(e){return new a(e)}loadOptions(e,t){if(!this.needsPlugin(t))return;let n=e.infection;void 0===(null==n?void 0:n.load)&&(e.infection=n=new i),n.load(null==t?void 0:t.infection)}needsPlugin(e){var t,i;return null!==(i=null===(t=null==e?void 0:e.infection)||void 0===t?void 0:t.enable)&&void 0!==i&&i}}async function l(e){const t=new r;await e.addPlugin(t),await e.addInteractor("particlesInfection",(e=>new s(e)))}})(),o})()));
2
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],t);else{var i="object"==typeof exports?t(require("@tsparticles/engine")):t(e.window);for(var n in i)("object"==typeof exports?exports:e)[n]=i[n]}}(this,(e=>(()=>{"use strict";var t={533:t=>{t.exports=e}},i={};function n(e){var o=i[e];if(void 0!==o)return o.exports;var s=i[e]={exports:{}};return t[e](s,s.exports,n),s.exports}n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{loadInfectionPlugin:()=>l});var e=n(533);class t{constructor(){this.color=new e.OptionsColor,this.color.value="#ff0000",this.radius=0,this.rate=1}load(t){void 0!==t&&(void 0!==t.color&&(this.color=e.OptionsColor.create(this.color,t.color)),this.duration=t.duration,this.infectedStage=t.infectedStage,void 0!==t.radius&&(this.radius=t.radius),void 0!==t.rate&&(this.rate=t.rate))}}class i{constructor(){this.cure=!1,this.delay=0,this.enable=!1,this.infections=0,this.stages=[]}load(e){void 0!==e&&(void 0!==e.cure&&(this.cure=e.cure),void 0!==e.delay&&(this.delay=e.delay),void 0!==e.enable&&(this.enable=e.enable),void 0!==e.infections&&(this.infections=e.infections),void 0!==e.stages&&(this.stages=e.stages.map((e=>{const i=new t;return i.load(e),i}))))}}class s{constructor(e){this._nextInfectionStage=e=>{const t=this._container.actualOptions.infection,{infection:i}=e;if(!t||!i)return;const n=t.stages.length;if(!(n<=0||void 0===i.stage)&&(i.time=0,n<=++i.stage)){if(t.cure)return delete i.stage,void delete i.time;i.stage=0,i.time=0}},this._container=e}startInfection(e,t){const i=this._container.actualOptions.infection,{infection:n}=e;if(!i||!n)return;t>i.stages.length||t<0||(n.delay=0,n.delayStage=t)}updateInfection(e,t){const i=this._container.actualOptions.infection,{infection:n}=e;if(!i||!n)return;const o=i.stages,s=o.length;if(void 0!==n.delay&&void 0!==n.delayStage){const e=n.delayStage;if(e>s||e<0)return;n.delay>=1e3*i.delay?(n.stage=e,n.time=0,delete n.delay,delete n.delayStage):n.delay+=t}else delete n.delay,delete n.delayStage;if(void 0!==n.stage&&void 0!==n.time){const i=o[n.stage];void 0!==i.duration&&i.duration>=0&&n.time>1e3*i.duration?this._nextInfectionStage(e):n.time+=t}else delete n.stage,delete n.time}updateInfectionStage(e,t){const i=this._container.actualOptions,{infection:n}=e;if(!i.infection||!n)return;t>i.infection.stages.length||t<0||void 0!==n.stage&&n.stage>t||(n.stage=t,n.time=0)}}class a{constructor(e){this._container=e,this._container.infecter=new s(this._container)}particleFillColor(e){const t=this._container.actualOptions;if(!e.infection||!t.infection)return;const i=e.infection.stage,n=t.infection.stages;return void 0!==i?n[i].color:void 0}particleStrokeColor(e){return this.particleFillColor(e)}particlesSetup(){const t=this._container.actualOptions;if(t.infection)for(let i=0;i<t.infection.infections;i++){const t=this._container.particles.filter((e=>{const t=e;return t.infection||(t.infection={}),void 0===t.infection.stage})),i=(0,e.itemFromArray)(t);this._container.infecter?.startInfection(i,0)}}}class r extends e.ParticlesInteractorBase{constructor(e){super(e)}clear(){}init(){}async interact(t,i){const n=this.container.infecter;if(!n)return;if(n.updateInfection(t,i.value),void 0===t.infection?.stage)return;const o=this.container,s=o.actualOptions.infection;if(!s?.enable||s.stages.length<1)return;const a=s.stages[t.infection.stage],r=o.retina.pixelRatio,c=2*t.getRadius()+a.radius*r,l=t.getPosition(),f=a.infectedStage??t.infection.stage,d=o.particles.quadTree.queryCircle(l,c),u=a.rate,g=d.length;for(const i of d){const o=i;if(!(o===t||o.destroyed||o.spawning||void 0!==o.infection?.stage&&o.infection.stage===t.infection.stage||(0,e.getRandom)()>=u/g))if(void 0===o.infection?.stage)n.startInfection(o,f);else if(o.infection.stage<t.infection.stage)n.updateInfectionStage(o,f);else if(o.infection.stage>t.infection.stage){const e=s.stages[o.infection.stage],i=e?.infectedStage??o.infection.stage;n.updateInfectionStage(t,i)}}}isEnabled(){return this.container.actualOptions?.infection?.enable??!1}reset(){}}class c{constructor(){this.id="infection"}getPlugin(e){return new a(e)}loadOptions(e,t){if(!this.needsPlugin(e)&&!this.needsPlugin(t))return;let n=e.infection;void 0===n?.load&&(e.infection=n=new i),n.load(t?.infection)}needsPlugin(e){return e?.infection?.enable??!1}}async function l(e,t=!0){const i=new c;await e.addPlugin(i,t),await e.addInteractor("particlesInfection",(e=>new r(e)),t)}})(),o})()));
@@ -1,8 +1 @@
1
- /*!
2
- * Author : Matteo Bruni
3
- * MIT license: https://opensource.org/licenses/MIT
4
- * Demo / Generator : https://particles.js.org/
5
- * GitHub : https://www.github.com/matteobruni/tsparticles
6
- * How to use? : Check the GitHub README
7
- * v3.0.0-alpha.1
8
- */
1
+ /*! tsParticles Infection Plugin v3.0.0-beta.0 by Matteo Bruni */
@@ -1,9 +1,9 @@
1
1
  import type { InfectableContainer, InfectableParticle } from "./Types";
2
2
  export declare class Infecter {
3
- private readonly container;
3
+ private readonly _container;
4
4
  constructor(container: InfectableContainer);
5
5
  startInfection(particle: InfectableParticle, stage: number): void;
6
6
  updateInfection(particle: InfectableParticle, delta: number): void;
7
7
  updateInfectionStage(particle: InfectableParticle, stage: number): void;
8
- private nextInfectionStage;
8
+ private readonly _nextInfectionStage;
9
9
  }
@@ -1,7 +1,7 @@
1
- import type { IContainerPlugin, IOptionsColor, Particle } from "@tsparticles/engine";
1
+ import { type IContainerPlugin, type IOptionsColor, type Particle } from "@tsparticles/engine";
2
2
  import type { InfectableContainer, InfectableParticle } from "./Types";
3
3
  export declare class InfectionInstance implements IContainerPlugin {
4
- private readonly container;
4
+ private readonly _container;
5
5
  constructor(container: InfectableContainer);
6
6
  particleFillColor(particle: InfectableParticle): string | IOptionsColor | undefined;
7
7
  particleStrokeColor(particle: Particle): string | IOptionsColor | undefined;
@@ -1,6 +1,5 @@
1
- import type { IOptionLoader, RecursivePartial } from "@tsparticles/engine";
1
+ import { type IOptionLoader, OptionsColor, type RecursivePartial } from "@tsparticles/engine";
2
2
  import type { IInfectionStage } from "../Interfaces/IInfectionStage";
3
- import { OptionsColor } from "@tsparticles/engine";
4
3
  export declare class InfectionStage implements IInfectionStage, IOptionLoader<IInfectionStage> {
5
4
  color: OptionsColor;
6
5
  duration?: number;
@@ -1,6 +1,5 @@
1
+ import { type IDelta, ParticlesInteractorBase } from "@tsparticles/engine";
1
2
  import type { InfectableContainer, InfectableParticle } from "./Types";
2
- import { ParticlesInteractorBase } from "@tsparticles/engine";
3
- import type { IDelta } from "@tsparticles/engine";
4
3
  export declare class ParticlesInfecter extends ParticlesInteractorBase<InfectableContainer> {
5
4
  constructor(container: InfectableContainer);
6
5
  clear(): void;
package/types/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { Engine } from "@tsparticles/engine";
2
- export declare function loadInfectionPlugin(engine: Engine): Promise<void>;
2
+ export declare function loadInfectionPlugin(engine: Engine, refresh?: boolean): Promise<void>;
3
3
  export * from "./Options/Interfaces/IInfection";
4
4
  export * from "./Options/Interfaces/IInfectionStage";
package/umd/Infecter.js CHANGED
@@ -12,99 +12,97 @@
12
12
  exports.Infecter = void 0;
13
13
  class Infecter {
14
14
  constructor(container) {
15
- this.container = container;
15
+ this._nextInfectionStage = (particle) => {
16
+ const infectionOptions = this._container.actualOptions.infection, { infection } = particle;
17
+ if (!infectionOptions || !infection) {
18
+ return;
19
+ }
20
+ const stagesCount = infectionOptions.stages.length;
21
+ if (stagesCount <= 0 || infection.stage === undefined) {
22
+ return;
23
+ }
24
+ infection.time = 0;
25
+ if (stagesCount <= ++infection.stage) {
26
+ if (infectionOptions.cure) {
27
+ delete infection.stage;
28
+ delete infection.time;
29
+ return;
30
+ }
31
+ else {
32
+ infection.stage = 0;
33
+ infection.time = 0;
34
+ }
35
+ }
36
+ };
37
+ this._container = container;
16
38
  }
17
39
  startInfection(particle, stage) {
18
- const options = this.container.actualOptions;
19
- if (!options.infection || !particle.infection) {
40
+ const infectionOptions = this._container.actualOptions.infection, { infection } = particle;
41
+ if (!infectionOptions || !infection) {
20
42
  return;
21
43
  }
22
- const stages = options.infection.stages, stagesCount = stages.length;
44
+ const stages = infectionOptions.stages, stagesCount = stages.length;
23
45
  if (stage > stagesCount || stage < 0) {
24
46
  return;
25
47
  }
26
- particle.infection.delay = 0;
27
- particle.infection.delayStage = stage;
48
+ infection.delay = 0;
49
+ infection.delayStage = stage;
28
50
  }
29
51
  updateInfection(particle, delta) {
30
- const infection = this.container.actualOptions.infection;
31
- if (!infection || !particle.infection) {
52
+ const infectionOptions = this._container.actualOptions.infection, { infection } = particle;
53
+ if (!infectionOptions || !infection) {
32
54
  return;
33
55
  }
34
- const stages = infection.stages, stagesCount = stages.length;
35
- if (particle.infection.delay !== undefined && particle.infection.delayStage !== undefined) {
36
- const stage = particle.infection.delayStage;
56
+ const stages = infectionOptions.stages, stagesCount = stages.length;
57
+ if (infection.delay !== undefined && infection.delayStage !== undefined) {
58
+ const stage = infection.delayStage;
37
59
  if (stage > stagesCount || stage < 0) {
38
60
  return;
39
61
  }
40
- if (particle.infection.delay >= infection.delay * 1000) {
41
- particle.infection.stage = stage;
42
- particle.infection.time = 0;
43
- delete particle.infection.delay;
44
- delete particle.infection.delayStage;
62
+ if (infection.delay >= infectionOptions.delay * 1000) {
63
+ infection.stage = stage;
64
+ infection.time = 0;
65
+ delete infection.delay;
66
+ delete infection.delayStage;
45
67
  }
46
68
  else {
47
- particle.infection.delay += delta;
69
+ infection.delay += delta;
48
70
  }
49
71
  }
50
72
  else {
51
- delete particle.infection.delay;
52
- delete particle.infection.delayStage;
73
+ delete infection.delay;
74
+ delete infection.delayStage;
53
75
  }
54
- if (particle.infection.stage !== undefined && particle.infection.time !== undefined) {
55
- const infectionStage = stages[particle.infection.stage];
76
+ if (infection.stage !== undefined && infection.time !== undefined) {
77
+ const infectionStage = stages[infection.stage];
56
78
  if (infectionStage.duration !== undefined && infectionStage.duration >= 0) {
57
- if (particle.infection.time > infectionStage.duration * 1000) {
58
- this.nextInfectionStage(particle);
79
+ if (infection.time > infectionStage.duration * 1000) {
80
+ this._nextInfectionStage(particle);
59
81
  }
60
82
  else {
61
- particle.infection.time += delta;
83
+ infection.time += delta;
62
84
  }
63
85
  }
64
86
  else {
65
- particle.infection.time += delta;
87
+ infection.time += delta;
66
88
  }
67
89
  }
68
90
  else {
69
- delete particle.infection.stage;
70
- delete particle.infection.time;
91
+ delete infection.stage;
92
+ delete infection.time;
71
93
  }
72
94
  }
73
95
  updateInfectionStage(particle, stage) {
74
- const options = this.container.actualOptions;
75
- if (!options.infection || !particle.infection) {
96
+ const options = this._container.actualOptions, { infection } = particle;
97
+ if (!options.infection || !infection) {
76
98
  return;
77
99
  }
78
100
  const stagesCount = options.infection.stages.length;
79
- if (stage > stagesCount ||
80
- stage < 0 ||
81
- (particle.infection.stage !== undefined && particle.infection.stage > stage)) {
101
+ if (stage > stagesCount || stage < 0 || (infection.stage !== undefined && infection.stage > stage)) {
82
102
  return;
83
103
  }
84
- particle.infection.stage = stage;
85
- particle.infection.time = 0;
86
- }
87
- nextInfectionStage(particle) {
88
- const options = this.container.actualOptions;
89
- if (!options.infection || !particle.infection) {
90
- return;
91
- }
92
- const stagesCount = options.infection.stages.length;
93
- if (stagesCount <= 0 || particle.infection.stage === undefined) {
94
- return;
95
- }
96
- particle.infection.time = 0;
97
- if (stagesCount <= ++particle.infection.stage) {
98
- if (options.infection.cure) {
99
- delete particle.infection.stage;
100
- delete particle.infection.time;
101
- return;
102
- }
103
- else {
104
- particle.infection.stage = 0;
105
- particle.infection.time = 0;
106
- }
107
- }
104
+ infection.stage = stage;
105
+ infection.time = 0;
108
106
  }
109
107
  }
110
108
  exports.Infecter = Infecter;
@@ -4,21 +4,21 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./Infecter", "@tsparticles/engine"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./Infecter"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.InfectionInstance = void 0;
13
- const Infecter_1 = require("./Infecter");
14
13
  const engine_1 = require("@tsparticles/engine");
14
+ const Infecter_1 = require("./Infecter");
15
15
  class InfectionInstance {
16
16
  constructor(container) {
17
- this.container = container;
18
- this.container.infecter = new Infecter_1.Infecter(this.container);
17
+ this._container = container;
18
+ this._container.infecter = new Infecter_1.Infecter(this._container);
19
19
  }
20
20
  particleFillColor(particle) {
21
- const options = this.container.actualOptions;
21
+ const options = this._container.actualOptions;
22
22
  if (!particle.infection || !options.infection) {
23
23
  return;
24
24
  }
@@ -29,13 +29,12 @@
29
29
  return this.particleFillColor(particle);
30
30
  }
31
31
  particlesSetup() {
32
- var _a;
33
- const options = this.container.actualOptions;
32
+ const options = this._container.actualOptions;
34
33
  if (!options.infection) {
35
34
  return;
36
35
  }
37
36
  for (let i = 0; i < options.infection.infections; i++) {
38
- const notInfected = this.container.particles.array.filter((p) => {
37
+ const notInfected = this._container.particles.filter((p) => {
39
38
  const infP = p;
40
39
  if (!infP.infection) {
41
40
  infP.infection = {};
@@ -43,7 +42,7 @@
43
42
  return infP.infection.stage === undefined;
44
43
  });
45
44
  const infected = (0, engine_1.itemFromArray)(notInfected);
46
- (_a = this.container.infecter) === null || _a === void 0 ? void 0 : _a.startInfection(infected, 0);
45
+ this._container.infecter?.startInfection(infected, 0);
47
46
  }
48
47
  }
49
48
  }