├── .eslintrc ├── .gitignore ├── LICENSE ├── README.md ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── img │ ├── github-logo.png │ └── screeshot.png ├── index.html └── manifest.json └── src ├── components ├── App │ ├── __test__ │ │ └── index.test.js │ └── index.jsx ├── ControlPanel │ └── index.js ├── Header │ └── index.jsx └── P5Wrapper │ ├── index.jsx │ └── sketch.js ├── index.css ├── index.js ├── lib ├── config │ └── index.js └── sliders │ └── index.js └── registerServiceWorker.js /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "react-app" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | 6 | # testing 7 | /coverage 8 | 9 | # production 10 | /build 11 | 12 | # misc 13 | .DS_Store 14 | .env.local 15 | .env.development.local 16 | .env.test.local 17 | .env.production.local 18 | 19 | npm-debug.log* 20 | yarn-debug.log* 21 | yarn-error.log* 22 | 23 | __storage__ 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 atorov 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ### UPDATE 2019-04-21 3 | 4 | An example, build on the same concept but using the latest React features such as **hooks and context**), can be found [here](https://github.com/atorov/react-hooks-p5js) 5 | 6 | --- 7 | 8 | # Using React with p5.js 9 | 10 | ![alt application screenshot](https://raw.githubusercontent.com/atorov/fractal-tree-simulator/master/public/img/screeshot.png) 11 | 12 | This project demonstrates the possibility of combining React, Bootstrap, p5.js and other advanced technologies for real-time visualization, animation and simulation. 13 | 14 | [React](https://reactjs.org/) is one of the most popular JavaScript libraries for creating single page applications.
15 | [p5.js](https://p5js.org/) is a JavaScript library with a full set of drawing functionality.
16 | [Bootstrap](https://getbootstrap.com/) is an open source toolkit for developing with HTML, CSS, and JS.
17 | 18 | Check the online version [here](http://fractal-tree-simulator.surge.sh/). 19 | 20 | The basic idea is that the p5.js sketch is wrapped in a React component. The data that comes into the sketch is passed on to this component as props. Callbacks are used to return information back from the sketch to the application. 21 | 22 | A much more simpler example, built on the same concept, can be found [here](https://github.com/atorov/react-p5js). 23 | 24 | ## p5.js sketch 25 | - p5.Graphics for creating a graphics buffer; 26 | - p5.Vector for describing a two dimensional vectors; 27 | - p5.noise() - Perlin noise random sequence generator that produces a more natural ordered, harmonic succession of numbers compared to the standard random() function; 28 | - p5.colorMode() - both RGB and HSB color spaces; 29 | - p5.translate() and p5.rotate() matrix transformations, etc. 30 | 31 | ## Project Structure 32 | The project is build on [Create React App](https://github.com/facebookincubator/create-react-app). 33 | 34 | For the project to build, **these files must exist with exact filenames**: 35 | 36 | * `public/index.html` is the page template; 37 | * `src/index.js` is the JavaScript entry point. 38 | 39 | You can delete or rename the other files. You may create subdirectories inside `src`. For faster rebuilds, only files inside `src` are processed by Webpack. You need to **put any JS and CSS files inside `src`**, otherwise Webpack won’t see them. 40 | 41 | Only files inside `public` can be used from `public/index.html`. 42 | 43 | You can, however, create more top-level directories. They will not be included in the production build so you can use them for things like documentation. 44 | 45 | ## Available Scripts 46 | 47 | In the project directory, you can run: 48 | 49 | ### `npm start` 50 | 51 | Runs the app in the development mode. Open [http://localhost:3000](http://localhost:3000) to view it in the browser. The page will reload if you make edits. You will also see any lint errors in the console. 52 | 53 | ### `npm test` 54 | 55 | Launches the test runner in the interactive watch mode.
56 | 57 | ### `npm run build` 58 | 59 | Builds the app for production to the `build` folder. It correctly bundles React in production mode and optimizes the build for the best performance. The build is minified and the filenames include the hashes. Your app is ready to be deployed. 60 | 61 | ### `npm run eject` 62 | 63 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 64 | 65 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 66 | 67 | Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 68 | 69 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 70 | 71 | ## Supported Language Features and Polyfills 72 | 73 | This project supports a superset of the latest JavaScript standard. In addition to [ES6](https://github.com/lukehoban/es6features) syntax features, it also supports: 74 | 75 | * [Exponentiation Operator](https://github.com/rwaldron/exponentiation-operator) (ES2016). 76 | * [Async/await](https://github.com/tc39/ecmascript-asyncawait) (ES2017). 77 | * [Object Rest/Spread Properties](https://github.com/sebmarkbage/ecmascript-rest-spread) (stage 3 proposal). 78 | * [Dynamic import()](https://github.com/tc39/proposal-dynamic-import) (stage 3 proposal) 79 | * [Class Fields and Static Properties](https://github.com/tc39/proposal-class-public-fields) (part of stage 3 proposal). 80 | * [JSX](https://facebook.github.io/react/docs/introducing-jsx.html) and [Flow](https://flowtype.org/) syntax. 81 | 82 | ## Syntax Highlighting and Displaying Lint Output in the Editor 83 | 84 | Тhe most popular editors should be covered and your editor should report the linting warnings.. 85 | 86 | ## Code Splitting 87 | 88 | Code splitting allows you to split your code into small chunks which you can then load on demand. 89 | This project setup supports code splitting via [dynamic `import()`](http://2ality.com/2017/01/import-operator.html#loading-code-on-demand). 90 | 91 | ## Adding a Router and Redux 92 | 93 | [React Router](https://reacttraining.com/react-router/) is the most popular option. 94 | [Redux](https://redux.js.org/) is a predictable state container for JavaScript apps. 95 | 96 | React Router and Redux can easily be added to the project. 97 | 98 | ## Running Tests 99 | 100 | [Jest](https://facebook.github.io/jest/) is a Node-based runner. While Jest provides browser globals such as `window` thanks to [jsdom](https://github.com/tmpvar/jsdom), they are only approximations of the real browser behavior. Jest is intended to be used for unit tests of your logic and your components rather than the DOM quirks. 101 | 102 | ### Filename Conventions 103 | 104 | Jest will look for test files with any of the following popular naming conventions: 105 | 106 | * Files with `.js` suffix in `__tests__` folders. 107 | * Files with `.test.js` suffix. 108 | * Files with `.spec.js` suffix. 109 | 110 | The `.test.js` / `.spec.js` files (or the `__tests__` folders) can be located at any depth under the `src` top level folder. It is recommended to put the test files (or `__tests__` folders) next to the code they are testing so that relative imports appear shorter. For example, if `App.test.js` and `App.js` are in the same folder, the test just needs to `import App from './App'` instead of a long relative path. Colocation also helps find tests more quickly in larger projects. 111 | 112 | ### Command Line Interface 113 | 114 | When you run `npm test`, Jest will launch in the watch mode. Every time you save a file, it will re-run the tests, just like `npm start` recompiles the code. 115 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fractal-tree-simulator", 3 | "version": "2.1.1", 4 | "private": true, 5 | "description": "An advanced example of using React with p5.js", 6 | "keywords": [ 7 | "react", 8 | "reactjs", 9 | "p5", 10 | "p5js", 11 | "p5.js", 12 | "processing", 13 | "fractals", 14 | "tree", 15 | "simulator", 16 | "physics" 17 | ], 18 | "homepage": ".", 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/atorov/fractal-tree-simulator.git" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/atorov/fractal-tree-simulator/issues" 25 | }, 26 | "author": "atorov", 27 | "license": "MIT", 28 | "main": "src/index.js", 29 | "dependencies": { 30 | "react": "^16.2.0", 31 | "react-bootstrap": "^0.32.1", 32 | "react-dom": "^16.2.0", 33 | "react-scripts": "1.1.0" 34 | }, 35 | "scripts": { 36 | "start": "react-scripts start", 37 | "build": "react-scripts build", 38 | "test": "react-scripts test --env=jsdom", 39 | "eject": "react-scripts eject" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/fractal-tree-simulator/cdd38f19c87bb06defc9e275fef4171f2da4b26f/public/favicon.ico -------------------------------------------------------------------------------- /public/img/github-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/fractal-tree-simulator/cdd38f19c87bb06defc9e275fef4171f2da4b26f/public/img/github-logo.png -------------------------------------------------------------------------------- /public/img/screeshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/fractal-tree-simulator/cdd38f19c87bb06defc9e275fef4171f2da4b26f/public/img/screeshot.png -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Fractal Tree Simulator 11 | 12 | 13 | 16 |
17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "fractal-tree-simulator", 3 | "name": "Fractal Tree Simulator", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": "./index.html", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /src/components/App/__test__/index.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | 4 | import App from '../'; 5 | 6 | window.p5 = function() {}; 7 | 8 | it('renders without crashing', () => { 9 | const div = document.createElement('div'); 10 | ReactDOM.render(, div); 11 | ReactDOM.unmountComponentAtNode(div); 12 | }); 13 | -------------------------------------------------------------------------------- /src/components/App/index.jsx: -------------------------------------------------------------------------------- 1 | /* global $ */ 2 | 3 | import React, { Component } from "react"; 4 | import ControlPanel from "../ControlPanel/"; 5 | import Header from "../Header/"; 6 | import P5Wrapper from "../P5Wrapper/"; 7 | import config from "../../lib/config/"; 8 | import { getPlant } from "../../lib/sliders/"; 9 | 10 | export default class App extends Component { 11 | // Constructor --------------------------------------------------------------- 12 | constructor() { 13 | super(); 14 | 15 | const plants = []; 16 | for (let i = 0; i < config.plantNum; i++) { 17 | plants.push({ ...getPlant() }); 18 | } 19 | 20 | this.state = { 21 | p5Props: { 22 | status: "", 23 | plants, 24 | }, 25 | }; 26 | } 27 | 28 | // Event handlers ------------------------------------------------------------ 29 | getBranchesNum = (index, num) => { 30 | const plants = $.extend(true, [], this.state.p5Props.plants); 31 | plants[index].branchesNum = num; 32 | this.setState({ p5Props: { ...this.state.p5Props, plants } }); 33 | } 34 | 35 | onReady = () => this.setState({ status: "ready" }); 36 | 37 | onSliderChange = (key) => (event) => { 38 | const plants = $.extend(true, [], this.state.p5Props.plants); 39 | plants.forEach((plant) => plant[key] = +event.target.value); 40 | this.setState({ p5Props: { ...this.state.p5Props, plants } }); 41 | } 42 | 43 | // Main renderer ------------------------------------------------------------- 44 | render() { 45 | return ( 46 |
47 | {/* Header --------------------------------------------------------- */} 48 |
49 | 50 | {/* Main content --------------------------------------------------- */} 51 |
55 | {/* p5.js sketch ------------------------------------------------- */} 56 | 61 | 62 | {/* Control panel ------------------------------------------------ */} 63 | 67 | 68 | {/* Source code link --------------------------------------------- */} 69 |
70 |

71 | 72 | github logo 73 | 74 |

75 |
76 |
77 | ); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/components/ControlPanel/index.js: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import PropTypes from "prop-types"; 3 | import config from "../../lib/config/"; 4 | 5 | export default class ControlPanel extends Component { 6 | static propTypes = { 7 | plants: PropTypes.array.isRequired, 8 | onSliderChange: PropTypes.func.isRequired, 9 | }; 10 | 11 | render() { 12 | return ( 13 |
14 | { 15 | config.sliders.map((item, index) => ( 16 |
20 | {item.label}:  21 | {this.props.plants[0][item.key].toFixed(item.precision)} 22 |
23 | 30 |
31 |
32 | )) 33 | } 34 | 35 |
36 | {this.props.plants.map((plant, index) => ( 37 | 38 | 39 | Tree {(index + 1)} 40 | 41 | 42 | : {plant.branchesNum || "--"} branches;  43 | 44 | 45 | ))} 46 |
47 |
48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/components/Header/index.jsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | const Header = () => ( 4 | 8 | ); 9 | 10 | export default Header; 11 | -------------------------------------------------------------------------------- /src/components/P5Wrapper/index.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from "react"; 2 | import PropTypes from "prop-types"; 3 | 4 | import sketch from "./sketch.js"; 5 | 6 | class P5Wrapper extends Component { 7 | static propTypes = { 8 | plants: PropTypes.array.isRequired, 9 | getBranchesNum: PropTypes.func.isRequired, 10 | onReady: PropTypes.func.isRequired, 11 | }; 12 | 13 | componentDidMount() { 14 | this.canvas = new window.p5(sketch, "app-p5_container"); 15 | this.canvas.setOnReady(this.props.onReady); 16 | } 17 | 18 | componentWillReceiveProps(nextProps) { 19 | this.canvas.pushProps(nextProps); 20 | } 21 | 22 | shouldComponentUpdate() { // just in case :) 23 | return false; 24 | } 25 | 26 | componentWillUnmount() { 27 | this.canvas.remove(); 28 | } 29 | 30 | render() { 31 | // console.log("::: P5Wrapper.props:", this.props); 32 | return ( 33 |
37 | ); 38 | } 39 | } 40 | 41 | export default P5Wrapper; 42 | -------------------------------------------------------------------------------- /src/components/P5Wrapper/sketch.js: -------------------------------------------------------------------------------- 1 | /* globals $ */ 2 | 3 | export default function (p) { 4 | // Methods ------------------------------------------------------------------- 5 | p.setOnReady = function(cb) { 6 | onReady = cb; 7 | }; 8 | 9 | p.pushProps = function (_props) { 10 | props = _props; 11 | p.loop(); 12 | } 13 | 14 | // Private members ----------------------------------------------------------- 15 | let onReady = () => {}; 16 | let props = {}; 17 | let plants = []; 18 | 19 | // Private classes ----------------------------------------------------------- 20 | class Plant { 21 | constructor(width, height, play, id) { 22 | this.pg = p.createGraphics(width, height); 23 | this.pg.colorMode(p.HSB, 360, 100, 100, 1.0); 24 | this.play = play; 25 | this.id = id; 26 | } 27 | 28 | render(cfg) { 29 | const pg = this.pg; 30 | const { 31 | brightness, 32 | gamma, 33 | hue, 34 | recursionDepth, randomization, 35 | saturation, size 36 | } = cfg; 37 | 38 | this.branchesCounter = 0; 39 | this.noiseOffset = (p.frameCount + this.id * 20); 40 | 41 | pg.clear(); 42 | pg.background(0, 0); 43 | 44 | pg.translate(pg.width / 2, pg.height - size / 6); // mark the bottom center of pg 45 | pg.ellipseMode(p.CENTER); pg.noStroke(); pg.fill(hue, saturation, 100 * brightness, 0.1); 46 | pg.ellipse(0, 0, size * 1.5, size / 2); 47 | pg.resetMatrix(); 48 | 49 | pg.translate(pg.width / 2, pg.height - size / 6); // move to the bottom center of pg 50 | // pg.translate(pg.width / 2, pg.height / 2 ); // move to the center of pg 51 | 52 | const deltaGammaNoise = (this.play ? (p.noise(this.noiseOffset / 150 + 4000) - 0.5) * 2 : 0) * gamma; 53 | 54 | const sizeNoise = 1 - p.noise(this.id + 1 * 10, this.branchesCounter * 10) ** (randomization + 2); 55 | const scaledSize = size * sizeNoise; 56 | const dir = pg.createVector(0, -1) // v(0, -1) -> grow to top 57 | .setMag(scaledSize).rotate(gamma + deltaGammaNoise); 58 | 59 | this.renderBranch(dir, recursionDepth, cfg); 60 | pg.resetMatrix(); 61 | } 62 | 63 | renderBranch(dir, depth, cfg) { 64 | const pg = this.pg; 65 | const { 66 | alpha, 67 | branchMinLength, brightness, 68 | decCoeffA, decCoeffB, 69 | deltaAlpha, deltaBeta, 70 | hue, 71 | randomization, recursionDepth, 72 | saturation, 73 | thickness, 74 | } = cfg; 75 | 76 | this.branchesCounter++; 77 | 78 | const size = dir.mag(); 79 | const sizeNoise = 1 - p.noise(this.id + 1 * 10, this.branchesCounter * 10) ** randomization; 80 | const scaledSize = size * sizeNoise; 81 | const rescaledDir = dir.copy().setMag(scaledSize); 82 | 83 | pg.stroke(hue, saturation, (recursionDepth - depth) / recursionDepth * 100 * brightness, depth / recursionDepth + 0.33); 84 | pg.strokeWeight((depth + 1) * thickness); 85 | pg.line(0, 0, rescaledDir.x, rescaledDir.y); 86 | // pg.noFill(); pg.ellipse(rescaledDir.x, rescaledDir.y, 5); 87 | 88 | if (depth > 0 && scaledSize >= branchMinLength) { 89 | const alphaNoise = (this.play ? (p.noise(this.noiseOffset / 110 + 1000) - 0.5) * 2 : 0) * alpha; 90 | const deltaAlphaNoise = (this.play ? (p.noise(this.noiseOffset / 60 + 2000) - 0.5) * 2 : 0) * deltaAlpha; 91 | const deltaBetaNoise = (this.play ? (p.noise(this.noiseOffset / 60 + 3000) - 0.5) * 2 : 0) * deltaBeta; 92 | 93 | 94 | pg.push(); 95 | pg.translate(rescaledDir.x, rescaledDir.y); 96 | const newDirA = rescaledDir.copy().setMag(scaledSize * decCoeffA) 97 | .rotate(alpha + alphaNoise + deltaAlpha + deltaAlphaNoise); 98 | this.renderBranch(newDirA, depth - 1, cfg); 99 | pg.pop() 100 | 101 | pg.push(); 102 | pg.translate(rescaledDir.x, rescaledDir.y); 103 | const newDirB = rescaledDir.copy().setMag(scaledSize * decCoeffB) 104 | .rotate(-alpha - alphaNoise + deltaBeta + deltaBetaNoise); 105 | this.renderBranch(newDirB, depth - 1, cfg); 106 | pg.pop() 107 | } 108 | } 109 | } 110 | 111 | // Lifecycle methods ========================================================= 112 | // preload() ----------------------------------------------------------------- 113 | p.preload = function() {} 114 | 115 | // setup() ------------------------------------------------------------------- 116 | p.setup = function() { 117 | // console.log("::: setup() props:", props); 118 | 119 | p.createCanvas(800, 300); 120 | p.colorMode(p.RGB, 255, 255, 255, 1.0); 121 | p.pixelDensity(1); 122 | p.frameRate(15); 123 | p.noLoop(); 124 | onReady(); 125 | } 126 | 127 | // draw() -------------------------------------------------------------------- 128 | p.draw = function() { 129 | // console.log("::: draw() props:", props); 130 | 131 | if (!plants.length && !$.isEmptyObject(props)) { 132 | console.log("::: draw()/init props:", props); 133 | props.plants.forEach((_, index) => plants.push(new Plant(500, 300, !(index % 1), index))); 134 | } 135 | else if (plants.length && !$.isEmptyObject(props)) { 136 | // console.log("::: draw()/loop props:", props); 137 | p.background(120, 120, 120); 138 | 139 | plants.forEach((plant, index) => { 140 | const plantProps = props.plants[index]; 141 | plant.render(plantProps); 142 | const x = p.width / (plants.length + 1) * (index + 1); 143 | const y = p.height / 2; 144 | p.imageMode(p.CENTER); p.image(plant.pg, x, y); 145 | 146 | if (!(p.frameCount % (60 + index)) && p.frameCount) { 147 | props.getBranchesNum(index, plant.branchesCounter); 148 | } 149 | }); 150 | } 151 | 152 | // s.noLoop(); 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | background: rgb(120, 120, 120); 3 | font-family: sans-serif; 4 | margin: 0; 5 | padding: 0; 6 | } 7 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import registerServiceWorker from "./registerServiceWorker"; 4 | import App from "./components/App/"; 5 | import "./index.css"; 6 | 7 | ReactDOM.render(, document.getElementById("root")); 8 | registerServiceWorker(); 9 | -------------------------------------------------------------------------------- /src/lib/config/index.js: -------------------------------------------------------------------------------- 1 | export default { 2 | plantNum: 3, 3 | 4 | sliders: [ 5 | { 6 | label: "Recursion Depth", 7 | key: "recursionDepth", 8 | min: 0, 9 | init: 7, 10 | max: 7, 11 | step: 1, 12 | precision: 0, 13 | }, 14 | { 15 | label: "Size", 16 | key: "size", 17 | min: 0, 18 | init: 100, 19 | max: 200, 20 | step: 1, 21 | precision: 0, 22 | }, 23 | { 24 | label: "Branck Min Length", 25 | key: "branchMinLength", 26 | min: 0.0, 27 | init: 1.0, 28 | max: 10.0, 29 | step: 0.1, 30 | precision: 1, 31 | }, 32 | { 33 | label: "Decreasing Coefficient A", 34 | key: "decCoeffA", 35 | min: 0.00, 36 | init: 0.90, 37 | max: 1.00, 38 | step: 0.01, 39 | precision: 2, 40 | }, 41 | { 42 | label: "Decreasing Coefficient B", 43 | key: "decCoeffB", 44 | min: 0.00, 45 | init: 0.90, 46 | max: 1.00, 47 | step: 0.01, 48 | precision: 2, 49 | }, 50 | { 51 | label: "Thickness", 52 | key: "thickness", 53 | min: 0.00, 54 | init: 1.00, 55 | max: 2.00, 56 | step: 0.01, 57 | precision: 2, 58 | }, 59 | { 60 | label: "Angle α", 61 | key: "alpha", 62 | min: 0.000, 63 | init: Math.PI / 8, 64 | max: Math.PI / 4, 65 | step: 0.001, 66 | precision: 3, 67 | }, 68 | { 69 | label: "Angle Δα", 70 | key: "deltaAlpha", 71 | min: -Math.PI / 8, 72 | init: 0, 73 | max: Math.PI / 8, 74 | step: 0.001, 75 | precision: 3, 76 | }, 77 | { 78 | label: "Angle Δβ", 79 | key: "deltaBeta", 80 | min: -Math.PI / 8, 81 | init: 0, 82 | max: Math.PI / 8, 83 | step: 0.001, 84 | precision: 3, 85 | }, 86 | { 87 | label: "Angle γ", 88 | key: "gamma", 89 | min: -Math.PI / 4, 90 | init: 0, 91 | max: Math.PI / 4, 92 | step: 0.001, 93 | precision: 3, 94 | }, 95 | { 96 | label: "Randomization", 97 | key: "randomization", 98 | min: 0.0, 99 | init: 2.0, 100 | max: 10.0, 101 | step: 0.01, 102 | precision: 2, 103 | }, 104 | { 105 | label: "Hue", 106 | key: "hue", 107 | min: 0, 108 | init: 180, 109 | max: 360, 110 | step: 1, 111 | precision: 0, 112 | }, 113 | { 114 | label: "Saturation", 115 | key: "saturation", 116 | min: 0, 117 | init: 100, 118 | max: 100, 119 | step: 1, 120 | precision: 0, 121 | }, 122 | { 123 | label: "Brightness", 124 | key: "brightness", 125 | min: 0.00, 126 | init: 1.00, 127 | max: 1.00, 128 | step: 0.01, 129 | precision: 2, 130 | }, 131 | ], 132 | }; 133 | -------------------------------------------------------------------------------- /src/lib/sliders/index.js: -------------------------------------------------------------------------------- 1 | import config from "../config/" 2 | 3 | function getSlidersInitValue(key, data) { 4 | return data.find((item) => item.key === key).init; 5 | } 6 | 7 | export function getPlant() { 8 | return { 9 | recursionDepth: getSlidersInitValue("recursionDepth", config.sliders), 10 | size: getSlidersInitValue("size", config.sliders), 11 | branchMinLength: getSlidersInitValue("branchMinLength", config.sliders), 12 | decCoeffA: getSlidersInitValue("decCoeffA", config.sliders), 13 | decCoeffB: getSlidersInitValue("decCoeffB", config.sliders), 14 | thickness: getSlidersInitValue("thickness", config.sliders), 15 | alpha: getSlidersInitValue("alpha", config.sliders), 16 | deltaAlpha: getSlidersInitValue("deltaAlpha", config.sliders), 17 | deltaBeta: getSlidersInitValue("deltaBeta", config.sliders), 18 | gamma: getSlidersInitValue("gamma", config.sliders), 19 | randomization: getSlidersInitValue("randomization", config.sliders), 20 | hue: getSlidersInitValue("hue", config.sliders), 21 | saturation: getSlidersInitValue("saturation", config.sliders), 22 | brightness: getSlidersInitValue("brightness", config.sliders), 23 | branchesNum: null, 24 | }; 25 | }; 26 | -------------------------------------------------------------------------------- /src/registerServiceWorker.js: -------------------------------------------------------------------------------- 1 | // In production, we register a service worker to serve assets from local cache. 2 | 3 | // This lets the app load faster on subsequent visits in production, and gives 4 | // it offline capabilities. However, it also means that developers (and users) 5 | // will only see deployed updates on the "N+1" visit to a page, since previously 6 | // cached resources are updated in the background. 7 | 8 | // To learn more about the benefits of this model, read https://goo.gl/KwvDNy. 9 | // This link also includes instructions on opting out of this behavior. 10 | 11 | const isLocalhost = Boolean( 12 | window.location.hostname === 'localhost' || 13 | // [::1] is the IPv6 localhost address. 14 | window.location.hostname === '[::1]' || 15 | // 127.0.0.1/8 is considered localhost for IPv4. 16 | window.location.hostname.match( 17 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 18 | ) 19 | ); 20 | 21 | export default function register() { 22 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 23 | // The URL constructor is available in all browsers that support SW. 24 | const publicUrl = new URL(process.env.PUBLIC_URL, window.location); 25 | if (publicUrl.origin !== window.location.origin) { 26 | // Our service worker won't work if PUBLIC_URL is on a different origin 27 | // from what our page is served on. This might happen if a CDN is used to 28 | // serve assets; see https://github.com/facebookincubator/create-react-app/issues/2374 29 | return; 30 | } 31 | 32 | window.addEventListener('load', () => { 33 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 34 | 35 | if (isLocalhost) { 36 | // This is running on localhost. Lets check if a service worker still exists or not. 37 | checkValidServiceWorker(swUrl); 38 | 39 | // Add some additional logging to localhost, pointing developers to the 40 | // service worker/PWA documentation. 41 | navigator.serviceWorker.ready.then(() => { 42 | console.log( 43 | 'This web app is being served cache-first by a service ' + 44 | 'worker. To learn more, visit https://goo.gl/SC7cgQ' 45 | ); 46 | }); 47 | } else { 48 | // Is not local host. Just register service worker 49 | registerValidSW(swUrl); 50 | } 51 | }); 52 | } 53 | } 54 | 55 | function registerValidSW(swUrl) { 56 | navigator.serviceWorker 57 | .register(swUrl) 58 | .then(registration => { 59 | registration.onupdatefound = () => { 60 | const installingWorker = registration.installing; 61 | installingWorker.onstatechange = () => { 62 | if (installingWorker.state === 'installed') { 63 | if (navigator.serviceWorker.controller) { 64 | // At this point, the old content will have been purged and 65 | // the fresh content will have been added to the cache. 66 | // It's the perfect time to display a "New content is 67 | // available; please refresh." message in your web app. 68 | console.log('New content is available; please refresh.'); 69 | } else { 70 | // At this point, everything has been precached. 71 | // It's the perfect time to display a 72 | // "Content is cached for offline use." message. 73 | console.log('Content is cached for offline use.'); 74 | } 75 | } 76 | }; 77 | }; 78 | }) 79 | .catch(error => { 80 | console.error('Error during service worker registration:', error); 81 | }); 82 | } 83 | 84 | function checkValidServiceWorker(swUrl) { 85 | // Check if the service worker can be found. If it can't reload the page. 86 | fetch(swUrl) 87 | .then(response => { 88 | // Ensure service worker exists, and that we really are getting a JS file. 89 | if ( 90 | response.status === 404 || 91 | response.headers.get('content-type').indexOf('javascript') === -1 92 | ) { 93 | // No service worker found. Probably a different app. Reload the page. 94 | navigator.serviceWorker.ready.then(registration => { 95 | registration.unregister().then(() => { 96 | window.location.reload(); 97 | }); 98 | }); 99 | } else { 100 | // Service worker found. Proceed as normal. 101 | registerValidSW(swUrl); 102 | } 103 | }) 104 | .catch(() => { 105 | console.log( 106 | 'No internet connection found. App is running in offline mode.' 107 | ); 108 | }); 109 | } 110 | 111 | export function unregister() { 112 | if ('serviceWorker' in navigator) { 113 | navigator.serviceWorker.ready.then(registration => { 114 | registration.unregister(); 115 | }); 116 | } 117 | } 118 | --------------------------------------------------------------------------------