forked from stephband/jquery.event.move
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
440 lines (352 loc) · 14.7 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
---
title: jQuery.event.move
---
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="author" content="">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.5">
<title>jQuery.event.move</title>
<script>document.documentElement.className = 'js';</script>
<link rel="icon" type="image/png" href="images/favicon.png" />
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<link rel="stylesheet" type="text/css" href="http://stephen.band/bolt/package/css/bolt-0.9.8.css" />
<link rel="stylesheet" type="text/css" href="http://stephband.info/css/template.min.css" />
<link rel="stylesheet" type="text/css" href="http://stephband.info/css/template.theme.min.css" />
<link rel="stylesheet" type="text/css" href="http://stephband.github.com/css/site.layout.css" />
<link rel="stylesheet" type="text/css" href="http://stephband.github.com/css/docs.classes.css" />
<link rel="stylesheet" type="text/css" href="http://stephband.github.com/css/site.highlight.light.css" />
<link rel="stylesheet" type="text/css" href="css/demo.css" />
</head>
<body>
<header class="site_header" id="page_header">
<div class="site_wrap wrap">
<a class="logo_thumb thumb" href="http://stephband.info" title="stephband" rel="index">
<h1>stephband.info</h1>
</a>
</div>
</header>
<div class="site_wrap wrap">
<h1>jQuery.event.move</h1>
<div class="test_wrap wrap">
<div class="test">
<div class="resize"></div>
<div class="rotate"></div>
</div>
<div class="centre_mark"></div>
<div class="origin_mark"></div>
</div>
<h2>Project</h2>
<ul>
<li>Github: <a href="http://github.com/stephband/jquery.event.move">github.com/stephband/jquery.event.move</a></li>
<li>Issues: <a href="http://github.com/stephband/jquery.event.move/issues">github.com/stephband/jquery.event.move/issues</a></li>
</ul>
<p>*UPDATE 2.0.0*: `move` events are now compatible with jQuery 3.x. In addition, the
underlying implementation is rewritten using vanilla DOM (where before it
was jQuery special events only) – jQuery is no longer a requirement.</p>
<h2 id="what">Move events</h2>
<dl>
<dt>movestart</dt>
<dd>Fired following touchmove or mousemove, when the touch (or mouse) crosses a threshold distance from the position of the mousedown or touchstart.</dd>
<dt>move</dt>
<dd>Fired on every animation frame where a touchmove or mousemove has changed position.</dd>
<dt>moveend</dt>
<dd>Fired following mouseup or touchend, after the last move event, and in the case of touch events when the finger that started the move has been lifted.</dd>
</dl>
<p>Move event objects are augmented with the properties:</p>
<dl>
<dt>e.pageX, e.pageY</dt>
<dd>Current page coordinates of pointer.</dd>
<dt>e.startX, e.startY</dt>
<dd>Page coordinates the pointer had at movestart.</dd>
<dt>e.distX, e.distY</dt>
<dd>Distance the pointer has moved since movestart.</dd>
<dt>e.deltaX, e.deltaY</dt>
<dd>Distance the pointer has moved since last move event.</dd>
<dt>e.velocityX, e.velocityY</dt>
<dd>Velocity in pixels/ms, averaged over the last few events.</dd>
</dl>
<h2 id="how">How to use move events</h2>
<div class="grid-block block">
<div class="grid-1/2 block">
<h3 class="text-4">Native DOM</h3>
{% highlight js %}
var node = document.querySelector('.mydiv');
// A movestart event must be bound
// and explicitly enabled
node.addEventListener('movestart', function(e) {
e.enableMove();
});
node.addEventListener('move', function(e) {
// move .mydiv horizontally
this.style.left = (e.startX + e.distX) + 'px';
})
node.addEventListener('moveend', function() {
// move is complete!
});{% endhighlight %}
<p><code>.enableMove()</code> is a necessary performance optimisation
(that works around the DOM's lack of event inspection).</p>
</div
><div class="grid-1/2 block">
<h3 class="text-4">jQuery</h3>
{% highlight js %}
jQuery('.mydiv')
.on('move', function(e) {
// move .mydiv horizontally
jQuery(this).css({ left: e.startX + e.distX });
})
.on('moveend', function() {
// move is complete!
});{% endhighlight %}
<p>(jQuery's special event system does the work of enabling
move events so there is no need to explicitly bind to
<code>movestart</code>.)</p>
</div>
</div>
<h2 id="why1">Why not just use mouse or touch events?</h2>
<p>Well, yes, you can. But move events abstract away the details that need attention when writing this kind of interaction model with mouse and touch events:</p>
<ul>
<li>Supports mouse and touch devices, exposing the same event API for both</li>
<li>Throttles moves to animation frames, preventing unneccesary calls</li>
<li>Ignores the right mouse button and clicks with modifier keys</li>
<li>Prevents text selection while moving</li>
<!--li>Prevents scrolling of touch interfaces while moving</li-->
<li>Handles multiple touches</li>
<li>Deals with bug (Chrome, possibly all Android) where changedTouches includes touches that have not changed</li>
<li>Handles browser differences where touches in iOS are live objects, where in Android they are static</li>
<li>Does not bork interaction with form inputs inside moveable nodes</li>
<li>Suppresses drag and drop events</li>
</ul>
<p>Move events are intended as 'building blocks' for helping to build interactions.
They track individual fingers or a single mouse, and expose properties on their event objects that are useful for detecting gestures.</p>
<h2 id="why2">What about drag events?</h2>
<p>Move events are designed to compliment drag events, where the two have different meanings: drag events are for transferring data while move events are for making interactive interfaces.
That said, <code>movestart</code>, <code>move</code> and <code>moveend</code> events deliberately echo <code>dragstart</code>, <code>drag</code> and <code>dragend</code> events, with one main difference:
where the <code>drag</code> event fires continuously whether you have moved the pointer or not, the <code>move</code> event fires only after the pointer has been moved, and only on animation frames.</p>
<p>Where both a <code>dragstart</code> and any move event are bound to the same node, drag events are suppressed.</p>
<!--h2 id="where">Where is jquery.event.move being used?</h2>
<p>It's part of my front-end toolkit, <a href="http://stephband.info/bolt">Bolt</a>. It's also used for:</p>
<ul>
<li><a href="http://www.webdoc.com">webdoc.com</a></li>
<li><a href="http://stephband.info/jquery.event.swipe">jquery.event.swipe</a></li>
</ul>
<p>Do let me know at <a href="http://twitter.com/stephband">@stephband</a> if you use it in your project. Cheers!</p-->
</div>
<footer class="site_footer">
<div class="full_wrap wrap">
<div class="bio_col col">
<h3>Who made this?</h3>
<p>I did. I come from Scotland and I live in Lausanne, Switzerland. I make web things at <a href="http://cruncher.ch">Cruncher</a>. I miss Branston Pickle. When I'm not glued to a screen I play music and hike mountains.</p>
</div
><div class="repo_col col">
<h3>More code</h3>
<ul class="repo_index index">
<li><a href="http://stephband.info/bolt">Bolt</a></li>
<li><a href="http://stephband.info/jparallax/">jQuery.parallax</a></li>
<li><a href="http://stephband.info/jquery.event.tap/">jQuery.event.tap</a></li>
<li><a href="http://stephband.info/jquery.event.move/">jQuery.event.move</a></li>
<li><a href="http://stephband.info/jquery.event.swipe/">jQuery.event.swipe</a></li>
</ul>
</div
><div class="social_col col">
<h3>Find me on...</h3>
<ul class="social_index index">
<li><a href="http://twitter.com/stephband">twitter</a></li>
<li><a href="http://github.com/stephband">github</a></li>
<li><a href="http://plus.google.com/114566808430966059989/photos">google+</a></li>
<li><a href="http://www.linkedin.com/profile/view?id=27013870">linkedin</a></li>
</ul>
</div>
</div>
</footer>
<script src="js/jquery-3.1.1.js"></script>
<script src="js/jquery.event.move.js"></script>
<script>
function toDeg(n) { return n * 57.295779513; }
function toRad(n) { return n / 57.295779513; }
// FUNCTIONS
var pi = Math.PI,
pi2 = pi * 2;
// Converts cartesian [x, y] to polar [distance, angle] coordinates,
// downward, anti-clockwise, angle in radians.
function toPolar(cart) {
var x = cart[0],
y = cart[1];
// Detect quadrant and work out vector
if (y === 0) { return x === 0 ? [0, 0] : x > 0 ? [x, 0.5 * pi] : [-x, 1.5 * pi] ; }
if (y < 0) { return x === 0 ? [-y, pi] : [Math.sqrt(x*x + y*y), Math.atan(x/y) + pi] ; }
if (y > 0) { return x === 0 ? [y, 0] : [Math.sqrt(x*x + y*y), (x > 0) ? Math.atan(x/y) : pi2 + Math.atan(x/y)] ; }
}
// Converts [distance, angle] vector to cartesian [x, y] coordinates.
function toCartesian(vect) {
var d = vect[0],
a = vect[1];
// Work out cartesian coordinates
return [ Math.sin(a) * d, Math.cos(a) * d ];
}
// log event objects
function logEvent(e){ window.console && console.log(e.type, e); }
jQuery(document)
//.bind('mousedown mouseup', logEvent)
.ready(function(){
var start;
var box = jQuery('.test');
var resize = jQuery('.resize');
var rotate = jQuery('.rotate');
var centreMark = jQuery('.centre_mark');
var originMark = jQuery('.origin_mark');
// Test setup and teardown with multiple binds and unbinds...
box
//.on('movestart move moveend', logEvent)
//.off('movestart')
//.off('move')
//.off('moveend')
//.bind('movestart move moveend', logEvent)
.on('movestart', function(e){
// Only listen to one finger
if (e.targetTouches && e.targetTouches.length > 1) {
e.preventDefault();
return;
}
if (e.target == e.currentTarget) {
start = {
x: parseInt(box.css('left')),
y: parseInt(box.css('top'))
};
}
})
.on('move', function(e){
if (e.target == e.currentTarget) {
// Test object
box.css({
left: start.x + e.distX,
top: start.y + e.distY
});
// Guides
originMark.css({
left: start.x + e.distX,
top: start.y + e.distY
});
centreMark.css({
left: start.x + e.distX + box.width()/2,
top: start.y + e.distY + box.height()/2
});
}
});
var rotation = 0;
(function(){
var start, rotatedOrigin, distX, distY;
resize
.on('movestart', function(e){
var polarOrigin;
start = {
left: parseInt(box.css('left')),
top: parseInt(box.css('top')),
width: box.width(),
height: box.height()
};
})
.on('move', function(e){
var polarDelta = toPolar([e.distX, e.distY]),
normalisedDelta, originDelta, width, height;
// Comments are useless. I can't describe what's going
// on here without a pencil and paper. Sorry, but basically,
// we're undoing the rotate transform to work out where
// the rotated origin lies.
polarDelta[1] += toRad(rotation);
normalisedDelta = toCartesian(polarDelta);
width = start.width + normalisedDelta[0];
height = start.height + normalisedDelta[1];
width = width >= 0 ? width : 0;
height = height >= 0 ? height : 0;
originDelta = [
e.distX/2 - (width === 0 ? -start.width/2 : normalisedDelta[0]/2),
e.distY/2 - (height === 0 ? -start.height/2 : normalisedDelta[1]/2)
];
box.css({
left: start.left + originDelta[0],
top: start.top + originDelta[1],
width: width,
height: height
});
// Guides
originMark.css({
left: start.left + originDelta[0],
top: start.top + originDelta[1]
});
centreMark.css({
left: start.left + originDelta[0] + width/2,
top: start.top + originDelta[1] + height/2
})
});
})();
(function(){
var centre, startRotate, startAngle, positionParent, offset;
rotate
.on('movestart', function(e){
positionParent = box.parent();
offset = positionParent.offset();
centre = {
x: parseInt(box.css('left')) + box.width()/2,
y: parseInt(box.css('top')) + box.height()/2
};
startRotate = rotation;
startAngle = toPolar([e.pageX - offset.left - centre.x, e.pageY - offset.top - centre.y])[1];
// Guides
originMark.css({
left: box.css('left'),
top: box.css('top')
});
centreMark.css({
left: centre.x,
top: centre.y
});
})
.on('move', function(e){
var nowAngle = toPolar([e.pageX - offset.left - centre.x, e.pageY - offset.top - centre.y])[1],
deltaRotate = nowAngle - startAngle,
transform;
rotation = parseInt(startRotate - toDeg(deltaRotate));
transform = 'rotate(' + rotation + 'deg)';
box.css({
transform: transform,
WebkitTransform: transform,
MozTransform: transform
});
});
})();
// Test properties
(function(){
// Test for correct deltaX and deltaY values
var distX = 0, distY = 0, pageX = 0, pageY = 0, startX = 0, startY = 0;
box
.on('movestart', function(e) {
startX = e.startX;
startY = e.startY;
distX = e.distX;
distY = e.distY;
pageX = e.pageX;
pageY = e.pageY;
console.log('movestart e.startX:', e.startX, 'e.distX:', e.distX, 'e.pageX:', e.pageX);
console.log('movestart e.startY:', e.startY, 'e.distY:', e.distY, 'e.pageY:', e.pageY);
})
.on('move movend', function(e){
// All these statements should be true
console.assert(e.startX === startX, 'e.startX property not correct. Expected:', startX, 'Actual:', e.startX);
console.assert(e.startY === startY, 'e.startY property not correct. Expected:', startY, 'Actual:', e.startY);
console.assert(e.deltaX === e.distX - distX, 'e.deltaX property not correct. e.startX:', e.startX, 'e.distX:', e.distX, 'e.deltaX:', e.deltaX, 'e.pageX:', e.pageX);
console.assert(e.deltaY === e.distY - distY, 'e.deltaY property not correct. e.startY:', e.startY, 'e.distY:', e.distY, 'e.deltaY:', e.deltaY, 'e.pageY:', e.pageY);
console.assert(e.deltaX === e.pageX - pageX, 'e.pageX property not correct. e.startX:', e.startX, 'e.distX:', e.distX, 'e.deltaX:', e.deltaX, 'e.pageX:', e.pageX);
console.assert(e.deltaY === e.pageY - pageY, 'e.pageY property not correct. e.startY:', e.startY, 'e.distY:', e.distY, 'e.deltaY:', e.deltaY, 'e.pageY:', e.pageY);
distY = e.distY;
distX = e.distX;
pageX = e.pageX;
pageY = e.pageY;
});
})();
});
</script>
</body>
</html>