|
| 1 | +import React from 'react'; |
| 2 | +import {TransitionMotion, spring} from 'react-motion' |
| 3 | +import _ from 'lodash' |
| 4 | + |
| 5 | +const leavingSpringConfig = {stiffness: 60, damping: 15}; |
| 6 | +const Ripple = React.createClass({ |
| 7 | + getInitialState() { |
| 8 | + return {mouse: [], now: 't' + 0}; |
| 9 | + }, |
| 10 | + |
| 11 | + handleMouseMove({pageX, pageY}) { |
| 12 | + // Make sure the state is queued and not batched. |
| 13 | + this.setState(() => { |
| 14 | + return { |
| 15 | + mouse: [pageX - 25, pageY - 25], |
| 16 | + now: 't' + Date.now(), |
| 17 | + }; |
| 18 | + }); |
| 19 | + }, |
| 20 | + |
| 21 | + handleTouchMove(e) { |
| 22 | + e.preventDefault(); |
| 23 | + this.handleMouseMove(e.touches[0]); |
| 24 | + }, |
| 25 | + |
| 26 | + willLeave(styleCell) { |
| 27 | + return { |
| 28 | + ...styleCell.style, |
| 29 | + opacity: spring(0, leavingSpringConfig), |
| 30 | + scale: spring(2, leavingSpringConfig), |
| 31 | + }; |
| 32 | + }, |
| 33 | + |
| 34 | + render() { |
| 35 | + const {mouse: [mouseX, mouseY], now} = this.state; |
| 36 | + const styles = mouseX == null ? [] : [{ |
| 37 | + key: now, |
| 38 | + style: { |
| 39 | + opacity: spring(1), |
| 40 | + scale: spring(0), |
| 41 | + x: spring(mouseX), |
| 42 | + y: spring(mouseY), |
| 43 | + } |
| 44 | + }]; |
| 45 | + return ( |
| 46 | + <TransitionMotion willLeave={this.willLeave} styles={styles}> |
| 47 | + {circles => |
| 48 | + <div |
| 49 | + onMouseMove={_.throttle(this.handleMouseMove, 100)} |
| 50 | + onTouchMove={_.throttle(this.handleTouchMove, 100)} |
| 51 | + > |
| 52 | + {circles.map(({key, style: {opacity, scale, x, y}}) => |
| 53 | + <div |
| 54 | + key={key} |
| 55 | + className="ripple-ball" |
| 56 | + style={{ |
| 57 | + opacity: opacity, |
| 58 | + scale: scale, |
| 59 | + transform: `translate3d(${x}px, ${y}px, 0) scale(${scale})`, |
| 60 | + WebkitTransform: `translate3d(${x}px, ${y}px, 0) scale(${scale})`, |
| 61 | + }} /> |
| 62 | + )} |
| 63 | + {this.props.children} |
| 64 | + </div> |
| 65 | + } |
| 66 | + </TransitionMotion> |
| 67 | + ); |
| 68 | + }, |
| 69 | +}); |
| 70 | + |
| 71 | +export default Ripple; |
| 72 | + |
| 73 | +// based on: https://github.com/chenglou/react-motion/blob/master/demos/demo7-water-ripples/Demo.jsx |
| 74 | +// |
| 75 | +// The MIT License (MIT) |
| 76 | +// |
| 77 | +// Copyright (c) 2015 React Motion authors |
| 78 | +// |
| 79 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 80 | +// of this software and associated documentation files (the "Software"), to deal |
| 81 | +// in the Software without restriction, including without limitation the rights |
| 82 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 83 | +// copies of the Software, and to permit persons to whom the Software is |
| 84 | +// furnished to do so, subject to the following conditions: |
| 85 | +// |
| 86 | +// The above copyright notice and this permission notice shall be included in all |
| 87 | +// copies or substantial portions of the Software. |
| 88 | +// |
| 89 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 90 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 91 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 92 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 93 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 94 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 95 | +// SOFTWARE. |
0 commit comments