forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDomEvent.js
More file actions
147 lines (135 loc) · 3.7 KB
/
Copy pathDomEvent.js
File metadata and controls
147 lines (135 loc) · 3.7 KB
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
/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
* Copyright (c) 2011 - 2013, Juerg Lehni & Jonathan Puckey
* http://lehni.org/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name DomEvent
* @namespace
* @private
*/
var DomEvent = {
add: function(el, events) {
for (var type in events) {
var func = events[type];
if (el.addEventListener) {
el.addEventListener(type, func, false);
} else if (el.attachEvent) {
// Make a bound closure that calls on the right object and
// passes on the global event object as a parameter.
el.attachEvent('on' + type, func.bound = function() {
func.call(el, window.event);
});
}
}
},
remove: function(el, events) {
for (var type in events) {
var func = events[type];
if (el.removeEventListener) {
el.removeEventListener(type, func, false);
} else if (el.detachEvent) {
// Remove the bound closure instead of func itself
el.detachEvent('on' + type, func.bound);
}
}
},
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;
},
getOffset: function(event, target) {
// Remove target offsets from page coordinates
return DomEvent.getPoint(event).subtract(DomElement.getOffset(
target || DomEvent.getTarget(event)));
},
preventDefault: function(event) {
if (event.preventDefault) {
event.preventDefault();
} else {
// IE
event.returnValue = false;
}
},
stopPropagation: function(event) {
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
},
stop: function(event) {
DomEvent.stopPropagation(event);
DomEvent.preventDefault(event);
}
};
DomEvent.requestAnimationFrame = new function() {
var part = 'equestAnimationFrame',
request = window['r' + part] || window['webkitR' + part]
|| window['mozR' + part] || window['oR' + part]
|| window['msR' + part];
if (request) {
// Chrome shipped without the time arg in m10. We need to check if time
// is defined in callbacks, and if not, clear request again so we won't
// use the faulty method.
request(function(time) {
if (time == null)
request = null;
});
}
// So we need to fake it. Define helper functions first:
var callbacks = [],
focused = true,
timer;
DomEvent.add(window, {
focus: function() {
focused = true;
},
blur: function() {
focused = false;
}
});
return function(callback, element) {
// See if we can handle natively first
if (request)
return request(callback, element);
// If not, do the callback handling ourself:
callbacks.push([callback, element]);
// We're done if there's already a timer installed
if (timer)
return;
// Installs interval timer that checks all callbacks. This results
// in faster animations than repeatedly installing timout timers.
timer = setInterval(function() {
// Checks all installed callbacks for element visibility and
// execute if needed.
for (var i = callbacks.length - 1; i >= 0; i--) {
var entry = callbacks[i],
func = entry[0],
el = entry[1];
if (!el || (PaperScope.getAttribute(el, 'keepalive') == 'true'
|| focused) && DomElement.isInView(el)) {
// Handle callback and remove it from callbacks list.
callbacks.splice(i, 1);
func(Date.now());
}
}
}, 1000 / 60);
};
};