2 * Raphael 1.0 - 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 window,
18 if (R.is(arguments[0], "array")) {
20 cnv = create[ap](R, a.splice(0, 3 + R.is(a[0], "number"))),
22 for (var i = 0, ii = a[ln]; i < ii; i++) {
24 j.type in {circle:1, rect:1, path:1, ellipse:1, text:1, image:1} && res.push(cnv[j.type]().attr(j));
28 return create[ap](R, arguments);
31 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},
32 availableAnimAttrs = {"clip-rect": "csv", cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "csv", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"},
33 events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"],
40 R.type = (window.SVGAngle || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
41 R.svg = !(R.vml = R.type == "VML");
44 R.is = function (o, type) {
45 type = (type + "").toLowerCase();
46 if ((type == "object" || type == "undefined") && typeof o == type) {
49 if (o == null && type == "null") {
52 return Object[proto].toString.call(o).replace(/^\[object\s+|\]$/gi, "").toLowerCase() == type;
54 R.setWindow = function (newwin) {
59 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
60 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
68 if (brightness == 0) {
69 return {r: 0, g: 0, b: 0, hex: "#000"};
71 if (hue > 1 || saturation > 1 || brightness > 1) {
76 var i = Math.floor(hue * 6),
78 p = brightness * (1 - saturation),
79 q = brightness * (1 - (saturation * f)),
80 t = brightness * (1 - (saturation * (1 - f)));
81 red = [brightness, q, p, p, t, brightness, brightness][i];
82 green = [t, brightness, brightness, q, p, p, t][i];
83 blue = [p, p, t, brightness, brightness, q, p][i];
87 var rgb = {r: red, g: green, b: blue},
88 r = (~~red).toString(16),
89 g = (~~green).toString(16),
90 b = (~~blue).toString(16);
100 rgb.hex = "#" + r + g + b;
103 R.rgb2hsb = cacher(function (red, green, blue) {
104 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
109 if (R.is(red, "string")) {
110 var clr = R.getRGB(red);
115 if (red > 1 || green > 1 || blue > 1) {
120 var max = Math.max(red, green, blue),
121 min = Math.min(red, green, blue),
126 return {h: 0, s: 0, b: max};
128 var delta = (max - min);
129 saturation = delta / max;
131 hue = (green - blue) / delta;
132 } else if (green == max) {
133 hue = 2 + ((blue - red) / delta);
135 hue = 4 + ((red - green) / delta);
145 return {h: hue, s: saturation, b: brightness};
147 R._path2string = function () {
150 for (var i = 0, ii = this[ln]; i < ii; i++) {
151 for (var j = 0, jj = this[i][ln]; j < jj; j++) {
153 j && j != jj - 1 && (res += ",");
155 i != ii - 1 && (res += "\n");
157 return res.replace(/,(?=-)/g, "");
159 function cacher(f, scope, postprocessor) {
161 var arg = Array[proto].splice.call(arguments, 0, arguments[ln]),
162 args = arg.join("\u25ba");
163 newf.cache = newf.cache || {};
164 newf.count = newf.count || [];
165 if (args in newf.cache) {
166 return postprocessor ? postprocessor(newf.cache[args]) : newf.cache[args];
168 if (newf.count[ln] > 1000) {
169 delete newf.cache[newf.count.unshift()];
171 newf.count.push(args);
172 newf.cache[args] = f[ap](scope, arg);
173 return postprocessor ? postprocessor(newf.cache[args]) : newf.cache[args];
178 R.getRGB = cacher(function (colour) {
179 var htmlcolors = {none: "none", aliceblue: "#f0f8ff", amethyst: "#96c", antiquewhite: "#faebd7", aqua: "#0ff", aquamarine: "#7fffd4", azure: "#f0ffff", beige: "#f5f5dc", bisque: "#ffe4c4", black: "#000", blanchedalmond: "#ffebcd", blue: "#00f", blueviolet: "#8a2be2", brown: "#a52a2a", burlywood: "#deb887", cadetblue: "#5f9ea0", chartreuse: "#7fff00", chocolate: "#d2691e", coral: "#ff7f50", cornflowerblue: "#6495ed", cornsilk: "#fff8dc", crimson: "#dc143c", cyan: "#0ff", darkblue: "#00008b", darkcyan: "#008b8b", darkgoldenrod: "#b8860b", darkgray: "#a9a9a9", darkgreen: "#006400", darkkhaki: "#bdb76b", darkmagenta: "#8b008b", darkolivegreen: "#556b2f", darkorange: "#ff8c00", darkorchid: "#9932cc", darkred: "#8b0000", darksalmon: "#e9967a", darkseagreen: "#8fbc8f", darkslateblue: "#483d8b", darkslategray: "#2f4f4f", darkturquoise: "#00ced1", darkviolet: "#9400d3", deeppink: "#ff1493", deepskyblue: "#00bfff", dimgray: "#696969", dodgerblue: "#1e90ff", firebrick: "#b22222", floralwhite: "#fffaf0", forestgreen: "#228b22", fuchsia: "#f0f", gainsboro: "#dcdcdc", ghostwhite: "#f8f8ff", gold: "#ffd700", goldenrod: "#daa520", gray: "#808080", green: "#008000", greenyellow: "#adff2f", honeydew: "#f0fff0", hotpink: "#ff69b4", indianred: "#cd5c5c", indigo: "#4b0082", ivory: "#fffff0", khaki: "#f0e68c", lavender: "#e6e6fa", lavenderblush: "#fff0f5", lawngreen: "#7cfc00", lemonchiffon: "#fffacd", lightblue: "#add8e6", lightcoral: "#f08080", lightcyan: "#e0ffff", lightgoldenrodyellow: "#fafad2", lightgreen: "#90ee90", lightgrey: "#d3d3d3", lightpink: "#ffb6c1", lightsalmon: "#ffa07a", lightsalmon: "#ffa07a", lightseagreen: "#20b2aa", lightskyblue: "#87cefa", lightslategray: "#789", lightsteelblue: "#b0c4de", lightyellow: "#ffffe0", lime: "#0f0", limegreen: "#32cd32", linen: "#faf0e6", magenta: "#f0f", maroon: "#800000", mediumaquamarine: "#66cdaa", mediumblue: "#0000cd", mediumorchid: "#ba55d3", mediumpurple: "#9370db", mediumseagreen: "#3cb371", mediumslateblue: "#7b68ee", mediumslateblue: "#7b68ee", mediumspringgreen: "#00fa9a", mediumturquoise: "#48d1cc", mediumvioletred: "#c71585", midnightblue: "#191970", mintcream: "#f5fffa", mistyrose: "#ffe4e1", moccasin: "#ffe4b5", navajowhite: "#ffdead", navy: "#000080", oldlace: "#fdf5e6", olive: "#808000", olivedrab: "#6b8e23", orange: "#ffa500", orangered: "#ff4500", orchid: "#da70d6", palegoldenrod: "#eee8aa", palegreen: "#98fb98", paleturquoise: "#afeeee", palevioletred: "#db7093", papayawhip: "#ffefd5", peachpuff: "#ffdab9", peru: "#cd853f", pink: "#ffc0cb", plum: "#dda0dd", powderblue: "#b0e0e6", purple: "#800080", red: "#f00", rosybrown: "#bc8f8f", royalblue: "#4169e1", saddlebrown: "#8b4513", salmon: "#fa8072", sandybrown: "#f4a460", seagreen: "#2e8b57", seashell: "#fff5ee", sienna: "#a0522d", silver: "#c0c0c0", skyblue: "#87ceeb", slateblue: "#6a5acd", slategray: "#708090", snow: "#fffafa", springgreen: "#00ff7f", steelblue: "#4682b4", tan: "#d2b48c", teal: "#008080", thistle: "#d8bfd8", tomato: "#ff6347", turquoise: "#40e0d0", violet: "#ee82ee", wheat: "#f5deb3", white: "#fff", whitesmoke: "#f5f5f5", yellow: "#ff0", yellowgreen: "#9acd32"},
181 colour = htmlcolors[(colour + "").toLowerCase()] || colour;
183 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
185 if (colour == "none") {
186 return {r: -1, g: -1, b: -1, hex: "none"};
191 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);
194 blue = win[pi](rgb[2].substring(5), 16);
195 green = win[pi](rgb[2].substring(3, 5), 16);
196 red = win[pi](rgb[2].substring(1, 3), 16);
199 blue = win[pi](rgb[3].substring(3) + rgb[3].substring(3), 16);
200 green = win[pi](rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
201 red = win[pi](rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
204 rgb = rgb[4].split(/\s*,\s*/);
205 red = win[pf](rgb[0]);
206 green = win[pf](rgb[1]);
207 blue = win[pf](rgb[2]);
210 rgb = rgb[5].split(/\s*,\s*/);
211 red = win[pf](rgb[0]) * 2.55;
212 green = win[pf](rgb[1]) * 2.55;
213 blue = win[pf](rgb[2]) * 2.55;
216 rgb = rgb[6].split(/\s*,\s*/);
217 red = win[pf](rgb[0]);
218 green = win[pf](rgb[1]);
219 blue = win[pf](rgb[2]);
220 return R.hsb2rgb(red, green, blue);
223 rgb = rgb[7].split(/\s*,\s*/);
224 red = win[pf](rgb[0]) * 2.55;
225 green = win[pf](rgb[1]) * 2.55;
226 blue = win[pf](rgb[2]) * 2.55;
227 return R.hsb2rgb(red, green, blue);
229 rgb = {r: red, g: green, b: blue};
230 var r = (~~red).toString(16),
231 g = (~~green).toString(16),
232 b = (~~blue).toString(16),
238 rgb.hex = "#" + r + g + b;
241 res = {r: -1, g: -1, b: -1, hex: "none", error: 1};
245 R.getColor = function (value) {
246 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
247 rgb = this.hsb2rgb(start.h, start.s, start.b);
253 this.getColor.start = {h: 0, s: 1, b: start.b};
258 R.getColor.reset = function () {
262 R.parsePathString = cacher(function (pathString) {
266 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
268 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
269 data = pathClone(pathString);
272 (pathString + "").replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
274 name = b.toLowerCase();
275 c.replace(/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
276 b && params.push(+b);
278 while (params[ln] >= paramCounts[name]) {
279 data.push([b].concat(params.splice(0, paramCounts[name])));
280 if (!paramCounts[name]) {
286 data.toString = R._path2string;
289 var pathDimensions = cacher(function (path) {
291 return {x: 0, y: 0, width: 0, height: 0};
293 path = path2curve(path);
298 for (var i = 0, ii = path[ln]; i < ii; i++) {
299 if (path[i][0] == "M") {
305 var dim = curveDim(x, y, path[i][1], path[i][2], path[i][3], path[i][4], path[i][5], path[i][6]);
306 X = X.concat(dim.min.x, dim.max.x);
307 Y = Y.concat(dim.min.y, dim.max.y);
310 var xmin = Math.min[ap](0, X),
311 ymin = Math.min[ap](0, Y);
315 width: Math.max[ap](0, X) - xmin,
316 height: Math.max[ap](0, Y) - ymin
319 pathClone = function (pathArray) {
321 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
322 pathArray = R.parsePathString(pathArray);
324 for (var i = 0, ii = pathArray[ln]; i < ii; i++) {
326 for (var j = 0, jj = pathArray[i][ln]; j < jj; j++) {
327 res[i][j] = pathArray[i][j];
330 res.toString = R._path2string;
333 pathToRelative = cacher(function (pathArray) {
334 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
335 pathArray = R.parsePathString(pathArray);
343 if (pathArray[0][0] == "M") {
349 res.push(["M", x, y]);
351 for (var i = start, ii = pathArray[ln]; i < ii; i++) {
354 if (pa[0] != pa[0].toLowerCase()) {
355 r[0] = pa[0].toLowerCase();
363 r[6] = +(pa[6] - x).toFixed(3);
364 r[7] = +(pa[7] - y).toFixed(3);
367 r[1] = +(pa[1] - y).toFixed(3);
373 for (var j = 1, jj = pa[ln]; j < jj; j++) {
374 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
383 for (var k = 0, kk = pa[ln]; k < kk; k++) {
387 var len = res[i][ln];
394 x += +res[i][len - 1];
397 y += +res[i][len - 1];
400 x += +res[i][len - 2];
401 y += +res[i][len - 1];
404 res.toString = R._path2string;
407 pathToAbsolute = cacher(function (pathArray) {
408 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
409 pathArray = R.parsePathString(pathArray);
417 if (pathArray[0][0] == "M") {
418 x = +pathArray[0][1];
419 y = +pathArray[0][2];
423 res[0] = ["M", x, y];
425 for (var i = start, ii = pathArray[ln]; i < ii; i++) {
428 if (pa[0] != (pa[0] + "").toUpperCase()) {
429 r[0] = (pa[0] + "").toUpperCase();
450 for (var j = 1, jj = pa[ln]; j < jj; j++) {
451 r[j] = +pa[j] + ((j % 2) ? x : y);
455 for (var k = 0, kk = pa[ln]; k < kk; k++) {
471 x = res[i][res[i][ln] - 2];
472 y = res[i][res[i][ln] - 1];
475 res.toString = R._path2string;
478 l2c = function (x1, y1, x2, y2) {
479 return [x1, y1, x2, y2, x2, y2];
481 q2c = function (x1, y1, ax, ay, x2, y2) {
493 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
494 // for more information of where this math came from visit:
495 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
497 _120 = PI * 120 / 180,
498 rad = PI / 180 * (+angle || 0),
501 rotate = cacher(function (x, y, rad) {
502 var X = x * Math.cos(rad) - y * Math.sin(rad),
503 Y = x * Math.sin(rad) + y * Math.cos(rad);
507 xy = rotate(x1, y1, -rad);
510 xy = rotate(x2, y2, -rad);
513 var cos = Math.cos(PI / 180 * angle),
514 sin = Math.sin(PI / 180 * angle),
517 rx = Math.max(rx, Math.abs(x));
518 ry = Math.max(ry, Math.abs(y));
521 k = (large_arc_flag == sweep_flag ? -1 : 1) *
522 Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
523 cx = k * rx * y / ry + (x1 + x2) / 2,
524 cy = k * -ry * x / rx + (y1 + y2) / 2,
525 f1 = Math.asin((y1 - cy) / ry),
526 f2 = Math.asin((y2 - cy) / ry);
528 f1 = x1 < cx ? PI - f1 : f1;
529 f2 = x2 < cx ? PI - f2 : f2;
530 f1 < 0 && (f1 = PI * 2 + f1);
531 f2 < 0 && (f2 = PI * 2 + f2);
532 if (sweep_flag && f1 > f2) {
535 if (!sweep_flag && f2 > f1) {
545 if (Math.abs(df) > _120) {
549 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
550 x2 = cx + rx * Math.cos(f2);
551 y2 = cy + ry * Math.sin(f2);
552 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
555 var c1 = Math.cos(f1),
559 t = Math.tan(df / 4),
563 m2 = [x1 + hx * s1, y1 - hy * c1],
564 m3 = [x2 + hx * s2, y2 - hy * c2],
566 m2[0] = 2 * m1[0] - m2[0];
567 m2[1] = 2 * m1[1] - m2[1];
569 return [m2, m3, m4].concat(res);
571 res = [m2, m3, m4].concat(res).join(",").split(",");
573 for (var i = 0, ii = res[ln]; i < ii; i++) {
574 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
579 findDotAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
580 var x = Math.pow(1 - t, 3) * p1x + Math.pow(1 - t, 2) * 3 * t * c1x + (1 - t) * 3 * t * t * c2x + Math.pow(t, 3) * p2x,
581 y = Math.pow(1 - t, 3) * p1y + Math.pow(1 - t, 2) * 3 * t * c1y + (1 - t) * 3 * t * t * c2y + Math.pow(t, 3) * p2y,
582 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
583 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
584 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
585 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
586 ax = (1 - t) * p1x + t * c1x,
587 ay = (1 - t) * p1y + t * c1y,
588 cx = (1 - t) * c2x + t * p2x,
589 cy = (1 - t) * c2y + t * p2y;
590 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}};
592 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
593 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
594 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
596 t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a,
597 t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a,
600 dot1 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1 > 0 && t1 < 1 ? t1 : 0),
601 dot2 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2 > 0 && t2 < 1 ? t2 : 0);
602 x = x.concat(dot1.x, dot2.x);
603 y = y.concat(dot1.y, dot2.y);
604 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
605 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
607 t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a;
608 t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a;
609 dot1 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1 > 0 && t1 < 1 ? t1 : 0);
610 dot2 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2 > 0 && t2 < 1 ? t2 : 0);
611 x = x.concat(dot1.x, dot2.x);
612 y = y.concat(dot1.y, dot2.y);
614 min: {x: Math.min[ap](Math, x), y: Math.min[ap](Math, y)},
615 max: {x: Math.max[ap](Math, x), y: Math.max[ap](Math, y)}
618 path2curve = cacher(function (path, path2) {
619 var p = pathToAbsolute(path),
620 p2 = path2 && pathToAbsolute(path2),
621 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
622 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
623 processPath = function (path, d) {
626 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
628 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
635 path = ["C"].concat(a2c[ap](0, [d.x, d.y].concat(path.slice(1))));
638 nx = d.x + (d.x - (d.bx || d.x));
639 ny = d.y + (d.y - (d.by || d.y));
640 path = ["C", nx, ny].concat(path.slice(1));
643 d.qx = d.x + (d.x - (d.qx || d.x));
644 d.qy = d.y + (d.y - (d.qy || d.y));
645 path = ["C"].concat(q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
650 path = ["C"].concat(q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
653 path = ["C"].concat(l2c(d.x, d.y, path[1], path[2]));
656 path = ["C"].concat(l2c(d.x, d.y, path[1], d.y));
659 path = ["C"].concat(l2c(d.x, d.y, d.x, path[1]));
662 path = ["C"].concat(l2c(d.x, d.y, d.X, d.Y));
667 fixArc = function (pp, i) {
672 pp.splice(i++, 0, ["C"].concat(pi.splice(0, 6)));
675 ii = Math.max(p[ln], p2 && p2[ln] || 0);
678 fixM = function (path1, path2, a1, a2, i) {
679 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
680 path2.splice(i, 0, ["M", a2.x, a2.y]);
685 ii = Math.max(p[ln], p2 && p2[ln] || 0);
688 for (var i = 0, ii = Math.max(p[ln], p2 && p2[ln] || 0); i < ii; i++) {
689 p[i] = processPath(p[i], attrs);
691 p2 && (p2[i] = processPath(p2[i], attrs2));
693 fixM(p, p2, attrs, attrs2, i);
694 fixM(p2, p, attrs2, attrs, i);
698 seg2len = p2 && seg2[ln];
699 attrs.x = seg[seglen - 2];
700 attrs.y = seg[seglen - 1];
701 attrs.bx = win[pf](seg[seglen - 4]) || attrs.x;
702 attrs.by = win[pf](seg[seglen - 3]) || attrs.y;
703 attrs2.bx = p2 && (win[pf](seg2[seg2len - 4]) || attrs2.x);
704 attrs2.by = p2 && (win[pf](seg2[seg2len - 3]) || attrs2.y);
705 attrs2.x = p2 && seg2[seg2len - 2];
706 attrs2.y = p2 && seg2[seg2len - 1];
708 return p2 ? [p, p2] : p;
710 parseDots = cacher(function (gradient) {
712 for (var i = 0, ii = gradient[ln]; i < ii; i++) {
714 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
715 dot.color = R.getRGB(par[1]);
716 if (dot.color.error) {
719 dot.color = dot.color.hex;
720 par[2] && (dot.offset = par[2] + "%");
723 for (var i = 1, ii = dots[ln] - 1; i < ii; i++) {
724 if (!dots[i].offset) {
725 var start = win[pf](dots[i - 1].offset || 0),
727 for (var j = i + 1; j < ii; j++) {
728 if (dots[j].offset) {
729 end = dots[j].offset;
738 var d = (end - start) / (j - i + 1);
741 dots[i].offset = start + "%";
747 getContainer = function () {
753 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
754 if (R.is(arguments[0], "string")) {
755 container = doc.getElementById(arguments[0]);
757 container = arguments[0];
759 if (container.tagName) {
760 if (arguments[1] == null) {
762 container: container,
763 width: container.style.pixelWidth || container.offsetWidth,
764 height: container.style.pixelHeight || container.offsetHeight
767 return {container: container, width: arguments[1], height: arguments[2]};
770 } else if (R.is(arguments[0], "number") && arguments[ln] > 3) {
771 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
774 plugins = function (con, add) {
776 for (var prop in add) if (add.hasOwnProperty(prop) && !(prop in con)) {
777 switch (typeof add[prop]) {
780 con[prop] = con === that ? f : function () { return f[ap](that, arguments); };
784 con[prop] = con[prop] || {};
785 plugins.call(this, con[prop], add[prop]);
788 con[prop] = add[prop];
796 paper.svgns = "http://www.w3.org/2000/svg";
797 paper.xlink = "http://www.w3.org/1999/xlink";
798 var round = function (num) {
799 return +num + (Math.floor(num) == num) * .5;
801 var roundPath = function (path) {
802 for (var i = 0, ii = path[ln]; i < ii; i++) {
803 if (path[i][0].toLowerCase() != "a") {
804 for (var j = 1, jj = path[i][ln]; j < jj; j++) {
805 path[i][j] = round(path[i][j]);
808 path[i][6] = round(path[i][6]);
809 path[i][7] = round(path[i][7]);
814 var $ = function (el, attr) {
816 for (var key in attr) if (attr.hasOwnProperty(key)) {
817 el.setAttribute(key, attr[key]);
820 return doc.createElementNS(paper.svgns, el);
823 R.toString = function () {
824 return "Your browser supports SVG.\nYou are running Rapha\u00ebl " + this.version;
826 var thePath = function (pathString, SVG) {
828 SVG.canvas && SVG.canvas.appendChild(el);
829 var p = new Element(el, SVG);
831 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
834 var addGradientFill = function (o, gradient, SVG) {
838 gradient = (gradient + "").replace(/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, _fx, _fy) {
843 if (Math.pow(fx - .5, 2) + Math.pow(fy - .5, 2) > .25) {
844 fy = Math.sqrt(.25 - Math.pow(fx - .5, 2)) + .5;
849 gradient = gradient.split(/\s*\-\s*/);
850 if (type == "linear") {
851 var angle = gradient.shift();
852 angle = -win[pf](angle);
856 var vector = [0, 0, Math.cos(angle * Math.PI / 180), Math.sin(angle * Math.PI / 180)],
857 max = 1 / (Math.max(Math.abs(vector[2]), Math.abs(vector[3])) || 1);
861 vector[0] = -vector[2];
865 vector[1] = -vector[3];
869 var dots = parseDots(gradient);
873 var el = $(type + "Gradient");
874 el.id = "r" + (R.idGenerator++).toString(36);
875 type == "radial" ? $(el, {fx: fx, fy: fy}) : $(el, {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
876 SVG.defs.appendChild(el);
877 for (var i = 0, ii = dots[ln]; i < ii; i++) {
878 var stop = $("stop");
880 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
881 "stop-color": dots[i].color || "#fff"
883 el.appendChild(stop);
886 fill: "url(#" + el.id + ")",
895 var updatePosition = function (o) {
896 var bbox = o.getBBox();
897 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
899 var setFillAndStroke = function (o, params) {
906 "-..": [3, 1, 1, 1, 1, 1],
912 "--..": [8, 3, 1, 3, 1, 3]
916 rot = o.attr("rotation"),
917 addDashes = function (o, value) {
918 value = dasharray[(value + "").toLowerCase()];
920 var width = o.attrs["stroke-width"] || "1",
921 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
925 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
927 $(node, {"stroke-dasharray": dashes.join(",")});
930 win[pf](rot) && o.rotate(0, true);
931 for (var att in params) if (params.hasOwnProperty(att)) {
932 if (!(att in availableAttrs)) {
935 var value = params[att];
942 var pn = node.parentNode;
943 if (pn.tagName.toLowerCase() != "a") {
945 pn.insertBefore(hl, node);
946 hl.appendChild(node);
949 pn.setAttributeNS(o.paper.xlink, att, value);
952 var rect = (value + "").split(separator);
954 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
955 var el = $("clipPath"),
957 el.id = "r" + (R.idGenerator++).toString(36);
965 o.paper.defs.appendChild(el);
966 $(node, {"clip-path": "url(#" + el.id + ")"});
970 var clip = doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, ""));
971 clip && clip.parentNode.removeChild(clip);
972 $(node, {"clip-path": ""});
977 if (value && o.type == "path") {
978 attrs.path = roundPath(pathToAbsolute(value));
979 $(node, {d: attrs.path});
983 node.setAttribute(att, value);
992 value = -attrs.x - (attrs.width || 0);
995 if (att == "rx" && o.type == "rect") {
999 node.setAttribute(att, value);
1000 o.pattern && updatePosition(o);
1003 node.setAttribute(att, value);
1012 value = -attrs.y - (attrs.height || 0);
1015 if (att == "ry" && o.type == "rect") {
1019 node.setAttribute(att, value);
1020 o.pattern && updatePosition(o);
1023 if (o.type == "rect") {
1024 $(node, {rx: value, ry: value});
1026 node.setAttribute(att, value);
1030 if (o.type == "image") {
1031 node.setAttributeNS(o.paper.xlink, "href", value);
1034 case "stroke-width":
1035 node.style.strokeWidth = value;
1036 // Need following line for Firefox
1037 node.setAttribute(att, value);
1038 if (attrs["stroke-dasharray"]) {
1039 addDashes(o, attrs["stroke-dasharray"]);
1042 case "stroke-dasharray":
1043 addDashes(o, value);
1047 o.rotate(value, true);
1050 var xy = (value + "").split(separator);
1051 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
1054 var xy = (value + "").split(separator);
1055 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1058 var isURL = (value + "").match(/^url\(['"]?([^\)]+)['"]?\)$/i);
1060 var el = $("pattern"),
1062 el.id = "r" + (R.idGenerator++).toString(36);
1063 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse"});
1065 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1068 var img = doc.createElement("img");
1069 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1070 img.onload = function () {
1071 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1072 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1073 doc.body.removeChild(this);
1076 doc.body.appendChild(img);
1078 o.paper.defs.appendChild(el);
1079 node.style.fill = "url(#" + el.id + ")";
1080 $(node, {fill: "url(#" + el.id + ")"});
1082 o.pattern && updatePosition(o);
1085 if (!R.getRGB(value).error) {
1086 delete params.gradient;
1087 delete attrs.gradient;
1088 if (!R.is(attrs.opacity, "undefined") && R.is(params.opacity, "undefined") ) {
1089 node.style.opacity = attrs.opacity;
1090 // Need following line for Firefox
1091 $(node, {opacity: attrs.opacity});
1093 if (!R.is(attrs["fill-opacity"], "undefined") && R.is(params["fill-opacity"], "undefined") ) {
1094 node.style.fillOpacity = attrs["fill-opacity"];
1095 // Need following line for Firefox
1096 $(node, {"fill-opacity": attrs["fill-opacity"]});
1098 } else if ((o.type in {circle: 1, ellipse: 1} || (value + "").charAt(0) != "r") && addGradientFill(node, value, o.paper)) {
1099 attrs.gradient = value;
1100 attrs.fill = "none";
1104 node.style[att] = R.getRGB(value).hex;
1105 // Need following line for Firefox
1106 node.setAttribute(att, R.getRGB(value).hex);
1109 (o.type in {circle: 1, ellipse: 1} || (value + "").charAt(0) != "r") && addGradientFill(node, value, o.paper);
1112 case "fill-opacity":
1113 if (attrs.gradient) {
1114 var gradient = doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
1116 var stops = gradient.getElementsByTagName("stop");
1117 stops[stops[ln] - 1].setAttribute("stop-opacity", value);
1122 att == "font-size" && (value = win[pi](value, 10) + "px");
1123 var cssrule = att.replace(/(\-.)/g, function (w) {
1124 return w.substring(1).toUpperCase();
1126 node.style[cssrule] = value;
1127 // Need following line for Firefox
1128 node.setAttribute(att, value);
1133 tuneText(o, params);
1134 win[pi](rot, 10) && o.rotate(rot, true);
1137 var tuneText = function (el, params) {
1138 if (el.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params || "y" in params)) {
1143 fontSize = node.firstChild ? win[pi](doc.defaultView.getComputedStyle(node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
1145 if ("text" in params) {
1146 while (node.firstChild) {
1147 node.removeChild(node.firstChild);
1149 var texts = (params.text + "").split("\n");
1150 for (var i = 0, ii = texts[ln]; i < ii; i++) {
1151 var tspan = $("tspan");
1152 i && $(tspan, {dy: fontSize * leading, x: a.x});
1153 tspan.appendChild(doc.createTextNode(texts[i]));
1154 node.appendChild(tspan);
1157 var texts = node.getElementsByTagName("tspan");
1158 for (var i = 0, ii = texts[ln]; i < ii; i++) {
1159 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1163 var bb = el.getBBox(),
1164 dif = a.y - (bb.y + bb.height / 2);
1165 dif && $(node, {y: a.y + dif});
1167 var Element = function (node, svg) {
1172 node.raphael = this;
1174 this.attrs = this.attrs || {};
1175 this.transformations = []; // rotate, translate, scale
1179 rt: {deg: 0, cx: 0, cy: 0},
1184 Element[proto].rotate = function (deg, cx, cy) {
1187 return [this._.rt.deg, this._.rt.cx, this._.rt.cy].join(" ");
1189 return this._.rt.deg;
1191 var bbox = this.getBBox();
1192 deg = (deg + "").split(separator);
1194 cx = win[pf](deg[1]);
1195 cy = win[pf](deg[2]);
1197 deg = win[pf](deg[0]);
1199 this._.rt.deg = deg;
1201 this._.rt.deg += deg;
1203 (cy == null) && (cx = null);
1206 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1207 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1208 if (this._.rt.deg) {
1209 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1210 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1212 this.transformations[0] = "";
1213 this.clip && $(this.clip, {transform: ""});
1215 $(this.node, {transform: this.transformations.join(" ")});
1218 Element[proto].hide = function () {
1219 this.node.style.display = "none";
1222 Element[proto].show = function () {
1223 this.node.style.display = "block";
1226 Element[proto].remove = function () {
1227 this.node.parentNode.removeChild(this.node);
1229 Element[proto].getBBox = function () {
1230 if (this.type == "path") {
1231 return pathDimensions(this.attrs.path);
1233 if (this.node.style.display == "none") {
1239 bbox = this.node.getBBox();
1241 // Firefox 3.0.x plays badly here
1245 if (this.type == "text") {
1246 bbox = {x: bbox.x, y: Infinity, width: bbox.width, height: 0};
1247 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1248 var bb = this.node.getExtentOfChar(i);
1249 (bb.y < bbox.y) && (bbox.y = bb.y);
1250 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1253 hide && this.hide();
1256 Element[proto].attr = function () {
1257 if (arguments[ln] == 1 && R.is(arguments[0], "string")) {
1258 if (arguments[0] == "translation") {
1259 return this.translate();
1261 if (arguments[0] == "rotation") {
1262 return this.rotate();
1264 if (arguments[0] == "scale") {
1265 return this.scale();
1267 return this.attrs[arguments[0]];
1269 if (arguments[ln] == 1 && R.is(arguments[0], "array")) {
1271 for (var j in arguments[0]) if (arguments[0].hasOwnProperty(j)) {
1272 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1276 if (arguments[ln] == 2) {
1278 params[arguments[0]] = arguments[1];
1279 setFillAndStroke(this, params);
1280 } else if (arguments[ln] == 1 && R.is(arguments[0], "object")) {
1281 setFillAndStroke(this, arguments[0]);
1285 Element[proto].toFront = function () {
1286 this.node.parentNode.appendChild(this.node);
1289 Element[proto].toBack = function () {
1290 if (this.node.parentNode.firstChild != this.node) {
1291 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1295 Element[proto].insertAfter = function (element) {
1296 if (element.node.nextSibling) {
1297 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1299 element.node.parentNode.appendChild(this.node);
1303 Element[proto].insertBefore = function (element) {
1304 var node = element.node;
1305 node.parentNode.insertBefore(this.node, node);
1309 var theCircle = function (svg, x, y, r) {
1312 var el = $("circle");
1313 svg.canvas && svg.canvas.appendChild(el);
1314 var res = new Element(el, svg);
1315 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1316 res.type = "circle";
1320 var theRect = function (svg, x, y, w, h, r) {
1324 svg.canvas && svg.canvas.appendChild(el);
1325 var res = new Element(el, svg);
1326 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1331 var theEllipse = function (svg, x, y, rx, ry) {
1334 var el = $("ellipse");
1335 svg.canvas && svg.canvas.appendChild(el);
1336 var res = new Element(el, svg);
1337 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1338 res.type = "ellipse";
1342 var theImage = function (svg, src, x, y, w, h) {
1343 var el = $("image");
1344 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1345 el.setAttributeNS(svg.xlink, "href", src);
1346 svg.canvas && svg.canvas.appendChild(el);
1347 var res = new Element(el, svg);
1348 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1352 var theText = function (svg, x, y, text) {
1354 $(el, {x: x, y: y, "text-anchor": "middle"});
1355 svg.canvas && svg.canvas.appendChild(el);
1356 var res = new Element(el, svg);
1357 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1359 setFillAndStroke(res, res.attrs);
1362 var setSize = function (width, height) {
1363 this.width = width || this.width;
1364 this.height = height || this.height;
1365 this.canvas.setAttribute("width", this.width);
1366 this.canvas.setAttribute("height", this.height);
1369 var create = function () {
1370 var con = getContainer[ap](null, arguments),
1371 container = con && con.container,
1375 height = con.height;
1377 throw new Error("SVG container not found.");
1379 paper.canvas = $("svg");
1380 var cnvs = paper.canvas,
1382 cnvs.setAttribute("width", width || 512);
1383 paper.width = width || 512;
1384 cnvs.setAttribute("height", height || 342);
1385 paper.height = height || 342;
1386 if (container == 1) {
1387 doc.body.appendChild(cnvs);
1388 stl.position = "absolute";
1389 stl.left = x + "px";
1392 if (container.firstChild) {
1393 container.insertBefore(cnvs, container.firstChild);
1395 container.appendChild(cnvs);
1400 clear: function () {
1401 while (this.canvas.firstChild) {
1402 this.canvas.removeChild(this.canvas.firstChild);
1404 this.desc = $("desc");
1405 this.defs = $("defs");
1406 this.desc.appendChild(doc.createTextNode("Created with Rapha\u00ebl"));
1407 this.canvas.appendChild(this.desc);
1408 this.canvas.appendChild(this.defs);
1411 for (var prop in paper) if (paper.hasOwnProperty(prop)) {
1412 if (prop != "create") {
1413 container[prop] = paper[prop];
1416 plugins.call(container, container, R.fn);
1418 container.raphael = R;
1421 paper.remove = function () {
1422 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1428 var path2vml = function (path) {
1429 var pa = path2curve(path);
1430 for (var i = 0, ii = pa[ln]; i < ii; i++) {
1431 pa[i][0] = (pa[i][0] + "").toLowerCase();
1432 pa[i][0] == "z" && (pa[i][0] = "x");
1433 for (var j = 1, jj = pa[i][ln]; j < jj; j++) {
1434 pa[i][j] = ~~(pa[i][j] + .5);
1439 R.toString = function () {
1440 return "Your browser doesn\u2019t support SVG. Assuming it is Internet Explorer and falling down to VML.\nYou are running Rapha\u00ebl " + this.version;
1442 var thePath = function (pathString, VML) {
1443 var g = createNode("group"), gl = g.style;
1444 gl.position = "absolute";
1447 gl.width = VML.width + "px";
1448 gl.height = VML.height + "px";
1449 g.coordsize = VML.coordsize;
1450 g.coordorigin = VML.coordorigin;
1451 var el = createNode("shape"), ol = el.style;
1452 ol.width = VML.width + "px";
1453 ol.height = VML.height + "px";
1455 el.coordsize = this.coordsize;
1456 el.coordorigin = this.coordorigin;
1458 var p = new Element(el, g, VML);
1459 p.isAbsolute = true;
1464 p.attrs.path = R.parsePathString(pathString);
1465 p.node.path = path2vml(p.attrs.path);
1467 setFillAndStroke(p, {fill: "none", stroke: "#000"});
1469 VML.canvas.appendChild(g);
1472 var setFillAndStroke = function (o, params) {
1473 o.attrs = o.attrs || {};
1479 for (var par in params) if (params.hasOwnProperty(par)) {
1480 a[par] = params[par];
1482 params.href && (node.href = params.href);
1483 params.title && (node.title = params.title);
1484 params.target && (node.target = params.target);
1485 if (params.path && o.type == "path") {
1486 a.path = R.parsePathString(params.path);
1487 node.path = path2vml(a.path);
1489 if (params.rotation != null) {
1490 o.rotate(params.rotation, true);
1492 if (params.translation) {
1493 xy = (params.translation + "").split(separator);
1494 o.translate(xy[0], xy[1]);
1497 xy = (params.scale + "").split(separator);
1498 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1500 if ("clip-rect" in params) {
1501 var rect = (params["clip-rect"] + "").split(separator);
1502 if (rect[ln] == 4) {
1503 rect[2] = +rect[2] + (+rect[0]);
1504 rect[3] = +rect[3] + (+rect[1]);
1505 var div = node.clipRect || doc.createElement("div"),
1507 group = node.parentNode;
1508 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1509 if (!node.clipRect) {
1510 dstyle.position = "absolute";
1513 dstyle.width = o.paper.width + "px";
1514 dstyle.height = o.paper.height + "px";
1515 group.parentNode.insertBefore(div, group);
1516 div.appendChild(group);
1517 node.clipRect = div;
1520 if (!params["clip-rect"]) {
1521 node.clipRect && (node.clipRect.style.clip = "");
1524 if (o.type == "image" && params.src) {
1525 node.src = params.src;
1527 if (o.type == "image" && params.opacity) {
1528 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1529 s.filter = (node.filterMatrix || "") + (node.filterOpacity || "");
1531 params.font && (s.font = params.font);
1532 params["font-family"] && (s.fontFamily = '"' + params["font-family"].split(",")[0].replace(/^['"]+|['"]+$/g, "") + '"');
1533 params["font-size"] && (s.fontSize = params["font-size"]);
1534 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1535 params["font-style"] && (s.fontStyle = params["font-style"]);
1536 if (params.opacity != null ||
1537 params["stroke-width"] != null ||
1538 params.fill != null ||
1539 params.stroke != null ||
1540 params["stroke-width"] != null ||
1541 params["stroke-opacity"] != null ||
1542 params["fill-opacity"] != null ||
1543 params["stroke-dasharray"] != null ||
1544 params["stroke-miterlimit"] != null ||
1545 params["stroke-linejoin"] != null ||
1546 params["stroke-linecap"] != null) {
1547 node = o.shape || node;
1548 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1550 !fill && (newfill = fill = createNode("fill"));
1551 if ("fill-opacity" in params || "opacity" in params) {
1552 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1553 opacity < 0 && (opacity = 0);
1554 opacity > 1 && (opacity = 1);
1555 fill.opacity = opacity;
1557 params.fill && (fill.on = true);
1558 if (fill.on == null || params.fill == "none") {
1561 if (fill.on && params.fill) {
1562 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1564 fill.src = isURL[1];
1567 fill.color = R.getRGB(params.fill).hex;
1569 fill.type = "solid";
1570 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + "").charAt(0) != "r") && addGradientFill(res, params.fill)) {
1572 a.gradient = params.fill;
1576 newfill && node.appendChild(fill);
1577 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1579 !stroke && (newstroke = stroke = createNode("stroke"));
1580 if ((params.stroke && params.stroke != "none") ||
1581 params["stroke-width"] ||
1582 params["stroke-opacity"] != null ||
1583 params["stroke-dasharray"] ||
1584 params["stroke-miterlimit"] ||
1585 params["stroke-linejoin"] ||
1586 params["stroke-linecap"]) {
1589 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1590 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1591 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1592 opacity < 0 && (opacity = 0);
1593 opacity > 1 && (opacity = 1);
1594 stroke.opacity = opacity;
1595 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1596 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1597 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1598 params["stroke-width"] && (stroke.weight = (win[pf](params["stroke-width"]) || 1) * 12 / 16);
1599 if (params["stroke-dasharray"]) {
1603 "-.": "shortdashdot",
1604 "-..": "shortdashdotdot",
1609 "--.": "longdashdot",
1610 "--..": "longdashdotdot"
1612 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1614 newstroke && node.appendChild(stroke);
1616 if (res.type == "text") {
1617 var s = res.paper.span.style;
1618 a.font && (s.font = a.font);
1619 a["font-family"] && (s.fontFamily = a["font-family"]);
1620 a["font-size"] && (s.fontSize = a["font-size"]);
1621 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1622 a["font-style"] && (s.fontStyle = a["font-style"]);
1623 res.node.string && (res.paper.span.innerHTML = res.node.string.replace(/</g, "<").replace(/&/g, "&").replace(/\n/g, "<br>"));
1624 res.W = a.w = res.paper.span.offsetWidth;
1625 res.H = a.h = res.paper.span.offsetHeight;
1627 res.Y = a.y + ~~(res.H / 2 + .5);
1629 // text-anchor emulation
1630 switch (a["text-anchor"]) {
1632 res.node.style["v-text-align"] = "left";
1633 res.bbx = ~~(res.W / 2 + .5);
1636 res.node.style["v-text-align"] = "right";
1637 res.bbx = -~~(res.W / 2 + .5);
1640 res.node.style["v-text-align"] = "center";
1645 var addGradientFill = function (o, gradient) {
1646 o.attrs = o.attrs || {};
1647 var attrs = o.attrs,
1648 fill = o.node.getElementsByTagName("fill"),
1651 o.attrs.gradient = gradient;
1652 gradient = (gradient + "").replace(/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, fx, fy) {
1657 if (Math.pow(fx - .5, 2) + Math.pow(fy - .5, 2) > .25) {
1658 fy = Math.sqrt(.25 - Math.pow(fx - .5, 2)) + .5;
1660 fxfy = fx + " " + fy;
1664 gradient = gradient.split(/\s*\-\s*/);
1665 if (type == "linear") {
1666 var angle = gradient.shift();
1667 angle = -win[pf](angle);
1672 var dots = parseDots(gradient);
1676 o = o.shape || o.node;
1677 fill = fill[0] || createNode("fill");
1680 fill.method = "none";
1681 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1682 fill.color = dots[0].color;
1683 fill.color2 = dots[dots[ln] - 1].color;
1685 for (var i = 0, ii = dots[ln]; i < ii; i++) {
1686 dots[i].offset && clrs.push(dots[i].offset + " " + dots[i].color);
1688 if (clrs[ln] && fill.colors) {
1689 fill.colors.value = clrs.join(",");
1691 fill.colors.value = "0% " + fill.color;
1693 if (type == "radial") {
1694 fill.focus = "100%";
1695 fill.focussize = fxfy;
1696 fill.focusposition = fxfy;
1698 fill.angle = (270 - angle) % 360;
1703 var Element = function (node, group, vml) {
1710 node.raphael = this;
1724 Element[proto].rotate = function (deg, cx, cy) {
1727 return [this._.rt.deg, this._.rt.cx, this._.rt.cy].join(" ");
1729 return this._.rt.deg;
1731 deg = (deg + "").split(separator);
1733 cx = win[pf](deg[1]);
1734 cy = win[pf](deg[2]);
1736 deg = win[pf](deg[0]);
1738 this._.rt.deg = deg;
1740 this._.rt.deg += deg;
1742 cy == null && (cx = null);
1745 this.setBox(this.attrs, cx, cy);
1746 this.Group.style.rotation = this._.rt.deg;
1747 // gradient fix for rotation. TODO
1748 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1749 // fill = fill[0] || {};
1750 // var b = ((360 - this._.rt.deg) - 270) % 360;
1751 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1754 Element[proto].setBox = function (params, cx, cy) {
1755 var gs = this.Group.style,
1756 os = (this.shape && this.shape.style) || this.node.style;
1757 params = params || {};
1758 for (var i in params) if (params.hasOwnProperty(i)) {
1759 this.attrs[i] = params[i];
1761 cx = cx || this._.rt.cx;
1762 cy = cy || this._.rt.cy;
1763 var attr = this.attrs,
1768 switch (this.type) {
1770 x = attr.cx - attr.r;
1771 y = attr.cy - attr.r;
1775 x = attr.cx - attr.rx;
1776 y = attr.cy - attr.ry;
1784 w = attr.width || 0;
1785 h = attr.height || 0;
1788 this.textpath.v = ["m", ~~(attr.x + .5), ", ", ~~(attr.y - 2 + .5), "l", ~~(attr.x + .5) + 1, ", ", ~~(attr.y - 2 + .5)].join("");
1789 x = attr.x - ~~(this.W / 2 + .5);
1790 y = attr.y - this.H / 2;
1795 if (!this.attrs.path) {
1798 w = this.paper.width;
1799 h = this.paper.height;
1801 var dim = pathDimensions(this.attrs.path);
1811 w = this.paper.width;
1812 h = this.paper.height;
1815 cx = (cx == null) ? x + w / 2 : cx;
1816 cy = (cy == null) ? y + h / 2 : cy;
1817 var left = cx - this.paper.width / 2,
1818 top = cy - this.paper.height / 2;
1819 if (this.type == "path" || this.type == "text") {
1820 (gs.left != left + "px") && (gs.left = left + "px");
1821 (gs.top != top + "px") && (gs.top = top + "px");
1822 this.X = this.type == "text" ? x : -left;
1823 this.Y = this.type == "text" ? y : -top;
1826 (os.left != -left + "px") && (os.left = -left + "px");
1827 (os.top != -top + "px") && (os.top = -top + "px");
1829 (gs.left != left + "px") && (gs.left = left + "px");
1830 (gs.top != top + "px") && (gs.top = top + "px");
1835 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
1836 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
1837 (os.left != x - left + "px") && (os.left = x - left + "px");
1838 (os.top != y - top + "px") && (os.top = y - top + "px");
1839 (os.width != w + "px") && (os.width = w + "px");
1840 (os.height != h + "px") && (os.height = h + "px");
1841 var arcsize = (+params.r || 0) / (Math.min(w, h));
1842 if (this.type == "rect" && this.arcsize != arcsize && (arcsize || this.arcsize)) {
1843 // We should replace element with the new one
1844 var o = createNode(arcsize ? "roundrect" : "rect");
1845 o.arcsize = arcsize;
1846 this.Group.appendChild(o);
1847 this.node.parentNode.removeChild(this.node);
1849 this.arcsize = arcsize;
1850 setFillAndStroke(this, this.attrs);
1851 this.setBox(this.attrs);
1855 Element[proto].hide = function () {
1856 this.Group.style.display = "none";
1859 Element[proto].show = function () {
1860 this.Group.style.display = "block";
1863 Element[proto].getBBox = function () {
1864 if (this.type == "path") {
1865 return pathDimensions(this.attrs.path);
1868 x: this.X + (this.bbx || 0),
1874 Element[proto].remove = function () {
1875 this[0].parentNode.removeChild(this[0]);
1876 this.Group.parentNode.removeChild(this.Group);
1877 this.shape && this.shape.parentNode.removeChild(this.shape);
1879 Element[proto].attr = function () {
1880 if (arguments[ln] == 1 && R.is(arguments[0], "string")) {
1881 if (arguments[0] == "translation") {
1882 return this.translate();
1884 if (arguments[0] == "rotation") {
1885 return this.rotate();
1887 if (arguments[0] == "scale") {
1888 return this.scale();
1890 return this.attrs[arguments[0]];
1892 if (this.attrs && arguments[ln] == 1 && R.is(arguments[0], "array")) {
1894 for (var i = 0, ii = arguments[0][ln]; i < ii; i++) {
1895 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1900 if (arguments[ln] == 2) {
1902 params[arguments[0]] = arguments[1];
1904 if (arguments[ln] == 1 && R.is(arguments[0], "object")) {
1905 params = arguments[0];
1908 if (params.gradient && (this.type in {circle: 1, ellipse: 1} || (params.gradient + "").charAt(0) != "r")) {
1909 addGradientFill(this, params.gradient);
1911 if (params.text && this.type == "text") {
1912 this.node.string = params.text;
1914 setFillAndStroke(this, params);
1915 this.setBox(this.attrs);
1919 Element[proto].toFront = function () {
1920 this.Group.parentNode.appendChild(this.Group);
1923 Element[proto].toBack = function () {
1924 if (this.Group.parentNode.firstChild != this.Group) {
1925 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1929 Element[proto].insertAfter = function (element) {
1930 if (element.Group.nextSibling) {
1931 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1933 element.Group.parentNode.appendChild(this.Group);
1937 Element[proto].insertBefore = function (element) {
1938 element.Group.parentNode.insertBefore(this.Group, element.Group);
1942 var theCircle = function (vml, x, y, r) {
1943 var g = createNode("group"),
1944 o = createNode("oval"),
1946 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1947 g.coordsize = vml.coordsize;
1948 g.coordorigin = vml.coordorigin;
1950 var res = new Element(o, g, vml);
1951 res.type = "circle";
1952 setFillAndStroke(res, {stroke: "#000", fill: "none"});
1956 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1957 vml.canvas.appendChild(g);
1960 var theRect = function (vml, x, y, w, h, r) {
1961 var g = createNode("group"),
1962 o = createNode(r ? "roundrect" : "rect"),
1963 arcsize = (+r || 0) / (Math.min(w, h));
1964 o.arcsize = arcsize;
1965 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1966 g.coordsize = vml.coordsize;
1967 g.coordorigin = vml.coordorigin;
1969 var res = new Element(o, g, vml);
1971 setFillAndStroke(res, {stroke: "#000"});
1972 res.arcsize = arcsize;
1973 res.setBox({x: x, y: y, width: w, height: h, r: +r});
1974 vml.canvas.appendChild(g);
1977 var theEllipse = function (vml, x, y, rx, ry) {
1978 var g = createNode("group"),
1979 o = createNode("oval"),
1981 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1982 g.coordsize = vml.coordsize;
1983 g.coordorigin = vml.coordorigin;
1985 var res = new Element(o, g, vml);
1986 res.type = "ellipse";
1987 setFillAndStroke(res, {stroke: "#000"});
1992 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
1993 vml.canvas.appendChild(g);
1996 var theImage = function (vml, src, x, y, w, h) {
1997 var g = createNode("group"),
1998 o = createNode("image"),
2000 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2001 g.coordsize = vml.coordsize;
2002 g.coordorigin = vml.coordorigin;
2005 var res = new Element(o, g, vml);
2007 res.attrs.src = src;
2012 res.setBox({x: x, y: y, width: w, height: h});
2013 vml.canvas.appendChild(g);
2016 var theText = function (vml, x, y, text) {
2017 var g = createNode("group"),
2018 el = createNode("shape"),
2020 path = createNode("path"),
2022 o = createNode("textpath");
2023 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2024 g.coordsize = vml.coordsize;
2025 g.coordorigin = vml.coordorigin;
2026 path.v = R.format("m{0},{1}l{2},{1}", ~~(x + .5), ~~(y + .5), ~~(x + .5) + 1);
2027 path.textpathok = true;
2028 ol.width = vml.width;
2029 ol.height = vml.height;
2033 el.appendChild(path);
2035 var res = new Element(o, g, vml);
2037 res.textpath = path;
2039 res.attrs.text = text;
2044 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2046 vml.canvas.appendChild(g);
2049 var setSize = function (width, height) {
2050 var cs = this.canvas.style;
2051 this.width = win[pf](width || this.width);
2052 this.height = win[pf](height || this.height);
2053 cs.width = this.width + "px";
2054 cs.height = this.height + "px";
2055 cs.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2056 this.coordsize = this.width + " " + this.height;
2059 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2061 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2062 var createNode = function (tagName) {
2063 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2066 var createNode = function (tagName) {
2067 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2070 var create = function () {
2071 var con = getContainer[ap](null, arguments),
2072 container = con.container,
2073 height = con.height,
2079 throw new Error("VML container not found.");
2082 c = res.canvas = doc.createElement("div"),
2084 width = win[pf](width) || 512;
2085 height = win[pf](height) || 342;
2087 res.height = height;
2088 res.coordsize = width + " " + height;
2089 res.coordorigin = "0 0";
2090 res.span = doc.createElement("span");
2091 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2092 c.appendChild(res.span);
2093 cs.cssText = R.format("width:{0}px;height:{1}px;position:absolute;clip:rect(0 {0}px {1}px 0)", width, height);
2094 if (container == 1) {
2095 doc.body.appendChild(c);
2105 container.style.width = width;
2106 container.style.height = height;
2107 if (container.firstChild) {
2108 container.insertBefore(c, container.firstChild);
2110 container.appendChild(c);
2113 for (var prop in paper) if (paper.hasOwnProperty(prop)) {
2114 res[prop] = paper[prop];
2116 plugins.call(res, res, R.fn);
2117 res.clear = function () {
2118 while (c.firstChild) {
2119 c.removeChild(c.firstChild);
2125 paper.remove = function () {
2126 this.canvas.parentNode.removeChild(this.canvas);
2131 // Safari or Chrome (WebKit) rendering bug workaround method
2132 if ({"Apple Computer, Inc.": 1, "Google Inc.": 1}[navigator.vendor]) {
2133 paper.safari = function () {
2134 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2135 setTimeout(function () {rect.remove();});
2138 paper.safari = function () {};
2142 var addEvent = (function () {
2143 if (doc.addEventListener) {
2144 return function (obj, type, fn, element) {
2145 var f = function (e) {
2146 return fn.call(element, e);
2148 obj.addEventListener(type, f, false);
2149 return function () {
2150 obj.removeEventListener(type, f, false);
2154 } else if (doc.attachEvent) {
2155 return function (obj, type, fn, element) {
2156 var f = function (e) {
2157 return fn.call(element, e || win.event);
2159 obj.attachEvent("on" + type, f);
2160 var detacher = function () {
2161 obj.detachEvent("on" + type, f);
2164 if (type == "mouseover") {
2165 obj.attachEvent("onmouseenter", f);
2166 return function () {
2167 obj.detachEvent("onmouseenter", f);
2170 } else if (type == "mouseout") {
2171 obj.attachEvent("onmouseleave", f);
2172 return function () {
2173 obj.detachEvent("onmouseleave", f);
2181 for (var i = events[ln]; i--;) {
2182 (function (eventName) {
2183 Element[proto][eventName] = function (fn) {
2184 if (R.is(fn, "function")) {
2185 this.events = this.events || {};
2186 this.events[eventName] = this.events[eventName] || {};
2187 this.events[eventName][fn] = this.events[eventName][fn] || [];
2188 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2192 Element[proto]["un" + eventName] = function (fn) {
2194 this.events[eventName] &&
2195 this.events[eventName][fn] &&
2196 this.events[eventName][fn][ln] &&
2197 this.events[eventName][fn].shift()() &&
2198 !this.events[eventName][fn][ln] &&
2199 delete this.events[eventName][fn];
2204 paper.circle = function (x, y, r) {
2205 return theCircle(this, x || 0, y || 0, r || 0);
2207 paper.rect = function (x, y, w, h, r) {
2208 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2210 paper.ellipse = function (x, y, rx, ry) {
2211 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2213 paper.path = function (pathString) {
2214 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += "");
2215 return thePath(R.format[ap](R, arguments), this);
2217 paper.image = function (src, x, y, w, h) {
2218 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2220 paper.text = function (x, y, text) {
2221 return theText(this, x || 0, y || 0, text || "");
2223 paper.set = function (itemsArray) {
2224 arguments[ln] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[ln]));
2225 return new Set(itemsArray);
2227 paper.setSize = setSize;
2228 Element[proto].stop = function () {
2229 clearTimeout(this.animation_in_progress);
2232 Element[proto].scale = function (x, y, cx, cy) {
2233 if (x == null && y == null) {
2234 return {x: this._.sx, y: this._.sy, toString: function () { return this.x + " " + this.y; }};
2244 var bb = this.getBBox(),
2245 rcx = bb.x + bb.width / 2,
2246 rcy = bb.y + bb.height / 2,
2249 cx = (+cx || cx == 0) ? cx : rcx;
2250 cy = (+cy || cy == 0) ? cy : rcy;
2251 var dirx = ~~(x / Math.abs(x)),
2252 diry = ~~(y / Math.abs(y)),
2253 s = this.node.style,
2254 ncx = cx + (rcx - cx) * dirx * kx,
2255 ncy = cy + (rcy - cy) * diry * ky;
2256 switch (this.type) {
2259 var neww = a.width * dirx * kx,
2260 newh = a.height * diry * ky,
2261 newr = a.r * Math.min(kx, ky),
2262 newx = ncx - neww / 2,
2263 newy = ncy - newh / 2;
2277 r: a.r * Math.min(kx, ky),
2283 var path = pathToRelative(a.path),
2285 for (var i = 0, ii = path[ln]; i < ii; i++) {
2287 if (p[0].toUpperCase() == "M" && skip) {
2292 if (R.svg && p[0].toUpperCase() == "A") {
2293 p[path[i][ln] - 2] *= kx;
2294 p[path[i][ln] - 1] *= ky;
2297 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2299 for (var j = 1, jj = p[ln]; j < jj; j++) {
2300 p[j] *= (j % 2) ? kx : ky;
2304 var dim2 = pathDimensions(path),
2305 dx = ncx - dim2.x - dim2.width / 2,
2306 dy = ncy - dim2.y - dim2.height / 2;
2310 this.attr({path: path});
2313 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2314 if (this.transformations) {
2315 this.transformations[2] = "scale(".concat(dirx, ",", diry, ")");
2316 this.node.setAttribute("transform", this.transformations.join(" "));
2317 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2318 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2319 this.attr({x: dx, y: dy});
2323 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=".concat(dirx,
2324 ", M12=0, M21=0, M22=", diry,
2325 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2326 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2329 if (this.transformations) {
2330 this.transformations[2] = "";
2331 this.node.setAttribute("transform", this.transformations.join(" "));
2335 this.node.filterMatrix = "";
2336 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2339 a.scale = [x, y, cx, cy].join(" ");
2346 // animation easing formulas
2347 R.easing_formulas = {
2348 linear: function (n) {
2352 return Math.pow(n, 3);
2355 return Math.pow(n - 1, 3) + 1;
2357 "<>": function (n) {
2360 return Math.pow(n, 3) / 2;
2363 return (Math.pow(n, 3) + 2) / 2;
2365 backIn: function (n) {
2367 return n * n * ((s + 1) * n - s);
2369 backOut: function (n) {
2372 return n * n * ((s + 1) * n + s) + 1;
2374 elastic: function (n) {
2375 if (n == 0 || n == 1) {
2380 return Math.pow(2, -10 * n) * Math.sin((n - s) * (2 * Math.PI) / p) + 1;
2382 bounce: function (n) {
2391 l = s * n * n + .75;
2393 if (n < (2.5 / p)) {
2395 l = s * n * n + .9375;
2398 l = s * n * n + .984375;
2406 Element[proto].animate = function (params, ms, easing, callback) {
2407 clearTimeout(this.animation_in_progress);
2408 if (R.is(easing, "function") || !easing) {
2409 callback = easing || null;
2415 for (var attr in params) if (params.hasOwnProperty(attr)) {
2416 if (attr in availableAnimAttrs) {
2417 from[attr] = this.attr(attr);
2418 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2419 to[attr] = params[attr];
2420 switch (availableAnimAttrs[attr]) {
2422 diff[attr] = (to[attr] - from[attr]) / ms;
2425 from[attr] = R.getRGB(from[attr]);
2426 var toColour = R.getRGB(to[attr]);
2428 r: (toColour.r - from[attr].r) / ms,
2429 g: (toColour.g - from[attr].g) / ms,
2430 b: (toColour.b - from[attr].b) / ms
2434 var pathes = path2curve(from[attr], to[attr]);
2435 from[attr] = pathes[0];
2436 to[attr] = pathes[1];
2438 for (var i = 0, ii = from[attr][ln]; i < ii; i++) {
2439 diff[attr][i] = [0];
2440 for (var j = 1, jj = from[attr][i][ln]; j < jj; j++) {
2441 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2446 var values = (params[attr] + "").split(separator),
2447 from2 = (from[attr] + "").split(separator);
2450 from[attr] = [0, 0];
2451 diff[attr] = [values[0] / ms, values[1] / ms];
2454 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2455 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2458 params[attr] = values;
2459 from[attr] = (from[attr] + "").split(separator);
2460 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2463 from[attr] = (from[attr] + "").split(separator);
2467 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2475 var start = +new Date,
2477 upto255 = function (color) {
2478 return color > 255 ? 255 : color;
2482 var time = new Date - start,
2486 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2487 for (var attr in from) if (from.hasOwnProperty(attr)) {
2488 switch (availableAnimAttrs[attr]) {
2490 now = +from[attr] + pos * ms * diff[attr];
2494 upto255(~~(from[attr].r + pos * ms * diff[attr].r + .5)),
2495 upto255(~~(from[attr].g + pos * ms * diff[attr].g + .5)),
2496 upto255(~~(from[attr].b + pos * ms * diff[attr].b + .5))
2501 for (var i = 0, ii = from[attr][ln]; i < ii; i++) {
2502 now[i] = [from[attr][i][0]];
2503 for (var j = 1, jj = from[attr][i][ln]; j < jj; j++) {
2504 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2506 now[i] = now[i].join(" ");
2508 now = now.join(" ");
2513 var x = diff[attr][0] * (time - prev),
2514 y = diff[attr][1] * (time - prev);
2517 now = [x, y].join(" ");
2520 now = +from[attr][0] + pos * ms * diff[attr][0];
2521 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2524 now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in params[attr] ? params[attr][2] : ""), (3 in params[attr] ? params[attr][3] : "")].join(" ");
2530 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2539 that.animation_in_progress = setTimeout(tick);
2540 R.svg && paper.safari();
2542 (t.x || t.y) && that.translate(-t.x, -t.y);
2544 clearTimeout(that.animation_in_progress);
2545 R.svg && paper.safari();
2546 (R.is(callback, "function")) && callback.call(that);
2552 Element[proto].translate = function (x, y) {
2554 return {x: this._.tx, y: this._.ty};
2558 switch (this.type) {
2561 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2566 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2569 var path = pathToRelative(this.attrs.path);
2572 this.attr({path: path});
2577 Element[proto].toString = function () {
2578 return "Rapha\u00ebl\u2019s object";
2583 var Set = function (items) {
2587 for (var i = 0, ii = items[ln]; i < ii; i++) {
2588 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2589 this[this.items[ln]] = this.items[this.items[ln]] = items[i];
2595 Set[proto].push = function () {
2598 for (var i = 0, ii = arguments[ln]; i < ii; i++) {
2599 item = arguments[i];
2600 if (item && (item.constructor == Element || item.constructor == Set)) {
2601 len = this.items[ln];
2602 this[len] = this.items[len] = item;
2608 Set[proto].pop = function () {
2609 delete this[this[ln]--];
2610 return this.items.pop();
2612 for (var method in Element[proto]) if (Element[proto].hasOwnProperty(method)) {
2613 Set[proto][method] = (function (methodname) {
2614 return function () {
2615 for (var i = 0, ii = this.items[ln]; i < ii; i++) {
2616 this.items[i][methodname][ap](this.items[i], arguments);
2622 Set[proto].attr = function (name, value) {
2623 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2624 for (var j = 0, jj = name[ln]; j < jj; j++) {
2625 this.items[j].attr(name[j]);
2628 for (var i = 0, ii = this.items[ln]; i < ii; i++) {
2629 this.items[i].attr[ap](this.items[i], arguments);
2634 Set[proto].animate = function (params, ms, easing, callback) {
2635 if (R.is(easing, "function") || !easing) {
2636 callback = easing || null;
2638 var len = this.items[ln],
2642 var collector = function () {
2643 !--len && callback.call(set);
2646 this.items[i].animate(params, ms, easing || collector, collector);
2650 this.items[i].animate(params, ms, easing);
2656 Set[proto].getBBox = function () {
2661 for (var i = this.items[ln]; i--;) {
2662 var box = this.items[i].getBBox();
2665 w.push(box.x + box.width);
2666 h.push(box.y + box.height);
2668 x = Math.min[ap](Math, x);
2669 y = Math.min[ap](Math, y);
2673 width: Math.max[ap](Math, w) - x,
2674 height: Math.max[ap](Math, h) - y
2678 R.registerFont = function (font) {
2682 this.fonts = this.fonts || {};
2688 family = font.face["font-family"];
2689 for (var prop in font.face) if (font.face.hasOwnProperty(prop)) {
2690 fontcopy.face[prop] = font.face[prop];
2692 if (this.fonts[family]) {
2693 this.fonts[family].push(fontcopy);
2695 this.fonts[family] = [fontcopy];
2698 fontcopy.face["units-per-em"] = win[pi](font.face["units-per-em"], 10);
2699 for (var glyph in font.glyphs) if (font.glyphs.hasOwnProperty(glyph)) {
2700 var path = font.glyphs[glyph];
2701 fontcopy.glyphs[glyph] = {
2704 d: path.d && "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
2705 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2709 for (var k in path.k) if (path.hasOwnProperty(k)) {
2710 fontcopy.glyphs[glyph].k[k] = path.k[k];
2717 paper.getFont = function (family, weight, style, stretch) {
2718 stretch = stretch || "normal";
2719 style = style || "normal";
2720 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2721 var font = R.fonts[family];
2723 var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, "") + "(\\s|$)", "i");
2724 for (var fontName in R.fonts) if (R.fonts.hasOwnProperty(fontName)) {
2725 if (name.test(fontName)) {
2726 font = R.fonts[fontName];
2733 for (var i = 0, ii = font[ln]; i < ii; i++) {
2735 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2742 paper.print = function (x, y, string, font, size) {
2743 var out = this.set(),
2744 letters = (string + "").split(""),
2748 R.is(font, "string") && (font = this.getFont(font));
2750 scale = (size || 16) / font.face["units-per-em"];
2751 for (var i = 0, ii = letters[ln]; i < ii; i++) {
2752 var prev = i && font.glyphs[letters[i - 1]] || {},
2753 curr = font.glyphs[letters[i]];
2754 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
2755 curr && curr.d && out.push(this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
2757 out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
2762 R.format = function (token) {
2763 var args = R.is(arguments[1], "array") ? [0].concat(arguments[1]) : arguments;
2764 token && R.is(token, "string") && args[ln] - 1 && (token = token.replace(/\{(\d+)\}/g, function (str, i) {
2765 return args[++i] == null ? "" : args[i];
2769 R.ninja = function () {
2770 var r = window.Raphael, u;
2771 if (oldRaphael.was) {
2772 window.Raphael = oldRaphael.is;
2775 delete window.Raphael;
2782 R.el = Element[proto];