-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
165 lines (130 loc) · 3.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @module pan-zoom
*
* Events for pan and zoom
*/
'use strict'
var Impetus = require('impetus')
var wheel = require('mouse-wheel')
var touchPinch = require('touch-pinch')
var position = require('touch-position')
var hasPassive = require('has-passive-events')
module.exports = panZoom
function panZoom (target, cb) {
if (target instanceof Function) {
cb = target
target = document.documentElement || document.body
}
if (typeof target === 'string') target = document.querySelector(target)
//enable panning
var touch = position.emitter({
element: target
})
var impetus
var initX = 0, initY = 0, init = true, srcElement
var initFn = function (e) { init = true, srcElement = e.srcElement }
target.addEventListener('mousedown', initFn)
target.addEventListener('touchstart', initFn, hasPassive ? { passive: true } : false)
var lastY = 0, lastX = 0
impetus = new Impetus({
source: target,
update: function (x, y, ...args) {
if (init) {
init = false
initX = touch.position[0]
initY = touch.position[1]
}
var e = {
srcElement,
target: target,
type: 'mouse',
dx: x - lastX, dy: y - lastY, dz: 0,
x: touch.position[0], y: touch.position[1],
x0: initX, y0: initY
}
lastX = x
lastY = y
schedule(e)
},
multiplier: 1,
friction: .75
})
var isPassive = [window, document, document.documentElement, document.body].indexOf(target) >= 0
//enable zooming
var wheelListener = wheel(target, function (dx, dy, dz, e) {
if (!isPassive) e.preventDefault()
schedule({
srcElement: e.srcElement,
target: target,
type: 'mouse',
dx: 0, dy: 0, dz: dy,
x: touch.position[0], y: touch.position[1],
x0: touch.position[0], y0: touch.position[1]
})
})
//mobile pinch zoom
var pinch = touchPinch(target)
var mult = 2
var initialCoords
pinch.on('start', function (curr) {
var f1 = pinch.fingers[0];
var f2 = pinch.fingers[1];
initialCoords = [
f2.position[0] * .5 + f1.position[0] * .5,
f2.position[1] * .5 + f1.position[1] * .5
]
impetus && impetus.pause()
})
pinch.on('end', function () {
if (!initialCoords) return
initialCoords = null
impetus && impetus.resume()
})
pinch.on('change', function (curr, prev) {
if (!pinch.pinching || !initialCoords) return
schedule({
srcElement: target,
target: target,
type: 'touch',
dx: 0, dy: 0, dz: - (curr - prev) * mult,
x: initialCoords[0], y: initialCoords[1],
x0: initialCoords[0], y0: initialCoords[1]
})
})
// schedule function to current or next frame
var planned, frameId
function schedule (ev) {
if (frameId != null) {
if (!planned) planned = ev
else {
planned.dx += ev.dx
planned.dy += ev.dy
planned.dz += ev.dz
planned.x = ev.x
planned.y = ev.y
}
return
}
// Firefox sometimes does not clear webgl current drawing buffer
// so we have to schedule callback to the next frame, not the current
// cb(ev)
frameId = window.requestAnimationFrame(function () {
cb(ev)
frameId = null
if (planned) {
var arg = planned
planned = null
schedule(arg)
}
})
}
return function unpanzoom () {
touch.dispose()
target.removeEventListener('mousedown', initFn)
target.removeEventListener('touchstart', initFn)
impetus.destroy()
target.removeEventListener('wheel', wheelListener)
pinch.disable()
window.cancelAnimationFrame(frameId)
}
}