-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
332 lines (281 loc) · 10.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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>DDMomentum Plugin Example Page</title>
<style type="text/css">
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#demo {
height: 400px;
width: 400px;
border: 1px solid black;
background-color: #8DD5E7;
cursor: move;
}
</style>
</head>
<h1>DDMomentum Plugin Example</h1>
<div id="demo">Drag Me</div>
<body>
<script src="http://yui.yahooapis.com/3.10.1/build/yui/yui-min.js"></script>
<script>
YUI.add("dd-plugin-momentum", function(Y){
/**
* Plugin for the DD utility that adds momentum to dragged elements.
* @class DDMomentumPlugin
*/
function DDMomentumPlugin(config) {
DDMomentumPlugin.superclass.constructor.apply(this, arguments);
}
DDMomentumPlugin.NAME = "DDMomentumPlugin";
DDMomentumPlugin.NS = "as";
DDMomentumPlugin.ATTRS = {};
Y.extend(DDMomentumPlugin, Y.Plugin.Base, {
initializer: function (config) {
// RequestAnimationFrame shim ala Paul Irish & MDN.
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (function () {
return window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback, element) {
window.setTimeout(callback, 1000 / 60);
};
}());
}
this.publish("end");
/* Instance attributes that do not need to fire 'change' events */
// max velocity to allow in px per animation frame
this._maxVelocity = config.maxVelocity || 3;
// min velocity to require or else end the slide in px per animation frame
// use typeof to avoid accidental match on 0
this._minVelocity = typeof config.minVelocity !== "undefined" ? config.minVelocity : 0.2;
// amount of drag to apply at each frame of animation,
// 1 == no drag, 0 == infinite drag
this._drag = typeof config.drag !== "undefined" ? config.drag : 0.92;
// time-ordered list of drag deltas
this._deltas = [];
// how many drag deltas to use to calculate mouse velocity at drag end
this._deltasToCount = config.deltasToCount || 5;
// the node to animate
this._node = config.host.get("dragNode");
// total pixel offset over the slide
this._totalDeltaX = 0;
this._totalDeltaY = 0;
// drag start position to calculate drag distance
this._dragStartXY = [];
// total offset from drag, excluding slide
this._totalDragOffset = [0, 0];
// duration of the last set of drag deltas
this._duration = 0;
// velocity of the mouse at release time
this._velocity = 0;
// flag to indicate delegate drag instead of normal drag
this._delegate = config.host.name === "delegate";
/* Event listener references for easy cleanup later */
this._listenDrag = null;
this._listenDragEnd = null;
this._listenDragStart = null;
this._attachListeners();
},
destructor: function () {
this._detachListeners();
},
/**
* Set up listeners on external elements.
* @method _attachListeners
*/
_attachListeners: function () {
this._listenDragStart = this.afterHostEvent("drag:start", this._handleDragStart, this);
this._listenDrag = this.afterHostEvent("drag:drag", this._handleDrag, this);
this._listenDragEnd = this.afterHostEvent("drag:end", this._handleDragEnd, this);
},
/**
* Clean up listeners we have on exterior elements.
* @method _detachListeners
*/
_detachListeners: function () {
Y.detach(this._listenDragStart);
Y.detach(this._listenDrag);
Y.detach(this._listenDragEnd);
},
/**
* Reset deltas and get the dragged node if using
* delegate drag.
* @method _handleDragStart
*/
_handleDragStart: function (e) {
this._velocityXY = [0, 0];
this._deltas = [];
this._totalDragOffset = [0, 0];
this._totalDeltaX = 0;
this._totalDeltaY = 0;
this._dragStartXY = [e.pageX, e.pageY];
// for delegate dragging _node will be undefined
// at instantiation. Set it here
if(this._delegate){
this._node = this.get("host").get("currentNode");
}
},
/**
* Update the array of drag deltas.
* @method _handleDrag
*/
_handleDrag: function (e) {
e.timeStamp = new Date().getTime();
// fill the array of deltas
if (this._deltas.length < this._deltasToCount) {
this._deltas.push(e);
} else {
// remove the first entry
this._deltas.shift();
// add a new last entry
this._deltas.push(e);
}
},
/**
* Check state and fire the animation if everything is okay.
* @method _handleDragEnd
*/
_handleDragEnd: function (e) {
var startVelocity = [],
dd = e.target;
this._totalDragOffset = [
(e.pageX - this._dragStartXY[0]), (e.pageY - this._dragStartXY[1])];
// bail if the drag was too short to get a good trajectory and velocity
if (this._deltas.length !== this._deltasToCount) {
return;
}
// bail if the user stopped moving the mouse more
// than 100MS prior to letting go
if ((new Date().getTime() - this._deltas[this._deltasToCount - 1].timeStamp) > 100) {
Y.log("[DDMomentumPlugin] end without momentum");
this.fire("end", {
"deltaX": this._totalDragOffset[0],
"deltaY": this._totalDragOffset[1]
});
return;
}
// everything looks okay, get the direction, speed and go
this._figureDeltaDuration();
this._figureVelocity();
this._runFrame({
"velocityXY": this._velocityXY,
"scope": this,
"timeStamp": new Date()
});
},
/**
* Computes the duration of the final set of deltas so we
* can accurately track mouse velocity when the user let go.
* @method _figureDeltaDuration
*/
_figureDeltaDuration: function () {
this._duration = this._deltas[(this._deltasToCount - 1)].timeStamp - this._deltas[0].timeStamp;
},
/**
* Computes the velocity of the mouse when the user let go.
* @method _figureVelocity
*/
_figureVelocity: function () {
var avgX = 0,
avgY = 0,
i;
for (i = 0; i < this._deltasToCount; i++) {
avgX += this._deltas[i].info.delta[0];
avgY += this._deltas[i].info.delta[1];
}
avgX = this._clampVelocity(avgX / this._duration);
avgY = this._clampVelocity(avgY / this._duration);
this._velocityXY = [avgX, avgY];
},
/**
* Modifies the current velocity by applying the drag
* delta to it.
* @method _applyDrag
*/
_applyDrag: function (currVelocityXY, drag) {
var toReturn = [
Math.abs(currVelocityXY[0] * drag),
Math.abs(currVelocityXY[1] * drag)];
if (currVelocityXY[0] < 0) {
toReturn[0] *= -1;
}
if (currVelocityXY[1] < 0) {
toReturn[1] *= -1;
}
return toReturn;
},
/**
* Clamps the velocity to be within the allowed range.
* @method _clampVelocity
*/
_clampVelocity: function (potentialVelocity) {
var clamped;
clamped = Math.abs(potentialVelocity);
clamped = Math.max(Math.min(clamped, this._maxVelocity), this._minVelocity);
if (potentialVelocity < 0) {
clamped = clamped * -1;
}
return clamped;
},
/**
* Move the sliding element one frames worth of animation and
* prepare values for the next frame.
* @method _runFrame
*/
_runFrame: function (config) {
var t = config.scope,
elapsed,
now = new Date(),
dx,
dy;
elapsed = now - config.timeStamp;
dx = config.velocityXY[0] * elapsed;
dy = config.velocityXY[1] * elapsed;
config.timeStamp = now;
if (Math.abs(config.velocityXY[0]) >= 0) {
t._totalDeltaX += Math.floor(dx);
t._node.setStyle("left", Math.floor((parseInt(t._node.getStyle("left"), 10) + dx)) + "px");
}
if (Math.abs(config.velocityXY[1]) >= 0) {
t._totalDeltaY += Math.floor(dy);
t._node.setStyle("top", Math.floor((parseInt(t._node.getStyle("top"), 10) + dy)) + "px");
}
// Halt check
if (Math.abs(config.velocityXY[0]) >= t._minVelocity || Math.abs(config.velocityXY[1]) >= t._minVelocity) {
config.velocityXY = t._applyDrag(config.velocityXY, t._drag);
window.requestAnimationFrame(function () {
t._runFrame(config);
});
} else {
Y.log("[DDMomentumPlugin] end momentum");
t.fire("end", {
"deltaX": t._totalDragOffset[0] + t._totalDeltaX,
"deltaY": t._totalDragOffset[1] + t._totalDeltaY
});
}
}
});
Y.DDMomentumPlugin = DDMomentumPlugin;
}, '0.0.1', {
requires: ['dd', 'plugin']
});
YUI().use("dd", "plugin", "dd-plugin-momentum", function(Y){
// Make the dd instance
var dd = new Y.DD.Drag({
node: '#demo'
});
// add momentum
dd.plug(Y.DDMomentumPlugin);
});
// Prevent iOS from moving the window around on drags
document.body.addEventListener('touchmove', function (ev) {
ev.preventDefault();
});
</script>
</body>
</html>