@tsparticles/path-perlin-noise 3.0.0-alpha.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Matteo Bruni
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ [![banner](https://particles.js.org/images/banner3.png)](https://particles.js.org)
2
+
3
+ # tsParticles Perlin Noise Path
4
+
5
+ [![jsDelivr](https://data.jsdelivr.com/v1/package/npm/tsparticles-path-perlin-noise/badge)](https://www.jsdelivr.com/package/npm/tsparticles-path-perlin-noise)
6
+ [![npmjs](https://badge.fury.io/js/tsparticles-path-perlin-noise.svg)](https://www.npmjs.com/package/tsparticles-path-perlin-noise)
7
+ [![npmjs](https://img.shields.io/npm/dt/tsparticles-path-perlin-noise)](https://www.npmjs.com/package/tsparticles-path-perlin-noise) [![GitHub Sponsors](https://img.shields.io/github/sponsors/matteobruni)](https://github.com/sponsors/matteobruni)
8
+
9
+ [tsParticles](https://github.com/matteobruni/tsparticles) path plugin for perlin noise movement.
10
+
11
+ ## How to use it
12
+
13
+ ### CDN / Vanilla JS / jQuery
14
+
15
+ The CDN/Vanilla version JS has one required file in vanilla configuration:
16
+
17
+ Including the `tsparticles.path.perlin.noise.min.js` file will export the function to load the path plugin:
18
+
19
+ ```text
20
+ loadPerlinNoisePath
21
+ ```
22
+
23
+ ### Usage
24
+
25
+ Once the scripts are loaded you can set up `tsParticles` and the path plugin like this:
26
+
27
+ ```javascript
28
+ (async () => {
29
+ await loadPerlinNoisePath(tsParticles);
30
+
31
+ await tsParticles.load({
32
+ id: "tsparticles",
33
+ options: {
34
+ /* options */
35
+ },
36
+ });
37
+ })();
38
+ ```
39
+
40
+ ### ESM / CommonJS
41
+
42
+ This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
43
+
44
+ ```shell
45
+ $ npm install tsparticles-path-perlin-noise
46
+ ```
47
+
48
+ or
49
+
50
+ ```shell
51
+ $ yarn add tsparticles-path-perlin-noise
52
+ ```
53
+
54
+ Then you need to import it in the app, like this:
55
+
56
+ ```javascript
57
+ const { tsParticles } = require("tsparticles-engine");
58
+ const { loadPerlinNoisePath } = require("tsparticles-path-perlin-noise");
59
+
60
+ loadPerlinNoisePath(tsParticles);
61
+ ```
62
+
63
+ or
64
+
65
+ ```javascript
66
+ import { tsParticles } from "tsparticles-engine";
67
+ import { loadPerlinNoisePath } from "tsparticles-path-perlin-noise";
68
+
69
+ loadPerlinNoisePath(tsParticles);
70
+ ```
@@ -0,0 +1,13 @@
1
+ export class Grad {
2
+ constructor(x, y, z) {
3
+ this.x = x;
4
+ this.y = y;
5
+ this.z = z;
6
+ }
7
+ dot2(x, y) {
8
+ return this.x * x + this.y * y;
9
+ }
10
+ dot3(x, y, z) {
11
+ return this.dot2(x, y) + this.z * z;
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
1
+ import { Grad } from "./Grad";
2
+ const grad3 = [
3
+ new Grad(1, 1, 0),
4
+ new Grad(-1, 1, 0),
5
+ new Grad(1, -1, 0),
6
+ new Grad(-1, -1, 0),
7
+ new Grad(1, 0, 1),
8
+ new Grad(-1, 0, 1),
9
+ new Grad(1, 0, -1),
10
+ new Grad(-1, 0, -1),
11
+ new Grad(0, 1, 1),
12
+ new Grad(0, -1, 1),
13
+ new Grad(0, 1, -1),
14
+ new Grad(0, -1, -1),
15
+ ];
16
+ const p = [
17
+ 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,
18
+ 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,
19
+ 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,
20
+ 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,
21
+ 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,
22
+ 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,
23
+ 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,
24
+ 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210,
25
+ 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84,
26
+ 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,
27
+ 66, 215, 61, 156, 180,
28
+ ];
29
+ const perm = new Array(512);
30
+ const gradP = new Array(512);
31
+ function fade(t) {
32
+ return t * t * t * (t * (t * 6 - 15) + 10);
33
+ }
34
+ function lerp(a, b, t) {
35
+ return (1 - t) * a + t * b;
36
+ }
37
+ export class PerlinNoise {
38
+ noise(x, y, z) {
39
+ let X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z);
40
+ x = x - X;
41
+ y = y - Y;
42
+ z = z - Z;
43
+ X = X & 255;
44
+ Y = Y & 255;
45
+ Z = Z & 255;
46
+ const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z);
47
+ const n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1);
48
+ const n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z);
49
+ const n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1);
50
+ const n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z);
51
+ const n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1);
52
+ const n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z);
53
+ const n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1);
54
+ const u = fade(x);
55
+ const v = fade(y);
56
+ const w = fade(z);
57
+ return lerp(lerp(lerp(n000, n100, u), lerp(n001, n101, u), w), lerp(lerp(n010, n110, u), lerp(n011, n111, u), w), v);
58
+ }
59
+ seed(inputSeed) {
60
+ let seed = inputSeed;
61
+ if (seed > 0 && seed < 1) {
62
+ seed *= 65536;
63
+ }
64
+ seed = Math.floor(seed);
65
+ if (seed < 256) {
66
+ seed |= seed << 8;
67
+ }
68
+ for (let i = 0; i < 256; i++) {
69
+ const v = i & 1 ? p[i] ^ (seed & 255) : p[i] ^ ((seed >> 8) & 255);
70
+ perm[i] = perm[i + 256] = v;
71
+ gradP[i] = gradP[i + 256] = grad3[v % 12];
72
+ }
73
+ }
74
+ }
@@ -0,0 +1,99 @@
1
+ import { Vector, getRandom } from "@tsparticles/engine";
2
+ import { PerlinNoise } from "./PerlinNoise";
3
+ export class PerlinNoiseGenerator {
4
+ constructor() {
5
+ this.noiseGen = new PerlinNoise();
6
+ this.field = [];
7
+ this.noiseZ = 0;
8
+ this.options = {
9
+ draw: false,
10
+ size: 20,
11
+ increment: 0.004,
12
+ columns: 0,
13
+ rows: 0,
14
+ width: 0,
15
+ height: 0,
16
+ };
17
+ }
18
+ 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;
29
+ }
30
+ init(container) {
31
+ this.container = container;
32
+ this.setup(container);
33
+ }
34
+ reset() {
35
+ }
36
+ update() {
37
+ if (!this.container) {
38
+ return;
39
+ }
40
+ this.calculateField();
41
+ this.noiseZ += this.options.increment;
42
+ if (this.options.draw) {
43
+ this.container.canvas.draw((ctx) => this.drawField(ctx));
44
+ }
45
+ }
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
+ }
@@ -0,0 +1,5 @@
1
+ import { PerlinNoiseGenerator } from "./PerlinNoiseGenerator";
2
+ export const perlinNoisePathName = "perlinNoise";
3
+ export function loadPerlinNoisePath(engine) {
4
+ engine.addPathGenerator(perlinNoisePathName, new PerlinNoiseGenerator());
5
+ }
package/cjs/Grad.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Grad = void 0;
4
+ class Grad {
5
+ constructor(x, y, z) {
6
+ this.x = x;
7
+ this.y = y;
8
+ this.z = z;
9
+ }
10
+ dot2(x, y) {
11
+ return this.x * x + this.y * y;
12
+ }
13
+ dot3(x, y, z) {
14
+ return this.dot2(x, y) + this.z * z;
15
+ }
16
+ }
17
+ exports.Grad = Grad;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PerlinNoise = void 0;
4
+ const Grad_1 = require("./Grad");
5
+ const grad3 = [
6
+ new Grad_1.Grad(1, 1, 0),
7
+ new Grad_1.Grad(-1, 1, 0),
8
+ new Grad_1.Grad(1, -1, 0),
9
+ new Grad_1.Grad(-1, -1, 0),
10
+ new Grad_1.Grad(1, 0, 1),
11
+ new Grad_1.Grad(-1, 0, 1),
12
+ new Grad_1.Grad(1, 0, -1),
13
+ new Grad_1.Grad(-1, 0, -1),
14
+ new Grad_1.Grad(0, 1, 1),
15
+ new Grad_1.Grad(0, -1, 1),
16
+ new Grad_1.Grad(0, 1, -1),
17
+ new Grad_1.Grad(0, -1, -1),
18
+ ];
19
+ const p = [
20
+ 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
+ 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,
22
+ 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,
23
+ 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,
24
+ 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,
25
+ 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,
26
+ 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,
27
+ 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210,
28
+ 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84,
29
+ 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,
30
+ 66, 215, 61, 156, 180,
31
+ ];
32
+ const perm = new Array(512);
33
+ const gradP = new Array(512);
34
+ function fade(t) {
35
+ return t * t * t * (t * (t * 6 - 15) + 10);
36
+ }
37
+ function lerp(a, b, t) {
38
+ return (1 - t) * a + t * b;
39
+ }
40
+ class PerlinNoise {
41
+ noise(x, y, z) {
42
+ let X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z);
43
+ x = x - X;
44
+ y = y - Y;
45
+ z = z - Z;
46
+ X = X & 255;
47
+ Y = Y & 255;
48
+ Z = Z & 255;
49
+ const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z);
50
+ const n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1);
51
+ const n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z);
52
+ const n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1);
53
+ const n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z);
54
+ const n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1);
55
+ const n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z);
56
+ const n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1);
57
+ const u = fade(x);
58
+ const v = fade(y);
59
+ const w = fade(z);
60
+ return lerp(lerp(lerp(n000, n100, u), lerp(n001, n101, u), w), lerp(lerp(n010, n110, u), lerp(n011, n111, u), w), v);
61
+ }
62
+ seed(inputSeed) {
63
+ let seed = inputSeed;
64
+ if (seed > 0 && seed < 1) {
65
+ seed *= 65536;
66
+ }
67
+ seed = Math.floor(seed);
68
+ if (seed < 256) {
69
+ seed |= seed << 8;
70
+ }
71
+ for (let i = 0; i < 256; i++) {
72
+ const v = i & 1 ? p[i] ^ (seed & 255) : p[i] ^ ((seed >> 8) & 255);
73
+ perm[i] = perm[i + 256] = v;
74
+ gradP[i] = gradP[i + 256] = grad3[v % 12];
75
+ }
76
+ }
77
+ }
78
+ exports.PerlinNoise = PerlinNoise;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PerlinNoiseGenerator = void 0;
4
+ const engine_1 = require("@tsparticles/engine");
5
+ const PerlinNoise_1 = require("./PerlinNoise");
6
+ class PerlinNoiseGenerator {
7
+ constructor() {
8
+ this.noiseGen = new PerlinNoise_1.PerlinNoise();
9
+ this.field = [];
10
+ this.noiseZ = 0;
11
+ this.options = {
12
+ draw: false,
13
+ size: 20,
14
+ increment: 0.004,
15
+ columns: 0,
16
+ rows: 0,
17
+ width: 0,
18
+ height: 0,
19
+ };
20
+ }
21
+ generate(particle) {
22
+ const pos = particle.getPosition(), point = {
23
+ x: Math.max(Math.floor(pos.x / this.options.size), 0),
24
+ y: Math.max(Math.floor(pos.y / this.options.size), 0),
25
+ }, v = engine_1.Vector.origin;
26
+ if (!this.field || !this.field[point.x] || !this.field[point.x][point.y]) {
27
+ return v;
28
+ }
29
+ v.length = this.field[point.x][point.y][1];
30
+ v.angle = this.field[point.x][point.y][0];
31
+ return v;
32
+ }
33
+ init(container) {
34
+ this.container = container;
35
+ this.setup(container);
36
+ }
37
+ reset() {
38
+ }
39
+ update() {
40
+ if (!this.container) {
41
+ return;
42
+ }
43
+ this.calculateField();
44
+ this.noiseZ += this.options.increment;
45
+ if (this.options.draw) {
46
+ this.container.canvas.draw((ctx) => this.drawField(ctx));
47
+ }
48
+ }
49
+ calculateField() {
50
+ for (let x = 0; x < this.options.columns; x++) {
51
+ for (let y = 0; y < this.options.rows; y++) {
52
+ const angle = this.noiseGen.noise(x / 50, y / 50, this.noiseZ) * Math.PI * 2;
53
+ const length = this.noiseGen.noise(x / 100 + 40000, y / 100 + 40000, this.noiseZ);
54
+ this.field[x][y][0] = angle;
55
+ this.field[x][y][1] = length;
56
+ }
57
+ }
58
+ }
59
+ drawField(ctx) {
60
+ for (let x = 0; x < this.options.columns; x++) {
61
+ for (let y = 0; y < this.options.rows; y++) {
62
+ const angle = this.field[x][y][0];
63
+ const length = this.field[x][y][1];
64
+ ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);
65
+ ctx.rotate(angle);
66
+ ctx.strokeStyle = "white";
67
+ ctx.beginPath();
68
+ ctx.moveTo(0, 0);
69
+ ctx.lineTo(0, this.options.size * length);
70
+ ctx.stroke();
71
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
72
+ }
73
+ }
74
+ }
75
+ initField() {
76
+ this.field = new Array(this.options.columns);
77
+ for (let x = 0; x < this.options.columns; x++) {
78
+ this.field[x] = new Array(this.options.rows);
79
+ for (let y = 0; y < this.options.rows; y++) {
80
+ this.field[x][y] = [0, 0];
81
+ }
82
+ }
83
+ }
84
+ resetField(container) {
85
+ var _a;
86
+ const sourceOptions = container.actualOptions.particles.move.path.options;
87
+ this.options.size = sourceOptions.size > 0 ? sourceOptions.size : 20;
88
+ this.options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : 0.004;
89
+ this.options.draw = !!sourceOptions.draw;
90
+ this.options.width = container.canvas.size.width;
91
+ this.options.height = container.canvas.size.height;
92
+ this.noiseGen.seed((_a = sourceOptions.seed) !== null && _a !== void 0 ? _a : (0, engine_1.getRandom)());
93
+ this.options.columns = Math.floor(this.options.width / this.options.size) + 1;
94
+ this.options.rows = Math.floor(this.options.height / this.options.size) + 1;
95
+ this.initField();
96
+ }
97
+ setup(container) {
98
+ this.noiseZ = 0;
99
+ this.resetField(container);
100
+ window.addEventListener("resize", () => this.resetField(container));
101
+ }
102
+ }
103
+ exports.PerlinNoiseGenerator = PerlinNoiseGenerator;
package/cjs/index.js ADDED
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadPerlinNoisePath = exports.perlinNoisePathName = void 0;
4
+ const PerlinNoiseGenerator_1 = require("./PerlinNoiseGenerator");
5
+ exports.perlinNoisePathName = "perlinNoise";
6
+ function loadPerlinNoisePath(engine) {
7
+ engine.addPathGenerator(exports.perlinNoisePathName, new PerlinNoiseGenerator_1.PerlinNoiseGenerator());
8
+ }
9
+ exports.loadPerlinNoisePath = loadPerlinNoisePath;
package/esm/Grad.js ADDED
@@ -0,0 +1,13 @@
1
+ export class Grad {
2
+ constructor(x, y, z) {
3
+ this.x = x;
4
+ this.y = y;
5
+ this.z = z;
6
+ }
7
+ dot2(x, y) {
8
+ return this.x * x + this.y * y;
9
+ }
10
+ dot3(x, y, z) {
11
+ return this.dot2(x, y) + this.z * z;
12
+ }
13
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
1
+ import { Grad } from "./Grad";
2
+ const grad3 = [
3
+ new Grad(1, 1, 0),
4
+ new Grad(-1, 1, 0),
5
+ new Grad(1, -1, 0),
6
+ new Grad(-1, -1, 0),
7
+ new Grad(1, 0, 1),
8
+ new Grad(-1, 0, 1),
9
+ new Grad(1, 0, -1),
10
+ new Grad(-1, 0, -1),
11
+ new Grad(0, 1, 1),
12
+ new Grad(0, -1, 1),
13
+ new Grad(0, 1, -1),
14
+ new Grad(0, -1, -1),
15
+ ];
16
+ const p = [
17
+ 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,
18
+ 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,
19
+ 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,
20
+ 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,
21
+ 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,
22
+ 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,
23
+ 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,
24
+ 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210,
25
+ 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84,
26
+ 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,
27
+ 66, 215, 61, 156, 180,
28
+ ];
29
+ const perm = new Array(512);
30
+ const gradP = new Array(512);
31
+ function fade(t) {
32
+ return t * t * t * (t * (t * 6 - 15) + 10);
33
+ }
34
+ function lerp(a, b, t) {
35
+ return (1 - t) * a + t * b;
36
+ }
37
+ export class PerlinNoise {
38
+ noise(x, y, z) {
39
+ let X = Math.floor(x), Y = Math.floor(y), Z = Math.floor(z);
40
+ x = x - X;
41
+ y = y - Y;
42
+ z = z - Z;
43
+ X = X & 255;
44
+ Y = Y & 255;
45
+ Z = Z & 255;
46
+ const n000 = gradP[X + perm[Y + perm[Z]]].dot3(x, y, z);
47
+ const n001 = gradP[X + perm[Y + perm[Z + 1]]].dot3(x, y, z - 1);
48
+ const n010 = gradP[X + perm[Y + 1 + perm[Z]]].dot3(x, y - 1, z);
49
+ const n011 = gradP[X + perm[Y + 1 + perm[Z + 1]]].dot3(x, y - 1, z - 1);
50
+ const n100 = gradP[X + 1 + perm[Y + perm[Z]]].dot3(x - 1, y, z);
51
+ const n101 = gradP[X + 1 + perm[Y + perm[Z + 1]]].dot3(x - 1, y, z - 1);
52
+ const n110 = gradP[X + 1 + perm[Y + 1 + perm[Z]]].dot3(x - 1, y - 1, z);
53
+ const n111 = gradP[X + 1 + perm[Y + 1 + perm[Z + 1]]].dot3(x - 1, y - 1, z - 1);
54
+ const u = fade(x);
55
+ const v = fade(y);
56
+ const w = fade(z);
57
+ return lerp(lerp(lerp(n000, n100, u), lerp(n001, n101, u), w), lerp(lerp(n010, n110, u), lerp(n011, n111, u), w), v);
58
+ }
59
+ seed(inputSeed) {
60
+ let seed = inputSeed;
61
+ if (seed > 0 && seed < 1) {
62
+ seed *= 65536;
63
+ }
64
+ seed = Math.floor(seed);
65
+ if (seed < 256) {
66
+ seed |= seed << 8;
67
+ }
68
+ for (let i = 0; i < 256; i++) {
69
+ const v = i & 1 ? p[i] ^ (seed & 255) : p[i] ^ ((seed >> 8) & 255);
70
+ perm[i] = perm[i + 256] = v;
71
+ gradP[i] = gradP[i + 256] = grad3[v % 12];
72
+ }
73
+ }
74
+ }