-
Notifications
You must be signed in to change notification settings - Fork 84
/
common.js
245 lines (224 loc) · 6.71 KB
/
common.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
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
// -----------------------------------------
//
// common.js
// Common functions used within PaintbrushJS
//
// -----------------------------------------
//
// These are adapted from code examples written by other people,
// and are excluded from the PaintbrushJS license.
//
// Please see respective URLs for usage and licensing restrictions.
//
// addLoadEvent function, to attach events to page load
// by Simon Willison
// http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
// getElementsByClassName
// Developed by Robert Nyman, http://www.robertnyman.com
// Code/licensing: http://code.google.com/p/getelementsbyclassname/
// Documentation: http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
var getElementsByClassName = function (className, tag, elm){
if (document.getElementsByClassName) {
getElementsByClassName = function (className, tag, elm) {
elm = elm || document;
var elements = elm.getElementsByClassName(className),
nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
returnElements = [],
current;
for(var i=0, il=elements.length; i<il; i+=1){
current = elements[i];
if(!nodeName || nodeName.test(current.nodeName)) {
returnElements.push(current);
}
}
return returnElements;
};
}
else if (document.evaluate) {
getElementsByClassName = function (className, tag, elm) {
tag = tag || "*";
elm = elm || document;
var classes = className.split(" "),
classesToCheck = "",
xhtmlNamespace = "http://www.w3.org/1999/xhtml",
namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
returnElements = [],
elements,
node;
for(var j=0, jl=classes.length; j<jl; j+=1){
classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
}
try {
elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
}
catch (e) {
elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
}
while ((node = elements.iterateNext())) {
returnElements.push(node);
}
return returnElements;
};
}
else {
getElementsByClassName = function (className, tag, elm) {
tag = tag || "*";
elm = elm || document;
var classes = className.split(" "),
classesToCheck = [],
elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
current,
returnElements = [],
match;
for(var k=0, kl=classes.length; k<kl; k+=1){
classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
}
for(var l=0, ll=elements.length; l<ll; l+=1){
current = elements[l];
match = false;
for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
match = classesToCheck[m].test(current.className);
if (!match) {
break;
}
}
if (match) {
returnElements.push(current);
}
}
return returnElements;
};
}
return getElementsByClassName(className, tag, elm);
};
// simple check to see whether canvas is supported in this browser
// copied and pasted from http://diveintohtml5.org/detect.html#canvas
function supports_canvas() {
return !!document.createElement('canvas').getContext;
}
// calculate gaussian blur
// adapted from http://pvnick.blogspot.com/2010/01/im-currently-porting-image-segmentation.html
function gaussianBlur(img, pixels, amount) {
var width = img.width;
var width4 = width << 2;
var height = img.height;
if (pixels) {
var data = pixels.data;
// compute coefficients as a function of amount
var q;
if (amount < 0.0) {
amount = 0.0;
}
if (amount >= 2.5) {
q = 0.98711 * amount - 0.96330;
} else if (amount >= 0.5) {
q = 3.97156 - 4.14554 * Math.sqrt(1.0 - 0.26891 * amount);
} else {
q = 2 * amount * (3.97156 - 4.14554 * Math.sqrt(1.0 - 0.26891 * 0.5));
}
//compute b0, b1, b2, and b3
var qq = q * q;
var qqq = qq * q;
var b0 = 1.57825 + (2.44413 * q) + (1.4281 * qq ) + (0.422205 * qqq);
var b1 = ((2.44413 * q) + (2.85619 * qq) + (1.26661 * qqq)) / b0;
var b2 = (-((1.4281 * qq) + (1.26661 * qqq))) / b0;
var b3 = (0.422205 * qqq) / b0;
var bigB = 1.0 - (b1 + b2 + b3);
// horizontal
for (var c = 0; c < 3; c++) {
for (var y = 0; y < height; y++) {
// forward
var index = y * width4 + c;
var indexLast = y * width4 + ((width - 1) << 2) + c;
var pixel = data[index];
var ppixel = pixel;
var pppixel = ppixel;
var ppppixel = pppixel;
for (; index <= indexLast; index += 4) {
pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
data[index] = pixel;
ppppixel = pppixel;
pppixel = ppixel;
ppixel = pixel;
}
// backward
index = y * width4 + ((width - 1) << 2) + c;
indexLast = y * width4 + c;
pixel = data[index];
ppixel = pixel;
pppixel = ppixel;
ppppixel = pppixel;
for (; index >= indexLast; index -= 4) {
pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
data[index] = pixel;
ppppixel = pppixel;
pppixel = ppixel;
ppixel = pixel;
}
}
}
// vertical
for (var c = 0; c < 3; c++) {
for (var x = 0; x < width; x++) {
// forward
var index = (x << 2) + c;
var indexLast = (height - 1) * width4 + (x << 2) + c;
var pixel = data[index];
var ppixel = pixel;
var pppixel = ppixel;
var ppppixel = pppixel;
for (; index <= indexLast; index += width4) {
pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
data[index] = pixel;
ppppixel = pppixel;
pppixel = ppixel;
ppixel = pixel;
}
// backward
index = (height - 1) * width4 + (x << 2) + c;
indexLast = (x << 2) + c;
pixel = data[index];
ppixel = pixel;
pppixel = ppixel;
ppppixel = pppixel;
for (; index >= indexLast; index -= width4) {
pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
data[index] = pixel;
ppppixel = pppixel;
pppixel = ppixel;
ppixel = pixel;
}
}
}
return(pixels);
}
}
// remove a specific class from an element
// from http://www.openjs.com/scripts/dom/class_manipulation.php
function removeClass(obj, cls) {
if (hasClass(obj, cls)) {
var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
obj.className = obj.className.replace(reg, ' ');
}
}
function addClass(obj, cls) {
if (!this.hasClass(obj, cls)) {
obj.className += " " + cls;
}
}
function hasClass(obj, cls) {
return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
}