2 * Raphael 1.4.0 - 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 upperCase = String[proto].toUpperCase,
70 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},
71 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},
73 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
74 if (R.type == "VML") {
75 var d = doc.createElement("div");
76 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
77 if (d.childNodes[length] != 2) {
82 R.svg = !(R.vml = R.type == "VML");
83 Paper[proto] = R[proto];
87 R.is = function (o, type) {
88 type = lowerCase.call(type);
89 return (type == "object" && o === Object(o)) ||
90 (type == "undefined" && typeof o == type) ||
91 (type == "null" && o == null) ||
92 lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
95 R.setWindow = function (newwin) {
100 var toHex = function (color) {
102 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
103 var trim = /^\s+|\s+$/g;
104 toHex = cacher(function (color) {
106 color = (color + E)[rp](trim, E);
108 var docum = new win.ActiveXObject("htmlfile");
109 docum.write("<body>");
113 bod = win.createPopup().document.body;
115 var range = bod.createTextRange();
117 bod.style.color = color;
118 var value = range.queryCommandValue("ForeColor");
119 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
120 return "#" + ("000000" + value[toString](16)).slice(-6);
126 var i = doc.createElement("i");
127 i.title = "Rapha\xebl Colour Picker";
128 i.style.display = "none";
129 doc.body[appendChild](i);
130 toHex = cacher(function (color) {
131 i.style.color = color;
132 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
137 var hsbtoString = function () {
138 return "hsb(" + [this.h, this.s, this.b] + ")";
140 rgbtoString = function () {
143 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
144 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
152 if (brightness == 0) {
153 return {r: 0, g: 0, b: 0, hex: "#000"};
155 if (hue > 1 || saturation > 1 || brightness > 1) {
162 p = brightness * (1 - saturation),
163 q = brightness * (1 - (saturation * f)),
164 t = brightness * (1 - (saturation * (1 - f)));
165 red = [brightness, q, p, p, t, brightness, brightness][i];
166 green = [t, brightness, brightness, q, p, p, t][i];
167 blue = [p, p, t, brightness, brightness, q, p][i];
171 var rgb = {r: red, g: green, b: blue, toString: rgbtoString},
172 r = (~~red)[toString](16),
173 g = (~~green)[toString](16),
174 b = (~~blue)[toString](16);
178 rgb.hex = "#" + r + g + b;
181 R.rgb2hsb = cacher(function (red, green, blue) {
182 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
187 if (R.is(red, string)) {
188 var clr = R.getRGB(red);
193 if (red > 1 || green > 1 || blue > 1) {
198 var max = mmax(red, green, blue),
199 min = mmin(red, green, blue),
204 return {h: 0, s: 0, b: max};
206 var delta = (max - min);
207 saturation = delta / max;
209 hue = (green - blue) / delta;
210 } else if (green == max) {
211 hue = 2 + ((blue - red) / delta);
213 hue = 4 + ((red - green) / delta);
219 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
221 var p2s = /,?([achlmqrstvxz]),?/gi,
222 commaSpaces = /\s*,\s*/,
223 hsrg = {hs: 1, rg: 1};
224 R._path2string = function () {
225 return this.join(",")[rp](p2s, "$1");
227 function cacher(f, scope, postprocessor) {
229 var arg = Array[proto].slice.call(arguments, 0),
230 args = arg[join]("\u25ba"),
231 cache = newf.cache = newf.cache || {},
232 count = newf.count = newf.count || [];
233 if (cache[has](args)) {
234 return postprocessor ? postprocessor(cache[args]) : cache[args];
236 count[length] >= 1e3 && delete cache[count.shift()];
238 cache[args] = f[apply](scope, arg);
239 return postprocessor ? postprocessor(cache[args]) : cache[args];
244 R.getRGB = cacher(function (colour) {
245 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
246 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
248 if (colour == "none") {
249 return {r: -1, g: -1, b: -1, hex: "none"};
251 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
258 rgb = colour.match(colourRegExp);
261 blue = toInt(rgb[2].substring(5), 16);
262 green = toInt(rgb[2].substring(3, 5), 16);
263 red = toInt(rgb[2].substring(1, 3), 16);
266 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
267 green = toInt((t = rgb[3].charAt(2)) + t, 16);
268 red = toInt((t = rgb[3].charAt(1)) + t, 16);
271 rgb = rgb[4][split](commaSpaces);
272 red = toFloat(rgb[0]);
273 green = toFloat(rgb[1]);
274 blue = toFloat(rgb[2]);
275 opacity = toFloat(rgb[3]);
278 rgb = rgb[5][split](commaSpaces);
279 red = toFloat(rgb[0]) * 2.55;
280 green = toFloat(rgb[1]) * 2.55;
281 blue = toFloat(rgb[2]) * 2.55;
282 opacity = toFloat(rgb[3]);
285 rgb = rgb[6][split](commaSpaces);
286 red = toFloat(rgb[0]);
287 green = toFloat(rgb[1]);
288 blue = toFloat(rgb[2]);
289 return R.hsb2rgb(red, green, blue);
292 rgb = rgb[7][split](commaSpaces);
293 red = toFloat(rgb[0]) * 2.55;
294 green = toFloat(rgb[1]) * 2.55;
295 blue = toFloat(rgb[2]) * 2.55;
296 return R.hsb2rgb(red, green, blue);
298 rgb = {r: red, g: green, b: blue};
299 var r = (~~red)[toString](16),
300 g = (~~green)[toString](16),
301 b = (~~blue)[toString](16);
305 rgb.hex = "#" + r + g + b;
306 isFinite(toFloat(opacity)) && (rgb.o = opacity);
309 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
311 R.getColor = function (value) {
312 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
313 rgb = this.hsb2rgb(start.h, start.s, start.b);
318 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
322 R.getColor.reset = function () {
326 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
327 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
328 R.parsePathString = cacher(function (pathString) {
332 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
334 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
335 data = pathClone(pathString);
338 (pathString + E)[rp](pathCommand, function (a, b, c) {
340 name = lowerCase.call(b);
341 c[rp](pathValues, function (a, b) {
342 b && params[push](+b);
344 if (name == "m" && params[length] > 2) {
345 data[push]([b][concat](params.splice(0, 2)));
347 b = b == "m" ? "l" : "L";
349 while (params[length] >= paramCounts[name]) {
350 data[push]([b][concat](params.splice(0, paramCounts[name])));
351 if (!paramCounts[name]) {
357 data[toString] = R._path2string;
360 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
362 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
363 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
364 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
365 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
366 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
367 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
368 ax = (1 - t) * p1x + t * c1x,
369 ay = (1 - t) * p1y + t * c1y,
370 cx = (1 - t) * c2x + t * p2x,
371 cy = (1 - t) * c2y + t * p2y,
372 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
373 (mx > nx || my < ny) && (alpha += 180);
374 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};
376 var pathDimensions = cacher(function (path) {
378 return {x: 0, y: 0, width: 0, height: 0};
380 path = path2curve(path);
386 for (var i = 0, ii = path[length]; i < ii; i++) {
394 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
395 X = X[concat](dim.min.x, dim.max.x);
396 Y = Y[concat](dim.min.y, dim.max.y);
401 var xmin = mmin[apply](0, X),
402 ymin = mmin[apply](0, Y);
406 width: mmax[apply](0, X) - xmin,
407 height: mmax[apply](0, Y) - ymin
410 pathClone = function (pathArray) {
412 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
413 pathArray = R.parsePathString(pathArray);
415 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
417 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
418 res[i][j] = pathArray[i][j];
421 res[toString] = R._path2string;
424 pathToRelative = cacher(function (pathArray) {
425 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
426 pathArray = R.parsePathString(pathArray);
434 if (pathArray[0][0] == "M") {
440 res[push](["M", x, y]);
442 for (var i = start, ii = pathArray[length]; i < ii; i++) {
445 if (pa[0] != lowerCase.call(pa[0])) {
446 r[0] = lowerCase.call(pa[0]);
454 r[6] = +(pa[6] - x).toFixed(3);
455 r[7] = +(pa[7] - y).toFixed(3);
458 r[1] = +(pa[1] - y).toFixed(3);
464 for (var j = 1, jj = pa[length]; j < jj; j++) {
465 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
474 for (var k = 0, kk = pa[length]; k < kk; k++) {
478 var len = res[i][length];
485 x += +res[i][len - 1];
488 y += +res[i][len - 1];
491 x += +res[i][len - 2];
492 y += +res[i][len - 1];
495 res[toString] = R._path2string;
498 pathToAbsolute = cacher(function (pathArray) {
499 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
500 pathArray = R.parsePathString(pathArray);
508 if (pathArray[0][0] == "M") {
509 x = +pathArray[0][1];
510 y = +pathArray[0][2];
514 res[0] = ["M", x, y];
516 for (var i = start, ii = pathArray[length]; i < ii; i++) {
519 if (pa[0] != upperCase.call(pa[0])) {
520 r[0] = upperCase.call(pa[0]);
541 for (var j = 1, jj = pa[length]; j < jj; j++) {
542 r[j] = +pa[j] + ((j % 2) ? x : y);
546 for (var k = 0, kk = pa[length]; k < kk; k++) {
562 x = res[i][res[i][length] - 2];
563 y = res[i][res[i][length] - 1];
566 res[toString] = R._path2string;
569 l2c = function (x1, y1, x2, y2) {
570 return [x1, y1, x2, y2, x2, y2];
572 q2c = function (x1, y1, ax, ay, x2, y2) {
584 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
585 // for more information of where this math came from visit:
586 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
588 _120 = PI * 120 / 180,
589 rad = PI / 180 * (+angle || 0),
592 rotate = cacher(function (x, y, rad) {
593 var X = x * math.cos(rad) - y * math.sin(rad),
594 Y = x * math.sin(rad) + y * math.cos(rad);
598 xy = rotate(x1, y1, -rad);
601 xy = rotate(x2, y2, -rad);
604 var cos = math.cos(PI / 180 * angle),
605 sin = math.sin(PI / 180 * angle),
608 // rx = mmax(rx, math.abs(x));
609 // ry = mmax(ry, math.abs(y));
610 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
618 k = (large_arc_flag == sweep_flag ? -1 : 1) *
619 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
620 cx = k * rx * y / ry + (x1 + x2) / 2,
621 cy = k * -ry * x / rx + (y1 + y2) / 2,
622 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
623 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
625 f1 = x1 < cx ? PI - f1 : f1;
626 f2 = x2 < cx ? PI - f2 : f2;
627 f1 < 0 && (f1 = PI * 2 + f1);
628 f2 < 0 && (f2 = PI * 2 + f2);
629 if (sweep_flag && f1 > f2) {
632 if (!sweep_flag && f2 > f1) {
642 if (math.abs(df) > _120) {
646 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
647 x2 = cx + rx * math.cos(f2);
648 y2 = cy + ry * math.sin(f2);
649 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
652 var c1 = math.cos(f1),
656 t = math.tan(df / 4),
660 m2 = [x1 + hx * s1, y1 - hy * c1],
661 m3 = [x2 + hx * s2, y2 - hy * c2],
663 m2[0] = 2 * m1[0] - m2[0];
664 m2[1] = 2 * m1[1] - m2[1];
666 return [m2, m3, m4][concat](res);
668 res = [m2, m3, m4][concat](res)[join]()[split](",");
670 for (var i = 0, ii = res[length]; i < ii; i++) {
671 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
677 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
680 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
681 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
684 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
685 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
686 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
688 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
689 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
693 math.abs(t1) > 1e12 && (t1 = .5);
694 math.abs(t2) > 1e12 && (t2 = .5);
695 if (t1 > 0 && t1 < 1) {
696 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
700 if (t2 > 0 && t2 < 1) {
701 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
705 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
706 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
708 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
709 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
710 math.abs(t1) > 1e12 && (t1 = .5);
711 math.abs(t2) > 1e12 && (t2 = .5);
712 if (t1 > 0 && t1 < 1) {
713 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
717 if (t2 > 0 && t2 < 1) {
718 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
723 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
724 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
727 path2curve = cacher(function (path, path2) {
728 var p = pathToAbsolute(path),
729 p2 = path2 && pathToAbsolute(path2),
730 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
731 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
732 processPath = function (path, d) {
735 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
737 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
744 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
747 nx = d.x + (d.x - (d.bx || d.x));
748 ny = d.y + (d.y - (d.by || d.y));
749 path = ["C", nx, ny][concat](path.slice(1));
752 d.qx = d.x + (d.x - (d.qx || d.x));
753 d.qy = d.y + (d.y - (d.qy || d.y));
754 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
759 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
762 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
765 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
768 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
771 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
776 fixArc = function (pp, i) {
777 if (pp[i][length] > 7) {
781 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
784 ii = mmax(p[length], p2 && p2[length] || 0);
787 fixM = function (path1, path2, a1, a2, i) {
788 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
789 path2.splice(i, 0, ["M", a2.x, a2.y]);
794 ii = mmax(p[length], p2 && p2[length] || 0);
797 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
798 p[i] = processPath(p[i], attrs);
800 p2 && (p2[i] = processPath(p2[i], attrs2));
802 fixM(p, p2, attrs, attrs2, i);
803 fixM(p2, p, attrs2, attrs, i);
806 seglen = seg[length],
807 seg2len = p2 && seg2[length];
808 attrs.x = seg[seglen - 2];
809 attrs.y = seg[seglen - 1];
810 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
811 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
812 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
813 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
814 attrs2.x = p2 && seg2[seg2len - 2];
815 attrs2.y = p2 && seg2[seg2len - 1];
817 return p2 ? [p, p2] : p;
819 parseDots = cacher(function (gradient) {
821 for (var i = 0, ii = gradient[length]; i < ii; i++) {
823 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
824 dot.color = R.getRGB(par[1]);
825 if (dot.color.error) {
828 dot.color = dot.color.hex;
829 par[2] && (dot.offset = par[2] + "%");
832 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
833 if (!dots[i].offset) {
834 var start = toFloat(dots[i - 1].offset || 0),
836 for (var j = i + 1; j < ii; j++) {
837 if (dots[j].offset) {
838 end = dots[j].offset;
847 var d = (end - start) / (j - i + 1);
850 dots[i].offset = start + "%";
856 getContainer = function (x, y, w, h) {
858 if (R.is(x, string) || R.is(x, "object")) {
859 container = R.is(x, string) ? doc.getElementById(x) : x;
860 if (container.tagName) {
863 container: container,
864 width: container.style.pixelWidth || container.offsetWidth,
865 height: container.style.pixelHeight || container.offsetHeight
868 return {container: container, width: y, height: w};
872 return {container: 1, x: x, y: y, width: w, height: h};
875 plugins = function (con, add) {
877 for (var prop in add) {
878 if (add[has](prop) && !(prop in con)) {
879 switch (typeof add[prop]) {
882 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
886 con[prop] = con[prop] || {};
887 plugins.call(this, con[prop], add[prop]);
890 con[prop] = add[prop];
896 tear = function (el, paper) {
897 el == paper.top && (paper.top = el.prev);
898 el == paper.bottom && (paper.bottom = el.next);
899 el.next && (el.next.prev = el.prev);
900 el.prev && (el.prev.next = el.next);
902 tofront = function (el, paper) {
903 if (paper.top === el) {
912 toback = function (el, paper) {
913 if (paper.bottom === el) {
917 el.next = paper.bottom;
919 paper.bottom.prev = el;
922 insertafter = function (el, el2, paper) {
924 el2 == paper.top && (paper.top = el);
925 el2.next && (el2.next.prev = el);
930 insertbefore = function (el, el2, paper) {
932 el2 == paper.bottom && (paper.bottom = el);
933 el2.prev && (el2.prev.next = el);
938 removed = function (methodname) {
940 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
943 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
947 Paper[proto].svgns = "http://www.w3.org/2000/svg";
948 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
949 round = function (num) {
950 return +num + (~~num === num) * .5;
952 var $ = function (el, attr) {
954 for (var key in attr) {
955 if (attr[has](key)) {
956 el[setAttribute](key, attr[key] + E);
960 el = doc.createElementNS(Paper[proto].svgns, el);
961 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
965 R[toString] = function () {
966 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
968 var thePath = function (pathString, SVG) {
970 SVG.canvas && SVG.canvas[appendChild](el);
971 var p = new Element(el, SVG);
973 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
976 var addGradientFill = function (o, gradient, SVG) {
980 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
985 var dir = ((fy > .5) * 2 - 1);
986 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
987 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
989 (fy = fy.toFixed(5) - 1e-5 * dir);
993 gradient = gradient[split](/\s*\-\s*/);
994 if (type == "linear") {
995 var angle = gradient.shift();
996 angle = -toFloat(angle);
1000 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1001 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1004 if (vector[2] < 0) {
1005 vector[0] = -vector[2];
1008 if (vector[3] < 0) {
1009 vector[1] = -vector[3];
1013 var dots = parseDots(gradient);
1017 var id = o.getAttribute(fillString);
1018 id = id.match(/^url\(#(.*)\)$/);
1019 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1021 var el = $(type + "Gradient");
1022 el.id = "r" + (R._id++)[toString](36);
1023 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1024 SVG.defs[appendChild](el);
1025 for (var i = 0, ii = dots[length]; i < ii; i++) {
1026 var stop = $("stop");
1028 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1029 "stop-color": dots[i].color || "#fff"
1031 el[appendChild](stop);
1034 fill: "url(#" + el.id + ")",
1043 var updatePosition = function (o) {
1044 var bbox = o.getBBox();
1045 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1047 var setFillAndStroke = function (o, params) {
1054 "-..": [3, 1, 1, 1, 1, 1],
1058 "- .": [4, 3, 1, 3],
1059 "--.": [8, 3, 1, 3],
1060 "--..": [8, 3, 1, 3, 1, 3]
1065 addDashes = function (o, value) {
1066 value = dasharray[lowerCase.call(value)];
1068 var width = o.attrs["stroke-width"] || "1",
1069 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1071 var i = value[length];
1073 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1075 $(node, {"stroke-dasharray": dashes[join](",")});
1078 params[has]("rotation") && (rot = params.rotation);
1079 var rotxy = (rot + E)[split](separator);
1080 if (!(rotxy.length - 1)) {
1083 rotxy[1] = +rotxy[1];
1084 rotxy[2] = +rotxy[2];
1086 toFloat(rot) && o.rotate(0, true);
1087 for (var att in params) {
1088 if (params[has](att)) {
1089 if (!availableAttrs[has](att)) {
1092 var value = params[att];
1099 o.rotate(value, true);
1104 var pn = node.parentNode;
1105 if (lowerCase.call(pn.tagName) != "a") {
1107 pn.insertBefore(hl, node);
1108 hl[appendChild](node);
1111 pn.setAttributeNS(o.paper.xlink, att, value);
1114 node.style.cursor = value;
1117 var rect = (value + E)[split](separator);
1118 if (rect[length] == 4) {
1119 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1120 var el = $("clipPath"),
1122 el.id = "r" + (R._id++)[toString](36);
1129 el[appendChild](rc);
1130 o.paper.defs[appendChild](el);
1131 $(node, {"clip-path": "url(#" + el.id + ")"});
1135 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1136 clip && clip.parentNode.removeChild(clip);
1137 $(node, {"clip-path": E});
1142 if (o.type == "path") {
1143 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1147 node[setAttribute](att, value);
1156 value = -attrs.x - (attrs.width || 0);
1159 if (att == "rx" && o.type == "rect") {
1163 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1164 node[setAttribute](att, round(value));
1165 o.pattern && updatePosition(o);
1168 node[setAttribute](att, value);
1177 value = -attrs.y - (attrs.height || 0);
1180 if (att == "ry" && o.type == "rect") {
1184 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1185 node[setAttribute](att, round(value));
1186 o.pattern && updatePosition(o);
1189 if (o.type == "rect") {
1190 $(node, {rx: value, ry: value});
1192 node[setAttribute](att, value);
1196 if (o.type == "image") {
1197 node.setAttributeNS(o.paper.xlink, "href", value);
1200 case "stroke-width":
1201 node.style.strokeWidth = value;
1202 // Need following line for Firefox
1203 node[setAttribute](att, value);
1204 if (attrs["stroke-dasharray"]) {
1205 addDashes(o, attrs["stroke-dasharray"]);
1208 case "stroke-dasharray":
1209 addDashes(o, value);
1212 var xy = (value + E)[split](separator);
1213 xy[0] = +xy[0] || 0;
1214 xy[1] = +xy[1] || 0;
1219 translate.call(o, xy[0], xy[1]);
1222 xy = (value + E)[split](separator);
1223 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1226 var isURL = (value + E).match(ISURL);
1229 var ig = $("image");
1230 el.id = "r" + (R._id++)[toString](36);
1231 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1232 $(ig, {x: 0, y: 0});
1233 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1234 el[appendChild](ig);
1236 var img = doc.createElement("img");
1237 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1238 img.onload = function () {
1239 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1240 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1241 doc.body.removeChild(this);
1244 doc.body[appendChild](img);
1246 o.paper.defs[appendChild](el);
1247 node.style.fill = "url(#" + el.id + ")";
1248 $(node, {fill: "url(#" + el.id + ")"});
1250 o.pattern && updatePosition(o);
1253 var clr = R.getRGB(value);
1255 delete params.gradient;
1256 delete attrs.gradient;
1257 !R.is(attrs.opacity, "undefined") &&
1258 R.is(params.opacity, "undefined") &&
1259 $(node, {opacity: attrs.opacity});
1260 !R.is(attrs["fill-opacity"], "undefined") &&
1261 R.is(params["fill-opacity"], "undefined") &&
1262 $(node, {"fill-opacity": attrs["fill-opacity"]});
1263 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1264 attrs.gradient = value;
1265 attrs.fill = "none";
1268 clr[has]("o") && $(node, {"fill-opacity": clr.o / 100});
1270 clr = R.getRGB(value);
1271 node[setAttribute](att, clr.hex);
1272 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o / 100});
1275 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1278 case "fill-opacity":
1279 if (attrs.gradient) {
1280 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1282 var stops = gradient.getElementsByTagName("stop");
1283 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1288 att == "font-size" && (value = toInt(value, 10) + "px");
1289 var cssrule = att[rp](/(\-.)/g, function (w) {
1290 return upperCase.call(w.substring(1));
1292 node.style[cssrule] = value;
1293 // Need following line for Firefox
1294 node[setAttribute](att, value);
1300 tuneText(o, params);
1302 o.rotate(rotxy.join(S));
1304 toFloat(rot) && o.rotate(rot, true);
1308 tuneText = function (el, params) {
1309 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1314 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1316 if (params[has]("text")) {
1317 a.text = params.text;
1318 while (node.firstChild) {
1319 node.removeChild(node.firstChild);
1321 var texts = (params.text + E)[split]("\n");
1322 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1323 var tspan = $("tspan");
1324 i && $(tspan, {dy: fontSize * leading, x: a.x});
1325 tspan[appendChild](doc.createTextNode(texts[i]));
1326 node[appendChild](tspan);
1329 texts = node.getElementsByTagName("tspan");
1330 for (i = 0, ii = texts[length]; i < ii; i++) {
1331 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1335 var bb = el.getBBox(),
1336 dif = a.y - (bb.y + bb.height / 2);
1337 dif && isFinite(dif) && $(node, {y: a.y + dif});
1339 Element = function (node, svg) {
1345 node.raphael = this;
1347 this.attrs = this.attrs || {};
1348 this.transformations = []; // rotate, translate, scale
1352 rt: {deg: 0, cx: 0, cy: 0},
1356 !svg.bottom && (svg.bottom = this);
1357 this.prev = svg.top;
1358 svg.top && (svg.top.next = this);
1362 Element[proto].rotate = function (deg, cx, cy) {
1368 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1370 return this._.rt.deg;
1372 var bbox = this.getBBox();
1373 deg = (deg + E)[split](separator);
1374 if (deg[length] - 1) {
1375 cx = toFloat(deg[1]);
1376 cy = toFloat(deg[2]);
1378 deg = toFloat(deg[0]);
1380 this._.rt.deg = deg;
1382 this._.rt.deg += deg;
1384 (cy == null) && (cx = null);
1387 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1388 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1389 if (this._.rt.deg) {
1390 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1391 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1393 this.transformations[0] = E;
1394 this.clip && $(this.clip, {transform: E});
1396 $(this.node, {transform: this.transformations[join](S)});
1399 Element[proto].hide = function () {
1400 !this.removed && (this.node.style.display = "none");
1403 Element[proto].show = function () {
1404 !this.removed && (this.node.style.display = "");
1407 Element[proto].remove = function () {
1411 tear(this, this.paper);
1412 this.node.parentNode.removeChild(this.node);
1413 for (var i in this) {
1416 this.removed = true;
1418 Element[proto].getBBox = function () {
1422 if (this.type == "path") {
1423 return pathDimensions(this.attrs.path);
1425 if (this.node.style.display == "none") {
1431 bbox = this.node.getBBox();
1433 // Firefox 3.0.x plays badly here
1437 if (this.type == "text") {
1438 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1439 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1440 var bb = this.node.getExtentOfChar(i);
1441 (bb.y < bbox.y) && (bbox.y = bb.y);
1442 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1443 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1446 hide && this.hide();
1449 Element[proto].attr = function (name, value) {
1455 for (var i in this.attrs) if (this.attrs[has](i)) {
1456 res[i] = this.attrs[i];
1458 this._.rt.deg && (res.rotation = this.rotate());
1459 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1460 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1463 if (value == null && R.is(name, string)) {
1464 if (name == "translation") {
1465 return translate.call(this);
1467 if (name == "rotation") {
1468 return this.rotate();
1470 if (name == "scale") {
1471 return this.scale();
1473 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1474 return this.attrs.gradient;
1476 return this.attrs[name];
1478 if (value == null && R.is(name, array)) {
1480 for (var j = 0, jj = name.length; j < jj; j++) {
1481 values[name[j]] = this.attr(name[j]);
1485 if (value != null) {
1487 params[name] = value;
1488 setFillAndStroke(this, params);
1489 } else if (name != null && R.is(name, "object")) {
1490 setFillAndStroke(this, name);
1494 Element[proto].toFront = function () {
1498 this.node.parentNode[appendChild](this.node);
1499 var svg = this.paper;
1500 svg.top != this && tofront(this, svg);
1503 Element[proto].toBack = function () {
1507 if (this.node.parentNode.firstChild != this.node) {
1508 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1509 toback(this, this.paper);
1510 var svg = this.paper;
1514 Element[proto].insertAfter = function (element) {
1518 var node = element.node;
1519 if (node.nextSibling) {
1520 node.parentNode.insertBefore(this.node, node.nextSibling);
1522 node.parentNode[appendChild](this.node);
1524 insertafter(this, element, this.paper);
1527 Element[proto].insertBefore = function (element) {
1531 var node = element.node;
1532 node.parentNode.insertBefore(this.node, node);
1533 insertbefore(this, element, this.paper);
1536 Element[proto].blur = function (size) {
1537 // Experimental. No Safari support. Use it on your own risk.
1540 var fltr = $("filter"),
1541 blur = $("feGaussianBlur");
1542 t.attrs.blur = size;
1543 fltr.id = "r" + (R._id++)[toString](36);
1544 $(blur, {stdDeviation: +size || 1.5});
1545 fltr.appendChild(blur);
1546 t.paper.defs.appendChild(fltr);
1548 $(t.node, {filter: "url(#" + fltr.id + ")"});
1551 t._blur.parentNode.removeChild(t._blur);
1553 delete t.attrs.blur;
1555 t.node.removeAttribute("filter");
1558 var theCircle = function (svg, x, y, r) {
1561 var el = $("circle");
1562 svg.canvas && svg.canvas[appendChild](el);
1563 var res = new Element(el, svg);
1564 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1565 res.type = "circle";
1569 var theRect = function (svg, x, y, w, h, r) {
1573 svg.canvas && svg.canvas[appendChild](el);
1574 var res = new Element(el, svg);
1575 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1580 var theEllipse = function (svg, x, y, rx, ry) {
1583 var el = $("ellipse");
1584 svg.canvas && svg.canvas[appendChild](el);
1585 var res = new Element(el, svg);
1586 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1587 res.type = "ellipse";
1591 var theImage = function (svg, src, x, y, w, h) {
1592 var el = $("image");
1593 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1594 el.setAttributeNS(svg.xlink, "href", src);
1595 svg.canvas && svg.canvas[appendChild](el);
1596 var res = new Element(el, svg);
1597 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1601 var theText = function (svg, x, y, text) {
1603 $(el, {x: x, y: y, "text-anchor": "middle"});
1604 svg.canvas && svg.canvas[appendChild](el);
1605 var res = new Element(el, svg);
1606 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1608 setFillAndStroke(res, res.attrs);
1611 var setSize = function (width, height) {
1612 this.width = width || this.width;
1613 this.height = height || this.height;
1614 this.canvas[setAttribute]("width", this.width);
1615 this.canvas[setAttribute]("height", this.height);
1618 var create = function () {
1619 var con = getContainer[apply](0, arguments),
1620 container = con && con.container,
1624 height = con.height;
1626 throw new Error("SVG container not found.");
1628 var cnvs = $("svg");
1631 width = width || 512;
1632 height = height || 342;
1634 xmlns: "http://www.w3.org/2000/svg",
1639 if (container == 1) {
1640 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1641 doc.body[appendChild](cnvs);
1643 if (container.firstChild) {
1644 container.insertBefore(cnvs, container.firstChild);
1646 container[appendChild](cnvs);
1649 container = new Paper;
1650 container.width = width;
1651 container.height = height;
1652 container.canvas = cnvs;
1653 plugins.call(container, container, R.fn);
1657 Paper[proto].clear = function () {
1658 var c = this.canvas;
1659 while (c.firstChild) {
1660 c.removeChild(c.firstChild);
1662 this.bottom = this.top = null;
1663 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1664 c[appendChild](this.desc);
1665 c[appendChild](this.defs = $("defs"));
1667 Paper[proto].remove = function () {
1668 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1669 for (var i in this) {
1670 this[i] = removed(i);
1677 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1678 bites = /([clmz]),?([^clmz]*)/gi,
1679 val = /-?[^,\s-]+/g,
1680 coordsize = 1e3 + S + 1e3,
1682 path2vml = function (path) {
1683 var total = /[ahqstv]/ig,
1684 command = pathToAbsolute;
1685 (path + E).match(total) && (command = path2curve);
1687 if (command == pathToAbsolute && !(path + E).match(total)) {
1688 var res = (path + E)[rp](bites, function (all, command, args) {
1690 isMove = lowerCase.call(command) == "m",
1692 args[rp](val, function (value) {
1693 if (isMove && vals[length] == 2) {
1694 res += vals + map[command == "m" ? "l" : "L"];
1697 vals[push](round(value * zoom));
1703 var pa = command(path), p, r;
1705 for (var i = 0, ii = pa[length]; i < ii; i++) {
1707 r = lowerCase.call(pa[i][0]);
1708 r == "z" && (r = "x");
1709 for (var j = 1, jj = p[length]; j < jj; j++) {
1710 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1714 return res[join](S);
1717 R[toString] = function () {
1718 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1720 thePath = function (pathString, vml) {
1721 var g = createNode("group");
1722 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1723 g.coordsize = vml.coordsize;
1724 g.coordorigin = vml.coordorigin;
1725 var el = createNode("shape"), ol = el.style;
1726 ol.width = vml.width + "px";
1727 ol.height = vml.height + "px";
1728 el.coordsize = coordsize;
1729 el.coordorigin = vml.coordorigin;
1731 var p = new Element(el, g, vml),
1732 attr = {fill: "none", stroke: "#000"};
1733 pathString && (attr.path = pathString);
1734 p.isAbsolute = true;
1738 setFillAndStroke(p, attr);
1739 vml.canvas[appendChild](g);
1742 setFillAndStroke = function (o, params) {
1743 o.attrs = o.attrs || {};
1749 for (var par in params) if (params[has](par)) {
1750 a[par] = params[par];
1752 params.href && (node.href = params.href);
1753 params.title && (node.title = params.title);
1754 params.target && (node.target = params.target);
1755 params.cursor && (s.cursor = params.cursor);
1756 "blur" in params && o.blur(params.blur);
1757 if (params.path && o.type == "path") {
1758 a.path = params.path;
1759 node.path = path2vml(a.path);
1761 if (params.rotation != null) {
1762 o.rotate(params.rotation, true);
1764 if (params.translation) {
1765 xy = (params.translation + E)[split](separator);
1766 translate.call(o, xy[0], xy[1]);
1767 if (o._.rt.cx != null) {
1768 o._.rt.cx +=+ xy[0];
1769 o._.rt.cy +=+ xy[1];
1770 o.setBox(o.attrs, xy[0], xy[1]);
1774 xy = (params.scale + E)[split](separator);
1775 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1777 if ("clip-rect" in params) {
1778 var rect = (params["clip-rect"] + E)[split](separator);
1779 if (rect[length] == 4) {
1780 rect[2] = +rect[2] + (+rect[0]);
1781 rect[3] = +rect[3] + (+rect[1]);
1782 var div = node.clipRect || doc.createElement("div"),
1784 group = node.parentNode;
1785 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1786 if (!node.clipRect) {
1787 dstyle.position = "absolute";
1790 dstyle.width = o.paper.width + "px";
1791 dstyle.height = o.paper.height + "px";
1792 group.parentNode.insertBefore(div, group);
1793 div[appendChild](group);
1794 node.clipRect = div;
1797 if (!params["clip-rect"]) {
1798 node.clipRect && (node.clipRect.style.clip = E);
1801 if (o.type == "image" && params.src) {
1802 node.src = params.src;
1804 if (o.type == "image" && params.opacity) {
1805 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1806 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1808 params.font && (s.font = params.font);
1809 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1810 params["font-size"] && (s.fontSize = params["font-size"]);
1811 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1812 params["font-style"] && (s.fontStyle = params["font-style"]);
1813 if (params.opacity != null ||
1814 params["stroke-width"] != null ||
1815 params.fill != null ||
1816 params.stroke != null ||
1817 params["stroke-width"] != null ||
1818 params["stroke-opacity"] != null ||
1819 params["fill-opacity"] != null ||
1820 params["stroke-dasharray"] != null ||
1821 params["stroke-miterlimit"] != null ||
1822 params["stroke-linejoin"] != null ||
1823 params["stroke-linecap"] != null) {
1824 node = o.shape || node;
1825 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1827 !fill && (newfill = fill = createNode(fillString));
1828 if ("fill-opacity" in params || "opacity" in params) {
1829 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1830 opacity < 0 && (opacity = 0);
1831 opacity > 1 && (opacity = 1);
1832 fill.opacity = opacity;
1834 params.fill && (fill.on = true);
1835 if (fill.on == null || params.fill == "none") {
1838 if (fill.on && params.fill) {
1839 var isURL = params.fill.match(ISURL);
1841 fill.src = isURL[1];
1844 fill.color = R.getRGB(params.fill).hex;
1846 fill.type = "solid";
1847 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1849 a.gradient = params.fill;
1853 newfill && node[appendChild](fill);
1854 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1856 !stroke && (newstroke = stroke = createNode("stroke"));
1857 if ((params.stroke && params.stroke != "none") ||
1858 params["stroke-width"] ||
1859 params["stroke-opacity"] != null ||
1860 params["stroke-dasharray"] ||
1861 params["stroke-miterlimit"] ||
1862 params["stroke-linejoin"] ||
1863 params["stroke-linecap"]) {
1866 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1867 var strokeColor = R.getRGB(params.stroke);
1868 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
1869 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
1870 var width = (toFloat(params["stroke-width"]) || 1) * .75;
1871 opacity < 0 && (opacity = 0);
1872 opacity > 1 && (opacity = 1);
1873 params["stroke-width"] == null && (width = a["stroke-width"]);
1874 params["stroke-width"] && (stroke.weight = width);
1875 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1876 stroke.opacity = opacity;
1878 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1879 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1880 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1881 if (params["stroke-dasharray"]) {
1885 "-.": "shortdashdot",
1886 "-..": "shortdashdotdot",
1891 "--.": "longdashdot",
1892 "--..": "longdashdotdot"
1894 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1896 newstroke && node[appendChild](stroke);
1898 if (res.type == "text") {
1899 s = res.paper.span.style;
1900 a.font && (s.font = a.font);
1901 a["font-family"] && (s.fontFamily = a["font-family"]);
1902 a["font-size"] && (s.fontSize = a["font-size"]);
1903 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1904 a["font-style"] && (s.fontStyle = a["font-style"]);
1905 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1906 res.W = a.w = res.paper.span.offsetWidth;
1907 res.H = a.h = res.paper.span.offsetHeight;
1909 res.Y = a.y + round(res.H / 2);
1911 // text-anchor emulationm
1912 switch (a["text-anchor"]) {
1914 res.node.style["v-text-align"] = "left";
1915 res.bbx = round(res.W / 2);
1918 res.node.style["v-text-align"] = "right";
1919 res.bbx = -round(res.W / 2);
1922 res.node.style["v-text-align"] = "center";
1927 addGradientFill = function (o, gradient) {
1928 o.attrs = o.attrs || {};
1929 var attrs = o.attrs,
1933 o.attrs.gradient = gradient;
1934 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1939 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1944 gradient = gradient[split](/\s*\-\s*/);
1945 if (type == "linear") {
1946 var angle = gradient.shift();
1947 angle = -toFloat(angle);
1952 var dots = parseDots(gradient);
1956 o = o.shape || o.node;
1957 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
1958 !fill.parentNode && o.appendChild(fill);
1961 fill.method = "none";
1962 fill.color = dots[0].color;
1963 fill.color2 = dots[dots[length] - 1].color;
1965 for (var i = 0, ii = dots[length]; i < ii; i++) {
1966 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1968 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
1969 if (type == "radial") {
1970 fill.type = "gradientradial";
1971 fill.focus = "100%";
1972 fill.focussize = fxfy;
1973 fill.focusposition = fxfy;
1975 fill.type = "gradient";
1976 fill.angle = (270 - angle) % 360;
1981 Element = function (node, group, vml) {
1989 node.raphael = this;
2002 !vml.bottom && (vml.bottom = this);
2003 this.prev = vml.top;
2004 vml.top && (vml.top.next = this);
2008 Element[proto].rotate = function (deg, cx, cy) {
2014 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2016 return this._.rt.deg;
2018 deg = (deg + E)[split](separator);
2019 if (deg[length] - 1) {
2020 cx = toFloat(deg[1]);
2021 cy = toFloat(deg[2]);
2023 deg = toFloat(deg[0]);
2025 this._.rt.deg = deg;
2027 this._.rt.deg += deg;
2029 cy == null && (cx = null);
2032 this.setBox(this.attrs, cx, cy);
2033 this.Group.style.rotation = this._.rt.deg;
2034 // gradient fix for rotation. TODO
2035 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2036 // fill = fill[0] || {};
2037 // var b = ((360 - this._.rt.deg) - 270) % 360;
2038 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2041 Element[proto].setBox = function (params, cx, cy) {
2045 var gs = this.Group.style,
2046 os = (this.shape && this.shape.style) || this.node.style;
2047 params = params || {};
2048 for (var i in params) if (params[has](i)) {
2049 this.attrs[i] = params[i];
2051 cx = cx || this._.rt.cx;
2052 cy = cy || this._.rt.cy;
2053 var attr = this.attrs,
2058 switch (this.type) {
2060 x = attr.cx - attr.r;
2061 y = attr.cy - attr.r;
2065 x = attr.cx - attr.rx;
2066 y = attr.cy - attr.ry;
2074 w = attr.width || 0;
2075 h = attr.height || 0;
2078 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2079 x = attr.x - round(this.W / 2);
2080 y = attr.y - this.H / 2;
2085 if (!this.attrs.path) {
2088 w = this.paper.width;
2089 h = this.paper.height;
2091 var dim = pathDimensions(this.attrs.path);
2101 w = this.paper.width;
2102 h = this.paper.height;
2105 cx = (cx == null) ? x + w / 2 : cx;
2106 cy = (cy == null) ? y + h / 2 : cy;
2107 var left = cx - this.paper.width / 2,
2108 top = cy - this.paper.height / 2, t;
2109 gs.left != (t = left + "px") && (gs.left = t);
2110 gs.top != (t = top + "px") && (gs.top = t);
2111 this.X = this.type == "path" ? -left : x;
2112 this.Y = this.type == "path" ? -top : y;
2115 if (this.type == "path") {
2116 os.left != (t = -left * zoom + "px") && (os.left = t);
2117 os.top != (t = -top * zoom + "px") && (os.top = t);
2118 } else if (this.type == "text") {
2119 os.left != (t = -left + "px") && (os.left = t);
2120 os.top != (t = -top + "px") && (os.top = t);
2122 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2123 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2124 os.left != (t = x - left + "px") && (os.left = t);
2125 os.top != (t = y - top + "px") && (os.top = t);
2126 os.width != (t = w + "px") && (os.width = t);
2127 os.height != (t = h + "px") && (os.height = t);
2128 var arcsize = (+params.r || 0) / mmin(w, h);
2129 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2130 // We should replace element with the new one
2131 var o = createNode("roundrect"),
2133 ii = this.events && this.events[length];
2135 o.arcsize = arcsize;
2137 this.Group[appendChild](o);
2138 this.Group.removeChild(this.node);
2139 this[0] = this.node = o;
2140 this.arcsize = arcsize;
2146 if (this.events) for (i = 0; i < ii; i++) {
2147 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2152 Element[proto].hide = function () {
2153 !this.removed && (this.Group.style.display = "none");
2156 Element[proto].show = function () {
2157 !this.removed && (this.Group.style.display = "block");
2160 Element[proto].getBBox = function () {
2164 if (this.type == "path") {
2165 return pathDimensions(this.attrs.path);
2168 x: this.X + (this.bbx || 0),
2174 Element[proto].remove = function () {
2178 tear(this, this.paper);
2179 this.node.parentNode.removeChild(this.node);
2180 this.Group.parentNode.removeChild(this.Group);
2181 this.shape && this.shape.parentNode.removeChild(this.shape);
2182 for (var i in this) {
2185 this.removed = true;
2187 Element[proto].attr = function (name, value) {
2193 for (var i in this.attrs) if (this.attrs[has](i)) {
2194 res[i] = this.attrs[i];
2196 this._.rt.deg && (res.rotation = this.rotate());
2197 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2198 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2201 if (value == null && R.is(name, string)) {
2202 if (name == "translation") {
2203 return translate.call(this);
2205 if (name == "rotation") {
2206 return this.rotate();
2208 if (name == "scale") {
2209 return this.scale();
2211 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2212 return this.attrs.gradient;
2214 return this.attrs[name];
2216 if (this.attrs && value == null && R.is(name, array)) {
2217 var ii, values = {};
2218 for (i = 0, ii = name[length]; i < ii; i++) {
2219 values[name[i]] = this.attr(name[i]);
2224 if (value != null) {
2226 params[name] = value;
2228 value == null && R.is(name, "object") && (params = name);
2230 if (params.text && this.type == "text") {
2231 this.node.string = params.text;
2233 setFillAndStroke(this, params);
2234 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2235 addGradientFill(this, params.gradient);
2237 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2241 Element[proto].toFront = function () {
2242 !this.removed && this.Group.parentNode[appendChild](this.Group);
2243 this.paper.top != this && tofront(this, this.paper);
2246 Element[proto].toBack = function () {
2250 if (this.Group.parentNode.firstChild != this.Group) {
2251 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2252 toback(this, this.paper);
2256 Element[proto].insertAfter = function (element) {
2260 if (element.Group.nextSibling) {
2261 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2263 element.Group.parentNode[appendChild](this.Group);
2265 insertafter(this, element, this.paper);
2268 Element[proto].insertBefore = function (element) {
2272 element.Group.parentNode.insertBefore(this.Group, element.Group);
2273 insertbefore(this, element, this.paper);
2276 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2277 Element[proto].blur = function (size) {
2278 var s = this.node.style,
2280 f = f.replace(blurregexp, "");
2282 this.attrs.blur = size;
2283 s.filter = f + " progid:DXImageTransform.Microsoft.Blur(pixelradius=" + (+size || 1.5) + ")";
2284 s.margin = Raphael.format("-{0}px 0 0 -{0}px", Math.round(+size || 1.5));
2288 delete this.attrs.blur;
2292 theCircle = function (vml, x, y, r) {
2293 var g = createNode("group"),
2294 o = createNode("oval"),
2296 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2297 g.coordsize = coordsize;
2298 g.coordorigin = vml.coordorigin;
2300 var res = new Element(o, g, vml);
2301 res.type = "circle";
2302 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2306 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2307 vml.canvas[appendChild](g);
2310 theRect = function (vml, x, y, w, h, r) {
2311 var g = createNode("group"),
2312 o = createNode("roundrect"),
2313 arcsize = (+r || 0) / (mmin(w, h));
2314 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2315 g.coordsize = coordsize;
2316 g.coordorigin = vml.coordorigin;
2318 o.arcsize = arcsize;
2319 var res = new Element(o, g, vml);
2321 setFillAndStroke(res, {stroke: "#000"});
2322 res.arcsize = arcsize;
2323 res.setBox({x: x, y: y, width: w, height: h, r: r});
2324 vml.canvas[appendChild](g);
2327 theEllipse = function (vml, x, y, rx, ry) {
2328 var g = createNode("group"),
2329 o = createNode("oval"),
2331 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2332 g.coordsize = coordsize;
2333 g.coordorigin = vml.coordorigin;
2335 var res = new Element(o, g, vml);
2336 res.type = "ellipse";
2337 setFillAndStroke(res, {stroke: "#000"});
2342 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2343 vml.canvas[appendChild](g);
2346 theImage = function (vml, src, x, y, w, h) {
2347 var g = createNode("group"),
2348 o = createNode("image"),
2350 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2351 g.coordsize = coordsize;
2352 g.coordorigin = vml.coordorigin;
2355 var res = new Element(o, g, vml);
2357 res.attrs.src = src;
2362 res.setBox({x: x, y: y, width: w, height: h});
2363 vml.canvas[appendChild](g);
2366 theText = function (vml, x, y, text) {
2367 var g = createNode("group"),
2368 el = createNode("shape"),
2370 path = createNode("path"),
2372 o = createNode("textpath");
2373 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2374 g.coordsize = coordsize;
2375 g.coordorigin = vml.coordorigin;
2376 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2377 path.textpathok = true;
2378 ol.width = vml.width;
2379 ol.height = vml.height;
2380 o.string = text + E;
2383 el[appendChild](path);
2385 var res = new Element(o, g, vml);
2387 res.textpath = path;
2389 res.attrs.text = text;
2394 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2396 vml.canvas[appendChild](g);
2399 setSize = function (width, height) {
2400 var cs = this.canvas.style;
2401 width == +width && (width += "px");
2402 height == +height && (height += "px");
2405 cs.clip = "rect(0 " + width + " " + height + " 0)";
2409 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2411 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2412 createNode = function (tagName) {
2413 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2416 createNode = function (tagName) {
2417 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2420 create = function () {
2421 var con = getContainer[apply](0, arguments),
2422 container = con.container,
2423 height = con.height,
2429 throw new Error("VML container not found.");
2431 var res = new Paper,
2432 c = res.canvas = doc.createElement("div"),
2436 width = width || 512;
2437 height = height || 342;
2438 width == +width && (width += "px");
2439 height == +height && (height += "px");
2442 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2443 res.coordorigin = "0 0";
2444 res.span = doc.createElement("span");
2445 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2446 c[appendChild](res.span);
2447 cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2448 if (container == 1) {
2449 doc.body[appendChild](c);
2453 if (container.firstChild) {
2454 container.insertBefore(c, container.firstChild);
2456 container[appendChild](c);
2459 plugins.call(res, res, R.fn);
2462 Paper[proto].clear = function () {
2463 this.canvas.innerHTML = E;
2464 this.span = doc.createElement("span");
2465 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2466 this.canvas[appendChild](this.span);
2467 this.bottom = this.top = null;
2469 Paper[proto].remove = function () {
2470 this.canvas.parentNode.removeChild(this.canvas);
2471 for (var i in this) {
2472 this[i] = removed(i);
2479 // Safari or Chrome (WebKit) rendering bug workaround method
2480 if ((/^Apple|^Google/).test(win.navigator.vendor) && (!(win.navigator.userAgent.indexOf("Version/4.0") + 1) || win.navigator.platform.slice(0, 2) == "iP")) {
2481 Paper[proto].safari = function () {
2482 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2483 win.setTimeout(function () {rect.remove();});
2486 Paper[proto].safari = function () {};
2490 var addEvent = (function () {
2491 if (doc.addEventListener) {
2492 return function (obj, type, fn, element) {
2493 var f = function (e) {
2495 for (var i = 0, ii = e.touches.length; i < ii; i++) {
2496 if (e.touches[i].target == obj) {
2499 e.originalEvent = e;
2503 return fn.call(element, e);
2505 obj.addEventListener(type, f, false);
2506 return function () {
2507 obj.removeEventListener(type, f, false);
2511 } else if (doc.attachEvent) {
2512 return function (obj, type, fn, element) {
2513 var f = function (e) {
2514 return fn.call(element, e || win.event);
2516 obj.attachEvent("on" + type, f);
2517 var detacher = function () {
2518 obj.detachEvent("on" + type, f);
2525 for (var i = events[length]; i--;) {
2526 (function (eventName) {
2527 R[eventName] = Element[proto][eventName] = function (fn) {
2528 if (R.is(fn, "function")) {
2529 var realName = supportsTouch && touchMap[eventName] ? touchMap[eventName] : eventName;
2530 this.events = this.events || [];
2531 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, realName, fn, this)});
2535 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2536 var events = this.events,
2538 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2540 events.splice(l, 1);
2541 !events.length && delete this.events;
2548 Element[proto].hover = function (f_in, f_out) {
2549 return this.mouseover(f_in).mouseout(f_out);
2551 Element[proto].unhover = function (f_in, f_out) {
2552 return this.unmouseover(f_in).unmouseout(f_out);
2554 Element[proto].drag = function (start, f, end) {
2556 var el = this.mousedown(function (e) {
2557 this._drag.x = e.clientX;
2558 this._drag.y = e.clientY;
2559 this._drag.id = e.identifier;
2560 start && start.call(this, e.clientX, e.clientY);
2561 Raphael.mousemove(move).mouseup(up);
2563 move = function (e) {
2564 if (e.changedTouches) {
2565 for (var i = 0, ii = e.changedTouches.length; i < ii; i++) {
2566 var touch = e.changedTouches[i];
2567 if (touch.identifier == el._drag.id) {
2568 f.call(el, touch.clientX - el._drag.x, touch.clientY - el._drag.y);
2573 f.call(el, e.clientX - el._drag.x, e.clientY - el._drag.y);
2578 if (e.changedTouches) {
2579 for (var i = 0, ii = e.changedTouches.length; i < ii; i++) {
2580 if (e.changedTouches[i].identifier == el._drag.id) {
2581 Raphael.unmousemove(move).unmouseup(up);
2582 end && end.call(el);
2586 Raphael.unmousemove(move).unmouseup(up);
2587 end && end.call(el);
2591 Paper[proto].circle = function (x, y, r) {
2592 return theCircle(this, x || 0, y || 0, r || 0);
2594 Paper[proto].rect = function (x, y, w, h, r) {
2595 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2597 Paper[proto].ellipse = function (x, y, rx, ry) {
2598 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2600 Paper[proto].path = function (pathString) {
2601 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2602 return thePath(R.format[apply](R, arguments), this);
2604 Paper[proto].image = function (src, x, y, w, h) {
2605 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2607 Paper[proto].text = function (x, y, text) {
2608 return theText(this, x || 0, y || 0, text || E);
2610 Paper[proto].set = function (itemsArray) {
2611 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2612 return new Set(itemsArray);
2614 Paper[proto].setSize = setSize;
2615 Paper[proto].top = Paper[proto].bottom = null;
2616 Paper[proto].raphael = R;
2618 return this.x + S + this.y;
2620 Element[proto].resetScale = function () {
2626 this.attrs.scale = "1 1";
2628 Element[proto].scale = function (x, y, cx, cy) {
2632 if (x == null && y == null) {
2647 var bb = this.getBBox(),
2648 rcx = bb.x + bb.width / 2,
2649 rcy = bb.y + bb.height / 2,
2652 cx = (+cx || cx == 0) ? cx : rcx;
2653 cy = (+cy || cy == 0) ? cy : rcy;
2654 var dirx = ~~(x / math.abs(x)),
2655 diry = ~~(y / math.abs(y)),
2656 s = this.node.style,
2657 ncx = cx + (rcx - cx) * kx,
2658 ncy = cy + (rcy - cy) * ky;
2659 switch (this.type) {
2662 var neww = a.width * dirx * kx,
2663 newh = a.height * diry * ky;
2666 r: a.r * mmin(dirx * kx, diry * ky),
2675 rx: a.rx * dirx * kx,
2676 ry: a.ry * diry * ky,
2677 r: a.r * mmin(dirx * kx, diry * ky),
2689 var path = pathToRelative(a.path),
2691 for (var i = 0, ii = path[length]; i < ii; i++) {
2693 P0 = upperCase.call(p[0]);
2694 if (P0 == "M" && skip) {
2700 p[path[i][length] - 2] *= kx;
2701 p[path[i][length] - 1] *= ky;
2704 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2705 } else if (P0 == "H") {
2706 for (var j = 1, jj = p[length]; j < jj; j++) {
2709 } else if (P0 == "V") {
2710 for (j = 1, jj = p[length]; j < jj; j++) {
2714 for (j = 1, jj = p[length]; j < jj; j++) {
2715 p[j] *= (j % 2) ? kx : ky;
2719 var dim2 = pathDimensions(path);
2720 dx = ncx - dim2.x - dim2.width / 2;
2721 dy = ncy - dim2.y - dim2.height / 2;
2724 this.attr({path: path});
2727 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2728 if (this.transformations) {
2729 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2730 this.node[setAttribute]("transform", this.transformations[join](S));
2731 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2732 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2733 this.attr({x: dx, y: dy});
2737 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2738 ", M12=0, M21=0, M22=", diry,
2739 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2740 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2743 if (this.transformations) {
2744 this.transformations[2] = E;
2745 this.node[setAttribute]("transform", this.transformations[join](S));
2749 this.node.filterMatrix = E;
2750 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2753 a.scale = [x, y, cx, cy][join](S);
2759 Element[proto].clone = function () {
2760 var attr = this.attr();
2762 delete attr.translation;
2763 return this.paper[this.type]().attr(attr);
2765 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2768 for (var i = 0; i < 1.001; i+=.001) {
2769 var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2770 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2771 if (len >= length) {
2777 getLengthFactory = function (istotal, subpath) {
2778 return function (path, length, onlystart) {
2779 path = path2curve(path);
2780 var x, y, p, l, sp = "", subpaths = {}, point,
2782 for (var i = 0, ii = path.length; i < ii; i++) {
2788 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2789 if (len + l > length) {
2790 if (subpath && !subpaths.start) {
2791 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2792 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2793 if (onlystart) {return sp;}
2794 subpaths.start = sp;
2795 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2801 if (!istotal && !subpath) {
2802 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2803 return {x: point.x, y: point.y, alpha: point.alpha};
2813 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2814 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2818 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2819 var old = {x: 0, y: 0},
2821 for (var i = 0; i < 1.01; i+=.01) {
2822 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2823 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2828 var getTotalLength = getLengthFactory(1),
2829 getPointAtLength = getLengthFactory(),
2830 getSubpathsAtLength = getLengthFactory(0, 1);
2831 Element[proto].getTotalLength = function () {
2832 if (this.type != "path") {return;}
2833 if (this.node.getTotalLength) {
2834 return this.node.getTotalLength();
2836 return getTotalLength(this.attrs.path);
2838 Element[proto].getPointAtLength = function (length) {
2839 if (this.type != "path") {return;}
2840 return getPointAtLength(this.attrs.path, length);
2842 Element[proto].getSubpath = function (from, to) {
2843 if (this.type != "path") {return;}
2844 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2845 return getSubpathsAtLength(this.attrs.path, from).end;
2847 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2848 return from ? getSubpathsAtLength(a, from).end : a;
2851 // animation easing formulas
2852 R.easing_formulas = {
2853 linear: function (n) {
2860 return pow(n - 1, 3) + 1;
2862 "<>": function (n) {
2865 return pow(n, 3) / 2;
2868 return (pow(n, 3) + 2) / 2;
2870 backIn: function (n) {
2872 return n * n * ((s + 1) * n - s);
2874 backOut: function (n) {
2877 return n * n * ((s + 1) * n + s) + 1;
2879 elastic: function (n) {
2880 if (n == 0 || n == 1) {
2885 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2887 bounce: function (n) {
2896 l = s * n * n + .75;
2898 if (n < (2.5 / p)) {
2900 l = s * n * n + .9375;
2903 l = s * n * n + .984375;
2911 var animationElements = {length : 0},
2912 animation = function () {
2913 var Now = +new Date;
2914 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2915 var e = animationElements[l];
2916 if (e.stop || e.el.removed) {
2917 delete animationElements[l];
2918 animationElements[length]--;
2921 var time = Now - e.start,
2930 callback = e.callback,
2934 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2935 for (var attr in from) if (from[has](attr)) {
2936 switch (availableAnimAttrs[attr]) {
2938 now = pos * ms * diff[attr];
2939 to.back && (now = to.len - now);
2940 var point = getPointAtLength(to[attr], now);
2941 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2944 that.translate(point.x - diff.sx, point.y - diff.sy);
2945 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2948 now = +from[attr] + pos * ms * diff[attr];
2952 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2953 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2954 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2959 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2960 now[i] = [from[attr][i][0]];
2961 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2962 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2964 now[i] = now[i][join](S);
2971 var x = diff[attr][0] * (time - prev),
2972 y = diff[attr][1] * (time - prev);
2978 now = +from[attr][0] + pos * ms * diff[attr][0];
2979 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2982 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);
2988 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2997 that._run && that._run.call(that);
3000 point = getPointAtLength(to.along, to.len * !to.back);
3001 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3002 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3004 (t.x || t.y) && that.translate(-t.x, -t.y);
3005 to.scale && (to.scale = to.scale + E);
3007 delete animationElements[l];
3008 animationElements[length]--;
3009 that.in_animation = null;
3010 R.is(callback, "function") && callback.call(that);
3014 R.svg && that && that.paper && that.paper.safari();
3015 animationElements[length] && win.setTimeout(animation);
3017 upto255 = function (color) {
3018 return mmax(mmin(color, 255), 0);
3020 translate = function (x, y) {
3022 return {x: this._.tx, y: this._.ty, toString: x_y};
3026 switch (this.type) {
3029 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3034 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3037 var path = pathToRelative(this.attrs.path);
3040 this.attr({path: path});
3045 Element[proto].animateWith = function (element, params, ms, easing, callback) {
3046 animationElements[element.id] && (params.start = animationElements[element.id].start);
3047 return this.animate(params, ms, easing, callback);
3049 Element[proto].animateAlong = along();
3050 Element[proto].animateAlongBack = along(1);
3051 function along(isBack) {
3052 return function (path, ms, rotate, callback) {
3053 var params = {back: isBack};
3054 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3055 path && path.constructor == Element && (path = path.attrs.path);
3056 path && (params.along = path);
3057 return this.animate(params, ms, callback);
3060 Element[proto].onAnimation = function (f) {
3064 Element[proto].animate = function (params, ms, easing, callback) {
3065 if (R.is(easing, "function") || !easing) {
3066 callback = easing || null;
3071 for (var attr in params) if (params[has](attr)) {
3072 if (availableAnimAttrs[has](attr)) {
3073 from[attr] = this.attr(attr);
3074 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3075 to[attr] = params[attr];
3076 switch (availableAnimAttrs[attr]) {
3078 var len = getTotalLength(params[attr]),
3079 point = getPointAtLength(params[attr], len * !!params.back),
3080 bb = this.getBBox();
3081 diff[attr] = len / ms;
3086 to.rot = params.rot;
3087 to.back = params.back;
3089 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3092 diff[attr] = (to[attr] - from[attr]) / ms;
3095 from[attr] = R.getRGB(from[attr]);
3096 var toColour = R.getRGB(to[attr]);
3098 r: (toColour.r - from[attr].r) / ms,
3099 g: (toColour.g - from[attr].g) / ms,
3100 b: (toColour.b - from[attr].b) / ms
3104 var pathes = path2curve(from[attr], to[attr]);
3105 from[attr] = pathes[0];
3106 var toPath = pathes[1];
3108 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3109 diff[attr][i] = [0];
3110 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3111 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3116 var values = (params[attr] + E)[split](separator),
3117 from2 = (from[attr] + E)[split](separator);
3120 from[attr] = [0, 0];
3121 diff[attr] = [values[0] / ms, values[1] / ms];
3124 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3125 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3128 params[attr] = values;
3129 from[attr] = (from[attr] + E)[split](separator);
3130 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3133 from[attr] = (from[attr] + E)[split](separator);
3137 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3146 this.in_animation = 1;
3147 animationElements[this.id] = {
3148 start: params.start || +new Date,
3158 ++animationElements[length] == 1 && animation();
3161 Element[proto].stop = function () {
3162 animationElements[this.id] && animationElements[length]--;
3163 delete animationElements[this.id];
3166 Element[proto].translate = function (x, y) {
3167 return this.attr({translation: x + " " + y});
3169 Element[proto][toString] = function () {
3170 return "Rapha\xebl\u2019s object";
3172 R.ae = animationElements;
3175 var Set = function (items) {
3180 for (var i = 0, ii = items[length]; i < ii; i++) {
3181 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3182 this[this.items[length]] = this.items[this.items[length]] = items[i];
3188 Set[proto][push] = function () {
3191 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3192 item = arguments[i];
3193 if (item && (item.constructor == Element || item.constructor == Set)) {
3194 len = this.items[length];
3195 this[len] = this.items[len] = item;
3201 Set[proto].pop = function () {
3202 delete this[this[length]--];
3203 return this.items.pop();
3205 for (var method in Element[proto]) if (Element[proto][has](method)) {
3206 Set[proto][method] = (function (methodname) {
3207 return function () {
3208 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3209 this.items[i][methodname][apply](this.items[i], arguments);
3215 Set[proto].attr = function (name, value) {
3216 if (name && R.is(name, array) && R.is(name[0], "object")) {
3217 for (var j = 0, jj = name[length]; j < jj; j++) {
3218 this.items[j].attr(name[j]);
3221 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3222 this.items[i].attr(name, value);
3227 Set[proto].animate = function (params, ms, easing, callback) {
3228 (R.is(easing, "function") || !easing) && (callback = easing || null);
3229 var len = this.items[length],
3233 callback && (collector = function () {
3234 !--len && callback.call(set);
3236 this.items[--i].animate(params, ms, easing || collector, collector);
3238 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3242 Set[proto].insertAfter = function (el) {
3243 var i = this.items[length];
3245 this.items[i].insertAfter(el);
3249 Set[proto].getBBox = function () {
3254 for (var i = this.items[length]; i--;) {
3255 var box = this.items[i].getBBox();
3258 w[push](box.x + box.width);
3259 h[push](box.y + box.height);
3261 x = mmin[apply](0, x);
3262 y = mmin[apply](0, y);
3266 width: mmax[apply](0, w) - x,
3267 height: mmax[apply](0, h) - y
3270 Set[proto].clone = function (s) {
3272 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3273 s[push](this.items[i].clone());
3278 R.registerFont = function (font) {
3282 this.fonts = this.fonts || {};
3288 family = font.face["font-family"];
3289 for (var prop in font.face) if (font.face[has](prop)) {
3290 fontcopy.face[prop] = font.face[prop];
3292 if (this.fonts[family]) {
3293 this.fonts[family][push](fontcopy);
3295 this.fonts[family] = [fontcopy];
3298 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3299 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3300 var path = font.glyphs[glyph];
3301 fontcopy.glyphs[glyph] = {
3304 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3305 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3309 for (var k in path.k) if (path[has](k)) {
3310 fontcopy.glyphs[glyph].k[k] = path.k[k];
3317 Paper[proto].getFont = function (family, weight, style, stretch) {
3318 stretch = stretch || "normal";
3319 style = style || "normal";
3320 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3324 var font = R.fonts[family];
3326 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3327 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3328 if (name.test(fontName)) {
3329 font = R.fonts[fontName];
3336 for (var i = 0, ii = font[length]; i < ii; i++) {
3338 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3345 Paper[proto].print = function (x, y, string, font, size, origin) {
3346 origin = origin || "middle"; // baseline|middle
3347 var out = this.set(),
3348 letters = (string + E)[split](E),
3352 R.is(font, string) && (font = this.getFont(font));
3354 scale = (size || 16) / font.face["units-per-em"];
3355 var bb = font.face.bbox.split(separator),
3357 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3358 for (var i = 0, ii = letters[length]; i < ii; i++) {
3359 var prev = i && font.glyphs[letters[i - 1]] || {},
3360 curr = font.glyphs[letters[i]];
3361 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3362 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3364 out.scale(scale, scale, top, height).translate(x - top, y - height);
3369 var formatrg = /\{(\d+)\}/g;
3370 R.format = function (token, array) {
3371 var args = R.is(array, array) ? [0][concat](array) : arguments;
3372 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3373 return args[++i] == null ? E : args[i];
3377 R.ninja = function () {
3378 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3381 R.el = Element[proto];