Skip to content

Commit afed8bd

Browse files
committed
Add ripple mouse effect
1 parent cc0f072 commit afed8bd

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
This project is published under license TODO.
2+
3+
GraphQL and its logo and color fall under BSD-3-Clause:
4+
5+
Copyright 2016 Facebook
6+
7+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
8+
9+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
12+
13+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

components/ripple.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.

css/raw.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ a:hover {
2323
text-decoration: underline;
2424
}
2525
26-
26+
.ripple-ball {
27+
width: 50px;
28+
height: 50px;
29+
border-radius: 99px;
30+
position: absolute;
31+
border: 1px solid #f3f3f3;
32+
pointer-events: none;
33+
}
2734
`

pages/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import _ from 'lodash'
1111

1212
import Email from '../components/email'
1313
import BookLI from '../components/book-li'
14+
import Ripple from '../components/ripple'
1415
import muiTheme from '../lib/muitheme'
1516
import { white, color } from '../lib/styles'
1617

@@ -66,11 +67,12 @@ class Index extends Component {
6667
render() {
6768
return (
6869
<MuiThemeProvider muiTheme={muiTheme}>
69-
<div>
70+
<Ripple>
7071
<Head>
7172
<title>
7273
The GraphQL Book
7374
</title>
75+
7476
</Head>
7577
<div className="animation-target">
7678
<Paper
@@ -296,7 +298,7 @@ class Index extends Component {
296298
</a>
297299
</Paper>
298300
</div>
299-
</div>
301+
</Ripple>
300302
</MuiThemeProvider>
301303
)
302304
}

0 commit comments

Comments
 (0)