-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
489 lines (453 loc) · 20.8 KB
/
index.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="author" content="Takashi Okamoto / BuzaMoto" />
<meta name="copyright" content="Programming by Takashi Okamoto. All rights reserved." />
<!-- (c) 2011 BuzaMoto. All rights reserved. -->
<title>BuzaMoto JSTapGestureRecognizer</title>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="viewport" content="user-scalable=yes" />
<!-- <script src="lib/prototype.js" type="text/javascript"></script> -->
<script src="lib/jquery.js" type="text/javascript"></script>
<script src="dist/gesturerecognizer.js" type="text/javascript"></script>
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shCore.css" rel="stylesheet" type="text/css" />
<link href="http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js" ></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js"></script>
<script type="text/javascript" charset="utf-8">
//<![CDATA[
// -- INIT ------------------------------------------------------------------
window.onload = function(event) {
// basic examples
(function() {
var tap = new JSTapGestureRecognizer();
tap.numberOfTapsRequired = 1;
tap.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('tapped : ' + sender.taps + ' time(s)');
}
});
// create pan gesture
var pan = new JSPanGestureRecognizer();
pan.maximumNumberOfTouches = 1;
pan.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var x = sender.view.x + sender.translation.x,
y = sender.view.y + sender.translation.y;
sender.view.setTransform({x: x, y: y });
sender.setTranslation({ x: 0, y: 0 });
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.x = x;
sender.view.y = y;
}
}
});
var press = new JSLongPressGestureRecognizer();
press.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('Long Pressed!')
}
});
var pinch = new JSPinchGestureRecognizer();
pinch.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var scale = sender.view.scale * sender.scale;
sender.view.setTransform({ scale: scale });
sender.setScale(1);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.scale = scale;
}
}
});
var rotation = new JSRotationGestureRecognizer();
rotation.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var rotation = sender.view.rotation+sender.rotation;
sender.view.setTransform({ rotation: rotation });
sender.setRotation(0);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.rotation = rotation;
}
}
});
var swipe = new JSSwipeGestureRecognizer();
swipe.direction = JSSwipeGestureRecognizerDirectionRight | JSSwipeGestureRecognizerDirectionLeft;
swipe.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('Swiped!');
}
});
new JSGestureView('pan').addGestureRecognizer(pan);
new JSGestureView('tap').addGestureRecognizer(tap);
new JSGestureView('rotate').addGestureRecognizer(rotation);
new JSGestureView('pinch').addGestureRecognizer(pinch);
new JSGestureView('swipe').addGestureRecognizer(swipe);
new JSGestureView('press').addGestureRecognizer(press);
})();
// combo examples
(function() {
var tap = new JSTapGestureRecognizer();
tap.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('tapped : ' + sender.taps + ' time(s)');
}
});
var pan = new JSPanGestureRecognizer();
pan.maximumNumberOfTouches = 1;
pan.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var x = sender.view.x + sender.translation.x,
y = sender.view.y + sender.translation.y;
sender.view.setTransform({x: x, y: y });
sender.setTranslation({ x: 0, y: 0 });
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.x = x;
sender.view.y = y;
}
}
});
var pinch = new JSPinchGestureRecognizer();
pinch.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var scale = sender.view.scale * sender.scale;
sender.view.setTransform({ scale: scale });
sender.setScale(1);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.scale = scale;
}
}
});
var rotation = new JSRotationGestureRecognizer();
rotation.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var rotation = sender.view.rotation+sender.rotation;
sender.view.setTransform({ rotation: rotation });
sender.setRotation(0);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.rotation = rotation;
}
}
});
var target = new JSGestureView('combo-1');
target.addGestureRecognizer(tap);
target.addGestureRecognizer(pan);
target.addGestureRecognizer(pinch);
target.addGestureRecognizer(rotation);
})();
}
//]]>
</script>
<style type="text/css" media="screen">
body {
background: #efefef;
-webkit-text-size-adjust: none;
}
div.example {
float: left;
width: 430px;
min-height: 430px;
border: 1px solid #efefef;
padding: 10px 10px 0 10px;
margin: 10px;
background: #fff;
}
div.block {
margin-left: 140px;
width: 158px;
height: 158px;
background-repeat: no-repeat;
background-size: 100%;
z-index: 100;
}
div.example div.syntaxhighlighter {
font-size: 11px !important;
}
div#pan { background-image: url(images/block-pan.png); }
div#tap { background-image: url(images/block-tap.png); }
div#rotate { background-image: url(images/block-rotate.png); }
div#pinch { background-image: url(images/block-pinch.png); }
div#swipe { background-image: url(images/block-swipe.png); }
div#press { background-image: url(images/block-press.png); }
div#combo-1 { background-image: url(images/block-combo-1.png); }
section {
clear: both;
padding-bottom: 10px;
}
footer {
clear: both;
padding-top: 10px;
}
.clear { clear: both; }
.clearfix:after, .container:after {content:"\0020";display:block;height:0;clear:both;visibility:hidden;overflow:hidden;}
.clearfix, .container {display:block;}
</style>
</head>
<body>
<header>
<h1>JSGestureRecognizer</h1>
<p>UIGestureRecognizer JavaScript port for Mobile Safari</p>
<p>Author: <a href="http://mud.mitplw.com">Takashi Okamoto</a> / <a href="http://buzamoto.com/">BuzaMoto</a></p>
<p>Download: <a href="http://github.com/mud/jsgesturerecognizer">Available on Github</a></p>
</header>
<h2>Contents</h2>
<ul>
<li><a href="#about">About</a></li>
<li><a href="#how-to-use">How To Use</a></li>
<li><a href="#single-gesture">Single Gesture Examples</a></li>
<li><a href="#combo-gesture">Combination Gesture Examples</a></li>
</ul>
<section id="about">
<h2>About</h2>
<p>
<strong>JSGestureRecognizer</strong> allows you to add gesture recognizers to DOM objects for Mobile Safari similarly to <a href="http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html">UIGestureRecognizer</a> in iOS. I decided to write this because:
</p>
<blockquote>
1. I got sick of writing <tt>touchstart/touchmove/touchend</tt> events to support Mobile Safari<br />
2. I didn't see any implementation out there that I liked<br />
3. I like how <tt>UIGestureRecognizer</tt> works in iOS and wanted to handle gestures on Mobile Safari similarly<br />
4. I wanted to easily create my own gestures
</blockquote>
<p>
Just like <tt>UIGestureRecognizer</tt>, there are two steps to enable gestures on a DOM element.
</p>
<blockquote>
1. Create an instance of a gesture recognizer, set its properties and initialize with a callback function.<br />
2. Attach the gesture recognizer to a DOM element.
</blockquote>
</section>
<section id="how-to-use">
<h2>How to use</h2>
<p>
Of course the initention is to make this library independent, but this relies heavily on custom events and I didn't want to implement another event handling system. For now we require either Prototype.js or jQuery.
</p>
<p><a href="http://github.com/mud/jsgesturerecognizer">Download</a> the newest version of JSGestureRecognizer from Github, and include prototype.js or jquery.js and gesturerecognizer.js on your page:
<script type="syntaxhighlighter" class="brush: xml"><![CDATA[
<script src="lib/prototype.js" type="text/javascript"></script>
<!--<script src="lib/jquery.js" type="text/javascript"></script>-->
<script src="dist/gesturerecognizer.js" type="text/javascript"></script>
]]></script>
</p>
<p>
Create an instance of a JSGestureRecognizer, set up optional properties and initialize with a callback function. For example, to create a new Tap Gesture:
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
// create new instance of JSTapGestureRecognizer
var tap = new JSTapGestureRecognizer();
// specify the number of taps required to recognize
tap.numberOfTapsRequired = 1;
// set up the callback function
// the callback function will pass the instance of the recognizer
// as its argument (ie sender)
tap.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('tapped : ' + sender.taps + ' time(s)');
}
});
// add gesture to a DOM element
JSGestureRecognizer.addGestureRecognizer(document.getElementById('foo'), tap);
]]></script>
Every time you tap on foo, the gesture will be recognized.
</p>
<h3>Callback Function</h3>
<p>
In iOS, you specify a target and action for the gesture recognizer. With JSGestureRecognizer, we instead specify a callback function. The callback function is called anytime the JSGestureRecognizer dispatches an event, which strictly follows <a href="http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html%23//apple_ref/doc/uid/TP40009541-CH6-SW11">UIGestureRecognizer's action messages</a>. The callback function passes the recognizer instance as an argument, and to handle the callback, you should do what you do similarly to iOS's implementation.
</p>
<blockquote>
1) Check the state of the recognizer (ie. JSGestureRecognizerStateBegan / JSGestureRecognizerStateChanged / JSGestureRecognizerStateEnded).<br />
2) Handle updates according to the state of the recognizer. You mostly want to do stuff during JSGestureRecognizerStateChanged / JSGestureRecognizerStateEnded.
</blockquote>
<h3>Value Accumulation</h3>
<p>
I tried to make the behavior of <tt>JSGestureRecognizer</tt> as close to <rr>UIGestureRecognizer</rr> as possible. This means the scale/rotation/translation values accumulate, meaning that if you want to animate scale/rotation/transformation you will have to set the value to 0 (or in case of scale to 1) after every event, which will return delta values. The examples below all do that.
</p>
<h3>Rotation Value</h3>
<p>
Rotation value in <tt>JSRotationGestureRecognizer</tt> is given in degrees and NOT radians. Why? Because <tt>webkitTransformation</tt> accepts degrees and <tt>gesturechange</tt> event on Mobile Safari returns degrees. I thought the extra transformation into radians would just be unnecessary, apologies to all the purests out there.
</p>
<h3>View Wrapper</h3>
<p>
Most of the time, you probably want to manipulate the dom object's transformation (ie. position, scale, rotation.) In order to preserve the transformation over events, we need to keep internal states of the position, scale and rotation values. You can do this by using <tt>(set/get)Attribute</tt>, but JSGestureRecognizer also includes <tt>JSGestureView</tt> which will keep track of transformation properties for you. The JSGestureView object will be accessible as the <tt>view</tt> property of the gesture recognizer instance. All examples below use <tt>JSGestureView</tt>.
</p>
</section>
<section id="single-gesture">
<h2>Single Gesture Examples</h2>
<p><strong>Alert:</strong> For the examples to work properly, you need to visit this page using Mobile Safari.</p>
<div class="example">
<div id="tap" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var tap = new JSTapGestureRecognizer();
tap.numberOfTapsRequired = 1;
tap.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('tapped : ' + sender.taps + ' time(s)');
}
});
var tapTarget = new JSGestureView('tap');
tapTarget.addGestureRecognizer(tap);
]]></script>
</div>
<div class="example">
<div id="rotate" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var rotation = new JSRotationGestureRecognizer();
rotation.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var rotation = sender.view.rotation+sender.rotation;
sender.view.setTransform({ rotation: rotation });
sender.setRotation(0);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.rotation = rotation;
}
}
});
var rotateTarget = new JSGestureView('rotate');
rotateTarget.addGestureRecognizer(rotation);
]]></script>
</div>
<div class="example">
<div id="pinch" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var pinch = new JSPinchGestureRecognizer();
pinch.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var scale = sender.view.scale * sender.scale;
sender.view.setTransform({ scale: scale });
sender.setScale(1);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.scale = scale;
}
}
});
var pinchTarget = new JSGestureView('pinch');
pinchTarget.addGestureRecognizer(pinch);
]]></script>
</div>
<div class="example">
<div id="swipe" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var swipe = new JSSwipeGestureRecognizer();
swipe.direction = JSSwipeGestureRecognizerDirectionRight |
JSSwipeGestureRecognizerDirectionLeft;
swipe.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('Swiped!');
}
});
var swipeTarget = new JSGestureView('swipe');
swipeTarget.addGestureRecognizer(swipe);
]]></script>
</div>
<div class="example">
<div id="pan" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var pan = new JSPanGestureRecognizer();
pan.maximumNumberOfTouches = 1;
pan.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var x = sender.view.x + sender.translation.x,
y = sender.view.y + sender.translation.y;
sender.view.setTransform({x: x, y: y });
sender.setTranslation({ x: 0, y: 0 });
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.x = x;
sender.view.y = y;
}
}
});
var panTarget = new JSGestureView('pan');
panTarget.addGestureRecognizer(tap);
]]></script>
</div>
<div class="example">
<div id="press" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var press = new JSLongPressGestureRecognizer();
press.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('Long Pressed!')
}
});
// create tap target
var pressTarget = new JSGestureView('press');
// add the tap gesture to the target
pressTarget.addGestureRecognizer(press);
]]></script>
</div>
</section>
<section id="combo-gesture">
<h2>Combination Gesture Examples</h2>
<div class="example">
<div id="combo-1" class="block"></div>
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
var tap = new JSTapGestureRecognizer();
tap.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateRecognized) {
alert('tapped : ' + sender.taps + ' time(s)');
}
});
var pan = new JSPanGestureRecognizer();
pan.maximumNumberOfTouches = 1;
pan.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var x = sender.view.x + sender.translation.x,
y = sender.view.y + sender.translation.y;
sender.view.setTransform({x: x, y: y });
sender.setTranslation({ x: 0, y: 0 });
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.x = x;
sender.view.y = y;
}
}
});
var pinch = new JSPinchGestureRecognizer();
pinch.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var scale = sender.view.scale * sender.scale;
sender.view.setTransform({ scale: scale });
sender.setScale(1);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.scale = scale;
}
}
});
var rotation = new JSRotationGestureRecognizer();
rotation.initWithCallback(function(sender) {
if (sender.state == JSGestureRecognizerStateEnded ||
sender.state == JSGestureRecognizerStateChanged) {
var rotation = sender.view.rotation+sender.rotation;
sender.view.setTransform({ rotation: rotation });
sender.setRotation(0);
if (sender.state == JSGestureRecognizerStateEnded) {
sender.view.rotation = rotation;
}
}
});
var target = new JSGestureView('combo-1');
target.addGestureRecognizer(tap);
target.addGestureRecognizer(pan);
target.addGestureRecognizer(pinch);
target.addGestureRecognizer(rotation);
]]></script>
</div>
</section>
<footer>
© 2011 <a href="http://buzamoto.com">BuzaMoto</a>. Check out the source at <a href="http://github.com/mud/jsgesturerecognizer">github</a>.
</footer>
<script type="text/javascript">
SyntaxHighlighter.defaults['gutter'] = false;
SyntaxHighlighter.all();
</script>
</body>
</html>