@tsparticles/path-perlin-noise
Version:
tsParticles perlin noise path
148 lines (135 loc) • 13.1 kB
JavaScript
/*!
* Author : Matteo Bruni
* MIT license: https://opensource.org/licenses/MIT
* Demo / Generator : https://particles.js.org/
* GitHub : https://www.github.com/matteobruni/tsparticles
* How to use? : Check the GitHub README
* v3.9.1
*/
/*
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
* This devtool is neither made for production nor for readable output files.
* It uses "eval()" calls to create a separate source file in the browser devtools.
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
* or disable the default devtool with "devtool: false".
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
*/
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory(require("@tsparticles/engine"), require("@tsparticles/perlin-noise"));
else if(typeof define === 'function' && define.amd)
define(["@tsparticles/engine", "@tsparticles/perlin-noise"], factory);
else {
var a = typeof exports === 'object' ? factory(require("@tsparticles/engine"), require("@tsparticles/perlin-noise")) : factory(root["window"], root["window"]);
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(this, (__WEBPACK_EXTERNAL_MODULE__tsparticles_engine__, __WEBPACK_EXTERNAL_MODULE__tsparticles_perlin_noise__) => {
return /******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./dist/browser/PerlinNoiseGenerator.js":
/*!**********************************************!*\
!*** ./dist/browser/PerlinNoiseGenerator.js ***!
\**********************************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ PerlinNoiseGenerator: () => (/* binding */ PerlinNoiseGenerator)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _tsparticles_perlin_noise__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @tsparticles/perlin-noise */ \"@tsparticles/perlin-noise\");\n/* harmony import */ var _tsparticles_perlin_noise__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_tsparticles_perlin_noise__WEBPACK_IMPORTED_MODULE_1__);\n\n\nconst double = 2,\n doublePI = Math.PI * double,\n defaultOptions = {\n draw: false,\n size: 20,\n increment: 0.004,\n columns: 0,\n rows: 0,\n layers: 0,\n width: 0,\n height: 0,\n factor: {\n angle: 0.02,\n length: 0.01\n },\n offset: {\n x: 40000,\n y: 40000,\n z: 40000\n }\n };\nclass PerlinNoiseGenerator {\n constructor() {\n this.noiseGen = new _tsparticles_perlin_noise__WEBPACK_IMPORTED_MODULE_1__.PerlinNoise();\n this.field = [];\n this.noiseW = 0;\n this.options = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.deepExtend)({}, defaultOptions);\n }\n generate(particle) {\n const pos = particle.getPosition(),\n {\n size\n } = this.options,\n point = {\n x: Math.max(Math.floor(pos.x / size), 0),\n y: Math.max(Math.floor(pos.y / size), 0),\n z: Math.max(Math.floor(pos.z / size), 0)\n },\n v = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.Vector.origin,\n {\n field\n } = this;\n return field?.[point.x]?.[point.y]?.[point.z] ? field[point.x][point.y][point.z].copy() : v;\n }\n init(container) {\n this.container = container;\n this._setup();\n }\n reset() {}\n update() {\n if (!this.container) {\n return;\n }\n this._calculateField();\n this.noiseW += this.options.increment;\n if (this.options.draw) {\n this.container.canvas.draw(ctx => this._drawField(ctx));\n }\n }\n _calculateField() {\n const {\n field,\n noiseGen,\n options,\n noiseW\n } = this,\n lengthFactor = options.factor.length,\n angleFactor = options.factor.angle;\n for (let x = 0; x < options.columns; x++) {\n for (let y = 0; y < options.rows; y++) {\n for (let z = 0; z < options.layers; z++) {\n const cell = field[x][y][z];\n cell.length = noiseGen.noise4d(x * lengthFactor + options.offset.x, y * lengthFactor + options.offset.y, z * lengthFactor + options.offset.z, noiseW);\n cell.angle = noiseGen.noise4d(x * angleFactor, y * angleFactor, z * angleFactor, noiseW) * doublePI;\n }\n }\n }\n }\n _drawField(ctx) {\n const {\n field,\n options\n } = this;\n for (let x = 0; x < options.columns; x++) {\n const column = field[x];\n for (let y = 0; y < options.rows; y++) {\n const cell = column[y][0],\n {\n angle,\n length\n } = cell;\n ctx.setTransform(1, 0, 0, 1, x * this.options.size, y * this.options.size);\n ctx.rotate(angle);\n ctx.strokeStyle = \"white\";\n ctx.beginPath();\n ctx.moveTo(0, 0);\n ctx.lineTo(0, this.options.size * length);\n ctx.stroke();\n ctx.setTransform(1, 0, 0, 1, 0, 0);\n }\n }\n }\n _initField() {\n const {\n columns,\n rows,\n layers\n } = this.options;\n this.field = new Array(columns);\n for (let x = 0; x < columns; x++) {\n this.field[x] = new Array(rows);\n for (let y = 0; y < rows; y++) {\n this.field[x][y] = new Array(layers);\n for (let z = 0; z < layers; z++) {\n this.field[x][y][z] = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.Vector.origin;\n }\n }\n }\n }\n _resetField() {\n const container = this.container;\n if (!container) {\n return;\n }\n const sourceOptions = container.actualOptions.particles.move.path.options,\n {\n options\n } = this;\n options.width = container.canvas.size.width;\n options.height = container.canvas.size.height;\n options.size = sourceOptions.size > 0 ? sourceOptions.size : defaultOptions.size;\n options.increment = sourceOptions.increment > 0 ? sourceOptions.increment : defaultOptions.increment;\n options.draw = !!sourceOptions.draw;\n const offset = sourceOptions.offset;\n options.offset.x = offset?.x ?? defaultOptions.offset.x;\n options.offset.y = offset?.y ?? defaultOptions.offset.y;\n options.offset.z = offset?.z ?? defaultOptions.offset.z;\n const factor = sourceOptions.factor;\n options.factor.angle = factor?.angle ?? defaultOptions.factor.angle;\n options.factor.length = factor?.length ?? defaultOptions.factor.length;\n options.seed = sourceOptions.seed;\n this.noiseGen.seed(options.seed ?? (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_0__.getRandom)());\n options.columns = Math.floor(options.width / options.size) + 1;\n options.rows = Math.floor(options.height / options.size) + 1;\n options.layers = Math.floor(container.zLayers / options.size) + 1;\n this._initField();\n }\n _setup() {\n this.noiseW = 0;\n this._resetField();\n addEventListener(\"resize\", () => this._resetField());\n }\n}\n\n//# sourceURL=webpack://@tsparticles/path-perlin-noise/./dist/browser/PerlinNoiseGenerator.js?\n}");
/***/ }),
/***/ "./dist/browser/index.js":
/*!*******************************!*\
!*** ./dist/browser/index.js ***!
\*******************************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ loadPerlinNoisePath: () => (/* binding */ loadPerlinNoisePath),\n/* harmony export */ perlinNoisePathName: () => (/* binding */ perlinNoisePathName)\n/* harmony export */ });\n/* harmony import */ var _PerlinNoiseGenerator_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./PerlinNoiseGenerator.js */ \"./dist/browser/PerlinNoiseGenerator.js\");\n\nconst perlinNoisePathName = \"perlinNoise\";\nasync function loadPerlinNoisePath(engine, refresh = true) {\n engine.checkVersion(\"3.9.1\");\n await engine.addPathGenerator(perlinNoisePathName, new _PerlinNoiseGenerator_js__WEBPACK_IMPORTED_MODULE_0__.PerlinNoiseGenerator(), refresh);\n}\n\n//# sourceURL=webpack://@tsparticles/path-perlin-noise/./dist/browser/index.js?\n}");
/***/ }),
/***/ "@tsparticles/engine":
/*!*********************************************************************************************************************************!*\
!*** external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"} ***!
\*********************************************************************************************************************************/
/***/ ((module) => {
module.exports = __WEBPACK_EXTERNAL_MODULE__tsparticles_engine__;
/***/ }),
/***/ "@tsparticles/perlin-noise":
/*!***************************************************************************************************************************************************!*\
!*** external {"commonjs":"@tsparticles/perlin-noise","commonjs2":"@tsparticles/perlin-noise","amd":"@tsparticles/perlin-noise","root":"window"} ***!
\***************************************************************************************************************************************************/
/***/ ((module) => {
module.exports = __WEBPACK_EXTERNAL_MODULE__tsparticles_perlin_noise__;
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => (module['default']) :
/******/ () => (module);
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
/******/
/******/ // startup
/******/ // Load entry module and return exports
/******/ // This entry module can't be inlined because the eval devtool is used.
/******/ var __webpack_exports__ = __webpack_require__("./dist/browser/index.js");
/******/
/******/ return __webpack_exports__;
/******/ })()
;
});