@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.
- package/LICENSE +21 -0
- package/README.md +74 -0
- package/browser/Interfaces/IPoissonPoint.js +1 -0
- package/browser/Options/Classes/Poisson.js +26 -0
- package/browser/Options/Interfaces/IPoisson.js +1 -0
- package/browser/PoissonDisc.js +138 -0
- package/browser/PoissonInstance.js +55 -0
- package/browser/index.js +27 -0
- package/browser/package.json +1 -0
- package/browser/types.js +1 -0
- package/cjs/Interfaces/IPoissonPoint.js +2 -0
- package/cjs/Options/Classes/Poisson.js +30 -0
- package/cjs/Options/Interfaces/IPoisson.js +2 -0
- package/cjs/PoissonDisc.js +142 -0
- package/cjs/PoissonInstance.js +59 -0
- package/cjs/index.js +31 -0
- package/cjs/package.json +1 -0
- package/cjs/types.js +2 -0
- package/esm/Interfaces/IPoissonPoint.js +1 -0
- package/esm/Options/Classes/Poisson.js +26 -0
- package/esm/Options/Interfaces/IPoisson.js +1 -0
- package/esm/PoissonDisc.js +138 -0
- package/esm/PoissonInstance.js +55 -0
- package/esm/index.js +27 -0
- package/esm/package.json +1 -0
- package/esm/types.js +1 -0
- package/package.json +93 -0
- package/report.html +39 -0
- package/tsparticles.plugin.poisson.js +366 -0
- package/tsparticles.plugin.poisson.min.js +2 -0
- package/tsparticles.plugin.poisson.min.js.LICENSE.txt +1 -0
- package/types/Interfaces/IPoissonPoint.d.ts +5 -0
- package/types/Options/Classes/Poisson.d.ts +11 -0
- package/types/Options/Interfaces/IPoisson.d.ts +7 -0
- package/types/PoissonDisc.d.ts +27 -0
- package/types/PoissonInstance.d.ts +16 -0
- package/types/index.d.ts +2 -0
- package/types/types.d.ts +12 -0
- package/umd/Interfaces/IPoissonPoint.js +12 -0
- package/umd/Options/Classes/Poisson.js +40 -0
- package/umd/Options/Interfaces/IPoisson.js +12 -0
- package/umd/PoissonDisc.js +152 -0
- package/umd/PoissonInstance.js +69 -0
- package/umd/index.js +41 -0
- package/umd/types.js +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Matteo Bruni
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[](https://particles.js.org)
|
|
2
|
+
|
|
3
|
+
# tsParticles Poisson Disc Plugin
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/@tsparticles/plugin-poisson)
|
|
6
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-poisson)
|
|
7
|
+
[](https://www.npmjs.com/package/@tsparticles/plugin-poisson) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/tsparticles/tsparticles) plugin for particles poisson disc effect.
|
|
10
|
+
|
|
11
|
+
## How to use it
|
|
12
|
+
|
|
13
|
+
### CDN / Vanilla JS / jQuery
|
|
14
|
+
|
|
15
|
+
The CDN/Vanilla version JS has one required file in vanilla configuration:
|
|
16
|
+
|
|
17
|
+
Including the `tsparticles.plugin.poisson.min.js` file will export the function to load the plugin:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
loadPoissonDiscPlugin;
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Usage
|
|
24
|
+
|
|
25
|
+
Once the scripts are loaded you can set up `tsParticles` and the plugin like this:
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
(async () => {
|
|
29
|
+
await loadPoissonDiscPlugin(tsParticles);
|
|
30
|
+
|
|
31
|
+
await tsParticles.load({
|
|
32
|
+
id: "tsparticles",
|
|
33
|
+
options: {
|
|
34
|
+
/* options */
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
})();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### ESM / CommonJS
|
|
41
|
+
|
|
42
|
+
This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
|
|
43
|
+
|
|
44
|
+
```shell
|
|
45
|
+
$ npm install @tsparticles/plugin-poisson
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
or
|
|
49
|
+
|
|
50
|
+
```shell
|
|
51
|
+
$ yarn add @tsparticles/plugin-poisson
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Then you need to import it in the app, like this:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
const { tsParticles } = require("@tsparticles/engine");
|
|
58
|
+
const { loadPoissonDiscPlugin } = require("@tsparticles/plugin-poisson");
|
|
59
|
+
|
|
60
|
+
(async () => {
|
|
61
|
+
await loadPoissonDiscPlugin(tsParticles);
|
|
62
|
+
})();
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
or
|
|
66
|
+
|
|
67
|
+
```javascript
|
|
68
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
69
|
+
import { loadPoissonDiscPlugin } from "@tsparticles/plugin-poisson";
|
|
70
|
+
|
|
71
|
+
(async () => {
|
|
72
|
+
await loadPoissonDiscPlugin(tsParticles);
|
|
73
|
+
})();
|
|
74
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class Poisson {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.enable = false;
|
|
4
|
+
this.dimensions = 2;
|
|
5
|
+
this.radius = 0;
|
|
6
|
+
this.retries = 30;
|
|
7
|
+
this.steps = 0;
|
|
8
|
+
}
|
|
9
|
+
load(data) {
|
|
10
|
+
if (!data) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (data.enable !== undefined) {
|
|
14
|
+
this.enable = data.enable;
|
|
15
|
+
}
|
|
16
|
+
if (data.dimensions !== undefined) {
|
|
17
|
+
this.dimensions = data.dimensions;
|
|
18
|
+
}
|
|
19
|
+
if (data.radius !== undefined) {
|
|
20
|
+
this.radius = data.radius;
|
|
21
|
+
}
|
|
22
|
+
if (data.retries !== undefined) {
|
|
23
|
+
this.retries = data.retries;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { getDistance, getRandom } from "@tsparticles/engine";
|
|
2
|
+
const double = 2, doublePI = Math.PI * double;
|
|
3
|
+
export class PoissonDisc {
|
|
4
|
+
constructor(size, radius, retries, dimensions, firstPoint) {
|
|
5
|
+
this.size = { ...size };
|
|
6
|
+
this.radius = radius;
|
|
7
|
+
this.retries = retries;
|
|
8
|
+
this.dimensions = dimensions;
|
|
9
|
+
this.cellSize = Math.floor(this.radius / Math.sqrt(this.dimensions));
|
|
10
|
+
this.cols = Math.floor(this.size.width / this.cellSize);
|
|
11
|
+
this.rows = Math.floor(this.size.height / this.cellSize);
|
|
12
|
+
this.points = [];
|
|
13
|
+
this.active = [];
|
|
14
|
+
this.grid = [];
|
|
15
|
+
this.firstPoint = firstPoint ? { ...firstPoint } : undefined;
|
|
16
|
+
this.reset();
|
|
17
|
+
}
|
|
18
|
+
addPoint(inputPoint) {
|
|
19
|
+
const point = {
|
|
20
|
+
position: { ...inputPoint },
|
|
21
|
+
gridPosition: {
|
|
22
|
+
x: Math.floor(inputPoint.x / this.cellSize),
|
|
23
|
+
y: Math.floor(inputPoint.y / this.cellSize),
|
|
24
|
+
},
|
|
25
|
+
}, pointIndex = this.points.length;
|
|
26
|
+
this.points.push(point);
|
|
27
|
+
this.grid[point.gridPosition.y][point.gridPosition.x] = pointIndex;
|
|
28
|
+
this.active.push(pointIndex);
|
|
29
|
+
}
|
|
30
|
+
getRandom(min, max) {
|
|
31
|
+
return Math.floor(getRandom() * (max - min)) + min;
|
|
32
|
+
}
|
|
33
|
+
initialiseGrid() {
|
|
34
|
+
for (let y = 0; y <= this.rows; y++) {
|
|
35
|
+
this.grid[y] = [];
|
|
36
|
+
for (let x = 0; x <= this.cols; x++) {
|
|
37
|
+
this.grid[y][x] = -1;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
reset() {
|
|
42
|
+
this.points = [];
|
|
43
|
+
this.active = [];
|
|
44
|
+
this.grid = [];
|
|
45
|
+
this.initialiseGrid();
|
|
46
|
+
if (this.firstPoint) {
|
|
47
|
+
this.addPoint(this.firstPoint);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
const minCoordinate = 0;
|
|
51
|
+
this.addPoint({
|
|
52
|
+
x: this.getRandom(minCoordinate, this.size.width),
|
|
53
|
+
y: this.getRandom(minCoordinate, this.size.height),
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async run() {
|
|
58
|
+
this.reset();
|
|
59
|
+
const minCount = 0, step = 1;
|
|
60
|
+
while (this.active.length > minCount) {
|
|
61
|
+
await this.steps(step);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async steps(steps) {
|
|
65
|
+
const minCount = 0;
|
|
66
|
+
for (let i = 0; i < steps; i++) {
|
|
67
|
+
if (this.active.length <= minCount) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
await this._step();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
_getNewPoint(currentPoint, tries) {
|
|
74
|
+
const minCoordinate = 0, gridMinValue = 0, maxNeighbourIndex = 1, newAngle = tries * (doublePI / this.retries), newDist = this.getRandom(this.radius, this.radius * double), offset = {
|
|
75
|
+
x: Math.cos(newAngle) * newDist,
|
|
76
|
+
y: Math.sin(newAngle) * newDist,
|
|
77
|
+
}, newPoint = {
|
|
78
|
+
x: Math.floor(currentPoint.position.x + offset.x),
|
|
79
|
+
y: Math.floor(currentPoint.position.y + offset.y),
|
|
80
|
+
}, newGridCoords = {
|
|
81
|
+
x: Math.floor(newPoint.x / this.cellSize),
|
|
82
|
+
y: Math.floor(newPoint.y / this.cellSize),
|
|
83
|
+
};
|
|
84
|
+
if (newPoint.x > minCoordinate &&
|
|
85
|
+
newPoint.x < this.size.width &&
|
|
86
|
+
newPoint.y > minCoordinate &&
|
|
87
|
+
newPoint.y < this.size.height) {
|
|
88
|
+
if (this.grid[newGridCoords.y][newGridCoords.x] < gridMinValue) {
|
|
89
|
+
for (let i = -1; i <= maxNeighbourIndex; i++) {
|
|
90
|
+
for (let j = -1; j <= maxNeighbourIndex; j++) {
|
|
91
|
+
const neighbourGrid = {
|
|
92
|
+
x: newGridCoords.x + j,
|
|
93
|
+
y: newGridCoords.y + i,
|
|
94
|
+
};
|
|
95
|
+
if (neighbourGrid.x >= minCoordinate &&
|
|
96
|
+
neighbourGrid.y >= minCoordinate &&
|
|
97
|
+
neighbourGrid.x < this.cols &&
|
|
98
|
+
neighbourGrid.y < this.rows &&
|
|
99
|
+
(neighbourGrid.x !== newGridCoords.x || neighbourGrid.y !== newGridCoords.y)) {
|
|
100
|
+
if (this.grid[neighbourGrid.y][neighbourGrid.x] >= gridMinValue) {
|
|
101
|
+
const neighbourIndex = this.grid[neighbourGrid.y][neighbourGrid.x], neighbour = this.points[neighbourIndex], dist = getDistance(newPoint, neighbour.position);
|
|
102
|
+
if (dist < this.radius) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
return newPoint;
|
|
118
|
+
}
|
|
119
|
+
async _step() {
|
|
120
|
+
const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
|
|
121
|
+
return new Promise((resolve) => {
|
|
122
|
+
let foundNewPoint = false;
|
|
123
|
+
for (let tries = 0; tries < this.retries; tries++) {
|
|
124
|
+
const newPoint = this._getNewPoint(this.points[this.active[randomActive]], tries);
|
|
125
|
+
if (newPoint) {
|
|
126
|
+
foundNewPoint = true;
|
|
127
|
+
this.addPoint(newPoint);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (!foundNewPoint) {
|
|
132
|
+
const deleteCount = 1;
|
|
133
|
+
this.active.splice(randomActive, deleteCount);
|
|
134
|
+
}
|
|
135
|
+
resolve();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getRangeMax } from "@tsparticles/engine";
|
|
2
|
+
import { PoissonDisc } from "./PoissonDisc.js";
|
|
3
|
+
export class PoissonInstance {
|
|
4
|
+
constructor(container, engine) {
|
|
5
|
+
this._container = container;
|
|
6
|
+
this._engine = engine;
|
|
7
|
+
this._currentIndex = 0;
|
|
8
|
+
}
|
|
9
|
+
async init() {
|
|
10
|
+
await this._initData();
|
|
11
|
+
}
|
|
12
|
+
particlePosition(position) {
|
|
13
|
+
const container = this._container, options = container.actualOptions.poisson;
|
|
14
|
+
if (!this.poissonDisc || !(options?.enable ?? false) || this._currentIndex >= this.poissonDisc.points.length) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
return position ?? this.poissonDisc?.points[this._currentIndex++]?.position;
|
|
18
|
+
}
|
|
19
|
+
resize() {
|
|
20
|
+
const container = this._container, options = container.actualOptions.poisson;
|
|
21
|
+
if (!(options?.enable ?? false)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (this.redrawTimeout) {
|
|
25
|
+
clearTimeout(this.redrawTimeout);
|
|
26
|
+
}
|
|
27
|
+
const timeout = 250;
|
|
28
|
+
this.redrawTimeout = window.setTimeout(() => {
|
|
29
|
+
void (async () => {
|
|
30
|
+
await this._initData();
|
|
31
|
+
await container.particles.redraw();
|
|
32
|
+
})();
|
|
33
|
+
}, timeout);
|
|
34
|
+
}
|
|
35
|
+
stop() {
|
|
36
|
+
delete this.poissonDisc;
|
|
37
|
+
}
|
|
38
|
+
async _initData() {
|
|
39
|
+
const container = this._container, poissonOptions = container.actualOptions.poisson, particlesOptions = container.actualOptions.particles, canvasSize = container.canvas.size, pixelRatio = container.retina.pixelRatio;
|
|
40
|
+
if (!poissonOptions?.enable) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this._currentIndex = 0;
|
|
44
|
+
this.poissonDisc = new PoissonDisc(canvasSize, poissonOptions.radius
|
|
45
|
+
? poissonOptions.radius * pixelRatio
|
|
46
|
+
: Math.max(getRangeMax(particlesOptions.size.value) * pixelRatio, Math.sqrt((canvasSize.width * canvasSize.height) / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);
|
|
47
|
+
const noSteps = 0;
|
|
48
|
+
if (poissonOptions.steps > noSteps) {
|
|
49
|
+
await this.poissonDisc.steps(poissonOptions.steps);
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
await this.poissonDisc.run();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Poisson } from "./Options/Classes/Poisson.js";
|
|
2
|
+
import { PoissonInstance } from "./PoissonInstance.js";
|
|
3
|
+
class PoissonDiscPlugin {
|
|
4
|
+
constructor(engine) {
|
|
5
|
+
this.id = "poisson";
|
|
6
|
+
this._engine = engine;
|
|
7
|
+
}
|
|
8
|
+
getPlugin(container) {
|
|
9
|
+
return new PoissonInstance(container, this._engine);
|
|
10
|
+
}
|
|
11
|
+
loadOptions(options, source) {
|
|
12
|
+
if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
let poissonOptions = options.poisson;
|
|
16
|
+
if (poissonOptions?.load === undefined) {
|
|
17
|
+
options.poisson = poissonOptions = new Poisson();
|
|
18
|
+
}
|
|
19
|
+
poissonOptions.load(source?.poisson);
|
|
20
|
+
}
|
|
21
|
+
needsPlugin(options) {
|
|
22
|
+
return options?.poisson?.enable ?? false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export async function loadPoissonDiscPlugin(engine, refresh = true) {
|
|
26
|
+
await engine.addPlugin(new PoissonDiscPlugin(engine), refresh);
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
package/browser/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Poisson = void 0;
|
|
4
|
+
class Poisson {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.enable = false;
|
|
7
|
+
this.dimensions = 2;
|
|
8
|
+
this.radius = 0;
|
|
9
|
+
this.retries = 30;
|
|
10
|
+
this.steps = 0;
|
|
11
|
+
}
|
|
12
|
+
load(data) {
|
|
13
|
+
if (!data) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (data.enable !== undefined) {
|
|
17
|
+
this.enable = data.enable;
|
|
18
|
+
}
|
|
19
|
+
if (data.dimensions !== undefined) {
|
|
20
|
+
this.dimensions = data.dimensions;
|
|
21
|
+
}
|
|
22
|
+
if (data.radius !== undefined) {
|
|
23
|
+
this.radius = data.radius;
|
|
24
|
+
}
|
|
25
|
+
if (data.retries !== undefined) {
|
|
26
|
+
this.retries = data.retries;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.Poisson = Poisson;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PoissonDisc = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
const double = 2, doublePI = Math.PI * double;
|
|
6
|
+
class PoissonDisc {
|
|
7
|
+
constructor(size, radius, retries, dimensions, firstPoint) {
|
|
8
|
+
this.size = { ...size };
|
|
9
|
+
this.radius = radius;
|
|
10
|
+
this.retries = retries;
|
|
11
|
+
this.dimensions = dimensions;
|
|
12
|
+
this.cellSize = Math.floor(this.radius / Math.sqrt(this.dimensions));
|
|
13
|
+
this.cols = Math.floor(this.size.width / this.cellSize);
|
|
14
|
+
this.rows = Math.floor(this.size.height / this.cellSize);
|
|
15
|
+
this.points = [];
|
|
16
|
+
this.active = [];
|
|
17
|
+
this.grid = [];
|
|
18
|
+
this.firstPoint = firstPoint ? { ...firstPoint } : undefined;
|
|
19
|
+
this.reset();
|
|
20
|
+
}
|
|
21
|
+
addPoint(inputPoint) {
|
|
22
|
+
const point = {
|
|
23
|
+
position: { ...inputPoint },
|
|
24
|
+
gridPosition: {
|
|
25
|
+
x: Math.floor(inputPoint.x / this.cellSize),
|
|
26
|
+
y: Math.floor(inputPoint.y / this.cellSize),
|
|
27
|
+
},
|
|
28
|
+
}, pointIndex = this.points.length;
|
|
29
|
+
this.points.push(point);
|
|
30
|
+
this.grid[point.gridPosition.y][point.gridPosition.x] = pointIndex;
|
|
31
|
+
this.active.push(pointIndex);
|
|
32
|
+
}
|
|
33
|
+
getRandom(min, max) {
|
|
34
|
+
return Math.floor((0, engine_1.getRandom)() * (max - min)) + min;
|
|
35
|
+
}
|
|
36
|
+
initialiseGrid() {
|
|
37
|
+
for (let y = 0; y <= this.rows; y++) {
|
|
38
|
+
this.grid[y] = [];
|
|
39
|
+
for (let x = 0; x <= this.cols; x++) {
|
|
40
|
+
this.grid[y][x] = -1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
reset() {
|
|
45
|
+
this.points = [];
|
|
46
|
+
this.active = [];
|
|
47
|
+
this.grid = [];
|
|
48
|
+
this.initialiseGrid();
|
|
49
|
+
if (this.firstPoint) {
|
|
50
|
+
this.addPoint(this.firstPoint);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const minCoordinate = 0;
|
|
54
|
+
this.addPoint({
|
|
55
|
+
x: this.getRandom(minCoordinate, this.size.width),
|
|
56
|
+
y: this.getRandom(minCoordinate, this.size.height),
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async run() {
|
|
61
|
+
this.reset();
|
|
62
|
+
const minCount = 0, step = 1;
|
|
63
|
+
while (this.active.length > minCount) {
|
|
64
|
+
await this.steps(step);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async steps(steps) {
|
|
68
|
+
const minCount = 0;
|
|
69
|
+
for (let i = 0; i < steps; i++) {
|
|
70
|
+
if (this.active.length <= minCount) {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
await this._step();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
_getNewPoint(currentPoint, tries) {
|
|
77
|
+
const minCoordinate = 0, gridMinValue = 0, maxNeighbourIndex = 1, newAngle = tries * (doublePI / this.retries), newDist = this.getRandom(this.radius, this.radius * double), offset = {
|
|
78
|
+
x: Math.cos(newAngle) * newDist,
|
|
79
|
+
y: Math.sin(newAngle) * newDist,
|
|
80
|
+
}, newPoint = {
|
|
81
|
+
x: Math.floor(currentPoint.position.x + offset.x),
|
|
82
|
+
y: Math.floor(currentPoint.position.y + offset.y),
|
|
83
|
+
}, newGridCoords = {
|
|
84
|
+
x: Math.floor(newPoint.x / this.cellSize),
|
|
85
|
+
y: Math.floor(newPoint.y / this.cellSize),
|
|
86
|
+
};
|
|
87
|
+
if (newPoint.x > minCoordinate &&
|
|
88
|
+
newPoint.x < this.size.width &&
|
|
89
|
+
newPoint.y > minCoordinate &&
|
|
90
|
+
newPoint.y < this.size.height) {
|
|
91
|
+
if (this.grid[newGridCoords.y][newGridCoords.x] < gridMinValue) {
|
|
92
|
+
for (let i = -1; i <= maxNeighbourIndex; i++) {
|
|
93
|
+
for (let j = -1; j <= maxNeighbourIndex; j++) {
|
|
94
|
+
const neighbourGrid = {
|
|
95
|
+
x: newGridCoords.x + j,
|
|
96
|
+
y: newGridCoords.y + i,
|
|
97
|
+
};
|
|
98
|
+
if (neighbourGrid.x >= minCoordinate &&
|
|
99
|
+
neighbourGrid.y >= minCoordinate &&
|
|
100
|
+
neighbourGrid.x < this.cols &&
|
|
101
|
+
neighbourGrid.y < this.rows &&
|
|
102
|
+
(neighbourGrid.x !== newGridCoords.x || neighbourGrid.y !== newGridCoords.y)) {
|
|
103
|
+
if (this.grid[neighbourGrid.y][neighbourGrid.x] >= gridMinValue) {
|
|
104
|
+
const neighbourIndex = this.grid[neighbourGrid.y][neighbourGrid.x], neighbour = this.points[neighbourIndex], dist = (0, engine_1.getDistance)(newPoint, neighbour.position);
|
|
105
|
+
if (dist < this.radius) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
return newPoint;
|
|
121
|
+
}
|
|
122
|
+
async _step() {
|
|
123
|
+
const minCount = 0, randomActive = this.getRandom(minCount, this.active.length);
|
|
124
|
+
return new Promise((resolve) => {
|
|
125
|
+
let foundNewPoint = false;
|
|
126
|
+
for (let tries = 0; tries < this.retries; tries++) {
|
|
127
|
+
const newPoint = this._getNewPoint(this.points[this.active[randomActive]], tries);
|
|
128
|
+
if (newPoint) {
|
|
129
|
+
foundNewPoint = true;
|
|
130
|
+
this.addPoint(newPoint);
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (!foundNewPoint) {
|
|
135
|
+
const deleteCount = 1;
|
|
136
|
+
this.active.splice(randomActive, deleteCount);
|
|
137
|
+
}
|
|
138
|
+
resolve();
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.PoissonDisc = PoissonDisc;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PoissonInstance = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
const PoissonDisc_js_1 = require("./PoissonDisc.js");
|
|
6
|
+
class PoissonInstance {
|
|
7
|
+
constructor(container, engine) {
|
|
8
|
+
this._container = container;
|
|
9
|
+
this._engine = engine;
|
|
10
|
+
this._currentIndex = 0;
|
|
11
|
+
}
|
|
12
|
+
async init() {
|
|
13
|
+
await this._initData();
|
|
14
|
+
}
|
|
15
|
+
particlePosition(position) {
|
|
16
|
+
const container = this._container, options = container.actualOptions.poisson;
|
|
17
|
+
if (!this.poissonDisc || !(options?.enable ?? false) || this._currentIndex >= this.poissonDisc.points.length) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
return position ?? this.poissonDisc?.points[this._currentIndex++]?.position;
|
|
21
|
+
}
|
|
22
|
+
resize() {
|
|
23
|
+
const container = this._container, options = container.actualOptions.poisson;
|
|
24
|
+
if (!(options?.enable ?? false)) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (this.redrawTimeout) {
|
|
28
|
+
clearTimeout(this.redrawTimeout);
|
|
29
|
+
}
|
|
30
|
+
const timeout = 250;
|
|
31
|
+
this.redrawTimeout = window.setTimeout(() => {
|
|
32
|
+
void (async () => {
|
|
33
|
+
await this._initData();
|
|
34
|
+
await container.particles.redraw();
|
|
35
|
+
})();
|
|
36
|
+
}, timeout);
|
|
37
|
+
}
|
|
38
|
+
stop() {
|
|
39
|
+
delete this.poissonDisc;
|
|
40
|
+
}
|
|
41
|
+
async _initData() {
|
|
42
|
+
const container = this._container, poissonOptions = container.actualOptions.poisson, particlesOptions = container.actualOptions.particles, canvasSize = container.canvas.size, pixelRatio = container.retina.pixelRatio;
|
|
43
|
+
if (!poissonOptions?.enable) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this._currentIndex = 0;
|
|
47
|
+
this.poissonDisc = new PoissonDisc_js_1.PoissonDisc(canvasSize, poissonOptions.radius
|
|
48
|
+
? poissonOptions.radius * pixelRatio
|
|
49
|
+
: Math.max((0, engine_1.getRangeMax)(particlesOptions.size.value) * pixelRatio, Math.sqrt((canvasSize.width * canvasSize.height) / particlesOptions.number.value)), poissonOptions.retries, poissonOptions.dimensions);
|
|
50
|
+
const noSteps = 0;
|
|
51
|
+
if (poissonOptions.steps > noSteps) {
|
|
52
|
+
await this.poissonDisc.steps(poissonOptions.steps);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
await this.poissonDisc.run();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.PoissonInstance = PoissonInstance;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadPoissonDiscPlugin = void 0;
|
|
4
|
+
const Poisson_js_1 = require("./Options/Classes/Poisson.js");
|
|
5
|
+
const PoissonInstance_js_1 = require("./PoissonInstance.js");
|
|
6
|
+
class PoissonDiscPlugin {
|
|
7
|
+
constructor(engine) {
|
|
8
|
+
this.id = "poisson";
|
|
9
|
+
this._engine = engine;
|
|
10
|
+
}
|
|
11
|
+
getPlugin(container) {
|
|
12
|
+
return new PoissonInstance_js_1.PoissonInstance(container, this._engine);
|
|
13
|
+
}
|
|
14
|
+
loadOptions(options, source) {
|
|
15
|
+
if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
let poissonOptions = options.poisson;
|
|
19
|
+
if (poissonOptions?.load === undefined) {
|
|
20
|
+
options.poisson = poissonOptions = new Poisson_js_1.Poisson();
|
|
21
|
+
}
|
|
22
|
+
poissonOptions.load(source?.poisson);
|
|
23
|
+
}
|
|
24
|
+
needsPlugin(options) {
|
|
25
|
+
return options?.poisson?.enable ?? false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function loadPoissonDiscPlugin(engine, refresh = true) {
|
|
29
|
+
await engine.addPlugin(new PoissonDiscPlugin(engine), refresh);
|
|
30
|
+
}
|
|
31
|
+
exports.loadPoissonDiscPlugin = loadPoissonDiscPlugin;
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
package/cjs/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export class Poisson {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.enable = false;
|
|
4
|
+
this.dimensions = 2;
|
|
5
|
+
this.radius = 0;
|
|
6
|
+
this.retries = 30;
|
|
7
|
+
this.steps = 0;
|
|
8
|
+
}
|
|
9
|
+
load(data) {
|
|
10
|
+
if (!data) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (data.enable !== undefined) {
|
|
14
|
+
this.enable = data.enable;
|
|
15
|
+
}
|
|
16
|
+
if (data.dimensions !== undefined) {
|
|
17
|
+
this.dimensions = data.dimensions;
|
|
18
|
+
}
|
|
19
|
+
if (data.radius !== undefined) {
|
|
20
|
+
this.radius = data.radius;
|
|
21
|
+
}
|
|
22
|
+
if (data.retries !== undefined) {
|
|
23
|
+
this.retries = data.retries;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|