@tsparticles/plugin-absorbers 3.0.0-alpha.1 → 3.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.
@@ -2,9 +2,61 @@ import { Vector, calcPositionOrRandomFromSize, calcPositionOrRandomFromSizeRange
2
2
  import { Absorber } from "./Options/Classes/Absorber";
3
3
  export class AbsorberInstance {
4
4
  constructor(absorbers, container, options, position) {
5
- var _a, _b, _c;
6
5
  this.absorbers = absorbers;
7
6
  this.container = container;
7
+ this._calcPosition = () => {
8
+ const exactPosition = calcPositionOrRandomFromSizeRanged({
9
+ size: this.container.canvas.size,
10
+ position: this.options.position,
11
+ });
12
+ return Vector.create(exactPosition.x, exactPosition.y);
13
+ };
14
+ this._updateParticlePosition = (particle, v) => {
15
+ if (particle.destroyed) {
16
+ return;
17
+ }
18
+ const container = this.container, canvasSize = container.canvas.size;
19
+ if (particle.needsNewPosition) {
20
+ const newPosition = calcPositionOrRandomFromSize({ size: canvasSize });
21
+ particle.position.setTo(newPosition);
22
+ particle.velocity.setTo(particle.initialVelocity);
23
+ particle.absorberOrbit = undefined;
24
+ particle.needsNewPosition = false;
25
+ }
26
+ if (this.options.orbits) {
27
+ if (particle.absorberOrbit === undefined) {
28
+ particle.absorberOrbit = Vector.create(0, 0);
29
+ particle.absorberOrbit.length = getDistance(particle.getPosition(), this.position);
30
+ particle.absorberOrbit.angle = getRandom() * Math.PI * 2;
31
+ }
32
+ if (particle.absorberOrbit.length <= this.size && !this.options.destroy) {
33
+ const minSize = Math.min(canvasSize.width, canvasSize.height);
34
+ particle.absorberOrbit.length = minSize * (1 + (getRandom() * 0.2 - 0.1));
35
+ }
36
+ if (particle.absorberOrbitDirection === undefined) {
37
+ particle.absorberOrbitDirection =
38
+ particle.velocity.x >= 0 ? "clockwise" : "counter-clockwise";
39
+ }
40
+ const orbitRadius = particle.absorberOrbit.length, orbitAngle = particle.absorberOrbit.angle, orbitDirection = particle.absorberOrbitDirection;
41
+ particle.velocity.setTo(Vector.origin);
42
+ const updateFunc = {
43
+ x: orbitDirection === "clockwise" ? Math.cos : Math.sin,
44
+ y: orbitDirection === "clockwise" ? Math.sin : Math.cos,
45
+ };
46
+ particle.position.x = this.position.x + orbitRadius * updateFunc.x(orbitAngle);
47
+ particle.position.y = this.position.y + orbitRadius * updateFunc.y(orbitAngle);
48
+ particle.absorberOrbit.length -= v.length;
49
+ particle.absorberOrbit.angle +=
50
+ (((particle.retina.moveSpeed ?? 0) * container.retina.pixelRatio) / 100) *
51
+ container.retina.reduceFactor;
52
+ }
53
+ else {
54
+ const addV = Vector.origin;
55
+ addV.length = v.length;
56
+ addV.angle = v.angle;
57
+ particle.velocity.addTo(addV);
58
+ }
59
+ };
8
60
  this.initialPosition = position ? Vector.create(position.x, position.y) : undefined;
9
61
  if (options instanceof Absorber) {
10
62
  this.options = options;
@@ -23,12 +75,12 @@ export class AbsorberInstance {
23
75
  radius: limit.radius * container.retina.pixelRatio * container.retina.reduceFactor,
24
76
  mass: limit.mass,
25
77
  };
26
- this.color = (_a = rangeColorToRgb(this.options.color)) !== null && _a !== void 0 ? _a : {
78
+ this.color = rangeColorToRgb(this.options.color) ?? {
27
79
  b: 0,
28
80
  g: 0,
29
81
  r: 0,
30
82
  };
31
- this.position = (_c = (_b = this.initialPosition) === null || _b === void 0 ? void 0 : _b.copy()) !== null && _c !== void 0 ? _c : this.calcPosition();
83
+ this.position = this.initialPosition?.copy() ?? this._calcPosition();
32
84
  }
33
85
  attract(particle) {
34
86
  const container = this.container, options = this.options;
@@ -59,14 +111,14 @@ export class AbsorberInstance {
59
111
  }
60
112
  else {
61
113
  particle.needsNewPosition = true;
62
- this.updateParticlePosition(particle, v);
114
+ this._updateParticlePosition(particle, v);
63
115
  }
64
116
  }
65
117
  else {
66
118
  if (options.destroy) {
67
119
  particle.size.value -= sizeFactor;
68
120
  }
69
- this.updateParticlePosition(particle, v);
121
+ this._updateParticlePosition(particle, v);
70
122
  }
71
123
  if (this.limit.radius <= 0 || this.size < this.limit.radius) {
72
124
  this.size += sizeFactor;
@@ -76,7 +128,7 @@ export class AbsorberInstance {
76
128
  }
77
129
  }
78
130
  else {
79
- this.updateParticlePosition(particle, v);
131
+ this._updateParticlePosition(particle, v);
80
132
  }
81
133
  }
82
134
  draw(context) {
@@ -92,60 +144,6 @@ export class AbsorberInstance {
92
144
  this.position =
93
145
  initialPosition && isPointInside(initialPosition, this.container.canvas.size, Vector.origin)
94
146
  ? 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
- }
147
+ : this._calcPosition();
150
148
  }
151
149
  }
package/esm/Absorbers.js CHANGED
@@ -1,4 +1,4 @@
1
- import { executeOnSingleOrMultiple, itemFromSingleOrMultiple } from "@tsparticles/engine";
1
+ import { executeOnSingleOrMultiple, isNumber, itemFromSingleOrMultiple, } from "@tsparticles/engine";
2
2
  import { AbsorberInstance } from "./AbsorberInstance";
3
3
  export class Absorbers {
4
4
  constructor(container) {
@@ -6,7 +6,7 @@ export class Absorbers {
6
6
  this.array = [];
7
7
  this.absorbers = [];
8
8
  this.interactivityAbsorbers = [];
9
- container.getAbsorber = (idxOrName) => idxOrName === undefined || typeof idxOrName === "number"
9
+ container.getAbsorber = (idxOrName) => idxOrName === undefined || isNumber(idxOrName)
10
10
  ? this.array[idxOrName || 0]
11
11
  : this.array.find((t) => t.name === idxOrName);
12
12
  container.addAbsorber = (options, position) => this.addAbsorber(options, position);
@@ -24,7 +24,7 @@ export class Absorbers {
24
24
  handleClickMode(mode) {
25
25
  const absorberOptions = this.absorbers, modeAbsorbers = this.interactivityAbsorbers;
26
26
  if (mode === "absorber") {
27
- const absorbersModeOptions = itemFromSingleOrMultiple(modeAbsorbers), absorbersOptions = absorbersModeOptions !== null && absorbersModeOptions !== void 0 ? absorbersModeOptions : itemFromSingleOrMultiple(absorberOptions), aPosition = this.container.interactivity.mouse.clickPosition;
27
+ const absorbersModeOptions = itemFromSingleOrMultiple(modeAbsorbers), absorbersOptions = absorbersModeOptions ?? itemFromSingleOrMultiple(absorberOptions), aPosition = this.container.interactivity.mouse.clickPosition;
28
28
  this.addAbsorber(absorbersOptions, aPosition);
29
29
  }
30
30
  }
@@ -1,4 +1,4 @@
1
- import { OptionsColor, setRangeValue } from "@tsparticles/engine";
1
+ import { OptionsColor, setRangeValue, } from "@tsparticles/engine";
2
2
  import { AbsorberSize } from "./AbsorberSize";
3
3
  export class Absorber {
4
4
  constructor() {
@@ -1,5 +1,5 @@
1
+ import { ValueWithRandom, isNumber } from "@tsparticles/engine";
1
2
  import { AbsorberSizeLimit } from "./AbsorberSizeLimit";
2
- import { ValueWithRandom } from "@tsparticles/engine";
3
3
  export class AbsorberSize extends ValueWithRandom {
4
4
  constructor() {
5
5
  super();
@@ -15,7 +15,7 @@ export class AbsorberSize extends ValueWithRandom {
15
15
  if (data.density !== undefined) {
16
16
  this.density = data.density;
17
17
  }
18
- if (typeof data.limit === "number") {
18
+ if (isNumber(data.limit)) {
19
19
  this.limit.radius = data.limit;
20
20
  }
21
21
  else {
package/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { executeOnSingleOrMultiple, isInArray } from "@tsparticles/engine";
1
+ import { executeOnSingleOrMultiple, isArray, isInArray, } from "@tsparticles/engine";
2
2
  import { Absorber } from "./Options/Classes/Absorber";
3
3
  import { Absorbers } from "./Absorbers";
4
4
  class AbsorbersPlugin {
@@ -9,45 +9,42 @@ class AbsorbersPlugin {
9
9
  return new Absorbers(container);
10
10
  }
11
11
  loadOptions(options, source) {
12
- var _a, _b;
13
12
  if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
14
13
  return;
15
14
  }
16
- if (source === null || source === void 0 ? void 0 : source.absorbers) {
15
+ if (source?.absorbers) {
17
16
  options.absorbers = executeOnSingleOrMultiple(source.absorbers, (absorber) => {
18
17
  const tmp = new Absorber();
19
18
  tmp.load(absorber);
20
19
  return tmp;
21
20
  });
22
21
  }
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) => {
22
+ options.interactivity.modes.absorbers = executeOnSingleOrMultiple(source?.interactivity?.modes?.absorbers, (absorber) => {
24
23
  const tmp = new Absorber();
25
24
  tmp.load(absorber);
26
25
  return tmp;
27
26
  });
28
27
  }
29
28
  needsPlugin(options) {
30
- var _a, _b, _c;
31
29
  if (!options) {
32
30
  return false;
33
31
  }
34
32
  const absorbers = options.absorbers;
35
- if (absorbers instanceof Array) {
33
+ if (isArray(absorbers)) {
36
34
  return !!absorbers.length;
37
35
  }
38
36
  else if (absorbers) {
39
37
  return true;
40
38
  }
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) &&
39
+ else if (options.interactivity?.events?.onClick?.mode &&
42
40
  isInArray("absorber", options.interactivity.events.onClick.mode)) {
43
41
  return true;
44
42
  }
45
43
  return false;
46
44
  }
47
45
  }
48
- export async function loadAbsorbersPlugin(engine) {
49
- const plugin = new AbsorbersPlugin();
50
- await engine.addPlugin(plugin);
46
+ export async function loadAbsorbersPlugin(engine, refresh = true) {
47
+ await engine.addPlugin(new AbsorbersPlugin(), refresh);
51
48
  }
52
49
  export * from "./AbsorberContainer";
53
50
  export * from "./Enums/AbsorberClickMode";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/plugin-absorbers",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-beta.0",
4
4
  "description": "tsParticles absorbers plugin",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -72,10 +72,11 @@
72
72
  "unpkg": "tsparticles.plugin.absorbers.min.js",
73
73
  "module": "esm/index.js",
74
74
  "types": "types/index.d.ts",
75
+ "sideEffects": false,
76
+ "dependencies": {
77
+ "@tsparticles/engine": "^3.0.0-beta.0"
78
+ },
75
79
  "publishConfig": {
76
80
  "access": "public"
77
- },
78
- "dependencies": {
79
- "@tsparticles/engine": "^3.0.0-alpha.1"
80
81
  }
81
- }
82
+ }