Skip to content

Commit 14de2c3

Browse files
committed
fix(touch-events): support touch events
1 parent c0d9d73 commit 14de2c3

File tree

3 files changed

+36
-25
lines changed

3 files changed

+36
-25
lines changed

examples/fit-to-area.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@
5656
minRatio: 1 / 1.5
5757
});
5858

59-
$('.icon-zoom-in').on('click', function(event) {
59+
$('.icon-zoom-in').on('click touchstart', function(event) {
6060
crop.zoomIn();
6161
event.stopPropagation();
6262
});
6363

64-
$('.icon-zoom-out').on('click', function(event) {
64+
$('.icon-zoom-out').on('click touchstart', function(event) {
6565
crop.zoomOut();
6666
event.stopPropagation();
6767
});

examples/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@
6565
height: 500
6666
})
6767

68-
$('.icon-zoom-in').on('click', function(event) {
68+
$('.icon-zoom-in').on('click touchstart', function(event) {
6969
crop.zoomIn();
7070
event.stopPropagation();
7171
});
7272

73-
$('.icon-zoom-out').on('click', function(event) {
73+
$('.icon-zoom-out').on('click touchstart', function(event) {
7474
crop.zoomOut();
7575
event.stopPropagation();
7676
});

src/events.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
const $ = require('jquery')
22

3+
const getPageCoordinates = function (event) {
4+
if (event.type.includes('touch')) {
5+
return {
6+
pageX: event.originalEvent.changedTouches[0].pageX,
7+
pageY: event.originalEvent.changedTouches[0].pageY
8+
}
9+
}
10+
return {pageX: event.pageX, pageY: event.pageY}
11+
}
12+
313
module.exports = class Events {
414
constructor ({parent, view, horizontal, vertical, actions}) {
515
this.parent = parent
@@ -22,20 +32,22 @@ module.exports = class Events {
2232

2333
pan () {
2434
const $doc = $(document)
25-
this.view.on('mousedown.srcissors', (e1) => {
35+
this.view.on('mousedown.srcissors touchstart.srcissors', (e1) => {
2636
const panData = {
2737
startX: this.parent.preview.x,
2838
startY: this.parent.preview.y
2939
}
3040

3141
e1.preventDefault()
32-
$doc.on('mousemove.srcissors-pan', (e2) => {
33-
panData.dx = e2.pageX - e1.pageX
34-
panData.dy = e2.pageY - e1.pageY
42+
$doc.on('mousemove.srcissors-pan touchmove.srcissors-pan', (e2) => {
43+
const {pageX, pageY} = getPageCoordinates(e2)
44+
const {pageX: prevPageX, pageY: prevPageY} = getPageCoordinates(e1)
45+
panData.dx = pageX - prevPageX
46+
panData.dy = pageY - prevPageY
3547
this.parent.onPan(panData)
36-
}).on('mouseup.srcissors-pan', () => {
37-
$doc.off('mouseup.srcissors-pan')
38-
$doc.off('mousemove.srcissors-pan')
48+
}).on('mouseup.srcissors-pan touchend.srcissors-pan', () => {
49+
$doc.off('mouseup.srcissors-pan touchend.srcissors-pan')
50+
$doc.off('mousemove.srcissors-pan touchmove.srcissors-pan')
3951

4052
// only trigger panEnd if pan has been called
4153
if (panData.dx != null) this.parent.onPanEnd()
@@ -46,10 +58,10 @@ module.exports = class Events {
4658
doubleClick () {
4759
let lastClick
4860

49-
this.view.on('mousedown.srcissors', event => {
61+
this.view.on('mousedown.srcissors touchstart.srcissors', event => {
5062
const now = new Date().getTime()
5163
if (lastClick && (lastClick > (now - this.doubleClickThreshold))) {
52-
this.parent.onDoubleClick({pageX: event.pageX, pageY: event.pageY})
64+
this.parent.onDoubleClick(getPageCoordinates(event))
5365
}
5466
lastClick = now
5567
})
@@ -73,7 +85,7 @@ module.exports = class Events {
7385
positions.forEach(position => {
7486
const $handler = $template.clone()
7587
$handler.addClass(`resize-handler-${position}`)
76-
$handler.on('mousedown.srcissors', this.getResizeMouseDown(position))
88+
$handler.on('mousedown.srcissors touchstart.srcissors', this.getResizeMouseDown(position))
7789

7890
this.view.append($handler)
7991
})
@@ -83,30 +95,29 @@ module.exports = class Events {
8395
const $doc = $(document)
8496

8597
return (event) => {
86-
let lastX = event.pageX
87-
let lastY = event.pageY
88-
98+
let {pageX: lastX, pageY: lastY} = getPageCoordinates(event)
8999
event.stopPropagation()
90100

91-
$doc.on('mousemove.srcissors-resize', e2 => {
101+
$doc.on('mousemove.srcissors-resize touchmove.srcissors-resize', e2 => {
92102
let dx, dy
103+
const {pageX, pageY} = getPageCoordinates(e2)
93104
switch (position) {
94105
case 'top': case 'bottom':
95-
dy = e2.pageY - lastY
106+
dy = pageY - lastY
96107
if (position === 'top') { dy = -dy }
97-
lastY = e2.pageY
108+
lastY = pageY
98109
break
99110
case 'left': case 'right':
100-
dx = e2.pageX - lastX
111+
dx = pageX - lastX
101112
if (position === 'left') { dx = -dx }
102-
lastX = e2.pageX
113+
lastX = pageX
103114
break
104115
}
105116

106117
this.parent.onResize({position, dx, dy})
107-
}).on('mouseup.srcissors-resize', () => {
108-
$doc.off('mouseup.srcissors-resize')
109-
$doc.off('mousemove.srcissors-resize')
118+
}).on('mouseup.srcissors-resize touchend.srcissors-resize', () => {
119+
$doc.off('mouseup.srcissors-resize touchmove.srcissors-resize')
120+
$doc.off('mousemove.srcissors-resize touchend.srcissors-resize')
110121

111122
// only trigger panEnd if pan has been called
112123
this.parent.onResizeEnd({position})

0 commit comments

Comments
 (0)