@tsparticles/plugin-infection 3.0.0-alpha.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.
Files changed (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/Infecter.js +97 -0
  4. package/browser/InfectionInstance.js +37 -0
  5. package/browser/Options/Classes/Infection.js +35 -0
  6. package/browser/Options/Classes/InfectionStage.js +25 -0
  7. package/browser/Options/Interfaces/IInfection.js +1 -0
  8. package/browser/Options/Interfaces/IInfectionStage.js +1 -0
  9. package/browser/ParticlesInfecter.js +54 -0
  10. package/browser/Types.js +1 -0
  11. package/browser/index.js +32 -0
  12. package/cjs/Infecter.js +101 -0
  13. package/cjs/InfectionInstance.js +41 -0
  14. package/cjs/Options/Classes/Infection.js +39 -0
  15. package/cjs/Options/Classes/InfectionStage.js +29 -0
  16. package/cjs/Options/Interfaces/IInfection.js +2 -0
  17. package/cjs/Options/Interfaces/IInfectionStage.js +2 -0
  18. package/cjs/ParticlesInfecter.js +69 -0
  19. package/cjs/Types.js +2 -0
  20. package/cjs/index.js +61 -0
  21. package/esm/Infecter.js +97 -0
  22. package/esm/InfectionInstance.js +37 -0
  23. package/esm/Options/Classes/Infection.js +35 -0
  24. package/esm/Options/Classes/InfectionStage.js +25 -0
  25. package/esm/Options/Interfaces/IInfection.js +1 -0
  26. package/esm/Options/Interfaces/IInfectionStage.js +1 -0
  27. package/esm/ParticlesInfecter.js +54 -0
  28. package/esm/Types.js +1 -0
  29. package/esm/index.js +32 -0
  30. package/package.json +91 -0
  31. package/report.html +39 -0
  32. package/tsparticles.plugin.infection.js +387 -0
  33. package/tsparticles.plugin.infection.min.js +2 -0
  34. package/tsparticles.plugin.infection.min.js.LICENSE.txt +8 -0
  35. package/types/Infecter.d.ts +9 -0
  36. package/types/InfectionInstance.d.ts +9 -0
  37. package/types/Options/Classes/Infection.d.ts +12 -0
  38. package/types/Options/Classes/InfectionStage.d.ts +12 -0
  39. package/types/Options/Interfaces/IInfection.d.ts +8 -0
  40. package/types/Options/Interfaces/IInfectionStage.d.ts +8 -0
  41. package/types/ParticlesInfecter.d.ts +11 -0
  42. package/types/Types.d.ts +24 -0
  43. package/types/index.d.ts +4 -0
  44. package/umd/Infecter.js +111 -0
  45. package/umd/InfectionInstance.js +51 -0
  46. package/umd/Options/Classes/Infection.js +49 -0
  47. package/umd/Options/Classes/InfectionStage.js +39 -0
  48. package/umd/Options/Interfaces/IInfection.js +12 -0
  49. package/umd/Options/Interfaces/IInfectionStage.js +12 -0
  50. package/umd/ParticlesInfecter.js +68 -0
  51. package/umd/Types.js +12 -0
  52. package/umd/index.js +60 -0
package/cjs/Types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/index.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18
+ return new (P || (P = Promise))(function (resolve, reject) {
19
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
23
+ });
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.loadInfectionPlugin = void 0;
27
+ const Infection_1 = require("./Options/Classes/Infection");
28
+ const InfectionInstance_1 = require("./InfectionInstance");
29
+ const ParticlesInfecter_1 = require("./ParticlesInfecter");
30
+ class InfectionPlugin {
31
+ constructor() {
32
+ this.id = "infection";
33
+ }
34
+ getPlugin(container) {
35
+ return new InfectionInstance_1.InfectionInstance(container);
36
+ }
37
+ loadOptions(options, source) {
38
+ if (!this.needsPlugin(source)) {
39
+ return;
40
+ }
41
+ let infectionOptions = options.infection;
42
+ if ((infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.load) === undefined) {
43
+ options.infection = infectionOptions = new Infection_1.Infection();
44
+ }
45
+ infectionOptions.load(source === null || source === void 0 ? void 0 : source.infection);
46
+ }
47
+ needsPlugin(options) {
48
+ var _a, _b;
49
+ 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;
50
+ }
51
+ }
52
+ function loadInfectionPlugin(engine) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const plugin = new InfectionPlugin();
55
+ yield engine.addPlugin(plugin);
56
+ yield engine.addInteractor("particlesInfection", (container) => new ParticlesInfecter_1.ParticlesInfecter(container));
57
+ });
58
+ }
59
+ exports.loadInfectionPlugin = loadInfectionPlugin;
60
+ __exportStar(require("./Options/Interfaces/IInfection"), exports);
61
+ __exportStar(require("./Options/Interfaces/IInfectionStage"), exports);
@@ -0,0 +1,97 @@
1
+ export class Infecter {
2
+ constructor(container) {
3
+ this.container = container;
4
+ }
5
+ startInfection(particle, stage) {
6
+ const options = this.container.actualOptions;
7
+ if (!options.infection || !particle.infection) {
8
+ return;
9
+ }
10
+ const stages = options.infection.stages, stagesCount = stages.length;
11
+ if (stage > stagesCount || stage < 0) {
12
+ return;
13
+ }
14
+ particle.infection.delay = 0;
15
+ particle.infection.delayStage = stage;
16
+ }
17
+ updateInfection(particle, delta) {
18
+ const infection = this.container.actualOptions.infection;
19
+ if (!infection || !particle.infection) {
20
+ return;
21
+ }
22
+ const stages = infection.stages, stagesCount = stages.length;
23
+ if (particle.infection.delay !== undefined && particle.infection.delayStage !== undefined) {
24
+ const stage = particle.infection.delayStage;
25
+ if (stage > stagesCount || stage < 0) {
26
+ return;
27
+ }
28
+ if (particle.infection.delay >= infection.delay * 1000) {
29
+ particle.infection.stage = stage;
30
+ particle.infection.time = 0;
31
+ delete particle.infection.delay;
32
+ delete particle.infection.delayStage;
33
+ }
34
+ else {
35
+ particle.infection.delay += delta;
36
+ }
37
+ }
38
+ else {
39
+ delete particle.infection.delay;
40
+ delete particle.infection.delayStage;
41
+ }
42
+ if (particle.infection.stage !== undefined && particle.infection.time !== undefined) {
43
+ const infectionStage = stages[particle.infection.stage];
44
+ if (infectionStage.duration !== undefined && infectionStage.duration >= 0) {
45
+ if (particle.infection.time > infectionStage.duration * 1000) {
46
+ this.nextInfectionStage(particle);
47
+ }
48
+ else {
49
+ particle.infection.time += delta;
50
+ }
51
+ }
52
+ else {
53
+ particle.infection.time += delta;
54
+ }
55
+ }
56
+ else {
57
+ delete particle.infection.stage;
58
+ delete particle.infection.time;
59
+ }
60
+ }
61
+ updateInfectionStage(particle, stage) {
62
+ const options = this.container.actualOptions;
63
+ if (!options.infection || !particle.infection) {
64
+ return;
65
+ }
66
+ const stagesCount = options.infection.stages.length;
67
+ if (stage > stagesCount ||
68
+ stage < 0 ||
69
+ (particle.infection.stage !== undefined && particle.infection.stage > stage)) {
70
+ return;
71
+ }
72
+ particle.infection.stage = stage;
73
+ particle.infection.time = 0;
74
+ }
75
+ nextInfectionStage(particle) {
76
+ const options = this.container.actualOptions;
77
+ if (!options.infection || !particle.infection) {
78
+ return;
79
+ }
80
+ const stagesCount = options.infection.stages.length;
81
+ if (stagesCount <= 0 || particle.infection.stage === undefined) {
82
+ return;
83
+ }
84
+ particle.infection.time = 0;
85
+ if (stagesCount <= ++particle.infection.stage) {
86
+ if (options.infection.cure) {
87
+ delete particle.infection.stage;
88
+ delete particle.infection.time;
89
+ return;
90
+ }
91
+ else {
92
+ particle.infection.stage = 0;
93
+ particle.infection.time = 0;
94
+ }
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,37 @@
1
+ import { Infecter } from "./Infecter";
2
+ import { itemFromArray } from "@tsparticles/engine";
3
+ export class InfectionInstance {
4
+ constructor(container) {
5
+ this.container = container;
6
+ this.container.infecter = new Infecter(this.container);
7
+ }
8
+ particleFillColor(particle) {
9
+ const options = this.container.actualOptions;
10
+ if (!particle.infection || !options.infection) {
11
+ return;
12
+ }
13
+ const infectionStage = particle.infection.stage, infection = options.infection, infectionStages = infection.stages;
14
+ return infectionStage !== undefined ? infectionStages[infectionStage].color : undefined;
15
+ }
16
+ particleStrokeColor(particle) {
17
+ return this.particleFillColor(particle);
18
+ }
19
+ particlesSetup() {
20
+ var _a;
21
+ const options = this.container.actualOptions;
22
+ if (!options.infection) {
23
+ return;
24
+ }
25
+ for (let i = 0; i < options.infection.infections; i++) {
26
+ const notInfected = this.container.particles.array.filter((p) => {
27
+ const infP = p;
28
+ if (!infP.infection) {
29
+ infP.infection = {};
30
+ }
31
+ return infP.infection.stage === undefined;
32
+ });
33
+ const infected = itemFromArray(notInfected);
34
+ (_a = this.container.infecter) === null || _a === void 0 ? void 0 : _a.startInfection(infected, 0);
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,35 @@
1
+ import { InfectionStage } from "./InfectionStage";
2
+ export class Infection {
3
+ constructor() {
4
+ this.cure = false;
5
+ this.delay = 0;
6
+ this.enable = false;
7
+ this.infections = 0;
8
+ this.stages = [];
9
+ }
10
+ load(data) {
11
+ if (data === undefined) {
12
+ return;
13
+ }
14
+ if (data.cure !== undefined) {
15
+ this.cure = data.cure;
16
+ }
17
+ if (data.delay !== undefined) {
18
+ this.delay = data.delay;
19
+ }
20
+ if (data.enable !== undefined) {
21
+ this.enable = data.enable;
22
+ }
23
+ if (data.infections !== undefined) {
24
+ this.infections = data.infections;
25
+ }
26
+ if (data.stages === undefined) {
27
+ return;
28
+ }
29
+ this.stages = data.stages.map((t) => {
30
+ const s = new InfectionStage();
31
+ s.load(t);
32
+ return s;
33
+ });
34
+ }
35
+ }
@@ -0,0 +1,25 @@
1
+ import { OptionsColor } from "@tsparticles/engine";
2
+ export class InfectionStage {
3
+ constructor() {
4
+ this.color = new OptionsColor();
5
+ this.color.value = "#ff0000";
6
+ this.radius = 0;
7
+ this.rate = 1;
8
+ }
9
+ load(data) {
10
+ if (data === undefined) {
11
+ return;
12
+ }
13
+ if (data.color !== undefined) {
14
+ this.color = OptionsColor.create(this.color, data.color);
15
+ }
16
+ this.duration = data.duration;
17
+ this.infectedStage = data.infectedStage;
18
+ if (data.radius !== undefined) {
19
+ this.radius = data.radius;
20
+ }
21
+ if (data.rate !== undefined) {
22
+ this.rate = data.rate;
23
+ }
24
+ }
25
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,54 @@
1
+ import { ParticlesInteractorBase, getRandom } from "@tsparticles/engine";
2
+ export class ParticlesInfecter extends ParticlesInteractorBase {
3
+ constructor(container) {
4
+ super(container);
5
+ }
6
+ clear() {
7
+ }
8
+ init() {
9
+ }
10
+ async interact(p1, delta) {
11
+ var _a, _b, _c, _d, _e;
12
+ const infecter = this.container.infecter;
13
+ if (!infecter) {
14
+ return;
15
+ }
16
+ infecter.updateInfection(p1, delta.value);
17
+ if (((_a = p1.infection) === null || _a === void 0 ? void 0 : _a.stage) === undefined) {
18
+ return;
19
+ }
20
+ const container = this.container, options = container.actualOptions, infectionOptions = options.infection;
21
+ if (!(infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.enable) || infectionOptions.stages.length < 1) {
22
+ return;
23
+ }
24
+ const infectionStage1 = infectionOptions.stages[p1.infection.stage], pxRatio = container.retina.pixelRatio, radius = p1.getRadius() * 2 + infectionStage1.radius * pxRatio, pos = p1.getPosition(), infectedStage1 = (_b = infectionStage1.infectedStage) !== null && _b !== void 0 ? _b : p1.infection.stage, query = container.particles.quadTree.queryCircle(pos, radius), infections = infectionStage1.rate, neighbors = query.length;
25
+ for (const p2 of query) {
26
+ const infP2 = p2;
27
+ if (infP2 === p1 ||
28
+ infP2.destroyed ||
29
+ infP2.spawning ||
30
+ !(((_c = infP2.infection) === null || _c === void 0 ? void 0 : _c.stage) === undefined || infP2.infection.stage !== p1.infection.stage)) {
31
+ continue;
32
+ }
33
+ if (getRandom() < infections / neighbors) {
34
+ if (((_d = infP2.infection) === null || _d === void 0 ? void 0 : _d.stage) === undefined) {
35
+ infecter.startInfection(infP2, infectedStage1);
36
+ }
37
+ else if (infP2.infection.stage < p1.infection.stage) {
38
+ infecter.updateInfectionStage(infP2, infectedStage1);
39
+ }
40
+ else if (infP2.infection.stage > p1.infection.stage) {
41
+ const infectionStage2 = infectionOptions.stages[infP2.infection.stage];
42
+ const infectedStage2 = (_e = infectionStage2 === null || infectionStage2 === void 0 ? void 0 : infectionStage2.infectedStage) !== null && _e !== void 0 ? _e : infP2.infection.stage;
43
+ infecter.updateInfectionStage(p1, infectedStage2);
44
+ }
45
+ }
46
+ }
47
+ }
48
+ isEnabled() {
49
+ var _a, _b, _c;
50
+ 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;
51
+ }
52
+ reset() {
53
+ }
54
+ }
package/esm/Types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/esm/index.js ADDED
@@ -0,0 +1,32 @@
1
+ import { Infection } from "./Options/Classes/Infection";
2
+ import { InfectionInstance } from "./InfectionInstance";
3
+ import { ParticlesInfecter } from "./ParticlesInfecter";
4
+ class InfectionPlugin {
5
+ constructor() {
6
+ this.id = "infection";
7
+ }
8
+ getPlugin(container) {
9
+ return new InfectionInstance(container);
10
+ }
11
+ loadOptions(options, source) {
12
+ if (!this.needsPlugin(source)) {
13
+ return;
14
+ }
15
+ let infectionOptions = options.infection;
16
+ if ((infectionOptions === null || infectionOptions === void 0 ? void 0 : infectionOptions.load) === undefined) {
17
+ options.infection = infectionOptions = new Infection();
18
+ }
19
+ infectionOptions.load(source === null || source === void 0 ? void 0 : source.infection);
20
+ }
21
+ needsPlugin(options) {
22
+ var _a, _b;
23
+ 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;
24
+ }
25
+ }
26
+ export async function loadInfectionPlugin(engine) {
27
+ const plugin = new InfectionPlugin();
28
+ await engine.addPlugin(plugin);
29
+ await engine.addInteractor("particlesInfection", (container) => new ParticlesInfecter(container));
30
+ }
31
+ export * from "./Options/Interfaces/IInfection";
32
+ export * from "./Options/Interfaces/IInfectionStage";
package/package.json ADDED
@@ -0,0 +1,91 @@
1
+ {
2
+ "name": "@tsparticles/plugin-infection",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "tsParticles infection plugin",
5
+ "homepage": "https://particles.js.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/matteobruni/tsparticles.git",
9
+ "directory": "plugins/infection"
10
+ },
11
+ "keywords": [
12
+ "front-end",
13
+ "frontend",
14
+ "tsparticles",
15
+ "particles.js",
16
+ "particlesjs",
17
+ "particles",
18
+ "particle",
19
+ "canvas",
20
+ "jsparticles",
21
+ "xparticles",
22
+ "particles-js",
23
+ "particles-bg",
24
+ "particles-bg-vue",
25
+ "particles-ts",
26
+ "particles.ts",
27
+ "react-particles-js",
28
+ "react-particles.js",
29
+ "react-particles",
30
+ "react",
31
+ "reactjs",
32
+ "vue-particles",
33
+ "ngx-particles",
34
+ "angular-particles",
35
+ "particleground",
36
+ "vue",
37
+ "vuejs",
38
+ "preact",
39
+ "preactjs",
40
+ "jquery",
41
+ "angularjs",
42
+ "angular",
43
+ "typescript",
44
+ "javascript",
45
+ "animation",
46
+ "web",
47
+ "html5",
48
+ "web-design",
49
+ "webdesign",
50
+ "css",
51
+ "html",
52
+ "css3",
53
+ "animated",
54
+ "background",
55
+ "confetti",
56
+ "canvas",
57
+ "fireworks",
58
+ "fireworks-js",
59
+ "confetti-js",
60
+ "confettijs",
61
+ "fireworksjs",
62
+ "canvas-confetti",
63
+ "@tsparticles/plugin"
64
+ ],
65
+ "author": "Matteo Bruni <matteo.bruni@me.com>",
66
+ "license": "MIT",
67
+ "bugs": {
68
+ "url": "https://github.com/matteobruni/tsparticles/issues"
69
+ },
70
+ "funding": [
71
+ {
72
+ "type": "github",
73
+ "url": "https://github.com/sponsors/matteobruni"
74
+ },
75
+ {
76
+ "type": "buymeacoffee",
77
+ "url": "https://www.buymeacoffee.com/matteobruni"
78
+ }
79
+ ],
80
+ "main": "cjs/index.js",
81
+ "jsdelivr": "tsparticles.plugin.infection.min.js",
82
+ "unpkg": "tsparticles.plugin.infection.min.js",
83
+ "module": "esm/index.js",
84
+ "types": "types/index.d.ts",
85
+ "publishConfig": {
86
+ "access": "public"
87
+ },
88
+ "dependencies": {
89
+ "@tsparticles/engine": "^3.0.0-alpha.0"
90
+ }
91
+ }