2 * Raphael 1.4.1 - JavaScript Vector Library
4 * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
8 Raphael = (function () {
10 if (R.is(arguments[0], array)) {
12 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
14 for (var i = 0, ii = a[length]; i < ii; i++) {
16 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
20 return create[apply](R, arguments);
23 var separator = /[, ]+/,
24 elements = /^(circle|rect|path|ellipse|text|image)$/,
26 has = "hasOwnProperty",
30 was: Object[proto][has].call(win, "Raphael"),
33 Paper = function () {},
34 appendChild = "appendChild",
37 supportsTouch = "createTouch" in doc,
41 events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
43 mousedown: "touchstart",
44 mousemove: "touchmove",
49 lowerCase = String[proto].toLowerCase,
56 toString = "toString",
58 objectToString = Object[proto][toString],
63 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
64 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+(?:\s*,\s*[\d\.]+)?)\s*\)|rgba?\(\s*([\d\.]+%\s*,\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,
66 setAttribute = "setAttribute",
69 ms = " progid:DXImageTransform.Microsoft",
70 upperCase = String[proto].toUpperCase,
71 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", 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},
72 availableAnimAttrs = {along: "along", blur: nu, "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},
74 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
75 if (R.type == "VML") {
76 var d = doc.createElement("div");
77 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
78 if (d.childNodes[length] != 2) {
83 R.svg = !(R.vml = R.type == "VML");
84 Paper[proto] = R[proto];
88 R.is = function (o, type) {
89 type = lowerCase.call(type);
90 return (type == "object" && o === Object(o)) ||
91 (type == "undefined" && typeof o == type) ||
92 (type == "null" && o == null) ||
93 lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
96 R.setWindow = function (newwin) {
101 var toHex = function (color) {
103 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
104 var trim = /^\s+|\s+$/g;
105 toHex = cacher(function (color) {
107 color = (color + E)[rp](trim, E);
109 var docum = new win.ActiveXObject("htmlfile");
110 docum.write("<body>");
114 bod = win.createPopup().document.body;
116 var range = bod.createTextRange();
118 bod.style.color = color;
119 var value = range.queryCommandValue("ForeColor");
120 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
121 return "#" + ("000000" + value[toString](16)).slice(-6);
127 var i = doc.createElement("i");
128 i.title = "Rapha\xebl Colour Picker";
129 i.style.display = "none";
130 doc.body[appendChild](i);
131 toHex = cacher(function (color) {
132 i.style.color = color;
133 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
138 var hsbtoString = function () {
139 return "hsb(" + [this.h, this.s, this.b] + ")";
141 rgbtoString = function () {
144 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
145 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
153 if (brightness == 0) {
154 return {r: 0, g: 0, b: 0, hex: "#000"};
156 if (hue > 1 || saturation > 1 || brightness > 1) {
163 p = brightness * (1 - saturation),
164 q = brightness * (1 - (saturation * f)),
165 t = brightness * (1 - (saturation * (1 - f)));
166 red = [brightness, q, p, p, t, brightness, brightness][i];
167 green = [t, brightness, brightness, q, p, p, t][i];
168 blue = [p, p, t, brightness, brightness, q, p][i];
172 var rgb = {r: red, g: green, b: blue, toString: rgbtoString},
173 r = (~~red)[toString](16),
174 g = (~~green)[toString](16),
175 b = (~~blue)[toString](16);
179 rgb.hex = "#" + r + g + b;
182 R.rgb2hsb = cacher(function (red, green, blue) {
183 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
188 if (R.is(red, string)) {
189 var clr = R.getRGB(red);
194 if (red > 1 || green > 1 || blue > 1) {
199 var max = mmax(red, green, blue),
200 min = mmin(red, green, blue),
205 return {h: 0, s: 0, b: max};
207 var delta = (max - min);
208 saturation = delta / max;
210 hue = (green - blue) / delta;
211 } else if (green == max) {
212 hue = 2 + ((blue - red) / delta);
214 hue = 4 + ((red - green) / delta);
220 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
222 var p2s = /,?([achlmqrstvxz]),?/gi,
223 commaSpaces = /\s*,\s*/,
224 hsrg = {hs: 1, rg: 1};
225 R._path2string = function () {
226 return this.join(",")[rp](p2s, "$1");
228 function cacher(f, scope, postprocessor) {
230 var arg = Array[proto].slice.call(arguments, 0),
231 args = arg[join]("\u25ba"),
232 cache = newf.cache = newf.cache || {},
233 count = newf.count = newf.count || [];
234 if (cache[has](args)) {
235 return postprocessor ? postprocessor(cache[args]) : cache[args];
237 count[length] >= 1e3 && delete cache[count.shift()];
239 cache[args] = f[apply](scope, arg);
240 return postprocessor ? postprocessor(cache[args]) : cache[args];
245 R.getRGB = cacher(function (colour) {
246 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
247 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
249 if (colour == "none") {
250 return {r: -1, g: -1, b: -1, hex: "none"};
252 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
259 rgb = colour.match(colourRegExp);
262 blue = toInt(rgb[2].substring(5), 16);
263 green = toInt(rgb[2].substring(3, 5), 16);
264 red = toInt(rgb[2].substring(1, 3), 16);
267 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
268 green = toInt((t = rgb[3].charAt(2)) + t, 16);
269 red = toInt((t = rgb[3].charAt(1)) + t, 16);
272 rgb = rgb[4][split](commaSpaces);
273 red = toFloat(rgb[0]);
274 green = toFloat(rgb[1]);
275 blue = toFloat(rgb[2]);
276 opacity = toFloat(rgb[3]);
279 rgb = rgb[5][split](commaSpaces);
280 red = toFloat(rgb[0]) * 2.55;
281 green = toFloat(rgb[1]) * 2.55;
282 blue = toFloat(rgb[2]) * 2.55;
283 opacity = toFloat(rgb[3]);
286 rgb = rgb[6][split](commaSpaces);
287 red = toFloat(rgb[0]);
288 green = toFloat(rgb[1]);
289 blue = toFloat(rgb[2]);
290 return R.hsb2rgb(red, green, blue);
293 rgb = rgb[7][split](commaSpaces);
294 red = toFloat(rgb[0]) * 2.55;
295 green = toFloat(rgb[1]) * 2.55;
296 blue = toFloat(rgb[2]) * 2.55;
297 return R.hsb2rgb(red, green, blue);
299 rgb = {r: red, g: green, b: blue};
300 var r = (~~red)[toString](16),
301 g = (~~green)[toString](16),
302 b = (~~blue)[toString](16);
306 rgb.hex = "#" + r + g + b;
307 isFinite(toFloat(opacity)) && (rgb.o = opacity);
310 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
312 R.getColor = function (value) {
313 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
314 rgb = this.hsb2rgb(start.h, start.s, start.b);
319 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
323 R.getColor.reset = function () {
327 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
328 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
329 R.parsePathString = cacher(function (pathString) {
333 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
335 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
336 data = pathClone(pathString);
339 (pathString + E)[rp](pathCommand, function (a, b, c) {
341 name = lowerCase.call(b);
342 c[rp](pathValues, function (a, b) {
343 b && params[push](+b);
345 if (name == "m" && params[length] > 2) {
346 data[push]([b][concat](params.splice(0, 2)));
348 b = b == "m" ? "l" : "L";
350 while (params[length] >= paramCounts[name]) {
351 data[push]([b][concat](params.splice(0, paramCounts[name])));
352 if (!paramCounts[name]) {
358 data[toString] = R._path2string;
361 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
363 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
364 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
365 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
366 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
367 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
368 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
369 ax = (1 - t) * p1x + t * c1x,
370 ay = (1 - t) * p1y + t * c1y,
371 cx = (1 - t) * c2x + t * p2x,
372 cy = (1 - t) * c2y + t * p2y,
373 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
374 (mx > nx || my < ny) && (alpha += 180);
375 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}, alpha: alpha};
377 var pathDimensions = cacher(function (path) {
379 return {x: 0, y: 0, width: 0, height: 0};
381 path = path2curve(path);
387 for (var i = 0, ii = path[length]; i < ii; i++) {
395 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
396 X = X[concat](dim.min.x, dim.max.x);
397 Y = Y[concat](dim.min.y, dim.max.y);
402 var xmin = mmin[apply](0, X),
403 ymin = mmin[apply](0, Y);
407 width: mmax[apply](0, X) - xmin,
408 height: mmax[apply](0, Y) - ymin
411 pathClone = function (pathArray) {
413 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
414 pathArray = R.parsePathString(pathArray);
416 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
418 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
419 res[i][j] = pathArray[i][j];
422 res[toString] = R._path2string;
425 pathToRelative = cacher(function (pathArray) {
426 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
427 pathArray = R.parsePathString(pathArray);
435 if (pathArray[0][0] == "M") {
441 res[push](["M", x, y]);
443 for (var i = start, ii = pathArray[length]; i < ii; i++) {
446 if (pa[0] != lowerCase.call(pa[0])) {
447 r[0] = lowerCase.call(pa[0]);
455 r[6] = +(pa[6] - x).toFixed(3);
456 r[7] = +(pa[7] - y).toFixed(3);
459 r[1] = +(pa[1] - y).toFixed(3);
465 for (var j = 1, jj = pa[length]; j < jj; j++) {
466 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
475 for (var k = 0, kk = pa[length]; k < kk; k++) {
479 var len = res[i][length];
486 x += +res[i][len - 1];
489 y += +res[i][len - 1];
492 x += +res[i][len - 2];
493 y += +res[i][len - 1];
496 res[toString] = R._path2string;
499 pathToAbsolute = cacher(function (pathArray) {
500 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
501 pathArray = R.parsePathString(pathArray);
509 if (pathArray[0][0] == "M") {
510 x = +pathArray[0][1];
511 y = +pathArray[0][2];
515 res[0] = ["M", x, y];
517 for (var i = start, ii = pathArray[length]; i < ii; i++) {
520 if (pa[0] != upperCase.call(pa[0])) {
521 r[0] = upperCase.call(pa[0]);
542 for (var j = 1, jj = pa[length]; j < jj; j++) {
543 r[j] = +pa[j] + ((j % 2) ? x : y);
547 for (var k = 0, kk = pa[length]; k < kk; k++) {
563 x = res[i][res[i][length] - 2];
564 y = res[i][res[i][length] - 1];
567 res[toString] = R._path2string;
570 l2c = function (x1, y1, x2, y2) {
571 return [x1, y1, x2, y2, x2, y2];
573 q2c = function (x1, y1, ax, ay, x2, y2) {
585 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
586 // for more information of where this math came from visit:
587 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
589 _120 = PI * 120 / 180,
590 rad = PI / 180 * (+angle || 0),
593 rotate = cacher(function (x, y, rad) {
594 var X = x * math.cos(rad) - y * math.sin(rad),
595 Y = x * math.sin(rad) + y * math.cos(rad);
599 xy = rotate(x1, y1, -rad);
602 xy = rotate(x2, y2, -rad);
605 var cos = math.cos(PI / 180 * angle),
606 sin = math.sin(PI / 180 * angle),
609 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
617 k = (large_arc_flag == sweep_flag ? -1 : 1) *
618 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
619 cx = k * rx * y / ry + (x1 + x2) / 2,
620 cy = k * -ry * x / rx + (y1 + y2) / 2,
621 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
622 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
624 f1 = x1 < cx ? PI - f1 : f1;
625 f2 = x2 < cx ? PI - f2 : f2;
626 f1 < 0 && (f1 = PI * 2 + f1);
627 f2 < 0 && (f2 = PI * 2 + f2);
628 if (sweep_flag && f1 > f2) {
631 if (!sweep_flag && f2 > f1) {
641 if (math.abs(df) > _120) {
645 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
646 x2 = cx + rx * math.cos(f2);
647 y2 = cy + ry * math.sin(f2);
648 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
651 var c1 = math.cos(f1),
655 t = math.tan(df / 4),
659 m2 = [x1 + hx * s1, y1 - hy * c1],
660 m3 = [x2 + hx * s2, y2 - hy * c2],
662 m2[0] = 2 * m1[0] - m2[0];
663 m2[1] = 2 * m1[1] - m2[1];
665 return [m2, m3, m4][concat](res);
667 res = [m2, m3, m4][concat](res)[join]()[split](",");
669 for (var i = 0, ii = res[length]; i < ii; i++) {
670 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
675 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
678 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
679 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
682 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
683 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
684 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
686 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
687 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
691 math.abs(t1) > 1e12 && (t1 = .5);
692 math.abs(t2) > 1e12 && (t2 = .5);
693 if (t1 > 0 && t1 < 1) {
694 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
698 if (t2 > 0 && t2 < 1) {
699 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
703 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
704 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
706 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
707 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
708 math.abs(t1) > 1e12 && (t1 = .5);
709 math.abs(t2) > 1e12 && (t2 = .5);
710 if (t1 > 0 && t1 < 1) {
711 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
715 if (t2 > 0 && t2 < 1) {
716 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
721 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
722 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
725 path2curve = cacher(function (path, path2) {
726 var p = pathToAbsolute(path),
727 p2 = path2 && pathToAbsolute(path2),
728 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
729 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
730 processPath = function (path, d) {
733 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
735 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
742 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
745 nx = d.x + (d.x - (d.bx || d.x));
746 ny = d.y + (d.y - (d.by || d.y));
747 path = ["C", nx, ny][concat](path.slice(1));
750 d.qx = d.x + (d.x - (d.qx || d.x));
751 d.qy = d.y + (d.y - (d.qy || d.y));
752 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
757 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
760 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
763 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
766 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
769 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
774 fixArc = function (pp, i) {
775 if (pp[i][length] > 7) {
779 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
782 ii = mmax(p[length], p2 && p2[length] || 0);
785 fixM = function (path1, path2, a1, a2, i) {
786 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
787 path2.splice(i, 0, ["M", a2.x, a2.y]);
792 ii = mmax(p[length], p2 && p2[length] || 0);
795 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
796 p[i] = processPath(p[i], attrs);
798 p2 && (p2[i] = processPath(p2[i], attrs2));
800 fixM(p, p2, attrs, attrs2, i);
801 fixM(p2, p, attrs2, attrs, i);
804 seglen = seg[length],
805 seg2len = p2 && seg2[length];
806 attrs.x = seg[seglen - 2];
807 attrs.y = seg[seglen - 1];
808 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
809 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
810 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
811 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
812 attrs2.x = p2 && seg2[seg2len - 2];
813 attrs2.y = p2 && seg2[seg2len - 1];
815 return p2 ? [p, p2] : p;
817 parseDots = cacher(function (gradient) {
819 for (var i = 0, ii = gradient[length]; i < ii; i++) {
821 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
822 dot.color = R.getRGB(par[1]);
823 if (dot.color.error) {
826 dot.color = dot.color.hex;
827 par[2] && (dot.offset = par[2] + "%");
830 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
831 if (!dots[i].offset) {
832 var start = toFloat(dots[i - 1].offset || 0),
834 for (var j = i + 1; j < ii; j++) {
835 if (dots[j].offset) {
836 end = dots[j].offset;
845 var d = (end - start) / (j - i + 1);
848 dots[i].offset = start + "%";
854 getContainer = function (x, y, w, h) {
856 if (R.is(x, string) || R.is(x, "object")) {
857 container = R.is(x, string) ? doc.getElementById(x) : x;
858 if (container.tagName) {
861 container: container,
862 width: container.style.pixelWidth || container.offsetWidth,
863 height: container.style.pixelHeight || container.offsetHeight
866 return {container: container, width: y, height: w};
870 return {container: 1, x: x, y: y, width: w, height: h};
873 plugins = function (con, add) {
875 for (var prop in add) {
876 if (add[has](prop) && !(prop in con)) {
877 switch (typeof add[prop]) {
880 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
884 con[prop] = con[prop] || {};
885 plugins.call(this, con[prop], add[prop]);
888 con[prop] = add[prop];
894 tear = function (el, paper) {
895 el == paper.top && (paper.top = el.prev);
896 el == paper.bottom && (paper.bottom = el.next);
897 el.next && (el.next.prev = el.prev);
898 el.prev && (el.prev.next = el.next);
900 tofront = function (el, paper) {
901 if (paper.top === el) {
910 toback = function (el, paper) {
911 if (paper.bottom === el) {
915 el.next = paper.bottom;
917 paper.bottom.prev = el;
920 insertafter = function (el, el2, paper) {
922 el2 == paper.top && (paper.top = el);
923 el2.next && (el2.next.prev = el);
928 insertbefore = function (el, el2, paper) {
930 el2 == paper.bottom && (paper.bottom = el);
931 el2.prev && (el2.prev.next = el);
936 removed = function (methodname) {
938 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
941 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
945 Paper[proto].svgns = "http://www.w3.org/2000/svg";
946 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
947 round = function (num) {
948 return +num + (~~num === num) * .5;
950 var $ = function (el, attr) {
952 for (var key in attr) {
953 if (attr[has](key)) {
954 el[setAttribute](key, attr[key] + E);
958 el = doc.createElementNS(Paper[proto].svgns, el);
959 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
963 R[toString] = function () {
964 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
966 var thePath = function (pathString, SVG) {
968 SVG.canvas && SVG.canvas[appendChild](el);
969 var p = new Element(el, SVG);
971 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
974 var addGradientFill = function (o, gradient, SVG) {
978 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
983 var dir = ((fy > .5) * 2 - 1);
984 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
985 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
987 (fy = fy.toFixed(5) - 1e-5 * dir);
991 gradient = gradient[split](/\s*\-\s*/);
992 if (type == "linear") {
993 var angle = gradient.shift();
994 angle = -toFloat(angle);
998 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
999 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1002 if (vector[2] < 0) {
1003 vector[0] = -vector[2];
1006 if (vector[3] < 0) {
1007 vector[1] = -vector[3];
1011 var dots = parseDots(gradient);
1015 var id = o.getAttribute(fillString);
1016 id = id.match(/^url\(#(.*)\)$/);
1017 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1019 var el = $(type + "Gradient");
1020 el.id = "r" + (R._id++)[toString](36);
1021 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1022 SVG.defs[appendChild](el);
1023 for (var i = 0, ii = dots[length]; i < ii; i++) {
1024 var stop = $("stop");
1026 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1027 "stop-color": dots[i].color || "#fff"
1029 el[appendChild](stop);
1032 fill: "url(#" + el.id + ")",
1041 var updatePosition = function (o) {
1042 var bbox = o.getBBox();
1043 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1045 var setFillAndStroke = function (o, params) {
1052 "-..": [3, 1, 1, 1, 1, 1],
1056 "- .": [4, 3, 1, 3],
1057 "--.": [8, 3, 1, 3],
1058 "--..": [8, 3, 1, 3, 1, 3]
1063 addDashes = function (o, value) {
1064 value = dasharray[lowerCase.call(value)];
1066 var width = o.attrs["stroke-width"] || "1",
1067 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1069 var i = value[length];
1071 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1073 $(node, {"stroke-dasharray": dashes[join](",")});
1076 params[has]("rotation") && (rot = params.rotation);
1077 var rotxy = (rot + E)[split](separator);
1078 if (!(rotxy.length - 1)) {
1081 rotxy[1] = +rotxy[1];
1082 rotxy[2] = +rotxy[2];
1084 toFloat(rot) && o.rotate(0, true);
1085 for (var att in params) {
1086 if (params[has](att)) {
1087 if (!availableAttrs[has](att)) {
1090 var value = params[att];
1097 o.rotate(value, true);
1102 var pn = node.parentNode;
1103 if (lowerCase.call(pn.tagName) != "a") {
1105 pn.insertBefore(hl, node);
1106 hl[appendChild](node);
1109 pn.setAttributeNS(o.paper.xlink, att, value);
1112 node.style.cursor = value;
1115 var rect = (value + E)[split](separator);
1116 if (rect[length] == 4) {
1117 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1118 var el = $("clipPath"),
1120 el.id = "r" + (R._id++)[toString](36);
1127 el[appendChild](rc);
1128 o.paper.defs[appendChild](el);
1129 $(node, {"clip-path": "url(#" + el.id + ")"});
1133 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1134 clip && clip.parentNode.removeChild(clip);
1135 $(node, {"clip-path": E});
1140 if (o.type == "path") {
1141 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1145 node[setAttribute](att, value);
1154 value = -attrs.x - (attrs.width || 0);
1157 if (att == "rx" && o.type == "rect") {
1161 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1162 node[setAttribute](att, round(value));
1163 o.pattern && updatePosition(o);
1166 node[setAttribute](att, value);
1175 value = -attrs.y - (attrs.height || 0);
1178 if (att == "ry" && o.type == "rect") {
1182 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1183 node[setAttribute](att, round(value));
1184 o.pattern && updatePosition(o);
1187 if (o.type == "rect") {
1188 $(node, {rx: value, ry: value});
1190 node[setAttribute](att, value);
1194 if (o.type == "image") {
1195 node.setAttributeNS(o.paper.xlink, "href", value);
1198 case "stroke-width":
1199 node.style.strokeWidth = value;
1200 // Need following line for Firefox
1201 node[setAttribute](att, value);
1202 if (attrs["stroke-dasharray"]) {
1203 addDashes(o, attrs["stroke-dasharray"]);
1206 case "stroke-dasharray":
1207 addDashes(o, value);
1210 var xy = (value + E)[split](separator);
1211 xy[0] = +xy[0] || 0;
1212 xy[1] = +xy[1] || 0;
1217 translate.call(o, xy[0], xy[1]);
1220 xy = (value + E)[split](separator);
1221 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1224 var isURL = (value + E).match(ISURL);
1227 var ig = $("image");
1228 el.id = "r" + (R._id++)[toString](36);
1229 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1230 $(ig, {x: 0, y: 0});
1231 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1232 el[appendChild](ig);
1234 var img = doc.createElement("img");
1235 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1236 img.onload = function () {
1237 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1238 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1239 doc.body.removeChild(this);
1242 doc.body[appendChild](img);
1244 o.paper.defs[appendChild](el);
1245 node.style.fill = "url(#" + el.id + ")";
1246 $(node, {fill: "url(#" + el.id + ")"});
1248 o.pattern && updatePosition(o);
1251 var clr = R.getRGB(value);
1253 delete params.gradient;
1254 delete attrs.gradient;
1255 !R.is(attrs.opacity, "undefined") &&
1256 R.is(params.opacity, "undefined") &&
1257 $(node, {opacity: attrs.opacity});
1258 !R.is(attrs["fill-opacity"], "undefined") &&
1259 R.is(params["fill-opacity"], "undefined") &&
1260 $(node, {"fill-opacity": attrs["fill-opacity"]});
1261 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1262 attrs.gradient = value;
1263 attrs.fill = "none";
1266 clr[has]("o") && $(node, {"fill-opacity": clr.o / 100});
1268 clr = R.getRGB(value);
1269 node[setAttribute](att, clr.hex);
1270 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o / 100});
1273 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1276 case "fill-opacity":
1277 if (attrs.gradient) {
1278 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1280 var stops = gradient.getElementsByTagName("stop");
1281 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1286 att == "font-size" && (value = toInt(value, 10) + "px");
1287 var cssrule = att[rp](/(\-.)/g, function (w) {
1288 return upperCase.call(w.substring(1));
1290 node.style[cssrule] = value;
1291 // Need following line for Firefox
1292 node[setAttribute](att, value);
1298 tuneText(o, params);
1300 o.rotate(rotxy.join(S));
1302 toFloat(rot) && o.rotate(rot, true);
1306 tuneText = function (el, params) {
1307 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1312 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1314 if (params[has]("text")) {
1315 a.text = params.text;
1316 while (node.firstChild) {
1317 node.removeChild(node.firstChild);
1319 var texts = (params.text + E)[split]("\n");
1320 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1321 var tspan = $("tspan");
1322 i && $(tspan, {dy: fontSize * leading, x: a.x});
1323 tspan[appendChild](doc.createTextNode(texts[i]));
1324 node[appendChild](tspan);
1327 texts = node.getElementsByTagName("tspan");
1328 for (i = 0, ii = texts[length]; i < ii; i++) {
1329 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1333 var bb = el.getBBox(),
1334 dif = a.y - (bb.y + bb.height / 2);
1335 dif && isFinite(dif) && $(node, {y: a.y + dif});
1337 Element = function (node, svg) {
1343 node.raphael = this;
1345 this.attrs = this.attrs || {};
1346 this.transformations = []; // rotate, translate, scale
1350 rt: {deg: 0, cx: 0, cy: 0},
1354 !svg.bottom && (svg.bottom = this);
1355 this.prev = svg.top;
1356 svg.top && (svg.top.next = this);
1360 Element[proto].rotate = function (deg, cx, cy) {
1366 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1368 return this._.rt.deg;
1370 var bbox = this.getBBox();
1371 deg = (deg + E)[split](separator);
1372 if (deg[length] - 1) {
1373 cx = toFloat(deg[1]);
1374 cy = toFloat(deg[2]);
1376 deg = toFloat(deg[0]);
1378 this._.rt.deg = deg;
1380 this._.rt.deg += deg;
1382 (cy == null) && (cx = null);
1385 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1386 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1387 if (this._.rt.deg) {
1388 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1389 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1391 this.transformations[0] = E;
1392 this.clip && $(this.clip, {transform: E});
1394 $(this.node, {transform: this.transformations[join](S)});
1397 Element[proto].hide = function () {
1398 !this.removed && (this.node.style.display = "none");
1401 Element[proto].show = function () {
1402 !this.removed && (this.node.style.display = "");
1405 Element[proto].remove = function () {
1409 tear(this, this.paper);
1410 this.node.parentNode.removeChild(this.node);
1411 for (var i in this) {
1414 this.removed = true;
1416 Element[proto].getBBox = function () {
1420 if (this.type == "path") {
1421 return pathDimensions(this.attrs.path);
1423 if (this.node.style.display == "none") {
1429 bbox = this.node.getBBox();
1431 // Firefox 3.0.x plays badly here
1435 if (this.type == "text") {
1436 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1437 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1438 var bb = this.node.getExtentOfChar(i);
1439 (bb.y < bbox.y) && (bbox.y = bb.y);
1440 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1441 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1444 hide && this.hide();
1447 Element[proto].attr = function (name, value) {
1453 for (var i in this.attrs) if (this.attrs[has](i)) {
1454 res[i] = this.attrs[i];
1456 this._.rt.deg && (res.rotation = this.rotate());
1457 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1458 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1461 if (value == null && R.is(name, string)) {
1462 if (name == "translation") {
1463 return translate.call(this);
1465 if (name == "rotation") {
1466 return this.rotate();
1468 if (name == "scale") {
1469 return this.scale();
1471 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1472 return this.attrs.gradient;
1474 return this.attrs[name];
1476 if (value == null && R.is(name, array)) {
1478 for (var j = 0, jj = name.length; j < jj; j++) {
1479 values[name[j]] = this.attr(name[j]);
1483 if (value != null) {
1485 params[name] = value;
1486 setFillAndStroke(this, params);
1487 } else if (name != null && R.is(name, "object")) {
1488 setFillAndStroke(this, name);
1492 Element[proto].toFront = function () {
1496 this.node.parentNode[appendChild](this.node);
1497 var svg = this.paper;
1498 svg.top != this && tofront(this, svg);
1501 Element[proto].toBack = function () {
1505 if (this.node.parentNode.firstChild != this.node) {
1506 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1507 toback(this, this.paper);
1508 var svg = this.paper;
1512 Element[proto].insertAfter = function (element) {
1516 var node = element.node;
1517 if (node.nextSibling) {
1518 node.parentNode.insertBefore(this.node, node.nextSibling);
1520 node.parentNode[appendChild](this.node);
1522 insertafter(this, element, this.paper);
1525 Element[proto].insertBefore = function (element) {
1529 var node = element.node;
1530 node.parentNode.insertBefore(this.node, node);
1531 insertbefore(this, element, this.paper);
1534 Element[proto].blur = function (size) {
1535 // Experimental. No Safari support. Use it on your own risk.
1538 var fltr = $("filter"),
1539 blur = $("feGaussianBlur");
1540 t.attrs.blur = size;
1541 fltr.id = "r" + (R._id++)[toString](36);
1542 $(blur, {stdDeviation: +size || 1.5});
1543 fltr.appendChild(blur);
1544 t.paper.defs.appendChild(fltr);
1546 $(t.node, {filter: "url(#" + fltr.id + ")"});
1549 t._blur.parentNode.removeChild(t._blur);
1551 delete t.attrs.blur;
1553 t.node.removeAttribute("filter");
1556 var theCircle = function (svg, x, y, r) {
1559 var el = $("circle");
1560 svg.canvas && svg.canvas[appendChild](el);
1561 var res = new Element(el, svg);
1562 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1563 res.type = "circle";
1567 var theRect = function (svg, x, y, w, h, r) {
1571 svg.canvas && svg.canvas[appendChild](el);
1572 var res = new Element(el, svg);
1573 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1578 var theEllipse = function (svg, x, y, rx, ry) {
1581 var el = $("ellipse");
1582 svg.canvas && svg.canvas[appendChild](el);
1583 var res = new Element(el, svg);
1584 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1585 res.type = "ellipse";
1589 var theImage = function (svg, src, x, y, w, h) {
1590 var el = $("image");
1591 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1592 el.setAttributeNS(svg.xlink, "href", src);
1593 svg.canvas && svg.canvas[appendChild](el);
1594 var res = new Element(el, svg);
1595 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1599 var theText = function (svg, x, y, text) {
1601 $(el, {x: x, y: y, "text-anchor": "middle"});
1602 svg.canvas && svg.canvas[appendChild](el);
1603 var res = new Element(el, svg);
1604 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1606 setFillAndStroke(res, res.attrs);
1609 var setSize = function (width, height) {
1610 this.width = width || this.width;
1611 this.height = height || this.height;
1612 this.canvas[setAttribute]("width", this.width);
1613 this.canvas[setAttribute]("height", this.height);
1616 var create = function () {
1617 var con = getContainer[apply](0, arguments),
1618 container = con && con.container,
1622 height = con.height;
1624 throw new Error("SVG container not found.");
1626 var cnvs = $("svg");
1629 width = width || 512;
1630 height = height || 342;
1632 xmlns: "http://www.w3.org/2000/svg",
1637 if (container == 1) {
1638 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1639 doc.body[appendChild](cnvs);
1641 if (container.firstChild) {
1642 container.insertBefore(cnvs, container.firstChild);
1644 container[appendChild](cnvs);
1647 container = new Paper;
1648 container.width = width;
1649 container.height = height;
1650 container.canvas = cnvs;
1651 plugins.call(container, container, R.fn);
1655 Paper[proto].clear = function () {
1656 var c = this.canvas;
1657 while (c.firstChild) {
1658 c.removeChild(c.firstChild);
1660 this.bottom = this.top = null;
1661 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1662 c[appendChild](this.desc);
1663 c[appendChild](this.defs = $("defs"));
1665 Paper[proto].remove = function () {
1666 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1667 for (var i in this) {
1668 this[i] = removed(i);
1675 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1676 bites = /([clmz]),?([^clmz]*)/gi,
1677 val = /-?[^,\s-]+/g,
1678 coordsize = 1e3 + S + 1e3,
1680 pathlike = {path: 1, rect: 1},
1681 path2vml = function (path) {
1682 var total = /[ahqstv]/ig,
1683 command = pathToAbsolute;
1684 (path + E).match(total) && (command = path2curve);
1686 if (command == pathToAbsolute && !(path + E).match(total)) {
1687 var res = (path + E)[rp](bites, function (all, command, args) {
1689 isMove = lowerCase.call(command) == "m",
1691 args[rp](val, function (value) {
1692 if (isMove && vals[length] == 2) {
1693 res += vals + map[command == "m" ? "l" : "L"];
1696 vals[push](round(value * zoom));
1702 var pa = command(path), p, r;
1704 for (var i = 0, ii = pa[length]; i < ii; i++) {
1706 r = lowerCase.call(pa[i][0]);
1707 r == "z" && (r = "x");
1708 for (var j = 1, jj = p[length]; j < jj; j++) {
1709 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1713 return res[join](S);
1716 R[toString] = function () {
1717 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1719 thePath = function (pathString, vml) {
1720 var g = createNode("group");
1721 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1722 g.coordsize = vml.coordsize;
1723 g.coordorigin = vml.coordorigin;
1724 var el = createNode("shape"), ol = el.style;
1725 ol.width = vml.width + "px";
1726 ol.height = vml.height + "px";
1727 el.coordsize = coordsize;
1728 el.coordorigin = vml.coordorigin;
1730 var p = new Element(el, g, vml),
1731 attr = {fill: "none", stroke: "#000"};
1732 pathString && (attr.path = pathString);
1733 p.isAbsolute = true;
1737 setFillAndStroke(p, attr);
1738 vml.canvas[appendChild](g);
1741 setFillAndStroke = function (o, params) {
1742 o.attrs = o.attrs || {};
1747 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1750 for (var par in params) if (params[has](par)) {
1751 a[par] = params[par];
1754 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1760 params.href && (node.href = params.href);
1761 params.title && (node.title = params.title);
1762 params.target && (node.target = params.target);
1763 params.cursor && (s.cursor = params.cursor);
1764 "blur" in params && o.blur(params.blur);
1765 if (params.path && o.type == "path" || newpath) {
1766 node.path = path2vml(a.path);
1768 if (params.rotation != null) {
1769 o.rotate(params.rotation, true);
1771 if (params.translation) {
1772 xy = (params.translation + E)[split](separator);
1773 translate.call(o, xy[0], xy[1]);
1774 if (o._.rt.cx != null) {
1775 o._.rt.cx +=+ xy[0];
1776 o._.rt.cy +=+ xy[1];
1777 o.setBox(o.attrs, xy[0], xy[1]);
1781 xy = (params.scale + E)[split](separator);
1782 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1784 if ("clip-rect" in params) {
1785 var rect = (params["clip-rect"] + E)[split](separator);
1786 if (rect[length] == 4) {
1787 rect[2] = +rect[2] + (+rect[0]);
1788 rect[3] = +rect[3] + (+rect[1]);
1789 var div = node.clipRect || doc.createElement("div"),
1791 group = node.parentNode;
1792 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1793 if (!node.clipRect) {
1794 dstyle.position = "absolute";
1797 dstyle.width = o.paper.width + "px";
1798 dstyle.height = o.paper.height + "px";
1799 group.parentNode.insertBefore(div, group);
1800 div[appendChild](group);
1801 node.clipRect = div;
1804 if (!params["clip-rect"]) {
1805 node.clipRect && (node.clipRect.style.clip = E);
1808 if (o.type == "image" && params.src) {
1809 node.src = params.src;
1811 if (o.type == "image" && params.opacity) {
1812 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1813 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1815 params.font && (s.font = params.font);
1816 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1817 params["font-size"] && (s.fontSize = params["font-size"]);
1818 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1819 params["font-style"] && (s.fontStyle = params["font-style"]);
1820 if (params.opacity != null ||
1821 params["stroke-width"] != null ||
1822 params.fill != null ||
1823 params.stroke != null ||
1824 params["stroke-width"] != null ||
1825 params["stroke-opacity"] != null ||
1826 params["fill-opacity"] != null ||
1827 params["stroke-dasharray"] != null ||
1828 params["stroke-miterlimit"] != null ||
1829 params["stroke-linejoin"] != null ||
1830 params["stroke-linecap"] != null) {
1831 node = o.shape || node;
1832 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1834 !fill && (newfill = fill = createNode(fillString));
1835 if ("fill-opacity" in params || "opacity" in params) {
1836 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1837 opacity < 0 && (opacity = 0);
1838 opacity > 1 && (opacity = 1);
1839 fill.opacity = opacity;
1841 params.fill && (fill.on = true);
1842 if (fill.on == null || params.fill == "none") {
1845 if (fill.on && params.fill) {
1846 var isURL = params.fill.match(ISURL);
1848 fill.src = isURL[1];
1851 fill.color = R.getRGB(params.fill).hex;
1853 fill.type = "solid";
1854 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1856 a.gradient = params.fill;
1860 newfill && node[appendChild](fill);
1861 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1863 !stroke && (newstroke = stroke = createNode("stroke"));
1864 if ((params.stroke && params.stroke != "none") ||
1865 params["stroke-width"] ||
1866 params["stroke-opacity"] != null ||
1867 params["stroke-dasharray"] ||
1868 params["stroke-miterlimit"] ||
1869 params["stroke-linejoin"] ||
1870 params["stroke-linecap"]) {
1873 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1874 var strokeColor = R.getRGB(params.stroke);
1875 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
1876 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
1877 var width = (toFloat(params["stroke-width"]) || 1) * .75;
1878 opacity < 0 && (opacity = 0);
1879 opacity > 1 && (opacity = 1);
1880 params["stroke-width"] == null && (width = a["stroke-width"]);
1881 params["stroke-width"] && (stroke.weight = width);
1882 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1883 stroke.opacity = opacity;
1885 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1886 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1887 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1888 if (params["stroke-dasharray"]) {
1892 "-.": "shortdashdot",
1893 "-..": "shortdashdotdot",
1898 "--.": "longdashdot",
1899 "--..": "longdashdotdot"
1901 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1903 newstroke && node[appendChild](stroke);
1905 if (res.type == "text") {
1906 s = res.paper.span.style;
1907 a.font && (s.font = a.font);
1908 a["font-family"] && (s.fontFamily = a["font-family"]);
1909 a["font-size"] && (s.fontSize = a["font-size"]);
1910 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1911 a["font-style"] && (s.fontStyle = a["font-style"]);
1912 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1913 res.W = a.w = res.paper.span.offsetWidth;
1914 res.H = a.h = res.paper.span.offsetHeight;
1916 res.Y = a.y + round(res.H / 2);
1918 // text-anchor emulationm
1919 switch (a["text-anchor"]) {
1921 res.node.style["v-text-align"] = "left";
1922 res.bbx = round(res.W / 2);
1925 res.node.style["v-text-align"] = "right";
1926 res.bbx = -round(res.W / 2);
1929 res.node.style["v-text-align"] = "center";
1934 addGradientFill = function (o, gradient) {
1935 o.attrs = o.attrs || {};
1936 var attrs = o.attrs,
1940 o.attrs.gradient = gradient;
1941 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1946 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1951 gradient = gradient[split](/\s*\-\s*/);
1952 if (type == "linear") {
1953 var angle = gradient.shift();
1954 angle = -toFloat(angle);
1959 var dots = parseDots(gradient);
1963 o = o.shape || o.node;
1964 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
1965 !fill.parentNode && o.appendChild(fill);
1968 fill.method = "none";
1969 fill.color = dots[0].color;
1970 fill.color2 = dots[dots[length] - 1].color;
1972 for (var i = 0, ii = dots[length]; i < ii; i++) {
1973 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1975 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
1976 if (type == "radial") {
1977 fill.type = "gradientradial";
1978 fill.focus = "100%";
1979 fill.focussize = fxfy;
1980 fill.focusposition = fxfy;
1982 fill.type = "gradient";
1983 fill.angle = (270 - angle) % 360;
1988 Element = function (node, group, vml) {
1996 node.raphael = this;
2009 !vml.bottom && (vml.bottom = this);
2010 this.prev = vml.top;
2011 vml.top && (vml.top.next = this);
2015 Element[proto].rotate = function (deg, cx, cy) {
2021 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2023 return this._.rt.deg;
2025 deg = (deg + E)[split](separator);
2026 if (deg[length] - 1) {
2027 cx = toFloat(deg[1]);
2028 cy = toFloat(deg[2]);
2030 deg = toFloat(deg[0]);
2032 this._.rt.deg = deg;
2034 this._.rt.deg += deg;
2036 cy == null && (cx = null);
2039 this.setBox(this.attrs, cx, cy);
2040 this.Group.style.rotation = this._.rt.deg;
2041 // gradient fix for rotation. TODO
2042 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2043 // fill = fill[0] || {};
2044 // var b = ((360 - this._.rt.deg) - 270) % 360;
2045 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2048 Element[proto].setBox = function (params, cx, cy) {
2052 var gs = this.Group.style,
2053 os = (this.shape && this.shape.style) || this.node.style;
2054 params = params || {};
2055 for (var i in params) if (params[has](i)) {
2056 this.attrs[i] = params[i];
2058 cx = cx || this._.rt.cx;
2059 cy = cy || this._.rt.cy;
2060 var attr = this.attrs,
2065 switch (this.type) {
2067 x = attr.cx - attr.r;
2068 y = attr.cy - attr.r;
2072 x = attr.cx - attr.rx;
2073 y = attr.cy - attr.ry;
2080 w = attr.width || 0;
2081 h = attr.height || 0;
2084 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2085 x = attr.x - round(this.W / 2);
2086 y = attr.y - this.H / 2;
2092 if (!this.attrs.path) {
2095 w = this.paper.width;
2096 h = this.paper.height;
2098 var dim = pathDimensions(this.attrs.path);
2108 w = this.paper.width;
2109 h = this.paper.height;
2112 cx = (cx == null) ? x + w / 2 : cx;
2113 cy = (cy == null) ? y + h / 2 : cy;
2114 var left = cx - this.paper.width / 2,
2115 top = cy - this.paper.height / 2, t;
2116 gs.left != (t = left + "px") && (gs.left = t);
2117 gs.top != (t = top + "px") && (gs.top = t);
2118 this.X = pathlike[has](this.type) ? -left : x;
2119 this.Y = pathlike[has](this.type) ? -top : y;
2122 if (pathlike[has](this.type)) {
2123 os.left != (t = -left * zoom + "px") && (os.left = t);
2124 os.top != (t = -top * zoom + "px") && (os.top = t);
2125 } else if (this.type == "text") {
2126 os.left != (t = -left + "px") && (os.left = t);
2127 os.top != (t = -top + "px") && (os.top = t);
2129 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2130 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2131 os.left != (t = x - left + "px") && (os.left = t);
2132 os.top != (t = y - top + "px") && (os.top = t);
2133 os.width != (t = w + "px") && (os.width = t);
2134 os.height != (t = h + "px") && (os.height = t);
2137 Element[proto].hide = function () {
2138 !this.removed && (this.Group.style.display = "none");
2141 Element[proto].show = function () {
2142 !this.removed && (this.Group.style.display = "block");
2145 Element[proto].getBBox = function () {
2149 if (pathlike[has](this.type)) {
2150 return pathDimensions(this.attrs.path);
2153 x: this.X + (this.bbx || 0),
2159 Element[proto].remove = function () {
2163 tear(this, this.paper);
2164 this.node.parentNode.removeChild(this.node);
2165 this.Group.parentNode.removeChild(this.Group);
2166 this.shape && this.shape.parentNode.removeChild(this.shape);
2167 for (var i in this) {
2170 this.removed = true;
2172 Element[proto].attr = function (name, value) {
2178 for (var i in this.attrs) if (this.attrs[has](i)) {
2179 res[i] = this.attrs[i];
2181 this._.rt.deg && (res.rotation = this.rotate());
2182 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2183 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2186 if (value == null && R.is(name, string)) {
2187 if (name == "translation") {
2188 return translate.call(this);
2190 if (name == "rotation") {
2191 return this.rotate();
2193 if (name == "scale") {
2194 return this.scale();
2196 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2197 return this.attrs.gradient;
2199 return this.attrs[name];
2201 if (this.attrs && value == null && R.is(name, array)) {
2202 var ii, values = {};
2203 for (i = 0, ii = name[length]; i < ii; i++) {
2204 values[name[i]] = this.attr(name[i]);
2209 if (value != null) {
2211 params[name] = value;
2213 value == null && R.is(name, "object") && (params = name);
2215 if (params.text && this.type == "text") {
2216 this.node.string = params.text;
2218 setFillAndStroke(this, params);
2219 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2220 addGradientFill(this, params.gradient);
2222 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2226 Element[proto].toFront = function () {
2227 !this.removed && this.Group.parentNode[appendChild](this.Group);
2228 this.paper.top != this && tofront(this, this.paper);
2231 Element[proto].toBack = function () {
2235 if (this.Group.parentNode.firstChild != this.Group) {
2236 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2237 toback(this, this.paper);
2241 Element[proto].insertAfter = function (element) {
2245 if (element.Group.nextSibling) {
2246 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2248 element.Group.parentNode[appendChild](this.Group);
2250 insertafter(this, element, this.paper);
2253 Element[proto].insertBefore = function (element) {
2257 element.Group.parentNode.insertBefore(this.Group, element.Group);
2258 insertbefore(this, element, this.paper);
2261 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2262 Element[proto].blur = function (size) {
2263 var s = this.node.style,
2265 f = f.replace(blurregexp, "");
2267 this.attrs.blur = size;
2268 s.filter = f + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2269 s.margin = Raphael.format("-{0}px 0 0 -{0}px", Math.round(+size || 1.5));
2273 delete this.attrs.blur;
2277 theCircle = function (vml, x, y, r) {
2278 var g = createNode("group"),
2279 o = createNode("oval"),
2281 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2282 g.coordsize = coordsize;
2283 g.coordorigin = vml.coordorigin;
2285 var res = new Element(o, g, vml);
2286 res.type = "circle";
2287 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2291 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2292 vml.canvas[appendChild](g);
2295 function rectPath(x, y, w, h, r) {
2297 return R.format("M{0},{1}l{2},0a{3},{3},0,0,1,{3},{3}l0,{5}a{3},{3},0,0,1,{4},{3}l{6},0a{3},{3},0,0,1,{4},{4}l0,{7}a{3},{3},0,0,1,{3},{4}z", x + r, y, w - r * 2, r, -r, h - r * 2, r * 2 - w, r * 2 - h);
2299 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2302 theRect = function (vml, x, y, w, h, r) {
2303 var path = rectPath(x, y, w, h, r),
2304 res = vml.path(path),
2308 res.W = a.width = w;
2309 res.H = a.height = h;
2315 theEllipse = function (vml, x, y, rx, ry) {
2316 var g = createNode("group"),
2317 o = createNode("oval"),
2319 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2320 g.coordsize = coordsize;
2321 g.coordorigin = vml.coordorigin;
2323 var res = new Element(o, g, vml);
2324 res.type = "ellipse";
2325 setFillAndStroke(res, {stroke: "#000"});
2330 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2331 vml.canvas[appendChild](g);
2334 theImage = function (vml, src, x, y, w, h) {
2335 var g = createNode("group"),
2336 o = createNode("image"),
2338 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2339 g.coordsize = coordsize;
2340 g.coordorigin = vml.coordorigin;
2343 var res = new Element(o, g, vml);
2345 res.attrs.src = src;
2350 res.setBox({x: x, y: y, width: w, height: h});
2351 vml.canvas[appendChild](g);
2354 theText = function (vml, x, y, text) {
2355 var g = createNode("group"),
2356 el = createNode("shape"),
2358 path = createNode("path"),
2360 o = createNode("textpath");
2361 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2362 g.coordsize = coordsize;
2363 g.coordorigin = vml.coordorigin;
2364 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2365 path.textpathok = true;
2366 ol.width = vml.width;
2367 ol.height = vml.height;
2368 o.string = text + E;
2371 el[appendChild](path);
2373 var res = new Element(o, g, vml);
2375 res.textpath = path;
2377 res.attrs.text = text;
2382 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2384 vml.canvas[appendChild](g);
2387 setSize = function (width, height) {
2388 var cs = this.canvas.style;
2389 width == +width && (width += "px");
2390 height == +height && (height += "px");
2393 cs.clip = "rect(0 " + width + " " + height + " 0)";
2397 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2399 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2400 createNode = function (tagName) {
2401 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2404 createNode = function (tagName) {
2405 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2408 create = function () {
2409 var con = getContainer[apply](0, arguments),
2410 container = con.container,
2411 height = con.height,
2417 throw new Error("VML container not found.");
2419 var res = new Paper,
2420 c = res.canvas = doc.createElement("div"),
2424 width = width || 512;
2425 height = height || 342;
2426 width == +width && (width += "px");
2427 height == +height && (height += "px");
2430 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2431 res.coordorigin = "0 0";
2432 res.span = doc.createElement("span");
2433 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2434 c[appendChild](res.span);
2435 cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2436 if (container == 1) {
2437 doc.body[appendChild](c);
2440 cs.position = "absolute";
2442 if (container.firstChild) {
2443 container.insertBefore(c, container.firstChild);
2445 container[appendChild](c);
2448 plugins.call(res, res, R.fn);
2451 Paper[proto].clear = function () {
2452 this.canvas.innerHTML = E;
2453 this.span = doc.createElement("span");
2454 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2455 this.canvas[appendChild](this.span);
2456 this.bottom = this.top = null;
2458 Paper[proto].remove = function () {
2459 this.canvas.parentNode.removeChild(this.canvas);
2460 for (var i in this) {
2461 this[i] = removed(i);
2468 // Safari or Chrome (WebKit) rendering bug workaround method
2469 if ((/^Apple|^Google/).test(win.navigator.vendor) && (!(win.navigator.userAgent.indexOf("Version/4.0") + 1) || win.navigator.platform.slice(0, 2) == "iP")) {
2470 Paper[proto].safari = function () {
2471 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2472 win.setTimeout(function () {rect.remove();});
2475 Paper[proto].safari = function () {};
2479 var preventDefault = function () {
2480 this.returnValue = false;
2482 stopPropagation = function () {
2483 this.cancelBubble = true;
2485 addEvent = (function () {
2486 if (doc.addEventListener) {
2487 return function (obj, type, fn, element) {
2488 var f = function (e) {
2489 if (supportsTouch) {
2490 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2491 if (e.targetTouches[i].target == obj) {
2493 e = e.targetTouches[i];
2494 e.originalEvent = olde;
2499 return fn.call(element, e);
2501 obj.addEventListener(type, f, false);
2502 return function () {
2503 obj.removeEventListener(type, f, false);
2507 } else if (doc.attachEvent) {
2508 return function (obj, type, fn, element) {
2509 var f = function (e) {
2511 e.preventDefault = e.preventDefault || preventDefault;
2512 e.stopPropagation = e.stopPropagation || stopPropagation;
2513 return fn.call(element, e);
2515 obj.attachEvent("on" + type, f);
2516 var detacher = function () {
2517 obj.detachEvent("on" + type, f);
2524 for (var i = events[length]; i--;) {
2525 (function (eventName) {
2526 R[eventName] = Element[proto][eventName] = function (fn) {
2527 if (R.is(fn, "function")) {
2528 var realName = supportsTouch && touchMap[eventName] ? touchMap[eventName] : eventName;
2529 this.events = this.events || [];
2530 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, realName, fn, this)});
2534 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2535 var events = this.events,
2537 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2539 events.splice(l, 1);
2540 !events.length && delete this.events;
2547 Element[proto].hover = function (f_in, f_out) {
2548 return this.mouseover(f_in).mouseout(f_out);
2550 Element[proto].unhover = function (f_in, f_out) {
2551 return this.unmouseover(f_in).unmouseout(f_out);
2553 Element[proto].drag = function (onmove, onstart, onend) {
2555 var el = this.mousedown(function (e) {
2556 (e.originalEvent ? e.originalEvent : e).preventDefault();
2557 this._drag.x = e.clientX;
2558 this._drag.y = e.clientY;
2559 this._drag.id = e.identifier;
2560 onstart && onstart.call(this, e.clientX, e.clientY);
2561 Raphael.mousemove(move).mouseup(up);
2563 move = function (e) {
2566 if (supportsTouch) {
2567 var i = e.touches.length,
2570 touch = e.touches[i];
2571 if (touch.identifier == el._drag.id) {
2574 (e.originalEvent ? e.originalEvent : e).preventDefault();
2581 onmove && onmove.call(el, x - el._drag.x, y - el._drag.y, x, y);
2585 Raphael.unmousemove(move).unmouseup(up);
2586 onend && onend.call(el);
2590 Paper[proto].circle = function (x, y, r) {
2591 return theCircle(this, x || 0, y || 0, r || 0);
2593 Paper[proto].rect = function (x, y, w, h, r) {
2594 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2596 Paper[proto].ellipse = function (x, y, rx, ry) {
2597 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2599 Paper[proto].path = function (pathString) {
2600 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2601 return thePath(R.format[apply](R, arguments), this);
2603 Paper[proto].image = function (src, x, y, w, h) {
2604 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2606 Paper[proto].text = function (x, y, text) {
2607 return theText(this, x || 0, y || 0, text || E);
2609 Paper[proto].set = function (itemsArray) {
2610 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2611 return new Set(itemsArray);
2613 Paper[proto].setSize = setSize;
2614 Paper[proto].top = Paper[proto].bottom = null;
2615 Paper[proto].raphael = R;
2617 return this.x + S + this.y;
2619 Element[proto].resetScale = function () {
2625 this.attrs.scale = "1 1";
2627 Element[proto].scale = function (x, y, cx, cy) {
2631 if (x == null && y == null) {
2646 var bb = this.getBBox(),
2647 rcx = bb.x + bb.width / 2,
2648 rcy = bb.y + bb.height / 2,
2651 cx = (+cx || cx == 0) ? cx : rcx;
2652 cy = (+cy || cy == 0) ? cy : rcy;
2653 var dirx = ~~(x / math.abs(x)),
2654 diry = ~~(y / math.abs(y)),
2655 s = this.node.style,
2656 ncx = cx + (rcx - cx) * kx,
2657 ncy = cy + (rcy - cy) * ky;
2658 switch (this.type) {
2661 var neww = a.width * dirx * kx,
2662 newh = a.height * diry * ky;
2665 r: a.r * mmin(dirx * kx, diry * ky),
2674 rx: a.rx * dirx * kx,
2675 ry: a.ry * diry * ky,
2676 r: a.r * mmin(dirx * kx, diry * ky),
2688 var path = pathToRelative(a.path),
2690 for (var i = 0, ii = path[length]; i < ii; i++) {
2692 P0 = upperCase.call(p[0]);
2693 if (P0 == "M" && skip) {
2699 p[path[i][length] - 2] *= kx;
2700 p[path[i][length] - 1] *= ky;
2703 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2704 } else if (P0 == "H") {
2705 for (var j = 1, jj = p[length]; j < jj; j++) {
2708 } else if (P0 == "V") {
2709 for (j = 1, jj = p[length]; j < jj; j++) {
2713 for (j = 1, jj = p[length]; j < jj; j++) {
2714 p[j] *= (j % 2) ? kx : ky;
2718 var dim2 = pathDimensions(path);
2719 dx = ncx - dim2.x - dim2.width / 2;
2720 dy = ncy - dim2.y - dim2.height / 2;
2723 this.attr({path: path});
2726 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2727 if (this.transformations) {
2728 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2729 this.node[setAttribute]("transform", this.transformations[join](S));
2730 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2731 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2732 this.attr({x: dx, y: dy});
2736 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2737 ", M12=0, M21=0, M22=", diry,
2738 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2739 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2742 if (this.transformations) {
2743 this.transformations[2] = E;
2744 this.node[setAttribute]("transform", this.transformations[join](S));
2748 this.node.filterMatrix = E;
2749 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2752 a.scale = [x, y, cx, cy][join](S);
2758 Element[proto].clone = function () {
2759 var attr = this.attr();
2761 delete attr.translation;
2762 return this.paper[this.type]().attr(attr);
2764 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2767 for (var i = 0; i < 1.001; i+=.001) {
2768 var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2769 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2770 if (len >= length) {
2776 getLengthFactory = function (istotal, subpath) {
2777 return function (path, length, onlystart) {
2778 path = path2curve(path);
2779 var x, y, p, l, sp = "", subpaths = {}, point,
2781 for (var i = 0, ii = path.length; i < ii; i++) {
2787 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2788 if (len + l > length) {
2789 if (subpath && !subpaths.start) {
2790 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2791 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2792 if (onlystart) {return sp;}
2793 subpaths.start = sp;
2794 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2800 if (!istotal && !subpath) {
2801 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2802 return {x: point.x, y: point.y, alpha: point.alpha};
2812 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2813 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2817 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2818 var old = {x: 0, y: 0},
2820 for (var i = 0; i < 1.01; i+=.01) {
2821 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2822 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2827 var getTotalLength = getLengthFactory(1),
2828 getPointAtLength = getLengthFactory(),
2829 getSubpathsAtLength = getLengthFactory(0, 1);
2830 Element[proto].getTotalLength = function () {
2831 if (this.type != "path") {return;}
2832 if (this.node.getTotalLength) {
2833 return this.node.getTotalLength();
2835 return getTotalLength(this.attrs.path);
2837 Element[proto].getPointAtLength = function (length) {
2838 if (this.type != "path") {return;}
2839 return getPointAtLength(this.attrs.path, length);
2841 Element[proto].getSubpath = function (from, to) {
2842 if (this.type != "path") {return;}
2843 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2844 return getSubpathsAtLength(this.attrs.path, from).end;
2846 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2847 return from ? getSubpathsAtLength(a, from).end : a;
2850 // animation easing formulas
2851 R.easing_formulas = {
2852 linear: function (n) {
2859 return pow(n - 1, 3) + 1;
2861 "<>": function (n) {
2864 return pow(n, 3) / 2;
2867 return (pow(n, 3) + 2) / 2;
2869 backIn: function (n) {
2871 return n * n * ((s + 1) * n - s);
2873 backOut: function (n) {
2876 return n * n * ((s + 1) * n + s) + 1;
2878 elastic: function (n) {
2879 if (n == 0 || n == 1) {
2884 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2886 bounce: function (n) {
2895 l = s * n * n + .75;
2897 if (n < (2.5 / p)) {
2899 l = s * n * n + .9375;
2902 l = s * n * n + .984375;
2910 var animationElements = {length : 0},
2911 animation = function () {
2912 var Now = +new Date;
2913 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2914 var e = animationElements[l];
2915 if (e.stop || e.el.removed) {
2916 delete animationElements[l];
2917 animationElements[length]--;
2920 var time = Now - e.start,
2929 callback = e.callback,
2933 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2934 for (var attr in from) if (from[has](attr)) {
2935 switch (availableAnimAttrs[attr]) {
2937 now = pos * ms * diff[attr];
2938 to.back && (now = to.len - now);
2939 var point = getPointAtLength(to[attr], now);
2940 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2943 that.translate(point.x - diff.sx, point.y - diff.sy);
2944 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2947 now = +from[attr] + pos * ms * diff[attr];
2951 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2952 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2953 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2958 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2959 now[i] = [from[attr][i][0]];
2960 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2961 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2963 now[i] = now[i][join](S);
2970 var x = diff[attr][0] * (time - prev),
2971 y = diff[attr][1] * (time - prev);
2977 now = +from[attr][0] + pos * ms * diff[attr][0];
2978 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2981 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);
2987 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2996 that._run && that._run.call(that);
2999 point = getPointAtLength(to.along, to.len * !to.back);
3000 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3001 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3003 (t.x || t.y) && that.translate(-t.x, -t.y);
3004 to.scale && (to.scale = to.scale + E);
3006 delete animationElements[l];
3007 animationElements[length]--;
3008 that.in_animation = null;
3009 R.is(callback, "function") && callback.call(that);
3013 R.svg && that && that.paper && that.paper.safari();
3014 animationElements[length] && win.setTimeout(animation);
3016 upto255 = function (color) {
3017 return mmax(mmin(color, 255), 0);
3019 translate = function (x, y) {
3021 return {x: this._.tx, y: this._.ty, toString: x_y};
3025 switch (this.type) {
3028 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3033 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3036 var path = pathToRelative(this.attrs.path);
3039 this.attr({path: path});
3044 Element[proto].animateWith = function (element, params, ms, easing, callback) {
3045 animationElements[element.id] && (params.start = animationElements[element.id].start);
3046 return this.animate(params, ms, easing, callback);
3048 Element[proto].animateAlong = along();
3049 Element[proto].animateAlongBack = along(1);
3050 function along(isBack) {
3051 return function (path, ms, rotate, callback) {
3052 var params = {back: isBack};
3053 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3054 path && path.constructor == Element && (path = path.attrs.path);
3055 path && (params.along = path);
3056 return this.animate(params, ms, callback);
3059 Element[proto].onAnimation = function (f) {
3063 Element[proto].animate = function (params, ms, easing, callback) {
3064 if (R.is(easing, "function") || !easing) {
3065 callback = easing || null;
3070 for (var attr in params) if (params[has](attr)) {
3071 if (availableAnimAttrs[has](attr)) {
3072 from[attr] = this.attr(attr);
3073 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3074 to[attr] = params[attr];
3075 switch (availableAnimAttrs[attr]) {
3077 var len = getTotalLength(params[attr]),
3078 point = getPointAtLength(params[attr], len * !!params.back),
3079 bb = this.getBBox();
3080 diff[attr] = len / ms;
3085 to.rot = params.rot;
3086 to.back = params.back;
3088 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3091 diff[attr] = (to[attr] - from[attr]) / ms;
3094 from[attr] = R.getRGB(from[attr]);
3095 var toColour = R.getRGB(to[attr]);
3097 r: (toColour.r - from[attr].r) / ms,
3098 g: (toColour.g - from[attr].g) / ms,
3099 b: (toColour.b - from[attr].b) / ms
3103 var pathes = path2curve(from[attr], to[attr]);
3104 from[attr] = pathes[0];
3105 var toPath = pathes[1];
3107 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3108 diff[attr][i] = [0];
3109 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3110 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3115 var values = (params[attr] + E)[split](separator),
3116 from2 = (from[attr] + E)[split](separator);
3119 from[attr] = [0, 0];
3120 diff[attr] = [values[0] / ms, values[1] / ms];
3123 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3124 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3127 params[attr] = values;
3128 from[attr] = (from[attr] + E)[split](separator);
3129 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3132 from[attr] = (from[attr] + E)[split](separator);
3136 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3145 this.in_animation = 1;
3146 animationElements[this.id] = {
3147 start: params.start || +new Date,
3157 ++animationElements[length] == 1 && animation();
3160 Element[proto].stop = function () {
3161 animationElements[this.id] && animationElements[length]--;
3162 delete animationElements[this.id];
3165 Element[proto].translate = function (x, y) {
3166 return this.attr({translation: x + " " + y});
3168 Element[proto][toString] = function () {
3169 return "Rapha\xebl\u2019s object";
3171 R.ae = animationElements;
3174 var Set = function (items) {
3179 for (var i = 0, ii = items[length]; i < ii; i++) {
3180 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3181 this[this.items[length]] = this.items[this.items[length]] = items[i];
3187 Set[proto][push] = function () {
3190 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3191 item = arguments[i];
3192 if (item && (item.constructor == Element || item.constructor == Set)) {
3193 len = this.items[length];
3194 this[len] = this.items[len] = item;
3200 Set[proto].pop = function () {
3201 delete this[this[length]--];
3202 return this.items.pop();
3204 for (var method in Element[proto]) if (Element[proto][has](method)) {
3205 Set[proto][method] = (function (methodname) {
3206 return function () {
3207 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3208 this.items[i][methodname][apply](this.items[i], arguments);
3214 Set[proto].attr = function (name, value) {
3215 if (name && R.is(name, array) && R.is(name[0], "object")) {
3216 for (var j = 0, jj = name[length]; j < jj; j++) {
3217 this.items[j].attr(name[j]);
3220 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3221 this.items[i].attr(name, value);
3226 Set[proto].animate = function (params, ms, easing, callback) {
3227 (R.is(easing, "function") || !easing) && (callback = easing || null);
3228 var len = this.items[length],
3232 callback && (collector = function () {
3233 !--len && callback.call(set);
3235 this.items[--i].animate(params, ms, easing || collector, collector);
3237 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3241 Set[proto].insertAfter = function (el) {
3242 var i = this.items[length];
3244 this.items[i].insertAfter(el);
3248 Set[proto].getBBox = function () {
3253 for (var i = this.items[length]; i--;) {
3254 var box = this.items[i].getBBox();
3257 w[push](box.x + box.width);
3258 h[push](box.y + box.height);
3260 x = mmin[apply](0, x);
3261 y = mmin[apply](0, y);
3265 width: mmax[apply](0, w) - x,
3266 height: mmax[apply](0, h) - y
3269 Set[proto].clone = function (s) {
3271 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3272 s[push](this.items[i].clone());
3277 R.registerFont = function (font) {
3281 this.fonts = this.fonts || {};
3287 family = font.face["font-family"];
3288 for (var prop in font.face) if (font.face[has](prop)) {
3289 fontcopy.face[prop] = font.face[prop];
3291 if (this.fonts[family]) {
3292 this.fonts[family][push](fontcopy);
3294 this.fonts[family] = [fontcopy];
3297 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3298 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3299 var path = font.glyphs[glyph];
3300 fontcopy.glyphs[glyph] = {
3303 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3304 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3308 for (var k in path.k) if (path[has](k)) {
3309 fontcopy.glyphs[glyph].k[k] = path.k[k];
3316 Paper[proto].getFont = function (family, weight, style, stretch) {
3317 stretch = stretch || "normal";
3318 style = style || "normal";
3319 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3323 var font = R.fonts[family];
3325 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3326 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3327 if (name.test(fontName)) {
3328 font = R.fonts[fontName];
3335 for (var i = 0, ii = font[length]; i < ii; i++) {
3337 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3344 Paper[proto].print = function (x, y, string, font, size, origin) {
3345 origin = origin || "middle"; // baseline|middle
3346 var out = this.set(),
3347 letters = (string + E)[split](E),
3351 R.is(font, string) && (font = this.getFont(font));
3353 scale = (size || 16) / font.face["units-per-em"];
3354 var bb = font.face.bbox.split(separator),
3356 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3357 for (var i = 0, ii = letters[length]; i < ii; i++) {
3358 var prev = i && font.glyphs[letters[i - 1]] || {},
3359 curr = font.glyphs[letters[i]];
3360 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3361 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3363 out.scale(scale, scale, top, height).translate(x - top, y - height);
3368 var formatrg = /\{(\d+)\}/g;
3369 R.format = function (token, array) {
3370 var args = R.is(array, array) ? [0][concat](array) : arguments;
3371 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3372 return args[++i] == null ? E : args[i];
3376 R.ninja = function () {
3377 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3380 R.el = Element[proto];