├── .eslintrc ├── public ├── favicon.ico ├── img │ ├── screencast.gif │ ├── screeshot.png │ └── github-logo.png ├── manifest.json └── index.html ├── src ├── index.css ├── index.js └── components │ ├── App │ ├── __test__ │ │ └── index.test.js │ └── index.jsx │ └── P5Wrapper │ ├── sketch2 │ ├── lib.js │ └── index.js │ ├── sketch1 │ └── index.js │ └── index.jsx ├── .gitignore ├── package.json ├── LICENSE └── README.md /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "react-app" 3 | } 4 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/react-p5js/HEAD/public/favicon.ico -------------------------------------------------------------------------------- /src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: sans-serif; 3 | margin: 0; 4 | padding: 0; 5 | } 6 | -------------------------------------------------------------------------------- /public/img/screencast.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/react-p5js/HEAD/public/img/screencast.gif -------------------------------------------------------------------------------- /public/img/screeshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/react-p5js/HEAD/public/img/screeshot.png -------------------------------------------------------------------------------- /public/img/github-logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atorov/react-p5js/HEAD/public/img/github-logo.png -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react' 2 | import { render } from 'react-dom' 3 | 4 | import App from './components/App/' 5 | 6 | import './index.css' 7 | 8 | render(, document.getElementById('root')) 9 | -------------------------------------------------------------------------------- /src/components/App/__test__/index.test.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import App from '../'; 4 | 5 | window.p5 = function() {}; 6 | 7 | it('renders without crashing', () => { 8 | const div = document.createElement('div'); 9 | ReactDOM.render(, div); 10 | ReactDOM.unmountComponentAtNode(div); 11 | }); 12 | -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "react-p5js", 3 | "name": "Using React with p5.js", 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 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/components/P5Wrapper/sketch2/lib.js: -------------------------------------------------------------------------------- 1 | export function drawBezier(s, x1, y1, x2, y2) { 2 | s.noStroke() 3 | s.fill(127, 0, 50, 31) 4 | s.bezier( 5 | x1, y1, 6 | s.width / 2 - s.props.slider * 2, s.height / 2 - s.props.slider, 7 | s.width / 2 + s.props.slider * 2, s.height / 2 + s.props.slider, 8 | x2, y2 9 | ) 10 | } 11 | 12 | export function drawFloatingText(s, rx, ry) { 13 | s.noStroke() 14 | s.fill(127, 0, 50, 191) 15 | s.ellipse(rx, ry, 8, 8) 16 | 17 | s.textAlign(s.CENTER); 18 | s.textSize(12) 19 | s.text(`(${s.round(rx)}, ${s.round(ry)})`, rx, ry + 16) 20 | } 21 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | react-p5js 10 | 11 | 12 | 15 |
16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/components/P5Wrapper/sketch1/index.js: -------------------------------------------------------------------------------- 1 | export default function (s) { 2 | s.props = {} 3 | s.onSetAppState = () => {} 4 | 5 | s.setup = function() { 6 | s.createCanvas(900, 300) 7 | console.log('::: displayDensity:', s.displayDensity()) 8 | console.log('::: pixelDensity:', s.pixelDensity()) 9 | } 10 | 11 | s.draw = function() { 12 | if (s.frameCount % 60 === 1) { 13 | s.onSetAppState({ frameRate: s.frameRate().toFixed(1) }) 14 | } 15 | 16 | s.background(127, 0, 50) 17 | const weight = s.map(s.props.slider, 5, 290, 0, 10) 18 | s.strokeWeight(weight) 19 | s.stroke(127, 255, 205) 20 | const alpha = s.map(s.props.slider, 5, 290, 255, 0) 21 | s.fill(127, 255, 205, alpha) 22 | s.ellipse(s.width / 2, s.height / 2, s.props.slider) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/components/P5Wrapper/sketch2/index.js: -------------------------------------------------------------------------------- 1 | import { 2 | drawBezier, 3 | drawFloatingText, 4 | } from './lib' 5 | 6 | export default function (s) { 7 | s.props = {} 8 | 9 | s.setup = function() { 10 | s.createCanvas(900, 300) 11 | } 12 | 13 | s.draw = function() { 14 | s.background(255) 15 | const noiseX = s.noise(s.frameCount / 150.0) * s.props.slider 16 | const noiseY = s.noise((s.frameCount + 1000) / 150.0) * s.props.slider 17 | 18 | const coordX1 = s.map(noiseX, 5, 290, 50, s.width / 2 - 50) 19 | const coordY1 = s.map(noiseY, 5, 290, 20, s.height - 20) 20 | drawFloatingText(s, coordX1, coordY1) 21 | 22 | const coordX2 = s.map(noiseX, 5, 290, s.width - 50, s.width / 2 + 50) 23 | const coordY2 = s.map(noiseY, 5, 290, s.height - 20, 20) 24 | drawFloatingText(s, coordX2, coordY2) 25 | 26 | drawBezier(s, coordX1, coordY1, coordX2, coordY2) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-p5js", 3 | "version": "0.3.0", 4 | "description": "Using React with p5.js", 5 | "keywords": [ 6 | "react", 7 | "reactjs", 8 | "p5", 9 | "p5js", 10 | "p5.js", 11 | "processing" 12 | ], 13 | "homepage": ".", 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/atorov/react-p5js.git" 17 | }, 18 | "bugs": { 19 | "url": "https://github.com/atorov/react-p5js/issues" 20 | }, 21 | "author": "atorov", 22 | "license": "MIT", 23 | "main": "src/index.js", 24 | "dependencies": { 25 | "react": "^16.6.0", 26 | "react-dom": "^16.6.0", 27 | "react-scripts": "^2.1.1" 28 | }, 29 | "scripts": { 30 | "start": "react-scripts start", 31 | "build": "react-scripts build", 32 | "test": "react-scripts test --env=jsdom", 33 | "eject": "react-scripts eject" 34 | }, 35 | "browserslist": [ 36 | ">0.2%", 37 | "not dead", 38 | "not ie <= 11", 39 | "not op_mini all" 40 | ] 41 | } 42 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/components/P5Wrapper/index.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | import PropTypes from 'prop-types' 3 | 4 | import sketch1 from './sketch1' 5 | import sketch2 from './sketch2' 6 | 7 | export default class P5Wrapper extends Component { 8 | static propTypes = { 9 | p5Props: PropTypes.object.isRequired, 10 | onSetAppState: PropTypes.func.isRequired, 11 | } 12 | 13 | componentDidMount() { 14 | this.canvas1 = new window.p5(sketch1, 'canvas1-container') 15 | this.canvas1.props = this.props.p5Props 16 | this.canvas1.onSetAppState = this.props.onSetAppState 17 | 18 | this.canvas2 = new window.p5(sketch2, 'canvas2-container') 19 | this.canvas2.props = this.props.p5Props 20 | } 21 | 22 | shouldComponentUpdate(nextProps) { 23 | this.canvas1.props = nextProps.p5Props 24 | this.canvas2.props = nextProps.p5Props 25 | return false 26 | } 27 | 28 | componentWillUnmount() { 29 | this.canvas1.remove() 30 | this.canvas2.remove() 31 | } 32 | 33 | render() { 34 | return ( 35 | <> 36 |
40 |
44 | 45 | ) 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/components/App/index.jsx: -------------------------------------------------------------------------------- 1 | import React, { Component } from 'react' 2 | 3 | import P5Wrapper from '../P5Wrapper' 4 | 5 | export default class App extends Component { 6 | constructor() { 7 | super() 8 | this.state = { 9 | slider: 100, 10 | frameRate: null, 11 | } 12 | } 13 | 14 | onSetAppState = (newState, cb) => this.setState(newState, cb) 15 | 16 | onSliderChange = (event) => this.setState({ slider: +event.target.value }) 17 | 18 | render() { 19 | return ( 20 | <> 21 | 25 | 26 |
27 | {this.state.slider} 28 |
29 | 36 |
37 | 38 |

39 | Sketch frame rate:  40 | {this.state.frameRate} 41 |  fps 42 |

43 | 44 |

45 | 46 | github logo 54 | 55 |

56 | 57 | ) 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /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/react-p5js/master/public/img/screeshot.png) 11 | 12 | This project demonstrates the possibility of combining React and p5.js. 13 | 14 | --- 15 | 16 | **UPDATE 2018-11-09 (v0.3):** 17 | 18 | ![alt application screen cast](https://raw.githubusercontent.com/atorov/react-p5js/master/public/img/screencast.gif) 19 | 20 | - Updated: Synchronized with the new React (16.3+) lifecycle methods; 21 | - Updated: Improved and simplified communication between main React app and p5 sketches; 22 | - New feature: Multiple independent P5 on the same page; 23 | - New feature: Sketch code splitting into separate files. 24 | 25 | --- 26 | 27 | [React](https://reactjs.org/) is one of the most popular JavaScript libraries for creating single page applications.
28 | [p5.js](https://p5js.org/) is a JavaScript library with a full set of drawing functionality. 29 | 30 | Check the online version [here](http://react-p5js.surge.sh/). 31 | 32 | 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. 33 | 34 | A much more advanced example, built on the same concept, can be found [here](https://github.com/atorov/fractal-tree-simulator). 35 | 36 | ## Project Structure 37 | The project is build on [Create React App](https://github.com/facebookincubator/create-react-app). 38 | 39 | For the project to build, **these files must exist with exact filenames**: 40 | 41 | * `public/index.html` is the page template; 42 | * `src/index.js` is the JavaScript entry point. 43 | 44 | 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. 45 | 46 | Only files inside `public` can be used from `public/index.html`. 47 | 48 | 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. 49 | 50 | ## Available Scripts 51 | 52 | In the project directory, you can run: 53 | 54 | ### `npm start` 55 | 56 | 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. 57 | 58 | ### `npm test` 59 | 60 | Launches the test runner in the interactive watch mode.
61 | 62 | ### `npm run build` 63 | 64 | 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. 65 | 66 | ### `npm run eject` 67 | 68 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 69 | 70 | 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. 71 | 72 | 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. 73 | 74 | 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. 75 | 76 | ## Supported Language Features and Polyfills 77 | 78 | This project supports a superset of the latest JavaScript standard. In addition to [ES6](https://github.com/lukehoban/es6features) syntax features, it also supports: 79 | 80 | * [Exponentiation Operator](https://github.com/rwaldron/exponentiation-operator) (ES2016). 81 | * [Async/await](https://github.com/tc39/ecmascript-asyncawait) (ES2017). 82 | * [Object Rest/Spread Properties](https://github.com/sebmarkbage/ecmascript-rest-spread) (stage 3 proposal). 83 | * [Dynamic import()](https://github.com/tc39/proposal-dynamic-import) (stage 3 proposal) 84 | * [Class Fields and Static Properties](https://github.com/tc39/proposal-class-public-fields) (part of stage 3 proposal). 85 | * [JSX](https://facebook.github.io/react/docs/introducing-jsx.html) and [Flow](https://flowtype.org/) syntax. 86 | 87 | ## Syntax Highlighting and Displaying Lint Output in the Editor 88 | 89 | Тhe most popular editors should be covered and your editor should report the linting warnings.. 90 | 91 | ## Code Splitting 92 | 93 | Code splitting allows you to split your code into small chunks which you can then load on demand. 94 | This project setup supports code splitting via [dynamic `import()`](http://2ality.com/2017/01/import-operator.html#loading-code-on-demand). 95 | 96 | ## Adding a Router and Redux 97 | 98 | [React Router](https://reacttraining.com/react-router/) is the most popular option. 99 | [Redux](https://redux.js.org/) is a predictable state container for JavaScript apps. 100 | 101 | React Router and Redux can easily be added to the project. 102 | 103 | ## Running Tests 104 | 105 | [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. 106 | 107 | ### Filename Conventions 108 | 109 | Jest will look for test files with any of the following popular naming conventions: 110 | 111 | * Files with `.js` suffix in `__tests__` folders. 112 | * Files with `.test.js` suffix. 113 | * Files with `.spec.js` suffix. 114 | 115 | 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. 116 | 117 | ### Command Line Interface 118 | 119 | 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. 120 | --------------------------------------------------------------------------------