Skip to content

Instantly share code, notes, and snippets.

@lazyTai
Created September 25, 2017 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lazyTai/364fe9eb60078af2c4579ce9fe10b23b to your computer and use it in GitHub Desktop.
Save lazyTai/364fe9eb60078af2c4579ce9fe10b23b to your computer and use it in GitHub Desktop.
mouse move the circle springmove
/*
*
刘明泰
mail:1352983234@qq.com
github:lazyTai
*/
import React from 'react';
import {Motion, spring} from 'react-motion';
import './css.css'
const springSetting = {stiffness: 180, damping: 10};
const cc = console
export default class Demo extends React.Component {
constructor(props) {
super(props);
this.state = {
isPressed: false,
mouseXY: [0, 0]
};
};
onMouseDown(e) {
e.preventDefault()
var {pageX, pageY} = e;
this.setState({
isPressed: true,
mouseXY: [pageX, pageY]
})
}
onMouseMove(e) {
var {pageX, pageY} = e;
if (this.state.isPressed) {
this.setState({
mouseXY: [pageX, pageY]
})
}
}
onMouseUp(e) {
this.setState({
isPressed: false
})
}
getStyles() {
var {mouseXY} = this.state
return {
translateX: spring(mouseXY[0], springSetting),
translateY: spring(mouseXY[1], springSetting),
}
}
render() {
return <div className="boxWrapper"
onMouseMove={this.onMouseMove.bind(this)}
onMouseUp={this.onMouseUp.bind(this)}
>
<Motion
style={this.getStyles()}
>
{({translateX, translateY, scale, boxShadow}) =>
<div className="box"
onMouseDown={this.onMouseDown.bind(this)}
style={{
WebkitTransform: `translate3d(${translateX}px, ${translateY}px, 0)`,
transform: `translate3d(${translateX}px, ${translateY}px, 0)`,
}}
></div>
}
</Motion>
</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment