2 * Raphael 1.2 - JavaScript Vector Library
4 * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 window.Raphael = (function () {
10 var separator = /[, ]+/,
14 was: "Raphael" in win,
18 if (R.is(arguments[0], "array")) {
20 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
22 for (var i = 0, ii = a[length]; i < ii; i++) {
24 ({circle:1, rect:1, path:1, ellipse:1, text:1, image:1}[has](j.type)) && res[push](cnv[j.type]().attr(j));
28 return create[apply](R, arguments);
31 events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"],
34 has = "hasOwnProperty",
36 setAttribute = "setAttribute",
37 appendChild = "appendChild",
52 toString = "toString",
53 availableAttrs = {"clip-rect": "0 0 10e9 10e9", 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},
54 availableAnimAttrs = {"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},
57 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
58 R.svg = !(R.vml = R.type == "VML");
62 R.is = function (o, type) {
63 type = (type + E).toLowerCase();
64 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || Object[proto][toString].call(o)[rp](/^\[object\s+|\]$/gi, E).toLowerCase() == type;
66 R.setWindow = function (newwin) {
71 var toHex = function (color) {
73 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
74 toHex = cacher(function (color) {
77 var document = new ActiveXObject("htmlfile");
78 document.write("<body>");
82 bod = createPopup().document.body;
84 var range = bod.createTextRange();
85 bod.style.color = color;
86 var value = range.queryCommandValue("ForeColor");
87 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
88 return "#" + ("000000" + value[toString](16)).slice(-6);
91 var i = doc.createElement("i");
92 i.className = "Rapha\u00ebl colour picker";
93 i.style.cssText = "display:none";
94 doc.body[appendChild](i);
95 toHex = cacher(function (color) {
96 i.style.color = color;
97 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
102 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
103 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
111 if (brightness == 0) {
112 return {r: 0, g: 0, b: 0, hex: "#000"};
114 if (hue > 1 || saturation > 1 || brightness > 1) {
121 p = brightness * (1 - saturation),
122 q = brightness * (1 - (saturation * f)),
123 t = brightness * (1 - (saturation * (1 - f)));
124 red = [brightness, q, p, p, t, brightness, brightness][i];
125 green = [t, brightness, brightness, q, p, p, t][i];
126 blue = [p, p, t, brightness, brightness, q, p][i];
130 var rgb = {r: red, g: green, b: blue},
131 r = (~~red)[toString](16),
132 g = (~~green)[toString](16),
133 b = (~~blue)[toString](16);
137 rgb.hex = "#" + r + g + b;
140 R.rgb2hsb = cacher(function (red, green, blue) {
141 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
146 if (R.is(red, "string")) {
147 var clr = R.getRGB(red);
152 if (red > 1 || green > 1 || blue > 1) {
157 var max = mmax(red, green, blue),
158 min = mmin(red, green, blue),
163 return {h: 0, s: 0, b: max};
165 var delta = (max - min);
166 saturation = delta / max;
168 hue = (green - blue) / delta;
169 } else if (green == max) {
170 hue = 2 + ((blue - red) / delta);
172 hue = 4 + ((red - green) / delta);
178 return {h: hue, s: saturation, b: brightness};
180 R._path2string = function () {
183 for (var i = 0, ii = this[length]; i < ii; i++) {
184 for (var j = 0, jj = this[i][length]; j < jj; j++) {
186 j && j != jj - 1 && (res += ",");
188 i != ii - 1 && (res += S);
190 return res[rp](/,(?=-)/g, E);
192 function cacher(f, scope, postprocessor) {
194 var arg = Array[proto].splice.call(arguments, 0, arguments[length]),
195 args = arg[join]("\u25ba"),
196 cache = newf.cache = newf.cache || {},
197 count = newf.count = newf.count || [];
198 if (cache[has](args)) {
199 return postprocessor ? postprocessor(cache[args]) : cache[args];
201 count[length] >= 1e3 && delete cache[count.shift()];
203 cache[args] = f[apply](scope, arg);
204 return postprocessor ? postprocessor(cache[args]) : cache[args];
209 R.getRGB = cacher(function (colour) {
210 if (!colour || !!((colour + E).indexOf("-") + 1)) {
211 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
214 if (colour == "none") {
215 return {r: -1, g: -1, b: -1, hex: "none"};
217 !(({hs: 1, rg: 1}[has](colour.substring(0, 2)))) && (colour = toHex(colour));
222 rgb = colour.match(/^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i);
225 blue = toInt(rgb[2].substring(5), 16);
226 green = toInt(rgb[2].substring(3, 5), 16);
227 red = toInt(rgb[2].substring(1, 3), 16);
230 blue = toInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
231 green = toInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
232 red = toInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
235 rgb = rgb[4][split](/\s*,\s*/);
236 red = toFloat(rgb[0]);
237 green = toFloat(rgb[1]);
238 blue = toFloat(rgb[2]);
241 rgb = rgb[5][split](/\s*,\s*/);
242 red = toFloat(rgb[0]) * 2.55;
243 green = toFloat(rgb[1]) * 2.55;
244 blue = toFloat(rgb[2]) * 2.55;
247 rgb = rgb[6][split](/\s*,\s*/);
248 red = toFloat(rgb[0]);
249 green = toFloat(rgb[1]);
250 blue = toFloat(rgb[2]);
251 return R.hsb2rgb(red, green, blue);
254 rgb = rgb[7][split](/\s*,\s*/);
255 red = toFloat(rgb[0]) * 2.55;
256 green = toFloat(rgb[1]) * 2.55;
257 blue = toFloat(rgb[2]) * 2.55;
258 return R.hsb2rgb(red, green, blue);
260 rgb = {r: red, g: green, b: blue};
261 var r = (~~red)[toString](16),
262 g = (~~green)[toString](16),
263 b = (~~blue)[toString](16);
267 rgb.hex = "#" + r + g + b;
270 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
272 R.getColor = function (value) {
273 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
274 rgb = this.hsb2rgb(start.h, start.s, start.b);
279 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
283 R.getColor.reset = function () {
287 R.parsePathString = cacher(function (pathString) {
291 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
293 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
294 data = pathClone(pathString);
297 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
299 name = b.toLowerCase();
300 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
301 b && params[push](+b);
303 while (params[length] >= paramCounts[name]) {
304 data[push]([b][concat](params.splice(0, paramCounts[name])));
305 if (!paramCounts[name]) {
311 data[toString] = R._path2string;
314 var pathDimensions = cacher(function (path) {
316 return {x: 0, y: 0, width: 0, height: 0};
318 path = path2curve(path);
323 for (var i = 0, ii = path[length]; i < ii; i++) {
324 if (path[i][0] == "M") {
330 var dim = curveDim(x, y, path[i][1], path[i][2], path[i][3], path[i][4], path[i][5], path[i][6]);
331 X = X[concat](dim.min.x, dim.max.x);
332 Y = Y[concat](dim.min.y, dim.max.y);
335 var xmin = mmin[apply](0, X),
336 ymin = mmin[apply](0, Y);
340 width: mmax[apply](0, X) - xmin,
341 height: mmax[apply](0, Y) - ymin
344 pathClone = function (pathArray) {
346 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
347 pathArray = R.parsePathString(pathArray);
349 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
351 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
352 res[i][j] = pathArray[i][j];
355 res[toString] = R._path2string;
358 pathToRelative = cacher(function (pathArray) {
359 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
360 pathArray = R.parsePathString(pathArray);
368 if (pathArray[0][0] == "M") {
374 res[push](["M", x, y]);
376 for (var i = start, ii = pathArray[length]; i < ii; i++) {
379 if (pa[0] != pa[0].toLowerCase()) {
380 r[0] = pa[0].toLowerCase();
388 r[6] = +(pa[6] - x).toFixed(3);
389 r[7] = +(pa[7] - y).toFixed(3);
392 r[1] = +(pa[1] - y).toFixed(3);
398 for (var j = 1, jj = pa[length]; j < jj; j++) {
399 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
408 for (var k = 0, kk = pa[length]; k < kk; k++) {
412 var len = res[i][length];
419 x += +res[i][len - 1];
422 y += +res[i][len - 1];
425 x += +res[i][len - 2];
426 y += +res[i][len - 1];
429 res[toString] = R._path2string;
432 pathToAbsolute = cacher(function (pathArray) {
433 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
434 pathArray = R.parsePathString(pathArray);
442 if (pathArray[0][0] == "M") {
443 x = +pathArray[0][1];
444 y = +pathArray[0][2];
448 res[0] = ["M", x, y];
450 for (var i = start, ii = pathArray[length]; i < ii; i++) {
453 if (pa[0] != (pa[0] + E).toUpperCase()) {
454 r[0] = (pa[0] + E).toUpperCase();
475 for (var j = 1, jj = pa[length]; j < jj; j++) {
476 r[j] = +pa[j] + ((j % 2) ? x : y);
480 for (var k = 0, kk = pa[length]; k < kk; k++) {
496 x = res[i][res[i][length] - 2];
497 y = res[i][res[i][length] - 1];
500 res[toString] = R._path2string;
503 l2c = function (x1, y1, x2, y2) {
504 return [x1, y1, x2, y2, x2, y2];
506 q2c = function (x1, y1, ax, ay, x2, y2) {
518 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
519 // for more information of where this math came from visit:
520 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
522 _120 = PI * 120 / 180,
523 rad = PI / 180 * (+angle || 0),
526 rotate = cacher(function (x, y, rad) {
527 var X = x * Math.cos(rad) - y * Math.sin(rad),
528 Y = x * Math.sin(rad) + y * Math.cos(rad);
532 xy = rotate(x1, y1, -rad);
535 xy = rotate(x2, y2, -rad);
538 var cos = Math.cos(PI / 180 * angle),
539 sin = Math.sin(PI / 180 * angle),
542 rx = mmax(rx, Math.abs(x));
543 ry = mmax(ry, Math.abs(y));
546 k = (large_arc_flag == sweep_flag ? -1 : 1) *
547 Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
548 cx = k * rx * y / ry + (x1 + x2) / 2,
549 cy = k * -ry * x / rx + (y1 + y2) / 2,
550 f1 = Math.asin((y1 - cy) / ry),
551 f2 = Math.asin((y2 - cy) / ry);
553 f1 = x1 < cx ? PI - f1 : f1;
554 f2 = x2 < cx ? PI - f2 : f2;
555 f1 < 0 && (f1 = PI * 2 + f1);
556 f2 < 0 && (f2 = PI * 2 + f2);
557 if (sweep_flag && f1 > f2) {
560 if (!sweep_flag && f2 > f1) {
570 if (Math.abs(df) > _120) {
574 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
575 x2 = cx + rx * Math.cos(f2);
576 y2 = cy + ry * Math.sin(f2);
577 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
580 var c1 = Math.cos(f1),
584 t = Math.tan(df / 4),
588 m2 = [x1 + hx * s1, y1 - hy * c1],
589 m3 = [x2 + hx * s2, y2 - hy * c2],
591 m2[0] = 2 * m1[0] - m2[0];
592 m2[1] = 2 * m1[1] - m2[1];
594 return [m2, m3, m4][concat](res);
596 res = [m2, m3, m4][concat](res)[join](",")[split](",");
598 for (var i = 0, ii = res[length]; i < ii; i++) {
599 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
604 findDotAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
605 var x = pow(1 - t, 3) * p1x + pow(1 - t, 2) * 3 * t * c1x + (1 - t) * 3 * t * t * c2x + pow(t, 3) * p2x,
606 y = pow(1 - t, 3) * p1y + pow(1 - t, 2) * 3 * t * c1y + (1 - t) * 3 * t * t * c2y + pow(t, 3) * p2y,
607 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
608 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
609 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
610 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
611 ax = (1 - t) * p1x + t * c1x,
612 ay = (1 - t) * p1y + t * c1y,
613 cx = (1 - t) * c2x + t * p2x,
614 cy = (1 - t) * c2y + t * p2y;
615 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}};
617 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
618 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
619 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
621 t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a,
622 t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a,
625 dot1 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1 > 0 && t1 < 1 ? t1 : 0),
626 dot2 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2 > 0 && t2 < 1 ? t2 : 0);
627 x = x[concat](dot1.x, dot2.x);
628 y = y[concat](dot1.y, dot2.y);
629 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
630 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
632 t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a;
633 t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a;
634 dot1 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1 > 0 && t1 < 1 ? t1 : 0);
635 dot2 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2 > 0 && t2 < 1 ? t2 : 0);
636 x = x[concat](dot1.x, dot2.x);
637 y = y[concat](dot1.y, dot2.y);
639 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
640 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
643 path2curve = cacher(function (path, path2) {
644 var p = pathToAbsolute(path),
645 p2 = path2 && pathToAbsolute(path2),
646 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
647 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
648 processPath = function (path, d) {
651 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
653 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
660 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
663 nx = d.x + (d.x - (d.bx || d.x));
664 ny = d.y + (d.y - (d.by || d.y));
665 path = ["C", nx, ny][concat](path.slice(1));
668 d.qx = d.x + (d.x - (d.qx || d.x));
669 d.qy = d.y + (d.y - (d.qy || d.y));
670 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
675 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
678 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
681 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
684 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
687 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
692 fixArc = function (pp, i) {
693 if (pp[i][length] > 7) {
697 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
700 ii = mmax(p[length], p2 && p2[length] || 0);
703 fixM = function (path1, path2, a1, a2, i) {
704 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
705 path2.splice(i, 0, ["M", a2.x, a2.y]);
710 ii = mmax(p[length], p2 && p2[length] || 0);
713 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
714 p[i] = processPath(p[i], attrs);
716 p2 && (p2[i] = processPath(p2[i], attrs2));
718 fixM(p, p2, attrs, attrs2, i);
719 fixM(p2, p, attrs2, attrs, i);
722 seglen = seg[length],
723 seg2len = p2 && seg2[length];
724 attrs.x = seg[seglen - 2];
725 attrs.y = seg[seglen - 1];
726 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
727 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
728 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
729 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
730 attrs2.x = p2 && seg2[seg2len - 2];
731 attrs2.y = p2 && seg2[seg2len - 1];
733 return p2 ? [p, p2] : p;
735 parseDots = cacher(function (gradient) {
737 for (var i = 0, ii = gradient[length]; i < ii; i++) {
739 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
740 dot.color = R.getRGB(par[1]);
741 if (dot.color.error) {
744 dot.color = dot.color.hex;
745 par[2] && (dot.offset = par[2] + "%");
748 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
749 if (!dots[i].offset) {
750 var start = toFloat(dots[i - 1].offset || 0),
752 for (var j = i + 1; j < ii; j++) {
753 if (dots[j].offset) {
754 end = dots[j].offset;
763 var d = (end - start) / (j - i + 1);
766 dots[i].offset = start + "%";
772 getContainer = function () {
778 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
779 if (R.is(arguments[0], "string")) {
780 container = doc.getElementById(arguments[0]);
782 container = arguments[0];
784 if (container.tagName) {
785 if (arguments[1] == null) {
787 container: container,
788 width: container.style.pixelWidth || container.offsetWidth,
789 height: container.style.pixelHeight || container.offsetHeight
792 return {container: container, width: arguments[1], height: arguments[2]};
795 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
796 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
799 plugins = function (con, add) {
801 for (var prop in add) if (add[has](prop) && !(prop in con)) {
802 switch (typeof add[prop]) {
805 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
809 con[prop] = con[prop] || {};
810 plugins.call(this, con[prop], add[prop]);
813 con[prop] = add[prop];
821 paper.svgns = "http://www.w3.org/2000/svg";
822 paper.xlink = "http://www.w3.org/1999/xlink";
823 var round = function (num) {
824 return +num + (~~num === num) * .5;
826 var roundPath = function (path) {
827 for (var i = 0, ii = path[length]; i < ii; i++) {
828 if (path[i][0].toLowerCase() != "a") {
829 for (var j = 1, jj = path[i][length]; j < jj; j++) {
830 path[i][j] = round(path[i][j]);
833 path[i][6] = round(path[i][6]);
834 path[i][7] = round(path[i][7]);
839 var $ = function (el, attr) {
841 for (var key in attr) if (attr[has](key)) {
842 el[setAttribute](key, attr[key]);
845 return doc.createElementNS(paper.svgns, el);
848 R[toString] = function () {
849 return "Your browser supports SVG.\nYou are running Rapha\u00ebl " + this.version;
851 var thePath = function (pathString, SVG) {
853 SVG.canvas && SVG.canvas[appendChild](el);
854 var p = new Element(el, SVG);
856 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
859 var addGradientFill = function (o, gradient, SVG) {
863 gradient = (gradient + E)[rp](/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, _fx, _fy) {
868 if (pow(fx - .5, 2) + pow(fy - .5, 2) > .25) {
869 fy = Math.sqrt(.25 - pow(fx - .5, 2)) + .5;
874 gradient = gradient[split](/\s*\-\s*/);
875 if (type == "linear") {
876 var angle = gradient.shift();
877 angle = -toFloat(angle);
881 var vector = [0, 0, Math.cos(angle * Math.PI / 180), Math.sin(angle * Math.PI / 180)],
882 max = 1 / (mmax(Math.abs(vector[2]), Math.abs(vector[3])) || 1);
886 vector[0] = -vector[2];
890 vector[1] = -vector[3];
894 var dots = parseDots(gradient);
898 var el = $(type + "Gradient");
899 el.id = "r" + (R._id++)[toString](36);
900 type == "radial" ? $(el, {fx: fx, fy: fy}) : $(el, {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
901 SVG.defs[appendChild](el);
902 for (var i = 0, ii = dots[length]; i < ii; i++) {
903 var stop = $("stop");
905 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
906 "stop-color": dots[i].color || "#fff"
908 el[appendChild](stop);
911 fill: "url(#" + el.id + ")",
920 var updatePosition = function (o) {
921 var bbox = o.getBBox();
922 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
924 var setFillAndStroke = function (o, params) {
931 "-..": [3, 1, 1, 1, 1, 1],
937 "--..": [8, 3, 1, 3, 1, 3]
942 addDashes = function (o, value) {
943 value = dasharray[(value + E).toLowerCase()];
945 var width = o.attrs["stroke-width"] || "1",
946 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
948 var i = value[length];
950 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
952 $(node, {"stroke-dasharray": dashes[join](",")});
955 toFloat(rot) && o.rotate(0, true);
956 for (var att in params) if (params[has](att)) {
957 if (!(att in availableAttrs)) {
960 var value = params[att];
967 var pn = node.parentNode;
968 if (pn.tagName.toLowerCase() != "a") {
970 pn.insertBefore(hl, node);
971 hl[appendChild](node);
974 pn.setAttributeNS(o.paper.xlink, att, value);
977 var rect = (value + E)[split](separator);
978 if (rect[length] == 4) {
979 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
980 var el = $("clipPath"),
982 el.id = "r" + (R._id++)[toString](36);
990 o.paper.defs[appendChild](el);
991 $(node, {"clip-path": "url(#" + el.id + ")"});
995 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
996 clip && clip.parentNode.removeChild(clip);
997 $(node, {"clip-path": E});
1002 if (value && o.type == "path") {
1003 attrs.path = roundPath(pathToAbsolute(value));
1004 $(node, {d: attrs.path});
1008 node[setAttribute](att, value);
1017 value = -attrs.x - (attrs.width || 0);
1020 if (att == "rx" && o.type == "rect") {
1024 node[setAttribute](att, round(value));
1025 o.pattern && updatePosition(o);
1028 node[setAttribute](att, value);
1037 value = -attrs.y - (attrs.height || 0);
1040 if (att == "ry" && o.type == "rect") {
1044 node[setAttribute](att, round(value));
1045 o.pattern && updatePosition(o);
1048 if (o.type == "rect") {
1049 $(node, {rx: value, ry: value});
1051 node[setAttribute](att, value);
1055 if (o.type == "image") {
1056 node.setAttributeNS(o.paper.xlink, "href", value);
1059 case "stroke-width":
1060 node.style.strokeWidth = value;
1061 // Need following line for Firefox
1062 node[setAttribute](att, value);
1063 if (attrs["stroke-dasharray"]) {
1064 addDashes(o, attrs["stroke-dasharray"]);
1067 case "stroke-dasharray":
1068 addDashes(o, value);
1072 o.rotate(value, true);
1075 var xy = (value + E)[split](separator);
1076 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
1079 var xy = (value + E)[split](separator);
1080 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1083 var isURL = (value + E).match(/^url\(['"]?([^\)]+)['"]?\)$/i);
1085 var el = $("pattern"),
1087 el.id = "r" + (R._id++)[toString](36);
1088 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse"});
1090 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1091 el[appendChild](ig);
1093 var img = doc.createElement("img");
1094 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1095 img.onload = function () {
1096 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1097 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1098 doc.body.removeChild(this);
1101 doc.body[appendChild](img);
1103 o.paper.defs[appendChild](el);
1104 node.style.fill = "url(#" + el.id + ")";
1105 $(node, {fill: "url(#" + el.id + ")"});
1107 o.pattern && updatePosition(o);
1110 if (!R.getRGB(value).error) {
1111 delete params.gradient;
1112 delete attrs.gradient;
1113 if (!R.is(attrs.opacity, "undefined") && R.is(params.opacity, "undefined") ) {
1114 node.style.opacity = attrs.opacity;
1115 // Need following line for Firefox
1116 $(node, {opacity: attrs.opacity});
1118 if (!R.is(attrs["fill-opacity"], "undefined") && R.is(params["fill-opacity"], "undefined") ) {
1119 node.style.fillOpacity = attrs["fill-opacity"];
1120 // Need following line for Firefox
1121 $(node, {"fill-opacity": attrs["fill-opacity"]});
1123 } else if ((o.type in {circle: 1, ellipse: 1} || (value + E).charAt(0) != "r") && addGradientFill(node, value, o.paper)) {
1124 attrs.gradient = value;
1125 attrs.fill = "none";
1129 node.style[att] = R.getRGB(value).hex;
1130 // Need following line for Firefox
1131 node[setAttribute](att, R.getRGB(value).hex);
1134 (o.type in {circle: 1, ellipse: 1} || (value + E).charAt(0) != "r") && addGradientFill(node, value, o.paper);
1137 case "fill-opacity":
1138 if (attrs.gradient) {
1139 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1141 var stops = gradient.getElementsByTagName("stop");
1142 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1147 att == "font-size" && (value = toInt(value, 10) + "px");
1148 var cssrule = att[rp](/(\-.)/g, function (w) {
1149 return w.substring(1).toUpperCase();
1151 node.style[cssrule] = value;
1152 // Need following line for Firefox
1153 node[setAttribute](att, value);
1158 tuneText(o, params);
1159 toFloat(rot) && o.rotate(rot, true);
1162 var tuneText = function (el, params) {
1163 if (el.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params || "y" in params)) {
1168 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1170 if ("text" in params) {
1171 while (node.firstChild) {
1172 node.removeChild(node.firstChild);
1174 var texts = (params.text + E)[split]("\n");
1175 for (var i = 0, ii = texts[length]; i < ii; i++) {
1176 var tspan = $("tspan");
1177 i && $(tspan, {dy: fontSize * leading, x: a.x});
1178 tspan[appendChild](doc.createTextNode(texts[i]));
1179 node[appendChild](tspan);
1182 var texts = node.getElementsByTagName("tspan");
1183 for (var i = 0, ii = texts[length]; i < ii; i++) {
1184 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1188 var bb = el.getBBox(),
1189 dif = a.y - (bb.y + bb.height / 2);
1190 dif && isFinite(dif) && $(node, {y: a.y + dif});
1192 var Element = function (node, svg) {
1198 node.raphael = this;
1200 this.attrs = this.attrs || {};
1201 this.transformations = []; // rotate, translate, scale
1205 rt: {deg: 0, cx: 0, cy: 0},
1210 Element[proto].rotate = function (deg, cx, cy) {
1216 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1218 return this._.rt.deg;
1220 var bbox = this.getBBox();
1221 deg = (deg + E)[split](separator);
1222 if (deg[length] - 1) {
1223 cx = toFloat(deg[1]);
1224 cy = toFloat(deg[2]);
1226 deg = toFloat(deg[0]);
1228 this._.rt.deg = deg;
1230 this._.rt.deg += deg;
1232 (cy == null) && (cx = null);
1235 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1236 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1237 if (this._.rt.deg) {
1238 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1239 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1241 this.transformations[0] = E;
1242 this.clip && $(this.clip, {transform: E});
1244 $(this.node, {transform: this.transformations[join](S)});
1247 Element[proto].hide = function () {
1248 !this.removed && (this.node.style.display = "none");
1251 Element[proto].show = function () {
1252 !this.removed && (this.node.style.display = "");
1255 Element[proto].remove = function () {
1256 this.node.parentNode.removeChild(this.node);
1257 for (var i in this) {
1260 this.removed = true;
1262 Element[proto].getBBox = function () {
1266 if (this.type == "path") {
1267 return pathDimensions(this.attrs.path);
1269 if (this.node.style.display == "none") {
1275 bbox = this.node.getBBox();
1277 // Firefox 3.0.x plays badly here
1281 if (this.type == "text") {
1282 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1283 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1284 var bb = this.node.getExtentOfChar(i);
1285 (bb.y < bbox.y) && (bbox.y = bb.y);
1286 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1287 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1290 hide && this.hide();
1293 Element[proto].attr = function () {
1297 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1298 if (arguments[0] == "translation") {
1299 return this.translate();
1301 if (arguments[0] == "rotation") {
1302 return this.rotate();
1304 if (arguments[0] == "scale") {
1305 return this.scale();
1307 return this.attrs[arguments[0]];
1309 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1311 for (var j in arguments[0]) if (arguments[0][has](j)) {
1312 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1316 if (arguments[length] == 2) {
1318 params[arguments[0]] = arguments[1];
1319 setFillAndStroke(this, params);
1320 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1321 setFillAndStroke(this, arguments[0]);
1325 Element[proto].toFront = function () {
1326 !this.removed && this.node.parentNode[appendChild](this.node);
1329 Element[proto].toBack = function () {
1333 if (this.node.parentNode.firstChild != this.node) {
1334 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1338 Element[proto].insertAfter = function (element) {
1342 if (element.node.nextSibling) {
1343 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1345 element.node.parentNode[appendChild](this.node);
1349 Element[proto].insertBefore = function (element) {
1353 var node = element.node;
1354 node.parentNode.insertBefore(this.node, node);
1358 var theCircle = function (svg, x, y, r) {
1361 var el = $("circle");
1362 svg.canvas && svg.canvas[appendChild](el);
1363 var res = new Element(el, svg);
1364 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1365 res.type = "circle";
1369 var theRect = function (svg, x, y, w, h, r) {
1373 svg.canvas && svg.canvas[appendChild](el);
1374 var res = new Element(el, svg);
1375 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1380 var theEllipse = function (svg, x, y, rx, ry) {
1383 var el = $("ellipse");
1384 svg.canvas && svg.canvas[appendChild](el);
1385 var res = new Element(el, svg);
1386 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1387 res.type = "ellipse";
1391 var theImage = function (svg, src, x, y, w, h) {
1392 var el = $("image");
1393 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1394 el.setAttributeNS(svg.xlink, "href", src);
1395 svg.canvas && svg.canvas[appendChild](el);
1396 var res = new Element(el, svg);
1397 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1401 var theText = function (svg, x, y, text) {
1403 $(el, {x: x, y: y, "text-anchor": "middle"});
1404 svg.canvas && svg.canvas[appendChild](el);
1405 var res = new Element(el, svg);
1406 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1408 setFillAndStroke(res, res.attrs);
1411 var setSize = function (width, height) {
1412 this.width = width || this.width;
1413 this.height = height || this.height;
1414 this.canvas[setAttribute]("width", this.width);
1415 this.canvas[setAttribute]("height", this.height);
1418 var create = function () {
1419 var con = getContainer[apply](null, arguments),
1420 container = con && con.container,
1424 height = con.height;
1426 throw new Error("SVG container not found.");
1428 paper.canvas = $("svg");
1429 var cnvs = paper.canvas;
1430 paper.width = width || 512;
1431 paper.height = height || 342;
1432 cnvs[setAttribute]("width", paper.width);
1433 cnvs[setAttribute]("height", paper.height);
1434 if (container == 1) {
1435 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1436 doc.body[appendChild](cnvs);
1438 if (container.firstChild) {
1439 container.insertBefore(cnvs, container.firstChild);
1441 container[appendChild](cnvs);
1444 container = { canvas: cnvs };
1445 for (var prop in paper) if (paper[has](prop)) {
1446 container[prop] = paper[prop];
1448 plugins.call(container, container, R.fn);
1450 container.raphael = R;
1453 paper.clear = function () {
1454 var c = this.canvas;
1455 while (c.firstChild) {
1456 c.removeChild(c.firstChild);
1458 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\u00ebl"));
1459 c[appendChild](this.desc);
1460 c[appendChild](this.defs = $("defs"));
1462 paper.remove = function () {
1463 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1464 for (var i in this) {
1472 var path2vml = function (path) {
1473 var total = /[ahqtv]/ig,
1474 command = pathToAbsolute;
1475 (path + E).match(total) && (command = path2curve);
1477 if (command == pathToAbsolute && !(path + E).match(total)) {
1478 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1479 bites = /([clmz]),?([^clmz]*)/gi,
1480 val = /-?[^,\s-]+/g;
1481 var res = (path + E)[rp](bites, function (all, command, args) {
1483 args[rp](val, function (value) {
1484 vals[push](round(value));
1486 return map[command] + vals;
1490 var pa = command(path), p, res = [], r;
1491 for (var i = 0, ii = pa[length]; i < ii; i++) {
1493 r = (pa[i][0] + E).toLowerCase();
1494 r == "z" && (r = "x");
1495 for (var j = 1, jj = p[length]; j < jj; j++) {
1496 r += round(p[j]) + (j != jj - 1 ? "," : E);
1500 return res[join](S);
1503 R[toString] = function () {
1504 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\u00ebl " + this.version;
1506 var thePath = function (pathString, VML) {
1507 var g = createNode("group");
1508 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1509 g.coordsize = VML.coordsize;
1510 g.coordorigin = VML.coordorigin;
1511 var el = createNode("shape"), ol = el.style;
1512 ol.width = VML.width + "px";
1513 ol.height = VML.height + "px";
1514 el.coordsize = this.coordsize;
1515 el.coordorigin = this.coordorigin;
1517 var p = new Element(el, g, VML);
1518 p.isAbsolute = true;
1522 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1523 VML.canvas[appendChild](g);
1526 var setFillAndStroke = function (o, params) {
1527 o.attrs = o.attrs || {};
1533 for (var par in params) if (params[has](par)) {
1534 a[par] = params[par];
1536 params.href && (node.href = params.href);
1537 params.title && (node.title = params.title);
1538 params.target && (node.target = params.target);
1539 if (params.path && o.type == "path") {
1540 a.path = params.path;
1541 node.path = path2vml(a.path);
1543 if (params.rotation != null) {
1544 o.rotate(params.rotation, true);
1546 if (params.translation) {
1547 xy = (params.translation + E)[split](separator);
1548 o.translate(xy[0], xy[1]);
1551 xy = (params.scale + E)[split](separator);
1552 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1554 if ("clip-rect" in params) {
1555 var rect = (params["clip-rect"] + E)[split](separator);
1556 if (rect[length] == 4) {
1557 rect[2] = +rect[2] + (+rect[0]);
1558 rect[3] = +rect[3] + (+rect[1]);
1559 var div = node.clipRect || doc.createElement("div"),
1561 group = node.parentNode;
1562 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1563 if (!node.clipRect) {
1564 dstyle.position = "absolute";
1567 dstyle.width = o.paper.width + "px";
1568 dstyle.height = o.paper.height + "px";
1569 group.parentNode.insertBefore(div, group);
1570 div[appendChild](group);
1571 node.clipRect = div;
1574 if (!params["clip-rect"]) {
1575 node.clipRect && (node.clipRect.style.clip = E);
1578 if (o.type == "image" && params.src) {
1579 node.src = params.src;
1581 if (o.type == "image" && params.opacity) {
1582 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1583 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1585 params.font && (s.font = params.font);
1586 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1587 params["font-size"] && (s.fontSize = params["font-size"]);
1588 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1589 params["font-style"] && (s.fontStyle = params["font-style"]);
1590 if (params.opacity != null ||
1591 params["stroke-width"] != null ||
1592 params.fill != null ||
1593 params.stroke != null ||
1594 params["stroke-width"] != null ||
1595 params["stroke-opacity"] != null ||
1596 params["fill-opacity"] != null ||
1597 params["stroke-dasharray"] != null ||
1598 params["stroke-miterlimit"] != null ||
1599 params["stroke-linejoin"] != null ||
1600 params["stroke-linecap"] != null) {
1601 node = o.shape || node;
1602 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1604 !fill && (newfill = fill = createNode("fill"));
1605 if ("fill-opacity" in params || "opacity" in params) {
1606 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1607 opacity < 0 && (opacity = 0);
1608 opacity > 1 && (opacity = 1);
1609 fill.opacity = opacity;
1611 params.fill && (fill.on = true);
1612 if (fill.on == null || params.fill == "none") {
1615 if (fill.on && params.fill) {
1616 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1618 fill.src = isURL[1];
1621 fill.color = R.getRGB(params.fill).hex;
1623 fill.type = "solid";
1624 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt(0) != "r") && addGradientFill(res, params.fill)) {
1626 a.gradient = params.fill;
1630 newfill && node[appendChild](fill);
1631 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1633 !stroke && (newstroke = stroke = createNode("stroke"));
1634 if ((params.stroke && params.stroke != "none") ||
1635 params["stroke-width"] ||
1636 params["stroke-opacity"] != null ||
1637 params["stroke-dasharray"] ||
1638 params["stroke-miterlimit"] ||
1639 params["stroke-linejoin"] ||
1640 params["stroke-linecap"]) {
1643 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1644 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1645 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1646 opacity < 0 && (opacity = 0);
1647 opacity > 1 && (opacity = 1);
1648 stroke.opacity = opacity;
1649 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1650 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1651 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1652 params["stroke-width"] && (stroke.weight = (toFloat(params["stroke-width"]) || 1) * 12 / 16);
1653 if (params["stroke-dasharray"]) {
1657 "-.": "shortdashdot",
1658 "-..": "shortdashdotdot",
1663 "--.": "longdashdot",
1664 "--..": "longdashdotdot"
1666 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || E;
1668 newstroke && node[appendChild](stroke);
1670 if (res.type == "text") {
1671 var s = res.paper.span.style;
1672 a.font && (s.font = a.font);
1673 a["font-family"] && (s.fontFamily = a["font-family"]);
1674 a["font-size"] && (s.fontSize = a["font-size"]);
1675 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1676 a["font-style"] && (s.fontStyle = a["font-style"]);
1677 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1678 res.W = a.w = res.paper.span.offsetWidth;
1679 res.H = a.h = res.paper.span.offsetHeight;
1681 res.Y = a.y + round(res.H / 2);
1683 // text-anchor emulationm
1684 switch (a["text-anchor"]) {
1686 res.node.style["v-text-align"] = "left";
1687 res.bbx = round(res.W / 2);
1690 res.node.style["v-text-align"] = "right";
1691 res.bbx = -round(res.W / 2);
1694 res.node.style["v-text-align"] = "center";
1699 var addGradientFill = function (o, gradient) {
1700 o.attrs = o.attrs || {};
1701 var attrs = o.attrs,
1702 fill = o.node.getElementsByTagName("fill"),
1705 o.attrs.gradient = gradient;
1706 gradient = (gradient + E)[rp](/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, fx, fy) {
1711 if (pow(fx - .5, 2) + pow(fy - .5, 2) > .25) {
1712 fy = Math.sqrt(.25 - pow(fx - .5, 2)) + .5;
1718 gradient = gradient[split](/\s*\-\s*/);
1719 if (type == "linear") {
1720 var angle = gradient.shift();
1721 angle = -toFloat(angle);
1726 var dots = parseDots(gradient);
1730 o = o.shape || o.node;
1731 fill = fill[0] || createNode("fill");
1734 fill.method = "none";
1735 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1736 fill.color = dots[0].color;
1737 fill.color2 = dots[dots[length] - 1].color;
1739 for (var i = 0, ii = dots[length]; i < ii; i++) {
1740 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1742 fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color;
1743 if (type == "radial") {
1744 fill.focus = "100%";
1745 fill.focussize = fxfy;
1746 fill.focusposition = fxfy;
1748 fill.angle = (270 - angle) % 360;
1753 var Element = function (node, group, vml) {
1761 node.raphael = this;
1775 Element[proto].rotate = function (deg, cx, cy) {
1781 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1783 return this._.rt.deg;
1785 deg = (deg + E)[split](separator);
1786 if (deg[length] - 1) {
1787 cx = toFloat(deg[1]);
1788 cy = toFloat(deg[2]);
1790 deg = toFloat(deg[0]);
1792 this._.rt.deg = deg;
1794 this._.rt.deg += deg;
1796 cy == null && (cx = null);
1799 this.setBox(this.attrs, cx, cy);
1800 this.Group.style.rotation = this._.rt.deg;
1801 // gradient fix for rotation. TODO
1802 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1803 // fill = fill[0] || {};
1804 // var b = ((360 - this._.rt.deg) - 270) % 360;
1805 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1808 Element[proto].setBox = function (params, cx, cy) {
1812 var gs = this.Group.style,
1813 os = (this.shape && this.shape.style) || this.node.style;
1814 params = params || {};
1815 for (var i in params) if (params[has](i)) {
1816 this.attrs[i] = params[i];
1818 cx = cx || this._.rt.cx;
1819 cy = cy || this._.rt.cy;
1820 var attr = this.attrs,
1825 switch (this.type) {
1827 x = attr.cx - attr.r;
1828 y = attr.cy - attr.r;
1832 x = attr.cx - attr.rx;
1833 y = attr.cy - attr.ry;
1841 w = attr.width || 0;
1842 h = attr.height || 0;
1845 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
1846 x = attr.x - round(this.W / 2);
1847 y = attr.y - this.H / 2;
1852 if (!this.attrs.path) {
1855 w = this.paper.width;
1856 h = this.paper.height;
1858 var dim = pathDimensions(this.attrs.path);
1868 w = this.paper.width;
1869 h = this.paper.height;
1872 cx = (cx == null) ? x + w / 2 : cx;
1873 cy = (cy == null) ? y + h / 2 : cy;
1874 var left = cx - this.paper.width / 2,
1875 top = cy - this.paper.height / 2;
1876 if (this.type == "path" || this.type == "text") {
1877 (gs.left != left + "px") && (gs.left = left + "px");
1878 (gs.top != top + "px") && (gs.top = top + "px");
1879 this.X = this.type == "text" ? x : -left;
1880 this.Y = this.type == "text" ? y : -top;
1883 (os.left != -left + "px") && (os.left = -left + "px");
1884 (os.top != -top + "px") && (os.top = -top + "px");
1886 (gs.left != left + "px") && (gs.left = left + "px");
1887 (gs.top != top + "px") && (gs.top = top + "px");
1892 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
1893 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
1894 (os.left != x - left + "px") && (os.left = x - left + "px");
1895 (os.top != y - top + "px") && (os.top = y - top + "px");
1896 (os.width != w + "px") && (os.width = w + "px");
1897 (os.height != h + "px") && (os.height = h + "px");
1898 var arcsize = (+params.r || 0) / (mmin(w, h));
1899 if (this.type == "rect" && this.arcsize != arcsize && (arcsize || this.arcsize)) {
1900 // We should replace element with the new one
1901 var o = createNode(arcsize ? "roundrect" : "rect");
1902 o.arcsize = arcsize;
1903 this.Group[appendChild](o);
1904 this.node.parentNode.removeChild(this.node);
1906 this.arcsize = arcsize;
1907 this.attr(this.attrs);
1911 Element[proto].hide = function () {
1912 !this.removed && (this.Group.style.display = "none");
1915 Element[proto].show = function () {
1916 !this.removed && (this.Group.style.display = "block");
1919 Element[proto].getBBox = function () {
1923 if (this.type == "path") {
1924 return pathDimensions(this.attrs.path);
1927 x: this.X + (this.bbx || 0),
1933 Element[proto].remove = function () {
1934 this.node.parentNode.removeChild(this[0]);
1935 this.Group.parentNode.removeChild(this.Group);
1936 this.shape && this.shape.parentNode.removeChild(this.shape);
1937 for (var i in this) {
1940 this.removed = true;
1942 Element[proto].attr = function () {
1946 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1947 if (arguments[0] == "translation") {
1948 return this.translate();
1950 if (arguments[0] == "rotation") {
1951 return this.rotate();
1953 if (arguments[0] == "scale") {
1954 return this.scale();
1956 return this.attrs[arguments[0]];
1958 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
1960 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
1961 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1966 if (arguments[length] == 2) {
1968 params[arguments[0]] = arguments[1];
1970 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
1972 if (params.text && this.type == "text") {
1973 this.node.string = params.text;
1975 setFillAndStroke(this, params);
1976 if (params.gradient && ({circle: 1, ellipse: 1}[has](this.type) || (params.gradient + E).charAt(0) != "r")) {
1977 addGradientFill(this, params.gradient);
1979 this.setBox(this.attrs);
1983 Element[proto].toFront = function () {
1984 !this.removed && this.Group.parentNode[appendChild](this.Group);
1987 Element[proto].toBack = function () {
1991 if (this.Group.parentNode.firstChild != this.Group) {
1992 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1996 Element[proto].insertAfter = function (element) {
2000 if (element.Group.nextSibling) {
2001 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2003 element.Group.parentNode[appendChild](this.Group);
2007 Element[proto].insertBefore = function (element) {
2008 !this.removed && element.Group.parentNode.insertBefore(this.Group, element.Group);
2012 var theCircle = function (vml, x, y, r) {
2013 var g = createNode("group"),
2014 o = createNode("oval"),
2016 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2017 g.coordsize = vml.coordsize;
2018 g.coordorigin = vml.coordorigin;
2020 var res = new Element(o, g, vml);
2021 res.type = "circle";
2022 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2026 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2027 vml.canvas[appendChild](g);
2030 var theRect = function (vml, x, y, w, h, r) {
2031 var g = createNode("group"),
2032 o = createNode(r ? "roundrect" : "rect"),
2033 arcsize = (+r || 0) / (mmin(w, h));
2034 o.arcsize = arcsize;
2035 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2036 g.coordsize = vml.coordsize;
2037 g.coordorigin = vml.coordorigin;
2039 var res = new Element(o, g, vml);
2041 setFillAndStroke(res, {stroke: "#000"});
2042 res.arcsize = arcsize;
2043 res.setBox({x: x, y: y, width: w, height: h, r: +r});
2044 vml.canvas[appendChild](g);
2047 var theEllipse = function (vml, x, y, rx, ry) {
2048 var g = createNode("group"),
2049 o = createNode("oval"),
2051 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2052 g.coordsize = vml.coordsize;
2053 g.coordorigin = vml.coordorigin;
2055 var res = new Element(o, g, vml);
2056 res.type = "ellipse";
2057 setFillAndStroke(res, {stroke: "#000"});
2062 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2063 vml.canvas[appendChild](g);
2066 var theImage = function (vml, src, x, y, w, h) {
2067 var g = createNode("group"),
2068 o = createNode("image"),
2070 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2071 g.coordsize = vml.coordsize;
2072 g.coordorigin = vml.coordorigin;
2075 var res = new Element(o, g, vml);
2077 res.attrs.src = src;
2082 res.setBox({x: x, y: y, width: w, height: h});
2083 vml.canvas[appendChild](g);
2086 var theText = function (vml, x, y, text) {
2087 var g = createNode("group"),
2088 el = createNode("shape"),
2090 path = createNode("path"),
2092 o = createNode("textpath");
2093 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2094 g.coordsize = vml.coordsize;
2095 g.coordorigin = vml.coordorigin;
2096 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2097 path.textpathok = true;
2098 ol.width = vml.width;
2099 ol.height = vml.height;
2100 o.string = text + E;
2103 el[appendChild](path);
2105 var res = new Element(o, g, vml);
2107 res.textpath = path;
2109 res.attrs.text = text;
2114 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2116 vml.canvas[appendChild](g);
2119 var setSize = function (width, height) {
2120 var cs = this.canvas.style;
2121 this.width = toFloat(width || this.width);
2122 this.height = toFloat(height || this.height);
2123 cs.width = this.width + "px";
2124 cs.height = this.height + "px";
2125 cs.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2126 this.coordsize = this.width + S + this.height;
2129 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2131 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2132 var createNode = function (tagName) {
2133 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2136 var createNode = function (tagName) {
2137 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2140 var create = function () {
2141 var con = getContainer[apply](null, arguments),
2142 container = con.container,
2143 height = con.height,
2149 throw new Error("VML container not found.");
2152 c = res.canvas = doc.createElement("div"),
2154 width = toFloat(width) || 512;
2155 height = toFloat(height) || 342;
2157 res.height = height;
2158 res.coordsize = width + S + height;
2159 res.coordorigin = "0 0";
2160 res.span = doc.createElement("span");
2161 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2162 c[appendChild](res.span);
2163 cs.cssText = R.format("width:{0}px;height:{1}px;position:absolute;clip:rect(0 {0}px {1}px 0)", width, height);
2164 if (container == 1) {
2165 doc.body[appendChild](c);
2175 container.style.width = width;
2176 container.style.height = height;
2177 if (container.firstChild) {
2178 container.insertBefore(c, container.firstChild);
2180 container[appendChild](c);
2183 for (var prop in paper) if (paper[has](prop)) {
2184 res[prop] = paper[prop];
2186 plugins.call(res, res, R.fn);
2187 res.clear = function () {
2193 paper.remove = function () {
2194 this.canvas.parentNode.removeChild(this.canvas);
2195 for (var i in this) {
2202 // Safari or Chrome (WebKit) rendering bug workaround method
2203 if ({"Apple Computer, Inc.": 1, "Google Inc.": 1}[navigator.vendor]) {
2204 paper.safari = function () {
2205 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2206 setTimeout(function () {rect.remove();});
2209 paper.safari = function () {};
2213 var addEvent = (function () {
2214 if (doc.addEventListener) {
2215 return function (obj, type, fn, element) {
2216 var f = function (e) {
2217 return fn.call(element, e);
2219 obj.addEventListener(type, f, false);
2220 return function () {
2221 obj.removeEventListener(type, f, false);
2225 } else if (doc.attachEvent) {
2226 return function (obj, type, fn, element) {
2227 var f = function (e) {
2228 return fn.call(element, e || win.event);
2230 obj.attachEvent("on" + type, f);
2231 var detacher = function () {
2232 obj.detachEvent("on" + type, f);
2235 if (type == "mouseover") {
2236 obj.attachEvent("onmouseenter", f);
2237 return function () {
2238 obj.detachEvent("onmouseenter", f);
2241 } else if (type == "mouseout") {
2242 obj.attachEvent("onmouseleave", f);
2243 return function () {
2244 obj.detachEvent("onmouseleave", f);
2252 for (var i = events[length]; i--;) {
2253 (function (eventName) {
2254 Element[proto][eventName] = function (fn) {
2255 if (R.is(fn, "function")) {
2256 this.events = this.events || {};
2257 this.events[eventName] = this.events[eventName] || {};
2258 this.events[eventName][fn] = this.events[eventName][fn] || [];
2259 this.events[eventName][fn][push](addEvent(this.shape || this.node, eventName, fn, this));
2263 Element[proto]["un" + eventName] = function (fn) {
2264 var e = this.events;
2268 e[eventName][fn][length] &&
2269 e[eventName][fn].shift()() &&
2270 !e[eventName][fn][length] &&
2271 delete e[eventName][fn];
2277 Element[proto].hover = function (f_in, f_out) {
2278 return this.mouseover(f_in).mouseout(f_out);
2280 paper.circle = function (x, y, r) {
2281 return theCircle(this, x || 0, y || 0, r || 0);
2283 paper.rect = function (x, y, w, h, r) {
2284 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2286 paper.ellipse = function (x, y, rx, ry) {
2287 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2289 paper.path = function (pathString) {
2290 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2291 return thePath(R.format[apply](R, arguments), this);
2293 paper.image = function (src, x, y, w, h) {
2294 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2296 paper.text = function (x, y, text) {
2297 return theText(this, x || 0, y || 0, text || E);
2299 paper.set = function (itemsArray) {
2300 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2301 return new Set(itemsArray);
2303 paper.setSize = setSize;
2304 Element[proto].scale = function (x, y, cx, cy) {
2305 if (x == null && y == null) {
2306 return {x: this._.sx, y: this._.sy, toString: function () { return this.x + S + this.y; }};
2316 var bb = this.getBBox(),
2317 rcx = bb.x + bb.width / 2,
2318 rcy = bb.y + bb.height / 2,
2321 cx = (+cx || cx == 0) ? cx : rcx;
2322 cy = (+cy || cy == 0) ? cy : rcy;
2323 var dirx = ~~(x / Math.abs(x)),
2324 diry = ~~(y / Math.abs(y)),
2325 s = this.node.style,
2326 ncx = cx + (rcx - cx) * dirx * kx,
2327 ncy = cy + (rcy - cy) * diry * ky;
2328 switch (this.type) {
2331 var neww = a.width * dirx * kx,
2332 newh = a.height * diry * ky,
2333 newr = a.r * mmin(kx, ky),
2334 newx = ncx - neww / 2,
2335 newy = ncy - newh / 2;
2349 r: a.r * mmin(kx, ky),
2355 var path = pathToRelative(a.path),
2357 for (var i = 0, ii = path[length]; i < ii; i++) {
2359 if (p[0].toUpperCase() == "M" && skip) {
2364 if (R.svg && p[0].toUpperCase() == "A") {
2365 p[path[i][length] - 2] *= kx;
2366 p[path[i][length] - 1] *= ky;
2369 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2371 for (var j = 1, jj = p[length]; j < jj; j++) {
2372 p[j] *= (j % 2) ? kx : ky;
2376 var dim2 = pathDimensions(path),
2377 dx = ncx - dim2.x - dim2.width / 2,
2378 dy = ncy - dim2.y - dim2.height / 2;
2382 this.attr({path: path});
2385 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2386 if (this.transformations) {
2387 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2388 this.node[setAttribute]("transform", this.transformations[join](S));
2389 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2390 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2391 this.attr({x: dx, y: dy});
2395 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2396 ", M12=0, M21=0, M22=", diry,
2397 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2398 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2401 if (this.transformations) {
2402 this.transformations[2] = E;
2403 this.node[setAttribute]("transform", this.transformations[join](S));
2407 this.node.filterMatrix = E;
2408 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2411 a.scale = [x, y, cx, cy][join](S);
2418 // animation easing formulas
2419 R.easing_formulas = {
2420 linear: function (n) {
2427 return pow(n - 1, 3) + 1;
2429 "<>": function (n) {
2432 return pow(n, 3) / 2;
2435 return (pow(n, 3) + 2) / 2;
2437 backIn: function (n) {
2439 return n * n * ((s + 1) * n - s);
2441 backOut: function (n) {
2444 return n * n * ((s + 1) * n + s) + 1;
2446 elastic: function (n) {
2447 if (n == 0 || n == 1) {
2452 return pow(2, -10 * n) * Math.sin((n - s) * (2 * Math.PI) / p) + 1;
2454 bounce: function (n) {
2463 l = s * n * n + .75;
2465 if (n < (2.5 / p)) {
2467 l = s * n * n + .9375;
2470 l = s * n * n + .984375;
2478 var animationElements = {length : 0},
2479 animation = function () {
2480 var Now = +new Date;
2481 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2482 var e = animationElements[l];
2484 delete animationElements[l];
2485 animationElements[length]--;
2488 var time = Now - e.start,
2497 callback = e.callback,
2501 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2502 for (var attr in from) if (from[has](attr)) {
2503 switch (availableAnimAttrs[attr]) {
2505 now = +from[attr] + pos * ms * diff[attr];
2509 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2510 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2511 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2516 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2517 now[i] = [from[attr][i][0]];
2518 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2519 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2521 now[i] = now[i][join](S);
2528 var x = diff[attr][0] * (time - prev),
2529 y = diff[attr][1] * (time - prev);
2535 now = +from[attr][0] + pos * ms * diff[attr][0];
2536 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2539 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);
2545 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2554 that._run && that._run.call(that);
2556 (t.x || t.y) && that.translate(-t.x, -t.y);
2557 to.scale && (to.scale = to.scale + E);
2559 R.is(callback, "function") && callback.call(that);
2560 delete animationElements[l];
2561 animationElements[length]--;
2562 that.in_animation = null;
2566 R.svg && paper.safari();
2567 animationElements[length] && setTimeout(animation);
2569 upto255 = function (color) {
2570 return color > 255 ? 255 : (color < 0 ? 0 : color);
2573 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2574 animationElements[element.in_animation] && (params.start = animationElements[element.in_animation].start);
2575 return this.animate(params, ms, easing, callback);
2577 Element[proto].onAnimation = function (f) {
2578 this._run = f || null;
2581 Element[proto].animate = function (params, ms, easing, callback) {
2582 if (R.is(easing, "function") || !easing) {
2583 callback = easing || null;
2588 for (var attr in params) if (params[has](attr)) {
2589 if (availableAnimAttrs[has](attr)) {
2590 from[attr] = this.attr(attr);
2591 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2592 to[attr] = params[attr];
2593 switch (availableAnimAttrs[attr]) {
2595 diff[attr] = (to[attr] - from[attr]) / ms;
2598 from[attr] = R.getRGB(from[attr]);
2599 var toColour = R.getRGB(to[attr]);
2601 r: (toColour.r - from[attr].r) / ms,
2602 g: (toColour.g - from[attr].g) / ms,
2603 b: (toColour.b - from[attr].b) / ms
2607 var pathes = path2curve(from[attr], to[attr]);
2608 from[attr] = pathes[0];
2609 to[attr] = pathes[1];
2611 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2612 diff[attr][i] = [0];
2613 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2614 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2619 var values = (params[attr] + E)[split](separator),
2620 from2 = (from[attr] + E)[split](separator);
2623 from[attr] = [0, 0];
2624 diff[attr] = [values[0] / ms, values[1] / ms];
2627 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2628 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2631 params[attr] = values;
2632 from[attr] = (from[attr] + E)[split](separator);
2633 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2636 from[attr] = (from[attr] + E)[split](separator);
2640 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2649 this.in_animation = 1;
2650 animationElements[this.id] = {
2651 start: params.start || +new Date,
2661 ++animationElements[length] == 1 && animation();
2664 Element[proto].stop = function () {
2665 delete animationElements[this.id];
2666 this.in_animation = 0;
2669 Element[proto].translate = function (x, y) {
2671 return {x: this._.tx, y: this._.ty};
2675 switch (this.type) {
2678 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2683 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2686 var path = pathToRelative(this.attrs.path);
2689 this.attr({path: path});
2694 Element[proto][toString] = function () {
2695 return "Rapha\u00ebl\u2019s object";
2697 R.ae = animationElements;
2700 var Set = function (items) {
2704 for (var i = 0, ii = items[length]; i < ii; i++) {
2705 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2706 this[this.items[length]] = this.items[this.items[length]] = items[i];
2712 Set[proto][push] = function () {
2715 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2716 item = arguments[i];
2717 if (item && (item.constructor == Element || item.constructor == Set)) {
2718 len = this.items[length];
2719 this[len] = this.items[len] = item;
2725 Set[proto].pop = function () {
2726 delete this[this[length]--];
2727 return this.items.pop();
2729 for (var method in Element[proto]) if (Element[proto][has](method)) {
2730 Set[proto][method] = (function (methodname) {
2731 return function () {
2732 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2733 this.items[i][methodname][apply](this.items[i], arguments);
2739 Set[proto].attr = function (name, value) {
2740 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2741 for (var j = 0, jj = name[length]; j < jj; j++) {
2742 this.items[j].attr(name[j]);
2745 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2746 this.items[i].attr[apply](this.items[i], arguments);
2751 Set[proto].animate = function (params, ms, easing, callback) {
2752 (R.is(easing, "function") || !easing) && (callback = easing || null);
2753 var len = this.items[length],
2757 var collector = function () {
2758 !--len && callback.call(set);
2760 this.items[--i].animate(params, ms, easing || collector, collector);
2762 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
2765 this.items[--i].animate(params, ms, easing);
2767 this.items[i].animateWith(this.items[len - 1], params, ms, easing);
2773 Set[proto].getBBox = function () {
2778 for (var i = this.items[length]; i--;) {
2779 var box = this.items[i].getBBox();
2782 w[push](box.x + box.width);
2783 h[push](box.y + box.height);
2785 x = mmin[apply](0, x);
2786 y = mmin[apply](0, y);
2790 width: mmax[apply](0, w) - x,
2791 height: mmax[apply](0, h) - y
2795 R.registerFont = function (font) {
2799 this.fonts = this.fonts || {};
2805 family = font.face["font-family"];
2806 for (var prop in font.face) if (font.face[has](prop)) {
2807 fontcopy.face[prop] = font.face[prop];
2809 if (this.fonts[family]) {
2810 this.fonts[family][push](fontcopy);
2812 this.fonts[family] = [fontcopy];
2815 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
2816 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
2817 var path = font.glyphs[glyph];
2818 fontcopy.glyphs[glyph] = {
2821 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
2822 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2826 for (var k in path.k) if (path[has](k)) {
2827 fontcopy.glyphs[glyph].k[k] = path.k[k];
2834 paper.getFont = function (family, weight, style, stretch) {
2835 stretch = stretch || "normal";
2836 style = style || "normal";
2837 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2838 var font = R.fonts[family];
2840 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
2841 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
2842 if (name.test(fontName)) {
2843 font = R.fonts[fontName];
2850 for (var i = 0, ii = font[length]; i < ii; i++) {
2852 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2859 paper.print = function (x, y, string, font, size) {
2860 var out = this.set(),
2861 letters = (string + E)[split](E),
2865 R.is(font, "string") && (font = this.getFont(font));
2867 scale = (size || 16) / font.face["units-per-em"];
2868 for (var i = 0, ii = letters[length]; i < ii; i++) {
2869 var prev = i && font.glyphs[letters[i - 1]] || {},
2870 curr = font.glyphs[letters[i]];
2871 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
2872 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
2874 out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
2879 R.format = function (token) {
2880 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
2882 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
2883 return args[++i] == null ? E : args[i];
2887 R.ninja = function () {
2888 var r = win.Raphael, u;
2889 if (oldRaphael.was) {
2890 win.Raphael = oldRaphael.is;
2900 R.el = Element[proto];