comparison books/raphael.js @ 6:077b0a0a3e6d

remaining originals according to dependency walk
author Robert Boland <robert@markup.co.uk>
date Thu, 16 Feb 2017 22:29:02 +0000
parents
children
comparison
equal deleted inserted replaced
5:55445b456ad0 6:077b0a0a3e6d
1 /*!
2 * Raphael 1.5.2 - JavaScript Vector Library
3 *
4 * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://raphaeljs.com/license.html) license.
6 */
7 (function () {
8 function R() {
9 if (R.is(arguments[0], array)) {
10 var a = arguments[0],
11 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
12 res = cnv.set();
13 for (var i = 0, ii = a[length]; i < ii; i++) {
14 var j = a[i] || {};
15 elements[has](j.type) && res[push](cnv[j.type]().attr(j));
16 }
17 return res;
18 }
19 return create[apply](R, arguments);
20 }
21 R.version = "1.5.2";
22 var separator = /[, ]+/,
23 elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
24 formatrg = /\{(\d+)\}/g,
25 proto = "prototype",
26 has = "hasOwnProperty",
27 doc = document,
28 win = window,
29 oldRaphael = {
30 was: Object[proto][has].call(win, "Raphael"),
31 is: win.Raphael
32 },
33 Paper = function () {
34 this.customAttributes = {};
35 },
36 paperproto,
37 appendChild = "appendChild",
38 apply = "apply",
39 concat = "concat",
40 supportsTouch = "createTouch" in doc,
41 E = "",
42 S = " ",
43 Str = String,
44 split = "split",
45 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
46 touchMap = {
47 mousedown: "touchstart",
48 mousemove: "touchmove",
49 mouseup: "touchend"
50 },
51 join = "join",
52 length = "length",
53 lowerCase = Str[proto].toLowerCase,
54 math = Math,
55 mmax = math.max,
56 mmin = math.min,
57 abs = math.abs,
58 pow = math.pow,
59 PI = math.PI,
60 nu = "number",
61 string = "string",
62 array = "array",
63 toString = "toString",
64 fillString = "fill",
65 objectToString = Object[proto][toString],
66 paper = {},
67 push = "push",
68 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
69 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+%?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsba?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsla?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\))\s*$/i,
70 isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
71 bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
72 round = math.round,
73 setAttribute = "setAttribute",
74 toFloat = parseFloat,
75 toInt = parseInt,
76 ms = " progid:DXImageTransform.Microsoft",
77 upperCase = Str[proto].toUpperCase,
78 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
79 availableAnimAttrs = {along: "along", blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
80 rp = "replace",
81 animKeyFrames= /^(from|to|\d+%?)$/,
82 commaSpaces = /\s*,\s*/,
83 hsrg = {hs: 1, rg: 1},
84 p2s = /,?([achlmqrstvxz]),?/gi,
85 pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
86 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
87 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/,
88 sortByKey = function (a, b) {
89 return a.key - b.key;
90 };
91
92 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
93 if (R.type == "VML") {
94 var d = doc.createElement("div"),
95 b;
96 d.innerHTML = '<v:shape adj="1"/>';
97 b = d.firstChild;
98 b.style.behavior = "url(#default#VML)";
99 if (!(b && typeof b.adj == "object")) {
100 return R.type = null;
101 }
102 d = null;
103 }
104 R.svg = !(R.vml = R.type == "VML");
105 Paper[proto] = R[proto];
106 paperproto = Paper[proto];
107 R._id = 0;
108 R._oid = 0;
109 R.fn = {};
110 R.is = function (o, type) {
111 type = lowerCase.call(type);
112 if (type == "finite") {
113 return !isnan[has](+o);
114 }
115 return (type == "null" && o === null) ||
116 (type == typeof o) ||
117 (type == "object" && o === Object(o)) ||
118 (type == "array" && Array.isArray && Array.isArray(o)) ||
119 objectToString.call(o).slice(8, -1).toLowerCase() == type;
120 };
121 R.angle = function (x1, y1, x2, y2, x3, y3) {
122 if (x3 == null) {
123 var x = x1 - x2,
124 y = y1 - y2;
125 if (!x && !y) {
126 return 0;
127 }
128 return ((x < 0) * 180 + math.atan(-y / -x) * 180 / PI + 360) % 360;
129 } else {
130 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
131 }
132 };
133 R.rad = function (deg) {
134 return deg % 360 * PI / 180;
135 };
136 R.deg = function (rad) {
137 return rad * 180 / PI % 360;
138 };
139 R.snapTo = function (values, value, tolerance) {
140 tolerance = R.is(tolerance, "finite") ? tolerance : 10;
141 if (R.is(values, array)) {
142 var i = values.length;
143 while (i--) if (abs(values[i] - value) <= tolerance) {
144 return values[i];
145 }
146 } else {
147 values = +values;
148 var rem = value % values;
149 if (rem < tolerance) {
150 return value - rem;
151 }
152 if (rem > values - tolerance) {
153 return value - rem + values;
154 }
155 }
156 return value;
157 };
158 function createUUID() {
159 // http://www.ietf.org/rfc/rfc4122.txt
160 var s = [],
161 i = 0;
162 for (; i < 32; i++) {
163 s[i] = (~~(math.random() * 16))[toString](16);
164 }
165 s[12] = 4; // bits 12-15 of the time_hi_and_version field to 0010
166 s[16] = ((s[16] & 3) | 8)[toString](16); // bits 6-7 of the clock_seq_hi_and_reserved to 01
167 return "r-" + s[join]("");
168 }
169
170 R.setWindow = function (newwin) {
171 win = newwin;
172 doc = win.document;
173 };
174 // colour utilities
175 var toHex = function (color) {
176 if (R.vml) {
177 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
178 var trim = /^\s+|\s+$/g;
179 var bod;
180 try {
181 var docum = new ActiveXObject("htmlfile");
182 docum.write("<body>");
183 docum.close();
184 bod = docum.body;
185 } catch(e) {
186 bod = createPopup().document.body;
187 }
188 var range = bod.createTextRange();
189 toHex = cacher(function (color) {
190 try {
191 bod.style.color = Str(color)[rp](trim, E);
192 var value = range.queryCommandValue("ForeColor");
193 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
194 return "#" + ("000000" + value[toString](16)).slice(-6);
195 } catch(e) {
196 return "none";
197 }
198 });
199 } else {
200 var i = doc.createElement("i");
201 i.title = "Rapha\xebl Colour Picker";
202 i.style.display = "none";
203 doc.body[appendChild](i);
204 toHex = cacher(function (color) {
205 i.style.color = color;
206 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
207 });
208 }
209 return toHex(color);
210 },
211 hsbtoString = function () {
212 return "hsb(" + [this.h, this.s, this.b] + ")";
213 },
214 hsltoString = function () {
215 return "hsl(" + [this.h, this.s, this.l] + ")";
216 },
217 rgbtoString = function () {
218 return this.hex;
219 };
220 R.hsb2rgb = function (h, s, b, o) {
221 if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
222 b = h.b;
223 s = h.s;
224 h = h.h;
225 o = h.o;
226 }
227 return R.hsl2rgb(h, s, b / 2, o);
228 };
229 R.hsl2rgb = function (h, s, l, o) {
230 if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
231 l = h.l;
232 s = h.s;
233 h = h.h;
234 }
235 if (h > 1 || s > 1 || l > 1) {
236 h /= 360;
237 s /= 100;
238 l /= 100;
239 }
240 var rgb = {},
241 channels = ["r", "g", "b"],
242 t2, t1, t3, r, g, b;
243 if (!s) {
244 rgb = {
245 r: l,
246 g: l,
247 b: l
248 };
249 } else {
250 if (l < .5) {
251 t2 = l * (1 + s);
252 } else {
253 t2 = l + s - l * s;
254 }
255 t1 = 2 * l - t2;
256 for (var i = 0; i < 3; i++) {
257 t3 = h + 1 / 3 * -(i - 1);
258 t3 < 0 && t3++;
259 t3 > 1 && t3--;
260 if (t3 * 6 < 1) {
261 rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
262 } else if (t3 * 2 < 1) {
263 rgb[channels[i]] = t2;
264 } else if (t3 * 3 < 2) {
265 rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
266 } else {
267 rgb[channels[i]] = t1;
268 }
269 }
270 }
271 rgb.r *= 255;
272 rgb.g *= 255;
273 rgb.b *= 255;
274 rgb.hex = "#" + (16777216 | rgb.b | (rgb.g << 8) | (rgb.r << 16)).toString(16).slice(1);
275 R.is(o, "finite") && (rgb.opacity = o);
276 rgb.toString = rgbtoString;
277 return rgb;
278 };
279 R.rgb2hsb = function (red, green, blue) {
280 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
281 blue = red.b;
282 green = red.g;
283 red = red.r;
284 }
285 if (green == null && R.is(red, string)) {
286 var clr = R.getRGB(red);
287 red = clr.r;
288 green = clr.g;
289 blue = clr.b;
290 }
291 if (red > 1 || green > 1 || blue > 1) {
292 red /= 255;
293 green /= 255;
294 blue /= 255;
295 }
296 var max = mmax(red, green, blue),
297 min = mmin(red, green, blue),
298 hue,
299 saturation,
300 brightness = max;
301 if (min == max) {
302 return {h: 0, s: 0, b: max, toString: hsbtoString};
303 } else {
304 var delta = (max - min);
305 saturation = delta / max;
306 if (red == max) {
307 hue = (green - blue) / delta;
308 } else if (green == max) {
309 hue = 2 + ((blue - red) / delta);
310 } else {
311 hue = 4 + ((red - green) / delta);
312 }
313 hue /= 6;
314 hue < 0 && hue++;
315 hue > 1 && hue--;
316 }
317 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
318 };
319 R.rgb2hsl = function (red, green, blue) {
320 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
321 blue = red.b;
322 green = red.g;
323 red = red.r;
324 }
325 if (green == null && R.is(red, string)) {
326 var clr = R.getRGB(red);
327 red = clr.r;
328 green = clr.g;
329 blue = clr.b;
330 }
331 if (red > 1 || green > 1 || blue > 1) {
332 red /= 255;
333 green /= 255;
334 blue /= 255;
335 }
336 var max = mmax(red, green, blue),
337 min = mmin(red, green, blue),
338 h,
339 s,
340 l = (max + min) / 2,
341 hsl;
342 if (min == max) {
343 hsl = {h: 0, s: 0, l: l};
344 } else {
345 var delta = max - min;
346 s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
347 if (red == max) {
348 h = (green - blue) / delta;
349 } else if (green == max) {
350 h = 2 + (blue - red) / delta;
351 } else {
352 h = 4 + (red - green) / delta;
353 }
354 h /= 6;
355 h < 0 && h++;
356 h > 1 && h--;
357 hsl = {h: h, s: s, l: l};
358 }
359 hsl.toString = hsltoString;
360 return hsl;
361 };
362 R._path2string = function () {
363 return this.join(",")[rp](p2s, "$1");
364 };
365 function cacher(f, scope, postprocessor) {
366 function newf() {
367 var arg = Array[proto].slice.call(arguments, 0),
368 args = arg[join]("\u25ba"),
369 cache = newf.cache = newf.cache || {},
370 count = newf.count = newf.count || [];
371 if (cache[has](args)) {
372 return postprocessor ? postprocessor(cache[args]) : cache[args];
373 }
374 count[length] >= 1e3 && delete cache[count.shift()];
375 count[push](args);
376 cache[args] = f[apply](scope, arg);
377 return postprocessor ? postprocessor(cache[args]) : cache[args];
378 }
379 return newf;
380 }
381
382 R.getRGB = cacher(function (colour) {
383 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
384 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
385 }
386 if (colour == "none") {
387 return {r: -1, g: -1, b: -1, hex: "none"};
388 }
389 !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
390 var res,
391 red,
392 green,
393 blue,
394 opacity,
395 t,
396 values,
397 rgb = colour.match(colourRegExp);
398 if (rgb) {
399 if (rgb[2]) {
400 blue = toInt(rgb[2].substring(5), 16);
401 green = toInt(rgb[2].substring(3, 5), 16);
402 red = toInt(rgb[2].substring(1, 3), 16);
403 }
404 if (rgb[3]) {
405 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
406 green = toInt((t = rgb[3].charAt(2)) + t, 16);
407 red = toInt((t = rgb[3].charAt(1)) + t, 16);
408 }
409 if (rgb[4]) {
410 values = rgb[4][split](commaSpaces);
411 red = toFloat(values[0]);
412 values[0].slice(-1) == "%" && (red *= 2.55);
413 green = toFloat(values[1]);
414 values[1].slice(-1) == "%" && (green *= 2.55);
415 blue = toFloat(values[2]);
416 values[2].slice(-1) == "%" && (blue *= 2.55);
417 rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
418 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
419 }
420 if (rgb[5]) {
421 values = rgb[5][split](commaSpaces);
422 red = toFloat(values[0]);
423 values[0].slice(-1) == "%" && (red *= 2.55);
424 green = toFloat(values[1]);
425 values[1].slice(-1) == "%" && (green *= 2.55);
426 blue = toFloat(values[2]);
427 values[2].slice(-1) == "%" && (blue *= 2.55);
428 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
429 rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
430 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
431 return R.hsb2rgb(red, green, blue, opacity);
432 }
433 if (rgb[6]) {
434 values = rgb[6][split](commaSpaces);
435 red = toFloat(values[0]);
436 values[0].slice(-1) == "%" && (red *= 2.55);
437 green = toFloat(values[1]);
438 values[1].slice(-1) == "%" && (green *= 2.55);
439 blue = toFloat(values[2]);
440 values[2].slice(-1) == "%" && (blue *= 2.55);
441 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
442 rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
443 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
444 return R.hsl2rgb(red, green, blue, opacity);
445 }
446 rgb = {r: red, g: green, b: blue};
447 rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
448 R.is(opacity, "finite") && (rgb.opacity = opacity);
449 return rgb;
450 }
451 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
452 }, R);
453 R.getColor = function (value) {
454 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
455 rgb = this.hsb2rgb(start.h, start.s, start.b);
456 start.h += .075;
457 if (start.h > 1) {
458 start.h = 0;
459 start.s -= .2;
460 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
461 }
462 return rgb.hex;
463 };
464 R.getColor.reset = function () {
465 delete this.start;
466 };
467 // path utilities
468 R.parsePathString = cacher(function (pathString) {
469 if (!pathString) {
470 return null;
471 }
472 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
473 data = [];
474 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
475 data = pathClone(pathString);
476 }
477 if (!data[length]) {
478 Str(pathString)[rp](pathCommand, function (a, b, c) {
479 var params = [],
480 name = lowerCase.call(b);
481 c[rp](pathValues, function (a, b) {
482 b && params[push](+b);
483 });
484 if (name == "m" && params[length] > 2) {
485 data[push]([b][concat](params.splice(0, 2)));
486 name = "l";
487 b = b == "m" ? "l" : "L";
488 }
489 while (params[length] >= paramCounts[name]) {
490 data[push]([b][concat](params.splice(0, paramCounts[name])));
491 if (!paramCounts[name]) {
492 break;
493 }
494 }
495 });
496 }
497 data[toString] = R._path2string;
498 return data;
499 });
500 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
501 var t1 = 1 - t,
502 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
503 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
504 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
505 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
506 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
507 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
508 ax = (1 - t) * p1x + t * c1x,
509 ay = (1 - t) * p1y + t * c1y,
510 cx = (1 - t) * c2x + t * p2x,
511 cy = (1 - t) * c2y + t * p2y,
512 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / PI);
513 (mx > nx || my < ny) && (alpha += 180);
514 return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
515 };
516 var pathDimensions = cacher(function (path) {
517 if (!path) {
518 return {x: 0, y: 0, width: 0, height: 0};
519 }
520 path = path2curve(path);
521 var x = 0,
522 y = 0,
523 X = [],
524 Y = [],
525 p;
526 for (var i = 0, ii = path[length]; i < ii; i++) {
527 p = path[i];
528 if (p[0] == "M") {
529 x = p[1];
530 y = p[2];
531 X[push](x);
532 Y[push](y);
533 } else {
534 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
535 X = X[concat](dim.min.x, dim.max.x);
536 Y = Y[concat](dim.min.y, dim.max.y);
537 x = p[5];
538 y = p[6];
539 }
540 }
541 var xmin = mmin[apply](0, X),
542 ymin = mmin[apply](0, Y);
543 return {
544 x: xmin,
545 y: ymin,
546 width: mmax[apply](0, X) - xmin,
547 height: mmax[apply](0, Y) - ymin
548 };
549 }),
550 pathClone = function (pathArray) {
551 var res = [];
552 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
553 pathArray = R.parsePathString(pathArray);
554 }
555 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
556 res[i] = [];
557 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
558 res[i][j] = pathArray[i][j];
559 }
560 }
561 res[toString] = R._path2string;
562 return res;
563 },
564 pathToRelative = cacher(function (pathArray) {
565 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
566 pathArray = R.parsePathString(pathArray);
567 }
568 var res = [],
569 x = 0,
570 y = 0,
571 mx = 0,
572 my = 0,
573 start = 0;
574 if (pathArray[0][0] == "M") {
575 x = pathArray[0][1];
576 y = pathArray[0][2];
577 mx = x;
578 my = y;
579 start++;
580 res[push](["M", x, y]);
581 }
582 for (var i = start, ii = pathArray[length]; i < ii; i++) {
583 var r = res[i] = [],
584 pa = pathArray[i];
585 if (pa[0] != lowerCase.call(pa[0])) {
586 r[0] = lowerCase.call(pa[0]);
587 switch (r[0]) {
588 case "a":
589 r[1] = pa[1];
590 r[2] = pa[2];
591 r[3] = pa[3];
592 r[4] = pa[4];
593 r[5] = pa[5];
594 r[6] = +(pa[6] - x).toFixed(3);
595 r[7] = +(pa[7] - y).toFixed(3);
596 break;
597 case "v":
598 r[1] = +(pa[1] - y).toFixed(3);
599 break;
600 case "m":
601 mx = pa[1];
602 my = pa[2];
603 default:
604 for (var j = 1, jj = pa[length]; j < jj; j++) {
605 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
606 }
607 }
608 } else {
609 r = res[i] = [];
610 if (pa[0] == "m") {
611 mx = pa[1] + x;
612 my = pa[2] + y;
613 }
614 for (var k = 0, kk = pa[length]; k < kk; k++) {
615 res[i][k] = pa[k];
616 }
617 }
618 var len = res[i][length];
619 switch (res[i][0]) {
620 case "z":
621 x = mx;
622 y = my;
623 break;
624 case "h":
625 x += +res[i][len - 1];
626 break;
627 case "v":
628 y += +res[i][len - 1];
629 break;
630 default:
631 x += +res[i][len - 2];
632 y += +res[i][len - 1];
633 }
634 }
635 res[toString] = R._path2string;
636 return res;
637 }, 0, pathClone),
638 pathToAbsolute = cacher(function (pathArray) {
639 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
640 pathArray = R.parsePathString(pathArray);
641 }
642 var res = [],
643 x = 0,
644 y = 0,
645 mx = 0,
646 my = 0,
647 start = 0;
648 if (pathArray[0][0] == "M") {
649 x = +pathArray[0][1];
650 y = +pathArray[0][2];
651 mx = x;
652 my = y;
653 start++;
654 res[0] = ["M", x, y];
655 }
656 for (var i = start, ii = pathArray[length]; i < ii; i++) {
657 var r = res[i] = [],
658 pa = pathArray[i];
659 if (pa[0] != upperCase.call(pa[0])) {
660 r[0] = upperCase.call(pa[0]);
661 switch (r[0]) {
662 case "A":
663 r[1] = pa[1];
664 r[2] = pa[2];
665 r[3] = pa[3];
666 r[4] = pa[4];
667 r[5] = pa[5];
668 r[6] = +(pa[6] + x);
669 r[7] = +(pa[7] + y);
670 break;
671 case "V":
672 r[1] = +pa[1] + y;
673 break;
674 case "H":
675 r[1] = +pa[1] + x;
676 break;
677 case "M":
678 mx = +pa[1] + x;
679 my = +pa[2] + y;
680 default:
681 for (var j = 1, jj = pa[length]; j < jj; j++) {
682 r[j] = +pa[j] + ((j % 2) ? x : y);
683 }
684 }
685 } else {
686 for (var k = 0, kk = pa[length]; k < kk; k++) {
687 res[i][k] = pa[k];
688 }
689 }
690 switch (r[0]) {
691 case "Z":
692 x = mx;
693 y = my;
694 break;
695 case "H":
696 x = r[1];
697 break;
698 case "V":
699 y = r[1];
700 break;
701 case "M":
702 mx = res[i][res[i][length] - 2];
703 my = res[i][res[i][length] - 1];
704 default:
705 x = res[i][res[i][length] - 2];
706 y = res[i][res[i][length] - 1];
707 }
708 }
709 res[toString] = R._path2string;
710 return res;
711 }, null, pathClone),
712 l2c = function (x1, y1, x2, y2) {
713 return [x1, y1, x2, y2, x2, y2];
714 },
715 q2c = function (x1, y1, ax, ay, x2, y2) {
716 var _13 = 1 / 3,
717 _23 = 2 / 3;
718 return [
719 _13 * x1 + _23 * ax,
720 _13 * y1 + _23 * ay,
721 _13 * x2 + _23 * ax,
722 _13 * y2 + _23 * ay,
723 x2,
724 y2
725 ];
726 },
727 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
728 // for more information of where this math came from visit:
729 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
730 var _120 = PI * 120 / 180,
731 rad = PI / 180 * (+angle || 0),
732 res = [],
733 xy,
734 rotate = cacher(function (x, y, rad) {
735 var X = x * math.cos(rad) - y * math.sin(rad),
736 Y = x * math.sin(rad) + y * math.cos(rad);
737 return {x: X, y: Y};
738 });
739 if (!recursive) {
740 xy = rotate(x1, y1, -rad);
741 x1 = xy.x;
742 y1 = xy.y;
743 xy = rotate(x2, y2, -rad);
744 x2 = xy.x;
745 y2 = xy.y;
746 var cos = math.cos(PI / 180 * angle),
747 sin = math.sin(PI / 180 * angle),
748 x = (x1 - x2) / 2,
749 y = (y1 - y2) / 2;
750 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
751 if (h > 1) {
752 h = math.sqrt(h);
753 rx = h * rx;
754 ry = h * ry;
755 }
756 var rx2 = rx * rx,
757 ry2 = ry * ry,
758 k = (large_arc_flag == sweep_flag ? -1 : 1) *
759 math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
760 cx = k * rx * y / ry + (x1 + x2) / 2,
761 cy = k * -ry * x / rx + (y1 + y2) / 2,
762 f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
763 f2 = math.asin(((y2 - cy) / ry).toFixed(9));
764
765 f1 = x1 < cx ? PI - f1 : f1;
766 f2 = x2 < cx ? PI - f2 : f2;
767 f1 < 0 && (f1 = PI * 2 + f1);
768 f2 < 0 && (f2 = PI * 2 + f2);
769 if (sweep_flag && f1 > f2) {
770 f1 = f1 - PI * 2;
771 }
772 if (!sweep_flag && f2 > f1) {
773 f2 = f2 - PI * 2;
774 }
775 } else {
776 f1 = recursive[0];
777 f2 = recursive[1];
778 cx = recursive[2];
779 cy = recursive[3];
780 }
781 var df = f2 - f1;
782 if (abs(df) > _120) {
783 var f2old = f2,
784 x2old = x2,
785 y2old = y2;
786 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
787 x2 = cx + rx * math.cos(f2);
788 y2 = cy + ry * math.sin(f2);
789 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
790 }
791 df = f2 - f1;
792 var c1 = math.cos(f1),
793 s1 = math.sin(f1),
794 c2 = math.cos(f2),
795 s2 = math.sin(f2),
796 t = math.tan(df / 4),
797 hx = 4 / 3 * rx * t,
798 hy = 4 / 3 * ry * t,
799 m1 = [x1, y1],
800 m2 = [x1 + hx * s1, y1 - hy * c1],
801 m3 = [x2 + hx * s2, y2 - hy * c2],
802 m4 = [x2, y2];
803 m2[0] = 2 * m1[0] - m2[0];
804 m2[1] = 2 * m1[1] - m2[1];
805 if (recursive) {
806 return [m2, m3, m4][concat](res);
807 } else {
808 res = [m2, m3, m4][concat](res)[join]()[split](",");
809 var newres = [];
810 for (var i = 0, ii = res[length]; i < ii; i++) {
811 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
812 }
813 return newres;
814 }
815 },
816 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
817 var t1 = 1 - t;
818 return {
819 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
820 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
821 };
822 },
823 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
824 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
825 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
826 c = p1x - c1x,
827 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
828 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
829 y = [p1y, p2y],
830 x = [p1x, p2x],
831 dot;
832 abs(t1) > "1e12" && (t1 = .5);
833 abs(t2) > "1e12" && (t2 = .5);
834 if (t1 > 0 && t1 < 1) {
835 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
836 x[push](dot.x);
837 y[push](dot.y);
838 }
839 if (t2 > 0 && t2 < 1) {
840 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
841 x[push](dot.x);
842 y[push](dot.y);
843 }
844 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
845 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
846 c = p1y - c1y;
847 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
848 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
849 abs(t1) > "1e12" && (t1 = .5);
850 abs(t2) > "1e12" && (t2 = .5);
851 if (t1 > 0 && t1 < 1) {
852 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
853 x[push](dot.x);
854 y[push](dot.y);
855 }
856 if (t2 > 0 && t2 < 1) {
857 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
858 x[push](dot.x);
859 y[push](dot.y);
860 }
861 return {
862 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
863 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
864 };
865 }),
866 path2curve = cacher(function (path, path2) {
867 var p = pathToAbsolute(path),
868 p2 = path2 && pathToAbsolute(path2),
869 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
870 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
871 processPath = function (path, d) {
872 var nx, ny;
873 if (!path) {
874 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
875 }
876 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
877 switch (path[0]) {
878 case "M":
879 d.X = path[1];
880 d.Y = path[2];
881 break;
882 case "A":
883 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
884 break;
885 case "S":
886 nx = d.x + (d.x - (d.bx || d.x));
887 ny = d.y + (d.y - (d.by || d.y));
888 path = ["C", nx, ny][concat](path.slice(1));
889 break;
890 case "T":
891 d.qx = d.x + (d.x - (d.qx || d.x));
892 d.qy = d.y + (d.y - (d.qy || d.y));
893 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
894 break;
895 case "Q":
896 d.qx = path[1];
897 d.qy = path[2];
898 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
899 break;
900 case "L":
901 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
902 break;
903 case "H":
904 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
905 break;
906 case "V":
907 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
908 break;
909 case "Z":
910 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
911 break;
912 }
913 return path;
914 },
915 fixArc = function (pp, i) {
916 if (pp[i][length] > 7) {
917 pp[i].shift();
918 var pi = pp[i];
919 while (pi[length]) {
920 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
921 }
922 pp.splice(i, 1);
923 ii = mmax(p[length], p2 && p2[length] || 0);
924 }
925 },
926 fixM = function (path1, path2, a1, a2, i) {
927 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
928 path2.splice(i, 0, ["M", a2.x, a2.y]);
929 a1.bx = 0;
930 a1.by = 0;
931 a1.x = path1[i][1];
932 a1.y = path1[i][2];
933 ii = mmax(p[length], p2 && p2[length] || 0);
934 }
935 };
936 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
937 p[i] = processPath(p[i], attrs);
938 fixArc(p, i);
939 p2 && (p2[i] = processPath(p2[i], attrs2));
940 p2 && fixArc(p2, i);
941 fixM(p, p2, attrs, attrs2, i);
942 fixM(p2, p, attrs2, attrs, i);
943 var seg = p[i],
944 seg2 = p2 && p2[i],
945 seglen = seg[length],
946 seg2len = p2 && seg2[length];
947 attrs.x = seg[seglen - 2];
948 attrs.y = seg[seglen - 1];
949 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
950 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
951 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
952 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
953 attrs2.x = p2 && seg2[seg2len - 2];
954 attrs2.y = p2 && seg2[seg2len - 1];
955 }
956 return p2 ? [p, p2] : p;
957 }, null, pathClone),
958 parseDots = cacher(function (gradient) {
959 var dots = [];
960 for (var i = 0, ii = gradient[length]; i < ii; i++) {
961 var dot = {},
962 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
963 dot.color = R.getRGB(par[1]);
964 if (dot.color.error) {
965 return null;
966 }
967 dot.color = dot.color.hex;
968 par[2] && (dot.offset = par[2] + "%");
969 dots[push](dot);
970 }
971 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
972 if (!dots[i].offset) {
973 var start = toFloat(dots[i - 1].offset || 0),
974 end = 0;
975 for (var j = i + 1; j < ii; j++) {
976 if (dots[j].offset) {
977 end = dots[j].offset;
978 break;
979 }
980 }
981 if (!end) {
982 end = 100;
983 j = ii;
984 }
985 end = toFloat(end);
986 var d = (end - start) / (j - i + 1);
987 for (; i < j; i++) {
988 start += d;
989 dots[i].offset = start + "%";
990 }
991 }
992 }
993 return dots;
994 }),
995 getContainer = function (x, y, w, h) {
996 var container;
997 if (R.is(x, string) || R.is(x, "object")) {
998 container = R.is(x, string) ? doc.getElementById(x) : x;
999 if (container.tagName) {
1000 if (y == null) {
1001 return {
1002 container: container,
1003 width: container.style.pixelWidth || container.offsetWidth,
1004 height: container.style.pixelHeight || container.offsetHeight
1005 };
1006 } else {
1007 return {container: container, width: y, height: w};
1008 }
1009 }
1010 } else {
1011 return {container: 1, x: x, y: y, width: w, height: h};
1012 }
1013 },
1014 plugins = function (con, add) {
1015 var that = this;
1016 for (var prop in add) {
1017 if (add[has](prop) && !(prop in con)) {
1018 switch (typeof add[prop]) {
1019 case "function":
1020 (function (f) {
1021 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1022 })(add[prop]);
1023 break;
1024 case "object":
1025 con[prop] = con[prop] || {};
1026 plugins.call(this, con[prop], add[prop]);
1027 break;
1028 default:
1029 con[prop] = add[prop];
1030 break;
1031 }
1032 }
1033 }
1034 },
1035 tear = function (el, paper) {
1036 el == paper.top && (paper.top = el.prev);
1037 el == paper.bottom && (paper.bottom = el.next);
1038 el.next && (el.next.prev = el.prev);
1039 el.prev && (el.prev.next = el.next);
1040 },
1041 tofront = function (el, paper) {
1042 if (paper.top === el) {
1043 return;
1044 }
1045 tear(el, paper);
1046 el.next = null;
1047 el.prev = paper.top;
1048 paper.top.next = el;
1049 paper.top = el;
1050 },
1051 toback = function (el, paper) {
1052 if (paper.bottom === el) {
1053 return;
1054 }
1055 tear(el, paper);
1056 el.next = paper.bottom;
1057 el.prev = null;
1058 paper.bottom.prev = el;
1059 paper.bottom = el;
1060 },
1061 insertafter = function (el, el2, paper) {
1062 tear(el, paper);
1063 el2 == paper.top && (paper.top = el);
1064 el2.next && (el2.next.prev = el);
1065 el.next = el2.next;
1066 el.prev = el2;
1067 el2.next = el;
1068 },
1069 insertbefore = function (el, el2, paper) {
1070 tear(el, paper);
1071 el2 == paper.bottom && (paper.bottom = el);
1072 el2.prev && (el2.prev.next = el);
1073 el.prev = el2.prev;
1074 el2.prev = el;
1075 el.next = el2;
1076 },
1077 removed = function (methodname) {
1078 return function () {
1079 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1080 };
1081 };
1082 R.pathToRelative = pathToRelative;
1083 // SVG
1084 if (R.svg) {
1085 paperproto.svgns = "http://www.w3.org/2000/svg";
1086 paperproto.xlink = "http://www.w3.org/1999/xlink";
1087 round = function (num) {
1088 return +num + (~~num === num) * .5;
1089 };
1090 var $ = function (el, attr) {
1091 if (attr) {
1092 for (var key in attr) {
1093 if (attr[has](key)) {
1094 el[setAttribute](key, Str(attr[key]));
1095 }
1096 }
1097 } else {
1098 el = doc.createElementNS(paperproto.svgns, el);
1099 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1100 return el;
1101 }
1102 };
1103 R[toString] = function () {
1104 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1105 };
1106 var thePath = function (pathString, SVG) {
1107 var el = $("path");
1108 SVG.canvas && SVG.canvas[appendChild](el);
1109 var p = new Element(el, SVG);
1110 p.type = "path";
1111 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1112 return p;
1113 };
1114 var addGradientFill = function (o, gradient, SVG) {
1115 var type = "linear",
1116 fx = .5, fy = .5,
1117 s = o.style;
1118 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1119 type = "radial";
1120 if (_fx && _fy) {
1121 fx = toFloat(_fx);
1122 fy = toFloat(_fy);
1123 var dir = ((fy > .5) * 2 - 1);
1124 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1125 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1126 fy != .5 &&
1127 (fy = fy.toFixed(5) - 1e-5 * dir);
1128 }
1129 return E;
1130 });
1131 gradient = gradient[split](/\s*\-\s*/);
1132 if (type == "linear") {
1133 var angle = gradient.shift();
1134 angle = -toFloat(angle);
1135 if (isNaN(angle)) {
1136 return null;
1137 }
1138 var vector = [0, 0, math.cos(angle * PI / 180), math.sin(angle * PI / 180)],
1139 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
1140 vector[2] *= max;
1141 vector[3] *= max;
1142 if (vector[2] < 0) {
1143 vector[0] = -vector[2];
1144 vector[2] = 0;
1145 }
1146 if (vector[3] < 0) {
1147 vector[1] = -vector[3];
1148 vector[3] = 0;
1149 }
1150 }
1151 var dots = parseDots(gradient);
1152 if (!dots) {
1153 return null;
1154 }
1155 var id = o.getAttribute(fillString);
1156 id = id.match(/^url\(#(.*)\)$/);
1157 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1158
1159 var el = $(type + "Gradient");
1160 el.id = createUUID();
1161 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1162 SVG.defs[appendChild](el);
1163 for (var i = 0, ii = dots[length]; i < ii; i++) {
1164 var stop = $("stop");
1165 $(stop, {
1166 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1167 "stop-color": dots[i].color || "#fff"
1168 });
1169 el[appendChild](stop);
1170 }
1171 $(o, {
1172 fill: "url(#" + el.id + ")",
1173 opacity: 1,
1174 "fill-opacity": 1
1175 });
1176 s.fill = E;
1177 s.opacity = 1;
1178 s.fillOpacity = 1;
1179 return 1;
1180 };
1181 var updatePosition = function (o) {
1182 var bbox = o.getBBox();
1183 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1184 };
1185 var setFillAndStroke = function (o, params) {
1186 var dasharray = {
1187 "": [0],
1188 "none": [0],
1189 "-": [3, 1],
1190 ".": [1, 1],
1191 "-.": [3, 1, 1, 1],
1192 "-..": [3, 1, 1, 1, 1, 1],
1193 ". ": [1, 3],
1194 "- ": [4, 3],
1195 "--": [8, 3],
1196 "- .": [4, 3, 1, 3],
1197 "--.": [8, 3, 1, 3],
1198 "--..": [8, 3, 1, 3, 1, 3]
1199 },
1200 node = o.node,
1201 attrs = o.attrs,
1202 rot = o.rotate(),
1203 addDashes = function (o, value) {
1204 value = dasharray[lowerCase.call(value)];
1205 if (value) {
1206 var width = o.attrs["stroke-width"] || "1",
1207 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1208 dashes = [];
1209 var i = value[length];
1210 while (i--) {
1211 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1212 }
1213 $(node, {"stroke-dasharray": dashes[join](",")});
1214 }
1215 };
1216 params[has]("rotation") && (rot = params.rotation);
1217 var rotxy = Str(rot)[split](separator);
1218 if (!(rotxy.length - 1)) {
1219 rotxy = null;
1220 } else {
1221 rotxy[1] = +rotxy[1];
1222 rotxy[2] = +rotxy[2];
1223 }
1224 toFloat(rot) && o.rotate(0, true);
1225 for (var att in params) {
1226 if (params[has](att)) {
1227 if (!availableAttrs[has](att)) {
1228 continue;
1229 }
1230 var value = params[att];
1231 attrs[att] = value;
1232 switch (att) {
1233 case "blur":
1234 o.blur(value);
1235 break;
1236 case "rotation":
1237 o.rotate(value, true);
1238 break;
1239 case "href":
1240 case "title":
1241 case "target":
1242 var pn = node.parentNode;
1243 if (lowerCase.call(pn.tagName) != "a") {
1244 var hl = $("a");
1245 pn.insertBefore(hl, node);
1246 hl[appendChild](node);
1247 pn = hl;
1248 }
1249 if (att == "target" && value == "blank") {
1250 pn.setAttributeNS(o.paper.xlink, "show", "new");
1251 } else {
1252 pn.setAttributeNS(o.paper.xlink, att, value);
1253 }
1254 break;
1255 case "cursor":
1256 node.style.cursor = value;
1257 break;
1258 case "clip-rect":
1259 var rect = Str(value)[split](separator);
1260 if (rect[length] == 4) {
1261 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1262 var el = $("clipPath"),
1263 rc = $("rect");
1264 el.id = createUUID();
1265 $(rc, {
1266 x: rect[0],
1267 y: rect[1],
1268 width: rect[2],
1269 height: rect[3]
1270 });
1271 el[appendChild](rc);
1272 o.paper.defs[appendChild](el);
1273 $(node, {"clip-path": "url(#" + el.id + ")"});
1274 o.clip = rc;
1275 }
1276 if (!value) {
1277 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1278 clip && clip.parentNode.removeChild(clip);
1279 $(node, {"clip-path": E});
1280 delete o.clip;
1281 }
1282 break;
1283 case "path":
1284 if (o.type == "path") {
1285 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1286 }
1287 break;
1288 case "width":
1289 node[setAttribute](att, value);
1290 if (attrs.fx) {
1291 att = "x";
1292 value = attrs.x;
1293 } else {
1294 break;
1295 }
1296 case "x":
1297 if (attrs.fx) {
1298 value = -attrs.x - (attrs.width || 0);
1299 }
1300 case "rx":
1301 if (att == "rx" && o.type == "rect") {
1302 break;
1303 }
1304 case "cx":
1305 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1306 node[setAttribute](att, value);
1307 o.pattern && updatePosition(o);
1308 break;
1309 case "height":
1310 node[setAttribute](att, value);
1311 if (attrs.fy) {
1312 att = "y";
1313 value = attrs.y;
1314 } else {
1315 break;
1316 }
1317 case "y":
1318 if (attrs.fy) {
1319 value = -attrs.y - (attrs.height || 0);
1320 }
1321 case "ry":
1322 if (att == "ry" && o.type == "rect") {
1323 break;
1324 }
1325 case "cy":
1326 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1327 node[setAttribute](att, value);
1328 o.pattern && updatePosition(o);
1329 break;
1330 case "r":
1331 if (o.type == "rect") {
1332 $(node, {rx: value, ry: value});
1333 } else {
1334 node[setAttribute](att, value);
1335 }
1336 break;
1337 case "src":
1338 if (o.type == "image") {
1339 node.setAttributeNS(o.paper.xlink, "href", value);
1340 }
1341 break;
1342 case "stroke-width":
1343 node.style.strokeWidth = value;
1344 // Need following line for Firefox
1345 node[setAttribute](att, value);
1346 if (attrs["stroke-dasharray"]) {
1347 addDashes(o, attrs["stroke-dasharray"]);
1348 }
1349 break;
1350 case "stroke-dasharray":
1351 addDashes(o, value);
1352 break;
1353 case "translation":
1354 var xy = Str(value)[split](separator);
1355 xy[0] = +xy[0] || 0;
1356 xy[1] = +xy[1] || 0;
1357 if (rotxy) {
1358 rotxy[1] += xy[0];
1359 rotxy[2] += xy[1];
1360 }
1361 translate.call(o, xy[0], xy[1]);
1362 break;
1363 case "scale":
1364 xy = Str(value)[split](separator);
1365 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1366 break;
1367 case fillString:
1368 var isURL = Str(value).match(ISURL);
1369 if (isURL) {
1370 el = $("pattern");
1371 var ig = $("image");
1372 el.id = createUUID();
1373 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1374 $(ig, {x: 0, y: 0});
1375 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1376 el[appendChild](ig);
1377
1378 var img = doc.createElement("img");
1379 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1380 img.onload = function () {
1381 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1382 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1383 doc.body.removeChild(this);
1384 o.paper.safari();
1385 };
1386 doc.body[appendChild](img);
1387 img.src = isURL[1];
1388 o.paper.defs[appendChild](el);
1389 node.style.fill = "url(#" + el.id + ")";
1390 $(node, {fill: "url(#" + el.id + ")"});
1391 o.pattern = el;
1392 o.pattern && updatePosition(o);
1393 break;
1394 }
1395 var clr = R.getRGB(value);
1396 if (!clr.error) {
1397 delete params.gradient;
1398 delete attrs.gradient;
1399 !R.is(attrs.opacity, "undefined") &&
1400 R.is(params.opacity, "undefined") &&
1401 $(node, {opacity: attrs.opacity});
1402 !R.is(attrs["fill-opacity"], "undefined") &&
1403 R.is(params["fill-opacity"], "undefined") &&
1404 $(node, {"fill-opacity": attrs["fill-opacity"]});
1405 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1406 attrs.gradient = value;
1407 attrs.fill = "none";
1408 break;
1409 }
1410 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1411 case "stroke":
1412 clr = R.getRGB(value);
1413 node[setAttribute](att, clr.hex);
1414 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1415 break;
1416 case "gradient":
1417 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1418 break;
1419 case "opacity":
1420 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
1421 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
1422 }
1423 // fall
1424 case "fill-opacity":
1425 if (attrs.gradient) {
1426 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1427 if (gradient) {
1428 var stops = gradient.getElementsByTagName("stop");
1429 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1430 }
1431 break;
1432 }
1433 default:
1434 att == "font-size" && (value = toInt(value, 10) + "px");
1435 var cssrule = att[rp](/(\-.)/g, function (w) {
1436 return upperCase.call(w.substring(1));
1437 });
1438 node.style[cssrule] = value;
1439 // Need following line for Firefox
1440 node[setAttribute](att, value);
1441 break;
1442 }
1443 }
1444 }
1445
1446 tuneText(o, params);
1447 if (rotxy) {
1448 o.rotate(rotxy.join(S));
1449 } else {
1450 toFloat(rot) && o.rotate(rot, true);
1451 }
1452 };
1453 var leading = 1.2,
1454 tuneText = function (el, params) {
1455 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1456 return;
1457 }
1458 var a = el.attrs,
1459 node = el.node,
1460 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1461
1462 if (params[has]("text")) {
1463 a.text = params.text;
1464 while (node.firstChild) {
1465 node.removeChild(node.firstChild);
1466 }
1467 var texts = Str(params.text)[split]("\n");
1468 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1469 var tspan = $("tspan");
1470 i && $(tspan, {dy: fontSize * leading, x: a.x});
1471 tspan[appendChild](doc.createTextNode(texts[i]));
1472 node[appendChild](tspan);
1473 }
1474 } else {
1475 texts = node.getElementsByTagName("tspan");
1476 for (i = 0, ii = texts[length]; i < ii; i++) {
1477 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1478 }
1479 }
1480 $(node, {y: a.y});
1481 var bb = el.getBBox(),
1482 dif = a.y - (bb.y + bb.height / 2);
1483 dif && R.is(dif, "finite") && $(node, {y: a.y + dif});
1484 },
1485 Element = function (node, svg) {
1486 var X = 0,
1487 Y = 0;
1488 this[0] = node;
1489 this.id = R._oid++;
1490 this.node = node;
1491 node.raphael = this;
1492 this.paper = svg;
1493 this.attrs = this.attrs || {};
1494 this.transformations = []; // rotate, translate, scale
1495 this._ = {
1496 tx: 0,
1497 ty: 0,
1498 rt: {deg: 0, cx: 0, cy: 0},
1499 sx: 1,
1500 sy: 1
1501 };
1502 !svg.bottom && (svg.bottom = this);
1503 this.prev = svg.top;
1504 svg.top && (svg.top.next = this);
1505 svg.top = this;
1506 this.next = null;
1507 };
1508 var elproto = Element[proto];
1509 Element[proto].rotate = function (deg, cx, cy) {
1510 if (this.removed) {
1511 return this;
1512 }
1513 if (deg == null) {
1514 if (this._.rt.cx) {
1515 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1516 }
1517 return this._.rt.deg;
1518 }
1519 var bbox = this.getBBox();
1520 deg = Str(deg)[split](separator);
1521 if (deg[length] - 1) {
1522 cx = toFloat(deg[1]);
1523 cy = toFloat(deg[2]);
1524 }
1525 deg = toFloat(deg[0]);
1526 if (cx != null && cx !== false) {
1527 this._.rt.deg = deg;
1528 } else {
1529 this._.rt.deg += deg;
1530 }
1531 (cy == null) && (cx = null);
1532 this._.rt.cx = cx;
1533 this._.rt.cy = cy;
1534 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1535 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1536 if (this._.rt.deg) {
1537 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1538 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1539 } else {
1540 this.transformations[0] = E;
1541 this.clip && $(this.clip, {transform: E});
1542 }
1543 $(this.node, {transform: this.transformations[join](S)});
1544 return this;
1545 };
1546 Element[proto].hide = function () {
1547 !this.removed && (this.node.style.display = "none");
1548 return this;
1549 };
1550 Element[proto].show = function () {
1551 !this.removed && (this.node.style.display = "");
1552 return this;
1553 };
1554 Element[proto].remove = function () {
1555 if (this.removed) {
1556 return;
1557 }
1558 tear(this, this.paper);
1559 this.node.parentNode.removeChild(this.node);
1560 for (var i in this) {
1561 delete this[i];
1562 }
1563 this.removed = true;
1564 };
1565 Element[proto].getBBox = function () {
1566 if (this.removed) {
1567 return this;
1568 }
1569 if (this.type == "path") {
1570 return pathDimensions(this.attrs.path);
1571 }
1572 if (this.node.style.display == "none") {
1573 this.show();
1574 var hide = true;
1575 }
1576 var bbox = {};
1577 try {
1578 bbox = this.node.getBBox();
1579 } catch(e) {
1580 // Firefox 3.0.x plays badly here
1581 } finally {
1582 bbox = bbox || {};
1583 }
1584 if (this.type == "text") {
1585 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1586 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1587 var bb = this.node.getExtentOfChar(i);
1588 (bb.y < bbox.y) && (bbox.y = bb.y);
1589 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1590 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1591 }
1592 }
1593 hide && this.hide();
1594 return bbox;
1595 };
1596 Element[proto].attr = function (name, value) {
1597 if (this.removed) {
1598 return this;
1599 }
1600 if (name == null) {
1601 var res = {};
1602 for (var i in this.attrs) if (this.attrs[has](i)) {
1603 res[i] = this.attrs[i];
1604 }
1605 this._.rt.deg && (res.rotation = this.rotate());
1606 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1607 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1608 return res;
1609 }
1610 if (value == null && R.is(name, string)) {
1611 if (name == "translation") {
1612 return translate.call(this);
1613 }
1614 if (name == "rotation") {
1615 return this.rotate();
1616 }
1617 if (name == "scale") {
1618 return this.scale();
1619 }
1620 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1621 return this.attrs.gradient;
1622 }
1623 return this.attrs[name];
1624 }
1625 if (value == null && R.is(name, array)) {
1626 var values = {};
1627 for (var j = 0, jj = name.length; j < jj; j++) {
1628 values[name[j]] = this.attr(name[j]);
1629 }
1630 return values;
1631 }
1632 if (value != null) {
1633 var params = {};
1634 params[name] = value;
1635 } else if (name != null && R.is(name, "object")) {
1636 params = name;
1637 }
1638 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1639 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1640 this.attrs[key] = params[key];
1641 for (var subkey in par) if (par[has](subkey)) {
1642 params[subkey] = par[subkey];
1643 }
1644 }
1645 setFillAndStroke(this, params);
1646 return this;
1647 };
1648 Element[proto].toFront = function () {
1649 if (this.removed) {
1650 return this;
1651 }
1652 this.node.parentNode[appendChild](this.node);
1653 var svg = this.paper;
1654 svg.top != this && tofront(this, svg);
1655 return this;
1656 };
1657 Element[proto].toBack = function () {
1658 if (this.removed) {
1659 return this;
1660 }
1661 if (this.node.parentNode.firstChild != this.node) {
1662 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1663 toback(this, this.paper);
1664 var svg = this.paper;
1665 }
1666 return this;
1667 };
1668 Element[proto].insertAfter = function (element) {
1669 if (this.removed) {
1670 return this;
1671 }
1672 var node = element.node || element[element.length - 1].node;
1673 if (node.nextSibling) {
1674 node.parentNode.insertBefore(this.node, node.nextSibling);
1675 } else {
1676 node.parentNode[appendChild](this.node);
1677 }
1678 insertafter(this, element, this.paper);
1679 return this;
1680 };
1681 Element[proto].insertBefore = function (element) {
1682 if (this.removed) {
1683 return this;
1684 }
1685 var node = element.node || element[0].node;
1686 node.parentNode.insertBefore(this.node, node);
1687 insertbefore(this, element, this.paper);
1688 return this;
1689 };
1690 Element[proto].blur = function (size) {
1691 // Experimental. No Safari support. Use it on your own risk.
1692 var t = this;
1693 if (+size !== 0) {
1694 var fltr = $("filter"),
1695 blur = $("feGaussianBlur");
1696 t.attrs.blur = size;
1697 fltr.id = createUUID();
1698 $(blur, {stdDeviation: +size || 1.5});
1699 fltr.appendChild(blur);
1700 t.paper.defs.appendChild(fltr);
1701 t._blur = fltr;
1702 $(t.node, {filter: "url(#" + fltr.id + ")"});
1703 } else {
1704 if (t._blur) {
1705 t._blur.parentNode.removeChild(t._blur);
1706 delete t._blur;
1707 delete t.attrs.blur;
1708 }
1709 t.node.removeAttribute("filter");
1710 }
1711 };
1712 var theCircle = function (svg, x, y, r) {
1713 var el = $("circle");
1714 svg.canvas && svg.canvas[appendChild](el);
1715 var res = new Element(el, svg);
1716 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1717 res.type = "circle";
1718 $(el, res.attrs);
1719 return res;
1720 },
1721 theRect = function (svg, x, y, w, h, r) {
1722 var el = $("rect");
1723 svg.canvas && svg.canvas[appendChild](el);
1724 var res = new Element(el, svg);
1725 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1726 res.type = "rect";
1727 $(el, res.attrs);
1728 return res;
1729 },
1730 theEllipse = function (svg, x, y, rx, ry) {
1731 var el = $("ellipse");
1732 svg.canvas && svg.canvas[appendChild](el);
1733 var res = new Element(el, svg);
1734 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1735 res.type = "ellipse";
1736 $(el, res.attrs);
1737 return res;
1738 },
1739 theImage = function (svg, src, x, y, w, h) {
1740 var el = $("image");
1741 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1742 el.setAttributeNS(svg.xlink, "href", src);
1743 svg.canvas && svg.canvas[appendChild](el);
1744 var res = new Element(el, svg);
1745 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1746 res.type = "image";
1747 return res;
1748 },
1749 theText = function (svg, x, y, text) {
1750 var el = $("text");
1751 $(el, {x: x, y: y, "text-anchor": "middle"});
1752 svg.canvas && svg.canvas[appendChild](el);
1753 var res = new Element(el, svg);
1754 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1755 res.type = "text";
1756 setFillAndStroke(res, res.attrs);
1757 return res;
1758 },
1759 setSize = function (width, height) {
1760 this.width = width || this.width;
1761 this.height = height || this.height;
1762 this.canvas[setAttribute]("width", this.width);
1763 this.canvas[setAttribute]("height", this.height);
1764 return this;
1765 },
1766 create = function () {
1767 var con = getContainer[apply](0, arguments),
1768 container = con && con.container,
1769 x = con.x,
1770 y = con.y,
1771 width = con.width,
1772 height = con.height;
1773 if (!container) {
1774 throw new Error("SVG container not found.");
1775 }
1776 var cnvs = $("svg");
1777 x = x || 0;
1778 y = y || 0;
1779 width = width || 512;
1780 height = height || 342;
1781 $(cnvs, {
1782 xmlns: "http://www.w3.org/2000/svg",
1783 version: 1.1,
1784 width: width,
1785 height: height
1786 });
1787 if (container == 1) {
1788 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1789 doc.body[appendChild](cnvs);
1790 } else {
1791 if (container.firstChild) {
1792 container.insertBefore(cnvs, container.firstChild);
1793 } else {
1794 container[appendChild](cnvs);
1795 }
1796 }
1797 container = new Paper;
1798 container.width = width;
1799 container.height = height;
1800 container.canvas = cnvs;
1801 plugins.call(container, container, R.fn);
1802 container.clear();
1803 return container;
1804 };
1805 paperproto.clear = function () {
1806 var c = this.canvas;
1807 while (c.firstChild) {
1808 c.removeChild(c.firstChild);
1809 }
1810 this.bottom = this.top = null;
1811 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1812 c[appendChild](this.desc);
1813 c[appendChild](this.defs = $("defs"));
1814 };
1815 paperproto.remove = function () {
1816 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1817 for (var i in this) {
1818 this[i] = removed(i);
1819 }
1820 };
1821 }
1822
1823 // VML
1824 if (R.vml) {
1825 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1826 bites = /([clmz]),?([^clmz]*)/gi,
1827 blurregexp = / progid:\S+Blur\([^\)]+\)/g,
1828 val = /-?[^,\s-]+/g,
1829 coordsize = 1e3 + S + 1e3,
1830 zoom = 10,
1831 pathlike = {path: 1, rect: 1},
1832 path2vml = function (path) {
1833 var total = /[ahqstv]/ig,
1834 command = pathToAbsolute;
1835 Str(path).match(total) && (command = path2curve);
1836 total = /[clmz]/g;
1837 if (command == pathToAbsolute && !Str(path).match(total)) {
1838 var res = Str(path)[rp](bites, function (all, command, args) {
1839 var vals = [],
1840 isMove = lowerCase.call(command) == "m",
1841 res = map[command];
1842 args[rp](val, function (value) {
1843 if (isMove && vals[length] == 2) {
1844 res += vals + map[command == "m" ? "l" : "L"];
1845 vals = [];
1846 }
1847 vals[push](round(value * zoom));
1848 });
1849 return res + vals;
1850 });
1851 return res;
1852 }
1853 var pa = command(path), p, r;
1854 res = [];
1855 for (var i = 0, ii = pa[length]; i < ii; i++) {
1856 p = pa[i];
1857 r = lowerCase.call(pa[i][0]);
1858 r == "z" && (r = "x");
1859 for (var j = 1, jj = p[length]; j < jj; j++) {
1860 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1861 }
1862 res[push](r);
1863 }
1864 return res[join](S);
1865 };
1866
1867 R[toString] = function () {
1868 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1869 };
1870 thePath = function (pathString, vml) {
1871 var g = createNode("group");
1872 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1873 g.coordsize = vml.coordsize;
1874 g.coordorigin = vml.coordorigin;
1875 var el = createNode("shape"), ol = el.style;
1876 ol.width = vml.width + "px";
1877 ol.height = vml.height + "px";
1878 el.coordsize = coordsize;
1879 el.coordorigin = vml.coordorigin;
1880 g[appendChild](el);
1881 var p = new Element(el, g, vml),
1882 attr = {fill: "none", stroke: "#000"};
1883 pathString && (attr.path = pathString);
1884 p.type = "path";
1885 p.path = [];
1886 p.Path = E;
1887 setFillAndStroke(p, attr);
1888 vml.canvas[appendChild](g);
1889 return p;
1890 };
1891 setFillAndStroke = function (o, params) {
1892 o.attrs = o.attrs || {};
1893 var node = o.node,
1894 a = o.attrs,
1895 s = node.style,
1896 xy,
1897 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1898 res = o;
1899
1900 for (var par in params) if (params[has](par)) {
1901 a[par] = params[par];
1902 }
1903 if (newpath) {
1904 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1905 o.X = a.x;
1906 o.Y = a.y;
1907 o.W = a.width;
1908 o.H = a.height;
1909 }
1910 params.href && (node.href = params.href);
1911 params.title && (node.title = params.title);
1912 params.target && (node.target = params.target);
1913 params.cursor && (s.cursor = params.cursor);
1914 "blur" in params && o.blur(params.blur);
1915 if (params.path && o.type == "path" || newpath) {
1916 node.path = path2vml(a.path);
1917 }
1918 if (params.rotation != null) {
1919 o.rotate(params.rotation, true);
1920 }
1921 if (params.translation) {
1922 xy = Str(params.translation)[split](separator);
1923 translate.call(o, xy[0], xy[1]);
1924 if (o._.rt.cx != null) {
1925 o._.rt.cx +=+ xy[0];
1926 o._.rt.cy +=+ xy[1];
1927 o.setBox(o.attrs, xy[0], xy[1]);
1928 }
1929 }
1930 if (params.scale) {
1931 xy = Str(params.scale)[split](separator);
1932 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1933 }
1934 if ("clip-rect" in params) {
1935 var rect = Str(params["clip-rect"])[split](separator);
1936 if (rect[length] == 4) {
1937 rect[2] = +rect[2] + (+rect[0]);
1938 rect[3] = +rect[3] + (+rect[1]);
1939 var div = node.clipRect || doc.createElement("div"),
1940 dstyle = div.style,
1941 group = node.parentNode;
1942 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1943 if (!node.clipRect) {
1944 dstyle.position = "absolute";
1945 dstyle.top = 0;
1946 dstyle.left = 0;
1947 dstyle.width = o.paper.width + "px";
1948 dstyle.height = o.paper.height + "px";
1949 group.parentNode.insertBefore(div, group);
1950 div[appendChild](group);
1951 node.clipRect = div;
1952 }
1953 }
1954 if (!params["clip-rect"]) {
1955 node.clipRect && (node.clipRect.style.clip = E);
1956 }
1957 }
1958 if (o.type == "image" && params.src) {
1959 node.src = params.src;
1960 }
1961 if (o.type == "image" && params.opacity) {
1962 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1963 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1964 }
1965 params.font && (s.font = params.font);
1966 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1967 params["font-size"] && (s.fontSize = params["font-size"]);
1968 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1969 params["font-style"] && (s.fontStyle = params["font-style"]);
1970 if (params.opacity != null ||
1971 params["stroke-width"] != null ||
1972 params.fill != null ||
1973 params.stroke != null ||
1974 params["stroke-width"] != null ||
1975 params["stroke-opacity"] != null ||
1976 params["fill-opacity"] != null ||
1977 params["stroke-dasharray"] != null ||
1978 params["stroke-miterlimit"] != null ||
1979 params["stroke-linejoin"] != null ||
1980 params["stroke-linecap"] != null) {
1981 node = o.shape || node;
1982 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1983 newfill = false;
1984 !fill && (newfill = fill = createNode(fillString));
1985 if ("fill-opacity" in params || "opacity" in params) {
1986 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1987 opacity = mmin(mmax(opacity, 0), 1);
1988 fill.opacity = opacity;
1989 }
1990 params.fill && (fill.on = true);
1991 if (fill.on == null || params.fill == "none") {
1992 fill.on = false;
1993 }
1994 if (fill.on && params.fill) {
1995 var isURL = params.fill.match(ISURL);
1996 if (isURL) {
1997 fill.src = isURL[1];
1998 fill.type = "tile";
1999 } else {
2000 fill.color = R.getRGB(params.fill).hex;
2001 fill.src = E;
2002 fill.type = "solid";
2003 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
2004 a.fill = "none";
2005 a.gradient = params.fill;
2006 }
2007 }
2008 }
2009 newfill && node[appendChild](fill);
2010 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
2011 newstroke = false;
2012 !stroke && (newstroke = stroke = createNode("stroke"));
2013 if ((params.stroke && params.stroke != "none") ||
2014 params["stroke-width"] ||
2015 params["stroke-opacity"] != null ||
2016 params["stroke-dasharray"] ||
2017 params["stroke-miterlimit"] ||
2018 params["stroke-linejoin"] ||
2019 params["stroke-linecap"]) {
2020 stroke.on = true;
2021 }
2022 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
2023 var strokeColor = R.getRGB(params.stroke);
2024 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
2025 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2026 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2027 opacity = mmin(mmax(opacity, 0), 1);
2028 params["stroke-width"] == null && (width = a["stroke-width"]);
2029 params["stroke-width"] && (stroke.weight = width);
2030 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2031 stroke.opacity = opacity;
2032
2033 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2034 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2035 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2036 if (params["stroke-dasharray"]) {
2037 var dasharray = {
2038 "-": "shortdash",
2039 ".": "shortdot",
2040 "-.": "shortdashdot",
2041 "-..": "shortdashdotdot",
2042 ". ": "dot",
2043 "- ": "dash",
2044 "--": "longdash",
2045 "- .": "dashdot",
2046 "--.": "longdashdot",
2047 "--..": "longdashdotdot"
2048 };
2049 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2050 }
2051 newstroke && node[appendChild](stroke);
2052 }
2053 if (res.type == "text") {
2054 s = res.paper.span.style;
2055 a.font && (s.font = a.font);
2056 a["font-family"] && (s.fontFamily = a["font-family"]);
2057 a["font-size"] && (s.fontSize = a["font-size"]);
2058 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2059 a["font-style"] && (s.fontStyle = a["font-style"]);
2060 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "&#60;")[rp](/&/g, "&#38;")[rp](/\n/g, "<br>"));
2061 res.W = a.w = res.paper.span.offsetWidth;
2062 res.H = a.h = res.paper.span.offsetHeight;
2063 res.X = a.x;
2064 res.Y = a.y + round(res.H / 2);
2065
2066 // text-anchor emulationm
2067 switch (a["text-anchor"]) {
2068 case "start":
2069 res.node.style["v-text-align"] = "left";
2070 res.bbx = round(res.W / 2);
2071 break;
2072 case "end":
2073 res.node.style["v-text-align"] = "right";
2074 res.bbx = -round(res.W / 2);
2075 break;
2076 default:
2077 res.node.style["v-text-align"] = "center";
2078 break;
2079 }
2080 }
2081 };
2082 addGradientFill = function (o, gradient) {
2083 o.attrs = o.attrs || {};
2084 var attrs = o.attrs,
2085 fill,
2086 type = "linear",
2087 fxfy = ".5 .5";
2088 o.attrs.gradient = gradient;
2089 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2090 type = "radial";
2091 if (fx && fy) {
2092 fx = toFloat(fx);
2093 fy = toFloat(fy);
2094 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2095 fxfy = fx + S + fy;
2096 }
2097 return E;
2098 });
2099 gradient = gradient[split](/\s*\-\s*/);
2100 if (type == "linear") {
2101 var angle = gradient.shift();
2102 angle = -toFloat(angle);
2103 if (isNaN(angle)) {
2104 return null;
2105 }
2106 }
2107 var dots = parseDots(gradient);
2108 if (!dots) {
2109 return null;
2110 }
2111 o = o.shape || o.node;
2112 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2113 !fill.parentNode && o.appendChild(fill);
2114 if (dots[length]) {
2115 fill.on = true;
2116 fill.method = "none";
2117 fill.color = dots[0].color;
2118 fill.color2 = dots[dots[length] - 1].color;
2119 var clrs = [];
2120 for (var i = 0, ii = dots[length]; i < ii; i++) {
2121 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2122 }
2123 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2124 if (type == "radial") {
2125 fill.type = "gradientradial";
2126 fill.focus = "100%";
2127 fill.focussize = fxfy;
2128 fill.focusposition = fxfy;
2129 } else {
2130 fill.type = "gradient";
2131 fill.angle = (270 - angle) % 360;
2132 }
2133 }
2134 return 1;
2135 };
2136 Element = function (node, group, vml) {
2137 var Rotation = 0,
2138 RotX = 0,
2139 RotY = 0,
2140 Scale = 1;
2141 this[0] = node;
2142 this.id = R._oid++;
2143 this.node = node;
2144 node.raphael = this;
2145 this.X = 0;
2146 this.Y = 0;
2147 this.attrs = {};
2148 this.Group = group;
2149 this.paper = vml;
2150 this._ = {
2151 tx: 0,
2152 ty: 0,
2153 rt: {deg:0},
2154 sx: 1,
2155 sy: 1
2156 };
2157 !vml.bottom && (vml.bottom = this);
2158 this.prev = vml.top;
2159 vml.top && (vml.top.next = this);
2160 vml.top = this;
2161 this.next = null;
2162 };
2163 elproto = Element[proto];
2164 elproto.rotate = function (deg, cx, cy) {
2165 if (this.removed) {
2166 return this;
2167 }
2168 if (deg == null) {
2169 if (this._.rt.cx) {
2170 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2171 }
2172 return this._.rt.deg;
2173 }
2174 deg = Str(deg)[split](separator);
2175 if (deg[length] - 1) {
2176 cx = toFloat(deg[1]);
2177 cy = toFloat(deg[2]);
2178 }
2179 deg = toFloat(deg[0]);
2180 if (cx != null) {
2181 this._.rt.deg = deg;
2182 } else {
2183 this._.rt.deg += deg;
2184 }
2185 cy == null && (cx = null);
2186 this._.rt.cx = cx;
2187 this._.rt.cy = cy;
2188 this.setBox(this.attrs, cx, cy);
2189 this.Group.style.rotation = this._.rt.deg;
2190 // gradient fix for rotation. TODO
2191 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2192 // fill = fill[0] || {};
2193 // var b = ((360 - this._.rt.deg) - 270) % 360;
2194 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2195 return this;
2196 };
2197 elproto.setBox = function (params, cx, cy) {
2198 if (this.removed) {
2199 return this;
2200 }
2201 var gs = this.Group.style,
2202 os = (this.shape && this.shape.style) || this.node.style;
2203 params = params || {};
2204 for (var i in params) if (params[has](i)) {
2205 this.attrs[i] = params[i];
2206 }
2207 cx = cx || this._.rt.cx;
2208 cy = cy || this._.rt.cy;
2209 var attr = this.attrs,
2210 x,
2211 y,
2212 w,
2213 h;
2214 switch (this.type) {
2215 case "circle":
2216 x = attr.cx - attr.r;
2217 y = attr.cy - attr.r;
2218 w = h = attr.r * 2;
2219 break;
2220 case "ellipse":
2221 x = attr.cx - attr.rx;
2222 y = attr.cy - attr.ry;
2223 w = attr.rx * 2;
2224 h = attr.ry * 2;
2225 break;
2226 case "image":
2227 x = +attr.x;
2228 y = +attr.y;
2229 w = attr.width || 0;
2230 h = attr.height || 0;
2231 break;
2232 case "text":
2233 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2234 x = attr.x - round(this.W / 2);
2235 y = attr.y - this.H / 2;
2236 w = this.W;
2237 h = this.H;
2238 break;
2239 case "rect":
2240 case "path":
2241 if (!this.attrs.path) {
2242 x = 0;
2243 y = 0;
2244 w = this.paper.width;
2245 h = this.paper.height;
2246 } else {
2247 var dim = pathDimensions(this.attrs.path);
2248 x = dim.x;
2249 y = dim.y;
2250 w = dim.width;
2251 h = dim.height;
2252 }
2253 break;
2254 default:
2255 x = 0;
2256 y = 0;
2257 w = this.paper.width;
2258 h = this.paper.height;
2259 break;
2260 }
2261 cx = (cx == null) ? x + w / 2 : cx;
2262 cy = (cy == null) ? y + h / 2 : cy;
2263 var left = cx - this.paper.width / 2,
2264 top = cy - this.paper.height / 2, t;
2265 gs.left != (t = left + "px") && (gs.left = t);
2266 gs.top != (t = top + "px") && (gs.top = t);
2267 this.X = pathlike[has](this.type) ? -left : x;
2268 this.Y = pathlike[has](this.type) ? -top : y;
2269 this.W = w;
2270 this.H = h;
2271 if (pathlike[has](this.type)) {
2272 os.left != (t = -left * zoom + "px") && (os.left = t);
2273 os.top != (t = -top * zoom + "px") && (os.top = t);
2274 } else if (this.type == "text") {
2275 os.left != (t = -left + "px") && (os.left = t);
2276 os.top != (t = -top + "px") && (os.top = t);
2277 } else {
2278 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2279 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2280 os.left != (t = x - left + "px") && (os.left = t);
2281 os.top != (t = y - top + "px") && (os.top = t);
2282 os.width != (t = w + "px") && (os.width = t);
2283 os.height != (t = h + "px") && (os.height = t);
2284 }
2285 };
2286 elproto.hide = function () {
2287 !this.removed && (this.Group.style.display = "none");
2288 return this;
2289 };
2290 elproto.show = function () {
2291 !this.removed && (this.Group.style.display = "block");
2292 return this;
2293 };
2294 elproto.getBBox = function () {
2295 if (this.removed) {
2296 return this;
2297 }
2298 if (pathlike[has](this.type)) {
2299 return pathDimensions(this.attrs.path);
2300 }
2301 return {
2302 x: this.X + (this.bbx || 0),
2303 y: this.Y,
2304 width: this.W,
2305 height: this.H
2306 };
2307 };
2308 elproto.remove = function () {
2309 if (this.removed) {
2310 return;
2311 }
2312 tear(this, this.paper);
2313 this.node.parentNode.removeChild(this.node);
2314 this.Group.parentNode.removeChild(this.Group);
2315 this.shape && this.shape.parentNode.removeChild(this.shape);
2316 for (var i in this) {
2317 delete this[i];
2318 }
2319 this.removed = true;
2320 };
2321 elproto.attr = function (name, value) {
2322 if (this.removed) {
2323 return this;
2324 }
2325 if (name == null) {
2326 var res = {};
2327 for (var i in this.attrs) if (this.attrs[has](i)) {
2328 res[i] = this.attrs[i];
2329 }
2330 this._.rt.deg && (res.rotation = this.rotate());
2331 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2332 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2333 return res;
2334 }
2335 if (value == null && R.is(name, "string")) {
2336 if (name == "translation") {
2337 return translate.call(this);
2338 }
2339 if (name == "rotation") {
2340 return this.rotate();
2341 }
2342 if (name == "scale") {
2343 return this.scale();
2344 }
2345 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2346 return this.attrs.gradient;
2347 }
2348 return this.attrs[name];
2349 }
2350 if (this.attrs && value == null && R.is(name, array)) {
2351 var ii, values = {};
2352 for (i = 0, ii = name[length]; i < ii; i++) {
2353 values[name[i]] = this.attr(name[i]);
2354 }
2355 return values;
2356 }
2357 var params;
2358 if (value != null) {
2359 params = {};
2360 params[name] = value;
2361 }
2362 value == null && R.is(name, "object") && (params = name);
2363 if (params) {
2364 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2365 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2366 this.attrs[key] = params[key];
2367 for (var subkey in par) if (par[has](subkey)) {
2368 params[subkey] = par[subkey];
2369 }
2370 }
2371 if (params.text && this.type == "text") {
2372 this.node.string = params.text;
2373 }
2374 setFillAndStroke(this, params);
2375 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2376 addGradientFill(this, params.gradient);
2377 }
2378 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2379 }
2380 return this;
2381 };
2382 elproto.toFront = function () {
2383 !this.removed && this.Group.parentNode[appendChild](this.Group);
2384 this.paper.top != this && tofront(this, this.paper);
2385 return this;
2386 };
2387 elproto.toBack = function () {
2388 if (this.removed) {
2389 return this;
2390 }
2391 if (this.Group.parentNode.firstChild != this.Group) {
2392 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2393 toback(this, this.paper);
2394 }
2395 return this;
2396 };
2397 elproto.insertAfter = function (element) {
2398 if (this.removed) {
2399 return this;
2400 }
2401 if (element.constructor == Set) {
2402 element = element[element.length - 1];
2403 }
2404 if (element.Group.nextSibling) {
2405 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2406 } else {
2407 element.Group.parentNode[appendChild](this.Group);
2408 }
2409 insertafter(this, element, this.paper);
2410 return this;
2411 };
2412 elproto.insertBefore = function (element) {
2413 if (this.removed) {
2414 return this;
2415 }
2416 if (element.constructor == Set) {
2417 element = element[0];
2418 }
2419 element.Group.parentNode.insertBefore(this.Group, element.Group);
2420 insertbefore(this, element, this.paper);
2421 return this;
2422 };
2423 elproto.blur = function (size) {
2424 var s = this.node.runtimeStyle,
2425 f = s.filter;
2426 f = f.replace(blurregexp, E);
2427 if (+size !== 0) {
2428 this.attrs.blur = size;
2429 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2430 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2431 } else {
2432 s.filter = f;
2433 s.margin = 0;
2434 delete this.attrs.blur;
2435 }
2436 };
2437
2438 theCircle = function (vml, x, y, r) {
2439 var g = createNode("group"),
2440 o = createNode("oval"),
2441 ol = o.style;
2442 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2443 g.coordsize = coordsize;
2444 g.coordorigin = vml.coordorigin;
2445 g[appendChild](o);
2446 var res = new Element(o, g, vml);
2447 res.type = "circle";
2448 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2449 res.attrs.cx = x;
2450 res.attrs.cy = y;
2451 res.attrs.r = r;
2452 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2453 vml.canvas[appendChild](g);
2454 return res;
2455 };
2456 function rectPath(x, y, w, h, r) {
2457 if (r) {
2458 return R.format("M{0},{1}l{2},0a{3},{3},0,0,1,{3},{3}l0,{5}a{3},{3},0,0,1,{4},{3}l{6},0a{3},{3},0,0,1,{4},{4}l0,{7}a{3},{3},0,0,1,{3},{4}z", x + r, y, w - r * 2, r, -r, h - r * 2, r * 2 - w, r * 2 - h);
2459 } else {
2460 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2461 }
2462 }
2463 theRect = function (vml, x, y, w, h, r) {
2464 var path = rectPath(x, y, w, h, r),
2465 res = vml.path(path),
2466 a = res.attrs;
2467 res.X = a.x = x;
2468 res.Y = a.y = y;
2469 res.W = a.width = w;
2470 res.H = a.height = h;
2471 a.r = r;
2472 a.path = path;
2473 res.type = "rect";
2474 return res;
2475 };
2476 theEllipse = function (vml, x, y, rx, ry) {
2477 var g = createNode("group"),
2478 o = createNode("oval"),
2479 ol = o.style;
2480 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2481 g.coordsize = coordsize;
2482 g.coordorigin = vml.coordorigin;
2483 g[appendChild](o);
2484 var res = new Element(o, g, vml);
2485 res.type = "ellipse";
2486 setFillAndStroke(res, {stroke: "#000"});
2487 res.attrs.cx = x;
2488 res.attrs.cy = y;
2489 res.attrs.rx = rx;
2490 res.attrs.ry = ry;
2491 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2492 vml.canvas[appendChild](g);
2493 return res;
2494 };
2495 theImage = function (vml, src, x, y, w, h) {
2496 var g = createNode("group"),
2497 o = createNode("image");
2498 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2499 g.coordsize = coordsize;
2500 g.coordorigin = vml.coordorigin;
2501 o.src = src;
2502 g[appendChild](o);
2503 var res = new Element(o, g, vml);
2504 res.type = "image";
2505 res.attrs.src = src;
2506 res.attrs.x = x;
2507 res.attrs.y = y;
2508 res.attrs.w = w;
2509 res.attrs.h = h;
2510 res.setBox({x: x, y: y, width: w, height: h});
2511 vml.canvas[appendChild](g);
2512 return res;
2513 };
2514 theText = function (vml, x, y, text) {
2515 var g = createNode("group"),
2516 el = createNode("shape"),
2517 ol = el.style,
2518 path = createNode("path"),
2519 ps = path.style,
2520 o = createNode("textpath");
2521 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2522 g.coordsize = coordsize;
2523 g.coordorigin = vml.coordorigin;
2524 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2525 path.textpathok = true;
2526 ol.width = vml.width;
2527 ol.height = vml.height;
2528 o.string = Str(text);
2529 o.on = true;
2530 el[appendChild](o);
2531 el[appendChild](path);
2532 g[appendChild](el);
2533 var res = new Element(o, g, vml);
2534 res.shape = el;
2535 res.textpath = path;
2536 res.type = "text";
2537 res.attrs.text = text;
2538 res.attrs.x = x;
2539 res.attrs.y = y;
2540 res.attrs.w = 1;
2541 res.attrs.h = 1;
2542 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2543 res.setBox();
2544 vml.canvas[appendChild](g);
2545 return res;
2546 };
2547 setSize = function (width, height) {
2548 var cs = this.canvas.style;
2549 width == +width && (width += "px");
2550 height == +height && (height += "px");
2551 cs.width = width;
2552 cs.height = height;
2553 cs.clip = "rect(0 " + width + " " + height + " 0)";
2554 return this;
2555 };
2556 var createNode;
2557 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2558 try {
2559 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2560 createNode = function (tagName) {
2561 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2562 };
2563 } catch (e) {
2564 createNode = function (tagName) {
2565 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2566 };
2567 }
2568 create = function () {
2569 var con = getContainer[apply](0, arguments),
2570 container = con.container,
2571 height = con.height,
2572 s,
2573 width = con.width,
2574 x = con.x,
2575 y = con.y;
2576 if (!container) {
2577 throw new Error("VML container not found.");
2578 }
2579 var res = new Paper,
2580 c = res.canvas = doc.createElement("div"),
2581 cs = c.style;
2582 x = x || 0;
2583 y = y || 0;
2584 width = width || 512;
2585 height = height || 342;
2586 width == +width && (width += "px");
2587 height == +height && (height += "px");
2588 res.width = 1e3;
2589 res.height = 1e3;
2590 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2591 res.coordorigin = "0 0";
2592 res.span = doc.createElement("span");
2593 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2594 c[appendChild](res.span);
2595 cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2596 if (container == 1) {
2597 doc.body[appendChild](c);
2598 cs.left = x + "px";
2599 cs.top = y + "px";
2600 cs.position = "absolute";
2601 } else {
2602 if (container.firstChild) {
2603 container.insertBefore(c, container.firstChild);
2604 } else {
2605 container[appendChild](c);
2606 }
2607 }
2608 plugins.call(res, res, R.fn);
2609 return res;
2610 };
2611 paperproto.clear = function () {
2612 this.canvas.innerHTML = E;
2613 this.span = doc.createElement("span");
2614 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2615 this.canvas[appendChild](this.span);
2616 this.bottom = this.top = null;
2617 };
2618 paperproto.remove = function () {
2619 this.canvas.parentNode.removeChild(this.canvas);
2620 for (var i in this) {
2621 this[i] = removed(i);
2622 }
2623 return true;
2624 };
2625 }
2626
2627 // rest
2628 // WebKit rendering bug workaround method
2629 var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2630 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2631 paperproto.safari = function () {
2632 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2633 win.setTimeout(function () {rect.remove();});
2634 };
2635 } else {
2636 paperproto.safari = function () {};
2637 }
2638
2639 // Events
2640 var preventDefault = function () {
2641 this.returnValue = false;
2642 },
2643 preventTouch = function () {
2644 return this.originalEvent.preventDefault();
2645 },
2646 stopPropagation = function () {
2647 this.cancelBubble = true;
2648 },
2649 stopTouch = function () {
2650 return this.originalEvent.stopPropagation();
2651 },
2652 addEvent = (function () {
2653 if (doc.addEventListener) {
2654 return function (obj, type, fn, element) {
2655 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2656 var f = function (e) {
2657 if (supportsTouch && touchMap[has](type)) {
2658 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2659 if (e.targetTouches[i].target == obj) {
2660 var olde = e;
2661 e = e.targetTouches[i];
2662 e.originalEvent = olde;
2663 e.preventDefault = preventTouch;
2664 e.stopPropagation = stopTouch;
2665 break;
2666 }
2667 }
2668 }
2669 return fn.call(element, e);
2670 };
2671 obj.addEventListener(realName, f, false);
2672 return function () {
2673 obj.removeEventListener(realName, f, false);
2674 return true;
2675 };
2676 };
2677 } else if (doc.attachEvent) {
2678 return function (obj, type, fn, element) {
2679 var f = function (e) {
2680 e = e || win.event;
2681 e.preventDefault = e.preventDefault || preventDefault;
2682 e.stopPropagation = e.stopPropagation || stopPropagation;
2683 return fn.call(element, e);
2684 };
2685 obj.attachEvent("on" + type, f);
2686 var detacher = function () {
2687 obj.detachEvent("on" + type, f);
2688 return true;
2689 };
2690 return detacher;
2691 };
2692 }
2693 })(),
2694 drag = [],
2695 dragMove = function (e) {
2696 var x = e.clientX,
2697 y = e.clientY,
2698 scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2699 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2700 dragi,
2701 j = drag.length;
2702 while (j--) {
2703 dragi = drag[j];
2704 if (supportsTouch) {
2705 var i = e.touches.length,
2706 touch;
2707 while (i--) {
2708 touch = e.touches[i];
2709 if (touch.identifier == dragi.el._drag.id) {
2710 x = touch.clientX;
2711 y = touch.clientY;
2712 (e.originalEvent ? e.originalEvent : e).preventDefault();
2713 break;
2714 }
2715 }
2716 } else {
2717 e.preventDefault();
2718 }
2719 x += scrollX;
2720 y += scrollY;
2721 dragi.move && dragi.move.call(dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
2722 }
2723 },
2724 dragUp = function (e) {
2725 R.unmousemove(dragMove).unmouseup(dragUp);
2726 var i = drag.length,
2727 dragi;
2728 while (i--) {
2729 dragi = drag[i];
2730 dragi.el._drag = {};
2731 dragi.end && dragi.end.call(dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
2732 }
2733 drag = [];
2734 };
2735 for (var i = events[length]; i--;) {
2736 (function (eventName) {
2737 R[eventName] = Element[proto][eventName] = function (fn, scope) {
2738 if (R.is(fn, "function")) {
2739 this.events = this.events || [];
2740 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, scope || this)});
2741 }
2742 return this;
2743 };
2744 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2745 var events = this.events,
2746 l = events[length];
2747 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2748 events[l].unbind();
2749 events.splice(l, 1);
2750 !events.length && delete this.events;
2751 return this;
2752 }
2753 return this;
2754 };
2755 })(events[i]);
2756 }
2757 elproto.hover = function (f_in, f_out, scope_in, scope_out) {
2758 return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
2759 };
2760 elproto.unhover = function (f_in, f_out) {
2761 return this.unmouseover(f_in).unmouseout(f_out);
2762 };
2763 elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
2764 this._drag = {};
2765 this.mousedown(function (e) {
2766 (e.originalEvent || e).preventDefault();
2767 var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2768 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2769 this._drag.x = e.clientX + scrollX;
2770 this._drag.y = e.clientY + scrollY;
2771 this._drag.id = e.identifier;
2772 onstart && onstart.call(start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e);
2773 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2774 drag.push({el: this, move: onmove, end: onend, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
2775 });
2776 return this;
2777 };
2778 elproto.undrag = function (onmove, onstart, onend) {
2779 var i = drag.length;
2780 while (i--) {
2781 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i++, 1);
2782 }
2783 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2784 };
2785 paperproto.circle = function (x, y, r) {
2786 return theCircle(this, x || 0, y || 0, r || 0);
2787 };
2788 paperproto.rect = function (x, y, w, h, r) {
2789 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2790 };
2791 paperproto.ellipse = function (x, y, rx, ry) {
2792 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2793 };
2794 paperproto.path = function (pathString) {
2795 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2796 return thePath(R.format[apply](R, arguments), this);
2797 };
2798 paperproto.image = function (src, x, y, w, h) {
2799 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2800 };
2801 paperproto.text = function (x, y, text) {
2802 return theText(this, x || 0, y || 0, Str(text));
2803 };
2804 paperproto.set = function (itemsArray) {
2805 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2806 return new Set(itemsArray);
2807 };
2808 paperproto.setSize = setSize;
2809 paperproto.top = paperproto.bottom = null;
2810 paperproto.raphael = R;
2811 function x_y() {
2812 return this.x + S + this.y;
2813 }
2814 elproto.resetScale = function () {
2815 if (this.removed) {
2816 return this;
2817 }
2818 this._.sx = 1;
2819 this._.sy = 1;
2820 this.attrs.scale = "1 1";
2821 };
2822 elproto.scale = function (x, y, cx, cy) {
2823 if (this.removed) {
2824 return this;
2825 }
2826 if (x == null && y == null) {
2827 return {
2828 x: this._.sx,
2829 y: this._.sy,
2830 toString: x_y
2831 };
2832 }
2833 y = y || x;
2834 !+y && (y = x);
2835 var dx,
2836 dy,
2837 dcx,
2838 dcy,
2839 a = this.attrs;
2840 if (x != 0) {
2841 var bb = this.getBBox(),
2842 rcx = bb.x + bb.width / 2,
2843 rcy = bb.y + bb.height / 2,
2844 kx = abs(x / this._.sx),
2845 ky = abs(y / this._.sy);
2846 cx = (+cx || cx == 0) ? cx : rcx;
2847 cy = (+cy || cy == 0) ? cy : rcy;
2848 var posx = this._.sx > 0,
2849 posy = this._.sy > 0,
2850 dirx = ~~(x / abs(x)),
2851 diry = ~~(y / abs(y)),
2852 dkx = kx * dirx,
2853 dky = ky * diry,
2854 s = this.node.style,
2855 ncx = cx + abs(rcx - cx) * dkx * (rcx > cx == posx ? 1 : -1),
2856 ncy = cy + abs(rcy - cy) * dky * (rcy > cy == posy ? 1 : -1),
2857 fr = (x * dirx > y * diry ? ky : kx);
2858 switch (this.type) {
2859 case "rect":
2860 case "image":
2861 var neww = a.width * kx,
2862 newh = a.height * ky;
2863 this.attr({
2864 height: newh,
2865 r: a.r * fr,
2866 width: neww,
2867 x: ncx - neww / 2,
2868 y: ncy - newh / 2
2869 });
2870 break;
2871 case "circle":
2872 case "ellipse":
2873 this.attr({
2874 rx: a.rx * kx,
2875 ry: a.ry * ky,
2876 r: a.r * fr,
2877 cx: ncx,
2878 cy: ncy
2879 });
2880 break;
2881 case "text":
2882 this.attr({
2883 x: ncx,
2884 y: ncy
2885 });
2886 break;
2887 case "path":
2888 var path = pathToRelative(a.path),
2889 skip = true,
2890 fx = posx ? dkx : kx,
2891 fy = posy ? dky : ky;
2892 for (var i = 0, ii = path[length]; i < ii; i++) {
2893 var p = path[i],
2894 P0 = upperCase.call(p[0]);
2895 if (P0 == "M" && skip) {
2896 continue;
2897 } else {
2898 skip = false;
2899 }
2900 if (P0 == "A") {
2901 p[path[i][length] - 2] *= fx;
2902 p[path[i][length] - 1] *= fy;
2903 p[1] *= kx;
2904 p[2] *= ky;
2905 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2906 } else if (P0 == "H") {
2907 for (var j = 1, jj = p[length]; j < jj; j++) {
2908 p[j] *= fx;
2909 }
2910 } else if (P0 == "V") {
2911 for (j = 1, jj = p[length]; j < jj; j++) {
2912 p[j] *= fy;
2913 }
2914 } else {
2915 for (j = 1, jj = p[length]; j < jj; j++) {
2916 p[j] *= (j % 2) ? fx : fy;
2917 }
2918 }
2919 }
2920 var dim2 = pathDimensions(path);
2921 dx = ncx - dim2.x - dim2.width / 2;
2922 dy = ncy - dim2.y - dim2.height / 2;
2923 path[0][1] += dx;
2924 path[0][2] += dy;
2925 this.attr({path: path});
2926 break;
2927 }
2928 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2929 if (this.transformations) {
2930 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2931 this.node[setAttribute]("transform", this.transformations[join](S));
2932 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2933 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2934 this.attr({x: dx, y: dy});
2935 a.fx = dirx - 1;
2936 a.fy = diry - 1;
2937 } else {
2938 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2939 ", M12=0, M21=0, M22=", diry,
2940 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2941 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2942 }
2943 } else {
2944 if (this.transformations) {
2945 this.transformations[2] = E;
2946 this.node[setAttribute]("transform", this.transformations[join](S));
2947 a.fx = 0;
2948 a.fy = 0;
2949 } else {
2950 this.node.filterMatrix = E;
2951 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2952 }
2953 }
2954 a.scale = [x, y, cx, cy][join](S);
2955 this._.sx = x;
2956 this._.sy = y;
2957 }
2958 return this;
2959 };
2960 elproto.clone = function () {
2961 if (this.removed) {
2962 return null;
2963 }
2964 var attr = this.attr();
2965 delete attr.scale;
2966 delete attr.translation;
2967 return this.paper[this.type]().attr(attr);
2968 };
2969 var curveslengths = {},
2970 getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2971 var len = 0,
2972 precision = 100,
2973 name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
2974 cache = curveslengths[name],
2975 old, dot;
2976 !cache && (curveslengths[name] = cache = {data: []});
2977 cache.timer && clearTimeout(cache.timer);
2978 cache.timer = setTimeout(function () {delete curveslengths[name];}, 2000);
2979 if (length != null) {
2980 var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
2981 precision = ~~total * 10;
2982 }
2983 for (var i = 0; i < precision + 1; i++) {
2984 if (cache.data[length] > i) {
2985 dot = cache.data[i * precision];
2986 } else {
2987 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
2988 cache.data[i] = dot;
2989 }
2990 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2991 if (length != null && len >= length) {
2992 return dot;
2993 }
2994 old = dot;
2995 }
2996 if (length == null) {
2997 return len;
2998 }
2999 },
3000 getLengthFactory = function (istotal, subpath) {
3001 return function (path, length, onlystart) {
3002 path = path2curve(path);
3003 var x, y, p, l, sp = "", subpaths = {}, point,
3004 len = 0;
3005 for (var i = 0, ii = path.length; i < ii; i++) {
3006 p = path[i];
3007 if (p[0] == "M") {
3008 x = +p[1];
3009 y = +p[2];
3010 } else {
3011 l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
3012 if (len + l > length) {
3013 if (subpath && !subpaths.start) {
3014 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3015 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
3016 if (onlystart) {return sp;}
3017 subpaths.start = sp;
3018 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
3019 len += l;
3020 x = +p[5];
3021 y = +p[6];
3022 continue;
3023 }
3024 if (!istotal && !subpath) {
3025 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3026 return {x: point.x, y: point.y, alpha: point.alpha};
3027 }
3028 }
3029 len += l;
3030 x = +p[5];
3031 y = +p[6];
3032 }
3033 sp += p;
3034 }
3035 subpaths.end = sp;
3036 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
3037 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
3038 return point;
3039 };
3040 };
3041 var getTotalLength = getLengthFactory(1),
3042 getPointAtLength = getLengthFactory(),
3043 getSubpathsAtLength = getLengthFactory(0, 1);
3044 elproto.getTotalLength = function () {
3045 if (this.type != "path") {return;}
3046 if (this.node.getTotalLength) {
3047 return this.node.getTotalLength();
3048 }
3049 return getTotalLength(this.attrs.path);
3050 };
3051 elproto.getPointAtLength = function (length) {
3052 if (this.type != "path") {return;}
3053 return getPointAtLength(this.attrs.path, length);
3054 };
3055 elproto.getSubpath = function (from, to) {
3056 if (this.type != "path") {return;}
3057 if (abs(this.getTotalLength() - to) < "1e-6") {
3058 return getSubpathsAtLength(this.attrs.path, from).end;
3059 }
3060 var a = getSubpathsAtLength(this.attrs.path, to, 1);
3061 return from ? getSubpathsAtLength(a, from).end : a;
3062 };
3063
3064 // animation easing formulas
3065 R.easing_formulas = {
3066 linear: function (n) {
3067 return n;
3068 },
3069 "<": function (n) {
3070 return pow(n, 3);
3071 },
3072 ">": function (n) {
3073 return pow(n - 1, 3) + 1;
3074 },
3075 "<>": function (n) {
3076 n = n * 2;
3077 if (n < 1) {
3078 return pow(n, 3) / 2;
3079 }
3080 n -= 2;
3081 return (pow(n, 3) + 2) / 2;
3082 },
3083 backIn: function (n) {
3084 var s = 1.70158;
3085 return n * n * ((s + 1) * n - s);
3086 },
3087 backOut: function (n) {
3088 n = n - 1;
3089 var s = 1.70158;
3090 return n * n * ((s + 1) * n + s) + 1;
3091 },
3092 elastic: function (n) {
3093 if (n == 0 || n == 1) {
3094 return n;
3095 }
3096 var p = .3,
3097 s = p / 4;
3098 return pow(2, -10 * n) * math.sin((n - s) * (2 * PI) / p) + 1;
3099 },
3100 bounce: function (n) {
3101 var s = 7.5625,
3102 p = 2.75,
3103 l;
3104 if (n < (1 / p)) {
3105 l = s * n * n;
3106 } else {
3107 if (n < (2 / p)) {
3108 n -= (1.5 / p);
3109 l = s * n * n + .75;
3110 } else {
3111 if (n < (2.5 / p)) {
3112 n -= (2.25 / p);
3113 l = s * n * n + .9375;
3114 } else {
3115 n -= (2.625 / p);
3116 l = s * n * n + .984375;
3117 }
3118 }
3119 }
3120 return l;
3121 }
3122 };
3123
3124 var animationElements = [],
3125 animation = function () {
3126 var Now = +new Date;
3127 for (var l = 0; l < animationElements[length]; l++) {
3128 var e = animationElements[l];
3129 if (e.stop || e.el.removed) {
3130 continue;
3131 }
3132 var time = Now - e.start,
3133 ms = e.ms,
3134 easing = e.easing,
3135 from = e.from,
3136 diff = e.diff,
3137 to = e.to,
3138 t = e.t,
3139 that = e.el,
3140 set = {},
3141 now;
3142 if (time < ms) {
3143 var pos = easing(time / ms);
3144 for (var attr in from) if (from[has](attr)) {
3145 switch (availableAnimAttrs[attr]) {
3146 case "along":
3147 now = pos * ms * diff[attr];
3148 to.back && (now = to.len - now);
3149 var point = getPointAtLength(to[attr], now);
3150 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3151 diff.x = point.x;
3152 diff.y = point.y;
3153 that.translate(point.x - diff.sx, point.y - diff.sy);
3154 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3155 break;
3156 case nu:
3157 now = +from[attr] + pos * ms * diff[attr];
3158 break;
3159 case "colour":
3160 now = "rgb(" + [
3161 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3162 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3163 upto255(round(from[attr].b + pos * ms * diff[attr].b))
3164 ][join](",") + ")";
3165 break;
3166 case "path":
3167 now = [];
3168 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3169 now[i] = [from[attr][i][0]];
3170 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3171 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3172 }
3173 now[i] = now[i][join](S);
3174 }
3175 now = now[join](S);
3176 break;
3177 case "csv":
3178 switch (attr) {
3179 case "translation":
3180 var x = pos * ms * diff[attr][0] - t.x,
3181 y = pos * ms * diff[attr][1] - t.y;
3182 t.x += x;
3183 t.y += y;
3184 now = x + S + y;
3185 break;
3186 case "rotation":
3187 now = +from[attr][0] + pos * ms * diff[attr][0];
3188 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3189 break;
3190 case "scale":
3191 now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in to[attr] ? to[attr][2] : E), (3 in to[attr] ? to[attr][3] : E)][join](S);
3192 break;
3193 case "clip-rect":
3194 now = [];
3195 i = 4;
3196 while (i--) {
3197 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3198 }
3199 break;
3200 }
3201 break;
3202 default:
3203 var from2 = [].concat(from[attr]);
3204 now = [];
3205 i = that.paper.customAttributes[attr].length;
3206 while (i--) {
3207 now[i] = +from2[i] + pos * ms * diff[attr][i];
3208 }
3209 break;
3210 }
3211 set[attr] = now;
3212 }
3213 that.attr(set);
3214 that._run && that._run.call(that);
3215 } else {
3216 if (to.along) {
3217 point = getPointAtLength(to.along, to.len * !to.back);
3218 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3219 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3220 }
3221 (t.x || t.y) && that.translate(-t.x, -t.y);
3222 to.scale && (to.scale += E);
3223 that.attr(to);
3224 animationElements.splice(l--, 1);
3225 }
3226 }
3227 R.svg && that && that.paper && that.paper.safari();
3228 animationElements[length] && setTimeout(animation);
3229 },
3230 keyframesRun = function (attr, element, time, prev, prevcallback) {
3231 var dif = time - prev;
3232 element.timeouts.push(setTimeout(function () {
3233 R.is(prevcallback, "function") && prevcallback.call(element);
3234 element.animate(attr, dif, attr.easing);
3235 }, prev));
3236 },
3237 upto255 = function (color) {
3238 return mmax(mmin(color, 255), 0);
3239 },
3240 translate = function (x, y) {
3241 if (x == null) {
3242 return {x: this._.tx, y: this._.ty, toString: x_y};
3243 }
3244 this._.tx += +x;
3245 this._.ty += +y;
3246 switch (this.type) {
3247 case "circle":
3248 case "ellipse":
3249 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3250 break;
3251 case "rect":
3252 case "image":
3253 case "text":
3254 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3255 break;
3256 case "path":
3257 var path = pathToRelative(this.attrs.path);
3258 path[0][1] += +x;
3259 path[0][2] += +y;
3260 this.attr({path: path});
3261 break;
3262 }
3263 return this;
3264 };
3265 elproto.animateWith = function (element, params, ms, easing, callback) {
3266 for (var i = 0, ii = animationElements.length; i < ii; i++) {
3267 if (animationElements[i].el.id == element.id) {
3268 params.start = animationElements[i].start;
3269 }
3270 }
3271 return this.animate(params, ms, easing, callback);
3272 };
3273 elproto.animateAlong = along();
3274 elproto.animateAlongBack = along(1);
3275 function along(isBack) {
3276 return function (path, ms, rotate, callback) {
3277 var params = {back: isBack};
3278 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3279 path && path.constructor == Element && (path = path.attrs.path);
3280 path && (params.along = path);
3281 return this.animate(params, ms, callback);
3282 };
3283 }
3284 function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
3285 var cx = 3 * p1x,
3286 bx = 3 * (p2x - p1x) - cx,
3287 ax = 1 - cx - bx,
3288 cy = 3 * p1y,
3289 by = 3 * (p2y - p1y) - cy,
3290 ay = 1 - cy - by;
3291 function sampleCurveX(t) {
3292 return ((ax * t + bx) * t + cx) * t;
3293 }
3294 function solve(x, epsilon) {
3295 var t = solveCurveX(x, epsilon);
3296 return ((ay * t + by) * t + cy) * t;
3297 }
3298 function solveCurveX(x, epsilon) {
3299 var t0, t1, t2, x2, d2, i;
3300 for(t2 = x, i = 0; i < 8; i++) {
3301 x2 = sampleCurveX(t2) - x;
3302 if (abs(x2) < epsilon) {
3303 return t2;
3304 }
3305 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
3306 if (abs(d2) < 1e-6) {
3307 break;
3308 }
3309 t2 = t2 - x2 / d2;
3310 }
3311 t0 = 0;
3312 t1 = 1;
3313 t2 = x;
3314 if (t2 < t0) {
3315 return t0;
3316 }
3317 if (t2 > t1) {
3318 return t1;
3319 }
3320 while (t0 < t1) {
3321 x2 = sampleCurveX(t2);
3322 if (abs(x2 - x) < epsilon) {
3323 return t2;
3324 }
3325 if (x > x2) {
3326 t0 = t2;
3327 } else {
3328 t1 = t2;
3329 }
3330 t2 = (t1 - t0) / 2 + t0;
3331 }
3332 return t2;
3333 }
3334 return solve(t, 1 / (200 * duration));
3335 }
3336 elproto.onAnimation = function (f) {
3337 this._run = f || 0;
3338 return this;
3339 };
3340 elproto.animate = function (params, ms, easing, callback) {
3341 var element = this;
3342 element.timeouts = element.timeouts || [];
3343 if (R.is(easing, "function") || !easing) {
3344 callback = easing || null;
3345 }
3346 if (element.removed) {
3347 callback && callback.call(element);
3348 return element;
3349 }
3350 var from = {},
3351 to = {},
3352 animateable = false,
3353 diff = {};
3354 for (var attr in params) if (params[has](attr)) {
3355 if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
3356 animateable = true;
3357 from[attr] = element.attr(attr);
3358 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3359 to[attr] = params[attr];
3360 switch (availableAnimAttrs[attr]) {
3361 case "along":
3362 var len = getTotalLength(params[attr]);
3363 var point = getPointAtLength(params[attr], len * !!params.back);
3364 var bb = element.getBBox();
3365 diff[attr] = len / ms;
3366 diff.tx = bb.x;
3367 diff.ty = bb.y;
3368 diff.sx = point.x;
3369 diff.sy = point.y;
3370 to.rot = params.rot;
3371 to.back = params.back;
3372 to.len = len;
3373 params.rot && (diff.r = toFloat(element.rotate()) || 0);
3374 break;
3375 case nu:
3376 diff[attr] = (to[attr] - from[attr]) / ms;
3377 break;
3378 case "colour":
3379 from[attr] = R.getRGB(from[attr]);
3380 var toColour = R.getRGB(to[attr]);
3381 diff[attr] = {
3382 r: (toColour.r - from[attr].r) / ms,
3383 g: (toColour.g - from[attr].g) / ms,
3384 b: (toColour.b - from[attr].b) / ms
3385 };
3386 break;
3387 case "path":
3388 var pathes = path2curve(from[attr], to[attr]);
3389 from[attr] = pathes[0];
3390 var toPath = pathes[1];
3391 diff[attr] = [];
3392 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3393 diff[attr][i] = [0];
3394 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3395 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3396 }
3397 }
3398 break;
3399 case "csv":
3400 var values = Str(params[attr])[split](separator),
3401 from2 = Str(from[attr])[split](separator);
3402 switch (attr) {
3403 case "translation":
3404 from[attr] = [0, 0];
3405 diff[attr] = [values[0] / ms, values[1] / ms];
3406 break;
3407 case "rotation":
3408 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3409 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3410 break;
3411 case "scale":
3412 params[attr] = values;
3413 from[attr] = Str(from[attr])[split](separator);
3414 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3415 break;
3416 case "clip-rect":
3417 from[attr] = Str(from[attr])[split](separator);
3418 diff[attr] = [];
3419 i = 4;
3420 while (i--) {
3421 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3422 }
3423 break;
3424 }
3425 to[attr] = values;
3426 break;
3427 default:
3428 values = [].concat(params[attr]);
3429 from2 = [].concat(from[attr]);
3430 diff[attr] = [];
3431 i = element.paper.customAttributes[attr][length];
3432 while (i--) {
3433 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3434 }
3435 break;
3436 }
3437 }
3438 }
3439 if (!animateable) {
3440 var attrs = [],
3441 lastcall;
3442 for (var key in params) if (params[has](key) && animKeyFrames.test(key)) {
3443 attr = {value: params[key]};
3444 key == "from" && (key = 0);
3445 key == "to" && (key = 100);
3446 attr.key = toInt(key, 10);
3447 attrs.push(attr);
3448 }
3449 attrs.sort(sortByKey);
3450 if (attrs[0].key) {
3451 attrs.unshift({key: 0, value: element.attrs});
3452 }
3453 for (i = 0, ii = attrs[length]; i < ii; i++) {
3454 keyframesRun(attrs[i].value, element, ms / 100 * attrs[i].key, ms / 100 * (attrs[i - 1] && attrs[i - 1].key || 0), attrs[i - 1] && attrs[i - 1].value.callback);
3455 }
3456 lastcall = attrs[attrs[length] - 1].value.callback;
3457 if (lastcall) {
3458 element.timeouts.push(setTimeout(function () {lastcall.call(element);}, ms));
3459 }
3460 } else {
3461 var easyeasy = R.easing_formulas[easing];
3462 if (!easyeasy) {
3463 easyeasy = Str(easing).match(bezierrg);
3464 if (easyeasy && easyeasy[length] == 5) {
3465 var curve = easyeasy;
3466 easyeasy = function (t) {
3467 return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
3468 };
3469 } else {
3470 easyeasy = function (t) {
3471 return t;
3472 };
3473 }
3474 }
3475 animationElements.push({
3476 start: params.start || +new Date,
3477 ms: ms,
3478 easing: easyeasy,
3479 from: from,
3480 diff: diff,
3481 to: to,
3482 el: element,
3483 t: {x: 0, y: 0}
3484 });
3485 R.is(callback, "function") && (element._ac = setTimeout(function () {
3486 callback.call(element);
3487 }, ms));
3488 animationElements[length] == 1 && setTimeout(animation);
3489 }
3490 return this;
3491 };
3492 elproto.stop = function () {
3493 for (var i = 0; i < animationElements.length; i++) {
3494 animationElements[i].el.id == this.id && animationElements.splice(i--, 1);
3495 }
3496 for (i = 0, ii = this.timeouts && this.timeouts.length; i < ii; i++) {
3497 clearTimeout(this.timeouts[i]);
3498 }
3499 this.timeouts = [];
3500 clearTimeout(this._ac);
3501 delete this._ac;
3502 return this;
3503 };
3504 elproto.translate = function (x, y) {
3505 return this.attr({translation: x + " " + y});
3506 };
3507 elproto[toString] = function () {
3508 return "Rapha\xebl\u2019s object";
3509 };
3510 R.ae = animationElements;
3511
3512 // Set
3513 var Set = function (items) {
3514 this.items = [];
3515 this[length] = 0;
3516 this.type = "set";
3517 if (items) {
3518 for (var i = 0, ii = items[length]; i < ii; i++) {
3519 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3520 this[this.items[length]] = this.items[this.items[length]] = items[i];
3521 this[length]++;
3522 }
3523 }
3524 }
3525 };
3526 Set[proto][push] = function () {
3527 var item,
3528 len;
3529 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3530 item = arguments[i];
3531 if (item && (item.constructor == Element || item.constructor == Set)) {
3532 len = this.items[length];
3533 this[len] = this.items[len] = item;
3534 this[length]++;
3535 }
3536 }
3537 return this;
3538 };
3539 Set[proto].pop = function () {
3540 delete this[this[length]--];
3541 return this.items.pop();
3542 };
3543 for (var method in elproto) if (elproto[has](method)) {
3544 Set[proto][method] = (function (methodname) {
3545 return function () {
3546 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3547 this.items[i][methodname][apply](this.items[i], arguments);
3548 }
3549 return this;
3550 };
3551 })(method);
3552 }
3553 Set[proto].attr = function (name, value) {
3554 if (name && R.is(name, array) && R.is(name[0], "object")) {
3555 for (var j = 0, jj = name[length]; j < jj; j++) {
3556 this.items[j].attr(name[j]);
3557 }
3558 } else {
3559 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3560 this.items[i].attr(name, value);
3561 }
3562 }
3563 return this;
3564 };
3565 Set[proto].animate = function (params, ms, easing, callback) {
3566 (R.is(easing, "function") || !easing) && (callback = easing || null);
3567 var len = this.items[length],
3568 i = len,
3569 item,
3570 set = this,
3571 collector;
3572 callback && (collector = function () {
3573 !--len && callback.call(set);
3574 });
3575 easing = R.is(easing, string) ? easing : collector;
3576 item = this.items[--i].animate(params, ms, easing, collector);
3577 while (i--) {
3578 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3579 }
3580 return this;
3581 };
3582 Set[proto].insertAfter = function (el) {
3583 var i = this.items[length];
3584 while (i--) {
3585 this.items[i].insertAfter(el);
3586 }
3587 return this;
3588 };
3589 Set[proto].getBBox = function () {
3590 var x = [],
3591 y = [],
3592 w = [],
3593 h = [];
3594 for (var i = this.items[length]; i--;) {
3595 var box = this.items[i].getBBox();
3596 x[push](box.x);
3597 y[push](box.y);
3598 w[push](box.x + box.width);
3599 h[push](box.y + box.height);
3600 }
3601 x = mmin[apply](0, x);
3602 y = mmin[apply](0, y);
3603 return {
3604 x: x,
3605 y: y,
3606 width: mmax[apply](0, w) - x,
3607 height: mmax[apply](0, h) - y
3608 };
3609 };
3610 Set[proto].clone = function (s) {
3611 s = new Set;
3612 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3613 s[push](this.items[i].clone());
3614 }
3615 return s;
3616 };
3617
3618 R.registerFont = function (font) {
3619 if (!font.face) {
3620 return font;
3621 }
3622 this.fonts = this.fonts || {};
3623 var fontcopy = {
3624 w: font.w,
3625 face: {},
3626 glyphs: {}
3627 },
3628 family = font.face["font-family"];
3629 for (var prop in font.face) if (font.face[has](prop)) {
3630 fontcopy.face[prop] = font.face[prop];
3631 }
3632 if (this.fonts[family]) {
3633 this.fonts[family][push](fontcopy);
3634 } else {
3635 this.fonts[family] = [fontcopy];
3636 }
3637 if (!font.svg) {
3638 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3639 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3640 var path = font.glyphs[glyph];
3641 fontcopy.glyphs[glyph] = {
3642 w: path.w,
3643 k: {},
3644 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3645 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3646 }) + "z"
3647 };
3648 if (path.k) {
3649 for (var k in path.k) if (path[has](k)) {
3650 fontcopy.glyphs[glyph].k[k] = path.k[k];
3651 }
3652 }
3653 }
3654 }
3655 return font;
3656 };
3657 paperproto.getFont = function (family, weight, style, stretch) {
3658 stretch = stretch || "normal";
3659 style = style || "normal";
3660 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3661 if (!R.fonts) {
3662 return;
3663 }
3664 var font = R.fonts[family];
3665 if (!font) {
3666 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3667 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3668 if (name.test(fontName)) {
3669 font = R.fonts[fontName];
3670 break;
3671 }
3672 }
3673 }
3674 var thefont;
3675 if (font) {
3676 for (var i = 0, ii = font[length]; i < ii; i++) {
3677 thefont = font[i];
3678 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3679 break;
3680 }
3681 }
3682 }
3683 return thefont;
3684 };
3685 paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
3686 origin = origin || "middle"; // baseline|middle
3687 letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
3688 var out = this.set(),
3689 letters = Str(string)[split](E),
3690 shift = 0,
3691 path = E,
3692 scale;
3693 R.is(font, string) && (font = this.getFont(font));
3694 if (font) {
3695 scale = (size || 16) / font.face["units-per-em"];
3696 var bb = font.face.bbox.split(separator),
3697 top = +bb[0],
3698 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3699 for (var i = 0, ii = letters[length]; i < ii; i++) {
3700 var prev = i && font.glyphs[letters[i - 1]] || {},
3701 curr = font.glyphs[letters[i]];
3702 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
3703 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3704 }
3705 out.scale(scale, scale, top, height).translate(x - top, y - height);
3706 }
3707 return out;
3708 };
3709
3710 R.format = function (token, params) {
3711 var args = R.is(params, array) ? [0][concat](params) : arguments;
3712 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3713 return args[++i] == null ? E : args[i];
3714 }));
3715 return token || E;
3716 };
3717 R.ninja = function () {
3718 oldRaphael.was ? (win.Raphael = oldRaphael.is) : delete Raphael;
3719 return R;
3720 };
3721 R.el = elproto;
3722 R.st = Set[proto];
3723
3724 oldRaphael.was ? (win.Raphael = R) : (Raphael = R);
3725 })();