@tsparticles/plugin-poisson-disc 4.0.0-alpha.8 → 4.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.
Files changed (38) hide show
  1. package/56.min.js +1 -0
  2. package/960.min.js +1 -0
  3. package/964.min.js +1 -0
  4. package/browser/Options/Classes/Poisson.js +5 -0
  5. package/browser/PoissonDisc.js +79 -67
  6. package/browser/PoissonDiscPlugin.js +1 -3
  7. package/browser/PoissonDiscPluginInstance.js +9 -2
  8. package/browser/index.js +1 -1
  9. package/cjs/Options/Classes/Poisson.js +5 -0
  10. package/cjs/PoissonDisc.js +79 -67
  11. package/cjs/PoissonDiscPlugin.js +1 -3
  12. package/cjs/PoissonDiscPluginInstance.js +9 -2
  13. package/cjs/index.js +1 -1
  14. package/dist_browser_PoissonDiscPluginInstance_js.js +2 -12
  15. package/dist_browser_PoissonDiscPlugin_js.js +3 -3
  16. package/dist_browser_PoissonDisc_js.js +30 -0
  17. package/esm/Options/Classes/Poisson.js +5 -0
  18. package/esm/PoissonDisc.js +79 -67
  19. package/esm/PoissonDiscPlugin.js +1 -3
  20. package/esm/PoissonDiscPluginInstance.js +9 -2
  21. package/esm/index.js +1 -1
  22. package/package.json +2 -2
  23. package/report.html +1 -1
  24. package/tsparticles.plugin.poisson.js +31 -19
  25. package/tsparticles.plugin.poisson.min.js +2 -2
  26. package/types/PoissonDisc.d.ts +1 -1
  27. package/types/PoissonDiscPlugin.d.ts +1 -2
  28. package/types/PoissonDiscPluginInstance.d.ts +1 -1
  29. package/umd/Options/Classes/Poisson.js +5 -0
  30. package/umd/PoissonDisc.js +79 -67
  31. package/umd/PoissonDiscPlugin.js +1 -3
  32. package/umd/PoissonDiscPluginInstance.js +45 -4
  33. package/umd/index.js +1 -1
  34. package/336.min.js +0 -2
  35. package/336.min.js.LICENSE.txt +0 -1
  36. package/879.min.js +0 -2
  37. package/879.min.js.LICENSE.txt +0 -1
  38. package/tsparticles.plugin.poisson.min.js.LICENSE.txt +0 -1
package/56.min.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";(this.webpackChunk_tsparticles_plugin_poisson_disc=this.webpackChunk_tsparticles_plugin_poisson_disc||[]).push([[56],{56(i,t,s){s.d(t,{PoissonDisc:()=>h});var e=s(303);class h{active;cellSize;cols;dimensions;firstPoint;grid;points;radius;retries;rows;size;constructor(i,t,s,e,h){this.size={...i},this.radius=t,this.retries=s,this.dimensions=e,this.cellSize=Math.floor(this.radius/Math.sqrt(this.dimensions)),this.cols=Math.floor(this.size.width/this.cellSize),this.rows=Math.floor(this.size.height/this.cellSize),this.points=[],this.active=[],this.grid=[],this.firstPoint=h?{...h}:void 0,this.reset()}addPoint(i){let t={position:{...i},gridPosition:{x:Math.floor(i.x/this.cellSize),y:Math.floor(i.y/this.cellSize)}},s=this.points.length,e=this.grid[t.gridPosition.y];e&&(this.points.push(t),e[t.gridPosition.x]=s,this.active.push(s))}getRandom(i,t){return Math.floor((0,e.getRandom)()*(t-i))+i}initialiseGrid(){for(let i=0;i<=this.rows;i++){this.grid[i]=[];let t=this.grid[i];if(t)for(let i=0;i<=this.cols;i++)t[i]=-1}}reset(){this.points=[],this.active=[],this.grid=[],this.initialiseGrid(),this.firstPoint?this.addPoint(this.firstPoint):this.addPoint({x:this.getRandom(0,this.size.width),y:this.getRandom(0,this.size.height)})}async run(){this.reset();let i=0;for(;this.active.length>0;)this.steps(1),++i%100==0&&await new Promise(i=>setTimeout(i))}steps(i){for(let t=0;t<i;t++)this.active.length<=0||this._step()}_getNewPoint(i,t){let s=t*(e.doublePI/this.retries),h=this.getRandom(this.radius,this.radius*e.double),o={x:Math.cos(s)*h,y:Math.sin(s)*h},r={x:Math.floor(i.position.x+o.x),y:Math.floor(i.position.y+o.y)},n={x:Math.floor(r.x/this.cellSize),y:Math.floor(r.y/this.cellSize)};if(r.x<=0||r.x>=this.size.width||r.y<=0||r.y>=this.size.height)return;let l=this.grid[n.y];if(!l)return;let a=l[n.x];if(void 0!==a&&!(a>=0)){for(let i=-1;i<=1;i++)for(let t=-1;t<=1;t++){if(!i&&!t)continue;let s={x:n.x+t,y:n.y+i};if(s.x<0||s.y<0||s.x>=this.cols||s.y>=this.rows)continue;let h=this.grid[s.y]?.[s.x];if(void 0===h||h<0)continue;let o=this.points[h];if(o&&(0,e.getDistance)(r,o.position)<this.radius)return}return r}}_step(){let i=this.getRandom(0,this.active.length),t=!1;for(let s=0;s<this.retries;s++){let e=this.active[i];if(void 0===e)continue;let h=this.points[e];if(!h)continue;let o=this._getNewPoint(h,s);if(o){t=!0,this.addPoint(o);break}}t||this.active.splice(i,1)}}}}]);
package/960.min.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";(this.webpackChunk_tsparticles_plugin_poisson_disc=this.webpackChunk_tsparticles_plugin_poisson_disc||[]).push([[960],{960(i,s,t){t.d(s,{PoissonDiscPluginInstance:()=>n});var e=t(303);class n{poissonDisc;redrawTimeout;_container;_currentIndex;constructor(i){this._container=i,this._currentIndex=0}async init(){await this._initData()}particlePosition(i){let s=this._container.actualOptions.poisson;if(this.poissonDisc&&s?.enable&&!(this._currentIndex>=this.poissonDisc.points.length))return i??this.poissonDisc.points[this._currentIndex++]?.position}resize(){let i=this._container,s=i.actualOptions.poisson;s?.enable&&(this.redrawTimeout&&clearTimeout(this.redrawTimeout),this.redrawTimeout=setTimeout(()=>{(async()=>{this._container.destroyed||(await this._initData(),await i.particles.redraw())})()},250))}stop(){delete this.poissonDisc}async _initData(){let i=this._container,s=i.actualOptions.poisson,n=i.actualOptions.particles,a=i.canvas.size,o=i.retina.pixelRatio;if(!s?.enable)return;this._currentIndex=0;let{PoissonDisc:r}=await t.e(56).then(t.bind(t,56));this.poissonDisc=new r(a,s.radius?s.radius*o:Math.max((0,e.getRangeMax)(n.size.value)*o,Math.sqrt(a.width*a.height/n.number.value)),s.retries,s.dimensions),s.steps>0?this.poissonDisc.steps(s.steps):await this.poissonDisc.run()}}}}]);
package/964.min.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";(this.webpackChunk_tsparticles_plugin_poisson_disc=this.webpackChunk_tsparticles_plugin_poisson_disc||[]).push([[964],{964(s,i,e){e.d(i,{PoissonDiscPlugin:()=>o});var n=e(303);class t{dimensions;enable;radius;retries;steps;constructor(){this.enable=!1,this.dimensions=2,this.radius=0,this.retries=30,this.steps=0}load(s){(0,n.isNull)(s)||(void 0!==s.enable&&(this.enable=s.enable),void 0!==s.dimensions&&(this.dimensions=s.dimensions),void 0!==s.radius&&(this.radius=s.radius),void 0!==s.retries&&(this.retries=s.retries))}}class o{id="poisson";async getPlugin(s){let{PoissonDiscPluginInstance:i}=await e.e(960).then(e.bind(e,960));return new i(s)}loadOptions(s,i,e){if(!this.needsPlugin(i)&&!this.needsPlugin(e))return;let n=i.poisson;n?.load===void 0&&(i.poisson=n=new t),n.load(e?.poisson)}needsPlugin(s){return s?.poisson?.enable??!1}}}}]);
@@ -1,5 +1,10 @@
1
1
  import { isNull } from "@tsparticles/engine";
2
2
  export class Poisson {
3
+ dimensions;
4
+ enable;
5
+ radius;
6
+ retries;
7
+ steps;
3
8
  constructor() {
4
9
  this.enable = false;
5
10
  this.dimensions = 2;
@@ -1,5 +1,16 @@
1
1
  import { double, doublePI, getDistance, getRandom } from "@tsparticles/engine";
2
2
  export class PoissonDisc {
3
+ active;
4
+ cellSize;
5
+ cols;
6
+ dimensions;
7
+ firstPoint;
8
+ grid;
9
+ points;
10
+ radius;
11
+ retries;
12
+ rows;
13
+ size;
3
14
  constructor(size, radius, retries, dimensions, firstPoint) {
4
15
  this.size = { ...size };
5
16
  this.radius = radius;
@@ -62,18 +73,22 @@ export class PoissonDisc {
62
73
  }
63
74
  async run() {
64
75
  this.reset();
65
- const minCount = 0, step = 1;
76
+ const minCount = 0, step = 1, yieldEvery = 100, yieldStepModule = 0;
77
+ let iterations = 0;
66
78
  while (this.active.length > minCount) {
67
- await this.steps(step);
79
+ this.steps(step);
80
+ if (++iterations % yieldEvery === yieldStepModule) {
81
+ await new Promise(resolve => setTimeout(resolve));
82
+ }
68
83
  }
69
84
  }
70
- async steps(steps) {
85
+ steps(steps) {
71
86
  const minCount = 0;
72
87
  for (let i = 0; i < steps; i++) {
73
88
  if (this.active.length <= minCount) {
74
89
  continue;
75
90
  }
76
- await this._step();
91
+ this._step();
77
92
  }
78
93
  }
79
94
  _getNewPoint(currentPoint, tries) {
@@ -87,78 +102,75 @@ export class PoissonDisc {
87
102
  x: Math.floor(newPoint.x / this.cellSize),
88
103
  y: Math.floor(newPoint.y / this.cellSize),
89
104
  };
90
- if (newPoint.x > minCoordinate &&
91
- newPoint.x < this.size.width &&
92
- newPoint.y > minCoordinate &&
93
- newPoint.y < this.size.height) {
94
- const row = this.grid[newGridCoords.y];
95
- if (!row) {
96
- return;
97
- }
98
- const point = row[newGridCoords.x];
99
- if (point === undefined) {
100
- return;
101
- }
102
- if (point < gridMinValue) {
103
- for (let i = -1; i <= maxNeighbourIndex; i++) {
104
- for (let j = -1; j <= maxNeighbourIndex; j++) {
105
- const neighbourGrid = {
106
- x: newGridCoords.x + j,
107
- y: newGridCoords.y + i,
108
- };
109
- if (neighbourGrid.x >= minCoordinate &&
110
- neighbourGrid.y >= minCoordinate &&
111
- neighbourGrid.x < this.cols &&
112
- neighbourGrid.y < this.rows &&
113
- (neighbourGrid.x !== newGridCoords.x || neighbourGrid.y !== newGridCoords.y)) {
114
- if (point >= gridMinValue) {
115
- const neighbourIndex = point, neighbour = this.points[neighbourIndex];
116
- if (!neighbour) {
117
- continue;
118
- }
119
- const dist = getDistance(newPoint, neighbour.position);
120
- if (dist < this.radius) {
121
- return;
122
- }
123
- }
124
- }
125
- }
126
- }
127
- }
128
- else {
129
- return;
130
- }
105
+ if (newPoint.x <= minCoordinate ||
106
+ newPoint.x >= this.size.width ||
107
+ newPoint.y <= minCoordinate ||
108
+ newPoint.y >= this.size.height) {
109
+ return;
131
110
  }
132
- else {
111
+ const row = this.grid[newGridCoords.y];
112
+ if (!row) {
133
113
  return;
134
114
  }
135
- return newPoint;
136
- }
137
- async _step() {
138
- const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
139
- return new Promise(resolve => {
140
- let foundNewPoint = false;
141
- for (let tries = 0; tries < this.retries; tries++) {
142
- const randomActivePointIndex = this.active[randomActive];
143
- if (randomActivePointIndex === undefined) {
115
+ const cellValue = row[newGridCoords.x];
116
+ if (cellValue === undefined) {
117
+ return;
118
+ }
119
+ if (cellValue >= gridMinValue) {
120
+ return;
121
+ }
122
+ for (let i = -1; i <= maxNeighbourIndex; i++) {
123
+ for (let j = -1; j <= maxNeighbourIndex; j++) {
124
+ if (!i && !j) {
125
+ continue;
126
+ }
127
+ const neighbourGrid = {
128
+ x: newGridCoords.x + j,
129
+ y: newGridCoords.y + i,
130
+ };
131
+ if (neighbourGrid.x < minCoordinate ||
132
+ neighbourGrid.y < minCoordinate ||
133
+ neighbourGrid.x >= this.cols ||
134
+ neighbourGrid.y >= this.rows) {
135
+ continue;
136
+ }
137
+ const neighbourCellValue = this.grid[neighbourGrid.y]?.[neighbourGrid.x];
138
+ if (neighbourCellValue === undefined || neighbourCellValue < gridMinValue) {
144
139
  continue;
145
140
  }
146
- const point = this.points[randomActivePointIndex];
147
- if (!point) {
141
+ const neighbour = this.points[neighbourCellValue];
142
+ if (!neighbour) {
148
143
  continue;
149
144
  }
150
- const newPoint = this._getNewPoint(point, tries);
151
- if (newPoint) {
152
- foundNewPoint = true;
153
- this.addPoint(newPoint);
154
- break;
145
+ if (getDistance(newPoint, neighbour.position) < this.radius) {
146
+ return;
155
147
  }
156
148
  }
157
- if (!foundNewPoint) {
158
- const deleteCount = 1;
159
- this.active.splice(randomActive, deleteCount);
149
+ }
150
+ return newPoint;
151
+ }
152
+ _step() {
153
+ const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
154
+ let foundNewPoint = false;
155
+ for (let tries = 0; tries < this.retries; tries++) {
156
+ const randomActivePointIndex = this.active[randomActive];
157
+ if (randomActivePointIndex === undefined) {
158
+ continue;
159
+ }
160
+ const point = this.points[randomActivePointIndex];
161
+ if (!point) {
162
+ continue;
160
163
  }
161
- resolve();
162
- });
164
+ const newPoint = this._getNewPoint(point, tries);
165
+ if (newPoint) {
166
+ foundNewPoint = true;
167
+ this.addPoint(newPoint);
168
+ break;
169
+ }
170
+ }
171
+ if (!foundNewPoint) {
172
+ const deleteCount = 1;
173
+ this.active.splice(randomActive, deleteCount);
174
+ }
163
175
  }
164
176
  }
@@ -1,8 +1,6 @@
1
1
  import { Poisson } from "./Options/Classes/Poisson.js";
2
2
  export class PoissonDiscPlugin {
3
- constructor() {
4
- this.id = "poisson";
5
- }
3
+ id = "poisson";
6
4
  async getPlugin(container) {
7
5
  const { PoissonDiscPluginInstance } = await import("./PoissonDiscPluginInstance.js");
8
6
  return new PoissonDiscPluginInstance(container);
@@ -1,6 +1,9 @@
1
1
  import { getRangeMax } from "@tsparticles/engine";
2
- import { PoissonDisc } from "./PoissonDisc.js";
3
2
  export class PoissonDiscPluginInstance {
3
+ poissonDisc;
4
+ redrawTimeout;
5
+ _container;
6
+ _currentIndex;
4
7
  constructor(container) {
5
8
  this._container = container;
6
9
  this._currentIndex = 0;
@@ -26,6 +29,9 @@ export class PoissonDiscPluginInstance {
26
29
  const timeout = 250;
27
30
  this.redrawTimeout = setTimeout(() => {
28
31
  void (async () => {
32
+ if (this._container.destroyed) {
33
+ return;
34
+ }
29
35
  await this._initData();
30
36
  await container.particles.redraw();
31
37
  })();
@@ -40,12 +46,13 @@ export class PoissonDiscPluginInstance {
40
46
  return;
41
47
  }
42
48
  this._currentIndex = 0;
49
+ const { PoissonDisc } = await import("./PoissonDisc.js");
43
50
  this.poissonDisc = new PoissonDisc(canvasSize, poissonOptions.radius
44
51
  ? poissonOptions.radius * pixelRatio
45
52
  : Math.max(getRangeMax(particlesOptions.size.value) * pixelRatio, Math.sqrt((canvasSize.width * canvasSize.height) / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);
46
53
  const noSteps = 0;
47
54
  if (poissonOptions.steps > noSteps) {
48
- await this.poissonDisc.steps(poissonOptions.steps);
55
+ this.poissonDisc.steps(poissonOptions.steps);
49
56
  }
50
57
  else {
51
58
  await this.poissonDisc.run();
package/browser/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export async function loadPoissonDiscPlugin(engine) {
2
- engine.checkVersion("4.0.0-alpha.8");
2
+ engine.checkVersion("4.0.0-beta.0");
3
3
  await engine.register(async (e) => {
4
4
  const { PoissonDiscPlugin } = await import("./PoissonDiscPlugin.js");
5
5
  e.addPlugin(new PoissonDiscPlugin());
@@ -1,5 +1,10 @@
1
1
  import { isNull } from "@tsparticles/engine";
2
2
  export class Poisson {
3
+ dimensions;
4
+ enable;
5
+ radius;
6
+ retries;
7
+ steps;
3
8
  constructor() {
4
9
  this.enable = false;
5
10
  this.dimensions = 2;
@@ -1,5 +1,16 @@
1
1
  import { double, doublePI, getDistance, getRandom } from "@tsparticles/engine";
2
2
  export class PoissonDisc {
3
+ active;
4
+ cellSize;
5
+ cols;
6
+ dimensions;
7
+ firstPoint;
8
+ grid;
9
+ points;
10
+ radius;
11
+ retries;
12
+ rows;
13
+ size;
3
14
  constructor(size, radius, retries, dimensions, firstPoint) {
4
15
  this.size = { ...size };
5
16
  this.radius = radius;
@@ -62,18 +73,22 @@ export class PoissonDisc {
62
73
  }
63
74
  async run() {
64
75
  this.reset();
65
- const minCount = 0, step = 1;
76
+ const minCount = 0, step = 1, yieldEvery = 100, yieldStepModule = 0;
77
+ let iterations = 0;
66
78
  while (this.active.length > minCount) {
67
- await this.steps(step);
79
+ this.steps(step);
80
+ if (++iterations % yieldEvery === yieldStepModule) {
81
+ await new Promise(resolve => setTimeout(resolve));
82
+ }
68
83
  }
69
84
  }
70
- async steps(steps) {
85
+ steps(steps) {
71
86
  const minCount = 0;
72
87
  for (let i = 0; i < steps; i++) {
73
88
  if (this.active.length <= minCount) {
74
89
  continue;
75
90
  }
76
- await this._step();
91
+ this._step();
77
92
  }
78
93
  }
79
94
  _getNewPoint(currentPoint, tries) {
@@ -87,78 +102,75 @@ export class PoissonDisc {
87
102
  x: Math.floor(newPoint.x / this.cellSize),
88
103
  y: Math.floor(newPoint.y / this.cellSize),
89
104
  };
90
- if (newPoint.x > minCoordinate &&
91
- newPoint.x < this.size.width &&
92
- newPoint.y > minCoordinate &&
93
- newPoint.y < this.size.height) {
94
- const row = this.grid[newGridCoords.y];
95
- if (!row) {
96
- return;
97
- }
98
- const point = row[newGridCoords.x];
99
- if (point === undefined) {
100
- return;
101
- }
102
- if (point < gridMinValue) {
103
- for (let i = -1; i <= maxNeighbourIndex; i++) {
104
- for (let j = -1; j <= maxNeighbourIndex; j++) {
105
- const neighbourGrid = {
106
- x: newGridCoords.x + j,
107
- y: newGridCoords.y + i,
108
- };
109
- if (neighbourGrid.x >= minCoordinate &&
110
- neighbourGrid.y >= minCoordinate &&
111
- neighbourGrid.x < this.cols &&
112
- neighbourGrid.y < this.rows &&
113
- (neighbourGrid.x !== newGridCoords.x || neighbourGrid.y !== newGridCoords.y)) {
114
- if (point >= gridMinValue) {
115
- const neighbourIndex = point, neighbour = this.points[neighbourIndex];
116
- if (!neighbour) {
117
- continue;
118
- }
119
- const dist = getDistance(newPoint, neighbour.position);
120
- if (dist < this.radius) {
121
- return;
122
- }
123
- }
124
- }
125
- }
126
- }
127
- }
128
- else {
129
- return;
130
- }
105
+ if (newPoint.x <= minCoordinate ||
106
+ newPoint.x >= this.size.width ||
107
+ newPoint.y <= minCoordinate ||
108
+ newPoint.y >= this.size.height) {
109
+ return;
131
110
  }
132
- else {
111
+ const row = this.grid[newGridCoords.y];
112
+ if (!row) {
133
113
  return;
134
114
  }
135
- return newPoint;
136
- }
137
- async _step() {
138
- const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
139
- return new Promise(resolve => {
140
- let foundNewPoint = false;
141
- for (let tries = 0; tries < this.retries; tries++) {
142
- const randomActivePointIndex = this.active[randomActive];
143
- if (randomActivePointIndex === undefined) {
115
+ const cellValue = row[newGridCoords.x];
116
+ if (cellValue === undefined) {
117
+ return;
118
+ }
119
+ if (cellValue >= gridMinValue) {
120
+ return;
121
+ }
122
+ for (let i = -1; i <= maxNeighbourIndex; i++) {
123
+ for (let j = -1; j <= maxNeighbourIndex; j++) {
124
+ if (!i && !j) {
125
+ continue;
126
+ }
127
+ const neighbourGrid = {
128
+ x: newGridCoords.x + j,
129
+ y: newGridCoords.y + i,
130
+ };
131
+ if (neighbourGrid.x < minCoordinate ||
132
+ neighbourGrid.y < minCoordinate ||
133
+ neighbourGrid.x >= this.cols ||
134
+ neighbourGrid.y >= this.rows) {
135
+ continue;
136
+ }
137
+ const neighbourCellValue = this.grid[neighbourGrid.y]?.[neighbourGrid.x];
138
+ if (neighbourCellValue === undefined || neighbourCellValue < gridMinValue) {
144
139
  continue;
145
140
  }
146
- const point = this.points[randomActivePointIndex];
147
- if (!point) {
141
+ const neighbour = this.points[neighbourCellValue];
142
+ if (!neighbour) {
148
143
  continue;
149
144
  }
150
- const newPoint = this._getNewPoint(point, tries);
151
- if (newPoint) {
152
- foundNewPoint = true;
153
- this.addPoint(newPoint);
154
- break;
145
+ if (getDistance(newPoint, neighbour.position) < this.radius) {
146
+ return;
155
147
  }
156
148
  }
157
- if (!foundNewPoint) {
158
- const deleteCount = 1;
159
- this.active.splice(randomActive, deleteCount);
149
+ }
150
+ return newPoint;
151
+ }
152
+ _step() {
153
+ const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
154
+ let foundNewPoint = false;
155
+ for (let tries = 0; tries < this.retries; tries++) {
156
+ const randomActivePointIndex = this.active[randomActive];
157
+ if (randomActivePointIndex === undefined) {
158
+ continue;
159
+ }
160
+ const point = this.points[randomActivePointIndex];
161
+ if (!point) {
162
+ continue;
160
163
  }
161
- resolve();
162
- });
164
+ const newPoint = this._getNewPoint(point, tries);
165
+ if (newPoint) {
166
+ foundNewPoint = true;
167
+ this.addPoint(newPoint);
168
+ break;
169
+ }
170
+ }
171
+ if (!foundNewPoint) {
172
+ const deleteCount = 1;
173
+ this.active.splice(randomActive, deleteCount);
174
+ }
163
175
  }
164
176
  }
@@ -1,8 +1,6 @@
1
1
  import { Poisson } from "./Options/Classes/Poisson.js";
2
2
  export class PoissonDiscPlugin {
3
- constructor() {
4
- this.id = "poisson";
5
- }
3
+ id = "poisson";
6
4
  async getPlugin(container) {
7
5
  const { PoissonDiscPluginInstance } = await import("./PoissonDiscPluginInstance.js");
8
6
  return new PoissonDiscPluginInstance(container);
@@ -1,6 +1,9 @@
1
1
  import { getRangeMax } from "@tsparticles/engine";
2
- import { PoissonDisc } from "./PoissonDisc.js";
3
2
  export class PoissonDiscPluginInstance {
3
+ poissonDisc;
4
+ redrawTimeout;
5
+ _container;
6
+ _currentIndex;
4
7
  constructor(container) {
5
8
  this._container = container;
6
9
  this._currentIndex = 0;
@@ -26,6 +29,9 @@ export class PoissonDiscPluginInstance {
26
29
  const timeout = 250;
27
30
  this.redrawTimeout = setTimeout(() => {
28
31
  void (async () => {
32
+ if (this._container.destroyed) {
33
+ return;
34
+ }
29
35
  await this._initData();
30
36
  await container.particles.redraw();
31
37
  })();
@@ -40,12 +46,13 @@ export class PoissonDiscPluginInstance {
40
46
  return;
41
47
  }
42
48
  this._currentIndex = 0;
49
+ const { PoissonDisc } = await import("./PoissonDisc.js");
43
50
  this.poissonDisc = new PoissonDisc(canvasSize, poissonOptions.radius
44
51
  ? poissonOptions.radius * pixelRatio
45
52
  : Math.max(getRangeMax(particlesOptions.size.value) * pixelRatio, Math.sqrt((canvasSize.width * canvasSize.height) / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);
46
53
  const noSteps = 0;
47
54
  if (poissonOptions.steps > noSteps) {
48
- await this.poissonDisc.steps(poissonOptions.steps);
55
+ this.poissonDisc.steps(poissonOptions.steps);
49
56
  }
50
57
  else {
51
58
  await this.poissonDisc.run();
package/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export async function loadPoissonDiscPlugin(engine) {
2
- engine.checkVersion("4.0.0-alpha.8");
2
+ engine.checkVersion("4.0.0-beta.0");
3
3
  await engine.register(async (e) => {
4
4
  const { PoissonDiscPlugin } = await import("./PoissonDiscPlugin.js");
5
5
  e.addPlugin(new PoissonDiscPlugin());
@@ -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
- * v4.0.0-alpha.8
7
+ * v4.0.0-beta.0
8
8
  */
9
9
  "use strict";
10
10
  /*
@@ -17,23 +17,13 @@
17
17
  */
18
18
  (this["webpackChunk_tsparticles_plugin_poisson_disc"] = this["webpackChunk_tsparticles_plugin_poisson_disc"] || []).push([["dist_browser_PoissonDiscPluginInstance_js"],{
19
19
 
20
- /***/ "./dist/browser/PoissonDisc.js"
21
- /*!*************************************!*\
22
- !*** ./dist/browser/PoissonDisc.js ***!
23
- \*************************************/
24
- (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
25
-
26
- eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PoissonDisc: () => (/* binding */ PoissonDisc)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nclass PoissonDisc {\n constructor(size, radius, retries, dimensions, firstPoint) {\n this.size = {\n ...size\n };\n this.radius = radius;\n this.retries = retries;\n this.dimensions = dimensions;\n this.cellSize = Math.floor(this.radius / Math.sqrt(this.dimensions));\n this.cols = Math.floor(this.size.width / this.cellSize);\n this.rows = Math.floor(this.size.height / this.cellSize);\n this.points = [];\n this.active = [];\n this.grid = [];\n this.firstPoint = firstPoint ? {\n ...firstPoint\n } : undefined;\n this.reset();\n }\n addPoint(inputPoint) {\n const point = {\n position: {\n ...inputPoint\n },\n gridPosition: {\n x: Math.floor(inputPoint.x / this.cellSize),\n y: Math.floor(inputPoint.y / this.cellSize)\n }\n },\n pointIndex = this.points.length,\n row = this.grid[point.gridPosition.y];\n if (!row) {\n return;\n }\n this.points.push(point);\n row[point.gridPosition.x] = pointIndex;\n this.active.push(pointIndex);\n }\n getRandom(min, max) {\n return Math.floor((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)() * (max - min)) + min;\n }\n initialiseGrid() {\n for (let y = 0; y <= this.rows; y++) {\n this.grid[y] = [];\n const row = this.grid[y];\n if (!row) {\n continue;\n }\n for (let x = 0; x <= this.cols; x++) {\n row[x] = -1;\n }\n }\n }\n reset() {\n this.points = [];\n this.active = [];\n this.grid = [];\n this.initialiseGrid();\n if (this.firstPoint) {\n this.addPoint(this.firstPoint);\n } else {\n const minCoordinate = 0;\n this.addPoint({\n x: this.getRandom(minCoordinate, this.size.width),\n y: this.getRandom(minCoordinate, this.size.height)\n });\n }\n }\n async run() {\n this.reset();\n const minCount = 0,\n step = 1;\n while (this.active.length > minCount) {\n await this.steps(step);\n }\n }\n async steps(steps) {\n const minCount = 0;\n for (let i = 0; i < steps; i++) {\n if (this.active.length <= minCount) {\n continue;\n }\n await this._step();\n }\n }\n _getNewPoint(currentPoint, tries) {\n const minCoordinate = 0,\n gridMinValue = 0,\n maxNeighbourIndex = 1,\n newAngle = tries * (_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.doublePI / this.retries),\n newDist = this.getRandom(this.radius, this.radius * _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.double),\n offset = {\n x: Math.cos(newAngle) * newDist,\n y: Math.sin(newAngle) * newDist\n },\n newPoint = {\n x: Math.floor(currentPoint.position.x + offset.x),\n y: Math.floor(currentPoint.position.y + offset.y)\n },\n newGridCoords = {\n x: Math.floor(newPoint.x / this.cellSize),\n y: Math.floor(newPoint.y / this.cellSize)\n };\n if (newPoint.x > minCoordinate && newPoint.x < this.size.width && newPoint.y > minCoordinate && newPoint.y < this.size.height) {\n const row = this.grid[newGridCoords.y];\n if (!row) {\n return;\n }\n const point = row[newGridCoords.x];\n if (point === undefined) {\n return;\n }\n if (point < gridMinValue) {\n for (let i = -1; i <= maxNeighbourIndex; i++) {\n for (let j = -1; j <= maxNeighbourIndex; j++) {\n const neighbourGrid = {\n x: newGridCoords.x + j,\n y: newGridCoords.y + i\n };\n if (neighbourGrid.x >= minCoordinate && neighbourGrid.y >= minCoordinate && neighbourGrid.x < this.cols && neighbourGrid.y < this.rows && (neighbourGrid.x !== newGridCoords.x || neighbourGrid.y !== newGridCoords.y)) {\n if (point >= gridMinValue) {\n const neighbourIndex = point,\n neighbour = this.points[neighbourIndex];\n if (!neighbour) {\n continue;\n }\n const dist = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getDistance)(newPoint, neighbour.position);\n if (dist < this.radius) {\n return;\n }\n }\n }\n }\n }\n } else {\n return;\n }\n } else {\n return;\n }\n return newPoint;\n }\n async _step() {\n const minCount = 0,\n randomActive = this.getRandom(minCount, this.active.length);\n return new Promise(resolve => {\n let foundNewPoint = false;\n for (let tries = 0; tries < this.retries; tries++) {\n const randomActivePointIndex = this.active[randomActive];\n if (randomActivePointIndex === undefined) {\n continue;\n }\n const point = this.points[randomActivePointIndex];\n if (!point) {\n continue;\n }\n const newPoint = this._getNewPoint(point, tries);\n if (newPoint) {\n foundNewPoint = true;\n this.addPoint(newPoint);\n break;\n }\n }\n if (!foundNewPoint) {\n const deleteCount = 1;\n this.active.splice(randomActive, deleteCount);\n }\n resolve();\n });\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/PoissonDisc.js?\n}");
27
-
28
- /***/ },
29
-
30
20
  /***/ "./dist/browser/PoissonDiscPluginInstance.js"
31
21
  /*!***************************************************!*\
32
22
  !*** ./dist/browser/PoissonDiscPluginInstance.js ***!
33
23
  \***************************************************/
34
24
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
35
25
 
36
- eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PoissonDiscPluginInstance: () => (/* binding */ PoissonDiscPluginInstance)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _PoissonDisc_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./PoissonDisc.js */ \"./dist/browser/PoissonDisc.js\");\n\n\nclass PoissonDiscPluginInstance {\n constructor(container) {\n this._container = container;\n this._currentIndex = 0;\n }\n async init() {\n await this._initData();\n }\n particlePosition(position) {\n const container = this._container,\n options = container.actualOptions.poisson;\n if (!this.poissonDisc || !(options?.enable ?? false) || this._currentIndex >= this.poissonDisc.points.length) {\n return;\n }\n return position ?? this.poissonDisc.points[this._currentIndex++]?.position;\n }\n resize() {\n const container = this._container,\n options = container.actualOptions.poisson;\n if (!(options?.enable ?? false)) {\n return;\n }\n if (this.redrawTimeout) {\n clearTimeout(this.redrawTimeout);\n }\n const timeout = 250;\n this.redrawTimeout = setTimeout(() => {\n void (async () => {\n await this._initData();\n await container.particles.redraw();\n })();\n }, timeout);\n }\n stop() {\n delete this.poissonDisc;\n }\n async _initData() {\n const container = this._container,\n poissonOptions = container.actualOptions.poisson,\n particlesOptions = container.actualOptions.particles,\n canvasSize = container.canvas.size,\n pixelRatio = container.retina.pixelRatio;\n if (!poissonOptions?.enable) {\n return;\n }\n this._currentIndex = 0;\n this.poissonDisc = new _PoissonDisc_js__WEBPACK_IMPORTED_MODULE_1__.PoissonDisc(canvasSize, poissonOptions.radius ? poissonOptions.radius * pixelRatio : Math.max((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRangeMax)(particlesOptions.size.value) * pixelRatio, Math.sqrt(canvasSize.width * canvasSize.height / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);\n const noSteps = 0;\n if (poissonOptions.steps > noSteps) {\n await this.poissonDisc.steps(poissonOptions.steps);\n } else {\n await this.poissonDisc.run();\n }\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/PoissonDiscPluginInstance.js?\n}");
26
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PoissonDiscPluginInstance: () => (/* binding */ PoissonDiscPluginInstance)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nclass PoissonDiscPluginInstance {\n poissonDisc;\n redrawTimeout;\n _container;\n _currentIndex;\n constructor(container){\n this._container = container;\n this._currentIndex = 0;\n }\n async init() {\n await this._initData();\n }\n particlePosition(position) {\n const container = this._container, options = container.actualOptions.poisson;\n if (!this.poissonDisc || !(options?.enable ?? false) || this._currentIndex >= this.poissonDisc.points.length) {\n return;\n }\n return position ?? this.poissonDisc.points[this._currentIndex++]?.position;\n }\n resize() {\n const container = this._container, options = container.actualOptions.poisson;\n if (!(options?.enable ?? false)) {\n return;\n }\n if (this.redrawTimeout) {\n clearTimeout(this.redrawTimeout);\n }\n const timeout = 250;\n this.redrawTimeout = setTimeout(()=>{\n void (async ()=>{\n if (this._container.destroyed) {\n return;\n }\n await this._initData();\n await container.particles.redraw();\n })();\n }, timeout);\n }\n stop() {\n delete this.poissonDisc;\n }\n async _initData() {\n const container = this._container, poissonOptions = container.actualOptions.poisson, particlesOptions = container.actualOptions.particles, canvasSize = container.canvas.size, pixelRatio = container.retina.pixelRatio;\n if (!poissonOptions?.enable) {\n return;\n }\n this._currentIndex = 0;\n const { PoissonDisc } = await __webpack_require__.e(/*! import() */ \"dist_browser_PoissonDisc_js\").then(__webpack_require__.bind(__webpack_require__, /*! ./PoissonDisc.js */ \"./dist/browser/PoissonDisc.js\"));\n this.poissonDisc = new PoissonDisc(canvasSize, poissonOptions.radius ? poissonOptions.radius * pixelRatio : Math.max((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRangeMax)(particlesOptions.size.value) * pixelRatio, Math.sqrt(canvasSize.width * canvasSize.height / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);\n const noSteps = 0;\n if (poissonOptions.steps > noSteps) {\n this.poissonDisc.steps(poissonOptions.steps);\n } else {\n await this.poissonDisc.run();\n }\n }\n}\n\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/PoissonDiscPluginInstance.js?\n}");
37
27
 
38
28
  /***/ }
39
29
 
@@ -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
- * v4.0.0-alpha.8
7
+ * v4.0.0-beta.0
8
8
  */
9
9
  "use strict";
10
10
  /*
@@ -23,7 +23,7 @@
23
23
  \*************************************************/
24
24
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
25
25
 
26
- eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Poisson: () => (/* binding */ Poisson)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nclass Poisson {\n constructor() {\n this.enable = false;\n this.dimensions = 2;\n this.radius = 0;\n this.retries = 30;\n this.steps = 0;\n }\n load(data) {\n if ((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.isNull)(data)) {\n return;\n }\n if (data.enable !== undefined) {\n this.enable = data.enable;\n }\n if (data.dimensions !== undefined) {\n this.dimensions = data.dimensions;\n }\n if (data.radius !== undefined) {\n this.radius = data.radius;\n }\n if (data.retries !== undefined) {\n this.retries = data.retries;\n }\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/Options/Classes/Poisson.js?\n}");
26
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Poisson: () => (/* binding */ Poisson)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n\nclass Poisson {\n dimensions;\n enable;\n radius;\n retries;\n steps;\n constructor(){\n this.enable = false;\n this.dimensions = 2;\n this.radius = 0;\n this.retries = 30;\n this.steps = 0;\n }\n load(data) {\n if ((0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.isNull)(data)) {\n return;\n }\n if (data.enable !== undefined) {\n this.enable = data.enable;\n }\n if (data.dimensions !== undefined) {\n this.dimensions = data.dimensions;\n }\n if (data.radius !== undefined) {\n this.radius = data.radius;\n }\n if (data.retries !== undefined) {\n this.retries = data.retries;\n }\n }\n}\n\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/Options/Classes/Poisson.js?\n}");
27
27
 
28
28
  /***/ },
29
29
 
@@ -33,7 +33,7 @@ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpa
33
33
  \*******************************************/
34
34
  (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
35
35
 
36
- eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PoissonDiscPlugin: () => (/* binding */ PoissonDiscPlugin)\n/* harmony export */ });\n/* harmony import */ var _Options_Classes_Poisson_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Options/Classes/Poisson.js */ \"./dist/browser/Options/Classes/Poisson.js\");\n\nclass PoissonDiscPlugin {\n constructor() {\n this.id = \"poisson\";\n }\n async getPlugin(container) {\n const {\n PoissonDiscPluginInstance\n } = await __webpack_require__.e(/*! import() */ \"dist_browser_PoissonDiscPluginInstance_js\").then(__webpack_require__.bind(__webpack_require__, /*! ./PoissonDiscPluginInstance.js */ \"./dist/browser/PoissonDiscPluginInstance.js\"));\n return new PoissonDiscPluginInstance(container);\n }\n loadOptions(_container, options, source) {\n if (!this.needsPlugin(options) && !this.needsPlugin(source)) {\n return;\n }\n let poissonOptions = options.poisson;\n if (poissonOptions?.load === undefined) {\n options.poisson = poissonOptions = new _Options_Classes_Poisson_js__WEBPACK_IMPORTED_MODULE_0__.Poisson();\n }\n poissonOptions.load(source?.poisson);\n }\n needsPlugin(options) {\n return options?.poisson?.enable ?? false;\n }\n}\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/PoissonDiscPlugin.js?\n}");
36
+ eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PoissonDiscPlugin: () => (/* binding */ PoissonDiscPlugin)\n/* harmony export */ });\n/* harmony import */ var _Options_Classes_Poisson_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./Options/Classes/Poisson.js */ \"./dist/browser/Options/Classes/Poisson.js\");\n\nclass PoissonDiscPlugin {\n id = \"poisson\";\n async getPlugin(container) {\n const { PoissonDiscPluginInstance } = await __webpack_require__.e(/*! import() */ \"dist_browser_PoissonDiscPluginInstance_js\").then(__webpack_require__.bind(__webpack_require__, /*! ./PoissonDiscPluginInstance.js */ \"./dist/browser/PoissonDiscPluginInstance.js\"));\n return new PoissonDiscPluginInstance(container);\n }\n loadOptions(_container, options, source) {\n if (!this.needsPlugin(options) && !this.needsPlugin(source)) {\n return;\n }\n let poissonOptions = options.poisson;\n if (poissonOptions?.load === undefined) {\n options.poisson = poissonOptions = new _Options_Classes_Poisson_js__WEBPACK_IMPORTED_MODULE_0__.Poisson();\n }\n poissonOptions.load(source?.poisson);\n }\n needsPlugin(options) {\n return options?.poisson?.enable ?? false;\n }\n}\n\n\n//# sourceURL=webpack://@tsparticles/plugin-poisson-disc/./dist/browser/PoissonDiscPlugin.js?\n}");
37
37
 
38
38
  /***/ }
39
39