2 * Raphael 1.2.8 - 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 = /[, ]+/,
11 elements = /^(circle|rect|path|ellipse|text|image)$/,
15 was: "Raphael" in win,
19 if (R.is(arguments[0], "array")) {
21 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
23 for (var i = 0, ii = a[length]; i < ii; i++) {
25 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
29 return create[apply](R, arguments);
31 appendChild = "appendChild",
36 events = "click dblclick mousedown mousemove mouseout mouseover mouseup".split(S),
37 has = "hasOwnProperty",
38 isit = /^\[object\s+|\]$/gi,
42 lowerCase = String[proto].toLowerCase,
47 toString = "toString",
48 objectToString = Object[proto][toString],
53 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
54 colourRegExp = /^\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,
56 setAttribute = "setAttribute",
60 upperCase = String[proto].toUpperCase,
61 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},
62 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},
65 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
66 R.svg = !(R.vml = R.type == "VML");
70 R.is = function (o, type) {
71 type = lowerCase.call(type);
72 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o)[rp](isit, E)) == type;
74 R.setWindow = function (newwin) {
79 var toHex = function (color) {
81 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
82 var trim = /^\s+|\s+$/g;
83 toHex = cacher(function (color) {
85 color = (color + E)[rp](trim, E);
87 var docum = new ActiveXObject("htmlfile");
88 docum.write("<body>");
92 bod = createPopup().document.body;
94 var range = bod.createTextRange();
96 bod.style.color = color;
97 var value = range.queryCommandValue("ForeColor");
98 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
99 return "#" + ("000000" + value[toString](16)).slice(-6);
105 var i = doc.createElement("i");
106 i.title = "Rapha\xebl Colour Picker";
107 i.style.display = "none";
108 doc.body[appendChild](i);
109 toHex = cacher(function (color) {
110 i.style.color = color;
111 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
116 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
117 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
125 if (brightness == 0) {
126 return {r: 0, g: 0, b: 0, hex: "#000"};
128 if (hue > 1 || saturation > 1 || brightness > 1) {
135 p = brightness * (1 - saturation),
136 q = brightness * (1 - (saturation * f)),
137 t = brightness * (1 - (saturation * (1 - f)));
138 red = [brightness, q, p, p, t, brightness, brightness][i];
139 green = [t, brightness, brightness, q, p, p, t][i];
140 blue = [p, p, t, brightness, brightness, q, p][i];
144 var rgb = {r: red, g: green, b: blue},
145 r = (~~red)[toString](16),
146 g = (~~green)[toString](16),
147 b = (~~blue)[toString](16);
151 rgb.hex = "#" + r + g + b;
154 R.rgb2hsb = cacher(function (red, green, blue) {
155 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
160 if (R.is(red, "string")) {
161 var clr = R.getRGB(red);
166 if (red > 1 || green > 1 || blue > 1) {
171 var max = mmax(red, green, blue),
172 min = mmin(red, green, blue),
177 return {h: 0, s: 0, b: max};
179 var delta = (max - min);
180 saturation = delta / max;
182 hue = (green - blue) / delta;
183 } else if (green == max) {
184 hue = 2 + ((blue - red) / delta);
186 hue = 4 + ((red - green) / delta);
192 return {h: hue, s: saturation, b: brightness};
194 var p2s = /,?([achlmqrstvxz]),?/gi;
195 R._path2string = function () {
196 return this.join(",")[rp](p2s, "$1");
198 function cacher(f, scope, postprocessor) {
200 var arg = Array[proto].slice.call(arguments, 0),
201 args = arg[join]("\u25ba"),
202 cache = newf.cache = newf.cache || {},
203 count = newf.count = newf.count || [];
204 if (cache[has](args)) {
205 return postprocessor ? postprocessor(cache[args]) : cache[args];
207 count[length] >= 1e3 && delete cache[count.shift()];
209 cache[args] = f[apply](scope, arg);
210 return postprocessor ? postprocessor(cache[args]) : cache[args];
215 R.getRGB = cacher(function (colour) {
216 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
217 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
219 if (colour == "none") {
220 return {r: -1, g: -1, b: -1, hex: "none"};
222 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
228 rgb = colour.match(colourRegExp);
231 blue = toInt(rgb[2].substring(5), 16);
232 green = toInt(rgb[2].substring(3, 5), 16);
233 red = toInt(rgb[2].substring(1, 3), 16);
236 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
237 green = toInt((t = rgb[3].charAt(2)) + t, 16);
238 red = toInt((t = rgb[3].charAt(1)) + t, 16);
241 rgb = rgb[4][split](/\s*,\s*/);
242 red = toFloat(rgb[0]);
243 green = toFloat(rgb[1]);
244 blue = toFloat(rgb[2]);
247 rgb = rgb[5][split](/\s*,\s*/);
248 red = toFloat(rgb[0]) * 2.55;
249 green = toFloat(rgb[1]) * 2.55;
250 blue = toFloat(rgb[2]) * 2.55;
253 rgb = rgb[6][split](/\s*,\s*/);
254 red = toFloat(rgb[0]);
255 green = toFloat(rgb[1]);
256 blue = toFloat(rgb[2]);
257 return R.hsb2rgb(red, green, blue);
260 rgb = rgb[7][split](/\s*,\s*/);
261 red = toFloat(rgb[0]) * 2.55;
262 green = toFloat(rgb[1]) * 2.55;
263 blue = toFloat(rgb[2]) * 2.55;
264 return R.hsb2rgb(red, green, blue);
266 rgb = {r: red, g: green, b: blue};
267 var r = (~~red)[toString](16),
268 g = (~~green)[toString](16),
269 b = (~~blue)[toString](16);
273 rgb.hex = "#" + r + g + b;
276 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
278 R.getColor = function (value) {
279 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
280 rgb = this.hsb2rgb(start.h, start.s, start.b);
285 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
289 R.getColor.reset = function () {
293 R.parsePathString = cacher(function (pathString) {
297 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
299 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
300 data = pathClone(pathString);
303 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
305 name = lowerCase.call(b);
306 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
307 b && params[push](+b);
309 while (params[length] >= paramCounts[name]) {
310 data[push]([b][concat](params.splice(0, paramCounts[name])));
311 if (!paramCounts[name]) {
317 data[toString] = R._path2string;
320 var pathDimensions = cacher(function (path) {
322 return {x: 0, y: 0, width: 0, height: 0};
324 path = path2curve(path);
330 for (var i = 0, ii = path[length]; i < ii; i++) {
338 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
339 X = X[concat](dim.min.x, dim.max.x);
340 Y = Y[concat](dim.min.y, dim.max.y);
345 var xmin = mmin[apply](0, X),
346 ymin = mmin[apply](0, Y);
350 width: mmax[apply](0, X) - xmin,
351 height: mmax[apply](0, Y) - ymin
354 pathClone = function (pathArray) {
356 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
357 pathArray = R.parsePathString(pathArray);
359 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
361 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
362 res[i][j] = pathArray[i][j];
365 res[toString] = R._path2string;
368 pathToRelative = cacher(function (pathArray) {
369 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
370 pathArray = R.parsePathString(pathArray);
378 if (pathArray[0][0] == "M") {
384 res[push](["M", x, y]);
386 for (var i = start, ii = pathArray[length]; i < ii; i++) {
389 if (pa[0] != lowerCase.call(pa[0])) {
390 r[0] = lowerCase.call(pa[0]);
398 r[6] = +(pa[6] - x).toFixed(3);
399 r[7] = +(pa[7] - y).toFixed(3);
402 r[1] = +(pa[1] - y).toFixed(3);
408 for (var j = 1, jj = pa[length]; j < jj; j++) {
409 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
418 for (var k = 0, kk = pa[length]; k < kk; k++) {
422 var len = res[i][length];
429 x += +res[i][len - 1];
432 y += +res[i][len - 1];
435 x += +res[i][len - 2];
436 y += +res[i][len - 1];
439 res[toString] = R._path2string;
442 pathToAbsolute = cacher(function (pathArray) {
443 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
444 pathArray = R.parsePathString(pathArray);
452 if (pathArray[0][0] == "M") {
453 x = +pathArray[0][1];
454 y = +pathArray[0][2];
458 res[0] = ["M", x, y];
460 for (var i = start, ii = pathArray[length]; i < ii; i++) {
463 if (pa[0] != upperCase.call(pa[0])) {
464 r[0] = upperCase.call(pa[0]);
485 for (var j = 1, jj = pa[length]; j < jj; j++) {
486 r[j] = +pa[j] + ((j % 2) ? x : y);
490 for (var k = 0, kk = pa[length]; k < kk; k++) {
506 x = res[i][res[i][length] - 2];
507 y = res[i][res[i][length] - 1];
510 res[toString] = R._path2string;
513 l2c = function (x1, y1, x2, y2) {
514 return [x1, y1, x2, y2, x2, y2];
516 q2c = function (x1, y1, ax, ay, x2, y2) {
528 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
529 // for more information of where this math came from visit:
530 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
532 _120 = PI * 120 / 180,
533 rad = PI / 180 * (+angle || 0),
536 rotate = cacher(function (x, y, rad) {
537 var X = x * math.cos(rad) - y * math.sin(rad),
538 Y = x * math.sin(rad) + y * math.cos(rad);
542 xy = rotate(x1, y1, -rad);
545 xy = rotate(x2, y2, -rad);
548 var cos = math.cos(PI / 180 * angle),
549 sin = math.sin(PI / 180 * angle),
552 rx = mmax(rx, math.abs(x));
553 ry = mmax(ry, math.abs(y));
556 k = (large_arc_flag == sweep_flag ? -1 : 1) *
557 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
558 cx = k * rx * y / ry + (x1 + x2) / 2,
559 cy = k * -ry * x / rx + (y1 + y2) / 2,
560 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
561 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
563 f1 = x1 < cx ? PI - f1 : f1;
564 f2 = x2 < cx ? PI - f2 : f2;
565 f1 < 0 && (f1 = PI * 2 + f1);
566 f2 < 0 && (f2 = PI * 2 + f2);
567 if (sweep_flag && f1 > f2) {
570 if (!sweep_flag && f2 > f1) {
580 if (math.abs(df) > _120) {
584 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
585 x2 = cx + rx * math.cos(f2);
586 y2 = cy + ry * math.sin(f2);
587 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
590 var c1 = math.cos(f1),
594 t = math.tan(df / 4),
598 m2 = [x1 + hx * s1, y1 - hy * c1],
599 m3 = [x2 + hx * s2, y2 - hy * c2],
601 m2[0] = 2 * m1[0] - m2[0];
602 m2[1] = 2 * m1[1] - m2[1];
604 return [m2, m3, m4][concat](res);
606 res = [m2, m3, m4][concat](res)[join]()[split](",");
608 for (var i = 0, ii = res[length]; i < ii; i++) {
609 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
614 findDotAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
617 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
618 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
621 findDotsAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
623 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
624 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
625 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
626 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
627 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
628 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
629 ax = (1 - t) * p1x + t * c1x,
630 ay = (1 - t) * p1y + t * c1y,
631 cx = (1 - t) * c2x + t * p2x,
632 cy = (1 - t) * c2y + t * p2y;
633 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}};
635 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
636 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
637 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
639 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
640 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
644 math.abs(t1) > 1e12 && (t1 = .5);
645 math.abs(t2) > 1e12 && (t2 = .5);
646 if (t1 > 0 && t1 < 1) {
647 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
651 if (t2 > 0 && t2 < 1) {
652 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
656 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
657 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
659 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
660 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
661 math.abs(t1) > 1e12 && (t1 = .5);
662 math.abs(t2) > 1e12 && (t2 = .5);
663 if (t1 > 0 && t1 < 1) {
664 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
668 if (t2 > 0 && t2 < 1) {
669 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
674 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
675 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
678 path2curve = cacher(function (path, path2) {
679 var p = pathToAbsolute(path),
680 p2 = path2 && pathToAbsolute(path2),
681 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
682 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
683 processPath = function (path, d) {
686 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
688 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
695 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
698 nx = d.x + (d.x - (d.bx || d.x));
699 ny = d.y + (d.y - (d.by || d.y));
700 path = ["C", nx, ny][concat](path.slice(1));
703 d.qx = d.x + (d.x - (d.qx || d.x));
704 d.qy = d.y + (d.y - (d.qy || d.y));
705 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
710 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
713 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
716 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
719 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
722 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
727 fixArc = function (pp, i) {
728 if (pp[i][length] > 7) {
732 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
735 ii = mmax(p[length], p2 && p2[length] || 0);
738 fixM = function (path1, path2, a1, a2, i) {
739 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
740 path2.splice(i, 0, ["M", a2.x, a2.y]);
745 ii = mmax(p[length], p2 && p2[length] || 0);
748 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
749 p[i] = processPath(p[i], attrs);
751 p2 && (p2[i] = processPath(p2[i], attrs2));
753 fixM(p, p2, attrs, attrs2, i);
754 fixM(p2, p, attrs2, attrs, i);
757 seglen = seg[length],
758 seg2len = p2 && seg2[length];
759 attrs.x = seg[seglen - 2];
760 attrs.y = seg[seglen - 1];
761 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
762 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
763 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
764 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
765 attrs2.x = p2 && seg2[seg2len - 2];
766 attrs2.y = p2 && seg2[seg2len - 1];
768 return p2 ? [p, p2] : p;
770 parseDots = cacher(function (gradient) {
772 for (var i = 0, ii = gradient[length]; i < ii; i++) {
774 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
775 dot.color = R.getRGB(par[1]);
776 if (dot.color.error) {
779 dot.color = dot.color.hex;
780 par[2] && (dot.offset = par[2] + "%");
783 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
784 if (!dots[i].offset) {
785 var start = toFloat(dots[i - 1].offset || 0),
787 for (var j = i + 1; j < ii; j++) {
788 if (dots[j].offset) {
789 end = dots[j].offset;
798 var d = (end - start) / (j - i + 1);
801 dots[i].offset = start + "%";
807 getContainer = function () {
813 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
814 if (R.is(arguments[0], "string")) {
815 container = doc.getElementById(arguments[0]);
817 container = arguments[0];
819 if (container.tagName) {
820 if (arguments[1] == null) {
822 container: container,
823 width: container.style.pixelWidth || container.offsetWidth,
824 height: container.style.pixelHeight || container.offsetHeight
827 return {container: container, width: arguments[1], height: arguments[2]};
830 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
831 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
834 plugins = function (con, add) {
836 for (var prop in add) if (add[has](prop) && !(prop in con)) {
837 switch (typeof add[prop]) {
840 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
844 con[prop] = con[prop] || {};
845 plugins.call(this, con[prop], add[prop]);
848 con[prop] = add[prop];
853 tear = function (el, paper) {
854 el == paper.top && (paper.top = el.prev);
855 el == paper.bottom && (paper.bottom = el.next);
856 el.next && (el.next.prev = el.prev);
857 el.prev && (el.prev.next = el.next);
859 tofront = function (el, paper) {
860 if (paper.top === el) {
869 toback = function (el, paper) {
870 if (paper.bottom === el) {
874 el.next = paper.bottom;
876 paper.bottom.prev = el;
879 insertafter = function (el, el2, paper) {
881 el2 == paper.top && (paper.top = el);
882 el2.next && (el2.next.prev = el);
887 insertbefore = function (el, el2, paper) {
889 el2 == paper.bottom && (paper.bottom = el);
890 el2.prev && (el2.prev.next = el);
895 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
899 paper.svgns = "http://www.w3.org/2000/svg";
900 paper.xlink = "http://www.w3.org/1999/xlink";
901 var round = function (num) {
902 return +num + (~~num === num) * .5;
904 roundPath = function (path) {
905 for (var i = 0, ii = path[length]; i < ii; i++) {
906 if (lowerCase.call(path[i][0]) != "a") {
907 for (var j = 1, jj = path[i][length]; j < jj; j++) {
908 path[i][j] = round(path[i][j]);
911 path[i][6] = round(path[i][6]);
912 path[i][7] = round(path[i][7]);
917 $ = function (el, attr) {
919 for (var key in attr) if (attr[has](key)) {
920 el[setAttribute](key, attr[key]);
923 return doc.createElementNS(paper.svgns, el);
926 R[toString] = function () {
927 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
929 var thePath = function (pathString, SVG) {
931 SVG.canvas && SVG.canvas[appendChild](el);
932 var p = new Element(el, SVG);
934 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
937 var addGradientFill = function (o, gradient, SVG) {
941 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
946 var dir = ((fy > .5) * 2 - 1);
947 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
948 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
950 (fy = fy.toFixed(5) - 1e-5 * dir);
954 gradient = gradient[split](/\s*\-\s*/);
955 if (type == "linear") {
956 var angle = gradient.shift();
957 angle = -toFloat(angle);
961 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
962 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
966 vector[0] = -vector[2];
970 vector[1] = -vector[3];
974 var dots = parseDots(gradient);
978 var el = $(type + "Gradient");
979 el.id = "r" + (R._id++)[toString](36);
980 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
981 SVG.defs[appendChild](el);
982 for (var i = 0, ii = dots[length]; i < ii; i++) {
983 var stop = $("stop");
985 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
986 "stop-color": dots[i].color || "#fff"
988 el[appendChild](stop);
991 fill: "url(#" + el.id + ")",
1000 var updatePosition = function (o) {
1001 var bbox = o.getBBox();
1002 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1004 var setFillAndStroke = function (o, params) {
1011 "-..": [3, 1, 1, 1, 1, 1],
1015 "- .": [4, 3, 1, 3],
1016 "--.": [8, 3, 1, 3],
1017 "--..": [8, 3, 1, 3, 1, 3]
1022 addDashes = function (o, value) {
1023 value = dasharray[lowerCase.call(value)];
1025 var width = o.attrs["stroke-width"] || "1",
1026 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1028 var i = value[length];
1030 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1032 $(node, {"stroke-dasharray": dashes[join](",")});
1035 params[has]("rotation") && (rot = params.rotation);
1036 var rotxy = (rot + E)[split](separator);
1037 if (!(rotxy.length - 1)) {
1040 rotxy[1] = +rotxy[1];
1041 rotxy[2] = +rotxy[2];
1043 toFloat(rot) && o.rotate(0, true);
1044 for (var att in params) if (params[has](att)) {
1045 if (!availableAttrs[has](att)) {
1048 var value = params[att];
1052 o.rotate(value, true);
1058 var pn = node.parentNode;
1059 if (lowerCase.call(pn.tagName) != "a") {
1061 pn.insertBefore(hl, node);
1062 hl[appendChild](node);
1065 pn.setAttributeNS(o.paper.xlink, att, value);
1068 node.style.cursor = value;
1071 var rect = (value + E)[split](separator);
1072 if (rect[length] == 4) {
1073 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1074 var el = $("clipPath"),
1076 el.id = "r" + (R._id++)[toString](36);
1083 el[appendChild](rc);
1084 o.paper.defs[appendChild](el);
1085 $(node, {"clip-path": "url(#" + el.id + ")"});
1089 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1090 clip && clip.parentNode.removeChild(clip);
1091 $(node, {"clip-path": E});
1096 if (value && o.type == "path") {
1097 attrs.path = roundPath(pathToAbsolute(value));
1098 $(node, {d: attrs.path});
1102 node[setAttribute](att, value);
1111 value = -attrs.x - (attrs.width || 0);
1114 if (att == "rx" && o.type == "rect") {
1118 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1119 node[setAttribute](att, round(value));
1120 o.pattern && updatePosition(o);
1123 node[setAttribute](att, value);
1132 value = -attrs.y - (attrs.height || 0);
1135 if (att == "ry" && o.type == "rect") {
1139 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1140 node[setAttribute](att, round(value));
1141 o.pattern && updatePosition(o);
1144 if (o.type == "rect") {
1145 $(node, {rx: value, ry: value});
1147 node[setAttribute](att, value);
1151 if (o.type == "image") {
1152 node.setAttributeNS(o.paper.xlink, "href", value);
1155 case "stroke-width":
1156 node.style.strokeWidth = value;
1157 // Need following line for Firefox
1158 node[setAttribute](att, value);
1159 if (attrs["stroke-dasharray"]) {
1160 addDashes(o, attrs["stroke-dasharray"]);
1163 case "stroke-dasharray":
1164 addDashes(o, value);
1167 var xy = (value + E)[split](separator);
1168 xy[0] = +xy[0] || 0;
1169 xy[1] = +xy[1] || 0;
1174 translate.call(o, xy[0], xy[1]);
1177 var xy = (value + E)[split](separator);
1178 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1181 var isURL = (value + E).match(ISURL);
1183 var el = $("pattern"),
1185 el.id = "r" + (R._id++)[toString](36);
1186 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1187 $(ig, {x: 0, y: 0});
1188 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1189 el[appendChild](ig);
1191 var img = doc.createElement("img");
1192 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1193 img.onload = function () {
1194 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1195 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1196 doc.body.removeChild(this);
1199 doc.body[appendChild](img);
1201 o.paper.defs[appendChild](el);
1202 node.style.fill = "url(#" + el.id + ")";
1203 $(node, {fill: "url(#" + el.id + ")"});
1205 o.pattern && updatePosition(o);
1208 if (!R.getRGB(value).error) {
1209 delete params.gradient;
1210 delete attrs.gradient;
1211 !R.is(attrs.opacity, "undefined") &&
1212 R.is(params.opacity, "undefined") &&
1213 $(node, {opacity: attrs.opacity});
1214 !R.is(attrs["fill-opacity"], "undefined") &&
1215 R.is(params["fill-opacity"], "undefined") &&
1216 $(node, {"fill-opacity": attrs["fill-opacity"]});
1217 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1218 attrs.gradient = value;
1219 attrs.fill = "none";
1223 node[setAttribute](att, R.getRGB(value).hex);
1226 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1229 case "fill-opacity":
1230 if (attrs.gradient) {
1231 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1233 var stops = gradient.getElementsByTagName("stop");
1234 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1239 att == "font-size" && (value = toInt(value, 10) + "px");
1240 var cssrule = att[rp](/(\-.)/g, function (w) {
1241 return upperCase.call(w.substring(1));
1243 node.style[cssrule] = value;
1244 // Need following line for Firefox
1245 node[setAttribute](att, value);
1250 tuneText(o, params);
1252 o.rotate(rotxy.join(S));
1254 toFloat(rot) && o.rotate(rot, true);
1258 var tuneText = function (el, params) {
1259 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1264 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1266 if (params[has]("text")) {
1267 a.text = params.text;
1268 while (node.firstChild) {
1269 node.removeChild(node.firstChild);
1271 var texts = (params.text + E)[split]("\n");
1272 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1273 var tspan = $("tspan");
1274 i && $(tspan, {dy: fontSize * leading, x: a.x});
1275 tspan[appendChild](doc.createTextNode(texts[i]));
1276 node[appendChild](tspan);
1279 var texts = node.getElementsByTagName("tspan");
1280 for (var i = 0, ii = texts[length]; i < ii; i++) {
1281 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1285 var bb = el.getBBox(),
1286 dif = a.y - (bb.y + bb.height / 2);
1287 dif && isFinite(dif) && $(node, {y: a.y + dif});
1289 var Element = function (node, svg) {
1295 node.raphael = this;
1297 this.attrs = this.attrs || {};
1298 this.transformations = []; // rotate, translate, scale
1302 rt: {deg: 0, cx: 0, cy: 0},
1306 !svg.bottom && (svg.bottom = this);
1307 this.prev = svg.top;
1308 svg.top && (svg.top.next = this);
1312 Element[proto].rotate = function (deg, cx, cy) {
1318 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1320 return this._.rt.deg;
1322 var bbox = this.getBBox();
1323 deg = (deg + E)[split](separator);
1324 if (deg[length] - 1) {
1325 cx = toFloat(deg[1]);
1326 cy = toFloat(deg[2]);
1328 deg = toFloat(deg[0]);
1330 this._.rt.deg = deg;
1332 this._.rt.deg += deg;
1334 (cy == null) && (cx = null);
1337 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1338 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1339 if (this._.rt.deg) {
1340 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1341 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1343 this.transformations[0] = E;
1344 this.clip && $(this.clip, {transform: E});
1346 $(this.node, {transform: this.transformations[join](S)});
1349 Element[proto].hide = function () {
1350 !this.removed && (this.node.style.display = "none");
1353 Element[proto].show = function () {
1354 !this.removed && (this.node.style.display = "");
1357 Element[proto].remove = function () {
1361 tear(this, this.paper);
1362 this.node.parentNode.removeChild(this.node);
1363 for (var i in this) {
1366 this.removed = true;
1368 Element[proto].getBBox = function () {
1372 if (this.type == "path") {
1373 return pathDimensions(this.attrs.path);
1375 if (this.node.style.display == "none") {
1381 bbox = this.node.getBBox();
1383 // Firefox 3.0.x plays badly here
1387 if (this.type == "text") {
1388 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1389 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1390 var bb = this.node.getExtentOfChar(i);
1391 (bb.y < bbox.y) && (bbox.y = bb.y);
1392 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1393 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1396 hide && this.hide();
1399 Element[proto].attr = function () {
1403 if (arguments[length] == 0) {
1405 for (var i in this.attrs) if (this.attrs[has](i)) {
1406 res[i] = this.attrs[i];
1408 this._.rt.deg && (res.rotation = this.rotate());
1409 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1410 delete res.translation;
1411 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1414 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1415 if (arguments[0] == "translation") {
1416 return translate.call(this);
1418 if (arguments[0] == "rotation") {
1419 return this.rotate();
1421 if (arguments[0] == "scale") {
1422 return this.scale();
1424 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1425 return this.attrs.gradient;
1427 return this.attrs[arguments[0]];
1429 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1431 for (var j in arguments[0]) if (arguments[0][has](j)) {
1432 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1436 if (arguments[length] == 2) {
1438 params[arguments[0]] = arguments[1];
1439 setFillAndStroke(this, params);
1440 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1441 setFillAndStroke(this, arguments[0]);
1445 Element[proto].toFront = function () {
1449 this.node.parentNode[appendChild](this.node);
1450 var svg = this.paper;
1451 svg.top != this && tofront(this, svg);
1454 Element[proto].toBack = function () {
1458 if (this.node.parentNode.firstChild != this.node) {
1459 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1460 toback(this, this.paper);
1461 var svg = this.paper;
1465 Element[proto].insertAfter = function (element) {
1469 var svg = this.paper,
1470 node = element.node;
1471 if (node.nextSibling) {
1472 node.parentNode.insertBefore(this.node, node.nextSibling);
1474 node.parentNode[appendChild](this.node);
1476 insertafter(this, element, this.paper);
1479 Element[proto].insertBefore = function (element) {
1483 var node = element.node;
1484 node.parentNode.insertBefore(this.node, node);
1485 insertbefore(this, element, this.paper);
1489 var theCircle = function (svg, x, y, r) {
1492 var el = $("circle");
1493 svg.canvas && svg.canvas[appendChild](el);
1494 var res = new Element(el, svg);
1495 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1496 res.type = "circle";
1500 var theRect = function (svg, x, y, w, h, r) {
1504 svg.canvas && svg.canvas[appendChild](el);
1505 var res = new Element(el, svg);
1506 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1511 var theEllipse = function (svg, x, y, rx, ry) {
1514 var el = $("ellipse");
1515 svg.canvas && svg.canvas[appendChild](el);
1516 var res = new Element(el, svg);
1517 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1518 res.type = "ellipse";
1522 var theImage = function (svg, src, x, y, w, h) {
1523 var el = $("image");
1524 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1525 el.setAttributeNS(svg.xlink, "href", src);
1526 svg.canvas && svg.canvas[appendChild](el);
1527 var res = new Element(el, svg);
1528 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1532 var theText = function (svg, x, y, text) {
1534 $(el, {x: x, y: y, "text-anchor": "middle"});
1535 svg.canvas && svg.canvas[appendChild](el);
1536 var res = new Element(el, svg);
1537 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1539 setFillAndStroke(res, res.attrs);
1542 var setSize = function (width, height) {
1543 this.width = width || this.width;
1544 this.height = height || this.height;
1545 this.canvas[setAttribute]("width", this.width);
1546 this.canvas[setAttribute]("height", this.height);
1549 var create = function () {
1550 var con = getContainer[apply](null, arguments),
1551 container = con && con.container,
1555 height = con.height;
1557 throw new Error("SVG container not found.");
1559 paper.canvas = $("svg");
1560 var cnvs = paper.canvas;
1561 paper.width = width || 512;
1562 paper.height = height || 342;
1564 xmlns: "http://www.w3.org/2000/svg",
1567 height: paper.height
1569 if (container == 1) {
1570 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1571 doc.body[appendChild](cnvs);
1573 if (container.firstChild) {
1574 container.insertBefore(cnvs, container.firstChild);
1576 container[appendChild](cnvs);
1579 container = { canvas: cnvs };
1580 for (var prop in paper) if (paper[has](prop)) {
1581 container[prop] = paper[prop];
1583 container.bottom = container.top = null;
1584 plugins.call(container, container, R.fn);
1586 container.raphael = R;
1589 paper.clear = function () {
1590 var c = this.canvas;
1591 while (c.firstChild) {
1592 c.removeChild(c.firstChild);
1594 this.bottom = this.top = null;
1595 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1596 c[appendChild](this.desc);
1597 c[appendChild](this.defs = $("defs"));
1599 paper.remove = function () {
1600 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1601 for (var i in this) {
1609 var path2vml = function (path) {
1610 var total = /[ahqtv]/ig,
1611 command = pathToAbsolute;
1612 (path + E).match(total) && (command = path2curve);
1614 if (command == pathToAbsolute && !(path + E).match(total)) {
1615 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1616 bites = /([clmz]),?([^clmz]*)/gi,
1617 val = /-?[^,\s-]+/g;
1618 var res = (path + E)[rp](bites, function (all, command, args) {
1620 args[rp](val, function (value) {
1621 vals[push](round(value));
1623 return map[command] + vals;
1627 var pa = command(path), p, res = [], r;
1628 for (var i = 0, ii = pa[length]; i < ii; i++) {
1630 r = lowerCase.call(pa[i][0]);
1631 r == "z" && (r = "x");
1632 for (var j = 1, jj = p[length]; j < jj; j++) {
1633 r += round(p[j]) + (j != jj - 1 ? "," : E);
1637 return res[join](S);
1640 R[toString] = function () {
1641 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1643 var thePath = function (pathString, VML) {
1644 var g = createNode("group");
1645 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1646 g.coordsize = VML.coordsize;
1647 g.coordorigin = VML.coordorigin;
1648 var el = createNode("shape"), ol = el.style;
1649 ol.width = VML.width + "px";
1650 ol.height = VML.height + "px";
1651 el.coordsize = this.coordsize;
1652 el.coordorigin = this.coordorigin;
1654 var p = new Element(el, g, VML);
1655 p.isAbsolute = true;
1659 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1660 VML.canvas[appendChild](g);
1663 var setFillAndStroke = function (o, params) {
1664 o.attrs = o.attrs || {};
1670 for (var par in params) if (params[has](par)) {
1671 a[par] = params[par];
1673 params.href && (node.href = params.href);
1674 params.title && (node.title = params.title);
1675 params.target && (node.target = params.target);
1676 params.cursor && (s.cursor = params.cursor);
1677 if (params.path && o.type == "path") {
1678 a.path = params.path;
1679 node.path = path2vml(a.path);
1681 if (params.rotation != null) {
1682 o.rotate(params.rotation, true);
1684 if (params.translation) {
1685 xy = (params.translation + E)[split](separator);
1686 translate.call(o, xy[0], xy[1]);
1687 if (o._.rt.cx != null) {
1688 o._.rt.cx +=+ xy[0];
1689 o._.rt.cy +=+ xy[1];
1690 o.setBox(o.attrs, xy[0], xy[1]);
1694 xy = (params.scale + E)[split](separator);
1695 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1697 if ("clip-rect" in params) {
1698 var rect = (params["clip-rect"] + E)[split](separator);
1699 if (rect[length] == 4) {
1700 rect[2] = +rect[2] + (+rect[0]);
1701 rect[3] = +rect[3] + (+rect[1]);
1702 var div = node.clipRect || doc.createElement("div"),
1704 group = node.parentNode;
1705 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1706 if (!node.clipRect) {
1707 dstyle.position = "absolute";
1710 dstyle.width = o.paper.width + "px";
1711 dstyle.height = o.paper.height + "px";
1712 group.parentNode.insertBefore(div, group);
1713 div[appendChild](group);
1714 node.clipRect = div;
1717 if (!params["clip-rect"]) {
1718 node.clipRect && (node.clipRect.style.clip = E);
1721 if (o.type == "image" && params.src) {
1722 node.src = params.src;
1724 if (o.type == "image" && params.opacity) {
1725 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1726 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1728 params.font && (s.font = params.font);
1729 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1730 params["font-size"] && (s.fontSize = params["font-size"]);
1731 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1732 params["font-style"] && (s.fontStyle = params["font-style"]);
1733 if (params.opacity != null ||
1734 params["stroke-width"] != null ||
1735 params.fill != null ||
1736 params.stroke != null ||
1737 params["stroke-width"] != null ||
1738 params["stroke-opacity"] != null ||
1739 params["fill-opacity"] != null ||
1740 params["stroke-dasharray"] != null ||
1741 params["stroke-miterlimit"] != null ||
1742 params["stroke-linejoin"] != null ||
1743 params["stroke-linecap"] != null) {
1744 node = o.shape || node;
1745 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1747 !fill && (newfill = fill = createNode("fill"));
1748 if ("fill-opacity" in params || "opacity" in params) {
1749 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1750 opacity < 0 && (opacity = 0);
1751 opacity > 1 && (opacity = 1);
1752 fill.opacity = opacity;
1754 params.fill && (fill.on = true);
1755 if (fill.on == null || params.fill == "none") {
1758 if (fill.on && params.fill) {
1759 var isURL = params.fill.match(ISURL);
1761 fill.src = isURL[1];
1764 fill.color = R.getRGB(params.fill).hex;
1766 fill.type = "solid";
1767 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1769 a.gradient = params.fill;
1773 newfill && node[appendChild](fill);
1774 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1776 !stroke && (newstroke = stroke = createNode("stroke"));
1777 if ((params.stroke && params.stroke != "none") ||
1778 params["stroke-width"] ||
1779 params["stroke-opacity"] != null ||
1780 params["stroke-dasharray"] ||
1781 params["stroke-miterlimit"] ||
1782 params["stroke-linejoin"] ||
1783 params["stroke-linecap"]) {
1786 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1787 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1788 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1789 width = (toFloat(params["stroke-width"]) || 1) * .75;
1790 opacity < 0 && (opacity = 0);
1791 opacity > 1 && (opacity = 1);
1792 params["stroke-width"] == null && (width = a["stroke-width"]);
1793 params["stroke-width"] && (stroke.weight = width);
1794 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1795 stroke.opacity = opacity;
1797 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1798 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1799 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1800 if (params["stroke-dasharray"]) {
1804 "-.": "shortdashdot",
1805 "-..": "shortdashdotdot",
1810 "--.": "longdashdot",
1811 "--..": "longdashdotdot"
1813 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1815 newstroke && node[appendChild](stroke);
1817 if (res.type == "text") {
1818 var s = res.paper.span.style;
1819 a.font && (s.font = a.font);
1820 a["font-family"] && (s.fontFamily = a["font-family"]);
1821 a["font-size"] && (s.fontSize = a["font-size"]);
1822 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1823 a["font-style"] && (s.fontStyle = a["font-style"]);
1824 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1825 res.W = a.w = res.paper.span.offsetWidth;
1826 res.H = a.h = res.paper.span.offsetHeight;
1828 res.Y = a.y + round(res.H / 2);
1830 // text-anchor emulationm
1831 switch (a["text-anchor"]) {
1833 res.node.style["v-text-align"] = "left";
1834 res.bbx = round(res.W / 2);
1837 res.node.style["v-text-align"] = "right";
1838 res.bbx = -round(res.W / 2);
1841 res.node.style["v-text-align"] = "center";
1846 var addGradientFill = function (o, gradient) {
1847 o.attrs = o.attrs || {};
1848 var attrs = o.attrs,
1849 fill = o.node.getElementsByTagName("fill"),
1852 o.attrs.gradient = gradient;
1853 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1858 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1863 gradient = gradient[split](/\s*\-\s*/);
1864 if (type == "linear") {
1865 var angle = gradient.shift();
1866 angle = -toFloat(angle);
1871 var dots = parseDots(gradient);
1875 o = o.shape || o.node;
1876 fill = fill[0] || createNode("fill");
1879 fill.method = "none";
1880 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1881 fill.color = dots[0].color;
1882 fill.color2 = dots[dots[length] - 1].color;
1884 for (var i = 0, ii = dots[length]; i < ii; i++) {
1885 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1887 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1888 if (type == "radial") {
1889 fill.focus = "100%";
1890 fill.focussize = fxfy;
1891 fill.focusposition = fxfy;
1893 fill.angle = (270 - angle) % 360;
1898 var Element = function (node, group, vml) {
1906 node.raphael = this;
1919 !vml.bottom && (vml.bottom = this);
1920 this.prev = vml.top;
1921 vml.top && (vml.top.next = this);
1925 Element[proto].rotate = function (deg, cx, cy) {
1931 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1933 return this._.rt.deg;
1935 deg = (deg + E)[split](separator);
1936 if (deg[length] - 1) {
1937 cx = toFloat(deg[1]);
1938 cy = toFloat(deg[2]);
1940 deg = toFloat(deg[0]);
1942 this._.rt.deg = deg;
1944 this._.rt.deg += deg;
1946 cy == null && (cx = null);
1949 this.setBox(this.attrs, cx, cy);
1950 this.Group.style.rotation = this._.rt.deg;
1951 // gradient fix for rotation. TODO
1952 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1953 // fill = fill[0] || {};
1954 // var b = ((360 - this._.rt.deg) - 270) % 360;
1955 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1958 Element[proto].setBox = function (params, cx, cy) {
1962 var gs = this.Group.style,
1963 os = (this.shape && this.shape.style) || this.node.style;
1964 params = params || {};
1965 for (var i in params) if (params[has](i)) {
1966 this.attrs[i] = params[i];
1968 cx = cx || this._.rt.cx;
1969 cy = cy || this._.rt.cy;
1970 var attr = this.attrs,
1975 switch (this.type) {
1977 x = attr.cx - attr.r;
1978 y = attr.cy - attr.r;
1982 x = attr.cx - attr.rx;
1983 y = attr.cy - attr.ry;
1991 w = attr.width || 0;
1992 h = attr.height || 0;
1995 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
1996 x = attr.x - round(this.W / 2);
1997 y = attr.y - this.H / 2;
2002 if (!this.attrs.path) {
2005 w = this.paper.width;
2006 h = this.paper.height;
2008 var dim = pathDimensions(this.attrs.path);
2018 w = this.paper.width;
2019 h = this.paper.height;
2022 cx = (cx == null) ? x + w / 2 : cx;
2023 cy = (cy == null) ? y + h / 2 : cy;
2024 var left = cx - this.paper.width / 2,
2025 top = cy - this.paper.height / 2;
2026 if (this.type == "path" || this.type == "text") {
2027 (gs.left != left + "px") && (gs.left = left + "px");
2028 (gs.top != top + "px") && (gs.top = top + "px");
2029 this.X = this.type == "text" ? x : -left;
2030 this.Y = this.type == "text" ? y : -top;
2033 (os.left != -left + "px") && (os.left = -left + "px");
2034 (os.top != -top + "px") && (os.top = -top + "px");
2036 (gs.left != left + "px") && (gs.left = left + "px");
2037 (gs.top != top + "px") && (gs.top = top + "px");
2042 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2043 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2044 (os.left != x - left + "px") && (os.left = x - left + "px");
2045 (os.top != y - top + "px") && (os.top = y - top + "px");
2046 (os.width != w + "px") && (os.width = w + "px");
2047 (os.height != h + "px") && (os.height = h + "px");
2048 var arcsize = (+params.r || 0) / mmin(w, h);
2049 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2050 // We should replace element with the new one
2051 var o = createNode("roundrect"),
2054 ii = this.events && this.events[length];
2055 o.arcsize = arcsize;
2057 this.Group[appendChild](o);
2058 this.Group.removeChild(this.node);
2059 this[0] = this.node = o;
2060 this.arcsize = arcsize;
2061 for (var i in attr) {
2066 if (this.events) for (; i < ii; i++) {
2067 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2072 Element[proto].hide = function () {
2073 !this.removed && (this.Group.style.display = "none");
2076 Element[proto].show = function () {
2077 !this.removed && (this.Group.style.display = "block");
2080 Element[proto].getBBox = function () {
2084 if (this.type == "path") {
2085 return pathDimensions(this.attrs.path);
2088 x: this.X + (this.bbx || 0),
2094 Element[proto].remove = function () {
2098 tear(this, this.paper);
2099 this.node.parentNode.removeChild(this.node);
2100 this.Group.parentNode.removeChild(this.Group);
2101 this.shape && this.shape.parentNode.removeChild(this.shape);
2102 for (var i in this) {
2105 this.removed = true;
2107 Element[proto].attr = function () {
2111 if (arguments[length] == 0) {
2113 for (var i in this.attrs) if (this.attrs[has](i)) {
2114 res[i] = this.attrs[i];
2116 this._.rt.deg && (res.rotation = this.rotate());
2117 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2118 delete res.translation;
2119 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2122 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2123 if (arguments[0] == "translation") {
2124 return translate.call(this);
2126 if (arguments[0] == "rotation") {
2127 return this.rotate();
2129 if (arguments[0] == "scale") {
2130 return this.scale();
2132 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2133 return this.attrs.gradient;
2135 return this.attrs[arguments[0]];
2137 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2139 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2140 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2145 if (arguments[length] == 2) {
2147 params[arguments[0]] = arguments[1];
2149 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2151 if (params.text && this.type == "text") {
2152 this.node.string = params.text;
2154 setFillAndStroke(this, params);
2155 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2156 addGradientFill(this, params.gradient);
2158 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2162 Element[proto].toFront = function () {
2163 !this.removed && this.Group.parentNode[appendChild](this.Group);
2164 this.paper.top != this && tofront(this, this.paper);
2167 Element[proto].toBack = function () {
2171 if (this.Group.parentNode.firstChild != this.Group) {
2172 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2173 toback(this, this.paper);
2177 Element[proto].insertAfter = function (element) {
2181 if (element.Group.nextSibling) {
2182 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2184 element.Group.parentNode[appendChild](this.Group);
2186 insertafter(this, element, this.paper);
2189 Element[proto].insertBefore = function (element) {
2193 element.Group.parentNode.insertBefore(this.Group, element.Group);
2194 insertbefore(this, element, this.paper);
2198 var theCircle = function (vml, x, y, r) {
2199 var g = createNode("group"),
2200 o = createNode("oval"),
2202 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2203 g.coordsize = vml.coordsize;
2204 g.coordorigin = vml.coordorigin;
2206 var res = new Element(o, g, vml);
2207 res.type = "circle";
2208 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2212 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2213 vml.canvas[appendChild](g);
2216 var theRect = function (vml, x, y, w, h, r) {
2217 var g = createNode("group"),
2218 o = createNode("roundrect"),
2219 arcsize = (+r || 0) / (mmin(w, h));
2220 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2221 g.coordsize = vml.coordsize;
2222 g.coordorigin = vml.coordorigin;
2224 o.arcsize = arcsize;
2225 var res = new Element(o, g, vml);
2227 setFillAndStroke(res, {stroke: "#000"});
2228 res.arcsize = arcsize;
2229 res.setBox({x: x, y: y, width: w, height: h, r: r});
2230 vml.canvas[appendChild](g);
2233 var theEllipse = function (vml, x, y, rx, ry) {
2234 var g = createNode("group"),
2235 o = createNode("oval"),
2237 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2238 g.coordsize = vml.coordsize;
2239 g.coordorigin = vml.coordorigin;
2241 var res = new Element(o, g, vml);
2242 res.type = "ellipse";
2243 setFillAndStroke(res, {stroke: "#000"});
2248 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2249 vml.canvas[appendChild](g);
2252 var theImage = function (vml, src, x, y, w, h) {
2253 var g = createNode("group"),
2254 o = createNode("image"),
2256 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2257 g.coordsize = vml.coordsize;
2258 g.coordorigin = vml.coordorigin;
2261 var res = new Element(o, g, vml);
2263 res.attrs.src = src;
2268 res.setBox({x: x, y: y, width: w, height: h});
2269 vml.canvas[appendChild](g);
2272 var theText = function (vml, x, y, text) {
2273 var g = createNode("group"),
2274 el = createNode("shape"),
2276 path = createNode("path"),
2278 o = createNode("textpath");
2279 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2280 g.coordsize = vml.coordsize;
2281 g.coordorigin = vml.coordorigin;
2282 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2283 path.textpathok = true;
2284 ol.width = vml.width;
2285 ol.height = vml.height;
2286 o.string = text + E;
2289 el[appendChild](path);
2291 var res = new Element(o, g, vml);
2293 res.textpath = path;
2295 res.attrs.text = text;
2300 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2302 vml.canvas[appendChild](g);
2305 var setSize = function (width, height) {
2306 var cs = this.canvas.style;
2307 width == +width && (width += "px");
2308 height == +height && (height += "px");
2311 cs.clip = "rect(0 " + width + " " + height + " 0)";
2314 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2316 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2317 var createNode = function (tagName) {
2318 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2321 var createNode = function (tagName) {
2322 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2325 var create = function () {
2326 var con = getContainer[apply](null, arguments),
2327 container = con.container,
2328 height = con.height,
2334 throw new Error("VML container not found.");
2337 c = res.canvas = doc.createElement("div"),
2339 width = width || 512;
2340 height = height || 342;
2341 width == +width && (width += "px");
2342 height == +height && (height += "px");
2345 res.coordsize = "1000 1000";
2346 res.coordorigin = "0 0";
2347 res.span = doc.createElement("span");
2348 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2349 c[appendChild](res.span);
2350 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2351 if (container == 1) {
2352 doc.body[appendChild](c);
2362 container.style.width = width;
2363 container.style.height = height;
2364 if (container.firstChild) {
2365 container.insertBefore(c, container.firstChild);
2367 container[appendChild](c);
2370 for (var prop in paper) if (paper[has](prop)) {
2371 res[prop] = paper[prop];
2373 plugins.call(res, res, R.fn);
2374 res.top = res.bottom = null;
2378 paper.clear = function () {
2379 this.canvas.innerHTML = E;
2380 this.bottom = this.top = null;
2382 paper.remove = function () {
2383 this.canvas.parentNode.removeChild(this.canvas);
2384 for (var i in this) {
2391 // Safari or Chrome (WebKit) rendering bug workaround method
2392 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2393 paper.safari = function () {
2394 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2395 setTimeout(function () {rect.remove();});
2398 paper.safari = function () {};
2402 var addEvent = (function () {
2403 if (doc.addEventListener) {
2404 return function (obj, type, fn, element) {
2405 var f = function (e) {
2406 return fn.call(element, e);
2408 obj.addEventListener(type, f, false);
2409 return function () {
2410 obj.removeEventListener(type, f, false);
2414 } else if (doc.attachEvent) {
2415 return function (obj, type, fn, element) {
2416 var f = function (e) {
2417 return fn.call(element, e || win.event);
2419 obj.attachEvent("on" + type, f);
2420 var detacher = function () {
2421 obj.detachEvent("on" + type, f);
2428 for (var i = events[length]; i--;) {
2429 (function (eventName) {
2430 Element[proto][eventName] = function (fn) {
2431 if (R.is(fn, "function")) {
2432 this.events = this.events || [];
2433 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2437 Element[proto]["un" + eventName] = function (fn) {
2438 var events = this.events,
2440 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2442 events.splice(l, 1);
2443 !events.length && delete this.events;
2450 Element[proto].hover = function (f_in, f_out) {
2451 return this.mouseover(f_in).mouseout(f_out);
2453 Element[proto].unhover = function (f_in, f_out) {
2454 return this.unmouseover(f_in).unmouseout(f_out);
2456 paper.circle = function (x, y, r) {
2457 return theCircle(this, x || 0, y || 0, r || 0);
2459 paper.rect = function (x, y, w, h, r) {
2460 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2462 paper.ellipse = function (x, y, rx, ry) {
2463 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2465 paper.path = function (pathString) {
2466 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2467 return thePath(R.format[apply](R, arguments), this);
2469 paper.image = function (src, x, y, w, h) {
2470 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2472 paper.text = function (x, y, text) {
2473 return theText(this, x || 0, y || 0, text || E);
2475 paper.set = function (itemsArray) {
2476 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2477 return new Set(itemsArray);
2479 paper.setSize = setSize;
2481 return this.x + S + this.y;
2483 Element[proto].scale = function (x, y, cx, cy) {
2484 if (x == null && y == null) {
2499 var bb = this.getBBox(),
2500 rcx = bb.x + bb.width / 2,
2501 rcy = bb.y + bb.height / 2,
2504 cx = (+cx || cx == 0) ? cx : rcx;
2505 cy = (+cy || cy == 0) ? cy : rcy;
2506 var dirx = ~~(x / math.abs(x)),
2507 diry = ~~(y / math.abs(y)),
2508 s = this.node.style,
2509 ncx = cx + (rcx - cx) * kx,
2510 ncy = cy + (rcy - cy) * ky;
2511 switch (this.type) {
2514 var neww = a.width * dirx * kx,
2515 newh = a.height * diry * ky,
2516 newr = a.r * mmin(kx, ky),
2517 newx = ncx - neww / 2,
2518 newy = ncy - newh / 2;
2530 rx: a.rx * dirx * kx,
2531 ry: a.ry * diry * ky,
2532 r: a.r * mmin(dirx * kx, diry * ky),
2538 var path = pathToRelative(a.path),
2540 for (var i = 0, ii = path[length]; i < ii; i++) {
2543 P0 = upperCase.call(p[0]);
2544 if (P0 == "M" && skip) {
2550 p[path[i][length] - 2] *= kx;
2551 p[path[i][length] - 1] *= ky;
2554 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2555 } else if (P0 == "H") {
2556 for (j = 1, jj = p[length]; j < jj; j++) {
2559 } else if (P0 == "V") {
2560 for (j = 1, jj = p[length]; j < jj; j++) {
2564 for (j = 1, jj = p[length]; j < jj; j++) {
2565 p[j] *= (j % 2) ? kx : ky;
2569 var dim2 = pathDimensions(path),
2570 dx = ncx - dim2.x - dim2.width / 2,
2571 dy = ncy - dim2.y - dim2.height / 2;
2575 this.attr({path: path});
2578 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2579 if (this.transformations) {
2580 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2581 this.node[setAttribute]("transform", this.transformations[join](S));
2582 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2583 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2584 this.attr({x: dx, y: dy});
2588 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2589 ", M12=0, M21=0, M22=", diry,
2590 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2591 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2594 if (this.transformations) {
2595 this.transformations[2] = E;
2596 this.node[setAttribute]("transform", this.transformations[join](S));
2600 this.node.filterMatrix = E;
2601 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2604 a.scale = [x, y, cx, cy][join](S);
2610 Element[proto].clone = function () {
2611 return this.paper[this.type]().attr(this.attr());
2614 // animation easing formulas
2615 R.easing_formulas = {
2616 linear: function (n) {
2623 return pow(n - 1, 3) + 1;
2625 "<>": function (n) {
2628 return pow(n, 3) / 2;
2631 return (pow(n, 3) + 2) / 2;
2633 backIn: function (n) {
2635 return n * n * ((s + 1) * n - s);
2637 backOut: function (n) {
2640 return n * n * ((s + 1) * n + s) + 1;
2642 elastic: function (n) {
2643 if (n == 0 || n == 1) {
2648 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2650 bounce: function (n) {
2659 l = s * n * n + .75;
2661 if (n < (2.5 / p)) {
2663 l = s * n * n + .9375;
2666 l = s * n * n + .984375;
2674 var animationElements = {length : 0},
2675 animation = function () {
2676 var Now = +new Date;
2677 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2678 var e = animationElements[l];
2680 delete animationElements[l];
2681 animationElements[length]--;
2684 var time = Now - e.start,
2693 callback = e.callback,
2697 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2698 for (var attr in from) if (from[has](attr)) {
2699 switch (availableAnimAttrs[attr]) {
2701 now = +from[attr] + pos * ms * diff[attr];
2705 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2706 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2707 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2712 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2713 now[i] = [from[attr][i][0]];
2714 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2715 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2717 now[i] = now[i][join](S);
2724 var x = diff[attr][0] * (time - prev),
2725 y = diff[attr][1] * (time - prev);
2731 now = +from[attr][0] + pos * ms * diff[attr][0];
2732 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2735 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);
2741 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2750 that._run && that._run.call(that);
2752 (t.x || t.y) && that.translate(-t.x, -t.y);
2753 to.scale && (to.scale = to.scale + E);
2755 delete animationElements[l];
2756 animationElements[length]--;
2757 that.in_animation = null;
2758 R.is(callback, "function") && callback.call(that);
2762 R.svg && paper.safari();
2763 animationElements[length] && setTimeout(animation);
2765 upto255 = function (color) {
2766 return color > 255 ? 255 : (color < 0 ? 0 : color);
2768 translate = function (x, y) {
2770 return {x: this._.tx, y: this._.ty, toString: x_y};
2774 switch (this.type) {
2777 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2782 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2785 var path = pathToRelative(this.attrs.path);
2788 this.attr({path: path});
2793 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2794 animationElements[element.id] && (params.start = animationElements[element.id].start);
2795 return this.animate(params, ms, easing, callback);
2797 Element[proto].onAnimation = function (f) {
2798 this._run = f || null;
2801 Element[proto].animate = function (params, ms, easing, callback) {
2802 if (R.is(easing, "function") || !easing) {
2803 callback = easing || null;
2808 for (var attr in params) if (params[has](attr)) {
2809 if (availableAnimAttrs[has](attr)) {
2810 from[attr] = this.attr(attr);
2811 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2812 to[attr] = params[attr];
2813 switch (availableAnimAttrs[attr]) {
2815 diff[attr] = (to[attr] - from[attr]) / ms;
2818 from[attr] = R.getRGB(from[attr]);
2819 var toColour = R.getRGB(to[attr]);
2821 r: (toColour.r - from[attr].r) / ms,
2822 g: (toColour.g - from[attr].g) / ms,
2823 b: (toColour.b - from[attr].b) / ms
2827 var pathes = path2curve(from[attr], to[attr]);
2828 from[attr] = pathes[0];
2829 to[attr] = pathes[1];
2831 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2832 diff[attr][i] = [0];
2833 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2834 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2839 var values = (params[attr] + E)[split](separator),
2840 from2 = (from[attr] + E)[split](separator);
2843 from[attr] = [0, 0];
2844 diff[attr] = [values[0] / ms, values[1] / ms];
2847 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2848 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2851 params[attr] = values;
2852 from[attr] = (from[attr] + E)[split](separator);
2853 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2856 from[attr] = (from[attr] + E)[split](separator);
2860 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2869 this.in_animation = 1;
2870 animationElements[this.id] = {
2871 start: params.start || +new Date,
2881 ++animationElements[length] == 1 && animation();
2884 Element[proto].stop = function () {
2885 animationElements[this.id] && animationElements[length]--;
2886 delete animationElements[this.id];
2889 Element[proto].translate = function (x, y) {
2890 return this.attr({translation: x + " " + y});
2892 Element[proto][toString] = function () {
2893 return "Rapha\xebl\u2019s object";
2895 R.ae = animationElements;
2898 var Set = function (items) {
2902 for (var i = 0, ii = items[length]; i < ii; i++) {
2903 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2904 this[this.items[length]] = this.items[this.items[length]] = items[i];
2910 Set[proto][push] = function () {
2913 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2914 item = arguments[i];
2915 if (item && (item.constructor == Element || item.constructor == Set)) {
2916 len = this.items[length];
2917 this[len] = this.items[len] = item;
2923 Set[proto].pop = function () {
2924 delete this[this[length]--];
2925 return this.items.pop();
2927 for (var method in Element[proto]) if (Element[proto][has](method)) {
2928 Set[proto][method] = (function (methodname) {
2929 return function () {
2930 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2931 this.items[i][methodname][apply](this.items[i], arguments);
2937 Set[proto].attr = function (name, value) {
2938 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2939 for (var j = 0, jj = name[length]; j < jj; j++) {
2940 this.items[j].attr(name[j]);
2943 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2944 this.items[i].attr[apply](this.items[i], arguments);
2949 Set[proto].animate = function (params, ms, easing, callback) {
2950 (R.is(easing, "function") || !easing) && (callback = easing || null);
2951 var len = this.items[length],
2955 callback && (collector = function () {
2956 !--len && callback.call(set);
2958 this.items[--i].animate(params, ms, easing || collector, collector);
2960 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
2964 Set[proto].insertAfter = function (el) {
2965 var i = this.items[length];
2967 this.items[i].insertAfter(el);
2971 Set[proto].getBBox = function () {
2976 for (var i = this.items[length]; i--;) {
2977 var box = this.items[i].getBBox();
2980 w[push](box.x + box.width);
2981 h[push](box.y + box.height);
2983 x = mmin[apply](0, x);
2984 y = mmin[apply](0, y);
2988 width: mmax[apply](0, w) - x,
2989 height: mmax[apply](0, h) - y
2993 R.registerFont = function (font) {
2997 this.fonts = this.fonts || {};
3003 family = font.face["font-family"];
3004 for (var prop in font.face) if (font.face[has](prop)) {
3005 fontcopy.face[prop] = font.face[prop];
3007 if (this.fonts[family]) {
3008 this.fonts[family][push](fontcopy);
3010 this.fonts[family] = [fontcopy];
3013 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3014 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3015 var path = font.glyphs[glyph];
3016 fontcopy.glyphs[glyph] = {
3019 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3020 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3024 for (var k in path.k) if (path[has](k)) {
3025 fontcopy.glyphs[glyph].k[k] = path.k[k];
3032 paper.getFont = function (family, weight, style, stretch) {
3033 stretch = stretch || "normal";
3034 style = style || "normal";
3035 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3036 var font = R.fonts[family];
3038 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3039 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3040 if (name.test(fontName)) {
3041 font = R.fonts[fontName];
3048 for (var i = 0, ii = font[length]; i < ii; i++) {
3050 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3057 paper.print = function (x, y, string, font, size, origin) {
3058 origin = origin || "middle"; // baseline|middle
3059 var out = this.set(),
3060 letters = (string + E)[split](E),
3064 R.is(font, "string") && (font = this.getFont(font));
3066 scale = (size || 16) / font.face["units-per-em"];
3067 var bb = font.face.bbox.split(separator),
3069 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3070 for (var i = 0, ii = letters[length]; i < ii; i++) {
3071 var prev = i && font.glyphs[letters[i - 1]] || {},
3072 curr = font.glyphs[letters[i]];
3073 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3074 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3076 out.scale(scale, scale, top, height).translate(x - top, y - height);
3081 R.format = function (token) {
3082 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3084 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3085 return args[++i] == null ? E : args[i];
3089 R.ninja = function () {
3090 var r = win.Raphael, u;
3091 if (oldRaphael.was) {
3092 win.Raphael = oldRaphael.is;
3102 R.el = Element[proto];