Debian Codegraph @

architecture/httpdlib/html/js/svg/jquerysvg/jquery.svganim.js

raw
1 /* http://keith-wood.name/svg.html
2 SVG attribute animations for jQuery v1.4.5.
3 Written by Keith Wood (kbwood{at}iinet.com.au) June 2008.
4 Dual licensed under the GPL (http://dev.jquery.com/browser/trunk/jquery/GPL-LICENSE.txt) and
5 MIT (http://dev.jquery.com/browser/trunk/jquery/MIT-LICENSE.txt) licenses.
6 Please attribute the author if you use it. */
7
8 (function($) { // Hide scope, no $ conflict
9
10 // Enable animation for all of these SVG numeric attributes -
11 // named as svg-* or svg* (with first character upper case)
12 $.each(['x', 'y', 'width', 'height', 'rx', 'ry', 'cx', 'cy', 'r', 'x1', 'y1', 'x2', 'y2',
13 'stroke-width', 'strokeWidth', 'opacity', 'fill-opacity', 'fillOpacity',
14 'stroke-opacity', 'strokeOpacity', 'stroke-dashoffset', 'strokeDashOffset',
15 'font-size', 'fontSize', 'font-weight', 'fontWeight',
16 'letter-spacing', 'letterSpacing', 'word-spacing', 'wordSpacing'],
17 function(i, attrName) {
18 var ccName = attrName.charAt(0).toUpperCase() + attrName.substr(1);
19 if ($.cssProps) {
20 $.cssProps['svg' + ccName] = $.cssProps['svg-' + attrName] = attrName;
21 }
22 $.fx.step['svg' + ccName] = $.fx.step['svg-' + attrName] = function(fx) {
23 var realAttrName = $.svg._attrNames[attrName] || attrName;
24 var attr = fx.elem.attributes.getNamedItem(realAttrName);
25 if (!fx.set) {
26 fx.start = (attr ? parseFloat(attr.nodeValue) : 0);
27 var offset = ($.fn.jquery >= '1.6' ? '' :
28 fx.options.curAnim['svg' + ccName] || fx.options.curAnim['svg-' + attrName]);
29 if (/^[+-]=/.exec(offset)) {
30 fx.end = fx.start + parseFloat(offset.replace(/=/, ''));
31 }
32 $(fx.elem).css(realAttrName, '');
33 fx.set = true;
34 }
35 var value = (fx.pos * (fx.end - fx.start) + fx.start) + (fx.unit == '%' ? '%' : '');
36 (attr ? attr.nodeValue = value : fx.elem.setAttribute(realAttrName, value));
37 };
38 }
39 );
40
41 // Enable animation for the SVG strokeDashArray attribute
42 $.fx.step['svgStrokeDashArray'] = $.fx.step['svg-strokeDashArray'] =
43 $.fx.step['svgStroke-dasharray'] = $.fx.step['svg-stroke-dasharray'] = function(fx) {
44 var attr = fx.elem.attributes.getNamedItem('stroke-dasharray');
45 if (!fx.set) {
46 fx.start = parseDashArray(attr ? attr.nodeValue : '');
47 var offset = ($.fn.jquery >= '1.6' ? fx.end :
48 fx.options.curAnim['svgStrokeDashArray'] || fx.options.curAnim['svg-strokeDashArray'] ||
49 fx.options.curAnim['svgStroke-dasharray'] || fx.options.curAnim['svg-stroke-dasharray']);
50 fx.end = parseDashArray(offset);
51 if (/^[+-]=/.exec(offset)) {
52 offset = offset.split(/[, ]+/);
53 if (offset.length % 2 == 1) { // Must have an even number
54 var len = offset.length;
55 for (var i = 0; i < len; i++) { // So repeat
56 offset.push(offset[i]);
57 }
58 }
59 for (var i = 0; i < offset.length; i++) {
60 if (/^[+-]=/.exec(offset[i])) {
61 fx.end[i] = fx.start[i] + parseFloat(offset[i].replace(/=/, ''));
62 }
63 }
64 }
65 fx.set = true;
66 }
67 var value = $.map(fx.start, function(n, i) {
68 return (fx.pos * (fx.end[i] - n) + n);
69 }).join(',');
70 (attr ? attr.nodeValue = value : fx.elem.setAttribute('stroke-dasharray', value));
71 };
72
73 /* Parse a strokeDashArray definition: dash, gap, ...
74 @param value (string) the definition
75 @return (number[2n]) the extracted values */
76 function parseDashArray(value) {
77 var dashArray = value.split(/[, ]+/);
78 for (var i = 0; i < dashArray.length; i++) {
79 dashArray[i] = parseFloat(dashArray[i]);
80 if (isNaN(dashArray[i])) {
81 dashArray[i] = 0;
82 }
83 }
84 if (dashArray.length % 2 == 1) { // Must have an even number
85 var len = dashArray.length;
86 for (var i = 0; i < len; i++) { // So repeat
87 dashArray.push(dashArray[i]);
88 }
89 }
90 return dashArray;
91 }
92
93 // Enable animation for the SVG viewBox attribute
94 $.fx.step['svgViewBox'] = $.fx.step['svg-viewBox'] = function(fx) {
95 var attr = fx.elem.attributes.getNamedItem('viewBox');
96 if (!fx.set) {
97 fx.start = parseViewBox(attr ? attr.nodeValue : '');
98 var offset = ($.fn.jquery >= '1.6' ? fx.end :
99 fx.options.curAnim['svgViewBox'] || fx.options.curAnim['svg-viewBox']);
100 fx.end = parseViewBox(offset);
101 if (/^[+-]=/.exec(offset)) {
102 offset = offset.split(/[, ]+/);
103 while (offset.length < 4) {
104 offset.push('0');
105 }
106 for (var i = 0; i < 4; i++) {
107 if (/^[+-]=/.exec(offset[i])) {
108 fx.end[i] = fx.start[i] + parseFloat(offset[i].replace(/=/, ''));
109 }
110 }
111 }
112 fx.set = true;
113 }
114 var value = $.map(fx.start, function(n, i) {
115 return (fx.pos * (fx.end[i] - n) + n);
116 }).join(' ');
117 (attr ? attr.nodeValue = value : fx.elem.setAttribute('viewBox', value));
118 };
119
120 /* Parse a viewBox definition: x, y, width, height.
121 @param value (string) the definition
122 @return (number[4]) the extracted values */
123 function parseViewBox(value) {
124 var viewBox = value.split(/[, ]+/);
125 for (var i = 0; i < viewBox.length; i++) {
126 viewBox[i] = parseFloat(viewBox[i]);
127 if (isNaN(viewBox[i])) {
128 viewBox[i] = 0;
129 }
130 }
131 while (viewBox.length < 4) {
132 viewBox.push(0);
133 }
134 return viewBox;
135 }
136
137 // Enable animation for the SVG transform attribute
138 $.fx.step['svgTransform'] = $.fx.step['svg-transform'] = function(fx) {
139 var attr = fx.elem.attributes.getNamedItem('transform');
140 if (!fx.set) {
141 fx.start = parseTransform(attr ? attr.nodeValue : '');
142 fx.end = parseTransform(fx.end, fx.start);
143 fx.set = true;
144 }
145 var transform = '';
146 for (var i = 0; i < fx.end.order.length; i++) {
147 switch (fx.end.order.charAt(i)) {
148 case 't':
149 transform += ' translate(' +
150 (fx.pos * (fx.end.translateX - fx.start.translateX) + fx.start.translateX) + ',' +
151 (fx.pos * (fx.end.translateY - fx.start.translateY) + fx.start.translateY) + ')';
152 break;
153 case 's':
154 transform += ' scale(' +
155 (fx.pos * (fx.end.scaleX - fx.start.scaleX) + fx.start.scaleX) + ',' +
156 (fx.pos * (fx.end.scaleY - fx.start.scaleY) + fx.start.scaleY) + ')';
157 break;
158 case 'r':
159 transform += ' rotate(' +
160 (fx.pos * (fx.end.rotateA - fx.start.rotateA) + fx.start.rotateA) + ',' +
161 (fx.pos * (fx.end.rotateX - fx.start.rotateX) + fx.start.rotateX) + ',' +
162 (fx.pos * (fx.end.rotateY - fx.start.rotateY) + fx.start.rotateY) + ')';
163 break;
164 case 'x':
165 transform += ' skewX(' +
166 (fx.pos * (fx.end.skewX - fx.start.skewX) + fx.start.skewX) + ')';
167 case 'y':
168 transform += ' skewY(' +
169 (fx.pos * (fx.end.skewY - fx.start.skewY) + fx.start.skewY) + ')';
170 break;
171 case 'm':
172 var matrix = '';
173 for (var j = 0; j < 6; j++) {
174 matrix += ',' + (fx.pos * (fx.end.matrix[j] - fx.start.matrix[j]) + fx.start.matrix[j]);
175 }
176 transform += ' matrix(' + matrix.substr(1) + ')';
177 break;
178 }
179 }
180 (attr ? attr.nodeValue = transform : fx.elem.setAttribute('transform', transform));
181 };
182
183 /* Decode a transform string and extract component values.
184 @param value (string) the transform string to parse
185 @param original (object) the settings from the original node
186 @return (object) the combined transformation attributes */
187 function parseTransform(value, original) {
188 value = value || '';
189 if (typeof value == 'object') {
190 value = value.nodeValue;
191 }
192 var transform = $.extend({translateX: 0, translateY: 0, scaleX: 0, scaleY: 0,
193 rotateA: 0, rotateX: 0, rotateY: 0, skewX: 0, skewY: 0,
194 matrix: [0, 0, 0, 0, 0, 0]}, original || {});
195 transform.order = '';
196 var pattern = /([a-zA-Z]+)\(\s*([+-]?[\d\.]+)\s*(?:[\s,]\s*([+-]?[\d\.]+)\s*(?:[\s,]\s*([+-]?[\d\.]+)\s*(?:[\s,]\s*([+-]?[\d\.]+)\s*[\s,]\s*([+-]?[\d\.]+)\s*[\s,]\s*([+-]?[\d\.]+)\s*)?)?)?\)/g;
197 var result = pattern.exec(value);
198 while (result) {
199 switch (result[1]) {
200 case 'translate':
201 transform.order += 't';
202 transform.translateX = parseFloat(result[2]);
203 transform.translateY = (result[3] ? parseFloat(result[3]) : 0);
204 break;
205 case 'scale':
206 transform.order += 's';
207 transform.scaleX = parseFloat(result[2]);
208 transform.scaleY = (result[3] ? parseFloat(result[3]) : transform.scaleX);
209 break;
210 case 'rotate':
211 transform.order += 'r';
212 transform.rotateA = parseFloat(result[2]);
213 transform.rotateX = (result[3] ? parseFloat(result[3]) : 0);
214 transform.rotateY = (result[4] ? parseFloat(result[4]) : 0);
215 break;
216 case 'skewX':
217 transform.order += 'x';
218 transform.skewX = parseFloat(result[2]);
219 break;
220 case 'skewY':
221 transform.order += 'y';
222 transform.skewY = parseFloat(result[2]);
223 break;
224 case 'matrix':
225 transform.order += 'm';
226 transform.matrix = [parseFloat(result[2]), parseFloat(result[3]),
227 parseFloat(result[4]), parseFloat(result[5]),
228 parseFloat(result[6]), parseFloat(result[7])];
229 break;
230 }
231 result = pattern.exec(value);
232 }
233 if (transform.order == 'm' && Math.abs(transform.matrix[0]) == Math.abs(transform.matrix[3]) &&
234 transform.matrix[1] != 0 && Math.abs(transform.matrix[1]) == Math.abs(transform.matrix[2])) {
235 // Simple rotate about origin and translate
236 var angle = Math.acos(transform.matrix[0]) * 180 / Math.PI;
237 angle = (transform.matrix[1] < 0 ? 360 - angle : angle);
238 transform.order = 'rt';
239 transform.rotateA = angle;
240 transform.rotateX = transform.rotateY = 0;
241 transform.translateX = transform.matrix[4];
242 transform.translateY = transform.matrix[5];
243 }
244 return transform;
245 }
246
247 // Enable animation for all of these SVG colour properties - based on jquery.color.js
248 $.each(['fill', 'stroke'],
249 function(i, attrName) {
250 var ccName = attrName.charAt(0).toUpperCase() + attrName.substr(1);
251 $.fx.step['svg' + ccName] = $.fx.step['svg-' + attrName] = function(fx) {
252 if (!fx.set) {
253 fx.start = $.svg._getColour(fx.elem, attrName);
254 var toNone = (fx.end == 'none');
255 fx.end = (toNone ? $.svg._getColour(fx.elem.parentNode, attrName) : $.svg._getRGB(fx.end));
256 fx.end[3] = toNone;
257 $(fx.elem).css(attrName, '');
258 fx.set = true;
259 }
260 var attr = fx.elem.attributes.getNamedItem(attrName);
261 var colour = 'rgb(' + [
262 Math.min(Math.max(parseInt((fx.pos * (fx.end[0] - fx.start[0])) + fx.start[0], 10), 0), 255),
263 Math.min(Math.max(parseInt((fx.pos * (fx.end[1] - fx.start[1])) + fx.start[1], 10), 0), 255),
264 Math.min(Math.max(parseInt((fx.pos * (fx.end[2] - fx.start[2])) + fx.start[2], 10), 0), 255)
265 ].join(',') + ')';
266 colour = (fx.end[3] && fx.state == 1 ? 'none' : colour);
267 (attr ? attr.nodeValue = colour : fx.elem.setAttribute(attrName, colour));
268 }
269 }
270 );
271
272 /* Find this attribute value somewhere up the node hierarchy.
273 @param elem (element) the starting element to find the attribute
274 @param attr (string) the attribute name
275 @return (number[3]) RGB components for the attribute colour */
276 $.svg._getColour = function(elem, attr) {
277 elem = $(elem);
278 var colour;
279 do {
280 colour = elem.attr(attr) || elem.css(attr);
281 // Keep going until we find an element that has colour, or exit SVG
282 if ((colour != '' && colour != 'none') || elem.hasClass($.svg.markerClassName)) {
283 break;
284 }
285 } while (elem = elem.parent());
286 return $.svg._getRGB(colour);
287 };
288
289 /* Parse strings looking for common colour formats.
290 @param colour (string) colour description to parse
291 @return (number[3]) RGB components of this colour */
292 $.svg._getRGB = function(colour) {
293 var result;
294 // Check if we're already dealing with an array of colors
295 if (colour && colour.constructor == Array) {
296 return (colour.length == 3 || colour.length == 4 ? colour : colours['none']);
297 }
298 // Look for rgb(num,num,num)
299 if (result = /^rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)$/.exec(colour)) {
300 return [parseInt(result[1], 10), parseInt(result[2], 10), parseInt(result[3], 10)];
301 }
302 // Look for rgb(num%,num%,num%)
303 if (result = /^rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)$/.exec(colour)) {
304 return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55,
305 parseFloat(result[3]) * 2.55];
306 }
307 // Look for #a0b1c2
308 if (result = /^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/.exec(colour)) {
309 return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];
310 }
311 // Look for #abc
312 if (result = /^#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])$/.exec(colour)) {
313 return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16),
314 parseInt(result[3] + result[3], 16)];
315 }
316 // Otherwise, we're most likely dealing with a named color
317 return colours[$.trim(colour).toLowerCase()] || colours['none'];
318 };
319
320 // The SVG named colours
321 var colours = {
322 '': [255, 255, 255, 1],
323 none: [255, 255, 255, 1],
324 aliceblue: [240, 248, 255],
325 antiquewhite: [250, 235, 215],
326 aqua: [0, 255, 255],
327 aquamarine: [127, 255, 212],
328 azure: [240, 255, 255],
329 beige: [245, 245, 220],
330 bisque: [255, 228, 196],
331 black: [0, 0, 0],
332 blanchedalmond: [255, 235, 205],
333 blue: [0, 0, 255],
334 blueviolet: [138, 43, 226],
335 brown: [165, 42, 42],
336 burlywood: [222, 184, 135],
337 cadetblue: [95, 158, 160],
338 chartreuse: [127, 255, 0],
339 chocolate: [210, 105, 30],
340 coral: [255, 127, 80],
341 cornflowerblue: [100, 149, 237],
342 cornsilk: [255, 248, 220],
343 crimson: [220, 20, 60],
344 cyan: [0, 255, 255],
345 darkblue: [0, 0, 139],
346 darkcyan: [0, 139, 139],
347 darkgoldenrod: [184, 134, 11],
348 darkgray: [169, 169, 169],
349 darkgreen: [0, 100, 0],
350 darkgrey: [169, 169, 169],
351 darkkhaki: [189, 183, 107],
352 darkmagenta: [139, 0, 139],
353 darkolivegreen: [85, 107, 47],
354 darkorange: [255, 140, 0],
355 darkorchid: [153, 50, 204],
356 darkred: [139, 0, 0],
357 darksalmon: [233, 150, 122],
358 darkseagreen: [143, 188, 143],
359 darkslateblue: [72, 61, 139],
360 darkslategray: [47, 79, 79],
361 darkslategrey: [47, 79, 79],
362 darkturquoise: [0, 206, 209],
363 darkviolet: [148, 0, 211],
364 deeppink: [255, 20, 147],
365 deepskyblue: [0, 191, 255],
366 dimgray: [105, 105, 105],
367 dimgrey: [105, 105, 105],
368 dodgerblue: [30, 144, 255],
369 firebrick: [178, 34, 34],
370 floralwhite: [255, 250, 240],
371 forestgreen: [34, 139, 34],
372 fuchsia: [255, 0, 255],
373 gainsboro: [220, 220, 220],
374 ghostwhite: [248, 248, 255],
375 gold: [255, 215, 0],
376 goldenrod: [218, 165, 32],
377 gray: [128, 128, 128],
378 grey: [128, 128, 128],
379 green: [0, 128, 0],
380 greenyellow: [173, 255, 47],
381 honeydew: [240, 255, 240],
382 hotpink: [255, 105, 180],
383 indianred: [205, 92, 92],
384 indigo: [75, 0, 130],
385 ivory: [255, 255, 240],
386 khaki: [240, 230, 140],
387 lavender: [230, 230, 250],
388 lavenderblush: [255, 240, 245],
389 lawngreen: [124, 252, 0],
390 lemonchiffon: [255, 250, 205],
391 lightblue: [173, 216, 230],
392 lightcoral: [240, 128, 128],
393 lightcyan: [224, 255, 255],
394 lightgoldenrodyellow: [250, 250, 210],
395 lightgray: [211, 211, 211],
396 lightgreen: [144, 238, 144],
397 lightgrey: [211, 211, 211],
398 lightpink: [255, 182, 193],
399 lightsalmon: [255, 160, 122],
400 lightseagreen: [32, 178, 170],
401 lightskyblue: [135, 206, 250],
402 lightslategray: [119, 136, 153],
403 lightslategrey: [119, 136, 153],
404 lightsteelblue: [176, 196, 222],
405 lightyellow: [255, 255, 224],
406 lime: [0, 255, 0],
407 limegreen: [50, 205, 50],
408 linen: [250, 240, 230],
409 magenta: [255, 0, 255],
410 maroon: [128, 0, 0],
411 mediumaquamarine: [102, 205, 170],
412 mediumblue: [0, 0, 205],
413 mediumorchid: [186, 85, 211],
414 mediumpurple: [147, 112, 219],
415 mediumseagreen: [60, 179, 113],
416 mediumslateblue: [123, 104, 238],
417 mediumspringgreen: [0, 250, 154],
418 mediumturquoise: [72, 209, 204],
419 mediumvioletred: [199, 21, 133],
420 midnightblue: [25, 25, 112],
421 mintcream: [245, 255, 250],
422 mistyrose: [255, 228, 225],
423 moccasin: [255, 228, 181],
424 navajowhite: [255, 222, 173],
425 navy: [0, 0, 128],
426 oldlace: [253, 245, 230],
427 olive: [128, 128, 0],
428 olivedrab: [107, 142, 35],
429 orange: [255, 165, 0],
430 orangered: [255, 69, 0],
431 orchid: [218, 112, 214],
432 palegoldenrod: [238, 232, 170],
433 palegreen: [152, 251, 152],
434 paleturquoise: [175, 238, 238],
435 palevioletred: [219, 112, 147],
436 papayawhip: [255, 239, 213],
437 peachpuff: [255, 218, 185],
438 peru: [205, 133, 63],
439 pink: [255, 192, 203],
440 plum: [221, 160, 221],
441 powderblue: [176, 224, 230],
442 purple: [128, 0, 128],
443 red: [255, 0, 0],
444 rosybrown: [188, 143, 143],
445 royalblue: [65, 105, 225],
446 saddlebrown: [139, 69, 19],
447 salmon: [250, 128, 114],
448 sandybrown: [244, 164, 96],
449 seagreen: [46, 139, 87],
450 seashell: [255, 245, 238],
451 sienna: [160, 82, 45],
452 silver: [192, 192, 192],
453 skyblue: [135, 206, 235],
454 slateblue: [106, 90, 205],
455 slategray: [112, 128, 144],
456 slategrey: [112, 128, 144],
457 snow: [255, 250, 250],
458 springgreen: [0, 255, 127],
459 steelblue: [70, 130, 180],
460 tan: [210, 180, 140],
461 teal: [0, 128, 128],
462 thistle: [216, 191, 216],
463 tomato: [255, 99, 71],
464 turquoise: [64, 224, 208],
465 violet: [238, 130, 238],
466 wheat: [245, 222, 179],
467 white: [255, 255, 255],
468 whitesmoke: [245, 245, 245],
469 yellow: [255, 255, 0],
470 yellowgreen: [154, 205, 50]
471 };
472
473 })(jQuery);