forked from a-fung/jQueryTouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.gesture2.js
178 lines (159 loc) · 7.95 KB
/
jquery.gesture2.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
166
167
168
169
170
171
172
173
174
175
176
177
178
/*!
* jQueryTouch v0.0.6
* https://github.com/a-fung/jQueryTouch
*
* Copyright 2012 Man Kwan Liu
* Released under the Apache License Version 2.0
* http://www.apache.org/licenses/
*
* Date: Wed Oct 2012 23:14:09 GMT-0700 (Pacific Daylight Time)
*/
(function ($) {
// an easy gesture function
// will make a jQuery object move/scale/rotate on touch(es)
$.fn.gesture = function (options) {
if (!options || typeof (options) != "object") {
options = {};
}
// default options
options = $.extend(
{
// whether dragging, scaling and/or rotating are enable on the object
// all these can be boolean or a function that returns boolean
drag: true,
scale: true,
rotate: true,
// we can make this jQuery object responds to touch on another jQuery
touchtarget: null,
// LinkItem which will move and transform along with this
linkItem: null
},
options);
var linkItem = options.linkItem ? $(options.linkItem) : null,
_linkItem = linkItem,
linkItem_original_x = 0,
linkItem_original_y = 0,
linkItem_original_css = "";
var touchtarget = options.touchtarget ? $(options.touchtarget) : this,
_this = this,
in_drag = false,
in_gesture = false,
original_x = 0,
original_y = 0,
original_css = "",
__getvalue__ = function (value) {
return (typeof (value) == "function") ? value() : value;
};
// a simple drag handler
var touch_handler = function (event) {
if (in_gesture || !__getvalue__(options.drag)) return; // do nothing when gesture is happening
if (in_drag) {
var dx = ((event.touches.length == 0) ? event.pageX : event.touches[0].pageX) - original_x,
dy = ((event.touches.length == 0) ? event.pageY : event.touches[0].pageY) - original_y,
new_css = "translate(" + dx + "px," + dy + "px) " + original_css;
_this.__transform__(new_css);
// Transform LinkItem
if(_linkItem){
var linkItem_dx = ((event.touches.length == 0) ? event.pageX : event.touches[0].pageX) - linkItem_original_x,
linkItem_dy = ((event.touches.length == 0) ? event.pageY : event.touches[0].pageY) - linkItem_original_y,
linkItem_new_css = "translate(" + linkItem_dx + "px," + linkItem_dy + "px) " + linkItem_original_css;
_linkItem.__transform__(linkItem_new_css);
}
} else {
if (in_drag = event.type == "_gesture2_touch_start") {
original_x = event.touches[0].pageX;
original_y = event.touches[0].pageY;
original_css = _this.__transform__();
if (!original_css || original_css == "none") original_css = "";
// For linkItem
if(_linkItem){
linkItem_original_x = event.touches[0].pageX;
linkItem_original_y = event.touches[0].pageY;
linkItem_original_css = _linkItem.__transform__();
if (!linkItem_original_css || linkItem_original_css == "none") linkItem_original_css = "";
}
}
}
if (event.type == "_gesture2_touch_end") {
in_drag = false;
}
};
// gesture handler to make the object move/scale/rotate
var gesture_handler = function (event) {
in_drag = false;
if (!__getvalue__(options.scale) && !__getvalue__(options.rotate)) return;
if (in_gesture) {
var dx = event.pageX - original_x,
dy = event.pageY - original_y,
new_css = (__getvalue__(options.drag) ? ("translate(" + event.pageX + "px," + event.pageY + "px) ") : ("translate(" + original_x + "px," + original_y + "px) ")) +
(__getvalue__(options.scale) ? ("scale(" + event.scale + ")") : "") +
(__getvalue__(options.rotate) ? ("rotate(" + event.rotation + "rad) ") : "") +
"translate(" + (-original_x) + "px," + (-original_y) + "px) " +
original_css;
_this.__transform__(new_css);
// Transform LinkItem
if(_linkItem){
var linkItem_dx = event.pageX - linkItem_original_x,
linkItem_dy = event.pageY - linkItem_original_y,
linkItem_new_css = (__getvalue__(options.drag) ? ("translate(" + event.pageX + "px," + event.pageY + "px) ") : ("translate(" + linkItem_original_x + "px," + linkItem_original_y + "px) ")) +
(__getvalue__(options.scale) ? ("scale(" + event.scale + ")") : "") +
(__getvalue__(options.rotate) ? ("rotate(" + event.rotation + "rad) ") : "") +
"translate(" + (-linkItem_original_x) + "px," + (-linkItem_original_y) + "px) " +
linkItem_original_css;
_linkItem.__transform__(linkItem_new_css);
}
} else {
in_gesture = true;
original_css = _this.__transform__();
original_x = event.pageX;
original_y = event.pageY;
if (!original_css || original_css == "none") original_css = "";
// For LinkItem
if(_linkItem){
linkItem_original_css = _linkItem.__transform__();
linkItem_original_x = event.pageX;
linkItem_original_y = event.pageY;
if (!linkItem_original_css || linkItem_original_css == "none") linkItem_original_css = "";
}
}
if (event.type == "_gesture2_gesture_end") {
in_gesture = false;
}
};
// init gesture and touches, add handlers
touchtarget.gestureInit({ prefix: "_gesture2_", gesture_prefix: "_gesture2_" });
touchtarget.on("_gesture2_touch_start", touch_handler);
touchtarget.on("_gesture2_touch_move", touch_handler);
touchtarget.on("_gesture2_touch_end", touch_handler);
touchtarget.on("_gesture2_gesture_start", gesture_handler);
touchtarget.on("_gesture2_gesture_move", gesture_handler);
touchtarget.on("_gesture2_gesture_end", gesture_handler);
return this;
};
// a helper function to transform the object
$.fn.__transform__ = function (value) {
if (typeof (value) == "undefined") return this.css("transform");
if (typeof (value) == "string") {
if (!this.data("__offset__")) {
this.css("transform", "");
this.css("-moz-transform", "");
this.css("-ms-transform", "");
this.css("-o-transform", "");
this.css("-webkit-transform", "");
var offset = (-this.offset().left) + "px " + (-this.offset().top) + "px";
this.css("transform-origin", offset);
this.css("-moz-transform-origin", offset);
this.css("-ms-transform-origin", offset);
this.css("-o-transform-origin", offset);
this.css("-webkit-transform-origin", offset);
this.data("__offset__", offset);
}
this.css("transform", value);
this.css("-moz-transform", value);
this.css("-ms-transform", value);
this.css("-o-transform", value);
this.css("-webkit-transform", value);
}
return this;
};
})(jQuery);