Skip to content

Commit

Permalink
fixes #59, use both events
Browse files Browse the repository at this point in the history
  • Loading branch information
mosch committed Mar 9, 2018
1 parent a367ece commit c5a9fad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 63 deletions.
2 changes: 1 addition & 1 deletion docs/App.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import ReactDOM from 'react-dom'
import ReactAvatarEditor from '../src/index'
import Dropzone from 'react-dropzone'
import ReactAvatarEditor from '../src/index'
import Preview from './Preview.jsx'
class App extends React.Component {
state = {
Expand Down
73 changes: 11 additions & 62 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,6 @@ const isTouchDevice = !!(

const isFileAPISupported = typeof File !== 'undefined'

const draggableEvents = {
touch: {
react: {
down: 'onTouchStart',
mouseDown: 'onMouseDown',
drag: 'onTouchMove',
move: 'onTouchMove',
mouseMove: 'onMouseMove',
up: 'onTouchEnd',
mouseUp: 'onMouseUp',
},
native: {
down: 'touchstart',
mouseDown: 'mousedown',
drag: 'touchmove',
move: 'touchmove',
mouseMove: 'mousemove',
up: 'touchend',
mouseUp: 'mouseup',
},
},
desktop: {
react: {
down: 'onMouseDown',
drag: 'onDragOver',
move: 'onMouseMove',
up: 'onMouseUp',
},
native: {
down: 'mousedown',
drag: 'dragStart',
move: 'mousemove',
up: 'mouseup',
},
},
}
const deviceEvents = isTouchDevice
? draggableEvents.touch
: draggableEvents.desktop

const pixelRatio =
typeof window !== 'undefined' && window.devicePixelRatio
? window.devicePixelRatio
Expand Down Expand Up @@ -170,17 +130,16 @@ class AvatarEditor extends React.Component {
}
this.paint(context)
if (document) {
const nativeEvents = deviceEvents.native
document.addEventListener(nativeEvents.move, this.handleMouseMove, false)
document.addEventListener(nativeEvents.up, this.handleMouseUp, false)
document.addEventListener('mousemove', this.handleMouseMove, false)
document.addEventListener('mouseup', this.handleMouseUp, false)
if (isTouchDevice) {
document.addEventListener(
nativeEvents.mouseMove,
'touchmove',
this.handleMouseMove,
false
)
document.addEventListener(
nativeEvents.mouseUp,
'touchend',
this.handleMouseUp,
false
)
Expand Down Expand Up @@ -209,12 +168,6 @@ class AvatarEditor extends React.Component {
this.paintImage(context, this.state.image, this.props.border)

if (
prevProps.image !== this.props.image ||
prevProps.width !== this.props.width ||
prevProps.height !== this.props.height ||
prevProps.position !== this.props.position ||
prevProps.scale !== this.props.scale ||
prevProps.rotate !== this.props.rotate ||
prevState.my !== this.state.my ||
prevState.mx !== this.state.mx ||
prevState.image.x !== this.state.image.x ||
Expand All @@ -226,21 +179,20 @@ class AvatarEditor extends React.Component {

componentWillUnmount() {
if (document) {
const nativeEvents = deviceEvents.native
document.removeEventListener(
nativeEvents.move,
'mousemove',
this.handleMouseMove,
false
)
document.removeEventListener(nativeEvents.up, this.handleMouseUp, false)
document.removeEventListener('mouseup', this.handleMouseUp, false)
if (isTouchDevice) {
document.removeEventListener(
nativeEvents.mouseMove,
'touchmove',
this.handleMouseMove,
false
)
document.removeEventListener(
nativeEvents.mouseUp,
'touchend',
this.handleMouseUp,
false
)
Expand Down Expand Up @@ -651,13 +603,10 @@ class AvatarEditor extends React.Component {
height: dimensions.canvas.height * pixelRatio,
style: {
...defaultStyle,
...this.props.style,
...this.props.style
},
}

attributes[deviceEvents.react.down] = this.handleMouseDown
if (isTouchDevice) {
attributes[deviceEvents.react.mouseDown] = this.handleMouseDown
onTouchStart: this.handleMouseDown,
onMouseDown: this.handleMouseDown,
}

return <canvas ref={this.setCanvas} {...attributes} />
Expand Down

0 comments on commit c5a9fad

Please sign in to comment.