2 * Raphael 1.3.0dev - 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 Paper = function () {},
32 appendChild = "appendChild",
38 events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
39 has = "hasOwnProperty",
43 lowerCase = String[proto].toLowerCase,
49 toString = "toString",
50 objectToString = Object[proto][toString],
55 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
56 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,
58 setAttribute = "setAttribute",
61 upperCase = String[proto].toUpperCase,
62 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},
63 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.version = "1.3.0dev";
66 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
67 if (R.type == "VML") {
68 var d = document.createElement("div");
69 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
70 if (d.childNodes[length] != 2) {
74 R.svg = !(R.vml = R.type == "VML");
75 Paper[proto] = R[proto];
79 R.is = function (o, type) {
80 type = lowerCase.call(type);
81 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
83 R.setWindow = function (newwin) {
88 var toHex = function (color) {
90 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
91 var trim = /^\s+|\s+$/g;
92 toHex = cacher(function (color) {
94 color = (color + E)[rp](trim, E);
96 var docum = new ActiveXObject("htmlfile");
97 docum.write("<body>");
101 bod = createPopup().document.body;
103 var range = bod.createTextRange();
105 bod.style.color = color;
106 var value = range.queryCommandValue("ForeColor");
107 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
108 return "#" + ("000000" + value[toString](16)).slice(-6);
114 var i = doc.createElement("i");
115 i.title = "Rapha\xebl Colour Picker";
116 i.style.display = "none";
117 doc.body[appendChild](i);
118 toHex = cacher(function (color) {
119 i.style.color = color;
120 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
125 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
126 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
134 if (brightness == 0) {
135 return {r: 0, g: 0, b: 0, hex: "#000"};
137 if (hue > 1 || saturation > 1 || brightness > 1) {
144 p = brightness * (1 - saturation),
145 q = brightness * (1 - (saturation * f)),
146 t = brightness * (1 - (saturation * (1 - f)));
147 red = [brightness, q, p, p, t, brightness, brightness][i];
148 green = [t, brightness, brightness, q, p, p, t][i];
149 blue = [p, p, t, brightness, brightness, q, p][i];
153 var rgb = {r: red, g: green, b: blue},
154 r = (~~red)[toString](16),
155 g = (~~green)[toString](16),
156 b = (~~blue)[toString](16);
160 rgb.hex = "#" + r + g + b;
163 R.rgb2hsb = cacher(function (red, green, blue) {
164 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
169 if (R.is(red, "string")) {
170 var clr = R.getRGB(red);
175 if (red > 1 || green > 1 || blue > 1) {
180 var max = mmax(red, green, blue),
181 min = mmin(red, green, blue),
186 return {h: 0, s: 0, b: max};
188 var delta = (max - min);
189 saturation = delta / max;
191 hue = (green - blue) / delta;
192 } else if (green == max) {
193 hue = 2 + ((blue - red) / delta);
195 hue = 4 + ((red - green) / delta);
201 return {h: hue, s: saturation, b: brightness};
203 var p2s = /,?([achlmqrstvxz]),?/gi;
204 R._path2string = function () {
205 return this.join(",")[rp](p2s, "$1");
207 function cacher(f, scope, postprocessor) {
209 var arg = Array[proto].slice.call(arguments, 0),
210 args = arg[join]("\u25ba"),
211 cache = newf.cache = newf.cache || {},
212 count = newf.count = newf.count || [];
213 if (cache[has](args)) {
214 return postprocessor ? postprocessor(cache[args]) : cache[args];
216 count[length] >= 1e3 && delete cache[count.shift()];
218 cache[args] = f[apply](scope, arg);
219 return postprocessor ? postprocessor(cache[args]) : cache[args];
224 R.getRGB = cacher(function (colour) {
225 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
226 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
228 if (colour == "none") {
229 return {r: -1, g: -1, b: -1, hex: "none"};
231 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
237 rgb = colour.match(colourRegExp);
240 blue = toInt(rgb[2].substring(5), 16);
241 green = toInt(rgb[2].substring(3, 5), 16);
242 red = toInt(rgb[2].substring(1, 3), 16);
245 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
246 green = toInt((t = rgb[3].charAt(2)) + t, 16);
247 red = toInt((t = rgb[3].charAt(1)) + t, 16);
250 rgb = rgb[4][split](/\s*,\s*/);
251 red = toFloat(rgb[0]);
252 green = toFloat(rgb[1]);
253 blue = toFloat(rgb[2]);
256 rgb = rgb[5][split](/\s*,\s*/);
257 red = toFloat(rgb[0]) * 2.55;
258 green = toFloat(rgb[1]) * 2.55;
259 blue = toFloat(rgb[2]) * 2.55;
262 rgb = rgb[6][split](/\s*,\s*/);
263 red = toFloat(rgb[0]);
264 green = toFloat(rgb[1]);
265 blue = toFloat(rgb[2]);
266 return R.hsb2rgb(red, green, blue);
269 rgb = rgb[7][split](/\s*,\s*/);
270 red = toFloat(rgb[0]) * 2.55;
271 green = toFloat(rgb[1]) * 2.55;
272 blue = toFloat(rgb[2]) * 2.55;
273 return R.hsb2rgb(red, green, blue);
275 rgb = {r: red, g: green, b: blue};
276 var r = (~~red)[toString](16),
277 g = (~~green)[toString](16),
278 b = (~~blue)[toString](16);
282 rgb.hex = "#" + r + g + b;
285 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
287 R.getColor = function (value) {
288 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
289 rgb = this.hsb2rgb(start.h, start.s, start.b);
294 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
298 R.getColor.reset = function () {
302 R.parsePathString = cacher(function (pathString) {
306 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
308 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
309 data = pathClone(pathString);
312 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
314 name = lowerCase.call(b);
315 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
316 b && params[push](+b);
318 while (params[length] >= paramCounts[name]) {
319 data[push]([b][concat](params.splice(0, paramCounts[name])));
320 if (!paramCounts[name]) {
326 data[toString] = R._path2string;
329 R.findDotsAtSegment = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
331 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
332 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
333 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
334 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
335 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
336 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
337 ax = (1 - t) * p1x + t * c1x,
338 ay = (1 - t) * p1y + t * c1y,
339 cx = (1 - t) * c2x + t * p2x,
340 cy = (1 - t) * c2y + t * p2y;
341 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}};
343 var pathDimensions = cacher(function (path) {
345 return {x: 0, y: 0, width: 0, height: 0};
347 path = path2curve(path);
353 for (var i = 0, ii = path[length]; i < ii; i++) {
361 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
362 X = X[concat](dim.min.x, dim.max.x);
363 Y = Y[concat](dim.min.y, dim.max.y);
368 var xmin = mmin[apply](0, X),
369 ymin = mmin[apply](0, Y);
373 width: mmax[apply](0, X) - xmin,
374 height: mmax[apply](0, Y) - ymin
377 pathClone = function (pathArray) {
379 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
380 pathArray = R.parsePathString(pathArray);
382 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
384 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
385 res[i][j] = pathArray[i][j];
388 res[toString] = R._path2string;
391 pathToRelative = cacher(function (pathArray) {
392 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
393 pathArray = R.parsePathString(pathArray);
401 if (pathArray[0][0] == "M") {
407 res[push](["M", x, y]);
409 for (var i = start, ii = pathArray[length]; i < ii; i++) {
412 if (pa[0] != lowerCase.call(pa[0])) {
413 r[0] = lowerCase.call(pa[0]);
421 r[6] = +(pa[6] - x).toFixed(3);
422 r[7] = +(pa[7] - y).toFixed(3);
425 r[1] = +(pa[1] - y).toFixed(3);
431 for (var j = 1, jj = pa[length]; j < jj; j++) {
432 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
441 for (var k = 0, kk = pa[length]; k < kk; k++) {
445 var len = res[i][length];
452 x += +res[i][len - 1];
455 y += +res[i][len - 1];
458 x += +res[i][len - 2];
459 y += +res[i][len - 1];
462 res[toString] = R._path2string;
465 pathToAbsolute = cacher(function (pathArray) {
466 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
467 pathArray = R.parsePathString(pathArray);
475 if (pathArray[0][0] == "M") {
476 x = +pathArray[0][1];
477 y = +pathArray[0][2];
481 res[0] = ["M", x, y];
483 for (var i = start, ii = pathArray[length]; i < ii; i++) {
486 if (pa[0] != upperCase.call(pa[0])) {
487 r[0] = upperCase.call(pa[0]);
508 for (var j = 1, jj = pa[length]; j < jj; j++) {
509 r[j] = +pa[j] + ((j % 2) ? x : y);
513 for (var k = 0, kk = pa[length]; k < kk; k++) {
529 x = res[i][res[i][length] - 2];
530 y = res[i][res[i][length] - 1];
533 res[toString] = R._path2string;
536 l2c = function (x1, y1, x2, y2) {
537 return [x1, y1, x2, y2, x2, y2];
539 q2c = function (x1, y1, ax, ay, x2, y2) {
551 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
552 // for more information of where this math came from visit:
553 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
555 _120 = PI * 120 / 180,
556 rad = PI / 180 * (+angle || 0),
559 rotate = cacher(function (x, y, rad) {
560 var X = x * math.cos(rad) - y * math.sin(rad),
561 Y = x * math.sin(rad) + y * math.cos(rad);
565 xy = rotate(x1, y1, -rad);
568 xy = rotate(x2, y2, -rad);
571 var cos = math.cos(PI / 180 * angle),
572 sin = math.sin(PI / 180 * angle),
575 rx = mmax(rx, abs(x));
576 ry = mmax(ry, abs(y));
579 k = (large_arc_flag == sweep_flag ? -1 : 1) *
580 math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
581 cx = k * rx * y / ry + (x1 + x2) / 2,
582 cy = k * -ry * x / rx + (y1 + y2) / 2,
583 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
584 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
586 f1 = x1 < cx ? PI - f1 : f1;
587 f2 = x2 < cx ? PI - f2 : f2;
588 f1 < 0 && (f1 = PI * 2 + f1);
589 f2 < 0 && (f2 = PI * 2 + f2);
590 if (sweep_flag && f1 > f2) {
593 if (!sweep_flag && f2 > f1) {
603 if (abs(df) > _120) {
607 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
608 x2 = cx + rx * math.cos(f2);
609 y2 = cy + ry * math.sin(f2);
610 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
613 var c1 = math.cos(f1),
617 t = math.tan(df / 4),
621 m2 = [x1 + hx * s1, y1 - hy * c1],
622 m3 = [x2 + hx * s2, y2 - hy * c2],
624 m2[0] = 2 * m1[0] - m2[0];
625 m2[1] = 2 * m1[1] - m2[1];
627 return [m2, m3, m4][concat](res);
629 res = [m2, m3, m4][concat](res)[join]()[split](",");
631 for (var i = 0, ii = res[length]; i < ii; i++) {
632 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
637 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
640 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
641 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
644 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
645 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
646 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
648 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
649 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
653 abs(t1) > 1e12 && (t1 = .5);
654 abs(t2) > 1e12 && (t2 = .5);
655 if (t1 > 0 && t1 < 1) {
656 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
660 if (t2 > 0 && t2 < 1) {
661 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
665 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
666 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
668 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
669 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
670 abs(t1) > 1e12 && (t1 = .5);
671 abs(t2) > 1e12 && (t2 = .5);
672 if (t1 > 0 && t1 < 1) {
673 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
677 if (t2 > 0 && t2 < 1) {
678 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
683 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
684 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
687 path2curve = cacher(function (path, path2) {
688 var p = pathToAbsolute(path),
689 p2 = path2 && pathToAbsolute(path2),
690 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
691 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
692 processPath = function (path, d) {
695 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
697 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
704 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
707 nx = d.x + (d.x - (d.bx || d.x));
708 ny = d.y + (d.y - (d.by || d.y));
709 path = ["C", nx, ny][concat](path.slice(1));
712 d.qx = d.x + (d.x - (d.qx || d.x));
713 d.qy = d.y + (d.y - (d.qy || d.y));
714 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
719 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
722 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
725 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
728 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
731 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
736 fixArc = function (pp, i) {
737 if (pp[i][length] > 7) {
741 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
744 ii = mmax(p[length], p2 && p2[length] || 0);
747 fixM = function (path1, path2, a1, a2, i) {
748 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
749 path2.splice(i, 0, ["M", a2.x, a2.y]);
754 ii = mmax(p[length], p2 && p2[length] || 0);
757 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
758 p[i] = processPath(p[i], attrs);
760 p2 && (p2[i] = processPath(p2[i], attrs2));
762 fixM(p, p2, attrs, attrs2, i);
763 fixM(p2, p, attrs2, attrs, i);
766 seglen = seg[length],
767 seg2len = p2 && seg2[length];
768 attrs.x = seg[seglen - 2];
769 attrs.y = seg[seglen - 1];
770 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
771 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
772 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
773 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
774 attrs2.x = p2 && seg2[seg2len - 2];
775 attrs2.y = p2 && seg2[seg2len - 1];
777 return p2 ? [p, p2] : p;
779 parseDots = cacher(function (gradient) {
781 for (var i = 0, ii = gradient[length]; i < ii; i++) {
783 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
784 dot.color = R.getRGB(par[1]);
785 if (dot.color.error) {
788 dot.color = dot.color.hex;
789 par[2] && (dot.offset = par[2] + "%");
792 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
793 if (!dots[i].offset) {
794 var start = toFloat(dots[i - 1].offset || 0),
796 for (var j = i + 1; j < ii; j++) {
797 if (dots[j].offset) {
798 end = dots[j].offset;
807 var d = (end - start) / (j - i + 1);
810 dots[i].offset = start + "%";
816 getContainer = function () {
822 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
823 if (R.is(arguments[0], "string")) {
824 container = doc.getElementById(arguments[0]);
826 container = arguments[0];
828 if (container.tagName) {
829 if (arguments[1] == null) {
831 container: container,
832 width: container.style.pixelWidth || container.offsetWidth,
833 height: container.style.pixelHeight || container.offsetHeight
836 return {container: container, width: arguments[1], height: arguments[2]};
839 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
840 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
843 plugins = function (con, add) {
845 for (var prop in add) if (add[has](prop) && !(prop in con)) {
846 switch (typeof add[prop]) {
849 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
853 con[prop] = con[prop] || {};
854 plugins.call(this, con[prop], add[prop]);
857 con[prop] = add[prop];
862 tear = function (el, paper) {
863 el == paper.top && (paper.top = el.prev);
864 el == paper.bottom && (paper.bottom = el.next);
865 el.next && (el.next.prev = el.prev);
866 el.prev && (el.prev.next = el.next);
868 tofront = function (el, paper) {
869 if (paper.top === el) {
878 toback = function (el, paper) {
879 if (paper.bottom === el) {
883 el.next = paper.bottom;
885 paper.bottom.prev = el;
888 insertafter = function (el, el2, paper) {
890 el2 == paper.top && (paper.top = el);
891 el2.next && (el2.next.prev = el);
896 insertbefore = function (el, el2, paper) {
898 el2 == paper.bottom && (paper.bottom = el);
899 el2.prev && (el2.prev.next = el);
904 removed = function (methodname) {
906 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
909 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
913 Paper[proto].svgns = "http://www.w3.org/2000/svg";
914 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
915 var round = function (num) {
916 return +num + (~~num === num) * .5;
918 roundPath = function (path) {
919 for (var i = 0, ii = path[length]; i < ii; i++) {
920 if (lowerCase.call(path[i][0]) != "a") {
921 for (var j = 1, jj = path[i][length]; j < jj; j++) {
922 path[i][j] = round(path[i][j]);
925 path[i][6] = round(path[i][6]);
926 path[i][7] = round(path[i][7]);
931 $ = function (el, attr) {
933 for (var key in attr) if (attr[has](key)) {
934 el[setAttribute](key, attr[key]);
937 return doc.createElementNS(Paper[proto].svgns, el);
940 R[toString] = function () {
941 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
943 var thePath = function (pathString, SVG) {
945 SVG.canvas && SVG.canvas[appendChild](el);
946 var p = new Element(el, SVG);
948 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
951 var addGradientFill = function (o, gradient, SVG) {
955 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
960 var dir = ((fy > .5) * 2 - 1);
961 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
962 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
964 (fy = fy.toFixed(5) - 1e-5 * dir);
968 gradient = gradient[split](/\s*\-\s*/);
969 if (type == "linear") {
970 var angle = gradient.shift();
971 angle = -toFloat(angle);
975 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
976 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
980 vector[0] = -vector[2];
984 vector[1] = -vector[3];
988 var dots = parseDots(gradient);
992 var el = $(type + "Gradient");
993 el.id = "r" + (R._id++)[toString](36);
994 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
995 SVG.defs[appendChild](el);
996 for (var i = 0, ii = dots[length]; i < ii; i++) {
997 var stop = $("stop");
999 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1000 "stop-color": dots[i].color || "#fff"
1002 el[appendChild](stop);
1005 fill: "url(#" + el.id + ")",
1014 var updatePosition = function (o) {
1015 var bbox = o.getBBox();
1016 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1018 var setFillAndStroke = function (o, params) {
1025 "-..": [3, 1, 1, 1, 1, 1],
1029 "- .": [4, 3, 1, 3],
1030 "--.": [8, 3, 1, 3],
1031 "--..": [8, 3, 1, 3, 1, 3]
1036 addDashes = function (o, value) {
1037 value = dasharray[lowerCase.call(value)];
1039 var width = o.attrs["stroke-width"] || "1",
1040 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1042 var i = value[length];
1044 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1046 $(node, {"stroke-dasharray": dashes[join](",")});
1049 params[has]("rotation") && (rot = params.rotation);
1050 var rotxy = (rot + E)[split](separator);
1051 if (!(rotxy.length - 1)) {
1054 rotxy[1] = +rotxy[1];
1055 rotxy[2] = +rotxy[2];
1057 toFloat(rot) && o.rotate(0, true);
1058 for (var att in params) if (params[has](att)) {
1059 if (!availableAttrs[has](att)) {
1062 var value = params[att];
1066 o.rotate(value, true);
1072 var pn = node.parentNode;
1073 if (lowerCase.call(pn.tagName) != "a") {
1075 pn.insertBefore(hl, node);
1076 hl[appendChild](node);
1079 pn.setAttributeNS(o.Paper[proto].xlink, att, value);
1082 node.style.cursor = value;
1085 var rect = (value + E)[split](separator);
1086 if (rect[length] == 4) {
1087 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1088 var el = $("clipPath"),
1090 el.id = "r" + (R._id++)[toString](36);
1097 el[appendChild](rc);
1098 o.paper.defs[appendChild](el);
1099 $(node, {"clip-path": "url(#" + el.id + ")"});
1103 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1104 clip && clip.parentNode.removeChild(clip);
1105 $(node, {"clip-path": E});
1110 if (value && o.type == "path") {
1111 attrs.path = roundPath(pathToAbsolute(value));
1112 $(node, {d: attrs.path});
1116 node[setAttribute](att, value);
1125 value = -attrs.x - (attrs.width || 0);
1128 if (att == "rx" && o.type == "rect") {
1132 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1133 node[setAttribute](att, round(value));
1134 o.pattern && updatePosition(o);
1137 node[setAttribute](att, value);
1146 value = -attrs.y - (attrs.height || 0);
1149 if (att == "ry" && o.type == "rect") {
1153 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1154 node[setAttribute](att, round(value));
1155 o.pattern && updatePosition(o);
1158 if (o.type == "rect") {
1159 $(node, {rx: value, ry: value});
1161 node[setAttribute](att, value);
1165 if (o.type == "image") {
1166 node.setAttributeNS(o.paper.xlink, "href", value);
1169 case "stroke-width":
1170 node.style.strokeWidth = value;
1171 // Need following line for Firefox
1172 node[setAttribute](att, value);
1173 if (attrs["stroke-dasharray"]) {
1174 addDashes(o, attrs["stroke-dasharray"]);
1177 case "stroke-dasharray":
1178 addDashes(o, value);
1181 var xy = (value + E)[split](separator);
1182 xy[0] = +xy[0] || 0;
1183 xy[1] = +xy[1] || 0;
1188 translate.call(o, xy[0], xy[1]);
1191 var xy = (value + E)[split](separator);
1192 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1195 var isURL = (value + E).match(ISURL);
1197 var el = $("pattern"),
1199 el.id = "r" + (R._id++)[toString](36);
1200 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1201 $(ig, {x: 0, y: 0});
1202 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1203 el[appendChild](ig);
1205 var img = doc.createElement("img");
1206 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1207 img.onload = function () {
1208 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1209 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1210 doc.body.removeChild(this);
1213 doc.body[appendChild](img);
1215 o.paper.defs[appendChild](el);
1216 node.style.fill = "url(#" + el.id + ")";
1217 $(node, {fill: "url(#" + el.id + ")"});
1219 o.pattern && updatePosition(o);
1222 if (!R.getRGB(value).error) {
1223 delete params.gradient;
1224 delete attrs.gradient;
1225 !R.is(attrs.opacity, "undefined") &&
1226 R.is(params.opacity, "undefined") &&
1227 $(node, {opacity: attrs.opacity});
1228 !R.is(attrs["fill-opacity"], "undefined") &&
1229 R.is(params["fill-opacity"], "undefined") &&
1230 $(node, {"fill-opacity": attrs["fill-opacity"]});
1231 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1232 attrs.gradient = value;
1233 attrs.fill = "none";
1237 node[setAttribute](att, R.getRGB(value).hex);
1240 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1243 case "fill-opacity":
1244 if (attrs.gradient) {
1245 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1247 var stops = gradient.getElementsByTagName("stop");
1248 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1253 att == "font-size" && (value = toInt(value, 10) + "px");
1254 var cssrule = att[rp](/(\-.)/g, function (w) {
1255 return upperCase.call(w.substring(1));
1257 node.style[cssrule] = value;
1258 // Need following line for Firefox
1259 node[setAttribute](att, value);
1264 tuneText(o, params);
1266 o.rotate(rotxy.join(S));
1268 toFloat(rot) && o.rotate(rot, true);
1272 var tuneText = function (el, params) {
1273 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1278 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1280 if (params[has]("text")) {
1281 a.text = params.text;
1282 while (node.firstChild) {
1283 node.removeChild(node.firstChild);
1285 var texts = (params.text + E)[split]("\n");
1286 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1287 var tspan = $("tspan");
1288 i && $(tspan, {dy: fontSize * leading, x: a.x});
1289 tspan[appendChild](doc.createTextNode(texts[i]));
1290 node[appendChild](tspan);
1293 var texts = node.getElementsByTagName("tspan");
1294 for (var i = 0, ii = texts[length]; i < ii; i++) {
1295 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1299 var bb = el.getBBox(),
1300 dif = a.y - (bb.y + bb.height / 2);
1301 dif && isFinite(dif) && $(node, {y: a.y + dif});
1303 var Element = function (node, svg) {
1309 node.raphael = this;
1311 this.attrs = this.attrs || {};
1312 this.transformations = []; // rotate, translate, scale
1316 rt: {deg: 0, cx: 0, cy: 0},
1320 !svg.bottom && (svg.bottom = this);
1321 this.prev = svg.top;
1322 svg.top && (svg.top.next = this);
1326 Element[proto].rotate = function (deg, cx, cy) {
1332 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1334 return this._.rt.deg;
1336 var bbox = this.getBBox();
1337 deg = (deg + E)[split](separator);
1338 if (deg[length] - 1) {
1339 cx = toFloat(deg[1]);
1340 cy = toFloat(deg[2]);
1342 deg = toFloat(deg[0]);
1344 this._.rt.deg = deg;
1346 this._.rt.deg += deg;
1348 (cy == null) && (cx = null);
1351 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1352 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1353 if (this._.rt.deg) {
1354 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1355 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1357 this.transformations[0] = E;
1358 this.clip && $(this.clip, {transform: E});
1360 $(this.node, {transform: this.transformations[join](S)});
1363 Element[proto].hide = function () {
1364 !this.removed && (this.node.style.display = "none");
1367 Element[proto].show = function () {
1368 !this.removed && (this.node.style.display = "");
1371 Element[proto].remove = function () {
1375 tear(this, this.paper);
1376 this.node.parentNode.removeChild(this.node);
1377 for (var i in this) {
1380 this.removed = true;
1382 Element[proto].getBBox = function () {
1386 if (this.type == "path") {
1387 return pathDimensions(this.attrs.path);
1389 if (this.node.style.display == "none") {
1395 bbox = this.node.getBBox();
1397 // Firefox 3.0.x plays badly here
1401 if (this.type == "text") {
1402 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1403 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1404 var bb = this.node.getExtentOfChar(i);
1405 (bb.y < bbox.y) && (bbox.y = bb.y);
1406 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1407 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1410 hide && this.hide();
1413 Element[proto].attr = function () {
1417 if (arguments[length] == 0) {
1419 for (var i in this.attrs) if (this.attrs[has](i)) {
1420 res[i] = this.attrs[i];
1422 this._.rt.deg && (res.rotation = this.rotate());
1423 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1424 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1427 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1428 if (arguments[0] == "translation") {
1429 return translate.call(this);
1431 if (arguments[0] == "rotation") {
1432 return this.rotate();
1434 if (arguments[0] == "scale") {
1435 return this.scale();
1437 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1438 return this.attrs.gradient;
1440 return this.attrs[arguments[0]];
1442 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1444 for (var j in arguments[0]) if (arguments[0][has](j)) {
1445 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1449 if (arguments[length] == 2) {
1451 params[arguments[0]] = arguments[1];
1452 setFillAndStroke(this, params);
1453 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1454 setFillAndStroke(this, arguments[0]);
1458 Element[proto].toFront = function () {
1462 this.node.parentNode[appendChild](this.node);
1463 var svg = this.paper;
1464 svg.top != this && tofront(this, svg);
1467 Element[proto].toBack = function () {
1471 if (this.node.parentNode.firstChild != this.node) {
1472 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1473 toback(this, this.paper);
1474 var svg = this.paper;
1478 Element[proto].insertAfter = function (element) {
1482 var svg = this.paper,
1483 node = element.node;
1484 if (node.nextSibling) {
1485 node.parentNode.insertBefore(this.node, node.nextSibling);
1487 node.parentNode[appendChild](this.node);
1489 insertafter(this, element, this.paper);
1492 Element[proto].insertBefore = function (element) {
1496 var node = element.node;
1497 node.parentNode.insertBefore(this.node, node);
1498 insertbefore(this, element, this.paper);
1501 Element[proto].getTotalLength = function () {
1502 if (this.type != "path") {
1505 return this.node.getTotalLength();
1507 Element[proto].getPointAtLength = function (length) {
1508 if (this.type != "path") {
1511 var point = this.node.getPointAtLength(length);
1512 return {x: point.x, y: point.y};
1514 var theCircle = function (svg, x, y, r) {
1517 var el = $("circle");
1518 svg.canvas && svg.canvas[appendChild](el);
1519 var res = new Element(el, svg);
1520 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1521 res.type = "circle";
1525 var theRect = function (svg, x, y, w, h, r) {
1529 svg.canvas && svg.canvas[appendChild](el);
1530 var res = new Element(el, svg);
1531 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1536 var theEllipse = function (svg, x, y, rx, ry) {
1539 var el = $("ellipse");
1540 svg.canvas && svg.canvas[appendChild](el);
1541 var res = new Element(el, svg);
1542 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1543 res.type = "ellipse";
1547 var theImage = function (svg, src, x, y, w, h) {
1548 var el = $("image");
1549 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1550 el.setAttributeNS(svg.xlink, "href", src);
1551 svg.canvas && svg.canvas[appendChild](el);
1552 var res = new Element(el, svg);
1553 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1557 var theText = function (svg, x, y, text) {
1559 $(el, {x: x, y: y, "text-anchor": "middle"});
1560 svg.canvas && svg.canvas[appendChild](el);
1561 var res = new Element(el, svg);
1562 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1564 setFillAndStroke(res, res.attrs);
1567 var setSize = function (width, height) {
1568 this.width = width || this.width;
1569 this.height = height || this.height;
1570 this.canvas[setAttribute]("width", this.width);
1571 this.canvas[setAttribute]("height", this.height);
1574 var create = function () {
1575 var con = getContainer[apply](null, arguments),
1576 container = con && con.container,
1580 height = con.height;
1582 throw new Error("SVG container not found.");
1584 var cnvs = $("svg");
1585 width = width || 512;
1586 height = height || 342;
1588 xmlns: "http://www.w3.org/2000/svg",
1593 if (container == 1) {
1594 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1595 doc.body[appendChild](cnvs);
1597 if (container.firstChild) {
1598 container.insertBefore(cnvs, container.firstChild);
1600 container[appendChild](cnvs);
1603 container = new Paper;
1604 container.width = width;
1605 container.height = height;
1606 container.canvas = cnvs;
1607 plugins.call(container, container, R.fn);
1611 Paper[proto].clear = function () {
1612 var c = this.canvas;
1613 while (c.firstChild) {
1614 c.removeChild(c.firstChild);
1616 this.bottom = this.top = null;
1617 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1618 c[appendChild](this.desc);
1619 c[appendChild](this.defs = $("defs"));
1621 Paper[proto].remove = function () {
1622 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1623 for (var i in this) {
1624 this[i] = removed(i);
1631 var path2vml = function (path) {
1632 var total = /[ahqtv]/ig,
1633 command = pathToAbsolute;
1634 (path + E).match(total) && (command = path2curve);
1636 if (command == pathToAbsolute && !(path + E).match(total)) {
1637 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1638 bites = /([clmz]),?([^clmz]*)/gi,
1639 val = /-?[^,\s-]+/g;
1640 var res = (path + E)[rp](bites, function (all, command, args) {
1642 args[rp](val, function (value) {
1643 vals[push](round(value));
1645 return map[command] + vals;
1649 var pa = command(path), p, res = [], r;
1650 for (var i = 0, ii = pa[length]; i < ii; i++) {
1652 r = lowerCase.call(pa[i][0]);
1653 r == "z" && (r = "x");
1654 for (var j = 1, jj = p[length]; j < jj; j++) {
1655 r += round(p[j]) + (j != jj - 1 ? "," : E);
1659 return res[join](S);
1661 segmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
1662 var old = {x: 0, y: 0},
1664 for (var i = 0; i < 1.01; i+=.01) {
1665 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
1666 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
1672 R[toString] = function () {
1673 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1675 var thePath = function (pathString, VML) {
1676 var g = createNode("group");
1677 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1678 g.coordsize = VML.coordsize;
1679 g.coordorigin = VML.coordorigin;
1680 var el = createNode("shape"), ol = el.style;
1681 ol.width = VML.width + "px";
1682 ol.height = VML.height + "px";
1683 el.coordsize = this.coordsize;
1684 el.coordorigin = this.coordorigin;
1686 var p = new Element(el, g, VML);
1687 p.isAbsolute = true;
1691 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1692 VML.canvas[appendChild](g);
1695 var setFillAndStroke = function (o, params) {
1696 o.attrs = o.attrs || {};
1702 for (var par in params) if (params[has](par)) {
1703 a[par] = params[par];
1705 params.href && (node.href = params.href);
1706 params.title && (node.title = params.title);
1707 params.target && (node.target = params.target);
1708 params.cursor && (s.cursor = params.cursor);
1709 if (params.path && o.type == "path") {
1710 a.path = params.path;
1711 node.path = path2vml(a.path);
1713 if (params.rotation != null) {
1714 o.rotate(params.rotation, true);
1716 if (params.translation) {
1717 xy = (params.translation + E)[split](separator);
1718 translate.call(o, xy[0], xy[1]);
1719 if (o._.rt.cx != null) {
1720 o._.rt.cx +=+ xy[0];
1721 o._.rt.cy +=+ xy[1];
1722 o.setBox(o.attrs, xy[0], xy[1]);
1726 xy = (params.scale + E)[split](separator);
1727 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1729 if ("clip-rect" in params) {
1730 var rect = (params["clip-rect"] + E)[split](separator);
1731 if (rect[length] == 4) {
1732 rect[2] = +rect[2] + (+rect[0]);
1733 rect[3] = +rect[3] + (+rect[1]);
1734 var div = node.clipRect || doc.createElement("div"),
1736 group = node.parentNode;
1737 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1738 if (!node.clipRect) {
1739 dstyle.position = "absolute";
1742 dstyle.width = o.paper.width + "px";
1743 dstyle.height = o.paper.height + "px";
1744 group.parentNode.insertBefore(div, group);
1745 div[appendChild](group);
1746 node.clipRect = div;
1749 if (!params["clip-rect"]) {
1750 node.clipRect && (node.clipRect.style.clip = E);
1753 if (o.type == "image" && params.src) {
1754 node.src = params.src;
1756 if (o.type == "image" && params.opacity) {
1757 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1758 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1760 params.font && (s.font = params.font);
1761 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1762 params["font-size"] && (s.fontSize = params["font-size"]);
1763 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1764 params["font-style"] && (s.fontStyle = params["font-style"]);
1765 if (params.opacity != null ||
1766 params["stroke-width"] != null ||
1767 params.fill != null ||
1768 params.stroke != null ||
1769 params["stroke-width"] != null ||
1770 params["stroke-opacity"] != null ||
1771 params["fill-opacity"] != null ||
1772 params["stroke-dasharray"] != null ||
1773 params["stroke-miterlimit"] != null ||
1774 params["stroke-linejoin"] != null ||
1775 params["stroke-linecap"] != null) {
1776 node = o.shape || node;
1777 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1779 !fill && (newfill = fill = createNode("fill"));
1780 if ("fill-opacity" in params || "opacity" in params) {
1781 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1782 opacity < 0 && (opacity = 0);
1783 opacity > 1 && (opacity = 1);
1784 fill.opacity = opacity;
1786 params.fill && (fill.on = true);
1787 if (fill.on == null || params.fill == "none") {
1790 if (fill.on && params.fill) {
1791 var isURL = params.fill.match(ISURL);
1793 fill.src = isURL[1];
1796 fill.color = R.getRGB(params.fill).hex;
1798 fill.type = "solid";
1799 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1801 a.gradient = params.fill;
1805 newfill && node[appendChild](fill);
1806 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1808 !stroke && (newstroke = stroke = createNode("stroke"));
1809 if ((params.stroke && params.stroke != "none") ||
1810 params["stroke-width"] ||
1811 params["stroke-opacity"] != null ||
1812 params["stroke-dasharray"] ||
1813 params["stroke-miterlimit"] ||
1814 params["stroke-linejoin"] ||
1815 params["stroke-linecap"]) {
1818 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1819 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1820 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1821 width = (toFloat(params["stroke-width"]) || 1) * .75;
1822 opacity < 0 && (opacity = 0);
1823 opacity > 1 && (opacity = 1);
1824 params["stroke-width"] == null && (width = a["stroke-width"]);
1825 params["stroke-width"] && (stroke.weight = width);
1826 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1827 stroke.opacity = opacity;
1829 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1830 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1831 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1832 if (params["stroke-dasharray"]) {
1836 "-.": "shortdashdot",
1837 "-..": "shortdashdotdot",
1842 "--.": "longdashdot",
1843 "--..": "longdashdotdot"
1845 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1847 newstroke && node[appendChild](stroke);
1849 if (res.type == "text") {
1850 var s = res.paper.span.style;
1851 a.font && (s.font = a.font);
1852 a["font-family"] && (s.fontFamily = a["font-family"]);
1853 a["font-size"] && (s.fontSize = a["font-size"]);
1854 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1855 a["font-style"] && (s.fontStyle = a["font-style"]);
1856 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1857 res.W = a.w = res.paper.span.offsetWidth;
1858 res.H = a.h = res.paper.span.offsetHeight;
1860 res.Y = a.y + round(res.H / 2);
1862 // text-anchor emulationm
1863 switch (a["text-anchor"]) {
1865 res.node.style["v-text-align"] = "left";
1866 res.bbx = round(res.W / 2);
1869 res.node.style["v-text-align"] = "right";
1870 res.bbx = -round(res.W / 2);
1873 res.node.style["v-text-align"] = "center";
1878 var addGradientFill = function (o, gradient) {
1879 o.attrs = o.attrs || {};
1880 var attrs = o.attrs,
1881 fill = o.node.getElementsByTagName("fill"),
1884 o.attrs.gradient = gradient;
1885 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1890 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1895 gradient = gradient[split](/\s*\-\s*/);
1896 if (type == "linear") {
1897 var angle = gradient.shift();
1898 angle = -toFloat(angle);
1903 var dots = parseDots(gradient);
1907 o = o.shape || o.node;
1908 fill = fill[0] || createNode("fill");
1911 fill.method = "none";
1912 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1913 fill.color = dots[0].color;
1914 fill.color2 = dots[dots[length] - 1].color;
1916 for (var i = 0, ii = dots[length]; i < ii; i++) {
1917 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1919 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1920 if (type == "radial") {
1921 fill.focus = "100%";
1922 fill.focussize = fxfy;
1923 fill.focusposition = fxfy;
1925 fill.angle = (270 - angle) % 360;
1930 var Element = function (node, group, vml) {
1938 node.raphael = this;
1951 !vml.bottom && (vml.bottom = this);
1952 this.prev = vml.top;
1953 vml.top && (vml.top.next = this);
1957 Element[proto].rotate = function (deg, cx, cy) {
1963 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1965 return this._.rt.deg;
1967 deg = (deg + E)[split](separator);
1968 if (deg[length] - 1) {
1969 cx = toFloat(deg[1]);
1970 cy = toFloat(deg[2]);
1972 deg = toFloat(deg[0]);
1974 this._.rt.deg = deg;
1976 this._.rt.deg += deg;
1978 cy == null && (cx = null);
1981 this.setBox(this.attrs, cx, cy);
1982 this.Group.style.rotation = this._.rt.deg;
1983 // gradient fix for rotation. TODO
1984 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1985 // fill = fill[0] || {};
1986 // var b = ((360 - this._.rt.deg) - 270) % 360;
1987 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1990 Element[proto].setBox = function (params, cx, cy) {
1994 var gs = this.Group.style,
1995 os = (this.shape && this.shape.style) || this.node.style;
1996 params = params || {};
1997 for (var i in params) if (params[has](i)) {
1998 this.attrs[i] = params[i];
2000 cx = cx || this._.rt.cx;
2001 cy = cy || this._.rt.cy;
2002 var attr = this.attrs,
2007 switch (this.type) {
2009 x = attr.cx - attr.r;
2010 y = attr.cy - attr.r;
2014 x = attr.cx - attr.rx;
2015 y = attr.cy - attr.ry;
2023 w = attr.width || 0;
2024 h = attr.height || 0;
2027 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2028 x = attr.x - round(this.W / 2);
2029 y = attr.y - this.H / 2;
2034 if (!this.attrs.path) {
2037 w = this.paper.width;
2038 h = this.paper.height;
2040 var dim = pathDimensions(this.attrs.path);
2050 w = this.paper.width;
2051 h = this.paper.height;
2054 cx = (cx == null) ? x + w / 2 : cx;
2055 cy = (cy == null) ? y + h / 2 : cy;
2056 var left = cx - this.paper.width / 2,
2057 top = cy - this.paper.height / 2;
2058 if (this.type == "path" || this.type == "text") {
2059 (gs.left != left + "px") && (gs.left = left + "px");
2060 (gs.top != top + "px") && (gs.top = top + "px");
2061 this.X = this.type == "text" ? x : -left;
2062 this.Y = this.type == "text" ? y : -top;
2065 (os.left != -left + "px") && (os.left = -left + "px");
2066 (os.top != -top + "px") && (os.top = -top + "px");
2068 (gs.left != left + "px") && (gs.left = left + "px");
2069 (gs.top != top + "px") && (gs.top = top + "px");
2074 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2075 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2076 (os.left != x - left + "px") && (os.left = x - left + "px");
2077 (os.top != y - top + "px") && (os.top = y - top + "px");
2078 (os.width != w + "px") && (os.width = w + "px");
2079 (os.height != h + "px") && (os.height = h + "px");
2080 var arcsize = (+params.r || 0) / mmin(w, h);
2081 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2082 // We should replace element with the new one
2083 var o = createNode("roundrect"),
2086 ii = this.events && this.events[length];
2087 o.arcsize = arcsize;
2089 this.Group[appendChild](o);
2090 this.Group.removeChild(this.node);
2091 this[0] = this.node = o;
2092 this.arcsize = arcsize;
2093 for (var i in attr) {
2098 if (this.events) for (; i < ii; i++) {
2099 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2104 Element[proto].hide = function () {
2105 !this.removed && (this.Group.style.display = "none");
2108 Element[proto].show = function () {
2109 !this.removed && (this.Group.style.display = "block");
2112 Element[proto].getBBox = function () {
2116 if (this.type == "path") {
2117 return pathDimensions(this.attrs.path);
2120 x: this.X + (this.bbx || 0),
2126 Element[proto].remove = function () {
2130 tear(this, this.paper);
2131 this.node.parentNode.removeChild(this.node);
2132 this.Group.parentNode.removeChild(this.Group);
2133 this.shape && this.shape.parentNode.removeChild(this.shape);
2134 for (var i in this) {
2137 this.removed = true;
2139 Element[proto].attr = function () {
2143 if (arguments[length] == 0) {
2145 for (var i in this.attrs) if (this.attrs[has](i)) {
2146 res[i] = this.attrs[i];
2148 this._.rt.deg && (res.rotation = this.rotate());
2149 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2150 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2153 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2154 if (arguments[0] == "translation") {
2155 return translate.call(this);
2157 if (arguments[0] == "rotation") {
2158 return this.rotate();
2160 if (arguments[0] == "scale") {
2161 return this.scale();
2163 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2164 return this.attrs.gradient;
2166 return this.attrs[arguments[0]];
2168 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2170 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2171 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2176 if (arguments[length] == 2) {
2178 params[arguments[0]] = arguments[1];
2180 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2182 if (params.text && this.type == "text") {
2183 this.node.string = params.text;
2185 setFillAndStroke(this, params);
2186 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2187 addGradientFill(this, params.gradient);
2189 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2193 Element[proto].toFront = function () {
2194 !this.removed && this.Group.parentNode[appendChild](this.Group);
2195 this.paper.top != this && tofront(this, this.paper);
2198 Element[proto].toBack = function () {
2202 if (this.Group.parentNode.firstChild != this.Group) {
2203 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2204 toback(this, this.paper);
2208 Element[proto].insertAfter = function (element) {
2212 if (element.Group.nextSibling) {
2213 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2215 element.Group.parentNode[appendChild](this.Group);
2217 insertafter(this, element, this.paper);
2220 Element[proto].insertBefore = function (element) {
2224 element.Group.parentNode.insertBefore(this.Group, element.Group);
2225 insertbefore(this, element, this.paper);
2228 var getLengthFactory = function (istotal) {
2229 return function (length) {
2230 if (this.type != "path") {
2233 var path = path2curve(this.attrs.path), x, y, p, l,
2235 for (var i = 0, ii = path.length; i < ii; i++) {
2241 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2242 if (!istotal && len + l > length) {
2243 return findDotAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2250 return istotal ? len : {x: x, y: y};
2253 Element[proto].getTotalLength = getLengthFactory(1);
2254 Element[proto].getPointAtLength = getLengthFactory();
2256 var theCircle = function (vml, x, y, r) {
2257 var g = createNode("group"),
2258 o = createNode("oval"),
2260 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2261 g.coordsize = vml.coordsize;
2262 g.coordorigin = vml.coordorigin;
2264 var res = new Element(o, g, vml);
2265 res.type = "circle";
2266 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2270 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2271 vml.canvas[appendChild](g);
2274 theRect = function (vml, x, y, w, h, r) {
2275 var g = createNode("group"),
2276 o = createNode("roundrect"),
2277 arcsize = (+r || 0) / (mmin(w, h));
2278 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2279 g.coordsize = vml.coordsize;
2280 g.coordorigin = vml.coordorigin;
2282 o.arcsize = arcsize;
2283 var res = new Element(o, g, vml);
2285 setFillAndStroke(res, {stroke: "#000"});
2286 res.arcsize = arcsize;
2287 res.setBox({x: x, y: y, width: w, height: h, r: r});
2288 vml.canvas[appendChild](g);
2291 theEllipse = function (vml, x, y, rx, ry) {
2292 var g = createNode("group"),
2293 o = createNode("oval"),
2295 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2296 g.coordsize = vml.coordsize;
2297 g.coordorigin = vml.coordorigin;
2299 var res = new Element(o, g, vml);
2300 res.type = "ellipse";
2301 setFillAndStroke(res, {stroke: "#000"});
2306 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2307 vml.canvas[appendChild](g);
2310 theImage = function (vml, src, x, y, w, h) {
2311 var g = createNode("group"),
2312 o = createNode("image"),
2314 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2315 g.coordsize = vml.coordsize;
2316 g.coordorigin = vml.coordorigin;
2319 var res = new Element(o, g, vml);
2321 res.attrs.src = src;
2326 res.setBox({x: x, y: y, width: w, height: h});
2327 vml.canvas[appendChild](g);
2330 theText = function (vml, x, y, text) {
2331 var g = createNode("group"),
2332 el = createNode("shape"),
2334 path = createNode("path"),
2336 o = createNode("textpath");
2337 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2338 g.coordsize = vml.coordsize;
2339 g.coordorigin = vml.coordorigin;
2340 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2341 path.textpathok = true;
2342 ol.width = vml.width;
2343 ol.height = vml.height;
2344 o.string = text + E;
2347 el[appendChild](path);
2349 var res = new Element(o, g, vml);
2351 res.textpath = path;
2353 res.attrs.text = text;
2358 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2360 vml.canvas[appendChild](g);
2363 setSize = function (width, height) {
2364 var cs = this.canvas.style;
2365 width == +width && (width += "px");
2366 height == +height && (height += "px");
2369 cs.clip = "rect(0 " + width + " " + height + " 0)";
2373 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2375 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2376 createNode = function (tagName) {
2377 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2380 createNode = function (tagName) {
2381 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2384 var create = function () {
2385 var con = getContainer[apply](null, arguments),
2386 container = con.container,
2387 height = con.height,
2393 throw new Error("VML container not found.");
2395 var res = new Paper,
2396 c = res.canvas = doc.createElement("div"),
2398 width = width || 512;
2399 height = height || 342;
2400 width == +width && (width += "px");
2401 height == +height && (height += "px");
2404 res.coordsize = "1000 1000";
2405 res.coordorigin = "0 0";
2406 res.span = doc.createElement("span");
2407 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2408 c[appendChild](res.span);
2409 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2410 if (container == 1) {
2411 doc.body[appendChild](c);
2415 container.style.width = width;
2416 container.style.height = height;
2417 if (container.firstChild) {
2418 container.insertBefore(c, container.firstChild);
2420 container[appendChild](c);
2423 plugins.call(res, res, R.fn);
2426 Paper[proto].clear = function () {
2427 this.canvas.innerHTML = E;
2428 this.span = doc.createElement("span");
2429 this.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2430 this.canvas[appendChild](this.span);
2431 this.bottom = this.top = null;
2433 Paper[proto].remove = function () {
2434 this.canvas.parentNode.removeChild(this.canvas);
2435 for (var i in this) {
2436 this[i] = removed(i);
2442 // Safari or Chrome (WebKit) rendering bug workaround method
2443 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2444 Paper[proto].safari = function () {
2445 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2446 setTimeout(function () {rect.remove();});
2449 Paper[proto].safari = function () {};
2453 var addEvent = (function () {
2454 if (doc.addEventListener) {
2455 return function (obj, type, fn, element) {
2456 var f = function (e) {
2457 return fn.call(element, e);
2459 obj.addEventListener(type, f, false);
2460 return function () {
2461 obj.removeEventListener(type, f, false);
2465 } else if (doc.attachEvent) {
2466 return function (obj, type, fn, element) {
2467 var f = function (e) {
2468 return fn.call(element, e || win.event);
2470 obj.attachEvent("on" + type, f);
2471 var detacher = function () {
2472 obj.detachEvent("on" + type, f);
2479 for (var i = events[length]; i--;) {
2480 (function (eventName) {
2481 Element[proto][eventName] = function (fn) {
2482 if (R.is(fn, "function")) {
2483 this.events = this.events || [];
2484 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2488 Element[proto]["un" + eventName] = function (fn) {
2489 var events = this.events,
2491 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2493 events.splice(l, 1);
2494 !events.length && delete this.events;
2501 Element[proto].hover = function (f_in, f_out) {
2502 return this.mouseover(f_in).mouseout(f_out);
2504 Element[proto].unhover = function (f_in, f_out) {
2505 return this.unmouseover(f_in).unmouseout(f_out);
2507 Paper[proto].circle = function (x, y, r) {
2508 return theCircle(this, x || 0, y || 0, r || 0);
2510 Paper[proto].rect = function (x, y, w, h, r) {
2511 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2513 Paper[proto].ellipse = function (x, y, rx, ry) {
2514 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2516 Paper[proto].path = function (pathString) {
2517 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2518 return thePath(R.format[apply](R, arguments), this);
2520 Paper[proto].image = function (src, x, y, w, h) {
2521 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2523 Paper[proto].text = function (x, y, text) {
2524 return theText(this, x || 0, y || 0, text || E);
2526 Paper[proto].set = function (itemsArray) {
2527 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2528 return new Set(itemsArray);
2530 Paper[proto].setSize = setSize;
2531 Paper[proto].top = Paper[proto].bottom = null;
2532 Paper[proto].raphael = R;
2534 return this.x + S + this.y;
2536 Element[proto].scale = function (x, y, cx, cy) {
2537 if (x == null && y == null) {
2552 var bb = this.getBBox(),
2553 rcx = bb.x + bb.width / 2,
2554 rcy = bb.y + bb.height / 2,
2555 kx = abs(x / this._.sx),
2556 ky = abs(y / this._.sy);
2557 cx = (+cx || cx == 0) ? cx : rcx;
2558 cy = (+cy || cy == 0) ? cy : rcy;
2559 var dirx = ~~(x / abs(x)),
2560 diry = ~~(y / abs(y)),
2563 s = this.node.style,
2564 ncx = cx - abs(rcx - cx) * dkx,
2565 ncy = cy - abs(rcy - cy) * dky;
2566 switch (this.type) {
2569 var neww = a.width * kx,
2570 newh = a.height * ky;
2573 r: a.r * mmin(kx, ky),
2584 r: a.r * mmin(kx, ky),
2590 var path = pathToRelative(a.path),
2592 for (var i = 0, ii = path[length]; i < ii; i++) {
2595 P0 = upperCase.call(p[0]);
2596 if (P0 == "M" && skip) {
2602 p[path[i][length] - 2] = abs(p[path[i][length] - 2]) * dkx;
2603 p[path[i][length] - 1] = abs(p[path[i][length] - 1]) * dky;
2606 p[5] = +(dirx + diry ? !+p[4] : !!+p[4]);
2607 } else if (P0 == "H") {
2608 for (j = 1, jj = p[length]; j < jj; j++) {
2609 p[j] = abs(p[j]) * dkx;
2611 } else if (P0 == "V") {
2612 for (j = 1, jj = p[length]; j < jj; j++) {
2613 p[j] = abs(p[j]) * dky;
2616 for (j = 1, jj = p[length]; j < jj; j++) {
2617 p[j] = abs(p[j]) * ( (j % 2) ? dkx : dky );
2621 var dim2 = pathDimensions(path),
2622 dx = ncx - dim2.x - dim2.width / 2,
2623 dy = ncy - dim2.y - dim2.height / 2;
2626 this.attr({path: path});
2629 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2630 if (this.transformations) {
2631 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2632 this.node[setAttribute]("transform", this.transformations[join](S));
2633 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2634 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2635 this.attr({x: dx, y: dy});
2639 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2640 ", M12=0, M21=0, M22=", diry,
2641 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2642 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2645 if (this.transformations) {
2646 this.transformations[2] = E;
2647 this.node[setAttribute]("transform", this.transformations[join](S));
2651 this.node.filterMatrix = E;
2652 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2655 a.scale = [x, y, cx, cy][join](S);
2661 Element[proto].clone = function () {
2662 var attr = this.attr();
2664 delete attr.translation;
2665 return this.paper[this.type]().attr(attr);
2668 // animation easing formulas
2669 R.easing_formulas = {
2670 linear: function (n) {
2677 return pow(n - 1, 3) + 1;
2679 "<>": function (n) {
2682 return pow(n, 3) / 2;
2685 return (pow(n, 3) + 2) / 2;
2687 backIn: function (n) {
2689 return n * n * ((s + 1) * n - s);
2691 backOut: function (n) {
2694 return n * n * ((s + 1) * n + s) + 1;
2696 elastic: function (n) {
2697 if (n == 0 || n == 1) {
2702 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2704 bounce: function (n) {
2713 l = s * n * n + .75;
2715 if (n < (2.5 / p)) {
2717 l = s * n * n + .9375;
2720 l = s * n * n + .984375;
2728 var animationElements = {length : 0},
2729 animation = function () {
2730 var Now = +new Date;
2731 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2732 var e = animationElements[l];
2734 delete animationElements[l];
2735 animationElements[length]--;
2738 var time = Now - e.start,
2747 callback = e.callback,
2751 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2752 for (var attr in from) if (from[has](attr)) {
2753 switch (availableAnimAttrs[attr]) {
2755 now = +from[attr] + pos * ms * diff[attr];
2759 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2760 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2761 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2766 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2767 now[i] = [from[attr][i][0]];
2768 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2769 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2771 now[i] = now[i][join](S);
2778 var x = diff[attr][0] * (time - prev),
2779 y = diff[attr][1] * (time - prev);
2785 now = +from[attr][0] + pos * ms * diff[attr][0];
2786 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2789 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);
2795 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2804 that._run && that._run.call(that);
2806 (t.x || t.y) && that.translate(-t.x, -t.y);
2807 to.scale && (to.scale = to.scale + E);
2809 delete animationElements[l];
2810 animationElements[length]--;
2811 that.in_animation = null;
2812 R.is(callback, "function") && callback.call(that);
2816 R.svg && that.paper.safari();
2817 animationElements[length] && setTimeout(animation);
2819 upto255 = function (color) {
2820 return color > 255 ? 255 : (color < 0 ? 0 : color);
2822 translate = function (x, y) {
2824 return {x: this._.tx, y: this._.ty, toString: x_y};
2828 switch (this.type) {
2831 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2836 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2839 var path = pathToRelative(this.attrs.path);
2842 this.attr({path: path});
2847 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2848 animationElements[element.id] && (params.start = animationElements[element.id].start);
2849 return this.animate(params, ms, easing, callback);
2851 Element[proto].onAnimation = function (f) {
2852 this._run = f || null;
2855 Element[proto].animate = function (params, ms, easing, callback) {
2856 if (R.is(easing, "function") || !easing) {
2857 callback = easing || null;
2862 for (var attr in params) if (params[has](attr)) {
2863 if (availableAnimAttrs[has](attr)) {
2864 from[attr] = this.attr(attr);
2865 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2866 to[attr] = params[attr];
2867 switch (availableAnimAttrs[attr]) {
2869 diff[attr] = (to[attr] - from[attr]) / ms;
2872 from[attr] = R.getRGB(from[attr]);
2873 var toColour = R.getRGB(to[attr]);
2875 r: (toColour.r - from[attr].r) / ms,
2876 g: (toColour.g - from[attr].g) / ms,
2877 b: (toColour.b - from[attr].b) / ms
2881 var pathes = path2curve(from[attr], to[attr]);
2882 from[attr] = pathes[0];
2883 var toPath = pathes[1];
2885 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2886 diff[attr][i] = [0];
2887 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2888 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2893 var values = (params[attr] + E)[split](separator),
2894 from2 = (from[attr] + E)[split](separator);
2897 from[attr] = [0, 0];
2898 diff[attr] = [values[0] / ms, values[1] / ms];
2901 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2902 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2905 params[attr] = values;
2906 from[attr] = (from[attr] + E)[split](separator);
2907 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2910 from[attr] = (from[attr] + E)[split](separator);
2914 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2923 this.in_animation = 1;
2924 animationElements[this.id] = {
2925 start: params.start || +new Date,
2935 ++animationElements[length] == 1 && animation();
2938 Element[proto].stop = function () {
2939 animationElements[this.id] && animationElements[length]--;
2940 delete animationElements[this.id];
2943 Element[proto].translate = function (x, y) {
2944 return this.attr({translation: x + " " + y});
2946 Element[proto][toString] = function () {
2947 return "Rapha\xebl\u2019s object";
2949 R.ae = animationElements;
2952 var Set = function (items) {
2956 for (var i = 0, ii = items[length]; i < ii; i++) {
2957 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2958 this[this.items[length]] = this.items[this.items[length]] = items[i];
2964 Set[proto][push] = function () {
2967 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2968 item = arguments[i];
2969 if (item && (item.constructor == Element || item.constructor == Set)) {
2970 len = this.items[length];
2971 this[len] = this.items[len] = item;
2977 Set[proto].pop = function () {
2978 delete this[this[length]--];
2979 return this.items.pop();
2981 for (var method in Element[proto]) if (Element[proto][has](method)) {
2982 Set[proto][method] = (function (methodname) {
2983 return function () {
2984 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2985 this.items[i][methodname][apply](this.items[i], arguments);
2991 Set[proto].attr = function (name, value) {
2992 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2993 for (var j = 0, jj = name[length]; j < jj; j++) {
2994 this.items[j].attr(name[j]);
2997 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2998 this.items[i].attr[apply](this.items[i], arguments);
3003 Set[proto].animate = function (params, ms, easing, callback) {
3004 (R.is(easing, "function") || !easing) && (callback = easing || null);
3005 var len = this.items[length],
3009 callback && (collector = function () {
3010 !--len && callback.call(set);
3012 this.items[--i].animate(params, ms, easing || collector, collector);
3014 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3018 Set[proto].insertAfter = function (el) {
3019 var i = this.items[length];
3021 this.items[i].insertAfter(el);
3025 Set[proto].getBBox = function () {
3030 for (var i = this.items[length]; i--;) {
3031 var box = this.items[i].getBBox();
3034 w[push](box.x + box.width);
3035 h[push](box.y + box.height);
3037 x = mmin[apply](0, x);
3038 y = mmin[apply](0, y);
3042 width: mmax[apply](0, w) - x,
3043 height: mmax[apply](0, h) - y
3047 R.registerFont = function (font) {
3051 this.fonts = this.fonts || {};
3057 family = font.face["font-family"];
3058 for (var prop in font.face) if (font.face[has](prop)) {
3059 fontcopy.face[prop] = font.face[prop];
3061 if (this.fonts[family]) {
3062 this.fonts[family][push](fontcopy);
3064 this.fonts[family] = [fontcopy];
3067 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3068 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3069 var path = font.glyphs[glyph];
3070 fontcopy.glyphs[glyph] = {
3073 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3074 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3078 for (var k in path.k) if (path[has](k)) {
3079 fontcopy.glyphs[glyph].k[k] = path.k[k];
3086 Paper[proto].getFont = function (family, weight, style, stretch) {
3087 stretch = stretch || "normal";
3088 style = style || "normal";
3089 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3090 var font = R.fonts[family];
3092 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3093 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3094 if (name.test(fontName)) {
3095 font = R.fonts[fontName];
3102 for (var i = 0, ii = font[length]; i < ii; i++) {
3104 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3111 Paper[proto].print = function (x, y, string, font, size, origin) {
3112 origin = origin || "middle"; // baseline|middle
3113 var out = this.set(),
3114 letters = (string + E)[split](E),
3118 R.is(font, "string") && (font = this.getFont(font));
3120 scale = (size || 16) / font.face["units-per-em"];
3121 var bb = font.face.bbox.split(separator),
3123 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3124 for (var i = 0, ii = letters[length]; i < ii; i++) {
3125 var prev = i && font.glyphs[letters[i - 1]] || {},
3126 curr = font.glyphs[letters[i]];
3127 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3128 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3130 out.scale(scale, scale, top, height).translate(x - top, y - height);
3135 R.format = function (token) {
3136 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3138 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3139 return args[++i] == null ? E : args[i];
3143 R.ninja = function () {
3144 var r = win.Raphael, u;
3145 if (oldRaphael.was) {
3146 win.Raphael = oldRaphael.is;
3156 R.el = Element[proto];