@tsparticles/plugin-poisson-disc 3.1.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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +74 -0
  3. package/browser/Interfaces/IPoissonPoint.js +1 -0
  4. package/browser/Options/Classes/Poisson.js +26 -0
  5. package/browser/Options/Interfaces/IPoisson.js +1 -0
  6. package/browser/PoissonDisc.js +138 -0
  7. package/browser/PoissonInstance.js +55 -0
  8. package/browser/index.js +27 -0
  9. package/browser/package.json +1 -0
  10. package/browser/types.js +1 -0
  11. package/cjs/Interfaces/IPoissonPoint.js +2 -0
  12. package/cjs/Options/Classes/Poisson.js +30 -0
  13. package/cjs/Options/Interfaces/IPoisson.js +2 -0
  14. package/cjs/PoissonDisc.js +142 -0
  15. package/cjs/PoissonInstance.js +59 -0
  16. package/cjs/index.js +31 -0
  17. package/cjs/package.json +1 -0
  18. package/cjs/types.js +2 -0
  19. package/esm/Interfaces/IPoissonPoint.js +1 -0
  20. package/esm/Options/Classes/Poisson.js +26 -0
  21. package/esm/Options/Interfaces/IPoisson.js +1 -0
  22. package/esm/PoissonDisc.js +138 -0
  23. package/esm/PoissonInstance.js +55 -0
  24. package/esm/index.js +27 -0
  25. package/esm/package.json +1 -0
  26. package/esm/types.js +1 -0
  27. package/package.json +93 -0
  28. package/report.html +39 -0
  29. package/tsparticles.plugin.poisson.js +366 -0
  30. package/tsparticles.plugin.poisson.min.js +2 -0
  31. package/tsparticles.plugin.poisson.min.js.LICENSE.txt +1 -0
  32. package/types/Interfaces/IPoissonPoint.d.ts +5 -0
  33. package/types/Options/Classes/Poisson.d.ts +11 -0
  34. package/types/Options/Interfaces/IPoisson.d.ts +7 -0
  35. package/types/PoissonDisc.d.ts +27 -0
  36. package/types/PoissonInstance.d.ts +16 -0
  37. package/types/index.d.ts +2 -0
  38. package/types/types.d.ts +12 -0
  39. package/umd/Interfaces/IPoissonPoint.js +12 -0
  40. package/umd/Options/Classes/Poisson.js +40 -0
  41. package/umd/Options/Interfaces/IPoisson.js +12 -0
  42. package/umd/PoissonDisc.js +152 -0
  43. package/umd/PoissonInstance.js +69 -0
  44. package/umd/index.js +41 -0
  45. package/umd/types.js +12 -0
@@ -0,0 +1,152 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "@tsparticles/engine"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PoissonDisc = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const double = 2, doublePI = Math.PI * double;
15
+ class PoissonDisc {
16
+ constructor(size, radius, retries, dimensions, firstPoint) {
17
+ this.size = { ...size };
18
+ this.radius = radius;
19
+ this.retries = retries;
20
+ this.dimensions = dimensions;
21
+ this.cellSize = Math.floor(this.radius / Math.sqrt(this.dimensions));
22
+ this.cols = Math.floor(this.size.width / this.cellSize);
23
+ this.rows = Math.floor(this.size.height / this.cellSize);
24
+ this.points = [];
25
+ this.active = [];
26
+ this.grid = [];
27
+ this.firstPoint = firstPoint ? { ...firstPoint } : undefined;
28
+ this.reset();
29
+ }
30
+ addPoint(inputPoint) {
31
+ const point = {
32
+ position: { ...inputPoint },
33
+ gridPosition: {
34
+ x: Math.floor(inputPoint.x / this.cellSize),
35
+ y: Math.floor(inputPoint.y / this.cellSize),
36
+ },
37
+ }, pointIndex = this.points.length;
38
+ this.points.push(point);
39
+ this.grid[point.gridPosition.y][point.gridPosition.x] = pointIndex;
40
+ this.active.push(pointIndex);
41
+ }
42
+ getRandom(min, max) {
43
+ return Math.floor((0, engine_1.getRandom)() * (max - min)) + min;
44
+ }
45
+ initialiseGrid() {
46
+ for (let y = 0; y <= this.rows; y++) {
47
+ this.grid[y] = [];
48
+ for (let x = 0; x <= this.cols; x++) {
49
+ this.grid[y][x] = -1;
50
+ }
51
+ }
52
+ }
53
+ reset() {
54
+ this.points = [];
55
+ this.active = [];
56
+ this.grid = [];
57
+ this.initialiseGrid();
58
+ if (this.firstPoint) {
59
+ this.addPoint(this.firstPoint);
60
+ }
61
+ else {
62
+ const minCoordinate = 0;
63
+ this.addPoint({
64
+ x: this.getRandom(minCoordinate, this.size.width),
65
+ y: this.getRandom(minCoordinate, this.size.height),
66
+ });
67
+ }
68
+ }
69
+ async run() {
70
+ this.reset();
71
+ const minCount = 0, step = 1;
72
+ while (this.active.length > minCount) {
73
+ await this.steps(step);
74
+ }
75
+ }
76
+ async steps(steps) {
77
+ const minCount = 0;
78
+ for (let i = 0; i < steps; i++) {
79
+ if (this.active.length <= minCount) {
80
+ continue;
81
+ }
82
+ await this._step();
83
+ }
84
+ }
85
+ _getNewPoint(currentPoint, tries) {
86
+ const minCoordinate = 0, gridMinValue = 0, maxNeighbourIndex = 1, newAngle = tries * (doublePI / this.retries), newDist = this.getRandom(this.radius, this.radius * double), offset = {
87
+ x: Math.cos(newAngle) * newDist,
88
+ y: Math.sin(newAngle) * newDist,
89
+ }, newPoint = {
90
+ x: Math.floor(currentPoint.position.x + offset.x),
91
+ y: Math.floor(currentPoint.position.y + offset.y),
92
+ }, newGridCoords = {
93
+ x: Math.floor(newPoint.x / this.cellSize),
94
+ y: Math.floor(newPoint.y / this.cellSize),
95
+ };
96
+ if (newPoint.x > minCoordinate &&
97
+ newPoint.x < this.size.width &&
98
+ newPoint.y > minCoordinate &&
99
+ newPoint.y < this.size.height) {
100
+ if (this.grid[newGridCoords.y][newGridCoords.x] < gridMinValue) {
101
+ for (let i = -1; i <= maxNeighbourIndex; i++) {
102
+ for (let j = -1; j <= maxNeighbourIndex; j++) {
103
+ const neighbourGrid = {
104
+ x: newGridCoords.x + j,
105
+ y: newGridCoords.y + i,
106
+ };
107
+ if (neighbourGrid.x >= minCoordinate &&
108
+ neighbourGrid.y >= minCoordinate &&
109
+ neighbourGrid.x < this.cols &&
110
+ neighbourGrid.y < this.rows &&
111
+ (neighbourGrid.x !== newGridCoords.x || neighbourGrid.y !== newGridCoords.y)) {
112
+ if (this.grid[neighbourGrid.y][neighbourGrid.x] >= gridMinValue) {
113
+ const neighbourIndex = this.grid[neighbourGrid.y][neighbourGrid.x], neighbour = this.points[neighbourIndex], dist = (0, engine_1.getDistance)(newPoint, neighbour.position);
114
+ if (dist < this.radius) {
115
+ return;
116
+ }
117
+ }
118
+ }
119
+ }
120
+ }
121
+ }
122
+ else {
123
+ return;
124
+ }
125
+ }
126
+ else {
127
+ return;
128
+ }
129
+ return newPoint;
130
+ }
131
+ async _step() {
132
+ const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
133
+ return new Promise((resolve) => {
134
+ let foundNewPoint = false;
135
+ for (let tries = 0; tries < this.retries; tries++) {
136
+ const newPoint = this._getNewPoint(this.points[this.active[randomActive]], tries);
137
+ if (newPoint) {
138
+ foundNewPoint = true;
139
+ this.addPoint(newPoint);
140
+ break;
141
+ }
142
+ }
143
+ if (!foundNewPoint) {
144
+ const deleteCount = 1;
145
+ this.active.splice(randomActive, deleteCount);
146
+ }
147
+ resolve();
148
+ });
149
+ }
150
+ }
151
+ exports.PoissonDisc = PoissonDisc;
152
+ });
@@ -0,0 +1,69 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "@tsparticles/engine", "./PoissonDisc.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.PoissonInstance = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const PoissonDisc_js_1 = require("./PoissonDisc.js");
15
+ class PoissonInstance {
16
+ constructor(container, engine) {
17
+ this._container = container;
18
+ this._engine = engine;
19
+ this._currentIndex = 0;
20
+ }
21
+ async init() {
22
+ await this._initData();
23
+ }
24
+ particlePosition(position) {
25
+ const container = this._container, options = container.actualOptions.poisson;
26
+ if (!this.poissonDisc || !(options?.enable ?? false) || this._currentIndex >= this.poissonDisc.points.length) {
27
+ return;
28
+ }
29
+ return position ?? this.poissonDisc?.points[this._currentIndex++]?.position;
30
+ }
31
+ resize() {
32
+ const container = this._container, options = container.actualOptions.poisson;
33
+ if (!(options?.enable ?? false)) {
34
+ return;
35
+ }
36
+ if (this.redrawTimeout) {
37
+ clearTimeout(this.redrawTimeout);
38
+ }
39
+ const timeout = 250;
40
+ this.redrawTimeout = window.setTimeout(() => {
41
+ void (async () => {
42
+ await this._initData();
43
+ await container.particles.redraw();
44
+ })();
45
+ }, timeout);
46
+ }
47
+ stop() {
48
+ delete this.poissonDisc;
49
+ }
50
+ async _initData() {
51
+ const container = this._container, poissonOptions = container.actualOptions.poisson, particlesOptions = container.actualOptions.particles, canvasSize = container.canvas.size, pixelRatio = container.retina.pixelRatio;
52
+ if (!poissonOptions?.enable) {
53
+ return;
54
+ }
55
+ this._currentIndex = 0;
56
+ this.poissonDisc = new PoissonDisc_js_1.PoissonDisc(canvasSize, poissonOptions.radius
57
+ ? poissonOptions.radius * pixelRatio
58
+ : Math.max((0, engine_1.getRangeMax)(particlesOptions.size.value) * pixelRatio, Math.sqrt((canvasSize.width * canvasSize.height) / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);
59
+ const noSteps = 0;
60
+ if (poissonOptions.steps > noSteps) {
61
+ await this.poissonDisc.steps(poissonOptions.steps);
62
+ }
63
+ else {
64
+ await this.poissonDisc.run();
65
+ }
66
+ }
67
+ }
68
+ exports.PoissonInstance = PoissonInstance;
69
+ });
package/umd/index.js ADDED
@@ -0,0 +1,41 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports", "./Options/Classes/Poisson.js", "./PoissonInstance.js"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.loadPoissonDiscPlugin = void 0;
13
+ const Poisson_js_1 = require("./Options/Classes/Poisson.js");
14
+ const PoissonInstance_js_1 = require("./PoissonInstance.js");
15
+ class PoissonDiscPlugin {
16
+ constructor(engine) {
17
+ this.id = "poisson";
18
+ this._engine = engine;
19
+ }
20
+ getPlugin(container) {
21
+ return new PoissonInstance_js_1.PoissonInstance(container, this._engine);
22
+ }
23
+ loadOptions(options, source) {
24
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
25
+ return;
26
+ }
27
+ let poissonOptions = options.poisson;
28
+ if (poissonOptions?.load === undefined) {
29
+ options.poisson = poissonOptions = new Poisson_js_1.Poisson();
30
+ }
31
+ poissonOptions.load(source?.poisson);
32
+ }
33
+ needsPlugin(options) {
34
+ return options?.poisson?.enable ?? false;
35
+ }
36
+ }
37
+ async function loadPoissonDiscPlugin(engine, refresh = true) {
38
+ await engine.addPlugin(new PoissonDiscPlugin(engine), refresh);
39
+ }
40
+ exports.loadPoissonDiscPlugin = loadPoissonDiscPlugin;
41
+ });
package/umd/types.js ADDED
@@ -0,0 +1,12 @@
1
+ (function (factory) {
2
+ if (typeof module === "object" && typeof module.exports === "object") {
3
+ var v = factory(require, exports);
4
+ if (v !== undefined) module.exports = v;
5
+ }
6
+ else if (typeof define === "function" && define.amd) {
7
+ define(["require", "exports"], factory);
8
+ }
9
+ })(function (require, exports) {
10
+ "use strict";
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ });