@tsparticles/plugin-absorbers 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.
- package/LICENSE +21 -0
- package/README.md +70 -0
- package/browser/AbsorberContainer.js +1 -0
- package/browser/AbsorberInstance.js +151 -0
- package/browser/Absorbers.js +60 -0
- package/browser/Enums/AbsorberClickMode.js +1 -0
- package/browser/Options/Classes/Absorber.js +46 -0
- package/browser/Options/Classes/AbsorberSize.js +25 -0
- package/browser/Options/Classes/AbsorberSizeLimit.js +17 -0
- package/browser/Options/Interfaces/IAbsorber.js +1 -0
- package/browser/Options/Interfaces/IAbsorberSize.js +1 -0
- package/browser/Options/Interfaces/IAbsorberSizeLimit.js +1 -0
- package/browser/index.js +53 -0
- package/browser/types.js +1 -0
- package/cjs/AbsorberContainer.js +2 -0
- package/cjs/AbsorberInstance.js +155 -0
- package/cjs/Absorbers.js +75 -0
- package/cjs/Enums/AbsorberClickMode.js +2 -0
- package/cjs/Options/Classes/Absorber.js +50 -0
- package/cjs/Options/Classes/AbsorberSize.js +29 -0
- package/cjs/Options/Classes/AbsorberSizeLimit.js +21 -0
- package/cjs/Options/Interfaces/IAbsorber.js +2 -0
- package/cjs/Options/Interfaces/IAbsorberSize.js +2 -0
- package/cjs/Options/Interfaces/IAbsorberSizeLimit.js +2 -0
- package/cjs/index.js +82 -0
- package/cjs/types.js +2 -0
- package/esm/AbsorberContainer.js +1 -0
- package/esm/AbsorberInstance.js +151 -0
- package/esm/Absorbers.js +60 -0
- package/esm/Enums/AbsorberClickMode.js +1 -0
- package/esm/Options/Classes/Absorber.js +46 -0
- package/esm/Options/Classes/AbsorberSize.js +25 -0
- package/esm/Options/Classes/AbsorberSizeLimit.js +17 -0
- package/esm/Options/Interfaces/IAbsorber.js +1 -0
- package/esm/Options/Interfaces/IAbsorberSize.js +1 -0
- package/esm/Options/Interfaces/IAbsorberSizeLimit.js +1 -0
- package/esm/index.js +53 -0
- package/esm/types.js +1 -0
- package/package.json +81 -0
- package/report.html +39 -0
- package/tsparticles.plugin.absorbers.js +458 -0
- package/tsparticles.plugin.absorbers.min.js +2 -0
- package/tsparticles.plugin.absorbers.min.js.LICENSE.txt +8 -0
- package/types/AbsorberContainer.d.ts +9 -0
- package/types/AbsorberInstance.d.ts +31 -0
- package/types/Absorbers.d.ts +20 -0
- package/types/Enums/AbsorberClickMode.d.ts +3 -0
- package/types/Options/Classes/Absorber.d.ts +16 -0
- package/types/Options/Classes/AbsorberSize.d.ts +10 -0
- package/types/Options/Classes/AbsorberSizeLimit.d.ts +8 -0
- package/types/Options/Interfaces/IAbsorber.d.ts +12 -0
- package/types/Options/Interfaces/IAbsorberSize.d.ts +6 -0
- package/types/Options/Interfaces/IAbsorberSizeLimit.d.ts +4 -0
- package/types/index.d.ts +4 -0
- package/types/types.d.ts +19 -0
- package/umd/AbsorberContainer.js +12 -0
- package/umd/AbsorberInstance.js +165 -0
- package/umd/Absorbers.js +74 -0
- package/umd/Enums/AbsorberClickMode.js +12 -0
- package/umd/Options/Classes/Absorber.js +60 -0
- package/umd/Options/Classes/AbsorberSize.js +39 -0
- package/umd/Options/Classes/AbsorberSizeLimit.js +31 -0
- package/umd/Options/Interfaces/IAbsorber.js +12 -0
- package/umd/Options/Interfaces/IAbsorberSize.js +12 -0
- package/umd/Options/Interfaces/IAbsorberSizeLimit.js +12 -0
- package/umd/index.js +81 -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,70 @@
|
|
|
1
|
+
[](https://particles.js.org)
|
|
2
|
+
|
|
3
|
+
# tsParticles Absorbers Plugin
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/tsparticles-plugin-absorbers)
|
|
6
|
+
[](https://www.npmjs.com/package/tsparticles-plugin-absorbers)
|
|
7
|
+
[](https://www.npmjs.com/package/tsparticles-plugin-absorbers) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/matteobruni/tsparticles) plugin for particles absorbers.
|
|
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.absorbers.min.js` file will export the function to load the plugin:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
loadAbsorbersPlugin;
|
|
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 loadAbsorbersPlugin(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-absorbers
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
or
|
|
49
|
+
|
|
50
|
+
```shell
|
|
51
|
+
$ yarn add tsparticles-plugin-absorbers
|
|
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 { loadAbsorbersPlugin } = require("tsparticles-plugin-absorbers");
|
|
59
|
+
|
|
60
|
+
loadAbsorbersPlugin(tsParticles);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
or
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
import { tsParticles } from "tsparticles-engine";
|
|
67
|
+
import { loadAbsorbersPlugin } from "tsparticles-plugin-absorbers";
|
|
68
|
+
|
|
69
|
+
loadAbsorbersPlugin(tsParticles);
|
|
70
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Vector, calcPositionOrRandomFromSize, calcPositionOrRandomFromSizeRanged, getDistance, getDistances, getRandom, getRangeValue, getStyleFromRgb, isPointInside, rangeColorToRgb, } from "@tsparticles/engine";
|
|
2
|
+
import { Absorber } from "./Options/Classes/Absorber";
|
|
3
|
+
export class AbsorberInstance {
|
|
4
|
+
constructor(absorbers, container, options, position) {
|
|
5
|
+
var _a, _b, _c;
|
|
6
|
+
this.absorbers = absorbers;
|
|
7
|
+
this.container = container;
|
|
8
|
+
this.initialPosition = position ? Vector.create(position.x, position.y) : undefined;
|
|
9
|
+
if (options instanceof Absorber) {
|
|
10
|
+
this.options = options;
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
this.options = new Absorber();
|
|
14
|
+
this.options.load(options);
|
|
15
|
+
}
|
|
16
|
+
this.dragging = false;
|
|
17
|
+
this.name = this.options.name;
|
|
18
|
+
this.opacity = this.options.opacity;
|
|
19
|
+
this.size = getRangeValue(this.options.size.value) * container.retina.pixelRatio;
|
|
20
|
+
this.mass = this.size * this.options.size.density * container.retina.reduceFactor;
|
|
21
|
+
const limit = this.options.size.limit;
|
|
22
|
+
this.limit = {
|
|
23
|
+
radius: limit.radius * container.retina.pixelRatio * container.retina.reduceFactor,
|
|
24
|
+
mass: limit.mass,
|
|
25
|
+
};
|
|
26
|
+
this.color = (_a = rangeColorToRgb(this.options.color)) !== null && _a !== void 0 ? _a : {
|
|
27
|
+
b: 0,
|
|
28
|
+
g: 0,
|
|
29
|
+
r: 0,
|
|
30
|
+
};
|
|
31
|
+
this.position = (_c = (_b = this.initialPosition) === null || _b === void 0 ? void 0 : _b.copy()) !== null && _c !== void 0 ? _c : this.calcPosition();
|
|
32
|
+
}
|
|
33
|
+
attract(particle) {
|
|
34
|
+
const container = this.container, options = this.options;
|
|
35
|
+
if (options.draggable) {
|
|
36
|
+
const mouse = container.interactivity.mouse;
|
|
37
|
+
if (mouse.clicking && mouse.downPosition) {
|
|
38
|
+
const mouseDist = getDistance(this.position, mouse.downPosition);
|
|
39
|
+
if (mouseDist <= this.size) {
|
|
40
|
+
this.dragging = true;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.dragging = false;
|
|
45
|
+
}
|
|
46
|
+
if (this.dragging && mouse.position) {
|
|
47
|
+
this.position.x = mouse.position.x;
|
|
48
|
+
this.position.y = mouse.position.y;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const pos = particle.getPosition(), { dx, dy, distance } = getDistances(this.position, pos), v = Vector.create(dx, dy);
|
|
52
|
+
v.length = (this.mass / Math.pow(distance, 2)) * container.retina.reduceFactor;
|
|
53
|
+
if (distance < this.size + particle.getRadius()) {
|
|
54
|
+
const sizeFactor = particle.getRadius() * 0.033 * container.retina.pixelRatio;
|
|
55
|
+
if ((this.size > particle.getRadius() && distance < this.size - particle.getRadius()) ||
|
|
56
|
+
(particle.absorberOrbit !== undefined && particle.absorberOrbit.length < 0)) {
|
|
57
|
+
if (options.destroy) {
|
|
58
|
+
particle.destroy();
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
particle.needsNewPosition = true;
|
|
62
|
+
this.updateParticlePosition(particle, v);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
if (options.destroy) {
|
|
67
|
+
particle.size.value -= sizeFactor;
|
|
68
|
+
}
|
|
69
|
+
this.updateParticlePosition(particle, v);
|
|
70
|
+
}
|
|
71
|
+
if (this.limit.radius <= 0 || this.size < this.limit.radius) {
|
|
72
|
+
this.size += sizeFactor;
|
|
73
|
+
}
|
|
74
|
+
if (this.limit.mass <= 0 || this.mass < this.limit.mass) {
|
|
75
|
+
this.mass += sizeFactor * this.options.size.density * container.retina.reduceFactor;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
this.updateParticlePosition(particle, v);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
draw(context) {
|
|
83
|
+
context.translate(this.position.x, this.position.y);
|
|
84
|
+
context.beginPath();
|
|
85
|
+
context.arc(0, 0, this.size, 0, Math.PI * 2, false);
|
|
86
|
+
context.closePath();
|
|
87
|
+
context.fillStyle = getStyleFromRgb(this.color, this.opacity);
|
|
88
|
+
context.fill();
|
|
89
|
+
}
|
|
90
|
+
resize() {
|
|
91
|
+
const initialPosition = this.initialPosition;
|
|
92
|
+
this.position =
|
|
93
|
+
initialPosition && isPointInside(initialPosition, this.container.canvas.size, Vector.origin)
|
|
94
|
+
? initialPosition
|
|
95
|
+
: this.calcPosition();
|
|
96
|
+
}
|
|
97
|
+
calcPosition() {
|
|
98
|
+
const exactPosition = calcPositionOrRandomFromSizeRanged({
|
|
99
|
+
size: this.container.canvas.size,
|
|
100
|
+
position: this.options.position,
|
|
101
|
+
});
|
|
102
|
+
return Vector.create(exactPosition.x, exactPosition.y);
|
|
103
|
+
}
|
|
104
|
+
updateParticlePosition(particle, v) {
|
|
105
|
+
var _a;
|
|
106
|
+
if (particle.destroyed) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const container = this.container, canvasSize = container.canvas.size;
|
|
110
|
+
if (particle.needsNewPosition) {
|
|
111
|
+
const newPosition = calcPositionOrRandomFromSize({ size: canvasSize });
|
|
112
|
+
particle.position.setTo(newPosition);
|
|
113
|
+
particle.velocity.setTo(particle.initialVelocity);
|
|
114
|
+
particle.absorberOrbit = undefined;
|
|
115
|
+
particle.needsNewPosition = false;
|
|
116
|
+
}
|
|
117
|
+
if (this.options.orbits) {
|
|
118
|
+
if (particle.absorberOrbit === undefined) {
|
|
119
|
+
particle.absorberOrbit = Vector.create(0, 0);
|
|
120
|
+
particle.absorberOrbit.length = getDistance(particle.getPosition(), this.position);
|
|
121
|
+
particle.absorberOrbit.angle = getRandom() * Math.PI * 2;
|
|
122
|
+
}
|
|
123
|
+
if (particle.absorberOrbit.length <= this.size && !this.options.destroy) {
|
|
124
|
+
const minSize = Math.min(canvasSize.width, canvasSize.height);
|
|
125
|
+
particle.absorberOrbit.length = minSize * (1 + (getRandom() * 0.2 - 0.1));
|
|
126
|
+
}
|
|
127
|
+
if (particle.absorberOrbitDirection === undefined) {
|
|
128
|
+
particle.absorberOrbitDirection =
|
|
129
|
+
particle.velocity.x >= 0 ? "clockwise" : "counter-clockwise";
|
|
130
|
+
}
|
|
131
|
+
const orbitRadius = particle.absorberOrbit.length, orbitAngle = particle.absorberOrbit.angle, orbitDirection = particle.absorberOrbitDirection;
|
|
132
|
+
particle.velocity.setTo(Vector.origin);
|
|
133
|
+
const updateFunc = {
|
|
134
|
+
x: orbitDirection === "clockwise" ? Math.cos : Math.sin,
|
|
135
|
+
y: orbitDirection === "clockwise" ? Math.sin : Math.cos,
|
|
136
|
+
};
|
|
137
|
+
particle.position.x = this.position.x + orbitRadius * updateFunc.x(orbitAngle);
|
|
138
|
+
particle.position.y = this.position.y + orbitRadius * updateFunc.y(orbitAngle);
|
|
139
|
+
particle.absorberOrbit.length -= v.length;
|
|
140
|
+
particle.absorberOrbit.angle +=
|
|
141
|
+
((((_a = particle.retina.moveSpeed) !== null && _a !== void 0 ? _a : 0) * container.retina.pixelRatio) / 100) *
|
|
142
|
+
container.retina.reduceFactor;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const addV = Vector.origin;
|
|
146
|
+
addV.length = v.length;
|
|
147
|
+
addV.angle = v.angle;
|
|
148
|
+
particle.velocity.addTo(addV);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { executeOnSingleOrMultiple, itemFromSingleOrMultiple } from "@tsparticles/engine";
|
|
2
|
+
import { AbsorberInstance } from "./AbsorberInstance";
|
|
3
|
+
export class Absorbers {
|
|
4
|
+
constructor(container) {
|
|
5
|
+
this.container = container;
|
|
6
|
+
this.array = [];
|
|
7
|
+
this.absorbers = [];
|
|
8
|
+
this.interactivityAbsorbers = [];
|
|
9
|
+
container.getAbsorber = (idxOrName) => idxOrName === undefined || typeof idxOrName === "number"
|
|
10
|
+
? this.array[idxOrName || 0]
|
|
11
|
+
: this.array.find((t) => t.name === idxOrName);
|
|
12
|
+
container.addAbsorber = (options, position) => this.addAbsorber(options, position);
|
|
13
|
+
}
|
|
14
|
+
addAbsorber(options, position) {
|
|
15
|
+
const absorber = new AbsorberInstance(this, this.container, options, position);
|
|
16
|
+
this.array.push(absorber);
|
|
17
|
+
return absorber;
|
|
18
|
+
}
|
|
19
|
+
draw(context) {
|
|
20
|
+
for (const absorber of this.array) {
|
|
21
|
+
absorber.draw(context);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
handleClickMode(mode) {
|
|
25
|
+
const absorberOptions = this.absorbers, modeAbsorbers = this.interactivityAbsorbers;
|
|
26
|
+
if (mode === "absorber") {
|
|
27
|
+
const absorbersModeOptions = itemFromSingleOrMultiple(modeAbsorbers), absorbersOptions = absorbersModeOptions !== null && absorbersModeOptions !== void 0 ? absorbersModeOptions : itemFromSingleOrMultiple(absorberOptions), aPosition = this.container.interactivity.mouse.clickPosition;
|
|
28
|
+
this.addAbsorber(absorbersOptions, aPosition);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async init() {
|
|
32
|
+
this.absorbers = this.container.actualOptions.absorbers;
|
|
33
|
+
this.interactivityAbsorbers = this.container.actualOptions.interactivity.modes.absorbers;
|
|
34
|
+
executeOnSingleOrMultiple(this.absorbers, (absorber) => {
|
|
35
|
+
this.addAbsorber(absorber);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
particleUpdate(particle) {
|
|
39
|
+
for (const absorber of this.array) {
|
|
40
|
+
absorber.attract(particle);
|
|
41
|
+
if (particle.destroyed) {
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
removeAbsorber(absorber) {
|
|
47
|
+
const index = this.array.indexOf(absorber);
|
|
48
|
+
if (index >= 0) {
|
|
49
|
+
this.array.splice(index, 1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
resize() {
|
|
53
|
+
for (const absorber of this.array) {
|
|
54
|
+
absorber.resize();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
stop() {
|
|
58
|
+
this.array = [];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { OptionsColor, setRangeValue } from "@tsparticles/engine";
|
|
2
|
+
import { AbsorberSize } from "./AbsorberSize";
|
|
3
|
+
export class Absorber {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.color = new OptionsColor();
|
|
6
|
+
this.color.value = "#000000";
|
|
7
|
+
this.draggable = false;
|
|
8
|
+
this.opacity = 1;
|
|
9
|
+
this.destroy = true;
|
|
10
|
+
this.orbits = false;
|
|
11
|
+
this.size = new AbsorberSize();
|
|
12
|
+
}
|
|
13
|
+
load(data) {
|
|
14
|
+
if (data === undefined) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (data.color !== undefined) {
|
|
18
|
+
this.color = OptionsColor.create(this.color, data.color);
|
|
19
|
+
}
|
|
20
|
+
if (data.draggable !== undefined) {
|
|
21
|
+
this.draggable = data.draggable;
|
|
22
|
+
}
|
|
23
|
+
this.name = data.name;
|
|
24
|
+
if (data.opacity !== undefined) {
|
|
25
|
+
this.opacity = data.opacity;
|
|
26
|
+
}
|
|
27
|
+
if (data.position !== undefined) {
|
|
28
|
+
this.position = {};
|
|
29
|
+
if (data.position.x !== undefined) {
|
|
30
|
+
this.position.x = setRangeValue(data.position.x);
|
|
31
|
+
}
|
|
32
|
+
if (data.position.y !== undefined) {
|
|
33
|
+
this.position.y = setRangeValue(data.position.y);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (data.size !== undefined) {
|
|
37
|
+
this.size.load(data.size);
|
|
38
|
+
}
|
|
39
|
+
if (data.destroy !== undefined) {
|
|
40
|
+
this.destroy = data.destroy;
|
|
41
|
+
}
|
|
42
|
+
if (data.orbits !== undefined) {
|
|
43
|
+
this.orbits = data.orbits;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AbsorberSizeLimit } from "./AbsorberSizeLimit";
|
|
2
|
+
import { ValueWithRandom } from "@tsparticles/engine";
|
|
3
|
+
export class AbsorberSize extends ValueWithRandom {
|
|
4
|
+
constructor() {
|
|
5
|
+
super();
|
|
6
|
+
this.density = 5;
|
|
7
|
+
this.value = 50;
|
|
8
|
+
this.limit = new AbsorberSizeLimit();
|
|
9
|
+
}
|
|
10
|
+
load(data) {
|
|
11
|
+
if (!data) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
super.load(data);
|
|
15
|
+
if (data.density !== undefined) {
|
|
16
|
+
this.density = data.density;
|
|
17
|
+
}
|
|
18
|
+
if (typeof data.limit === "number") {
|
|
19
|
+
this.limit.radius = data.limit;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.limit.load(data.limit);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export class AbsorberSizeLimit {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.radius = 0;
|
|
4
|
+
this.mass = 0;
|
|
5
|
+
}
|
|
6
|
+
load(data) {
|
|
7
|
+
if (!data) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
if (data.mass !== undefined) {
|
|
11
|
+
this.mass = data.mass;
|
|
12
|
+
}
|
|
13
|
+
if (data.radius !== undefined) {
|
|
14
|
+
this.radius = data.radius;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { executeOnSingleOrMultiple, isInArray } from "@tsparticles/engine";
|
|
2
|
+
import { Absorber } from "./Options/Classes/Absorber";
|
|
3
|
+
import { Absorbers } from "./Absorbers";
|
|
4
|
+
class AbsorbersPlugin {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.id = "absorbers";
|
|
7
|
+
}
|
|
8
|
+
getPlugin(container) {
|
|
9
|
+
return new Absorbers(container);
|
|
10
|
+
}
|
|
11
|
+
loadOptions(options, source) {
|
|
12
|
+
var _a, _b;
|
|
13
|
+
if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (source === null || source === void 0 ? void 0 : source.absorbers) {
|
|
17
|
+
options.absorbers = executeOnSingleOrMultiple(source.absorbers, (absorber) => {
|
|
18
|
+
const tmp = new Absorber();
|
|
19
|
+
tmp.load(absorber);
|
|
20
|
+
return tmp;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
options.interactivity.modes.absorbers = executeOnSingleOrMultiple((_b = (_a = source === null || source === void 0 ? void 0 : source.interactivity) === null || _a === void 0 ? void 0 : _a.modes) === null || _b === void 0 ? void 0 : _b.absorbers, (absorber) => {
|
|
24
|
+
const tmp = new Absorber();
|
|
25
|
+
tmp.load(absorber);
|
|
26
|
+
return tmp;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
needsPlugin(options) {
|
|
30
|
+
var _a, _b, _c;
|
|
31
|
+
if (!options) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
const absorbers = options.absorbers;
|
|
35
|
+
if (absorbers instanceof Array) {
|
|
36
|
+
return !!absorbers.length;
|
|
37
|
+
}
|
|
38
|
+
else if (absorbers) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
else if (((_c = (_b = (_a = options.interactivity) === null || _a === void 0 ? void 0 : _a.events) === null || _b === void 0 ? void 0 : _b.onClick) === null || _c === void 0 ? void 0 : _c.mode) &&
|
|
42
|
+
isInArray("absorber", options.interactivity.events.onClick.mode)) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export async function loadAbsorbersPlugin(engine) {
|
|
49
|
+
const plugin = new AbsorbersPlugin();
|
|
50
|
+
await engine.addPlugin(plugin);
|
|
51
|
+
}
|
|
52
|
+
export * from "./AbsorberContainer";
|
|
53
|
+
export * from "./Enums/AbsorberClickMode";
|
package/browser/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbsorberInstance = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
5
|
+
const Absorber_1 = require("./Options/Classes/Absorber");
|
|
6
|
+
class AbsorberInstance {
|
|
7
|
+
constructor(absorbers, container, options, position) {
|
|
8
|
+
var _a, _b, _c;
|
|
9
|
+
this.absorbers = absorbers;
|
|
10
|
+
this.container = container;
|
|
11
|
+
this.initialPosition = position ? engine_1.Vector.create(position.x, position.y) : undefined;
|
|
12
|
+
if (options instanceof Absorber_1.Absorber) {
|
|
13
|
+
this.options = options;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
this.options = new Absorber_1.Absorber();
|
|
17
|
+
this.options.load(options);
|
|
18
|
+
}
|
|
19
|
+
this.dragging = false;
|
|
20
|
+
this.name = this.options.name;
|
|
21
|
+
this.opacity = this.options.opacity;
|
|
22
|
+
this.size = (0, engine_1.getRangeValue)(this.options.size.value) * container.retina.pixelRatio;
|
|
23
|
+
this.mass = this.size * this.options.size.density * container.retina.reduceFactor;
|
|
24
|
+
const limit = this.options.size.limit;
|
|
25
|
+
this.limit = {
|
|
26
|
+
radius: limit.radius * container.retina.pixelRatio * container.retina.reduceFactor,
|
|
27
|
+
mass: limit.mass,
|
|
28
|
+
};
|
|
29
|
+
this.color = (_a = (0, engine_1.rangeColorToRgb)(this.options.color)) !== null && _a !== void 0 ? _a : {
|
|
30
|
+
b: 0,
|
|
31
|
+
g: 0,
|
|
32
|
+
r: 0,
|
|
33
|
+
};
|
|
34
|
+
this.position = (_c = (_b = this.initialPosition) === null || _b === void 0 ? void 0 : _b.copy()) !== null && _c !== void 0 ? _c : this.calcPosition();
|
|
35
|
+
}
|
|
36
|
+
attract(particle) {
|
|
37
|
+
const container = this.container, options = this.options;
|
|
38
|
+
if (options.draggable) {
|
|
39
|
+
const mouse = container.interactivity.mouse;
|
|
40
|
+
if (mouse.clicking && mouse.downPosition) {
|
|
41
|
+
const mouseDist = (0, engine_1.getDistance)(this.position, mouse.downPosition);
|
|
42
|
+
if (mouseDist <= this.size) {
|
|
43
|
+
this.dragging = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
this.dragging = false;
|
|
48
|
+
}
|
|
49
|
+
if (this.dragging && mouse.position) {
|
|
50
|
+
this.position.x = mouse.position.x;
|
|
51
|
+
this.position.y = mouse.position.y;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const pos = particle.getPosition(), { dx, dy, distance } = (0, engine_1.getDistances)(this.position, pos), v = engine_1.Vector.create(dx, dy);
|
|
55
|
+
v.length = (this.mass / Math.pow(distance, 2)) * container.retina.reduceFactor;
|
|
56
|
+
if (distance < this.size + particle.getRadius()) {
|
|
57
|
+
const sizeFactor = particle.getRadius() * 0.033 * container.retina.pixelRatio;
|
|
58
|
+
if ((this.size > particle.getRadius() && distance < this.size - particle.getRadius()) ||
|
|
59
|
+
(particle.absorberOrbit !== undefined && particle.absorberOrbit.length < 0)) {
|
|
60
|
+
if (options.destroy) {
|
|
61
|
+
particle.destroy();
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
particle.needsNewPosition = true;
|
|
65
|
+
this.updateParticlePosition(particle, v);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
if (options.destroy) {
|
|
70
|
+
particle.size.value -= sizeFactor;
|
|
71
|
+
}
|
|
72
|
+
this.updateParticlePosition(particle, v);
|
|
73
|
+
}
|
|
74
|
+
if (this.limit.radius <= 0 || this.size < this.limit.radius) {
|
|
75
|
+
this.size += sizeFactor;
|
|
76
|
+
}
|
|
77
|
+
if (this.limit.mass <= 0 || this.mass < this.limit.mass) {
|
|
78
|
+
this.mass += sizeFactor * this.options.size.density * container.retina.reduceFactor;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
this.updateParticlePosition(particle, v);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
draw(context) {
|
|
86
|
+
context.translate(this.position.x, this.position.y);
|
|
87
|
+
context.beginPath();
|
|
88
|
+
context.arc(0, 0, this.size, 0, Math.PI * 2, false);
|
|
89
|
+
context.closePath();
|
|
90
|
+
context.fillStyle = (0, engine_1.getStyleFromRgb)(this.color, this.opacity);
|
|
91
|
+
context.fill();
|
|
92
|
+
}
|
|
93
|
+
resize() {
|
|
94
|
+
const initialPosition = this.initialPosition;
|
|
95
|
+
this.position =
|
|
96
|
+
initialPosition && (0, engine_1.isPointInside)(initialPosition, this.container.canvas.size, engine_1.Vector.origin)
|
|
97
|
+
? initialPosition
|
|
98
|
+
: this.calcPosition();
|
|
99
|
+
}
|
|
100
|
+
calcPosition() {
|
|
101
|
+
const exactPosition = (0, engine_1.calcPositionOrRandomFromSizeRanged)({
|
|
102
|
+
size: this.container.canvas.size,
|
|
103
|
+
position: this.options.position,
|
|
104
|
+
});
|
|
105
|
+
return engine_1.Vector.create(exactPosition.x, exactPosition.y);
|
|
106
|
+
}
|
|
107
|
+
updateParticlePosition(particle, v) {
|
|
108
|
+
var _a;
|
|
109
|
+
if (particle.destroyed) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const container = this.container, canvasSize = container.canvas.size;
|
|
113
|
+
if (particle.needsNewPosition) {
|
|
114
|
+
const newPosition = (0, engine_1.calcPositionOrRandomFromSize)({ size: canvasSize });
|
|
115
|
+
particle.position.setTo(newPosition);
|
|
116
|
+
particle.velocity.setTo(particle.initialVelocity);
|
|
117
|
+
particle.absorberOrbit = undefined;
|
|
118
|
+
particle.needsNewPosition = false;
|
|
119
|
+
}
|
|
120
|
+
if (this.options.orbits) {
|
|
121
|
+
if (particle.absorberOrbit === undefined) {
|
|
122
|
+
particle.absorberOrbit = engine_1.Vector.create(0, 0);
|
|
123
|
+
particle.absorberOrbit.length = (0, engine_1.getDistance)(particle.getPosition(), this.position);
|
|
124
|
+
particle.absorberOrbit.angle = (0, engine_1.getRandom)() * Math.PI * 2;
|
|
125
|
+
}
|
|
126
|
+
if (particle.absorberOrbit.length <= this.size && !this.options.destroy) {
|
|
127
|
+
const minSize = Math.min(canvasSize.width, canvasSize.height);
|
|
128
|
+
particle.absorberOrbit.length = minSize * (1 + ((0, engine_1.getRandom)() * 0.2 - 0.1));
|
|
129
|
+
}
|
|
130
|
+
if (particle.absorberOrbitDirection === undefined) {
|
|
131
|
+
particle.absorberOrbitDirection =
|
|
132
|
+
particle.velocity.x >= 0 ? "clockwise" : "counter-clockwise";
|
|
133
|
+
}
|
|
134
|
+
const orbitRadius = particle.absorberOrbit.length, orbitAngle = particle.absorberOrbit.angle, orbitDirection = particle.absorberOrbitDirection;
|
|
135
|
+
particle.velocity.setTo(engine_1.Vector.origin);
|
|
136
|
+
const updateFunc = {
|
|
137
|
+
x: orbitDirection === "clockwise" ? Math.cos : Math.sin,
|
|
138
|
+
y: orbitDirection === "clockwise" ? Math.sin : Math.cos,
|
|
139
|
+
};
|
|
140
|
+
particle.position.x = this.position.x + orbitRadius * updateFunc.x(orbitAngle);
|
|
141
|
+
particle.position.y = this.position.y + orbitRadius * updateFunc.y(orbitAngle);
|
|
142
|
+
particle.absorberOrbit.length -= v.length;
|
|
143
|
+
particle.absorberOrbit.angle +=
|
|
144
|
+
((((_a = particle.retina.moveSpeed) !== null && _a !== void 0 ? _a : 0) * container.retina.pixelRatio) / 100) *
|
|
145
|
+
container.retina.reduceFactor;
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
const addV = engine_1.Vector.origin;
|
|
149
|
+
addV.length = v.length;
|
|
150
|
+
addV.angle = v.angle;
|
|
151
|
+
particle.velocity.addTo(addV);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
exports.AbsorberInstance = AbsorberInstance;
|