2 * Raphael 1.2.6 - 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",
35 events = "click dblclick mousedown mousemove mouseout mouseover mouseup".split(S),
36 has = "hasOwnProperty",
37 isit = /^\[object\s+|\]$/gi,
41 lowerCase = String[proto].toLowerCase,
46 toString = "toString",
47 objectToString = Object[proto][toString],
52 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
54 setAttribute = "setAttribute",
58 upperCase = String[proto].toUpperCase,
59 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},
60 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},
63 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
64 R.svg = !(R.vml = R.type == "VML");
68 R.is = function (o, type) {
69 type = lowerCase.call(type);
70 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o)[rp](isit, E)) == type;
72 R.setWindow = function (newwin) {
77 var toHex = function (color) {
79 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
80 var trim = /^\s+|\s+$/g;
81 toHex = cacher(function (color) {
83 color = (color + E).replace(trim, E);
85 var document = new ActiveXObject("htmlfile");
86 document.write("<body>");
90 bod = createPopup().document.body;
92 var range = bod.createTextRange();
94 bod.style.color = color;
95 var value = range.queryCommandValue("ForeColor");
96 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
97 return "#" + ("000000" + value[toString](16)).slice(-6);
103 var i = doc.createElement("i");
104 i.className = "Rapha\xebl Colour Picker";
105 i.style.cssText = "display:none";
106 doc.body[appendChild](i);
107 toHex = cacher(function (color) {
108 i.style.color = color;
109 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
114 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
115 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
123 if (brightness == 0) {
124 return {r: 0, g: 0, b: 0, hex: "#000"};
126 if (hue > 1 || saturation > 1 || brightness > 1) {
133 p = brightness * (1 - saturation),
134 q = brightness * (1 - (saturation * f)),
135 t = brightness * (1 - (saturation * (1 - f)));
136 red = [brightness, q, p, p, t, brightness, brightness][i];
137 green = [t, brightness, brightness, q, p, p, t][i];
138 blue = [p, p, t, brightness, brightness, q, p][i];
142 var rgb = {r: red, g: green, b: blue},
143 r = (~~red)[toString](16),
144 g = (~~green)[toString](16),
145 b = (~~blue)[toString](16);
149 rgb.hex = "#" + r + g + b;
152 R.rgb2hsb = cacher(function (red, green, blue) {
153 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
158 if (R.is(red, "string")) {
159 var clr = R.getRGB(red);
164 if (red > 1 || green > 1 || blue > 1) {
169 var max = mmax(red, green, blue),
170 min = mmin(red, green, blue),
175 return {h: 0, s: 0, b: max};
177 var delta = (max - min);
178 saturation = delta / max;
180 hue = (green - blue) / delta;
181 } else if (green == max) {
182 hue = 2 + ((blue - red) / delta);
184 hue = 4 + ((red - green) / delta);
190 return {h: hue, s: saturation, b: brightness};
192 var p2s = /,?([achlmqrstvxz]),?/gi;
193 R._path2string = function () {
194 return this.join(",")[rp](p2s, "$1");
196 function cacher(f, scope, postprocessor) {
198 var arg = Array[proto].slice.call(arguments, 0),
199 args = arg[join]("\u25ba"),
200 cache = newf.cache = newf.cache || {},
201 count = newf.count = newf.count || [];
202 if (cache[has](args)) {
203 return postprocessor ? postprocessor(cache[args]) : cache[args];
205 count[length] >= 1e3 && delete cache[count.shift()];
207 cache[args] = f[apply](scope, arg);
208 return postprocessor ? postprocessor(cache[args]) : cache[args];
213 R.getRGB = cacher(function (colour) {
214 if (!colour || !!((colour + E).indexOf("-") + 1)) {
215 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
218 if (colour == "none") {
219 return {r: -1, g: -1, b: -1, hex: "none"};
221 !({hs: 1, rg: 1})[has](colour.substring(0, 2)) && (colour = toHex(colour));
226 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);
229 blue = toInt(rgb[2].substring(5), 16);
230 green = toInt(rgb[2].substring(3, 5), 16);
231 red = toInt(rgb[2].substring(1, 3), 16);
234 blue = toInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
235 green = toInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
236 red = toInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
239 rgb = rgb[4][split](/\s*,\s*/);
240 red = toFloat(rgb[0]);
241 green = toFloat(rgb[1]);
242 blue = toFloat(rgb[2]);
245 rgb = rgb[5][split](/\s*,\s*/);
246 red = toFloat(rgb[0]) * 2.55;
247 green = toFloat(rgb[1]) * 2.55;
248 blue = toFloat(rgb[2]) * 2.55;
251 rgb = rgb[6][split](/\s*,\s*/);
252 red = toFloat(rgb[0]);
253 green = toFloat(rgb[1]);
254 blue = toFloat(rgb[2]);
255 return R.hsb2rgb(red, green, blue);
258 rgb = rgb[7][split](/\s*,\s*/);
259 red = toFloat(rgb[0]) * 2.55;
260 green = toFloat(rgb[1]) * 2.55;
261 blue = toFloat(rgb[2]) * 2.55;
262 return R.hsb2rgb(red, green, blue);
264 rgb = {r: red, g: green, b: blue};
265 var r = (~~red)[toString](16),
266 g = (~~green)[toString](16),
267 b = (~~blue)[toString](16);
271 rgb.hex = "#" + r + g + b;
274 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
276 R.getColor = function (value) {
277 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
278 rgb = this.hsb2rgb(start.h, start.s, start.b);
283 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
287 R.getColor.reset = function () {
291 R.parsePathString = cacher(function (pathString) {
295 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
297 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
298 data = pathClone(pathString);
301 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
303 name = lowerCase.call(b);
304 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
305 b && params[push](+b);
307 while (params[length] >= paramCounts[name]) {
308 data[push]([b][concat](params.splice(0, paramCounts[name])));
309 if (!paramCounts[name]) {
315 data[toString] = R._path2string;
318 var pathDimensions = cacher(function (path) {
320 return {x: 0, y: 0, width: 0, height: 0};
322 path = path2curve(path);
328 for (var i = 0, ii = path[length]; i < ii; i++) {
336 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
337 X = X[concat](dim.min.x, dim.max.x);
338 Y = Y[concat](dim.min.y, dim.max.y);
343 var xmin = mmin[apply](0, X),
344 ymin = mmin[apply](0, Y);
348 width: mmax[apply](0, X) - xmin,
349 height: mmax[apply](0, Y) - ymin
352 pathClone = function (pathArray) {
354 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
355 pathArray = R.parsePathString(pathArray);
357 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
359 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
360 res[i][j] = pathArray[i][j];
363 res[toString] = R._path2string;
366 pathToRelative = cacher(function (pathArray) {
367 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
368 pathArray = R.parsePathString(pathArray);
376 if (pathArray[0][0] == "M") {
382 res[push](["M", x, y]);
384 for (var i = start, ii = pathArray[length]; i < ii; i++) {
387 if (pa[0] != lowerCase.call(pa[0])) {
388 r[0] = lowerCase.call(pa[0]);
396 r[6] = +(pa[6] - x).toFixed(3);
397 r[7] = +(pa[7] - y).toFixed(3);
400 r[1] = +(pa[1] - y).toFixed(3);
406 for (var j = 1, jj = pa[length]; j < jj; j++) {
407 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
416 for (var k = 0, kk = pa[length]; k < kk; k++) {
420 var len = res[i][length];
427 x += +res[i][len - 1];
430 y += +res[i][len - 1];
433 x += +res[i][len - 2];
434 y += +res[i][len - 1];
437 res[toString] = R._path2string;
440 pathToAbsolute = cacher(function (pathArray) {
441 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
442 pathArray = R.parsePathString(pathArray);
450 if (pathArray[0][0] == "M") {
451 x = +pathArray[0][1];
452 y = +pathArray[0][2];
456 res[0] = ["M", x, y];
458 for (var i = start, ii = pathArray[length]; i < ii; i++) {
461 if (pa[0] != upperCase.call(pa[0])) {
462 r[0] = upperCase.call(pa[0]);
483 for (var j = 1, jj = pa[length]; j < jj; j++) {
484 r[j] = +pa[j] + ((j % 2) ? x : y);
488 for (var k = 0, kk = pa[length]; k < kk; k++) {
504 x = res[i][res[i][length] - 2];
505 y = res[i][res[i][length] - 1];
508 res[toString] = R._path2string;
511 l2c = function (x1, y1, x2, y2) {
512 return [x1, y1, x2, y2, x2, y2];
514 q2c = function (x1, y1, ax, ay, x2, y2) {
526 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
527 // for more information of where this math came from visit:
528 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
530 _120 = PI * 120 / 180,
531 rad = PI / 180 * (+angle || 0),
534 rotate = cacher(function (x, y, rad) {
535 var X = x * math.cos(rad) - y * math.sin(rad),
536 Y = x * math.sin(rad) + y * math.cos(rad);
540 xy = rotate(x1, y1, -rad);
543 xy = rotate(x2, y2, -rad);
546 var cos = math.cos(PI / 180 * angle),
547 sin = math.sin(PI / 180 * angle),
550 rx = mmax(rx, math.abs(x));
551 ry = mmax(ry, math.abs(y));
554 k = (large_arc_flag == sweep_flag ? -1 : 1) *
555 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
556 cx = k * rx * y / ry + (x1 + x2) / 2,
557 cy = k * -ry * x / rx + (y1 + y2) / 2,
558 f1 = math.asin((y1 - cy) / ry),
559 f2 = math.asin((y2 - cy) / ry);
561 f1 = x1 < cx ? PI - f1 : f1;
562 f2 = x2 < cx ? PI - f2 : f2;
563 f1 < 0 && (f1 = PI * 2 + f1);
564 f2 < 0 && (f2 = PI * 2 + f2);
565 if (sweep_flag && f1 > f2) {
568 if (!sweep_flag && f2 > f1) {
578 if (math.abs(df) > _120) {
582 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
583 x2 = cx + rx * math.cos(f2);
584 y2 = cy + ry * math.sin(f2);
585 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
588 var c1 = math.cos(f1),
592 t = math.tan(df / 4),
596 m2 = [x1 + hx * s1, y1 - hy * c1],
597 m3 = [x2 + hx * s2, y2 - hy * c2],
599 m2[0] = 2 * m1[0] - m2[0];
600 m2[1] = 2 * m1[1] - m2[1];
602 return [m2, m3, m4][concat](res);
604 res = [m2, m3, m4][concat](res)[join](",")[split](",");
606 for (var i = 0, ii = res[length]; i < ii; i++) {
607 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
612 findDotAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
615 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
616 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
619 findDotsAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
621 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
622 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
623 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
624 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
625 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
626 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
627 ax = (1 - t) * p1x + t * c1x,
628 ay = (1 - t) * p1y + t * c1y,
629 cx = (1 - t) * c2x + t * p2x,
630 cy = (1 - t) * c2y + t * p2y;
631 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}};
633 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
634 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
635 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
637 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
638 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
642 math.abs(t1) > 1e12 && (t1 = .5);
643 math.abs(t2) > 1e12 && (t2 = .5);
644 if (t1 > 0 && t1 < 1) {
645 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
649 if (t2 > 0 && t2 < 1) {
650 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
654 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
655 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
657 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
658 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
659 math.abs(t1) > 1e12 && (t1 = .5);
660 math.abs(t2) > 1e12 && (t2 = .5);
661 if (t1 > 0 && t1 < 1) {
662 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
666 if (t2 > 0 && t2 < 1) {
667 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
672 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
673 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
676 path2curve = cacher(function (path, path2) {
677 var p = pathToAbsolute(path),
678 p2 = path2 && pathToAbsolute(path2),
679 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
680 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
681 processPath = function (path, d) {
684 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
686 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
693 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
696 nx = d.x + (d.x - (d.bx || d.x));
697 ny = d.y + (d.y - (d.by || d.y));
698 path = ["C", nx, ny][concat](path.slice(1));
701 d.qx = d.x + (d.x - (d.qx || d.x));
702 d.qy = d.y + (d.y - (d.qy || d.y));
703 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
708 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
711 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
714 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
717 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
720 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
725 fixArc = function (pp, i) {
726 if (pp[i][length] > 7) {
730 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
733 ii = mmax(p[length], p2 && p2[length] || 0);
736 fixM = function (path1, path2, a1, a2, i) {
737 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
738 path2.splice(i, 0, ["M", a2.x, a2.y]);
743 ii = mmax(p[length], p2 && p2[length] || 0);
746 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
747 p[i] = processPath(p[i], attrs);
749 p2 && (p2[i] = processPath(p2[i], attrs2));
751 fixM(p, p2, attrs, attrs2, i);
752 fixM(p2, p, attrs2, attrs, i);
755 seglen = seg[length],
756 seg2len = p2 && seg2[length];
757 attrs.x = seg[seglen - 2];
758 attrs.y = seg[seglen - 1];
759 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
760 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
761 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
762 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
763 attrs2.x = p2 && seg2[seg2len - 2];
764 attrs2.y = p2 && seg2[seg2len - 1];
766 return p2 ? [p, p2] : p;
768 parseDots = cacher(function (gradient) {
770 for (var i = 0, ii = gradient[length]; i < ii; i++) {
772 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
773 dot.color = R.getRGB(par[1]);
774 if (dot.color.error) {
777 dot.color = dot.color.hex;
778 par[2] && (dot.offset = par[2] + "%");
781 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
782 if (!dots[i].offset) {
783 var start = toFloat(dots[i - 1].offset || 0),
785 for (var j = i + 1; j < ii; j++) {
786 if (dots[j].offset) {
787 end = dots[j].offset;
796 var d = (end - start) / (j - i + 1);
799 dots[i].offset = start + "%";
805 getContainer = function () {
811 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
812 if (R.is(arguments[0], "string")) {
813 container = doc.getElementById(arguments[0]);
815 container = arguments[0];
817 if (container.tagName) {
818 if (arguments[1] == null) {
820 container: container,
821 width: container.style.pixelWidth || container.offsetWidth,
822 height: container.style.pixelHeight || container.offsetHeight
825 return {container: container, width: arguments[1], height: arguments[2]};
828 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
829 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
832 plugins = function (con, add) {
834 for (var prop in add) if (add[has](prop) && !(prop in con)) {
835 switch (typeof add[prop]) {
838 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
842 con[prop] = con[prop] || {};
843 plugins.call(this, con[prop], add[prop]);
846 con[prop] = add[prop];
851 tear = function (el, paper) {
852 el == paper.top && (paper.top = el.prev);
853 el == paper.bottom && (paper.bottom = el.next);
854 el.next && (el.next.prev = el.prev);
855 el.prev && (el.prev.next = el.next);
857 tofront = function (el, paper) {
858 if (paper.top === el) {
867 toback = function (el, paper) {
868 if (paper.bottom === el) {
872 el.next = paper.bottom;
874 paper.bottom.prev = el;
877 insertafter = function (el, el2, paper) {
879 el2 == paper.top && (paper.top = el);
880 el2.next && (el2.next.prev = el);
885 insertbefore = function (el, el2, paper) {
887 el2 == paper.bottom && (paper.bottom = el);
888 el2.prev && (el2.prev.next = el);
896 paper.svgns = "http://www.w3.org/2000/svg";
897 paper.xlink = "http://www.w3.org/1999/xlink";
898 var round = function (num) {
899 return +num + (~~num === num) * .5;
901 roundPath = function (path) {
902 for (var i = 0, ii = path[length]; i < ii; i++) {
903 if (lowerCase.call(path[i][0]) != "a") {
904 for (var j = 1, jj = path[i][length]; j < jj; j++) {
905 path[i][j] = round(path[i][j]);
908 path[i][6] = round(path[i][6]);
909 path[i][7] = round(path[i][7]);
914 $ = function (el, attr) {
916 for (var key in attr) if (attr[has](key)) {
917 el[setAttribute](key, attr[key]);
920 return doc.createElementNS(paper.svgns, el);
923 R[toString] = function () {
924 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
926 var thePath = function (pathString, SVG) {
928 SVG.canvas && SVG.canvas[appendChild](el);
929 var p = new Element(el, SVG);
931 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
934 var addGradientFill = function (o, gradient, SVG) {
938 gradient = (gradient + E)[rp](/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, _fx, _fy) {
943 if (pow(fx - .5, 2) + pow(fy - .5, 2) > .25) {
944 fy = math.sqrt(.25 - pow(fx - .5, 2)) + .5;
949 gradient = gradient[split](/\s*\-\s*/);
950 if (type == "linear") {
951 var angle = gradient.shift();
952 angle = -toFloat(angle);
956 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
957 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
961 vector[0] = -vector[2];
965 vector[1] = -vector[3];
969 var dots = parseDots(gradient);
973 var el = $(type + "Gradient");
974 el.id = "r" + (R._id++)[toString](36);
975 type == "radial" ? $(el, {fx: fx, fy: fy}) : $(el, {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
976 SVG.defs[appendChild](el);
977 for (var i = 0, ii = dots[length]; i < ii; i++) {
978 var stop = $("stop");
980 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
981 "stop-color": dots[i].color || "#fff"
983 el[appendChild](stop);
986 fill: "url(#" + el.id + ")",
995 var updatePosition = function (o) {
996 var bbox = o.getBBox();
997 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
999 var setFillAndStroke = function (o, params) {
1006 "-..": [3, 1, 1, 1, 1, 1],
1010 "- .": [4, 3, 1, 3],
1011 "--.": [8, 3, 1, 3],
1012 "--..": [8, 3, 1, 3, 1, 3]
1017 addDashes = function (o, value) {
1018 value = dasharray[lowerCase.call(value)];
1020 var width = o.attrs["stroke-width"] || "1",
1021 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1023 var i = value[length];
1025 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1027 $(node, {"stroke-dasharray": dashes[join](",")});
1030 params[has]("rotation") && (rot = params.rotation);
1031 var rotxy = (rot + E)[split](separator);
1032 if (!(rotxy.length - 1)) {
1035 rotxy[1] = +rotxy[1];
1036 rotxy[2] = +rotxy[2];
1038 toFloat(rot) && o.rotate(0, true);
1039 for (var att in params) if (params[has](att)) {
1040 if (!availableAttrs[has](att)) {
1043 var value = params[att];
1047 o.rotate(value, true);
1053 var pn = node.parentNode;
1054 if (lowerCase.call(pn.tagName) != "a") {
1056 pn.insertBefore(hl, node);
1057 hl[appendChild](node);
1060 pn.setAttributeNS(o.paper.xlink, att, value);
1063 node.style.cursor = value;
1066 var rect = (value + E)[split](separator);
1067 if (rect[length] == 4) {
1068 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1069 var el = $("clipPath"),
1071 el.id = "r" + (R._id++)[toString](36);
1078 el[appendChild](rc);
1079 o.paper.defs[appendChild](el);
1080 $(node, {"clip-path": "url(#" + el.id + ")"});
1084 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1085 clip && clip.parentNode.removeChild(clip);
1086 $(node, {"clip-path": E});
1091 if (value && o.type == "path") {
1092 attrs.path = roundPath(pathToAbsolute(value));
1093 $(node, {d: attrs.path});
1097 node[setAttribute](att, value);
1106 value = -attrs.x - (attrs.width || 0);
1109 if (att == "rx" && o.type == "rect") {
1113 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1114 node[setAttribute](att, round(value));
1115 o.pattern && updatePosition(o);
1118 node[setAttribute](att, value);
1127 value = -attrs.y - (attrs.height || 0);
1130 if (att == "ry" && o.type == "rect") {
1134 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1135 node[setAttribute](att, round(value));
1136 o.pattern && updatePosition(o);
1139 if (o.type == "rect") {
1140 $(node, {rx: value, ry: value});
1142 node[setAttribute](att, value);
1146 if (o.type == "image") {
1147 node.setAttributeNS(o.paper.xlink, "href", value);
1150 case "stroke-width":
1151 node.style.strokeWidth = value;
1152 // Need following line for Firefox
1153 node[setAttribute](att, value);
1154 if (attrs["stroke-dasharray"]) {
1155 addDashes(o, attrs["stroke-dasharray"]);
1158 case "stroke-dasharray":
1159 addDashes(o, value);
1162 var xy = (value + E)[split](separator);
1163 xy[0] = +xy[0] || 0;
1164 xy[1] = +xy[1] || 0;
1169 translate.call(o, xy[0], xy[1]);
1172 var xy = (value + E)[split](separator);
1173 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1176 var isURL = (value + E).match(ISURL);
1178 var el = $("pattern"),
1180 el.id = "r" + (R._id++)[toString](36);
1181 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse"});
1182 $(ig, {x: 0, y: 0});
1183 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1184 el[appendChild](ig);
1186 var img = doc.createElement("img");
1187 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1188 img.onload = function () {
1189 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1190 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1191 doc.body.removeChild(this);
1194 doc.body[appendChild](img);
1196 o.paper.defs[appendChild](el);
1197 node.style.fill = "url(#" + el.id + ")";
1198 $(node, {fill: "url(#" + el.id + ")"});
1200 o.pattern && updatePosition(o);
1203 if (!R.getRGB(value).error) {
1204 delete params.gradient;
1205 delete attrs.gradient;
1206 !R.is(attrs.opacity, "undefined") &&
1207 R.is(params.opacity, "undefined") &&
1208 $(node, {opacity: attrs.opacity});
1209 !R.is(attrs["fill-opacity"], "undefined") &&
1210 R.is(params["fill-opacity"], "undefined") &&
1211 $(node, {"fill-opacity": attrs["fill-opacity"]});
1212 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1213 attrs.gradient = value;
1214 attrs.fill = "none";
1218 node[setAttribute](att, R.getRGB(value).hex);
1221 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1224 case "fill-opacity":
1225 if (attrs.gradient) {
1226 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1228 var stops = gradient.getElementsByTagName("stop");
1229 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1234 att == "font-size" && (value = toInt(value, 10) + "px");
1235 var cssrule = att[rp](/(\-.)/g, function (w) {
1236 return upperCase.call(w.substring(1));
1238 node.style[cssrule] = value;
1239 // Need following line for Firefox
1240 node[setAttribute](att, value);
1245 tuneText(o, params);
1247 o.rotate(rotxy.join(S));
1249 toFloat(rot) && o.rotate(rot, true);
1253 var tuneText = function (el, params) {
1254 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1259 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1261 if (params[has]("text")) {
1262 a.text = params.text;
1263 while (node.firstChild) {
1264 node.removeChild(node.firstChild);
1266 var texts = (params.text + E)[split]("\n");
1267 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1268 var tspan = $("tspan");
1269 i && $(tspan, {dy: fontSize * leading, x: a.x});
1270 tspan[appendChild](doc.createTextNode(texts[i]));
1271 node[appendChild](tspan);
1274 var texts = node.getElementsByTagName("tspan");
1275 for (var i = 0, ii = texts[length]; i < ii; i++) {
1276 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1280 var bb = el.getBBox(),
1281 dif = a.y - (bb.y + bb.height / 2);
1282 dif && isFinite(dif) && $(node, {y: a.y + dif});
1284 var Element = function (node, svg) {
1290 node.raphael = this;
1292 this.attrs = this.attrs || {};
1293 this.transformations = []; // rotate, translate, scale
1297 rt: {deg: 0, cx: 0, cy: 0},
1301 !svg.bottom && (svg.bottom = this);
1302 this.prev = svg.top;
1303 svg.top && (svg.top.next = this);
1307 Element[proto].rotate = function (deg, cx, cy) {
1313 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1315 return this._.rt.deg;
1317 var bbox = this.getBBox();
1318 deg = (deg + E)[split](separator);
1319 if (deg[length] - 1) {
1320 cx = toFloat(deg[1]);
1321 cy = toFloat(deg[2]);
1323 deg = toFloat(deg[0]);
1325 this._.rt.deg = deg;
1327 this._.rt.deg += deg;
1329 (cy == null) && (cx = null);
1332 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1333 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1334 if (this._.rt.deg) {
1335 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1336 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1338 this.transformations[0] = E;
1339 this.clip && $(this.clip, {transform: E});
1341 $(this.node, {transform: this.transformations[join](S)});
1344 Element[proto].hide = function () {
1345 !this.removed && (this.node.style.display = "none");
1348 Element[proto].show = function () {
1349 !this.removed && (this.node.style.display = "");
1352 Element[proto].remove = function () {
1356 tear(this, this.paper);
1357 this.node.parentNode.removeChild(this.node);
1358 for (var i in this) {
1361 this.removed = true;
1363 Element[proto].getBBox = function () {
1367 if (this.type == "path") {
1368 return pathDimensions(this.attrs.path);
1370 if (this.node.style.display == "none") {
1376 bbox = this.node.getBBox();
1378 // Firefox 3.0.x plays badly here
1382 if (this.type == "text") {
1383 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1384 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1385 var bb = this.node.getExtentOfChar(i);
1386 (bb.y < bbox.y) && (bbox.y = bb.y);
1387 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1388 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1391 hide && this.hide();
1394 Element[proto].attr = function () {
1398 if (arguments[length] == 0) {
1400 for (var i in this.attrs) if (this.attrs[has](i)) {
1401 res[i] = this.attrs[i];
1405 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1406 if (arguments[0] == "translation") {
1407 return translate.call(this);
1409 if (arguments[0] == "rotation") {
1410 return this.rotate();
1412 if (arguments[0] == "scale") {
1413 return this.scale();
1415 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1416 return this.attrs.gradient;
1418 return this.attrs[arguments[0]];
1420 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1422 for (var j in arguments[0]) if (arguments[0][has](j)) {
1423 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1427 if (arguments[length] == 2) {
1429 params[arguments[0]] = arguments[1];
1430 setFillAndStroke(this, params);
1431 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1432 setFillAndStroke(this, arguments[0]);
1436 Element[proto].toFront = function () {
1440 this.node.parentNode[appendChild](this.node);
1441 var svg = this.paper;
1442 svg.top != this && tofront(this, svg);
1445 Element[proto].toBack = function () {
1449 if (this.node.parentNode.firstChild != this.node) {
1450 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1451 toback(this, this.paper);
1452 var svg = this.paper;
1456 Element[proto].insertAfter = function (element) {
1460 var svg = this.paper,
1461 node = element.node;
1462 if (node.nextSibling) {
1463 node.parentNode.insertBefore(this.node, node.nextSibling);
1465 node.parentNode[appendChild](this.node);
1467 insertafter(this, element, this.paper);
1470 Element[proto].insertBefore = function (element) {
1474 var node = element.node;
1475 node.parentNode.insertBefore(this.node, node);
1476 insertbefore(this, element, this.paper);
1480 var theCircle = function (svg, x, y, r) {
1483 var el = $("circle");
1484 svg.canvas && svg.canvas[appendChild](el);
1485 var res = new Element(el, svg);
1486 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1487 res.type = "circle";
1491 var theRect = function (svg, x, y, w, h, r) {
1495 svg.canvas && svg.canvas[appendChild](el);
1496 var res = new Element(el, svg);
1497 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1502 var theEllipse = function (svg, x, y, rx, ry) {
1505 var el = $("ellipse");
1506 svg.canvas && svg.canvas[appendChild](el);
1507 var res = new Element(el, svg);
1508 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1509 res.type = "ellipse";
1513 var theImage = function (svg, src, x, y, w, h) {
1514 var el = $("image");
1515 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1516 el.setAttributeNS(svg.xlink, "href", src);
1517 svg.canvas && svg.canvas[appendChild](el);
1518 var res = new Element(el, svg);
1519 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1523 var theText = function (svg, x, y, text) {
1525 $(el, {x: x, y: y, "text-anchor": "middle"});
1526 svg.canvas && svg.canvas[appendChild](el);
1527 var res = new Element(el, svg);
1528 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1530 setFillAndStroke(res, res.attrs);
1533 var setSize = function (width, height) {
1534 this.width = width || this.width;
1535 this.height = height || this.height;
1536 this.canvas[setAttribute]("width", this.width);
1537 this.canvas[setAttribute]("height", this.height);
1540 var create = function () {
1541 var con = getContainer[apply](null, arguments),
1542 container = con && con.container,
1546 height = con.height;
1548 throw new Error("SVG container not found.");
1550 paper.canvas = $("svg");
1551 var cnvs = paper.canvas;
1552 paper.width = width || 512;
1553 paper.height = height || 342;
1555 xmlns: "http://www.w3.org/2000/svg",
1558 height: paper.height
1560 if (container == 1) {
1561 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1562 doc.body[appendChild](cnvs);
1564 if (container.firstChild) {
1565 container.insertBefore(cnvs, container.firstChild);
1567 container[appendChild](cnvs);
1570 container = { canvas: cnvs };
1571 for (var prop in paper) if (paper[has](prop)) {
1572 container[prop] = paper[prop];
1574 container.bottom = container.top = null;
1575 plugins.call(container, container, R.fn);
1577 container.raphael = R;
1580 paper.clear = function () {
1581 var c = this.canvas;
1582 while (c.firstChild) {
1583 c.removeChild(c.firstChild);
1585 this.bottom = this.top = null;
1586 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1587 c[appendChild](this.desc);
1588 c[appendChild](this.defs = $("defs"));
1590 paper.remove = function () {
1591 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1592 for (var i in this) {
1600 var path2vml = function (path) {
1601 var total = /[ahqtv]/ig,
1602 command = pathToAbsolute;
1603 (path + E).match(total) && (command = path2curve);
1605 if (command == pathToAbsolute && !(path + E).match(total)) {
1606 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1607 bites = /([clmz]),?([^clmz]*)/gi,
1608 val = /-?[^,\s-]+/g;
1609 var res = (path + E)[rp](bites, function (all, command, args) {
1611 args[rp](val, function (value) {
1612 vals[push](round(value));
1614 return map[command] + vals;
1618 var pa = command(path), p, res = [], r;
1619 for (var i = 0, ii = pa[length]; i < ii; i++) {
1621 r = lowerCase.call(pa[i][0]);
1622 r == "z" && (r = "x");
1623 for (var j = 1, jj = p[length]; j < jj; j++) {
1624 r += round(p[j]) + (j != jj - 1 ? "," : E);
1628 return res[join](S);
1631 R[toString] = function () {
1632 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1634 var thePath = function (pathString, VML) {
1635 var g = createNode("group");
1636 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1637 g.coordsize = VML.coordsize;
1638 g.coordorigin = VML.coordorigin;
1639 var el = createNode("shape"), ol = el.style;
1640 ol.width = VML.width + "px";
1641 ol.height = VML.height + "px";
1642 el.coordsize = this.coordsize;
1643 el.coordorigin = this.coordorigin;
1645 var p = new Element(el, g, VML);
1646 p.isAbsolute = true;
1650 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1651 VML.canvas[appendChild](g);
1654 var setFillAndStroke = function (o, params) {
1655 o.attrs = o.attrs || {};
1661 for (var par in params) if (params[has](par)) {
1662 a[par] = params[par];
1664 params.href && (node.href = params.href);
1665 params.title && (node.title = params.title);
1666 params.target && (node.target = params.target);
1667 params.cursor && (s.cursor = params.cursor);
1668 if (params.path && o.type == "path") {
1669 a.path = params.path;
1670 node.path = path2vml(a.path);
1672 if (params.rotation != null) {
1673 o.rotate(params.rotation, true);
1675 if (params.translation) {
1676 xy = (params.translation + E)[split](separator);
1677 translate.call(o, xy[0], xy[1]);
1678 if (o._.rt.cx != null) {
1679 o._.rt.cx +=+ xy[0];
1680 o._.rt.cy +=+ xy[1];
1681 o.setBox(o.attrs, xy[0], xy[1]);
1685 xy = (params.scale + E)[split](separator);
1686 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1688 if ("clip-rect" in params) {
1689 var rect = (params["clip-rect"] + E)[split](separator);
1690 if (rect[length] == 4) {
1691 rect[2] = +rect[2] + (+rect[0]);
1692 rect[3] = +rect[3] + (+rect[1]);
1693 var div = node.clipRect || doc.createElement("div"),
1695 group = node.parentNode;
1696 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1697 if (!node.clipRect) {
1698 dstyle.position = "absolute";
1701 dstyle.width = o.paper.width + "px";
1702 dstyle.height = o.paper.height + "px";
1703 group.parentNode.insertBefore(div, group);
1704 div[appendChild](group);
1705 node.clipRect = div;
1708 if (!params["clip-rect"]) {
1709 node.clipRect && (node.clipRect.style.clip = E);
1712 if (o.type == "image" && params.src) {
1713 node.src = params.src;
1715 if (o.type == "image" && params.opacity) {
1716 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1717 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1719 params.font && (s.font = params.font);
1720 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1721 params["font-size"] && (s.fontSize = params["font-size"]);
1722 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1723 params["font-style"] && (s.fontStyle = params["font-style"]);
1724 if (params.opacity != null ||
1725 params["stroke-width"] != null ||
1726 params.fill != null ||
1727 params.stroke != null ||
1728 params["stroke-width"] != null ||
1729 params["stroke-opacity"] != null ||
1730 params["fill-opacity"] != null ||
1731 params["stroke-dasharray"] != null ||
1732 params["stroke-miterlimit"] != null ||
1733 params["stroke-linejoin"] != null ||
1734 params["stroke-linecap"] != null) {
1735 node = o.shape || node;
1736 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1738 !fill && (newfill = fill = createNode("fill"));
1739 if ("fill-opacity" in params || "opacity" in params) {
1740 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1741 opacity < 0 && (opacity = 0);
1742 opacity > 1 && (opacity = 1);
1743 fill.opacity = opacity;
1745 params.fill && (fill.on = true);
1746 if (fill.on == null || params.fill == "none") {
1749 if (fill.on && params.fill) {
1750 var isURL = params.fill.match(ISURL);
1752 fill.src = isURL[1];
1755 fill.color = R.getRGB(params.fill).hex;
1757 fill.type = "solid";
1758 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1760 a.gradient = params.fill;
1764 newfill && node[appendChild](fill);
1765 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1767 !stroke && (newstroke = stroke = createNode("stroke"));
1768 if ((params.stroke && params.stroke != "none") ||
1769 params["stroke-width"] ||
1770 params["stroke-opacity"] != null ||
1771 params["stroke-dasharray"] ||
1772 params["stroke-miterlimit"] ||
1773 params["stroke-linejoin"] ||
1774 params["stroke-linecap"]) {
1777 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1778 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1779 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1780 width = (toFloat(params["stroke-width"]) || 1) * .75;
1781 opacity < 0 && (opacity = 0);
1782 opacity > 1 && (opacity = 1);
1783 params["stroke-width"] == null && (width = a["stroke-width"]);
1784 params["stroke-width"] && (stroke.weight = width);
1785 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1786 stroke.opacity = opacity;
1788 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1789 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1790 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1791 if (params["stroke-dasharray"]) {
1795 "-.": "shortdashdot",
1796 "-..": "shortdashdotdot",
1801 "--.": "longdashdot",
1802 "--..": "longdashdotdot"
1804 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1806 newstroke && node[appendChild](stroke);
1808 if (res.type == "text") {
1809 var s = res.paper.span.style;
1810 a.font && (s.font = a.font);
1811 a["font-family"] && (s.fontFamily = a["font-family"]);
1812 a["font-size"] && (s.fontSize = a["font-size"]);
1813 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1814 a["font-style"] && (s.fontStyle = a["font-style"]);
1815 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1816 res.W = a.w = res.paper.span.offsetWidth;
1817 res.H = a.h = res.paper.span.offsetHeight;
1819 res.Y = a.y + round(res.H / 2);
1821 // text-anchor emulationm
1822 switch (a["text-anchor"]) {
1824 res.node.style["v-text-align"] = "left";
1825 res.bbx = round(res.W / 2);
1828 res.node.style["v-text-align"] = "right";
1829 res.bbx = -round(res.W / 2);
1832 res.node.style["v-text-align"] = "center";
1837 var addGradientFill = function (o, gradient) {
1838 o.attrs = o.attrs || {};
1839 var attrs = o.attrs,
1840 fill = o.node.getElementsByTagName("fill"),
1843 o.attrs.gradient = gradient;
1844 gradient = (gradient + E)[rp](/^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/, function (all, fx, fy) {
1849 if (pow(fx - .5, 2) + pow(fy - .5, 2) > .25) {
1850 fy = math.sqrt(.25 - pow(fx - .5, 2)) + .5;
1856 gradient = gradient[split](/\s*\-\s*/);
1857 if (type == "linear") {
1858 var angle = gradient.shift();
1859 angle = -toFloat(angle);
1864 var dots = parseDots(gradient);
1868 o = o.shape || o.node;
1869 fill = fill[0] || createNode("fill");
1872 fill.method = "none";
1873 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1874 fill.color = dots[0].color;
1875 fill.color2 = dots[dots[length] - 1].color;
1877 for (var i = 0, ii = dots[length]; i < ii; i++) {
1878 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1880 fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color;
1881 if (type == "radial") {
1882 fill.focus = "100%";
1883 fill.focussize = fxfy;
1884 fill.focusposition = fxfy;
1886 fill.angle = (270 - angle) % 360;
1891 var Element = function (node, group, vml) {
1899 node.raphael = this;
1912 !vml.bottom && (vml.bottom = this);
1913 this.prev = vml.top;
1914 vml.top && (vml.top.next = this);
1918 Element[proto].rotate = function (deg, cx, cy) {
1924 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1926 return this._.rt.deg;
1928 deg = (deg + E)[split](separator);
1929 if (deg[length] - 1) {
1930 cx = toFloat(deg[1]);
1931 cy = toFloat(deg[2]);
1933 deg = toFloat(deg[0]);
1935 this._.rt.deg = deg;
1937 this._.rt.deg += deg;
1939 cy == null && (cx = null);
1942 this.setBox(this.attrs, cx, cy);
1943 this.Group.style.rotation = this._.rt.deg;
1944 // gradient fix for rotation. TODO
1945 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1946 // fill = fill[0] || {};
1947 // var b = ((360 - this._.rt.deg) - 270) % 360;
1948 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1951 Element[proto].setBox = function (params, cx, cy) {
1955 var gs = this.Group.style,
1956 os = (this.shape && this.shape.style) || this.node.style;
1957 params = params || {};
1958 for (var i in params) if (params[has](i)) {
1959 this.attrs[i] = params[i];
1961 cx = cx || this._.rt.cx;
1962 cy = cy || this._.rt.cy;
1963 var attr = this.attrs,
1968 switch (this.type) {
1970 x = attr.cx - attr.r;
1971 y = attr.cy - attr.r;
1975 x = attr.cx - attr.rx;
1976 y = attr.cy - attr.ry;
1984 w = attr.width || 0;
1985 h = attr.height || 0;
1988 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
1989 x = attr.x - round(this.W / 2);
1990 y = attr.y - this.H / 2;
1995 if (!this.attrs.path) {
1998 w = this.paper.width;
1999 h = this.paper.height;
2001 var dim = pathDimensions(this.attrs.path);
2011 w = this.paper.width;
2012 h = this.paper.height;
2015 cx = (cx == null) ? x + w / 2 : cx;
2016 cy = (cy == null) ? y + h / 2 : cy;
2017 var left = cx - this.paper.width / 2,
2018 top = cy - this.paper.height / 2;
2019 if (this.type == "path" || this.type == "text") {
2020 (gs.left != left + "px") && (gs.left = left + "px");
2021 (gs.top != top + "px") && (gs.top = top + "px");
2022 this.X = this.type == "text" ? x : -left;
2023 this.Y = this.type == "text" ? y : -top;
2026 (os.left != -left + "px") && (os.left = -left + "px");
2027 (os.top != -top + "px") && (os.top = -top + "px");
2029 (gs.left != left + "px") && (gs.left = left + "px");
2030 (gs.top != top + "px") && (gs.top = top + "px");
2035 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2036 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2037 (os.left != x - left + "px") && (os.left = x - left + "px");
2038 (os.top != y - top + "px") && (os.top = y - top + "px");
2039 (os.width != w + "px") && (os.width = w + "px");
2040 (os.height != h + "px") && (os.height = h + "px");
2041 var arcsize = (+params.r || 0) / mmin(w, h);
2042 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2043 // We should replace element with the new one
2044 var o = createNode("roundrect"),
2047 ii = this.events[length];
2048 o.arcsize = arcsize;
2050 this.Group[appendChild](o);
2051 this.Group.removeChild(this.node);
2052 this[0] = this.node = o;
2053 this.arcsize = arcsize;
2054 for (var i in attr) {
2059 if (this.events) for (; i < ii; i++) {
2060 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2065 Element[proto].hide = function () {
2066 !this.removed && (this.Group.style.display = "none");
2069 Element[proto].show = function () {
2070 !this.removed && (this.Group.style.display = "block");
2073 Element[proto].getBBox = function () {
2077 if (this.type == "path") {
2078 return pathDimensions(this.attrs.path);
2081 x: this.X + (this.bbx || 0),
2087 Element[proto].remove = function () {
2091 tear(this, this.paper);
2092 this.node.parentNode.removeChild(this.node);
2093 this.Group.parentNode.removeChild(this.Group);
2094 this.shape && this.shape.parentNode.removeChild(this.shape);
2095 for (var i in this) {
2098 this.removed = true;
2100 Element[proto].attr = function () {
2104 if (arguments[length] == 0) {
2106 for (var i in this.attrs) if (this.attrs[has](i)) {
2107 res[i] = this.attrs[i];
2111 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2112 if (arguments[0] == "translation") {
2113 return translate.call(this);
2115 if (arguments[0] == "rotation") {
2116 return this.rotate();
2118 if (arguments[0] == "scale") {
2119 return this.scale();
2121 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2122 return this.attrs.gradient;
2124 return this.attrs[arguments[0]];
2126 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2128 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2129 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2134 if (arguments[length] == 2) {
2136 params[arguments[0]] = arguments[1];
2138 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2140 if (params.text && this.type == "text") {
2141 this.node.string = params.text;
2143 setFillAndStroke(this, params);
2144 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2145 addGradientFill(this, params.gradient);
2147 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2151 Element[proto].toFront = function () {
2152 !this.removed && this.Group.parentNode[appendChild](this.Group);
2153 this.paper.top != this && tofront(this, this.paper);
2156 Element[proto].toBack = function () {
2160 if (this.Group.parentNode.firstChild != this.Group) {
2161 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2162 toback(this, this.paper);
2166 Element[proto].insertAfter = function (element) {
2170 if (element.Group.nextSibling) {
2171 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2173 element.Group.parentNode[appendChild](this.Group);
2175 insertafter(this, element, this.paper);
2178 Element[proto].insertBefore = function (element) {
2182 element.Group.parentNode.insertBefore(this.Group, element.Group);
2183 insertbefore(this, element, this.paper);
2187 var theCircle = function (vml, x, y, r) {
2188 var g = createNode("group"),
2189 o = createNode("oval"),
2191 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2192 g.coordsize = vml.coordsize;
2193 g.coordorigin = vml.coordorigin;
2195 var res = new Element(o, g, vml);
2196 res.type = "circle";
2197 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2201 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2202 vml.canvas[appendChild](g);
2205 var theRect = function (vml, x, y, w, h, r) {
2206 var g = createNode("group"),
2207 o = createNode("roundrect"),
2208 arcsize = (+r || 0) / (mmin(w, h));
2209 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2210 g.coordsize = vml.coordsize;
2211 g.coordorigin = vml.coordorigin;
2213 o.arcsize = arcsize;
2214 var res = new Element(o, g, vml);
2216 setFillAndStroke(res, {stroke: "#000"});
2217 res.arcsize = arcsize;
2218 res.setBox({x: x, y: y, width: w, height: h, r: r});
2219 vml.canvas[appendChild](g);
2222 var theEllipse = function (vml, x, y, rx, ry) {
2223 var g = createNode("group"),
2224 o = createNode("oval"),
2226 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2227 g.coordsize = vml.coordsize;
2228 g.coordorigin = vml.coordorigin;
2230 var res = new Element(o, g, vml);
2231 res.type = "ellipse";
2232 setFillAndStroke(res, {stroke: "#000"});
2237 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2238 vml.canvas[appendChild](g);
2241 var theImage = function (vml, src, x, y, w, h) {
2242 var g = createNode("group"),
2243 o = createNode("image"),
2245 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2246 g.coordsize = vml.coordsize;
2247 g.coordorigin = vml.coordorigin;
2250 var res = new Element(o, g, vml);
2252 res.attrs.src = src;
2257 res.setBox({x: x, y: y, width: w, height: h});
2258 vml.canvas[appendChild](g);
2261 var theText = function (vml, x, y, text) {
2262 var g = createNode("group"),
2263 el = createNode("shape"),
2265 path = createNode("path"),
2267 o = createNode("textpath");
2268 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2269 g.coordsize = vml.coordsize;
2270 g.coordorigin = vml.coordorigin;
2271 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2272 path.textpathok = true;
2273 ol.width = vml.width;
2274 ol.height = vml.height;
2275 o.string = text + E;
2278 el[appendChild](path);
2280 var res = new Element(o, g, vml);
2282 res.textpath = path;
2284 res.attrs.text = text;
2289 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2291 vml.canvas[appendChild](g);
2294 var setSize = function (width, height) {
2295 var cs = this.canvas.style;
2296 width == +width && (width += "px");
2297 height == +height && (height += "px");
2300 cs.clip = "rect(0 " + width + " " + height + " 0)";
2303 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2305 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2306 var createNode = function (tagName) {
2307 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2310 var createNode = function (tagName) {
2311 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2314 var create = function () {
2315 var con = getContainer[apply](null, arguments),
2316 container = con.container,
2317 height = con.height,
2323 throw new Error("VML container not found.");
2326 c = res.canvas = doc.createElement("div"),
2328 width = width || 512;
2329 height = height || 342;
2330 width == +width && (width += "px");
2331 height == +height && (height += "px");
2334 res.coordsize = "1000 1000";
2335 res.coordorigin = "0 0";
2336 res.span = doc.createElement("span");
2337 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2338 c[appendChild](res.span);
2339 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2340 if (container == 1) {
2341 doc.body[appendChild](c);
2351 container.style.width = width;
2352 container.style.height = height;
2353 if (container.firstChild) {
2354 container.insertBefore(c, container.firstChild);
2356 container[appendChild](c);
2359 for (var prop in paper) if (paper[has](prop)) {
2360 res[prop] = paper[prop];
2362 plugins.call(res, res, R.fn);
2363 res.top = res.bottom = null;
2367 paper.clear = function () {
2368 this.canvas.innerHTML = E;
2369 this.bottom = this.top = null;
2371 paper.remove = function () {
2372 this.canvas.parentNode.removeChild(this.canvas);
2373 for (var i in this) {
2380 // Safari or Chrome (WebKit) rendering bug workaround method
2381 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2382 paper.safari = function () {
2383 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2384 setTimeout(function () {rect.remove();});
2387 paper.safari = function () {};
2391 var addEvent = (function () {
2392 if (doc.addEventListener) {
2393 return function (obj, type, fn, element) {
2394 var f = function (e) {
2395 return fn.call(element, e);
2397 obj.addEventListener(type, f, false);
2398 return function () {
2399 obj.removeEventListener(type, f, false);
2403 } else if (doc.attachEvent) {
2404 return function (obj, type, fn, element) {
2405 var f = function (e) {
2406 return fn.call(element, e || win.event);
2408 obj.attachEvent("on" + type, f);
2409 var detacher = function () {
2410 obj.detachEvent("on" + type, f);
2417 for (var i = events[length]; i--;) {
2418 (function (eventName) {
2419 Element[proto][eventName] = function (fn) {
2420 if (R.is(fn, "function")) {
2421 this.events = this.events || [];
2422 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2426 Element[proto]["un" + eventName] = function (fn) {
2427 var events = this.events,
2429 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2431 events.splice(l, 1);
2432 !events.length && delete this.events;
2439 Element[proto].hover = function (f_in, f_out) {
2440 return this.mouseover(f_in).mouseout(f_out);
2442 Element[proto].unhover = function (f_in, f_out) {
2443 return this.unmouseover(f_in).unmouseout(f_out);
2445 paper.circle = function (x, y, r) {
2446 return theCircle(this, x || 0, y || 0, r || 0);
2448 paper.rect = function (x, y, w, h, r) {
2449 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2451 paper.ellipse = function (x, y, rx, ry) {
2452 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2454 paper.path = function (pathString) {
2455 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2456 return thePath(R.format[apply](R, arguments), this);
2458 paper.image = function (src, x, y, w, h) {
2459 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2461 paper.text = function (x, y, text) {
2462 return theText(this, x || 0, y || 0, text || E);
2464 paper.set = function (itemsArray) {
2465 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2466 return new Set(itemsArray);
2468 paper.setSize = setSize;
2469 function scaleToString() {
2470 return this.x + S + this.y;
2472 Element[proto].scale = function (x, y, cx, cy) {
2473 if (x == null && y == null) {
2477 toString: scaleToString
2488 var bb = this.getBBox(),
2489 rcx = bb.x + bb.width / 2,
2490 rcy = bb.y + bb.height / 2,
2493 cx = (+cx || cx == 0) ? cx : rcx;
2494 cy = (+cy || cy == 0) ? cy : rcy;
2495 var dirx = ~~(x / math.abs(x)),
2496 diry = ~~(y / math.abs(y)),
2497 s = this.node.style,
2498 ncx = cx + (rcx - cx) * kx,
2499 ncy = cy + (rcy - cy) * ky;
2500 switch (this.type) {
2503 var neww = a.width * dirx * kx,
2504 newh = a.height * diry * ky,
2505 newr = a.r * mmin(kx, ky),
2506 newx = ncx - neww / 2,
2507 newy = ncy - newh / 2;
2519 rx: a.rx * dirx * kx,
2520 ry: a.ry * diry * ky,
2521 r: a.r * mmin(dirx * kx, diry * ky),
2527 var path = pathToRelative(a.path),
2529 for (var i = 0, ii = path[length]; i < ii; i++) {
2532 P0 = upperCase.call(p[0]);
2533 if (P0 == "M" && skip) {
2539 p[path[i][length] - 2] *= kx;
2540 p[path[i][length] - 1] *= ky;
2543 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2544 } else if (P0 == "H") {
2545 for (j = 1, jj = p[length]; j < jj; j++) {
2548 } else if (P0 == "V") {
2549 for (j = 1, jj = p[length]; j < jj; j++) {
2553 for (j = 1, jj = p[length]; j < jj; j++) {
2554 p[j] *= (j % 2) ? kx : ky;
2558 var dim2 = pathDimensions(path),
2559 dx = ncx - dim2.x - dim2.width / 2,
2560 dy = ncy - dim2.y - dim2.height / 2;
2564 this.attr({path: path});
2567 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2568 if (this.transformations) {
2569 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2570 this.node[setAttribute]("transform", this.transformations[join](S));
2571 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2572 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2573 this.attr({x: dx, y: dy});
2577 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2578 ", M12=0, M21=0, M22=", diry,
2579 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2580 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2583 if (this.transformations) {
2584 this.transformations[2] = E;
2585 this.node[setAttribute]("transform", this.transformations[join](S));
2589 this.node.filterMatrix = E;
2590 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2593 a.scale = [x, y, cx, cy][join](S);
2600 // animation easing formulas
2601 R.easing_formulas = {
2602 linear: function (n) {
2609 return pow(n - 1, 3) + 1;
2611 "<>": function (n) {
2614 return pow(n, 3) / 2;
2617 return (pow(n, 3) + 2) / 2;
2619 backIn: function (n) {
2621 return n * n * ((s + 1) * n - s);
2623 backOut: function (n) {
2626 return n * n * ((s + 1) * n + s) + 1;
2628 elastic: function (n) {
2629 if (n == 0 || n == 1) {
2634 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2636 bounce: function (n) {
2645 l = s * n * n + .75;
2647 if (n < (2.5 / p)) {
2649 l = s * n * n + .9375;
2652 l = s * n * n + .984375;
2660 var animationElements = {length : 0},
2661 animation = function () {
2662 var Now = +new Date;
2663 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2664 var e = animationElements[l];
2666 delete animationElements[l];
2667 animationElements[length]--;
2670 var time = Now - e.start,
2679 callback = e.callback,
2683 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2684 for (var attr in from) if (from[has](attr)) {
2685 switch (availableAnimAttrs[attr]) {
2687 now = +from[attr] + pos * ms * diff[attr];
2691 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2692 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2693 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2698 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2699 now[i] = [from[attr][i][0]];
2700 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2701 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2703 now[i] = now[i][join](S);
2710 var x = diff[attr][0] * (time - prev),
2711 y = diff[attr][1] * (time - prev);
2717 now = +from[attr][0] + pos * ms * diff[attr][0];
2718 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2721 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);
2727 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2736 that._run && that._run.call(that);
2738 (t.x || t.y) && that.translate(-t.x, -t.y);
2739 to.scale && (to.scale = to.scale + E);
2741 delete animationElements[l];
2742 animationElements[length]--;
2743 that.in_animation = null;
2744 R.is(callback, "function") && callback.call(that);
2748 R.svg && paper.safari();
2749 animationElements[length] && setTimeout(animation);
2751 upto255 = function (color) {
2752 return color > 255 ? 255 : (color < 0 ? 0 : color);
2754 translate = function (x, y) {
2756 return {x: this._.tx, y: this._.ty};
2760 switch (this.type) {
2763 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2768 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2771 var path = pathToRelative(this.attrs.path);
2774 this.attr({path: path});
2779 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2780 animationElements[element.id] && (params.start = animationElements[element.id].start);
2781 return this.animate(params, ms, easing, callback);
2783 Element[proto].onAnimation = function (f) {
2784 this._run = f || null;
2787 Element[proto].animate = function (params, ms, easing, callback) {
2788 if (R.is(easing, "function") || !easing) {
2789 callback = easing || null;
2794 for (var attr in params) if (params[has](attr)) {
2795 if (availableAnimAttrs[has](attr)) {
2796 from[attr] = this.attr(attr);
2797 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2798 to[attr] = params[attr];
2799 switch (availableAnimAttrs[attr]) {
2801 diff[attr] = (to[attr] - from[attr]) / ms;
2804 from[attr] = R.getRGB(from[attr]);
2805 var toColour = R.getRGB(to[attr]);
2807 r: (toColour.r - from[attr].r) / ms,
2808 g: (toColour.g - from[attr].g) / ms,
2809 b: (toColour.b - from[attr].b) / ms
2813 var pathes = path2curve(from[attr], to[attr]);
2814 from[attr] = pathes[0];
2815 to[attr] = pathes[1];
2817 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2818 diff[attr][i] = [0];
2819 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2820 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2825 var values = (params[attr] + E)[split](separator),
2826 from2 = (from[attr] + E)[split](separator);
2829 from[attr] = [0, 0];
2830 diff[attr] = [values[0] / ms, values[1] / ms];
2833 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2834 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2837 params[attr] = values;
2838 from[attr] = (from[attr] + E)[split](separator);
2839 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2842 from[attr] = (from[attr] + E)[split](separator);
2846 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2855 this.in_animation = 1;
2856 animationElements[this.id] = {
2857 start: params.start || +new Date,
2867 ++animationElements[length] == 1 && animation();
2870 Element[proto].stop = function () {
2871 animationElements[this.id] && animationElements[length]--;
2872 delete animationElements[this.id];
2875 Element[proto].translate = function (x, y) {
2876 return this.attr({translation: x + " " + y});
2878 Element[proto][toString] = function () {
2879 return "Rapha\xebl\u2019s object";
2881 R.ae = animationElements;
2884 var Set = function (items) {
2888 for (var i = 0, ii = items[length]; i < ii; i++) {
2889 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2890 this[this.items[length]] = this.items[this.items[length]] = items[i];
2896 Set[proto][push] = function () {
2899 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2900 item = arguments[i];
2901 if (item && (item.constructor == Element || item.constructor == Set)) {
2902 len = this.items[length];
2903 this[len] = this.items[len] = item;
2909 Set[proto].pop = function () {
2910 delete this[this[length]--];
2911 return this.items.pop();
2913 for (var method in Element[proto]) if (Element[proto][has](method)) {
2914 Set[proto][method] = (function (methodname) {
2915 return function () {
2916 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2917 this.items[i][methodname][apply](this.items[i], arguments);
2923 Set[proto].attr = function (name, value) {
2924 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2925 for (var j = 0, jj = name[length]; j < jj; j++) {
2926 this.items[j].attr(name[j]);
2929 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2930 this.items[i].attr[apply](this.items[i], arguments);
2935 Set[proto].animate = function (params, ms, easing, callback) {
2936 (R.is(easing, "function") || !easing) && (callback = easing || null);
2937 var len = this.items[length],
2941 callback && (collector = function () {
2942 !--len && callback.call(set);
2944 this.items[--i].animate(params, ms, easing || collector, collector);
2946 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
2950 Set[proto].insertAfter = function (el) {
2951 var i = this.items[length];
2953 this.items[i].insertAfter(el);
2957 Set[proto].getBBox = function () {
2962 for (var i = this.items[length]; i--;) {
2963 var box = this.items[i].getBBox();
2966 w[push](box.x + box.width);
2967 h[push](box.y + box.height);
2969 x = mmin[apply](0, x);
2970 y = mmin[apply](0, y);
2974 width: mmax[apply](0, w) - x,
2975 height: mmax[apply](0, h) - y
2979 R.registerFont = function (font) {
2983 this.fonts = this.fonts || {};
2989 family = font.face["font-family"];
2990 for (var prop in font.face) if (font.face[has](prop)) {
2991 fontcopy.face[prop] = font.face[prop];
2993 if (this.fonts[family]) {
2994 this.fonts[family][push](fontcopy);
2996 this.fonts[family] = [fontcopy];
2999 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3000 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3001 var path = font.glyphs[glyph];
3002 fontcopy.glyphs[glyph] = {
3005 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3006 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3010 for (var k in path.k) if (path[has](k)) {
3011 fontcopy.glyphs[glyph].k[k] = path.k[k];
3018 paper.getFont = function (family, weight, style, stretch) {
3019 stretch = stretch || "normal";
3020 style = style || "normal";
3021 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3022 var font = R.fonts[family];
3024 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3025 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3026 if (name.test(fontName)) {
3027 font = R.fonts[fontName];
3034 for (var i = 0, ii = font[length]; i < ii; i++) {
3036 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3043 paper.print = function (x, y, string, font, size, origin) {
3044 origin = origin || "middle"; // baseline|middle
3045 var out = this.set(),
3046 letters = (string + E)[split](E),
3050 R.is(font, "string") && (font = this.getFont(font));
3052 scale = (size || 16) / font.face["units-per-em"];
3053 var bb = font.face.bbox.split(separator),
3055 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3056 for (var i = 0, ii = letters[length]; i < ii; i++) {
3057 var prev = i && font.glyphs[letters[i - 1]] || {},
3058 curr = font.glyphs[letters[i]];
3059 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3060 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3062 out.scale(scale, scale, top, height).translate(x - top, y - height);
3067 R.format = function (token) {
3068 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3070 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3071 return args[++i] == null ? E : args[i];
3075 R.ninja = function () {
3076 var r = win.Raphael, u;
3077 if (oldRaphael.was) {
3078 win.Raphael = oldRaphael.is;
3088 R.el = Element[proto];