Repository: Swizec/react-particles-experiment
Branch: master
Commit: 37ad5b2e9d8b
Files: 14
Total size: 16.3 KB
Directory structure:
gitextract_s_pr5jvy/
├── .gitignore
├── LICENSE
├── README.md
├── package.json
├── public/
│ ├── index.html
│ └── manifest.json
└── src/
├── actions/
│ └── index.js
├── components/
│ ├── Footer.jsx
│ ├── Header.jsx
│ ├── Particles.jsx
│ └── index.jsx
├── containers/
│ └── AppContainer.jsx
├── index.js
└── reducers/
└── index.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
bower_components
build/.*
node_modules
# See https://help.github.com/ignore-files/ for more about ignoring files.
# dependencies
/node_modules
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2016 Swizec Teller
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# Animating with React, Redux, and d3

That's a particle generator. It makes tiny circles fly out of where you click. Hold down your mouse and move around. The particles keep flying out of your cursor.
On mobile and only have a finger? That works, too.
I'm a nerd, so this is what I consider fun. Your mileage may vary. Please do click in the embed and look at those circles fly. Ain't it cool?
## Here's how it works
The whole thing is built with React, Redux, and d3. No tricks for animation; just a bit of cleverness.
Here's the general approach:
We use **React to render everything**: the page, the SVG element, the particles inside. All of it is built with React components that take some props and return some DOM. This lets us tap into React's algorithms that decide which nodes to update and when to garbage collect old nodes.
Then we use some **d3 calculations and event detection**. D3 has great random generators, so we take advantage of that. D3's mouse and touch event handlers calculate coordinates relative to our SVG. We need those, and React can't do it. React's click handlers are based on DOM nodes, which don't correspond to `(x, y)` coordinates. D3 looks at real cursor position on screen.
All **particle coordinates are in a Redux store**. Each particle also has a movement vector. The store holds some useful flags and general parameters, too. This lets us treat animation as data transformations. I'll show you what I mean in a bit.
We use **actions to communicate user events** like creating particles, starting the animation, changing mouse position, and so on. On each requestAnimationFrame, we **dispatch an "advance animation" action**.
On each action, the **reducer calculates a new state** for the whole app. This includes **new particle positions** for each step of the animation.
When the store updates, **React flushes changes** via props and because **coordinates are state, the particles move**.
The result is smooth animation.
[Keep reading to learn the details](http://swizec.com/blog/animating-with-react-redux-and-d3/swizec/6775).
A version of this article will be featured as a chapter in my upcoming [React+d3js ES6 book](http://swizec.com/reactd3js/).
================================================
FILE: package.json
================================================
{
"name": "react-particles-experiment",
"version": "0.1.0",
"private": true,
"dependencies": {
"d3": "^5.7.0",
"konva": "^2.4.2",
"prop-types": "^15.6.2",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-konva": "^16.5.2",
"react-redux": "^5.0.7",
"redux": "^4.0.1"
},
"devDependencies": {
"react-scripts": "1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
================================================
FILE: public/index.html
================================================