@tsparticles/path-perlin-noise 3.0.0-alpha.1 → 3.0.0-beta.1

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.
@@ -1,7 +1,62 @@
1
1
  import { Vector, getRandom } from "@tsparticles/engine";
2
- import { PerlinNoise } from "./PerlinNoise";
2
+ import { PerlinNoise } from "./PerlinNoise.js";
3
3
  export class PerlinNoiseGenerator {
4
4
  constructor() {
5
+ this._calculateField = () => {
6
+ const { field, noiseGen, options } = this;
7
+ for (let x = 0; x < options.columns; x++) {
8
+ const column = field[x];
9
+ for (let y = 0; y < options.rows; y++) {
10
+ const cell = column[y];
11
+ cell.length = noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
12
+ cell.angle = noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
13
+ }
14
+ }
15
+ };
16
+ this._drawField = (ctx) => {
17
+ const { field, options } = this;
18
+ for (let x = 0; x < options.columns; x++) {
19
+ const column = field[x];
20
+ for (let y = 0; y < options.rows; y++) {
21
+ const cell = column[y], { angle, length } = cell;
22
+ ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
23
+ ctx.rotate(angle);
24
+ ctx.strokeStyle = "white";
25
+ ctx.beginPath();
26
+ ctx.moveTo(0, 0);
27
+ ctx.lineTo(0, this.options.size * length);
28
+ ctx.stroke();
29
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
30
+ }
31
+ }
32
+ };
33
+ this._initField = () => {
34
+ const { columns, rows } = this.options;
35
+ this.field = new Array(columns);
36
+ for (let x = 0; x < columns; x++) {
37
+ this.field[x] = new Array(rows);
38
+ for (let y = 0; y < rows; y++) {
39
+ this.field[x][y] = Vector.origin;
40
+ }
41
+ }
42
+ };
43
+ this._resetField = (container) => {
44
+ const sourceOptions = container.actualOptions.particles.move.path.options, { options } = this;
45
+ options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
46
+ options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
47
+ options.draw = !!sourceOptions.draw;
48
+ options.width = container.canvas.size.width;
49
+ options.height = container.canvas.size.height;
50
+ this.noiseGen.seed(sourceOptions.seed ?? getRandom());
51
+ options.columns = Math.floor(this.options.width / this.options.size) + 1;
52
+ options.rows = Math.floor(this.options.height / this.options.size) + 1;
53
+ this._initField();
54
+ };
55
+ this._setup = (container) => {
56
+ this.noiseZ = 0;
57
+ this._resetField(container);
58
+ window.addEventListener("resize", () => this._resetField(container));
59
+ };
5
60
  this.noiseGen = new PerlinNoise();
6
61
  this.field = [];
7
62
  this.noiseZ = 0;
@@ -16,20 +71,15 @@ export class PerlinNoiseGenerator {
16
71
  };
17
72
  }
18
73
  generate(particle) {
19
- const pos = particle.getPosition(), point = {
20
- x: Math.max(Math.floor(pos.x / this.options.size), 0),
21
- y: Math.max(Math.floor(pos.y / this.options.size), 0),
22
- }, v = Vector.origin;
23
- if (!this.field || !this.field[point.x] || !this.field[point.x][point.y]) {
24
- return v;
25
- }
26
- v.length = this.field[point.x][point.y][1];
27
- v.angle = this.field[point.x][point.y][0];
28
- return v;
74
+ const pos = particle.getPosition(), { size } = this.options, point = {
75
+ x: Math.max(Math.floor(pos.x / size), 0),
76
+ y: Math.max(Math.floor(pos.y / size), 0),
77
+ }, { field } = this;
78
+ return !field || !field[point.x] || !field[point.x][point.y] ? Vector.origin : field[point.x][point.y].copy();
29
79
  }
30
80
  init(container) {
31
81
  this.container = container;
32
- this.setup(container);
82
+ this._setup(container);
33
83
  }
34
84
  reset() {
35
85
  }
@@ -37,63 +87,10 @@ export class PerlinNoiseGenerator {
37
87
  if (!this.container) {
38
88
  return;
39
89
  }
40
- this.calculateField();
90
+ this._calculateField();
41
91
  this.noiseZ += this.options.increment;
42
92
  if (this.options.draw) {
43
- this.container.canvas.draw((ctx) => this.drawField(ctx));
93
+ this.container.canvas.draw((ctx) => this._drawField(ctx));
44
94
  }
45
95
  }
46
- calculateField() {
47
- for (let x = 0; x < this.options.columns; x++) {
48
- for (let y = 0; y < this.options.rows; y++) {
49
- const angle = this.noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
50
- const length = this.noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
51
- this.field[x][y][0] = angle;
52
- this.field[x][y][1] = length;
53
- }
54
- }
55
- }
56
- drawField(ctx) {
57
- for (let x = 0; x < this.options.columns; x++) {
58
- for (let y = 0; y < this.options.rows; y++) {
59
- const angle = this.field[x][y][0];
60
- const length = this.field[x][y][1];
61
- ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
62
- ctx.rotate(angle);
63
- ctx.strokeStyle = "white";
64
- ctx.beginPath();
65
- ctx.moveTo(0, 0);
66
- ctx.lineTo(0, this.options.size * length);
67
- ctx.stroke();
68
- ctx.setTransform(1, 0, 0, 1, 0, 0);
69
- }
70
- }
71
- }
72
- initField() {
73
- this.field = new Array(this.options.columns);
74
- for (let x = 0; x < this.options.columns; x++) {
75
- this.field[x] = new Array(this.options.rows);
76
- for (let y = 0; y < this.options.rows; y++) {
77
- this.field[x][y] = [0, 0];
78
- }
79
- }
80
- }
81
- resetField(container) {
82
- var _a;
83
- const sourceOptions = container.actualOptions.particles.move.path.options;
84
- this.options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
85
- this.options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
86
- this.options.draw = !!sourceOptions.draw;
87
- this.options.width = container.canvas.size.width;
88
- this.options.height = container.canvas.size.height;
89
- this.noiseGen.seed((_a = sourceOptions.seed) !== null && _a !== void 0 ? _a : getRandom());
90
- this.options.columns = Math.floor(this.options.width / this.options.size) + 1;
91
- this.options.rows = Math.floor(this.options.height / this.options.size) + 1;
92
- this.initField();
93
- }
94
- setup(container) {
95
- this.noiseZ = 0;
96
- this.resetField(container);
97
- window.addEventListener("resize", () => this.resetField(container));
98
- }
99
96
  }
package/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import { PerlinNoiseGenerator } from "./PerlinNoiseGenerator";
1
+ import { PerlinNoiseGenerator } from "./PerlinNoiseGenerator.js";
2
2
  export const perlinNoisePathName = "perlinNoise";
3
- export function loadPerlinNoisePath(engine) {
4
- engine.addPathGenerator(perlinNoisePathName, new PerlinNoiseGenerator());
3
+ export async function loadPerlinNoisePath(engine, refresh = true) {
4
+ await engine.addPathGenerator(perlinNoisePathName, new PerlinNoiseGenerator(), refresh);
5
5
  }
@@ -0,0 +1 @@
1
+ { "type": "module" }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/path-perlin-noise",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-beta.1",
4
4
  "description": "tsParticles perlin noise path",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -63,6 +63,9 @@
63
63
  "@tsparticles/plugin",
64
64
  "@tsparticles/path"
65
65
  ],
66
+ "publishConfig": {
67
+ "access": "public"
68
+ },
66
69
  "author": "Matteo Bruni <matteo.bruni@me.com>",
67
70
  "license": "MIT",
68
71
  "bugs": {
@@ -73,20 +76,34 @@
73
76
  "type": "github",
74
77
  "url": "https://github.com/sponsors/matteobruni"
75
78
  },
79
+ {
80
+ "type": "github",
81
+ "url": "https://github.com/sponsors/tsparticles"
82
+ },
76
83
  {
77
84
  "type": "buymeacoffee",
78
85
  "url": "https://www.buymeacoffee.com/matteobruni"
79
86
  }
80
87
  ],
81
- "main": "cjs/index.js",
88
+ "sideEffects": false,
82
89
  "jsdelivr": "tsparticles.path.perlin.noise.min.js",
83
90
  "unpkg": "tsparticles.path.perlin.noise.min.js",
91
+ "browser": "browser/index.js",
92
+ "main": "cjs/index.js",
84
93
  "module": "esm/index.js",
85
94
  "types": "types/index.d.ts",
86
- "publishConfig": {
87
- "access": "public"
95
+ "exports": {
96
+ ".": {
97
+ "types": "./types/index.d.ts",
98
+ "browser": "./browser/index.js",
99
+ "import": "./esm/index.js",
100
+ "require": "./cjs/index.js",
101
+ "umd": "./umd/index.js",
102
+ "default": "./cjs/index.js"
103
+ },
104
+ "./package.json": "./package.json"
88
105
  },
89
106
  "dependencies": {
90
- "@tsparticles/engine": "^3.0.0-alpha.1"
107
+ "@tsparticles/engine": "^3.0.0-beta.1"
91
108
  }
92
- }
109
+ }