@tsparticles/effect-trail 3.0.0-beta.4 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
- import { getDistance, getRangeValue, } from "@tsparticles/engine";
1
+ import { getRangeValue, } from "@tsparticles/engine";
2
2
  export class TrailDrawer {
3
3
  draw(data) {
4
- const { context, radius, particle } = data, pxRatio = particle.container.retina.pixelRatio, currentPos = particle.getPosition();
4
+ const { context, radius, particle } = data, diameter = radius * 2, pxRatio = particle.container.retina.pixelRatio, currentPos = particle.getPosition();
5
5
  if (!particle.trail || !particle.trailLength) {
6
6
  return;
7
7
  }
@@ -16,32 +16,48 @@ export class TrailDrawer {
16
16
  if (particle.trail.length < 2) {
17
17
  return;
18
18
  }
19
- if (particle.trail.length > pathLength) {
19
+ while (particle.trail.length > pathLength) {
20
20
  particle.trail.shift();
21
21
  }
22
22
  const trailLength = Math.min(particle.trail.length, pathLength), offsetPos = {
23
23
  x: currentPos.x,
24
24
  y: currentPos.y,
25
+ }, canvasSize = {
26
+ width: particle.container.canvas.size.width + diameter,
27
+ height: particle.container.canvas.size.height + diameter,
25
28
  };
26
29
  let lastPos = particle.trail[trailLength - 1].position;
30
+ context.setTransform(1, 0, 0, 1, currentPos.x, currentPos.y);
27
31
  for (let i = trailLength; i > 0; i--) {
28
32
  const step = particle.trail[i - 1], position = step.position;
29
- if (getDistance(lastPos, position) > radius * 2) {
30
- continue;
31
- }
32
33
  context.beginPath();
33
34
  context.moveTo(lastPos.x - offsetPos.x, lastPos.y - offsetPos.y);
34
- context.lineTo(position.x - offsetPos.x, position.y - offsetPos.y);
35
- const width = Math.max((i / trailLength) * radius * 2, pxRatio, particle.trailMinWidth ?? -1);
35
+ const warp = {
36
+ x: (lastPos.x + canvasSize.width) % canvasSize.width,
37
+ y: (lastPos.y + canvasSize.height) % canvasSize.height,
38
+ };
39
+ if (Math.abs(lastPos.x - position.x) > canvasSize.width / 2 ||
40
+ Math.abs(lastPos.y - position.y) > canvasSize.height / 2) {
41
+ lastPos = position;
42
+ continue;
43
+ }
44
+ context.lineTo((Math.abs(lastPos.x - position.x) > canvasSize.width / 2 ? warp.x : position.x) - offsetPos.x, (Math.abs(lastPos.y - position.y) > canvasSize.height / 2 ? warp.y : position.y) - offsetPos.y);
45
+ const width = Math.max((i / trailLength) * diameter, pxRatio, particle.trailMinWidth ?? -1);
46
+ const oldAlpha = context.globalAlpha;
47
+ context.globalAlpha = particle.trailFade ? i / trailLength : 1;
36
48
  context.lineWidth = particle.trailMaxWidth ? Math.min(width, particle.trailMaxWidth) : width;
37
49
  context.strokeStyle = step.color;
38
50
  context.stroke();
51
+ context.globalAlpha = oldAlpha;
39
52
  lastPos = position;
40
53
  }
54
+ const { transformData } = data;
55
+ context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, currentPos.x, currentPos.y);
41
56
  }
42
57
  particleInit(container, particle) {
43
58
  particle.trail = [];
44
59
  const effectData = particle.effectData;
60
+ particle.trailFade = effectData?.fade ?? true;
45
61
  particle.trailLength = getRangeValue(effectData?.length ?? 10) * container.retina.pixelRatio;
46
62
  particle.trailMaxWidth = effectData?.maxWidth
47
63
  ? getRangeValue(effectData.maxWidth) * container.retina.pixelRatio
@@ -4,7 +4,7 @@ exports.TrailDrawer = void 0;
4
4
  const engine_1 = require("@tsparticles/engine");
5
5
  class TrailDrawer {
6
6
  draw(data) {
7
- const { context, radius, particle } = data, pxRatio = particle.container.retina.pixelRatio, currentPos = particle.getPosition();
7
+ const { context, radius, particle } = data, diameter = radius * 2, pxRatio = particle.container.retina.pixelRatio, currentPos = particle.getPosition();
8
8
  if (!particle.trail || !particle.trailLength) {
9
9
  return;
10
10
  }
@@ -19,32 +19,48 @@ class TrailDrawer {
19
19
  if (particle.trail.length < 2) {
20
20
  return;
21
21
  }
22
- if (particle.trail.length > pathLength) {
22
+ while (particle.trail.length > pathLength) {
23
23
  particle.trail.shift();
24
24
  }
25
25
  const trailLength = Math.min(particle.trail.length, pathLength), offsetPos = {
26
26
  x: currentPos.x,
27
27
  y: currentPos.y,
28
+ }, canvasSize = {
29
+ width: particle.container.canvas.size.width + diameter,
30
+ height: particle.container.canvas.size.height + diameter,
28
31
  };
29
32
  let lastPos = particle.trail[trailLength - 1].position;
33
+ context.setTransform(1, 0, 0, 1, currentPos.x, currentPos.y);
30
34
  for (let i = trailLength; i > 0; i--) {
31
35
  const step = particle.trail[i - 1], position = step.position;
32
- if ((0, engine_1.getDistance)(lastPos, position) > radius * 2) {
33
- continue;
34
- }
35
36
  context.beginPath();
36
37
  context.moveTo(lastPos.x - offsetPos.x, lastPos.y - offsetPos.y);
37
- context.lineTo(position.x - offsetPos.x, position.y - offsetPos.y);
38
- const width = Math.max((i / trailLength) * radius * 2, pxRatio, particle.trailMinWidth ?? -1);
38
+ const warp = {
39
+ x: (lastPos.x + canvasSize.width) % canvasSize.width,
40
+ y: (lastPos.y + canvasSize.height) % canvasSize.height,
41
+ };
42
+ if (Math.abs(lastPos.x - position.x) > canvasSize.width / 2 ||
43
+ Math.abs(lastPos.y - position.y) > canvasSize.height / 2) {
44
+ lastPos = position;
45
+ continue;
46
+ }
47
+ context.lineTo((Math.abs(lastPos.x - position.x) > canvasSize.width / 2 ? warp.x : position.x) - offsetPos.x, (Math.abs(lastPos.y - position.y) > canvasSize.height / 2 ? warp.y : position.y) - offsetPos.y);
48
+ const width = Math.max((i / trailLength) * diameter, pxRatio, particle.trailMinWidth ?? -1);
49
+ const oldAlpha = context.globalAlpha;
50
+ context.globalAlpha = particle.trailFade ? i / trailLength : 1;
39
51
  context.lineWidth = particle.trailMaxWidth ? Math.min(width, particle.trailMaxWidth) : width;
40
52
  context.strokeStyle = step.color;
41
53
  context.stroke();
54
+ context.globalAlpha = oldAlpha;
42
55
  lastPos = position;
43
56
  }
57
+ const { transformData } = data;
58
+ context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, currentPos.x, currentPos.y);
44
59
  }
45
60
  particleInit(container, particle) {
46
61
  particle.trail = [];
47
62
  const effectData = particle.effectData;
63
+ particle.trailFade = effectData?.fade ?? true;
48
64
  particle.trailLength = (0, engine_1.getRangeValue)(effectData?.length ?? 10) * container.retina.pixelRatio;
49
65
  particle.trailMaxWidth = effectData?.maxWidth
50
66
  ? (0, engine_1.getRangeValue)(effectData.maxWidth) * container.retina.pixelRatio
@@ -1,7 +1,7 @@
1
- import { getDistance, getRangeValue, } from "@tsparticles/engine";
1
+ import { getRangeValue, } from "@tsparticles/engine";
2
2
  export class TrailDrawer {
3
3
  draw(data) {
4
- const { context, radius, particle } = data, pxRatio = particle.container.retina.pixelRatio, currentPos = particle.getPosition();
4
+ const { context, radius, particle } = data, diameter = radius * 2, pxRatio = particle.container.retina.pixelRatio, currentPos = particle.getPosition();
5
5
  if (!particle.trail || !particle.trailLength) {
6
6
  return;
7
7
  }
@@ -16,32 +16,48 @@ export class TrailDrawer {
16
16
  if (particle.trail.length < 2) {
17
17
  return;
18
18
  }
19
- if (particle.trail.length > pathLength) {
19
+ while (particle.trail.length > pathLength) {
20
20
  particle.trail.shift();
21
21
  }
22
22
  const trailLength = Math.min(particle.trail.length, pathLength), offsetPos = {
23
23
  x: currentPos.x,
24
24
  y: currentPos.y,
25
+ }, canvasSize = {
26
+ width: particle.container.canvas.size.width + diameter,
27
+ height: particle.container.canvas.size.height + diameter,
25
28
  };
26
29
  let lastPos = particle.trail[trailLength - 1].position;
30
+ context.setTransform(1, 0, 0, 1, currentPos.x, currentPos.y);
27
31
  for (let i = trailLength; i > 0; i--) {
28
32
  const step = particle.trail[i - 1], position = step.position;
29
- if (getDistance(lastPos, position) > radius * 2) {
30
- continue;
31
- }
32
33
  context.beginPath();
33
34
  context.moveTo(lastPos.x - offsetPos.x, lastPos.y - offsetPos.y);
34
- context.lineTo(position.x - offsetPos.x, position.y - offsetPos.y);
35
- const width = Math.max((i / trailLength) * radius * 2, pxRatio, particle.trailMinWidth ?? -1);
35
+ const warp = {
36
+ x: (lastPos.x + canvasSize.width) % canvasSize.width,
37
+ y: (lastPos.y + canvasSize.height) % canvasSize.height,
38
+ };
39
+ if (Math.abs(lastPos.x - position.x) > canvasSize.width / 2 ||
40
+ Math.abs(lastPos.y - position.y) > canvasSize.height / 2) {
41
+ lastPos = position;
42
+ continue;
43
+ }
44
+ context.lineTo((Math.abs(lastPos.x - position.x) > canvasSize.width / 2 ? warp.x : position.x) - offsetPos.x, (Math.abs(lastPos.y - position.y) > canvasSize.height / 2 ? warp.y : position.y) - offsetPos.y);
45
+ const width = Math.max((i / trailLength) * diameter, pxRatio, particle.trailMinWidth ?? -1);
46
+ const oldAlpha = context.globalAlpha;
47
+ context.globalAlpha = particle.trailFade ? i / trailLength : 1;
36
48
  context.lineWidth = particle.trailMaxWidth ? Math.min(width, particle.trailMaxWidth) : width;
37
49
  context.strokeStyle = step.color;
38
50
  context.stroke();
51
+ context.globalAlpha = oldAlpha;
39
52
  lastPos = position;
40
53
  }
54
+ const { transformData } = data;
55
+ context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, currentPos.x, currentPos.y);
41
56
  }
42
57
  particleInit(container, particle) {
43
58
  particle.trail = [];
44
59
  const effectData = particle.effectData;
60
+ particle.trailFade = effectData?.fade ?? true;
45
61
  particle.trailLength = getRangeValue(effectData?.length ?? 10) * container.retina.pixelRatio;
46
62
  particle.trailMaxWidth = effectData?.maxWidth
47
63
  ? getRangeValue(effectData.maxWidth) * container.retina.pixelRatio
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/effect-trail",
3
- "version": "3.0.0-beta.4",
3
+ "version": "3.0.0",
4
4
  "description": "tsParticles trail effect",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -100,7 +100,7 @@
100
100
  "./package.json": "./package.json"
101
101
  },
102
102
  "dependencies": {
103
- "@tsparticles/engine": "^3.0.0-beta.4"
103
+ "@tsparticles/engine": "^3.0.0"
104
104
  },
105
105
  "publishConfig": {
106
106
  "access": "public"