@tsparticles/engine 4.0.0-alpha.22 → 4.0.0-alpha.24

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 (53) hide show
  1. package/515.min.js +1 -1
  2. package/browser/Core/Canvas.js +17 -1
  3. package/browser/Core/Container.js +1 -4
  4. package/browser/Core/Engine.js +1 -1
  5. package/browser/Core/Particle.js +1 -2
  6. package/browser/Core/Utils/Constants.js +1 -1
  7. package/browser/Core/Utils/QuadTree.js +54 -33
  8. package/browser/Core/Utils/Ranges.js +1 -1
  9. package/browser/Utils/CanvasUtils.js +12 -10
  10. package/browser/Utils/ColorUtils.js +9 -1
  11. package/browser/Utils/MathUtils.js +4 -6
  12. package/cjs/Core/Canvas.js +17 -1
  13. package/cjs/Core/Container.js +1 -4
  14. package/cjs/Core/Engine.js +1 -1
  15. package/cjs/Core/Particle.js +1 -2
  16. package/cjs/Core/Utils/Constants.js +1 -1
  17. package/cjs/Core/Utils/QuadTree.js +54 -33
  18. package/cjs/Core/Utils/Ranges.js +1 -1
  19. package/cjs/Utils/CanvasUtils.js +12 -10
  20. package/cjs/Utils/ColorUtils.js +9 -1
  21. package/cjs/Utils/MathUtils.js +4 -6
  22. package/dist_browser_Core_Container_js.js +5 -5
  23. package/esm/Core/Canvas.js +17 -1
  24. package/esm/Core/Container.js +1 -4
  25. package/esm/Core/Engine.js +1 -1
  26. package/esm/Core/Particle.js +1 -2
  27. package/esm/Core/Utils/Constants.js +1 -1
  28. package/esm/Core/Utils/QuadTree.js +54 -33
  29. package/esm/Core/Utils/Ranges.js +1 -1
  30. package/esm/Utils/CanvasUtils.js +12 -10
  31. package/esm/Utils/ColorUtils.js +9 -1
  32. package/esm/Utils/MathUtils.js +4 -6
  33. package/package.json +1 -1
  34. package/report.html +1 -1
  35. package/tsparticles.engine.js +9 -9
  36. package/tsparticles.engine.min.js +2 -2
  37. package/types/Core/Canvas.d.ts +5 -0
  38. package/types/Core/Interfaces/IContainerPlugin.d.ts +2 -0
  39. package/types/Core/Interfaces/IShapeDrawData.d.ts +8 -0
  40. package/types/Core/Utils/Constants.d.ts +1 -1
  41. package/types/Core/Utils/QuadTree.d.ts +3 -4
  42. package/types/Utils/CanvasUtils.d.ts +0 -3
  43. package/types/Utils/ColorUtils.d.ts +2 -0
  44. package/umd/Core/Canvas.js +16 -0
  45. package/umd/Core/Container.js +1 -4
  46. package/umd/Core/Engine.js +1 -1
  47. package/umd/Core/Particle.js +3 -4
  48. package/umd/Core/Utils/Constants.js +2 -2
  49. package/umd/Core/Utils/QuadTree.js +53 -32
  50. package/umd/Core/Utils/Ranges.js +1 -1
  51. package/umd/Utils/CanvasUtils.js +12 -11
  52. package/umd/Utils/ColorUtils.js +10 -1
  53. package/umd/Utils/MathUtils.js +4 -6
@@ -16,48 +16,68 @@
16
16
  class QuadTree {
17
17
  rectangle;
18
18
  capacity;
19
- _divided;
20
- _points;
21
- _subs;
19
+ _points = [];
20
+ _subs = null;
22
21
  constructor(rectangle, capacity) {
23
22
  this.rectangle = rectangle;
24
23
  this.capacity = capacity;
25
- this._points = [];
26
- this._divided = false;
27
- this._subs = [];
28
24
  }
29
25
  insert(point) {
30
26
  if (!this.rectangle.contains(point.position)) {
31
27
  return false;
32
28
  }
33
- if (this._points.length < this.capacity) {
34
- this._points.push(point);
35
- return true;
36
- }
37
- if (!this._divided) {
29
+ const subs = this._subs;
30
+ if (!subs) {
31
+ if (this._points.length < this.capacity) {
32
+ this._points.push(point);
33
+ return true;
34
+ }
38
35
  this._subdivide();
36
+ const newSubs = this._subs;
37
+ for (const p of this._points) {
38
+ for (let s = 0; s < Constants_js_1.subdivideCount; s++) {
39
+ if (newSubs?.[s]?.insert(p)) {
40
+ break;
41
+ }
42
+ }
43
+ }
44
+ this._points.length = 0;
45
+ for (let s = 0; s < Constants_js_1.subdivideCount; s++) {
46
+ if (newSubs?.[s]?.insert(point)) {
47
+ return true;
48
+ }
49
+ }
50
+ return false;
39
51
  }
40
- return this._subs.some(sub => sub.insert(point));
52
+ for (let s = 0; s < Constants_js_1.subdivideCount; s++) {
53
+ if (subs[s]?.insert(point)) {
54
+ return true;
55
+ }
56
+ }
57
+ return false;
41
58
  }
42
- query(range, check) {
43
- const res = [];
59
+ query(range, check, out = []) {
44
60
  if (!range.intersects(this.rectangle)) {
45
- return [];
61
+ return out;
46
62
  }
47
- for (const p of this._points) {
48
- if (!range.contains(p.position) &&
49
- (0, MathUtils_js_1.getDistance)(range.position, p.position) > p.particle.getRadius() &&
50
- (!check || check(p.particle))) {
63
+ const points = this._points;
64
+ for (const p of points) {
65
+ const particle = p.particle;
66
+ if (!range.contains(p.position) && (0, MathUtils_js_1.getDistance)(range.position, p.position) > particle.getRadius()) {
67
+ continue;
68
+ }
69
+ if (check && !check(particle)) {
51
70
  continue;
52
71
  }
53
- res.push(p.particle);
72
+ out.push(particle);
54
73
  }
55
- if (this._divided) {
56
- for (const sub of this._subs) {
57
- res.push(...sub.query(range, check));
74
+ const subs = this._subs;
75
+ if (subs) {
76
+ for (const s of subs) {
77
+ s.query(range, check, out);
58
78
  }
59
79
  }
60
- return res;
80
+ return out;
61
81
  }
62
82
  queryCircle(position, radius, check) {
63
83
  return this.query(new Ranges_js_1.Circle(position.x, position.y, radius), check);
@@ -65,14 +85,15 @@
65
85
  queryRectangle(position, size, check) {
66
86
  return this.query(new Ranges_js_1.Rectangle(position.x, position.y, size.width, size.height), check);
67
87
  }
68
- _subdivide = () => {
69
- const { x, y } = this.rectangle.position, { width, height } = this.rectangle.size, { capacity } = this;
70
- for (let i = 0; i < Constants_js_1.subdivideCount; i++) {
71
- const fixedIndex = i % Constants_js_1.double;
72
- this._subs.push(new QuadTree(new Ranges_js_1.Rectangle(x + width * Constants_js_1.half * fixedIndex, y + height * Constants_js_1.half * (Math.round(i * Constants_js_1.half) - fixedIndex), width * Constants_js_1.half, height * Constants_js_1.half), capacity));
73
- }
74
- this._divided = true;
75
- };
88
+ _subdivide() {
89
+ const rect = this.rectangle, { x, y } = rect.position, { width, height } = rect.size, halfWidth = width * Constants_js_1.half, halfHeight = height * Constants_js_1.half, capacity = this.capacity;
90
+ this._subs = [
91
+ new QuadTree(new Ranges_js_1.Rectangle(x, y, halfWidth, halfHeight), capacity),
92
+ new QuadTree(new Ranges_js_1.Rectangle(x + halfWidth, y, halfWidth, halfHeight), capacity),
93
+ new QuadTree(new Ranges_js_1.Rectangle(x, y + halfHeight, halfWidth, halfHeight), capacity),
94
+ new QuadTree(new Ranges_js_1.Rectangle(x + halfWidth, y + halfHeight, halfWidth, halfHeight), capacity),
95
+ ];
96
+ }
76
97
  }
77
98
  exports.QuadTree = QuadTree;
78
99
  });
@@ -37,7 +37,7 @@
37
37
  intersects(range) {
38
38
  const pos1 = this.position, pos2 = range.position, distPos = { x: Math.abs(pos2.x - pos1.x), y: Math.abs(pos2.y - pos1.y) }, r = this.radius;
39
39
  if (range instanceof Circle || range.type === RangeType_js_1.RangeType.circle) {
40
- const circleRange = range, rSum = r + circleRange.radius, dist = Math.sqrt(distPos.x ** Constants_js_1.squareExp + distPos.y ** Constants_js_1.squareExp);
40
+ const circleRange = range, rSum = r + circleRange.radius, dist = Math.hypot(distPos.x, distPos.y);
41
41
  return rSum > dist;
42
42
  }
43
43
  else if (range instanceof Rectangle || range.type === RangeType_js_1.RangeType.rectangle) {
@@ -4,7 +4,7 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "../Core/Utils/Constants.js", "../Enums/Types/AlterType.js"], factory);
7
+ define(["require", "exports", "../Core/Utils/Constants.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -19,9 +19,7 @@
19
19
  exports.drawShapeAfterDraw = drawShapeAfterDraw;
20
20
  exports.drawShapeBeforeDraw = drawShapeBeforeDraw;
21
21
  exports.drawParticlePlugin = drawParticlePlugin;
22
- exports.alterHsl = alterHsl;
23
22
  const Constants_js_1 = require("../Core/Utils/Constants.js");
24
- const AlterType_js_1 = require("../Enums/Types/AlterType.js");
25
23
  function paintBase(context, dimension, baseColor) {
26
24
  context.fillStyle = baseColor ?? "rgba(0,0,0,0)";
27
25
  context.fillRect(Constants_js_1.originPoint.x, Constants_js_1.originPoint.y, dimension.width, dimension.height);
@@ -38,7 +36,10 @@
38
36
  context.clearRect(Constants_js_1.originPoint.x, Constants_js_1.originPoint.y, dimension.width, dimension.height);
39
37
  }
40
38
  function drawParticle(data) {
41
- const { container, context, particle, delta, colorStyles, radius, opacity, transform } = data, pos = particle.getPosition(), transformData = particle.getTransformData(transform);
39
+ const { container, context, particle, delta, colorStyles, radius, opacity, transform } = data, pos = particle.getPosition(), transformData = particle.getTransformData(transform), drawScale = Constants_js_1.defaultZoom, drawPosition = {
40
+ x: pos.x,
41
+ y: pos.y,
42
+ };
42
43
  context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, pos.x, pos.y);
43
44
  if (colorStyles.fill) {
44
45
  context.fillStyle = colorStyles.fill;
@@ -52,13 +53,20 @@
52
53
  context,
53
54
  particle,
54
55
  radius,
56
+ drawRadius: radius * drawScale,
55
57
  opacity,
56
58
  delta,
57
59
  pixelRatio: container.retina.pixelRatio,
58
60
  fill: particle.shapeFill,
59
61
  stroke: strokeWidth > Constants_js_1.minStrokeWidth || !particle.shapeFill,
60
62
  transformData,
63
+ position: { ...pos },
64
+ drawPosition,
65
+ drawScale,
61
66
  };
67
+ for (const plugin of container.plugins) {
68
+ plugin.drawParticleTransform?.(drawData);
69
+ }
62
70
  drawBeforeEffect(container, drawData);
63
71
  drawShapeBeforeDraw(container, drawData);
64
72
  drawShape(container, drawData);
@@ -137,11 +145,4 @@
137
145
  }
138
146
  plugin.drawParticle(context, particle, delta);
139
147
  }
140
- function alterHsl(color, type, value) {
141
- return {
142
- h: color.h,
143
- s: color.s,
144
- l: color.l + (type === AlterType_js_1.AlterType.darken ? -Constants_js_1.lFactor : Constants_js_1.lFactor) * value,
145
- };
146
- }
147
148
  });
@@ -4,7 +4,7 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./MathUtils.js", "../Core/Utils/Constants.js", "./TypeUtils.js", "../Enums/AnimationStatus.js", "./Utils.js"], factory);
7
+ define(["require", "exports", "./MathUtils.js", "../Core/Utils/Constants.js", "./TypeUtils.js", "../Enums/Types/AlterType.js", "../Enums/AnimationStatus.js", "./Utils.js"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
@@ -28,9 +28,11 @@
28
28
  exports.getHslAnimationFromHsl = getHslAnimationFromHsl;
29
29
  exports.updateColorValue = updateColorValue;
30
30
  exports.updateColor = updateColor;
31
+ exports.alterHsl = alterHsl;
31
32
  const MathUtils_js_1 = require("./MathUtils.js");
32
33
  const Constants_js_1 = require("../Core/Utils/Constants.js");
33
34
  const TypeUtils_js_1 = require("./TypeUtils.js");
35
+ const AlterType_js_1 = require("../Enums/Types/AlterType.js");
34
36
  const AnimationStatus_js_1 = require("../Enums/AnimationStatus.js");
35
37
  const Utils_js_1 = require("./Utils.js");
36
38
  const styleCache = new Map(), maxCacheSize = 1000, firstIndex = 0, rgbFixedPrecision = 2, hslFixedPrecision = 2;
@@ -380,4 +382,11 @@
380
382
  updateColorValue(s, ranges.s, true, delta);
381
383
  updateColorValue(l, ranges.l, true, delta);
382
384
  }
385
+ function alterHsl(color, type, value) {
386
+ return {
387
+ h: color.h,
388
+ s: color.s,
389
+ l: color.l + (type === AlterType_js_1.AlterType.darken ? -Constants_js_1.lFactor : Constants_js_1.lFactor) * value,
390
+ };
391
+ }
383
392
  });
@@ -57,10 +57,8 @@
57
57
  return getRandom() * (max - min) + min;
58
58
  }
59
59
  function setAnimationFunctions(nextFrame, cancel) {
60
- _animationLoop.nextFrame = (callback) => nextFrame(callback);
61
- _animationLoop.cancel = (handle) => {
62
- cancel(handle);
63
- };
60
+ _animationLoop.nextFrame = nextFrame;
61
+ _animationLoop.cancel = cancel;
64
62
  }
65
63
  function animate(fn) {
66
64
  return _animationLoop.nextFrame(fn);
@@ -104,8 +102,8 @@
104
102
  : setRangeValue(min, max);
105
103
  }
106
104
  function getDistances(pointA, pointB) {
107
- const dx = pointA.x - pointB.x, dy = pointA.y - pointB.y, squareExp = 2;
108
- return { dx: dx, dy: dy, distance: Math.sqrt(dx ** squareExp + dy ** squareExp) };
105
+ const dx = pointA.x - pointB.x, dy = pointA.y - pointB.y;
106
+ return { dx: dx, dy: dy, distance: Math.hypot(dx, dy) };
109
107
  }
110
108
  function getDistance(pointA, pointB) {
111
109
  return getDistances(pointA, pointB).distance;