2 * Raphael 1.2.4 - 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);
30 appendChild = "appendChild",
34 events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"],
35 has = "hasOwnProperty",
36 isit = /^\[object\s+|\]$/gi,
40 lowerCase = String[proto].toLowerCase,
44 toString = "toString",
45 objectToString = Object[proto][toString],
50 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
53 setAttribute = "setAttribute",
57 upperCase = String[proto].toUpperCase,
58 availableAttrs = {"clip-rect": "0 0 10e9 10e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
59 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},
62 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
63 R.svg = !(R.vml = R.type == "VML");
67 R.is = function (o, type) {
68 type = lowerCase.call(type);
69 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o)[rp](isit, E)) == type;
71 R.setWindow = function (newwin) {
76 var toHex = function (color) {
78 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
79 var trim = /^\s+|\s+$/g;
80 toHex = cacher(function (color) {
82 color = (color + E).replace(trim, E);
84 var document = new ActiveXObject("htmlfile");
85 document.write("<body>");
89 bod = createPopup().document.body;
91 var range = bod.createTextRange();
93 bod.style.color = color;
94 var value = range.queryCommandValue("ForeColor");
95 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
96 return "#" + ("000000" + value[toString](16)).slice(-6);
102 var i = doc.createElement("i");
103 i.className = "Rapha\xebl Colour Picker";
104 i.style.cssText = "display:none";
105 doc.body[appendChild](i);
106 toHex = cacher(function (color) {
107 i.style.color = color;
108 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
113 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
114 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
122 if (brightness == 0) {
123 return {r: 0, g: 0, b: 0, hex: "#000"};
125 if (hue > 1 || saturation > 1 || brightness > 1) {
132 p = brightness * (1 - saturation),
133 q = brightness * (1 - (saturation * f)),
134 t = brightness * (1 - (saturation * (1 - f)));
135 red = [brightness, q, p, p, t, brightness, brightness][i];
136 green = [t, brightness, brightness, q, p, p, t][i];
137 blue = [p, p, t, brightness, brightness, q, p][i];
141 var rgb = {r: red, g: green, b: blue},
142 r = (~~red)[toString](16),
143 g = (~~green)[toString](16),
144 b = (~~blue)[toString](16);
148 rgb.hex = "#" + r + g + b;
151 R.rgb2hsb = cacher(function (red, green, blue) {
152 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
157 if (R.is(red, "string")) {
158 var clr = R.getRGB(red);
163 if (red > 1 || green > 1 || blue > 1) {
168 var max = mmax(red, green, blue),
169 min = mmin(red, green, blue),
174 return {h: 0, s: 0, b: max};
176 var delta = (max - min);
177 saturation = delta / max;
179 hue = (green - blue) / delta;
180 } else if (green == max) {
181 hue = 2 + ((blue - red) / delta);
183 hue = 4 + ((red - green) / delta);
189 return {h: hue, s: saturation, b: brightness};
191 var p2s = /,?([achlmqrstvxz]),?/gi;
192 R._path2string = function () {
193 return this.join(",")[rp](p2s, "$1");
195 function cacher(f, scope, postprocessor) {
197 var arg = Array[proto].slice.call(arguments, 0),
198 args = arg[join]("\u25ba"),
199 cache = newf.cache = newf.cache || {},
200 count = newf.count = newf.count || [];
201 if (cache[has](args)) {
202 return postprocessor ? postprocessor(cache[args]) : cache[args];
204 count[length] >= 1e3 && delete cache[count.shift()];
206 cache[args] = f[apply](scope, arg);
207 return postprocessor ? postprocessor(cache[args]) : cache[args];
212 R.getRGB = cacher(function (colour) {
213 if (!colour || !!((colour + E).indexOf("-") + 1)) {
214 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
217 if (colour == "none") {
218 return {r: -1, g: -1, b: -1, hex: "none"};
220 !({hs: 1, rg: 1})[has](colour.substring(0, 2)) && (colour = toHex(colour));
225 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);
228 blue = toInt(rgb[2].substring(5), 16);
229 green = toInt(rgb[2].substring(3, 5), 16);
230 red = toInt(rgb[2].substring(1, 3), 16);
233 blue = toInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
234 green = toInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
235 red = toInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
238 rgb = rgb[4][split](/\s*,\s*/);
239 red = toFloat(rgb[0]);
240 green = toFloat(rgb[1]);
241 blue = toFloat(rgb[2]);
244 rgb = rgb[5][split](/\s*,\s*/);
245 red = toFloat(rgb[0]) * 2.55;
246 green = toFloat(rgb[1]) * 2.55;
247 blue = toFloat(rgb[2]) * 2.55;
250 rgb = rgb[6][split](/\s*,\s*/);
251 red = toFloat(rgb[0]);
252 green = toFloat(rgb[1]);
253 blue = toFloat(rgb[2]);
254 return R.hsb2rgb(red, green, blue);
257 rgb = rgb[7][split](/\s*,\s*/);
258 red = toFloat(rgb[0]) * 2.55;
259 green = toFloat(rgb[1]) * 2.55;
260 blue = toFloat(rgb[2]) * 2.55;
261 return R.hsb2rgb(red, green, blue);
263 rgb = {r: red, g: green, b: blue};
264 var r = (~~red)[toString](16),
265 g = (~~green)[toString](16),
266 b = (~~blue)[toString](16);
270 rgb.hex = "#" + r + g + b;
273 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
275 R.getColor = function (value) {
276 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
277 rgb = this.hsb2rgb(start.h, start.s, start.b);
282 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
286 R.getColor.reset = function () {
290 R.parsePathString = cacher(function (pathString) {
294 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
296 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
297 data = pathClone(pathString);
300 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
302 name = lowerCase.call(b);
303 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
304 b && params[push](+b);
306 while (params[length] >= paramCounts[name]) {
307 data[push]([b][concat](params.splice(0, paramCounts[name])));
308 if (!paramCounts[name]) {
314 data[toString] = R._path2string;
317 var pathDimensions = cacher(function (path) {
319 return {x: 0, y: 0, width: 0, height: 0};
321 path = path2curve(path);
326 for (var i = 0, ii = path[length]; i < ii; i++) {
327 if (path[i][0] == "M") {
333 var dim = curveDim(x, y, path[i][1], path[i][2], path[i][3], path[i][4], path[i][5], path[i][6]);
334 X = X[concat](dim.min.x, dim.max.x);
335 Y = Y[concat](dim.min.y, dim.max.y);
338 var xmin = mmin[apply](0, X),
339 ymin = mmin[apply](0, Y);
343 width: mmax[apply](0, X) - xmin,
344 height: mmax[apply](0, Y) - ymin
347 pathClone = function (pathArray) {
349 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
350 pathArray = R.parsePathString(pathArray);
352 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
354 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
355 res[i][j] = pathArray[i][j];
358 res[toString] = R._path2string;
361 pathToRelative = cacher(function (pathArray) {
362 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
363 pathArray = R.parsePathString(pathArray);
371 if (pathArray[0][0] == "M") {
377 res[push](["M", x, y]);
379 for (var i = start, ii = pathArray[length]; i < ii; i++) {
382 if (pa[0] != lowerCase.call(pa[0])) {
383 r[0] = lowerCase.call(pa[0]);
391 r[6] = +(pa[6] - x).toFixed(3);
392 r[7] = +(pa[7] - y).toFixed(3);
395 r[1] = +(pa[1] - y).toFixed(3);
401 for (var j = 1, jj = pa[length]; j < jj; j++) {
402 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
411 for (var k = 0, kk = pa[length]; k < kk; k++) {
415 var len = res[i][length];
422 x += +res[i][len - 1];
425 y += +res[i][len - 1];
428 x += +res[i][len - 2];
429 y += +res[i][len - 1];
432 res[toString] = R._path2string;
435 pathToAbsolute = cacher(function (pathArray) {
436 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
437 pathArray = R.parsePathString(pathArray);
445 if (pathArray[0][0] == "M") {
446 x = +pathArray[0][1];
447 y = +pathArray[0][2];
451 res[0] = ["M", x, y];
453 for (var i = start, ii = pathArray[length]; i < ii; i++) {
456 if (pa[0] != upperCase.call(pa[0])) {
457 r[0] = upperCase.call(pa[0]);
478 for (var j = 1, jj = pa[length]; j < jj; j++) {
479 r[j] = +pa[j] + ((j % 2) ? x : y);
483 for (var k = 0, kk = pa[length]; k < kk; k++) {
499 x = res[i][res[i][length] - 2];
500 y = res[i][res[i][length] - 1];
503 res[toString] = R._path2string;
506 l2c = function (x1, y1, x2, y2) {
507 return [x1, y1, x2, y2, x2, y2];
509 q2c = function (x1, y1, ax, ay, x2, y2) {
521 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
522 // for more information of where this math came from visit:
523 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
525 _120 = PI * 120 / 180,
526 rad = PI / 180 * (+angle || 0),
529 rotate = cacher(function (x, y, rad) {
530 var X = x * Math.cos(rad) - y * Math.sin(rad),
531 Y = x * Math.sin(rad) + y * Math.cos(rad);
535 xy = rotate(x1, y1, -rad);
538 xy = rotate(x2, y2, -rad);
541 var cos = Math.cos(PI / 180 * angle),
542 sin = Math.sin(PI / 180 * angle),
545 rx = mmax(rx, Math.abs(x));
546 ry = mmax(ry, Math.abs(y));
549 k = (large_arc_flag == sweep_flag ? -1 : 1) *
550 Math.sqrt(Math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
551 cx = k * rx * y / ry + (x1 + x2) / 2,
552 cy = k * -ry * x / rx + (y1 + y2) / 2,
553 f1 = Math.asin((y1 - cy) / ry),
554 f2 = Math.asin((y2 - cy) / ry);
556 f1 = x1 < cx ? PI - f1 : f1;
557 f2 = x2 < cx ? PI - f2 : f2;
558 f1 < 0 && (f1 = PI * 2 + f1);
559 f2 < 0 && (f2 = PI * 2 + f2);
560 if (sweep_flag && f1 > f2) {
563 if (!sweep_flag && f2 > f1) {
573 if (Math.abs(df) > _120) {
577 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
578 x2 = cx + rx * Math.cos(f2);
579 y2 = cy + ry * Math.sin(f2);
580 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
583 var c1 = Math.cos(f1),
587 t = Math.tan(df / 4),
591 m2 = [x1 + hx * s1, y1 - hy * c1],
592 m3 = [x2 + hx * s2, y2 - hy * c2],
594 m2[0] = 2 * m1[0] - m2[0];
595 m2[1] = 2 * m1[1] - m2[1];
597 return [m2, m3, m4][concat](res);
599 res = [m2, m3, m4][concat](res)[join](",")[split](",");
601 for (var i = 0, ii = res[length]; i < ii; i++) {
602 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
607 findDotAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
608 var x = pow(1 - t, 3) * p1x + pow(1 - t, 2) * 3 * t * c1x + (1 - t) * 3 * t * t * c2x + pow(t, 3) * p2x,
609 y = pow(1 - t, 3) * p1y + pow(1 - t, 2) * 3 * t * c1y + (1 - t) * 3 * t * t * c2y + pow(t, 3) * p2y,
610 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
611 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
612 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
613 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
614 ax = (1 - t) * p1x + t * c1x,
615 ay = (1 - t) * p1y + t * c1y,
616 cx = (1 - t) * c2x + t * p2x,
617 cy = (1 - t) * c2y + t * p2y;
618 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}};
620 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
621 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
622 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
624 t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a,
625 t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a,
628 dot1 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1 > 0 && t1 < 1 ? t1 : 0),
629 dot2 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2 > 0 && t2 < 1 ? t2 : 0);
630 x = x[concat](dot1.x, dot2.x);
631 y = y[concat](dot1.y, dot2.y);
632 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
633 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
635 t1 = (-b + Math.sqrt(b * b - 4 * a * c)) / 2 / a;
636 t2 = (-b - Math.sqrt(b * b - 4 * a * c)) / 2 / a;
637 dot1 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1 > 0 && t1 < 1 ? t1 : 0);
638 dot2 = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2 > 0 && t2 < 1 ? t2 : 0);
639 x = x[concat](dot1.x, dot2.x);
640 y = y[concat](dot1.y, dot2.y);
642 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
643 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
646 path2curve = cacher(function (path, path2) {
647 var p = pathToAbsolute(path),
648 p2 = path2 && pathToAbsolute(path2),
649 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
650 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
651 processPath = function (path, d) {
654 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
656 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
663 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
666 nx = d.x + (d.x - (d.bx || d.x));
667 ny = d.y + (d.y - (d.by || d.y));
668 path = ["C", nx, ny][concat](path.slice(1));
671 d.qx = d.x + (d.x - (d.qx || d.x));
672 d.qy = d.y + (d.y - (d.qy || d.y));
673 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
678 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
681 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
684 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
687 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
690 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
695 fixArc = function (pp, i) {
696 if (pp[i][length] > 7) {
700 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
703 ii = mmax(p[length], p2 && p2[length] || 0);
706 fixM = function (path1, path2, a1, a2, i) {
707 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
708 path2.splice(i, 0, ["M", a2.x, a2.y]);
713 ii = mmax(p[length], p2 && p2[length] || 0);
716 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
717 p[i] = processPath(p[i], attrs);
719 p2 && (p2[i] = processPath(p2[i], attrs2));
721 fixM(p, p2, attrs, attrs2, i);
722 fixM(p2, p, attrs2, attrs, i);
725 seglen = seg[length],
726 seg2len = p2 && seg2[length];
727 attrs.x = seg[seglen - 2];
728 attrs.y = seg[seglen - 1];
729 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
730 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
731 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
732 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
733 attrs2.x = p2 && seg2[seg2len - 2];
734 attrs2.y = p2 && seg2[seg2len - 1];
736 return p2 ? [p, p2] : p;
738 parseDots = cacher(function (gradient) {
740 for (var i = 0, ii = gradient[length]; i < ii; i++) {
742 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
743 dot.color = R.getRGB(par[1]);
744 if (dot.color.error) {
747 dot.color = dot.color.hex;
748 par[2] && (dot.offset = par[2] + "%");
751 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
752 if (!dots[i].offset) {
753 var start = toFloat(dots[i - 1].offset || 0),
755 for (var j = i + 1; j < ii; j++) {
756 if (dots[j].offset) {
757 end = dots[j].offset;
766 var d = (end - start) / (j - i + 1);
769 dots[i].offset = start + "%";
775 getContainer = function () {
781 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
782 if (R.is(arguments[0], "string")) {
783 container = doc.getElementById(arguments[0]);
785 container = arguments[0];
787 if (container.tagName) {
788 if (arguments[1] == null) {
790 container: container,
791 width: container.style.pixelWidth || container.offsetWidth,
792 height: container.style.pixelHeight || container.offsetHeight
795 return {container: container, width: arguments[1], height: arguments[2]};
798 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
799 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
802 plugins = function (con, add) {
804 for (var prop in add) if (add[has](prop) && !(prop in con)) {
805 switch (typeof add[prop]) {
808 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
812 con[prop] = con[prop] || {};
813 plugins.call(this, con[prop], add[prop]);
816 con[prop] = add[prop];
821 tear = function (el, paper) {
822 el == paper.top && (paper.top = el.prev);
823 el == paper.bottom && (paper.bottom = el.next);
824 el.next && (el.next.prev = el.prev);
825 el.prev && (el.prev.next = el.next);
827 tofront = function (el, paper) {
828 if (paper.top === el) {
837 toback = function (el, paper) {
838 if (paper.bottom === el) {
842 el.next = paper.bottom;
844 paper.bottom.prev = el;
847 insertafter = function (el, el2, paper) {
849 el2 == paper.top && (paper.top = el);
850 el2.next && (el2.next.prev = el);
855 insertbefore = function (el, el2, paper) {
857 el2 == paper.bottom && (paper.bottom = el);
858 el2.prev && (el2.prev.next = el);
866 paper.svgns = "http://www.w3.org/2000/svg";
867 paper.xlink = "http://www.w3.org/1999/xlink";
868 var round = function (num) {
869 return +num + (~~num === num) * .5;
871 roundPath = function (path) {
872 for (var i = 0, ii = path[length]; i < ii; i++) {
873 if (lowerCase.call(path[i][0]) != "a") {
874 for (var j = 1, jj = path[i][length]; j < jj; j++) {
875 path[i][j] = round(path[i][j]);
878 path[i][6] = round(path[i][6]);
879 path[i][7] = round(path[i][7]);
884 $ = function (el, attr) {
886 for (var key in attr) if (attr[has](key)) {
887 el[setAttribute](key, attr[key]);
890 return doc.createElementNS(paper.svgns, el);
893 R[toString] = function () {
894 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
896 var thePath = function (pathString, SVG) {
898 SVG.canvas && SVG.canvas[appendChild](el);
899 var p = new Element(el, SVG);
901 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
904 var addGradientFill = function (o, gradient, SVG) {
908 gradient = (gradient + E)[rp](/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, _fx, _fy) {
913 if (pow(fx - .5, 2) + pow(fy - .5, 2) > .25) {
914 fy = Math.sqrt(.25 - pow(fx - .5, 2)) + .5;
919 gradient = gradient[split](/\s*\-\s*/);
920 if (type == "linear") {
921 var angle = gradient.shift();
922 angle = -toFloat(angle);
926 var vector = [0, 0, Math.cos(angle * Math.PI / 180), Math.sin(angle * Math.PI / 180)],
927 max = 1 / (mmax(Math.abs(vector[2]), Math.abs(vector[3])) || 1);
931 vector[0] = -vector[2];
935 vector[1] = -vector[3];
939 var dots = parseDots(gradient);
943 var el = $(type + "Gradient");
944 el.id = "r" + (R._id++)[toString](36);
945 type == "radial" ? $(el, {fx: fx, fy: fy}) : $(el, {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
946 SVG.defs[appendChild](el);
947 for (var i = 0, ii = dots[length]; i < ii; i++) {
948 var stop = $("stop");
950 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
951 "stop-color": dots[i].color || "#fff"
953 el[appendChild](stop);
956 fill: "url(#" + el.id + ")",
965 var updatePosition = function (o) {
966 var bbox = o.getBBox();
967 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
969 var setFillAndStroke = function (o, params) {
976 "-..": [3, 1, 1, 1, 1, 1],
982 "--..": [8, 3, 1, 3, 1, 3]
987 addDashes = function (o, value) {
988 value = dasharray[lowerCase.call(value)];
990 var width = o.attrs["stroke-width"] || "1",
991 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
993 var i = value[length];
995 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
997 $(node, {"stroke-dasharray": dashes[join](",")});
1000 params[has]("rotation") && (rot = params.rotation);
1001 var rotxy = (rot + E)[split](separator);
1002 if (!(rotxy.length - 1)) {
1005 rotxy[1] = +rotxy[1];
1006 rotxy[2] = +rotxy[2];
1008 toFloat(rot) && o.rotate(0, true);
1009 for (var att in params) if (params[has](att)) {
1010 if (!availableAttrs[has](att)) {
1013 var value = params[att];
1017 o.rotate(value, true);
1023 var pn = node.parentNode;
1024 if (lowerCase.call(pn.tagName) != "a") {
1026 pn.insertBefore(hl, node);
1027 hl[appendChild](node);
1030 pn.setAttributeNS(o.paper.xlink, att, value);
1033 node.style.cursor = value;
1036 var rect = (value + E)[split](separator);
1037 if (rect[length] == 4) {
1038 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1039 var el = $("clipPath"),
1041 el.id = "r" + (R._id++)[toString](36);
1048 el[appendChild](rc);
1049 o.paper.defs[appendChild](el);
1050 $(node, {"clip-path": "url(#" + el.id + ")"});
1054 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1055 clip && clip.parentNode.removeChild(clip);
1056 $(node, {"clip-path": E});
1061 if (value && o.type == "path") {
1062 attrs.path = roundPath(pathToAbsolute(value));
1063 $(node, {d: attrs.path});
1067 node[setAttribute](att, value);
1076 value = -attrs.x - (attrs.width || 0);
1079 if (att == "rx" && o.type == "rect") {
1083 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1084 node[setAttribute](att, round(value));
1085 o.pattern && updatePosition(o);
1088 node[setAttribute](att, value);
1097 value = -attrs.y - (attrs.height || 0);
1100 if (att == "ry" && o.type == "rect") {
1104 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1105 node[setAttribute](att, round(value));
1106 o.pattern && updatePosition(o);
1109 if (o.type == "rect") {
1110 $(node, {rx: value, ry: value});
1112 node[setAttribute](att, value);
1116 if (o.type == "image") {
1117 node.setAttributeNS(o.paper.xlink, "href", value);
1120 case "stroke-width":
1121 node.style.strokeWidth = value;
1122 // Need following line for Firefox
1123 node[setAttribute](att, value);
1124 if (attrs["stroke-dasharray"]) {
1125 addDashes(o, attrs["stroke-dasharray"]);
1128 case "stroke-dasharray":
1129 addDashes(o, value);
1132 var xy = (value + E)[split](separator);
1133 xy[0] = +xy[0] || 0;
1134 xy[1] = +xy[1] || 0;
1139 translate.call(o, xy[0], xy[1]);
1142 var xy = (value + E)[split](separator);
1143 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1146 var isURL = (value + E).match(ISURL);
1148 var el = $("pattern"),
1150 el.id = "r" + (R._id++)[toString](36);
1151 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse"});
1152 $(ig, {x: 0, y: 0});
1153 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1154 el[appendChild](ig);
1156 var img = doc.createElement("img");
1157 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1158 img.onload = function () {
1159 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1160 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1161 doc.body.removeChild(this);
1164 doc.body[appendChild](img);
1166 o.paper.defs[appendChild](el);
1167 node.style.fill = "url(#" + el.id + ")";
1168 $(node, {fill: "url(#" + el.id + ")"});
1170 o.pattern && updatePosition(o);
1173 if (!R.getRGB(value).error) {
1174 delete params.gradient;
1175 delete attrs.gradient;
1176 !R.is(attrs.opacity, "undefined") &&
1177 R.is(params.opacity, "undefined") &&
1178 $(node, {opacity: attrs.opacity});
1179 !R.is(attrs["fill-opacity"], "undefined") &&
1180 R.is(params["fill-opacity"], "undefined") &&
1181 $(node, {"fill-opacity": attrs["fill-opacity"]});
1182 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1183 attrs.gradient = value;
1184 attrs.fill = "none";
1188 node[setAttribute](att, R.getRGB(value).hex);
1191 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1194 case "fill-opacity":
1195 if (attrs.gradient) {
1196 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1198 var stops = gradient.getElementsByTagName("stop");
1199 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1204 att == "font-size" && (value = toInt(value, 10) + "px");
1205 var cssrule = att[rp](/(\-.)/g, function (w) {
1206 return upperCase.call(w.substring(1));
1208 node.style[cssrule] = value;
1209 // Need following line for Firefox
1210 node[setAttribute](att, value);
1215 tuneText(o, params);
1217 o.rotate(rotxy.join(S));
1219 toFloat(rot) && o.rotate(rot, true);
1223 var tuneText = function (el, params) {
1224 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1229 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1231 if (params[has]("text")) {
1232 a.text = params.text;
1233 while (node.firstChild) {
1234 node.removeChild(node.firstChild);
1236 var texts = (params.text + E)[split]("\n");
1237 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1238 var tspan = $("tspan");
1239 i && $(tspan, {dy: fontSize * leading, x: a.x});
1240 tspan[appendChild](doc.createTextNode(texts[i]));
1241 node[appendChild](tspan);
1244 var texts = node.getElementsByTagName("tspan");
1245 for (var i = 0, ii = texts[length]; i < ii; i++) {
1246 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1250 var bb = el.getBBox(),
1251 dif = a.y - (bb.y + bb.height / 2);
1252 dif && isFinite(dif) && $(node, {y: a.y + dif});
1254 var Element = function (node, svg) {
1260 node.raphael = this;
1262 this.attrs = this.attrs || {};
1263 this.transformations = []; // rotate, translate, scale
1267 rt: {deg: 0, cx: 0, cy: 0},
1271 !svg.bottom && (svg.bottom = this);
1272 this.prev = svg.top;
1273 svg.top && (svg.top.next = this);
1277 Element[proto].rotate = function (deg, cx, cy) {
1283 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1285 return this._.rt.deg;
1287 var bbox = this.getBBox();
1288 deg = (deg + E)[split](separator);
1289 if (deg[length] - 1) {
1290 cx = toFloat(deg[1]);
1291 cy = toFloat(deg[2]);
1293 deg = toFloat(deg[0]);
1295 this._.rt.deg = deg;
1297 this._.rt.deg += deg;
1299 (cy == null) && (cx = null);
1302 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1303 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1304 if (this._.rt.deg) {
1305 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1306 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1308 this.transformations[0] = E;
1309 this.clip && $(this.clip, {transform: E});
1311 $(this.node, {transform: this.transformations[join](S)});
1314 Element[proto].hide = function () {
1315 !this.removed && (this.node.style.display = "none");
1318 Element[proto].show = function () {
1319 !this.removed && (this.node.style.display = "");
1322 Element[proto].remove = function () {
1326 tear(this, this.paper);
1327 this.node.parentNode.removeChild(this.node);
1328 for (var i in this) {
1331 this.removed = true;
1333 Element[proto].getBBox = function () {
1337 if (this.type == "path") {
1338 return pathDimensions(this.attrs.path);
1340 if (this.node.style.display == "none") {
1346 bbox = this.node.getBBox();
1348 // Firefox 3.0.x plays badly here
1352 if (this.type == "text") {
1353 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1354 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1355 var bb = this.node.getExtentOfChar(i);
1356 (bb.y < bbox.y) && (bbox.y = bb.y);
1357 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1358 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1361 hide && this.hide();
1364 Element[proto].attr = function () {
1368 if (arguments[length] == 0) {
1370 for (var i in this.attrs) if (this.attrs[has](i)) {
1371 res[i] = this.attrs[i];
1375 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1376 if (arguments[0] == "translation") {
1377 return translate.call(this);
1379 if (arguments[0] == "rotation") {
1380 return this.rotate();
1382 if (arguments[0] == "scale") {
1383 return this.scale();
1385 return this.attrs[arguments[0]];
1387 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1389 for (var j in arguments[0]) if (arguments[0][has](j)) {
1390 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1394 if (arguments[length] == 2) {
1396 params[arguments[0]] = arguments[1];
1397 setFillAndStroke(this, params);
1398 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1399 setFillAndStroke(this, arguments[0]);
1403 Element[proto].toFront = function () {
1407 this.node.parentNode[appendChild](this.node);
1408 var svg = this.paper;
1409 svg.top != this && tofront(this, svg);
1412 Element[proto].toBack = function () {
1416 if (this.node.parentNode.firstChild != this.node) {
1417 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1418 toback(this, this.paper);
1419 var svg = this.paper;
1423 Element[proto].insertAfter = function (element) {
1427 var svg = this.paper,
1428 node = element.node;
1429 if (node.nextSibling) {
1430 node.parentNode.insertBefore(this.node, node.nextSibling);
1432 node.parentNode[appendChild](this.node);
1434 insertafter(this, element, this.paper);
1437 Element[proto].insertBefore = function (element) {
1441 var node = element.node;
1442 node.parentNode.insertBefore(this.node, node);
1443 insertbefore(this, element, this.paper);
1447 var theCircle = function (svg, x, y, r) {
1450 var el = $("circle");
1451 svg.canvas && svg.canvas[appendChild](el);
1452 var res = new Element(el, svg);
1453 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1454 res.type = "circle";
1458 var theRect = function (svg, x, y, w, h, r) {
1462 svg.canvas && svg.canvas[appendChild](el);
1463 var res = new Element(el, svg);
1464 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1469 var theEllipse = function (svg, x, y, rx, ry) {
1472 var el = $("ellipse");
1473 svg.canvas && svg.canvas[appendChild](el);
1474 var res = new Element(el, svg);
1475 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1476 res.type = "ellipse";
1480 var theImage = function (svg, src, x, y, w, h) {
1481 var el = $("image");
1482 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1483 el.setAttributeNS(svg.xlink, "href", src);
1484 svg.canvas && svg.canvas[appendChild](el);
1485 var res = new Element(el, svg);
1486 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1490 var theText = function (svg, x, y, text) {
1492 $(el, {x: x, y: y, "text-anchor": "middle"});
1493 svg.canvas && svg.canvas[appendChild](el);
1494 var res = new Element(el, svg);
1495 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1497 setFillAndStroke(res, res.attrs);
1500 var setSize = function (width, height) {
1501 this.width = width || this.width;
1502 this.height = height || this.height;
1503 this.canvas[setAttribute]("width", this.width);
1504 this.canvas[setAttribute]("height", this.height);
1507 var create = function () {
1508 var con = getContainer[apply](null, arguments),
1509 container = con && con.container,
1513 height = con.height;
1515 throw new Error("SVG container not found.");
1517 paper.canvas = $("svg");
1518 var cnvs = paper.canvas;
1519 paper.width = width || 512;
1520 paper.height = height || 342;
1521 cnvs[setAttribute]("width", paper.width);
1522 cnvs[setAttribute]("height", paper.height);
1523 if (container == 1) {
1524 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1525 doc.body[appendChild](cnvs);
1527 if (container.firstChild) {
1528 container.insertBefore(cnvs, container.firstChild);
1530 container[appendChild](cnvs);
1533 container = { canvas: cnvs };
1534 for (var prop in paper) if (paper[has](prop)) {
1535 container[prop] = paper[prop];
1537 container.bottom = container.top = null;
1538 plugins.call(container, container, R.fn);
1540 container.raphael = R;
1543 paper.clear = function () {
1544 var c = this.canvas;
1545 while (c.firstChild) {
1546 c.removeChild(c.firstChild);
1548 this.bottom = this.top = null;
1549 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1550 c[appendChild](this.desc);
1551 c[appendChild](this.defs = $("defs"));
1553 paper.remove = function () {
1554 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1555 for (var i in this) {
1563 var path2vml = function (path) {
1564 var total = /[ahqtv]/ig,
1565 command = pathToAbsolute;
1566 (path + E).match(total) && (command = path2curve);
1568 if (command == pathToAbsolute && !(path + E).match(total)) {
1569 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1570 bites = /([clmz]),?([^clmz]*)/gi,
1571 val = /-?[^,\s-]+/g;
1572 var res = (path + E)[rp](bites, function (all, command, args) {
1574 args[rp](val, function (value) {
1575 vals[push](round(value));
1577 return map[command] + vals;
1581 var pa = command(path), p, res = [], r;
1582 for (var i = 0, ii = pa[length]; i < ii; i++) {
1584 r = lowerCase.call(pa[i][0]);
1585 r == "z" && (r = "x");
1586 for (var j = 1, jj = p[length]; j < jj; j++) {
1587 r += round(p[j]) + (j != jj - 1 ? "," : E);
1591 return res[join](S);
1594 R[toString] = function () {
1595 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1597 var thePath = function (pathString, VML) {
1598 var g = createNode("group");
1599 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1600 g.coordsize = VML.coordsize;
1601 g.coordorigin = VML.coordorigin;
1602 var el = createNode("shape"), ol = el.style;
1603 ol.width = VML.width + "px";
1604 ol.height = VML.height + "px";
1605 el.coordsize = this.coordsize;
1606 el.coordorigin = this.coordorigin;
1608 var p = new Element(el, g, VML);
1609 p.isAbsolute = true;
1613 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1614 VML.canvas[appendChild](g);
1617 var setFillAndStroke = function (o, params) {
1618 o.attrs = o.attrs || {};
1624 for (var par in params) if (params[has](par)) {
1625 a[par] = params[par];
1627 params.href && (node.href = params.href);
1628 params.title && (node.title = params.title);
1629 params.target && (node.target = params.target);
1630 params.cursor && (s.cursor = params.cursor);
1631 if (params.path && o.type == "path") {
1632 a.path = params.path;
1633 node.path = path2vml(a.path);
1635 if (params.rotation != null) {
1636 o.rotate(params.rotation, true);
1638 if (params.translation) {
1639 xy = (params.translation + E)[split](separator);
1640 translate.call(o, xy[0], xy[1]);
1641 if (o._.rt.cx != null) {
1642 o._.rt.cx +=+ xy[0];
1643 o._.rt.cy +=+ xy[1];
1644 o.setBox(o.attrs, xy[0], xy[1]);
1648 xy = (params.scale + E)[split](separator);
1649 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1651 if ("clip-rect" in params) {
1652 var rect = (params["clip-rect"] + E)[split](separator);
1653 if (rect[length] == 4) {
1654 rect[2] = +rect[2] + (+rect[0]);
1655 rect[3] = +rect[3] + (+rect[1]);
1656 var div = node.clipRect || doc.createElement("div"),
1658 group = node.parentNode;
1659 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1660 if (!node.clipRect) {
1661 dstyle.position = "absolute";
1664 dstyle.width = o.paper.width + "px";
1665 dstyle.height = o.paper.height + "px";
1666 group.parentNode.insertBefore(div, group);
1667 div[appendChild](group);
1668 node.clipRect = div;
1671 if (!params["clip-rect"]) {
1672 node.clipRect && (node.clipRect.style.clip = E);
1675 if (o.type == "image" && params.src) {
1676 node.src = params.src;
1678 if (o.type == "image" && params.opacity) {
1679 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1680 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1682 params.font && (s.font = params.font);
1683 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1684 params["font-size"] && (s.fontSize = params["font-size"]);
1685 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1686 params["font-style"] && (s.fontStyle = params["font-style"]);
1687 if (params.opacity != null ||
1688 params["stroke-width"] != null ||
1689 params.fill != null ||
1690 params.stroke != null ||
1691 params["stroke-width"] != null ||
1692 params["stroke-opacity"] != null ||
1693 params["fill-opacity"] != null ||
1694 params["stroke-dasharray"] != null ||
1695 params["stroke-miterlimit"] != null ||
1696 params["stroke-linejoin"] != null ||
1697 params["stroke-linecap"] != null) {
1698 node = o.shape || node;
1699 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1701 !fill && (newfill = fill = createNode("fill"));
1702 if ("fill-opacity" in params || "opacity" in params) {
1703 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1704 opacity < 0 && (opacity = 0);
1705 opacity > 1 && (opacity = 1);
1706 fill.opacity = opacity;
1708 params.fill && (fill.on = true);
1709 if (fill.on == null || params.fill == "none") {
1712 if (fill.on && params.fill) {
1713 var isURL = params.fill.match(ISURL);
1715 fill.src = isURL[1];
1718 fill.color = R.getRGB(params.fill).hex;
1720 fill.type = "solid";
1721 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1723 a.gradient = params.fill;
1727 newfill && node[appendChild](fill);
1728 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1730 !stroke && (newstroke = stroke = createNode("stroke"));
1731 if ((params.stroke && params.stroke != "none") ||
1732 params["stroke-width"] ||
1733 params["stroke-opacity"] != null ||
1734 params["stroke-dasharray"] ||
1735 params["stroke-miterlimit"] ||
1736 params["stroke-linejoin"] ||
1737 params["stroke-linecap"]) {
1740 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1741 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1742 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1743 width = (toFloat(params["stroke-width"]) || 1) * .75;
1744 opacity < 0 && (opacity = 0);
1745 opacity > 1 && (opacity = 1);
1746 params["stroke-width"] == null && (width = a["stroke-width"]);
1747 params["stroke-width"] && (stroke.weight = width);
1748 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1749 stroke.opacity = opacity;
1751 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1752 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1753 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1754 if (params["stroke-dasharray"]) {
1758 "-.": "shortdashdot",
1759 "-..": "shortdashdotdot",
1764 "--.": "longdashdot",
1765 "--..": "longdashdotdot"
1767 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1769 newstroke && node[appendChild](stroke);
1771 if (res.type == "text") {
1772 var s = res.paper.span.style;
1773 a.font && (s.font = a.font);
1774 a["font-family"] && (s.fontFamily = a["font-family"]);
1775 a["font-size"] && (s.fontSize = a["font-size"]);
1776 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1777 a["font-style"] && (s.fontStyle = a["font-style"]);
1778 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1779 res.W = a.w = res.paper.span.offsetWidth;
1780 res.H = a.h = res.paper.span.offsetHeight;
1782 res.Y = a.y + round(res.H / 2);
1784 // text-anchor emulationm
1785 switch (a["text-anchor"]) {
1787 res.node.style["v-text-align"] = "left";
1788 res.bbx = round(res.W / 2);
1791 res.node.style["v-text-align"] = "right";
1792 res.bbx = -round(res.W / 2);
1795 res.node.style["v-text-align"] = "center";
1800 var addGradientFill = function (o, gradient) {
1801 o.attrs = o.attrs || {};
1802 var attrs = o.attrs,
1803 fill = o.node.getElementsByTagName("fill"),
1806 o.attrs.gradient = gradient;
1807 gradient = (gradient + E)[rp](/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, fx, fy) {
1812 if (pow(fx - .5, 2) + pow(fy - .5, 2) > .25) {
1813 fy = Math.sqrt(.25 - pow(fx - .5, 2)) + .5;
1819 gradient = gradient[split](/\s*\-\s*/);
1820 if (type == "linear") {
1821 var angle = gradient.shift();
1822 angle = -toFloat(angle);
1827 var dots = parseDots(gradient);
1831 o = o.shape || o.node;
1832 fill = fill[0] || createNode("fill");
1835 fill.method = "none";
1836 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1837 fill.color = dots[0].color;
1838 fill.color2 = dots[dots[length] - 1].color;
1840 for (var i = 0, ii = dots[length]; i < ii; i++) {
1841 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1843 fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color;
1844 if (type == "radial") {
1845 fill.focus = "100%";
1846 fill.focussize = fxfy;
1847 fill.focusposition = fxfy;
1849 fill.angle = (270 - angle) % 360;
1854 var Element = function (node, group, vml) {
1862 node.raphael = this;
1875 !vml.bottom && (vml.bottom = this);
1876 this.prev = vml.top;
1877 vml.top && (vml.top.next = this);
1881 Element[proto].rotate = function (deg, cx, cy) {
1887 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1889 return this._.rt.deg;
1891 deg = (deg + E)[split](separator);
1892 if (deg[length] - 1) {
1893 cx = toFloat(deg[1]);
1894 cy = toFloat(deg[2]);
1896 deg = toFloat(deg[0]);
1898 this._.rt.deg = deg;
1900 this._.rt.deg += deg;
1902 cy == null && (cx = null);
1905 this.setBox(this.attrs, cx, cy);
1906 this.Group.style.rotation = this._.rt.deg;
1907 // gradient fix for rotation. TODO
1908 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1909 // fill = fill[0] || {};
1910 // var b = ((360 - this._.rt.deg) - 270) % 360;
1911 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1914 Element[proto].setBox = function (params, cx, cy) {
1918 var gs = this.Group.style,
1919 os = (this.shape && this.shape.style) || this.node.style;
1920 params = params || {};
1921 for (var i in params) if (params[has](i)) {
1922 this.attrs[i] = params[i];
1924 cx = cx || this._.rt.cx;
1925 cy = cy || this._.rt.cy;
1926 var attr = this.attrs,
1931 switch (this.type) {
1933 x = attr.cx - attr.r;
1934 y = attr.cy - attr.r;
1938 x = attr.cx - attr.rx;
1939 y = attr.cy - attr.ry;
1947 w = attr.width || 0;
1948 h = attr.height || 0;
1951 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
1952 x = attr.x - round(this.W / 2);
1953 y = attr.y - this.H / 2;
1958 if (!this.attrs.path) {
1961 w = this.paper.width;
1962 h = this.paper.height;
1964 var dim = pathDimensions(this.attrs.path);
1974 w = this.paper.width;
1975 h = this.paper.height;
1978 cx = (cx == null) ? x + w / 2 : cx;
1979 cy = (cy == null) ? y + h / 2 : cy;
1980 var left = cx - this.paper.width / 2,
1981 top = cy - this.paper.height / 2;
1982 if (this.type == "path" || this.type == "text") {
1983 (gs.left != left + "px") && (gs.left = left + "px");
1984 (gs.top != top + "px") && (gs.top = top + "px");
1985 this.X = this.type == "text" ? x : -left;
1986 this.Y = this.type == "text" ? y : -top;
1989 (os.left != -left + "px") && (os.left = -left + "px");
1990 (os.top != -top + "px") && (os.top = -top + "px");
1992 (gs.left != left + "px") && (gs.left = left + "px");
1993 (gs.top != top + "px") && (gs.top = top + "px");
1998 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
1999 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2000 (os.left != x - left + "px") && (os.left = x - left + "px");
2001 (os.top != y - top + "px") && (os.top = y - top + "px");
2002 (os.width != w + "px") && (os.width = w + "px");
2003 (os.height != h + "px") && (os.height = h + "px");
2004 var arcsize = (+params.r || 0) / (mmin(w, h));
2005 if (this.type == "rect" && this.arcsize != arcsize && (arcsize || this.arcsize)) {
2006 // We should replace element with the new one
2007 var o = createNode(arcsize ? "roundrect" : "rect");
2008 o.arcsize = arcsize;
2009 this.Group[appendChild](o);
2010 this.node.parentNode.removeChild(this.node);
2012 this.arcsize = arcsize;
2013 this.attr(this.attrs);
2017 Element[proto].hide = function () {
2018 !this.removed && (this.Group.style.display = "none");
2021 Element[proto].show = function () {
2022 !this.removed && (this.Group.style.display = "block");
2025 Element[proto].getBBox = function () {
2029 if (this.type == "path") {
2030 return pathDimensions(this.attrs.path);
2033 x: this.X + (this.bbx || 0),
2039 Element[proto].remove = function () {
2043 tear(this, this.paper);
2044 this.node.parentNode.removeChild(this[0]);
2045 this.Group.parentNode.removeChild(this.Group);
2046 this.shape && this.shape.parentNode.removeChild(this.shape);
2047 for (var i in this) {
2050 this.removed = true;
2052 Element[proto].attr = function () {
2056 if (arguments[length] == 0) {
2058 for (var i in this.attrs) if (this.attrs[has](i)) {
2059 res[i] = this.attrs[i];
2063 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2064 if (arguments[0] == "translation") {
2065 return translate.call(this);
2067 if (arguments[0] == "rotation") {
2068 return this.rotate();
2070 if (arguments[0] == "scale") {
2071 return this.scale();
2073 return this.attrs[arguments[0]];
2075 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2077 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2078 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2083 if (arguments[length] == 2) {
2085 params[arguments[0]] = arguments[1];
2087 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2089 if (params.text && this.type == "text") {
2090 this.node.string = params.text;
2092 setFillAndStroke(this, params);
2093 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2094 addGradientFill(this, params.gradient);
2096 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2100 Element[proto].toFront = function () {
2101 !this.removed && this.Group.parentNode[appendChild](this.Group);
2102 this.paper.top != this && tofront(this, this.paper);
2105 Element[proto].toBack = function () {
2109 if (this.Group.parentNode.firstChild != this.Group) {
2110 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2111 toback(this, this.paper);
2115 Element[proto].insertAfter = function (element) {
2119 if (element.Group.nextSibling) {
2120 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2122 element.Group.parentNode[appendChild](this.Group);
2124 insertafter(this, element, this.paper);
2127 Element[proto].insertBefore = function (element) {
2131 element.Group.parentNode.insertBefore(this.Group, element.Group);
2132 insertbefore(this, element, this.paper);
2136 var theCircle = function (vml, x, y, r) {
2137 var g = createNode("group"),
2138 o = createNode("oval"),
2140 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2141 g.coordsize = vml.coordsize;
2142 g.coordorigin = vml.coordorigin;
2144 var res = new Element(o, g, vml);
2145 res.type = "circle";
2146 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2150 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2151 vml.canvas[appendChild](g);
2154 var theRect = function (vml, x, y, w, h, r) {
2155 var g = createNode("group"),
2156 o = createNode(r ? "roundrect" : "rect"),
2157 arcsize = (+r || 0) / (mmin(w, h));
2158 o.arcsize = arcsize;
2159 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2160 g.coordsize = vml.coordsize;
2161 g.coordorigin = vml.coordorigin;
2163 var res = new Element(o, g, vml);
2165 setFillAndStroke(res, {stroke: "#000"});
2166 res.arcsize = arcsize;
2167 res.setBox({x: x, y: y, width: w, height: h, r: +r});
2168 vml.canvas[appendChild](g);
2171 var theEllipse = function (vml, x, y, rx, ry) {
2172 var g = createNode("group"),
2173 o = createNode("oval"),
2175 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2176 g.coordsize = vml.coordsize;
2177 g.coordorigin = vml.coordorigin;
2179 var res = new Element(o, g, vml);
2180 res.type = "ellipse";
2181 setFillAndStroke(res, {stroke: "#000"});
2186 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2187 vml.canvas[appendChild](g);
2190 var theImage = function (vml, src, x, y, w, h) {
2191 var g = createNode("group"),
2192 o = createNode("image"),
2194 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2195 g.coordsize = vml.coordsize;
2196 g.coordorigin = vml.coordorigin;
2199 var res = new Element(o, g, vml);
2201 res.attrs.src = src;
2206 res.setBox({x: x, y: y, width: w, height: h});
2207 vml.canvas[appendChild](g);
2210 var theText = function (vml, x, y, text) {
2211 var g = createNode("group"),
2212 el = createNode("shape"),
2214 path = createNode("path"),
2216 o = createNode("textpath");
2217 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2218 g.coordsize = vml.coordsize;
2219 g.coordorigin = vml.coordorigin;
2220 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2221 path.textpathok = true;
2222 ol.width = vml.width;
2223 ol.height = vml.height;
2224 o.string = text + E;
2227 el[appendChild](path);
2229 var res = new Element(o, g, vml);
2231 res.textpath = path;
2233 res.attrs.text = text;
2238 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2240 vml.canvas[appendChild](g);
2243 var setSize = function (width, height) {
2244 var cs = this.canvas.style;
2245 width == +width && (width += "px");
2246 height == +height && (height += "px");
2249 cs.clip = "rect(0 " + width + " " + height + " 0)";
2252 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2254 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2255 var createNode = function (tagName) {
2256 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2259 var createNode = function (tagName) {
2260 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2263 var create = function () {
2264 var con = getContainer[apply](null, arguments),
2265 container = con.container,
2266 height = con.height,
2272 throw new Error("VML container not found.");
2275 c = res.canvas = doc.createElement("div"),
2277 width = width || 512;
2278 height = height || 342;
2279 width == +width && (width += "px");
2280 height == +height && (height += "px");
2283 res.coordsize = "1000 1000";
2284 res.coordorigin = "0 0";
2285 res.span = doc.createElement("span");
2286 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2287 c[appendChild](res.span);
2288 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2289 if (container == 1) {
2290 doc.body[appendChild](c);
2300 container.style.width = width;
2301 container.style.height = height;
2302 if (container.firstChild) {
2303 container.insertBefore(c, container.firstChild);
2305 container[appendChild](c);
2308 for (var prop in paper) if (paper[has](prop)) {
2309 res[prop] = paper[prop];
2311 plugins.call(res, res, R.fn);
2312 res.top = res.bottom = null;
2316 paper.clear = function () {
2317 this.canvas.innerHTML = E;
2318 this.bottom = this.top = null;
2320 paper.remove = function () {
2321 this.canvas.parentNode.removeChild(this.canvas);
2322 for (var i in this) {
2329 // Safari or Chrome (WebKit) rendering bug workaround method
2330 if ({"Apple Computer, Inc.": 1, "Google Inc.": 1}[navigator.vendor] && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2331 paper.safari = function () {
2332 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2333 setTimeout(function () {rect.remove();});
2336 paper.safari = function () {};
2340 var addEvent = (function () {
2341 if (doc.addEventListener) {
2342 return function (obj, type, fn, element) {
2343 var f = function (e) {
2344 return fn.call(element, e);
2346 obj.addEventListener(type, f, false);
2347 return function () {
2348 obj.removeEventListener(type, f, false);
2352 } else if (doc.attachEvent) {
2353 return function (obj, type, fn, element) {
2354 var f = function (e) {
2355 return fn.call(element, e || win.event);
2357 obj.attachEvent("on" + type, f);
2358 var detacher = function () {
2359 obj.detachEvent("on" + type, f);
2362 if (type == "mouseover") {
2363 obj.attachEvent("onmouseenter", f);
2364 return function () {
2365 obj.detachEvent("onmouseenter", f);
2368 } else if (type == "mouseout") {
2369 obj.attachEvent("onmouseleave", f);
2370 return function () {
2371 obj.detachEvent("onmouseleave", f);
2379 for (var i = events[length]; i--;) {
2380 (function (eventName) {
2381 Element[proto][eventName] = function (fn) {
2382 if (R.is(fn, "function")) {
2383 this.events = this.events || {};
2384 this.events[eventName] = this.events[eventName] || {};
2385 this.events[eventName][fn] = this.events[eventName][fn] || [];
2386 this.events[eventName][fn][push](addEvent(this.shape || this.node, eventName, fn, this));
2390 Element[proto]["un" + eventName] = function (fn) {
2391 var e = this.events;
2395 e[eventName][fn][length] &&
2396 e[eventName][fn].shift()() &&
2397 !e[eventName][fn][length] &&
2398 delete e[eventName][fn];
2404 Element[proto].hover = function (f_in, f_out) {
2405 return this.mouseover(f_in).mouseout(f_out);
2407 paper.circle = function (x, y, r) {
2408 return theCircle(this, x || 0, y || 0, r || 0);
2410 paper.rect = function (x, y, w, h, r) {
2411 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2413 paper.ellipse = function (x, y, rx, ry) {
2414 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2416 paper.path = function (pathString) {
2417 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2418 return thePath(R.format[apply](R, arguments), this);
2420 paper.image = function (src, x, y, w, h) {
2421 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2423 paper.text = function (x, y, text) {
2424 return theText(this, x || 0, y || 0, text || E);
2426 paper.set = function (itemsArray) {
2427 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2428 return new Set(itemsArray);
2430 paper.setSize = setSize;
2431 Element[proto].scale = function (x, y, cx, cy) {
2432 if (x == null && y == null) {
2433 return {x: this._.sx, y: this._.sy, toString: function () { return this.x + S + this.y; }};
2443 var bb = this.getBBox(),
2444 rcx = bb.x + bb.width / 2,
2445 rcy = bb.y + bb.height / 2,
2448 cx = (+cx || cx == 0) ? cx : rcx;
2449 cy = (+cy || cy == 0) ? cy : rcy;
2450 var dirx = ~~(x / Math.abs(x)),
2451 diry = ~~(y / Math.abs(y)),
2452 s = this.node.style,
2453 ncx = cx + (rcx - cx) * kx,
2454 ncy = cy + (rcy - cy) * ky;
2455 switch (this.type) {
2458 var neww = a.width * dirx * kx,
2459 newh = a.height * diry * ky,
2460 newr = a.r * mmin(kx, ky),
2461 newx = ncx - neww / 2,
2462 newy = ncy - newh / 2;
2474 rx: a.rx * dirx * kx,
2475 ry: a.ry * diry * ky,
2476 r: a.r * mmin(dirx * kx, diry * ky),
2482 var path = pathToRelative(a.path),
2484 for (var i = 0, ii = path[length]; i < ii; i++) {
2487 P0 = upperCase.call(p[0]);
2488 if (P0 == "M" && skip) {
2493 if (R.svg && P0 == "A") {
2494 p[path[i][length] - 2] *= kx;
2495 p[path[i][length] - 1] *= ky;
2498 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2499 } else if (P0 == "H") {
2500 for (j = 1, jj = p[length]; j < jj; j++) {
2503 } else if (P0 == "V") {
2504 for (j = 1, jj = p[length]; j < jj; j++) {
2508 for (j = 1, jj = p[length]; j < jj; j++) {
2509 p[j] *= (j % 2) ? kx : ky;
2513 var dim2 = pathDimensions(path),
2514 dx = ncx - dim2.x - dim2.width / 2,
2515 dy = ncy - dim2.y - dim2.height / 2;
2519 this.attr({path: path});
2522 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2523 if (this.transformations) {
2524 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2525 this.node[setAttribute]("transform", this.transformations[join](S));
2526 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2527 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2528 this.attr({x: dx, y: dy});
2532 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2533 ", M12=0, M21=0, M22=", diry,
2534 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2535 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2538 if (this.transformations) {
2539 this.transformations[2] = E;
2540 this.node[setAttribute]("transform", this.transformations[join](S));
2544 this.node.filterMatrix = E;
2545 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2548 a.scale = [x, y, cx, cy][join](S);
2555 // animation easing formulas
2556 R.easing_formulas = {
2557 linear: function (n) {
2564 return pow(n - 1, 3) + 1;
2566 "<>": function (n) {
2569 return pow(n, 3) / 2;
2572 return (pow(n, 3) + 2) / 2;
2574 backIn: function (n) {
2576 return n * n * ((s + 1) * n - s);
2578 backOut: function (n) {
2581 return n * n * ((s + 1) * n + s) + 1;
2583 elastic: function (n) {
2584 if (n == 0 || n == 1) {
2589 return pow(2, -10 * n) * Math.sin((n - s) * (2 * Math.PI) / p) + 1;
2591 bounce: function (n) {
2600 l = s * n * n + .75;
2602 if (n < (2.5 / p)) {
2604 l = s * n * n + .9375;
2607 l = s * n * n + .984375;
2615 var animationElements = {length : 0},
2616 animation = function () {
2617 var Now = +new Date;
2618 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2619 var e = animationElements[l];
2621 delete animationElements[l];
2622 animationElements[length]--;
2625 var time = Now - e.start,
2634 callback = e.callback,
2638 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2639 for (var attr in from) if (from[has](attr)) {
2640 switch (availableAnimAttrs[attr]) {
2642 now = +from[attr] + pos * ms * diff[attr];
2646 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2647 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2648 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2653 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2654 now[i] = [from[attr][i][0]];
2655 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2656 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2658 now[i] = now[i][join](S);
2665 var x = diff[attr][0] * (time - prev),
2666 y = diff[attr][1] * (time - prev);
2672 now = +from[attr][0] + pos * ms * diff[attr][0];
2673 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2676 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);
2682 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2691 that._run && that._run.call(that);
2693 (t.x || t.y) && that.translate(-t.x, -t.y);
2694 to.scale && (to.scale = to.scale + E);
2696 delete animationElements[l];
2697 animationElements[length]--;
2698 that.in_animation = null;
2699 R.is(callback, "function") && callback.call(that);
2703 R.svg && paper.safari();
2704 animationElements[length] && setTimeout(animation);
2706 upto255 = function (color) {
2707 return color > 255 ? 255 : (color < 0 ? 0 : color);
2709 translate = function (x, y) {
2711 return {x: this._.tx, y: this._.ty};
2715 switch (this.type) {
2718 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2723 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2726 var path = pathToRelative(this.attrs.path);
2729 this.attr({path: path});
2734 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2735 animationElements[element.id] && (params.start = animationElements[element.id].start);
2736 return this.animate(params, ms, easing, callback);
2738 Element[proto].onAnimation = function (f) {
2739 this._run = f || null;
2742 Element[proto].animate = function (params, ms, easing, callback) {
2743 if (R.is(easing, "function") || !easing) {
2744 callback = easing || null;
2749 for (var attr in params) if (params[has](attr)) {
2750 if (availableAnimAttrs[has](attr)) {
2751 from[attr] = this.attr(attr);
2752 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2753 to[attr] = params[attr];
2754 switch (availableAnimAttrs[attr]) {
2756 diff[attr] = (to[attr] - from[attr]) / ms;
2759 from[attr] = R.getRGB(from[attr]);
2760 var toColour = R.getRGB(to[attr]);
2762 r: (toColour.r - from[attr].r) / ms,
2763 g: (toColour.g - from[attr].g) / ms,
2764 b: (toColour.b - from[attr].b) / ms
2768 var pathes = path2curve(from[attr], to[attr]);
2769 from[attr] = pathes[0];
2770 to[attr] = pathes[1];
2772 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2773 diff[attr][i] = [0];
2774 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2775 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2780 var values = (params[attr] + E)[split](separator),
2781 from2 = (from[attr] + E)[split](separator);
2784 from[attr] = [0, 0];
2785 diff[attr] = [values[0] / ms, values[1] / ms];
2788 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2789 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2792 params[attr] = values;
2793 from[attr] = (from[attr] + E)[split](separator);
2794 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2797 from[attr] = (from[attr] + E)[split](separator);
2801 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2810 this.in_animation = 1;
2811 animationElements[this.id] = {
2812 start: params.start || +new Date,
2822 ++animationElements[length] == 1 && animation();
2825 Element[proto].stop = function () {
2826 animationElements[this.id] && animationElements[length]--;
2827 delete animationElements[this.id];
2830 Element[proto].translate = function (x, y) {
2831 return this.attr({translation: x + " " + y});
2833 Element[proto][toString] = function () {
2834 return "Rapha\xebl\u2019s object";
2836 R.ae = animationElements;
2839 var Set = function (items) {
2843 for (var i = 0, ii = items[length]; i < ii; i++) {
2844 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2845 this[this.items[length]] = this.items[this.items[length]] = items[i];
2851 Set[proto][push] = function () {
2854 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2855 item = arguments[i];
2856 if (item && (item.constructor == Element || item.constructor == Set)) {
2857 len = this.items[length];
2858 this[len] = this.items[len] = item;
2864 Set[proto].pop = function () {
2865 delete this[this[length]--];
2866 return this.items.pop();
2868 for (var method in Element[proto]) if (Element[proto][has](method)) {
2869 Set[proto][method] = (function (methodname) {
2870 return function () {
2871 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2872 this.items[i][methodname][apply](this.items[i], arguments);
2878 Set[proto].attr = function (name, value) {
2879 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2880 for (var j = 0, jj = name[length]; j < jj; j++) {
2881 this.items[j].attr(name[j]);
2884 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2885 this.items[i].attr[apply](this.items[i], arguments);
2890 Set[proto].animate = function (params, ms, easing, callback) {
2891 (R.is(easing, "function") || !easing) && (callback = easing || null);
2892 var len = this.items[length],
2896 callback && (collector = function () {
2897 !--len && callback.call(set);
2899 this.items[--i].animate(params, ms, easing || collector, collector);
2901 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
2905 Set[proto].insertAfter = function (el) {
2906 var i = this.items[length];
2908 this.items[i].insertAfter(el);
2911 Set[proto].getBBox = function () {
2916 for (var i = this.items[length]; i--;) {
2917 var box = this.items[i].getBBox();
2920 w[push](box.x + box.width);
2921 h[push](box.y + box.height);
2923 x = mmin[apply](0, x);
2924 y = mmin[apply](0, y);
2928 width: mmax[apply](0, w) - x,
2929 height: mmax[apply](0, h) - y
2933 R.registerFont = function (font) {
2937 this.fonts = this.fonts || {};
2943 family = font.face["font-family"];
2944 for (var prop in font.face) if (font.face[has](prop)) {
2945 fontcopy.face[prop] = font.face[prop];
2947 if (this.fonts[family]) {
2948 this.fonts[family][push](fontcopy);
2950 this.fonts[family] = [fontcopy];
2953 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
2954 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
2955 var path = font.glyphs[glyph];
2956 fontcopy.glyphs[glyph] = {
2959 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
2960 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2964 for (var k in path.k) if (path[has](k)) {
2965 fontcopy.glyphs[glyph].k[k] = path.k[k];
2972 paper.getFont = function (family, weight, style, stretch) {
2973 stretch = stretch || "normal";
2974 style = style || "normal";
2975 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2976 var font = R.fonts[family];
2978 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
2979 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
2980 if (name.test(fontName)) {
2981 font = R.fonts[fontName];
2988 for (var i = 0, ii = font[length]; i < ii; i++) {
2990 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2997 paper.print = function (x, y, string, font, size, origin) {
2998 origin = origin || "middle"; // baseline|middle
2999 var out = this.set(),
3000 letters = (string + E)[split](E),
3004 R.is(font, "string") && (font = this.getFont(font));
3006 scale = (size || 16) / font.face["units-per-em"];
3007 var bb = font.face.bbox.split(separator),
3009 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3010 for (var i = 0, ii = letters[length]; i < ii; i++) {
3011 var prev = i && font.glyphs[letters[i - 1]] || {},
3012 curr = font.glyphs[letters[i]];
3013 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3014 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3016 out.scale(scale, scale, top, height).translate(x - top, y - height);
3021 R.format = function (token) {
3022 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3024 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3025 return args[++i] == null ? E : args[i];
3029 R.ninja = function () {
3030 var r = win.Raphael, u;
3031 if (oldRaphael.was) {
3032 win.Raphael = oldRaphael.is;
3042 R.el = Element[proto];