├── .babelrc
├── .browserslistrc
├── .eslintignore
├── .eslintrc.js
├── .gitignore
├── .vscode
└── settings.json
├── LICENSE
├── README.md
├── package-lock.json
├── package.json
├── src
├── assets
│ ├── audio
│ │ └── bensound-acoustic-breeze.mp3
│ ├── demo
│ │ ├── screencast-2.gif
│ │ └── screencast.gif
│ └── img
│ │ ├── audio-bgnd.jpg
│ │ ├── favicon.png
│ │ ├── github-logo.png
│ │ ├── pause-btn.svg
│ │ └── play-btn.svg
├── components
│ ├── App
│ │ ├── AppStateProvider
│ │ │ ├── index.jsx
│ │ │ ├── init-state.js
│ │ │ └── reducer.js
│ │ ├── index.jsx
│ │ └── style.css
│ ├── AppContent
│ │ └── index.jsx
│ ├── P5Wrapper
│ │ └── index.jsx
│ ├── Section1
│ │ └── index.jsx
│ ├── Section2
│ │ └── index.jsx
│ ├── Section3
│ │ └── index.jsx
│ ├── Section31
│ │ └── index.jsx
│ └── Section4
│ │ └── index.jsx
├── index.ejs
├── index.jsx
└── sketches
│ ├── sketch-1.js
│ ├── sketch-2.js
│ └── sketch-sound.js
└── webpack.config.js
/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "env": {
3 | "production": {
4 | "presets": [
5 | [
6 | "@babel/preset-env",
7 | {
8 | "useBuiltIns": "entry",
9 | "corejs": {
10 | "version": 3,
11 | "proposals": true
12 | }
13 | }
14 | ],
15 | [
16 | "@babel/preset-react",
17 | {
18 | "useBuiltIns": true
19 | }
20 | ]
21 | ]
22 | },
23 | "development": {
24 | "plugins": [
25 | "react-hot-loader/babel"
26 | ],
27 | "presets": [
28 | [
29 | "@babel/preset-env",
30 | {
31 | "modules": false,
32 | "useBuiltIns": "entry",
33 | "corejs": {
34 | "version": 3,
35 | "proposals": true
36 | }
37 | }
38 | ],
39 | [
40 | "@babel/preset-react",
41 | {
42 | "useBuiltIns": true
43 | }
44 | ]
45 | ]
46 | }
47 | },
48 | "ignore": [
49 | "__storage__",
50 | "build",
51 | "dist",
52 | "docs",
53 | "externals",
54 | "node_modules"
55 | ]
56 | }
57 |
--------------------------------------------------------------------------------
/.browserslistrc:
--------------------------------------------------------------------------------
1 | > 5% # can add an human readable comment
2 | Last 2 versions # Or last two versions
3 | IE 11
4 |
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | __storage__
2 | build
3 | dist
4 | docs
5 | externals
6 | node_modules
7 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | extends: 'airbnb',
3 | parser: 'babel-eslint',
4 | plugins: ['react-hooks'],
5 | rules: {
6 | 'brace-style': ['error', 'stroustrup'],
7 | 'camelcase': 0,
8 | 'default-case': 0,
9 | 'indent': ['error',
10 | 4,
11 | { 'SwitchCase': 1 }
12 | ],
13 | 'new-cap': 0,
14 | 'max-len': 0,
15 | 'no-bitwise': 0,
16 | 'no-console': 0,
17 | 'no-param-reassign': 0,
18 | 'no-underscore-dangle': 0,
19 | 'no-plusplus': 0,
20 | 'prefer-destructuring': 0,
21 | 'prefer-template': 0,
22 | 'semi': 0,
23 | 'semi-style': ['error', 'first'],
24 |
25 | 'react/forbid-prop-types': 0,
26 | 'react/destructuring-assignment': 0,
27 | 'react-hooks/rules-of-hooks': 'error',
28 | 'react-hooks/exhaustive-deps': 'warn',
29 |
30 | 'react/jsx-indent': ['error', 4],
31 | 'react/jsx-indent-props': ['error', 4],
32 | 'react/no-array-index-key': 0,
33 | 'react/prefer-stateless-function': 0,
34 | },
35 | globals: {
36 | window: true
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 | npm-debug.log*
5 |
6 | # Runtime data
7 | pids
8 | *.pid
9 | *.seed
10 |
11 | # Directory for instrumented libs generated by jscoverage/JSCover
12 | lib-cov
13 |
14 | # Coverage directory used by tools like istanbul
15 | coverage
16 | test_reports
17 |
18 | # nyc test coverage
19 | .nyc_output
20 |
21 | # node-waf configuration
22 | .lock-wscript
23 |
24 | # Compiled binary addons (http://nodejs.org/api/addons.html)
25 | build/Release
26 |
27 | # Dependency directories
28 | node_modules
29 | jspm_packages
30 |
31 | # Optional npm cache directory
32 | .npm
33 |
34 | # Optional REPL history
35 | .node_repl_history
36 |
37 | # Editor files
38 | *.iml
39 |
40 | # Custom
41 | __storage__
42 | .DS_Store
43 | build
44 | dist
45 | docs
46 | .idea/
47 | stats.json
48 | .tmp
49 |
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "cSpell.words": [
3 | "atorov",
4 | "bensound",
5 | "bgnd",
6 | "camelcase",
7 | "chunkhash",
8 | "color",
9 | "contenthash",
10 | "devtool",
11 | "entrypoint",
12 | "max",
13 | "minified",
14 | "plusplus",
15 | "size",
16 | "stroustrup"
17 | ]
18 | }
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 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 | # Using React with p5.js
2 |
3 | 
4 | 
5 |
6 | This project demonstrates how to combine React (including the latest features such as **hooks and context**) and p5.js:
7 |
8 | - Multiple p5 sketches on a same screen;
9 | - Multiple instances of one sketch mounted in a same component;
10 | - Mount sketches in different points of the DOM tree;
11 | - Dynamically switch on/off sketches;
12 | - p5.js libraries support (p5.sound example)
13 | - Bidirectional communication between the main React app and sketches;
14 | - Using the latest React features:
15 | - memo()
16 | - useContext()
17 | - useEffect()
18 | - useReducer()
19 | - Hot Module Replacement (HMR): exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways:
20 | - Retain application state which is lost during a full reload;
21 | - Save valuable development time by only updating what's changed.
22 |
23 | ---
24 |
25 | Check the online version [here](http://react-hooks-p5js.surge.sh/).
26 |
27 | 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.
28 |
29 | ---
30 |
31 | Another examples, build on the same concept but using class-based components, can be found [here](https://github.com/atorov/react-p5js) and [here](https://github.com/atorov/fractal-tree-simulator).
32 |
33 | ---
34 |
35 | ## Available Scripts
36 |
37 | In the project directory, you can run:
38 |
39 | ### `npm start`
40 |
41 | Runs the app in the development mode. Open [http://localhost:8080](http://localhost:8080) to view it in the browser. The page will reload if you make edits. You will also see any lint errors in the console.
42 |
43 | ### `npm run build`
44 |
45 | 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.
46 |
47 | ## Supported Language Features and Polyfills
48 |
49 | This project supports a superset of the latest JavaScript standard.
50 |
51 | ## Syntax Highlighting and Displaying Lint Output in the Editor
52 |
53 | Тhe most popular editors should be covered and your editor should report the linting warnings.
54 |
55 | ## Adding a Router and Redux
56 |
57 | [React Router](https://reacttraining.com/react-router/) is the most popular option.
58 |
59 | [Redux](https://redux.js.org/) is a predictable state container for JavaScript apps.
60 |
61 | React Router and Redux can easily be added to the project.
62 |
63 | ---
64 |
65 | [React](https://reactjs.org/) is one of the most popular JavaScript libraries for creating single page applications.
66 |
67 | [p5.js](https://p5js.org/) is a JavaScript library with a full set of drawing functionality.
68 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-hooks-p5js",
3 | "version": "2.2.0",
4 | "private": true,
5 | "description": "Using ",
6 | "keywords": [
7 | "react",
8 | "context",
9 | "hooks",
10 | "p5js"
11 | ],
12 | "repository": {
13 | "type": "git",
14 | "url": "https://github.com/atorov/react-hooks-p5js.git"
15 | },
16 | "bugs": "https://github.com/atorov/react-hooks-p5js.git/issues",
17 | "author": "atorov",
18 | "license": "MIT",
19 | "main": "src/index.jsx",
20 | "scripts": {
21 | "clean": "rm -rf ./build",
22 | "build": "npm run clean && NODE_ENV=production webpack",
23 | "start": "nodemon --watch webpack.*.js --exec \"NODE_ENV=development webpack-dev-server\""
24 | },
25 | "dependencies": {
26 | "@hot-loader/react-dom": "^16.8.6",
27 | "prop-types": "^15.7.2",
28 | "react": "^16.8.6",
29 | "react-dom": "^16.8.6",
30 | "react-hot-loader": "^4.8.4",
31 | "shortid": "^2.2.14"
32 | },
33 | "devDependencies": {
34 | "@babel/core": "^7.4.3",
35 | "@babel/preset-env": "^7.4.3",
36 | "@babel/preset-react": "^7.0.0",
37 | "autoprefixer": "^9.6.0",
38 | "babel-eslint": "^10.0.1",
39 | "babel-loader": "^8.0.5",
40 | "copy-webpack-plugin": "^5.0.2",
41 | "core-js": "^3.0.1",
42 | "css-loader": "^2.1.1",
43 | "eslint": "^5.16.0",
44 | "eslint-config-airbnb": "^17.1.0",
45 | "eslint-plugin-import": "^2.17.2",
46 | "eslint-plugin-jsx-a11y": "^6.2.1",
47 | "eslint-plugin-react": "^7.12.4",
48 | "eslint-plugin-react-hooks": "^1.6.0",
49 | "html-webpack-plugin": "^3.2.0",
50 | "mini-css-extract-plugin": "^0.7.0",
51 | "nodemon": "^1.18.11",
52 | "postcss-loader": "^3.0.0",
53 | "regenerator-runtime": "^0.13.2",
54 | "style-loader": "^0.23.1",
55 | "webpack": "^4.33.0",
56 | "webpack-cli": "^3.3.0",
57 | "webpack-dev-server": "^3.3.1"
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/assets/audio/bensound-acoustic-breeze.mp3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atorov/react-hooks-p5js/a72a42f4e2bdec7dbf1f19cba15031c0654ef067/src/assets/audio/bensound-acoustic-breeze.mp3
--------------------------------------------------------------------------------
/src/assets/demo/screencast-2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atorov/react-hooks-p5js/a72a42f4e2bdec7dbf1f19cba15031c0654ef067/src/assets/demo/screencast-2.gif
--------------------------------------------------------------------------------
/src/assets/demo/screencast.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atorov/react-hooks-p5js/a72a42f4e2bdec7dbf1f19cba15031c0654ef067/src/assets/demo/screencast.gif
--------------------------------------------------------------------------------
/src/assets/img/audio-bgnd.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atorov/react-hooks-p5js/a72a42f4e2bdec7dbf1f19cba15031c0654ef067/src/assets/img/audio-bgnd.jpg
--------------------------------------------------------------------------------
/src/assets/img/favicon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atorov/react-hooks-p5js/a72a42f4e2bdec7dbf1f19cba15031c0654ef067/src/assets/img/favicon.png
--------------------------------------------------------------------------------
/src/assets/img/github-logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/atorov/react-hooks-p5js/a72a42f4e2bdec7dbf1f19cba15031c0654ef067/src/assets/img/github-logo.png
--------------------------------------------------------------------------------
/src/assets/img/pause-btn.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
45 |
--------------------------------------------------------------------------------
/src/assets/img/play-btn.svg:
--------------------------------------------------------------------------------
1 |
2 |
3 |
39 |
--------------------------------------------------------------------------------
/src/components/App/AppStateProvider/index.jsx:
--------------------------------------------------------------------------------
1 | import React, { createContext, useReducer } from 'react'
2 | import PropTypes from 'prop-types'
3 |
4 | import initState from './init-state'
5 | import reducer from './reducer'
6 |
7 | export const AppDispatchContext = createContext(() => { })
8 | export const AppStateContext = createContext(initState)
9 |
10 | function AppStateProvider({ children }) {
11 | const [state, dispatch] = useReducer(reducer, initState)
12 |
13 | return (
14 |
20 |
21 |
28 |
29 |