@tsparticles/path-perlin-noise 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.
@@ -4,7 +4,7 @@
4
4
  * Demo / Generator : https://particles.js.org/
5
5
  * GitHub : https://www.github.com/matteobruni/tsparticles
6
6
  * How to use? : Check the GitHub README
7
- * v3.0.0-alpha.1
7
+ * v3.0.0-beta.0
8
8
  */
9
9
  (function webpackUniversalModuleDefinition(root, factory) {
10
10
  if(typeof exports === 'object' && typeof module === 'object')
@@ -91,8 +91,8 @@ __webpack_require__.r(__webpack_exports__);
91
91
 
92
92
  // EXPORTS
93
93
  __webpack_require__.d(__webpack_exports__, {
94
- "loadPerlinNoisePath": () => (/* binding */ loadPerlinNoisePath),
95
- "perlinNoisePathName": () => (/* binding */ perlinNoisePathName)
94
+ loadPerlinNoisePath: () => (/* binding */ loadPerlinNoisePath),
95
+ perlinNoisePathName: () => (/* binding */ perlinNoisePathName)
96
96
  });
97
97
 
98
98
  // EXTERNAL MODULE: external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"}
@@ -113,8 +113,8 @@ class Grad {
113
113
  }
114
114
  ;// CONCATENATED MODULE: ./dist/browser/PerlinNoise.js
115
115
 
116
- const grad3 = [new Grad(1, 1, 0), new Grad(-1, 1, 0), new Grad(1, -1, 0), new Grad(-1, -1, 0), new Grad(1, 0, 1), new Grad(-1, 0, 1), new Grad(1, 0, -1), new Grad(-1, 0, -1), new Grad(0, 1, 1), new Grad(0, -1, 1), new Grad(0, 1, -1), new Grad(0, -1, -1)];
117
- const p = [151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180];
116
+ const grad3 = [new Grad(1, 1, 0), new Grad(-1, 1, 0), new Grad(1, -1, 0), new Grad(-1, -1, 0), new Grad(1, 0, 1), new Grad(-1, 0, 1), new Grad(1, 0, -1), new Grad(-1, 0, -1), new Grad(0, 1, 1), new Grad(0, -1, 1), new Grad(0, 1, -1), new Grad(0, -1, -1)],
117
+ p = [151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180];
118
118
  const perm = new Array(512);
119
119
  const gradP = new Array(512);
120
120
  function fade(t) {
@@ -134,17 +134,17 @@ class PerlinNoise {
134
134
  X = X & 255;
135
135
  Y = Y & 255;
136
136
  Z = Z & 255;
137
- const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z);
138
- const n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1);
139
- const n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z);
140
- const n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1);
141
- const n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z);
142
- const n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1);
143
- const n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z);
144
- const n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1);
145
- const u = fade(x);
146
- const v = fade(y);
147
- const w = fade(z);
137
+ const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z),
138
+ n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1),
139
+ n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z),
140
+ n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1),
141
+ n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z),
142
+ n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1),
143
+ n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z),
144
+ n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1),
145
+ u = fade(x),
146
+ v = fade(y),
147
+ w = fade(z);
148
148
  return lerp(lerp(lerp(n000, n100, u), lerp(n001, n101, u), w), lerp(lerp(n010, n110, u), lerp(n011, n111, u), w), v);
149
149
  }
150
150
  seed(inputSeed) {
@@ -168,6 +168,78 @@ class PerlinNoise {
168
168
 
169
169
  class PerlinNoiseGenerator {
170
170
  constructor() {
171
+ this._calculateField = () => {
172
+ const {
173
+ field,
174
+ noiseGen,
175
+ options
176
+ } = this;
177
+ for (let x = 0; x < options.columns; x++) {
178
+ const column = field[x];
179
+ for (let y = 0; y < options.rows; y++) {
180
+ const cell = column[y];
181
+ cell.length = noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
182
+ cell.angle = noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
183
+ }
184
+ }
185
+ };
186
+ this._drawField = ctx => {
187
+ const {
188
+ field,
189
+ options
190
+ } = this;
191
+ for (let x = 0; x < options.columns; x++) {
192
+ const column = field[x];
193
+ for (let y = 0; y < options.rows; y++) {
194
+ const cell = column[y],
195
+ {
196
+ angle,
197
+ length
198
+ } = cell;
199
+ ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
200
+ ctx.rotate(angle);
201
+ ctx.strokeStyle = "white";
202
+ ctx.beginPath();
203
+ ctx.moveTo(0, 0);
204
+ ctx.lineTo(0, this.options.size * length);
205
+ ctx.stroke();
206
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
207
+ }
208
+ }
209
+ };
210
+ this._initField = () => {
211
+ const {
212
+ columns,
213
+ rows
214
+ } = this.options;
215
+ this.field = new Array(columns);
216
+ for (let x = 0; x < columns; x++) {
217
+ this.field[x] = new Array(rows);
218
+ for (let y = 0; y < rows; y++) {
219
+ this.field[x][y] = engine_root_window_.Vector.origin;
220
+ }
221
+ }
222
+ };
223
+ this._resetField = container => {
224
+ const sourceOptions = container.actualOptions.particles.move.path.options,
225
+ {
226
+ options
227
+ } = this;
228
+ options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
229
+ options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
230
+ options.draw = !!sourceOptions.draw;
231
+ options.width = container.canvas.size.width;
232
+ options.height = container.canvas.size.height;
233
+ this.noiseGen.seed(sourceOptions.seed ?? (0,engine_root_window_.getRandom)());
234
+ options.columns = Math.floor(this.options.width / this.options.size) + 1;
235
+ options.rows = Math.floor(this.options.height / this.options.size) + 1;
236
+ this._initField();
237
+ };
238
+ this._setup = container => {
239
+ this.noiseZ = 0;
240
+ this._resetField(container);
241
+ window.addEventListener("resize", () => this._resetField(container));
242
+ };
171
243
  this.noiseGen = new PerlinNoise();
172
244
  this.field = [];
173
245
  this.noiseZ = 0;
@@ -183,92 +255,39 @@ class PerlinNoiseGenerator {
183
255
  }
184
256
  generate(particle) {
185
257
  const pos = particle.getPosition(),
258
+ {
259
+ size
260
+ } = this.options,
186
261
  point = {
187
- x: Math.max(Math.floor(pos.x / this.options.size), 0),
188
- y: Math.max(Math.floor(pos.y / this.options.size), 0)
262
+ x: Math.max(Math.floor(pos.x / size), 0),
263
+ y: Math.max(Math.floor(pos.y / size), 0)
189
264
  },
190
- v = engine_root_window_.Vector.origin;
191
- if (!this.field || !this.field[point.x] || !this.field[point.x][point.y]) {
192
- return v;
193
- }
194
- v.length = this.field[point.x][point.y][1];
195
- v.angle = this.field[point.x][point.y][0];
196
- return v;
265
+ {
266
+ field
267
+ } = this;
268
+ return !field || !field[point.x] || !field[point.x][point.y] ? engine_root_window_.Vector.origin : field[point.x][point.y].copy();
197
269
  }
198
270
  init(container) {
199
271
  this.container = container;
200
- this.setup(container);
272
+ this._setup(container);
201
273
  }
202
274
  reset() {}
203
275
  update() {
204
276
  if (!this.container) {
205
277
  return;
206
278
  }
207
- this.calculateField();
279
+ this._calculateField();
208
280
  this.noiseZ += this.options.increment;
209
281
  if (this.options.draw) {
210
- this.container.canvas.draw(ctx => this.drawField(ctx));
282
+ this.container.canvas.draw(ctx => this._drawField(ctx));
211
283
  }
212
284
  }
213
- calculateField() {
214
- for (let x = 0; x < this.options.columns; x++) {
215
- for (let y = 0; y < this.options.rows; y++) {
216
- const angle = this.noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
217
- const length = this.noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
218
- this.field[x][y][0] = angle;
219
- this.field[x][y][1] = length;
220
- }
221
- }
222
- }
223
- drawField(ctx) {
224
- for (let x = 0; x < this.options.columns; x++) {
225
- for (let y = 0; y < this.options.rows; y++) {
226
- const angle = this.field[x][y][0];
227
- const length = this.field[x][y][1];
228
- ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
229
- ctx.rotate(angle);
230
- ctx.strokeStyle = "white";
231
- ctx.beginPath();
232
- ctx.moveTo(0, 0);
233
- ctx.lineTo(0, this.options.size * length);
234
- ctx.stroke();
235
- ctx.setTransform(1, 0, 0, 1, 0, 0);
236
- }
237
- }
238
- }
239
- initField() {
240
- this.field = new Array(this.options.columns);
241
- for (let x = 0; x < this.options.columns; x++) {
242
- this.field[x] = new Array(this.options.rows);
243
- for (let y = 0; y < this.options.rows; y++) {
244
- this.field[x][y] = [0, 0];
245
- }
246
- }
247
- }
248
- resetField(container) {
249
- var _a;
250
- const sourceOptions = container.actualOptions.particles.move.path.options;
251
- this.options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
252
- this.options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
253
- this.options.draw = !!sourceOptions.draw;
254
- this.options.width = container.canvas.size.width;
255
- this.options.height = container.canvas.size.height;
256
- this.noiseGen.seed((_a = sourceOptions.seed) !== null && _a !== void 0 ? _a : (0,engine_root_window_.getRandom)());
257
- this.options.columns = Math.floor(this.options.width / this.options.size) + 1;
258
- this.options.rows = Math.floor(this.options.height / this.options.size) + 1;
259
- this.initField();
260
- }
261
- setup(container) {
262
- this.noiseZ = 0;
263
- this.resetField(container);
264
- window.addEventListener("resize", () => this.resetField(container));
265
- }
266
285
  }
267
286
  ;// CONCATENATED MODULE: ./dist/browser/index.js
268
287
 
269
288
  const perlinNoisePathName = "perlinNoise";
270
- function loadPerlinNoisePath(engine) {
271
- engine.addPathGenerator(perlinNoisePathName, new PerlinNoiseGenerator());
289
+ async function loadPerlinNoisePath(engine, refresh = true) {
290
+ await engine.addPathGenerator(perlinNoisePathName, new PerlinNoiseGenerator(), refresh);
272
291
  }
273
292
  })();
274
293
 
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see tsparticles.path.perlin.noise.min.js.LICENSE.txt */
2
- !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],e);else{var i="object"==typeof exports?e(require("@tsparticles/engine")):e(t.window);for(var o in i)("object"==typeof exports?exports:t)[o]=i[o]}}(this,(t=>(()=>{"use strict";var e={533:e=>{e.exports=t}},i={};function o(t){var s=i[t];if(void 0!==s)return s.exports;var n=i[t]={exports:{}};return e[t](n,n.exports,o),n.exports}o.d=(t,e)=>{for(var i in e)o.o(e,i)&&!o.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var s={};return(()=>{o.r(s),o.d(s,{loadPerlinNoisePath:()=>f,perlinNoisePathName:()=>c});var t=o(533);class e{constructor(t,e,i){this.x=t,this.y=e,this.z=i}dot2(t,e){return this.x*t+this.y*e}dot3(t,e,i){return this.dot2(t,e)+this.z*i}}const i=[new e(1,1,0),new e(-1,1,0),new e(1,-1,0),new e(-1,-1,0),new e(1,0,1),new e(-1,0,1),new e(1,0,-1),new e(-1,0,-1),new e(0,1,1),new e(0,-1,1),new e(0,1,-1),new e(0,-1,-1)],n=[151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180],r=new Array(512),h=new Array(512);function l(t){return t*t*t*(t*(6*t-15)+10)}function a(t,e,i){return(1-i)*t+i*e}class d{noise(t,e,i){let o=Math.floor(t),s=Math.floor(e),n=Math.floor(i);t-=o,e-=s,i-=n,o&=255,s&=255,n&=255;const d=h[o+r[s+r[n]]].dot3(t,e,i),p=h[o+r[s+r[n+1]]].dot3(t,e,i-1),c=h[o+r[s+1+r[n]]].dot3(t,e-1,i),f=h[o+r[s+1+r[n+1]]].dot3(t,e-1,i-1),u=h[o+1+r[s+r[n]]].dot3(t-1,e,i),w=h[o+1+r[s+r[n+1]]].dot3(t-1,e,i-1),m=h[o+1+r[s+1+r[n]]].dot3(t-1,e-1,i),y=h[o+1+r[s+1+r[n+1]]].dot3(t-1,e-1,i-1),g=l(t),v=l(e),z=l(i);return a(a(a(d,u,g),a(p,w,g),z),a(a(c,m,g),a(f,y,g),z),v)}seed(t){let e=t;e>0&&e<1&&(e*=65536),e=Math.floor(e),e<256&&(e|=e<<8);for(let t=0;t<256;t++){const o=1&t?n[t]^255&e:n[t]^e>>8&255;r[t]=r[t+256]=o,h[t]=h[t+256]=i[o%12]}}}class p{constructor(){this.noiseGen=new d,this.field=[],this.noiseZ=0,this.options={draw:!1,size:20,increment:.004,columns:0,rows:0,width:0,height:0}}generate(e){const i=e.getPosition(),o=Math.max(Math.floor(i.x/this.options.size),0),s=Math.max(Math.floor(i.y/this.options.size),0),n=t.Vector.origin;return this.field&&this.field[o]&&this.field[o][s]?(n.length=this.field[o][s][1],n.angle=this.field[o][s][0],n):n}init(t){this.container=t,this.setup(t)}reset(){}update(){this.container&&(this.calculateField(),this.noiseZ+=this.options.increment,this.options.draw&&this.container.canvas.draw((t=>this.drawField(t))))}calculateField(){for(let t=0;t<this.options.columns;t++)for(let e=0;e<this.options.rows;e++){const i=this.noiseGen.noise(t/50,e/50,this.noiseZ)*Math.PI*2,o=this.noiseGen.noise(t/100+4e4,e/100+4e4,this.noiseZ);this.field[t][e][0]=i,this.field[t][e][1]=o}}drawField(t){for(let e=0;e<this.options.columns;e++)for(let i=0;i<this.options.rows;i++){const o=this.field[e][i][0],s=this.field[e][i][1];t.setTransform(1,0,0,1,e*this.options.size,i*this.options.size),t.rotate(o),t.strokeStyle="white",t.beginPath(),t.moveTo(0,0),t.lineTo(0,this.options.size*s),t.stroke(),t.setTransform(1,0,0,1,0,0)}}initField(){this.field=new Array(this.options.columns);for(let t=0;t<this.options.columns;t++){this.field[t]=new Array(this.options.rows);for(let e=0;e<this.options.rows;e++)this.field[t][e]=[0,0]}}resetField(e){var i;const o=e.actualOptions.particles.move.path.options;this.options.size=o.size>0?o.size:20,this.options.increment=o.increment>0?o.increment:.004,this.options.draw=!!o.draw,this.options.width=e.canvas.size.width,this.options.height=e.canvas.size.height,this.noiseGen.seed(null!==(i=o.seed)&&void 0!==i?i:(0,t.getRandom)()),this.options.columns=Math.floor(this.options.width/this.options.size)+1,this.options.rows=Math.floor(this.options.height/this.options.size)+1,this.initField()}setup(t){this.noiseZ=0,this.resetField(t),window.addEventListener("resize",(()=>this.resetField(t)))}}const c="perlinNoise";function f(t){t.addPathGenerator(c,new p)}})(),s})()));
2
+ !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],e);else{var o="object"==typeof exports?e(require("@tsparticles/engine")):e(t.window);for(var i in o)("object"==typeof exports?exports:t)[i]=o[i]}}(this,(t=>(()=>{"use strict";var e={533:e=>{e.exports=t}},o={};function i(t){var s=o[t];if(void 0!==s)return s.exports;var n=o[t]={exports:{}};return e[t](n,n.exports,i),n.exports}i.d=(t,e)=>{for(var o in e)i.o(e,o)&&!i.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),i.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var s={};return(()=>{i.r(s),i.d(s,{loadPerlinNoisePath:()=>f,perlinNoisePathName:()=>p});var t=i(533);class e{constructor(t,e,o){this.x=t,this.y=e,this.z=o}dot2(t,e){return this.x*t+this.y*e}dot3(t,e,o){return this.dot2(t,e)+this.z*o}}const o=[new e(1,1,0),new e(-1,1,0),new e(1,-1,0),new e(-1,-1,0),new e(1,0,1),new e(-1,0,1),new e(1,0,-1),new e(-1,0,-1),new e(0,1,1),new e(0,-1,1),new e(0,1,-1),new e(0,-1,-1)],n=[151,160,137,91,90,15,131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,190,6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,88,237,149,56,87,174,20,125,136,171,168,68,175,74,165,71,134,139,48,27,166,77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,102,143,54,65,25,63,161,1,216,80,73,209,76,132,187,208,89,18,169,200,196,135,130,116,188,159,86,164,100,109,198,173,186,3,64,52,217,226,250,124,123,5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,223,183,170,213,119,248,152,2,44,154,163,70,221,153,101,155,167,43,172,9,129,22,39,253,19,98,108,110,79,113,224,232,178,185,112,104,218,246,97,228,251,34,242,193,238,210,144,12,191,179,162,241,81,51,145,235,249,14,239,107,49,192,214,31,181,199,106,157,184,84,204,176,115,121,50,45,127,4,150,254,138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180],r=new Array(512),a=new Array(512);function h(t){return t*t*t*(t*(6*t-15)+10)}function l(t,e,o){return(1-o)*t+o*e}class d{noise(t,e,o){let i=Math.floor(t),s=Math.floor(e),n=Math.floor(o);t-=i,e-=s,o-=n,i&=255,s&=255,n&=255;const d=a[i+r[s+r[n]]].dot3(t,e,o),c=a[i+r[s+r[n+1]]].dot3(t,e,o-1),p=a[i+r[s+1+r[n]]].dot3(t,e-1,o),f=a[i+r[s+1+r[n+1]]].dot3(t,e-1,o-1),w=a[i+1+r[s+r[n]]].dot3(t-1,e,o),u=a[i+1+r[s+r[n+1]]].dot3(t-1,e,o-1),m=a[i+1+r[s+1+r[n]]].dot3(t-1,e-1,o),y=a[i+1+r[s+1+r[n+1]]].dot3(t-1,e-1,o-1),g=h(t),v=h(e),x=h(o);return l(l(l(d,w,g),l(c,u,g),x),l(l(p,m,g),l(f,y,g),x),v)}seed(t){let e=t;e>0&&e<1&&(e*=65536),e=Math.floor(e),e<256&&(e|=e<<8);for(let t=0;t<256;t++){const i=1&t?n[t]^255&e:n[t]^e>>8&255;r[t]=r[t+256]=i,a[t]=a[t+256]=o[i%12]}}}class c{constructor(){this._calculateField=()=>{const{field:t,noiseGen:e,options:o}=this;for(let i=0;i<o.columns;i++){const s=t[i];for(let t=0;t<o.rows;t++){const o=s[t];o.length=e.noise(i/100+4e4,t/100+4e4,this.noiseZ),o.angle=e.noise(i/50,t/50,this.noiseZ)*Math.PI*2}}},this._drawField=t=>{const{field:e,options:o}=this;for(let i=0;i<o.columns;i++){const s=e[i];for(let e=0;e<o.rows;e++){const o=s[e],{angle:n,length:r}=o;t.setTransform(1,0,0,1,i*this.options.size,e*this.options.size),t.rotate(n),t.strokeStyle="white",t.beginPath(),t.moveTo(0,0),t.lineTo(0,this.options.size*r),t.stroke(),t.setTransform(1,0,0,1,0,0)}}},this._initField=()=>{const{columns:e,rows:o}=this.options;this.field=new Array(e);for(let i=0;i<e;i++){this.field[i]=new Array(o);for(let e=0;e<o;e++)this.field[i][e]=t.Vector.origin}},this._resetField=e=>{const o=e.actualOptions.particles.move.path.options,{options:i}=this;i.size=o.size>0?o.size:20,i.increment=o.increment>0?o.increment:.004,i.draw=!!o.draw,i.width=e.canvas.size.width,i.height=e.canvas.size.height,this.noiseGen.seed(o.seed??(0,t.getRandom)()),i.columns=Math.floor(this.options.width/this.options.size)+1,i.rows=Math.floor(this.options.height/this.options.size)+1,this._initField()},this._setup=t=>{this.noiseZ=0,this._resetField(t),window.addEventListener("resize",(()=>this._resetField(t)))},this.noiseGen=new d,this.field=[],this.noiseZ=0,this.options={draw:!1,size:20,increment:.004,columns:0,rows:0,width:0,height:0}}generate(e){const o=e.getPosition(),{size:i}=this.options,s=Math.max(Math.floor(o.x/i),0),n=Math.max(Math.floor(o.y/i),0),{field:r}=this;return r&&r[s]&&r[s][n]?r[s][n].copy():t.Vector.origin}init(t){this.container=t,this._setup(t)}reset(){}update(){this.container&&(this._calculateField(),this.noiseZ+=this.options.increment,this.options.draw&&this.container.canvas.draw((t=>this._drawField(t))))}}const p="perlinNoise";async function f(t,e=!0){await t.addPathGenerator(p,new c,e)}})(),s})()));
@@ -1,8 +1 @@
1
- /*!
2
- * Author : Matteo Bruni
3
- * MIT license: https://opensource.org/licenses/MIT
4
- * Demo / Generator : https://particles.js.org/
5
- * GitHub : https://www.github.com/matteobruni/tsparticles
6
- * How to use? : Check the GitHub README
7
- * v3.0.0-alpha.1
8
- */
1
+ /*! tsParticles Perlin Noise Path v3.0.0-beta.0 by Matteo Bruni */
@@ -1,10 +1,9 @@
1
- import type { Container, IMovePathGenerator, Particle } from "@tsparticles/engine";
2
- import { Vector } from "@tsparticles/engine";
1
+ import { type Container, type IMovePathGenerator, type Particle, Vector } from "@tsparticles/engine";
3
2
  import type { IPerlinOptions } from "./IPerlinOptions";
4
3
  import { PerlinNoise } from "./PerlinNoise";
5
4
  export declare class PerlinNoiseGenerator implements IMovePathGenerator {
6
5
  container?: Container;
7
- field: number[][][];
6
+ field: Vector[][];
8
7
  readonly noiseGen: PerlinNoise;
9
8
  noiseZ: number;
10
9
  readonly options: IPerlinOptions;
@@ -13,9 +12,9 @@ export declare class PerlinNoiseGenerator implements IMovePathGenerator {
13
12
  init(container: Container): void;
14
13
  reset(): void;
15
14
  update(): void;
16
- private calculateField;
17
- private drawField;
18
- private initField;
19
- private resetField;
20
- private setup;
15
+ private readonly _calculateField;
16
+ private readonly _drawField;
17
+ private readonly _initField;
18
+ private readonly _resetField;
19
+ private readonly _setup;
21
20
  }
package/types/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  import type { Engine } from "@tsparticles/engine";
2
2
  export declare const perlinNoisePathName = "perlinNoise";
3
- export declare function loadPerlinNoisePath(engine: Engine): void;
3
+ export declare function loadPerlinNoisePath(engine: Engine, refresh?: boolean): Promise<void>;
@@ -24,19 +24,18 @@
24
24
  new Grad_1.Grad(0, -1, 1),
25
25
  new Grad_1.Grad(0, 1, -1),
26
26
  new Grad_1.Grad(0, -1, -1),
27
- ];
28
- const p = [
29
- 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240,
30
- 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88,
31
- 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83,
32
- 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80,
33
- 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64,
34
- 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182,
35
- 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22,
36
- 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210,
37
- 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84,
38
- 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78,
39
- 66, 215, 61, 156, 180,
27
+ ], p = [
28
+ 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37,
29
+ 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177,
30
+ 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146,
31
+ 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25,
32
+ 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100,
33
+ 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206,
34
+ 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153,
35
+ 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246,
36
+ 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192,
37
+ 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114,
38
+ 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180,
40
39
  ];
41
40
  const perm = new Array(512);
42
41
  const gradP = new Array(512);
@@ -55,17 +54,7 @@
55
54
  X = X & 255;
56
55
  Y = Y & 255;
57
56
  Z = Z & 255;
58
- const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z);
59
- const n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1);
60
- const n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z);
61
- const n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1);
62
- const n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z);
63
- const n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1);
64
- const n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z);
65
- const n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1);
66
- const u = fade(x);
67
- const v = fade(y);
68
- const w = fade(z);
57
+ const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z), n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1), n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z), n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1), n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z), n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1), n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z), n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1), u = fade(x), v = fade(y), w = fade(z);
69
58
  return lerp(lerp(lerp(n000, n100, u), lerp(n001, n101, u), w), lerp(lerp(n010, n110, u), lerp(n011, n111, u), w), v);
70
59
  }
71
60
  seed(inputSeed) {
@@ -14,6 +14,61 @@
14
14
  const PerlinNoise_1 = require("./PerlinNoise");
15
15
  class PerlinNoiseGenerator {
16
16
  constructor() {
17
+ this._calculateField = () => {
18
+ const { field, noiseGen, options } = this;
19
+ for (let x = 0; x < options.columns; x++) {
20
+ const column = field[x];
21
+ for (let y = 0; y < options.rows; y++) {
22
+ const cell = column[y];
23
+ cell.length = noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
24
+ cell.angle = noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
25
+ }
26
+ }
27
+ };
28
+ this._drawField = (ctx) => {
29
+ const { field, options } = this;
30
+ for (let x = 0; x < options.columns; x++) {
31
+ const column = field[x];
32
+ for (let y = 0; y < options.rows; y++) {
33
+ const cell = column[y], { angle, length } = cell;
34
+ ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
35
+ ctx.rotate(angle);
36
+ ctx.strokeStyle = "white";
37
+ ctx.beginPath();
38
+ ctx.moveTo(0, 0);
39
+ ctx.lineTo(0, this.options.size * length);
40
+ ctx.stroke();
41
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
42
+ }
43
+ }
44
+ };
45
+ this._initField = () => {
46
+ const { columns, rows } = this.options;
47
+ this.field = new Array(columns);
48
+ for (let x = 0; x < columns; x++) {
49
+ this.field[x] = new Array(rows);
50
+ for (let y = 0; y < rows; y++) {
51
+ this.field[x][y] = engine_1.Vector.origin;
52
+ }
53
+ }
54
+ };
55
+ this._resetField = (container) => {
56
+ const sourceOptions = container.actualOptions.particles.move.path.options, { options } = this;
57
+ options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
58
+ options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
59
+ options.draw = !!sourceOptions.draw;
60
+ options.width = container.canvas.size.width;
61
+ options.height = container.canvas.size.height;
62
+ this.noiseGen.seed(sourceOptions.seed ?? (0, engine_1.getRandom)());
63
+ options.columns = Math.floor(this.options.width / this.options.size) + 1;
64
+ options.rows = Math.floor(this.options.height / this.options.size) + 1;
65
+ this._initField();
66
+ };
67
+ this._setup = (container) => {
68
+ this.noiseZ = 0;
69
+ this._resetField(container);
70
+ window.addEventListener("resize", () => this._resetField(container));
71
+ };
17
72
  this.noiseGen = new PerlinNoise_1.PerlinNoise();
18
73
  this.field = [];
19
74
  this.noiseZ = 0;
@@ -28,20 +83,15 @@
28
83
  };
29
84
  }
30
85
  generate(particle) {
31
- const pos = particle.getPosition(), point = {
32
- x: Math.max(Math.floor(pos.x / this.options.size), 0),
33
- y: Math.max(Math.floor(pos.y / this.options.size), 0),
34
- }, v = engine_1.Vector.origin;
35
- if (!this.field || !this.field[point.x] || !this.field[point.x][point.y]) {
36
- return v;
37
- }
38
- v.length = this.field[point.x][point.y][1];
39
- v.angle = this.field[point.x][point.y][0];
40
- return v;
86
+ const pos = particle.getPosition(), { size } = this.options, point = {
87
+ x: Math.max(Math.floor(pos.x / size), 0),
88
+ y: Math.max(Math.floor(pos.y / size), 0),
89
+ }, { field } = this;
90
+ return !field || !field[point.x] || !field[point.x][point.y] ? engine_1.Vector.origin : field[point.x][point.y].copy();
41
91
  }
42
92
  init(container) {
43
93
  this.container = container;
44
- this.setup(container);
94
+ this._setup(container);
45
95
  }
46
96
  reset() {
47
97
  }
@@ -49,65 +99,12 @@
49
99
  if (!this.container) {
50
100
  return;
51
101
  }
52
- this.calculateField();
102
+ this._calculateField();
53
103
  this.noiseZ += this.options.increment;
54
104
  if (this.options.draw) {
55
- this.container.canvas.draw((ctx) => this.drawField(ctx));
105
+ this.container.canvas.draw((ctx) => this._drawField(ctx));
56
106
  }
57
107
  }
58
- calculateField() {
59
- for (let x = 0; x < this.options.columns; x++) {
60
- for (let y = 0; y < this.options.rows; y++) {
61
- const angle = this.noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
62
- const length = this.noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
63
- this.field[x][y][0] = angle;
64
- this.field[x][y][1] = length;
65
- }
66
- }
67
- }
68
- drawField(ctx) {
69
- for (let x = 0; x < this.options.columns; x++) {
70
- for (let y = 0; y < this.options.rows; y++) {
71
- const angle = this.field[x][y][0];
72
- const length = this.field[x][y][1];
73
- ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
74
- ctx.rotate(angle);
75
- ctx.strokeStyle = "white";
76
- ctx.beginPath();
77
- ctx.moveTo(0, 0);
78
- ctx.lineTo(0, this.options.size * length);
79
- ctx.stroke();
80
- ctx.setTransform(1, 0, 0, 1, 0, 0);
81
- }
82
- }
83
- }
84
- initField() {
85
- this.field = new Array(this.options.columns);
86
- for (let x = 0; x < this.options.columns; x++) {
87
- this.field[x] = new Array(this.options.rows);
88
- for (let y = 0; y < this.options.rows; y++) {
89
- this.field[x][y] = [0, 0];
90
- }
91
- }
92
- }
93
- resetField(container) {
94
- var _a;
95
- const sourceOptions = container.actualOptions.particles.move.path.options;
96
- this.options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
97
- this.options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
98
- this.options.draw = !!sourceOptions.draw;
99
- this.options.width = container.canvas.size.width;
100
- this.options.height = container.canvas.size.height;
101
- this.noiseGen.seed((_a = sourceOptions.seed) !== null && _a !== void 0 ? _a : (0, engine_1.getRandom)());
102
- this.options.columns = Math.floor(this.options.width / this.options.size) + 1;
103
- this.options.rows = Math.floor(this.options.height / this.options.size) + 1;
104
- this.initField();
105
- }
106
- setup(container) {
107
- this.noiseZ = 0;
108
- this.resetField(container);
109
- window.addEventListener("resize", () => this.resetField(container));
110
- }
111
108
  }
112
109
  exports.PerlinNoiseGenerator = PerlinNoiseGenerator;
113
110
  });
package/umd/index.js CHANGED
@@ -12,8 +12,8 @@
12
12
  exports.loadPerlinNoisePath = exports.perlinNoisePathName = void 0;
13
13
  const PerlinNoiseGenerator_1 = require("./PerlinNoiseGenerator");
14
14
  exports.perlinNoisePathName = "perlinNoise";
15
- function loadPerlinNoisePath(engine) {
16
- engine.addPathGenerator(exports.perlinNoisePathName, new PerlinNoiseGenerator_1.PerlinNoiseGenerator());
15
+ async function loadPerlinNoisePath(engine, refresh = true) {
16
+ await engine.addPathGenerator(exports.perlinNoisePathName, new PerlinNoiseGenerator_1.PerlinNoiseGenerator(), refresh);
17
17
  }
18
18
  exports.loadPerlinNoisePath = loadPerlinNoisePath;
19
19
  });