-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
155 lines (128 loc) · 2.66 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
/*
* Module dependencies
*/
var events = require('component-events');
var E = require('./e');
/**
* Export `Pinch`
*/
module.exports = Pinch;
/**
* Initialize `Pinch`
*
* @param {Element} el
* @param {Function} fn
* @return {Pinch}
* @api public
*/
function Pinch(el, fn) {
if (!(this instanceof Pinch)) return new Pinch(el, fn);
this.el = el;
this.parent = el.parentNode;
this.fn = fn || function(){};
this.midpoint = null;
this.scale = 1;
this.lastScale = 1;
this.pinching = false;
this.events = events(el, this);
this.events.bind('touchstart');
this.events.bind('touchmove');
this.events.bind('touchend');
this.fingers = {};
}
/**
* Touch start
*
* @param {Event} e
* @return {Pinch}
* @api private
*/
Pinch.prototype.ontouchstart = function(e) {
var touches = e.touches;
if (!touches || 2 != touches.length) return this;
e.preventDefault();
var coords = [];
for(var i = 0, finger; finger = touches[i]; i++) {
coords.push(finger.pageX, finger.pageY);
}
this.pinching = true;
this.distance = distance(coords);
this.midpoint = midpoint(coords);
return this;
};
/**
* Touch move
*
* @param {Event} e
* @return {Pinch}
* @api private
*/
Pinch.prototype.ontouchmove = function(e) {
var touches = e.touches;
if (!touches || touches.length != 2 || !this.pinching) return this;
var coords = [];
for(var i = 0, finger; finger = touches[i]; i++) {
coords.push(finger.pageX, finger.pageY);
}
var changed = e.changedTouches;
var dist = distance(coords);
var mid = midpoint(coords);
// make event properties mutable
e = E(e);
// iphone does scale natively, just use that
e.scale = dist / this.distance * this.scale;
e.x = mid.x;
e.y = mid.y;
this.fn(e);
this.lastScale = e.scale;
return this;
};
/**
* Touchend
*
* @param {Event} e
* @return {Pinch}
* @api private
*/
Pinch.prototype.ontouchend = function(e) {
var touches = e.touches;
if (!touches || touches.length == 2 || !this.pinching) return this;
this.scale = this.lastScale;
this.pinching = false;
return this;
};
/**
* Unbind
*
* @return {Pinch}
* @api public
*/
Pinch.prototype.unbind = function() {
this.events.unbind();
return this;
};
/**
* Get the distance between two points
*
* @param {Array} arr
* @return {Number}
* @api private
*/
function distance(arr) {
var x = Math.pow(arr[0] - arr[2], 2);
var y = Math.pow(arr[1] - arr[3], 2);
return Math.sqrt(x + y);
}
/**
* Get the midpoint
*
* @param {Array} arr
* @return {Object} coords
* @api private
*/
function midpoint(arr) {
var coords = {};
coords.x = (arr[0] + arr[2]) / 2;
coords.y = (arr[1] + arr[3]) / 2;
return coords;
}