-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
DomEvent.js
123 lines (114 loc) · 4.31 KB
/
DomEvent.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
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
* http://juerglehni.com/ & https://puckey.studio/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name DomEvent
* @namespace
* @private
*/
var DomEvent = /** @lends DomEvent */{
add: function(el, events) {
// Do not fail if el is not defined, that way we can keep the code that
// should not fail in web-workers to a minimum.
if (el) {
for (var type in events) {
var func = events[type],
parts = type.split(/[\s,]+/g);
for (var i = 0, l = parts.length; i < l; i++) {
var name = parts[i];
// For touchstart/touchmove events on document, we need to
// explicitly declare that the event is not passive (can be
// prevented). Otherwise chrome browser would ignore
// `event.preventDefault()` calls and omit warnings.
// See #1501 and:
// https://www.chromestatus.com/features/5093566007214080
var options = (
el === document
&& (name === 'touchstart' || name === 'touchmove')
) ? { passive: false } : false;
el.addEventListener(name, func, options);
}
}
}
},
remove: function(el, events) {
// See DomEvent.add() for an explanation of this check:
if (el) {
for (var type in events) {
var func = events[type],
parts = type.split(/[\s,]+/g);
for (var i = 0, l = parts.length; i < l; i++)
el.removeEventListener(parts[i], func, false);
}
}
},
getPoint: function(event) {
var pos = event.targetTouches
? event.targetTouches.length
? event.targetTouches[0]
: event.changedTouches[0]
: event;
return new Point(
pos.pageX || pos.clientX + document.documentElement.scrollLeft,
pos.pageY || pos.clientY + document.documentElement.scrollTop
);
},
getTarget: function(event) {
return event.target || event.srcElement;
},
getRelatedTarget: function(event) {
return event.relatedTarget || event.toElement;
},
getOffset: function(event, target) {
// Remove target offsets from page coordinates
return DomEvent.getPoint(event).subtract(DomElement.getOffset(
target || DomEvent.getTarget(event)));
}
};
DomEvent.requestAnimationFrame = new function() {
var nativeRequest = DomElement.getPrefixed(window, 'requestAnimationFrame'),
requested = false,
callbacks = [],
timer;
function handleCallbacks() {
// Make a local references to the current callbacks array and set
// callbacks to a new empty array, so it can collect the functions for
// the new requests.
var functions = callbacks;
callbacks = [];
// Call the collected callback functions.
for (var i = 0, l = functions.length; i < l; i++)
functions[i]();
// Now see if the above calls have collected new callbacks. Keep
// requesting new frames as long as we have callbacks.
requested = nativeRequest && callbacks.length;
if (requested)
nativeRequest(handleCallbacks);
}
return function(callback) {
// Add to the list of callbacks to be called in the next animation
// frame.
callbacks.push(callback);
if (nativeRequest) {
// Handle animation natively. We only need to request the frame
// once for all collected callbacks.
if (!requested) {
nativeRequest(handleCallbacks);
requested = true;
}
} else if (!timer) {
// Install interval timer that checks all callbacks. This
// results in faster animations than repeatedly installing
// timeout timers.
timer = setInterval(handleCallbacks, 1000 / 60);
}
};
};