2 * Raphael 1.4.5 - 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,
42 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
44 mousedown: "touchstart",
45 mousemove: "touchmove",
50 lowerCase = String[proto].toLowerCase,
57 toString = "toString",
59 objectToString = Object[proto][toString],
64 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
65 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,
67 setAttribute = "setAttribute",
70 ms = " progid:DXImageTransform.Microsoft",
71 upperCase = String[proto].toUpperCase,
72 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},
73 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},
75 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
76 if (R.type == "VML") {
77 var d = doc.createElement("div"),
79 d.innerHTML = '<v:shape adj="1"/>';
81 b.style.behavior = "url(#default#VML)";
82 if (!(b && typeof b.adj == "object")) {
87 R.svg = !(R.vml = R.type == "VML");
88 Paper[proto] = R[proto];
92 R.is = function (o, type) {
93 type = lowerCase.call(type);
94 return (type == "object" && o === Object(o)) ||
95 (type == "undefined" && typeof o == type) ||
96 (type == "null" && o == null) ||
97 (type == "array" && Array.isArray && Array.isArray(o)) ||
98 lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
101 R.setWindow = function (newwin) {
106 var toHex = function (color) {
108 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
109 var trim = /^\s+|\s+$/g;
110 toHex = cacher(function (color) {
112 color = Str(color)[rp](trim, E);
114 var docum = new win.ActiveXObject("htmlfile");
115 docum.write("<body>");
119 bod = win.createPopup().document.body;
121 var range = bod.createTextRange();
123 bod.style.color = color;
124 var value = range.queryCommandValue("ForeColor");
125 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
126 return "#" + ("000000" + value[toString](16)).slice(-6);
132 var i = doc.createElement("i");
133 i.title = "Rapha\xebl Colour Picker";
134 i.style.display = "none";
135 doc.body[appendChild](i);
136 toHex = cacher(function (color) {
137 i.style.color = color;
138 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
143 var hsbtoString = function () {
144 return "hsb(" + [this.h, this.s, this.b] + ")";
146 rgbtoString = function () {
149 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
150 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
158 if (brightness == 0) {
159 return {r: 0, g: 0, b: 0, hex: "#000"};
161 if (hue > 1 || saturation > 1 || brightness > 1) {
168 p = brightness * (1 - saturation),
169 q = brightness * (1 - (saturation * f)),
170 t = brightness * (1 - (saturation * (1 - f)));
171 red = [brightness, q, p, p, t, brightness, brightness][i];
172 green = [t, brightness, brightness, q, p, p, t][i];
173 blue = [p, p, t, brightness, brightness, q, p][i];
177 var rgb = {r: red, g: green, b: blue, toString: rgbtoString},
178 r = (~~red)[toString](16),
179 g = (~~green)[toString](16),
180 b = (~~blue)[toString](16);
184 rgb.hex = "#" + r + g + b;
187 R.rgb2hsb = cacher(function (red, green, blue) {
188 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
193 if (R.is(red, string)) {
194 var clr = R.getRGB(red);
199 if (red > 1 || green > 1 || blue > 1) {
204 var max = mmax(red, green, blue),
205 min = mmin(red, green, blue),
210 return {h: 0, s: 0, b: max};
212 var delta = (max - min);
213 saturation = delta / max;
215 hue = (green - blue) / delta;
216 } else if (green == max) {
217 hue = 2 + ((blue - red) / delta);
219 hue = 4 + ((red - green) / delta);
225 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
227 var p2s = /,?([achlmqrstvxz]),?/gi,
228 commaSpaces = /\s*,\s*/,
229 hsrg = {hs: 1, rg: 1};
230 R._path2string = function () {
231 return this.join(",")[rp](p2s, "$1");
233 function cacher(f, scope, postprocessor) {
235 var arg = Array[proto].slice.call(arguments, 0),
236 args = arg[join]("\u25ba"),
237 cache = newf.cache = newf.cache || {},
238 count = newf.count = newf.count || [];
239 if (cache[has](args)) {
240 return postprocessor ? postprocessor(cache[args]) : cache[args];
242 count[length] >= 1e3 && delete cache[count.shift()];
244 cache[args] = f[apply](scope, arg);
245 return postprocessor ? postprocessor(cache[args]) : cache[args];
250 R.getRGB = cacher(function (colour) {
251 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
252 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
254 if (colour == "none") {
255 return {r: -1, g: -1, b: -1, hex: "none"};
257 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
264 rgb = colour.match(colourRegExp);
267 blue = toInt(rgb[2].substring(5), 16);
268 green = toInt(rgb[2].substring(3, 5), 16);
269 red = toInt(rgb[2].substring(1, 3), 16);
272 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
273 green = toInt((t = rgb[3].charAt(2)) + t, 16);
274 red = toInt((t = rgb[3].charAt(1)) + t, 16);
277 rgb = rgb[4][split](commaSpaces);
278 red = toFloat(rgb[0]);
279 green = toFloat(rgb[1]);
280 blue = toFloat(rgb[2]);
281 opacity = toFloat(rgb[3]);
284 rgb = rgb[5][split](commaSpaces);
285 red = toFloat(rgb[0]) * 2.55;
286 green = toFloat(rgb[1]) * 2.55;
287 blue = toFloat(rgb[2]) * 2.55;
288 opacity = toFloat(rgb[3]);
291 rgb = rgb[6][split](commaSpaces);
292 red = toFloat(rgb[0]);
293 green = toFloat(rgb[1]);
294 blue = toFloat(rgb[2]);
295 return R.hsb2rgb(red, green, blue);
298 rgb = rgb[7][split](commaSpaces);
299 red = toFloat(rgb[0]) * 2.55;
300 green = toFloat(rgb[1]) * 2.55;
301 blue = toFloat(rgb[2]) * 2.55;
302 return R.hsb2rgb(red, green, blue);
304 rgb = {r: red, g: green, b: blue};
305 var r = (~~red)[toString](16),
306 g = (~~green)[toString](16),
307 b = (~~blue)[toString](16);
311 rgb.hex = "#" + r + g + b;
312 isFinite(toFloat(opacity)) && (rgb.o = opacity);
315 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
317 R.getColor = function (value) {
318 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
319 rgb = this.hsb2rgb(start.h, start.s, start.b);
324 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
328 R.getColor.reset = function () {
332 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
333 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
334 R.parsePathString = cacher(function (pathString) {
338 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
340 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
341 data = pathClone(pathString);
344 Str(pathString)[rp](pathCommand, function (a, b, c) {
346 name = lowerCase.call(b);
347 c[rp](pathValues, function (a, b) {
348 b && params[push](+b);
350 if (name == "m" && params[length] > 2) {
351 data[push]([b][concat](params.splice(0, 2)));
353 b = b == "m" ? "l" : "L";
355 while (params[length] >= paramCounts[name]) {
356 data[push]([b][concat](params.splice(0, paramCounts[name])));
357 if (!paramCounts[name]) {
363 data[toString] = R._path2string;
366 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
368 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
369 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
370 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
371 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
372 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
373 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
374 ax = (1 - t) * p1x + t * c1x,
375 ay = (1 - t) * p1y + t * c1y,
376 cx = (1 - t) * c2x + t * p2x,
377 cy = (1 - t) * c2y + t * p2y,
378 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
379 (mx > nx || my < ny) && (alpha += 180);
380 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};
382 var pathDimensions = cacher(function (path) {
384 return {x: 0, y: 0, width: 0, height: 0};
386 path = path2curve(path);
392 for (var i = 0, ii = path[length]; i < ii; i++) {
400 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
401 X = X[concat](dim.min.x, dim.max.x);
402 Y = Y[concat](dim.min.y, dim.max.y);
407 var xmin = mmin[apply](0, X),
408 ymin = mmin[apply](0, Y);
412 width: mmax[apply](0, X) - xmin,
413 height: mmax[apply](0, Y) - ymin
416 pathClone = function (pathArray) {
418 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
419 pathArray = R.parsePathString(pathArray);
421 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
423 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
424 res[i][j] = pathArray[i][j];
427 res[toString] = R._path2string;
430 pathToRelative = cacher(function (pathArray) {
431 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
432 pathArray = R.parsePathString(pathArray);
440 if (pathArray[0][0] == "M") {
446 res[push](["M", x, y]);
448 for (var i = start, ii = pathArray[length]; i < ii; i++) {
451 if (pa[0] != lowerCase.call(pa[0])) {
452 r[0] = lowerCase.call(pa[0]);
460 r[6] = +(pa[6] - x).toFixed(3);
461 r[7] = +(pa[7] - y).toFixed(3);
464 r[1] = +(pa[1] - y).toFixed(3);
470 for (var j = 1, jj = pa[length]; j < jj; j++) {
471 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
480 for (var k = 0, kk = pa[length]; k < kk; k++) {
484 var len = res[i][length];
491 x += +res[i][len - 1];
494 y += +res[i][len - 1];
497 x += +res[i][len - 2];
498 y += +res[i][len - 1];
501 res[toString] = R._path2string;
504 pathToAbsolute = cacher(function (pathArray) {
505 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
506 pathArray = R.parsePathString(pathArray);
514 if (pathArray[0][0] == "M") {
515 x = +pathArray[0][1];
516 y = +pathArray[0][2];
520 res[0] = ["M", x, y];
522 for (var i = start, ii = pathArray[length]; i < ii; i++) {
525 if (pa[0] != upperCase.call(pa[0])) {
526 r[0] = upperCase.call(pa[0]);
547 for (var j = 1, jj = pa[length]; j < jj; j++) {
548 r[j] = +pa[j] + ((j % 2) ? x : y);
552 for (var k = 0, kk = pa[length]; k < kk; k++) {
568 mx = res[i][res[i][length] - 2];
569 my = res[i][res[i][length] - 1];
571 x = res[i][res[i][length] - 2];
572 y = res[i][res[i][length] - 1];
575 res[toString] = R._path2string;
578 l2c = function (x1, y1, x2, y2) {
579 return [x1, y1, x2, y2, x2, y2];
581 q2c = function (x1, y1, ax, ay, x2, y2) {
593 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
594 // for more information of where this math came from visit:
595 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
597 _120 = PI * 120 / 180,
598 rad = PI / 180 * (+angle || 0),
601 rotate = cacher(function (x, y, rad) {
602 var X = x * math.cos(rad) - y * math.sin(rad),
603 Y = x * math.sin(rad) + y * math.cos(rad);
607 xy = rotate(x1, y1, -rad);
610 xy = rotate(x2, y2, -rad);
613 var cos = math.cos(PI / 180 * angle),
614 sin = math.sin(PI / 180 * angle),
617 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
625 k = (large_arc_flag == sweep_flag ? -1 : 1) *
626 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
627 cx = k * rx * y / ry + (x1 + x2) / 2,
628 cy = k * -ry * x / rx + (y1 + y2) / 2,
629 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
630 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
632 f1 = x1 < cx ? PI - f1 : f1;
633 f2 = x2 < cx ? PI - f2 : f2;
634 f1 < 0 && (f1 = PI * 2 + f1);
635 f2 < 0 && (f2 = PI * 2 + f2);
636 if (sweep_flag && f1 > f2) {
639 if (!sweep_flag && f2 > f1) {
649 if (math.abs(df) > _120) {
653 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
654 x2 = cx + rx * math.cos(f2);
655 y2 = cy + ry * math.sin(f2);
656 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
659 var c1 = math.cos(f1),
663 t = math.tan(df / 4),
667 m2 = [x1 + hx * s1, y1 - hy * c1],
668 m3 = [x2 + hx * s2, y2 - hy * c2],
670 m2[0] = 2 * m1[0] - m2[0];
671 m2[1] = 2 * m1[1] - m2[1];
673 return [m2, m3, m4][concat](res);
675 res = [m2, m3, m4][concat](res)[join]()[split](",");
677 for (var i = 0, ii = res[length]; i < ii; i++) {
678 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
683 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
686 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
687 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
690 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
691 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
692 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
694 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
695 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
699 math.abs(t1) > 1e12 && (t1 = .5);
700 math.abs(t2) > 1e12 && (t2 = .5);
701 if (t1 > 0 && t1 < 1) {
702 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
706 if (t2 > 0 && t2 < 1) {
707 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
711 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
712 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
714 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
715 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
716 math.abs(t1) > 1e12 && (t1 = .5);
717 math.abs(t2) > 1e12 && (t2 = .5);
718 if (t1 > 0 && t1 < 1) {
719 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
723 if (t2 > 0 && t2 < 1) {
724 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
729 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
730 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
733 path2curve = cacher(function (path, path2) {
734 var p = pathToAbsolute(path),
735 p2 = path2 && pathToAbsolute(path2),
736 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
737 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
738 processPath = function (path, d) {
741 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
743 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
750 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
753 nx = d.x + (d.x - (d.bx || d.x));
754 ny = d.y + (d.y - (d.by || d.y));
755 path = ["C", nx, ny][concat](path.slice(1));
758 d.qx = d.x + (d.x - (d.qx || d.x));
759 d.qy = d.y + (d.y - (d.qy || d.y));
760 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
765 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
768 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
771 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
774 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
777 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
782 fixArc = function (pp, i) {
783 if (pp[i][length] > 7) {
787 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
790 ii = mmax(p[length], p2 && p2[length] || 0);
793 fixM = function (path1, path2, a1, a2, i) {
794 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
795 path2.splice(i, 0, ["M", a2.x, a2.y]);
800 ii = mmax(p[length], p2 && p2[length] || 0);
803 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
804 p[i] = processPath(p[i], attrs);
806 p2 && (p2[i] = processPath(p2[i], attrs2));
808 fixM(p, p2, attrs, attrs2, i);
809 fixM(p2, p, attrs2, attrs, i);
812 seglen = seg[length],
813 seg2len = p2 && seg2[length];
814 attrs.x = seg[seglen - 2];
815 attrs.y = seg[seglen - 1];
816 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
817 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
818 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
819 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
820 attrs2.x = p2 && seg2[seg2len - 2];
821 attrs2.y = p2 && seg2[seg2len - 1];
823 return p2 ? [p, p2] : p;
825 parseDots = cacher(function (gradient) {
827 for (var i = 0, ii = gradient[length]; i < ii; i++) {
829 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
830 dot.color = R.getRGB(par[1]);
831 if (dot.color.error) {
834 dot.color = dot.color.hex;
835 par[2] && (dot.offset = par[2] + "%");
838 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
839 if (!dots[i].offset) {
840 var start = toFloat(dots[i - 1].offset || 0),
842 for (var j = i + 1; j < ii; j++) {
843 if (dots[j].offset) {
844 end = dots[j].offset;
853 var d = (end - start) / (j - i + 1);
856 dots[i].offset = start + "%";
862 getContainer = function (x, y, w, h) {
864 if (R.is(x, string) || R.is(x, "object")) {
865 container = R.is(x, string) ? doc.getElementById(x) : x;
866 if (container.tagName) {
869 container: container,
870 width: container.style.pixelWidth || container.offsetWidth,
871 height: container.style.pixelHeight || container.offsetHeight
874 return {container: container, width: y, height: w};
878 return {container: 1, x: x, y: y, width: w, height: h};
881 plugins = function (con, add) {
883 for (var prop in add) {
884 if (add[has](prop) && !(prop in con)) {
885 switch (typeof add[prop]) {
888 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
892 con[prop] = con[prop] || {};
893 plugins.call(this, con[prop], add[prop]);
896 con[prop] = add[prop];
902 tear = function (el, paper) {
903 el == paper.top && (paper.top = el.prev);
904 el == paper.bottom && (paper.bottom = el.next);
905 el.next && (el.next.prev = el.prev);
906 el.prev && (el.prev.next = el.next);
908 tofront = function (el, paper) {
909 if (paper.top === el) {
918 toback = function (el, paper) {
919 if (paper.bottom === el) {
923 el.next = paper.bottom;
925 paper.bottom.prev = el;
928 insertafter = function (el, el2, paper) {
930 el2 == paper.top && (paper.top = el);
931 el2.next && (el2.next.prev = el);
936 insertbefore = function (el, el2, paper) {
938 el2 == paper.bottom && (paper.bottom = el);
939 el2.prev && (el2.prev.next = el);
944 removed = function (methodname) {
946 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
949 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
950 R.pathToRelative = pathToRelative;
953 Paper[proto].svgns = "http://www.w3.org/2000/svg";
954 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
955 round = function (num) {
956 return +num + (~~num === num) * .5;
958 var $ = function (el, attr) {
960 for (var key in attr) {
961 if (attr[has](key)) {
962 el[setAttribute](key, Str(attr[key]));
966 el = doc.createElementNS(Paper[proto].svgns, el);
967 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
971 R[toString] = function () {
972 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
974 var thePath = function (pathString, SVG) {
976 SVG.canvas && SVG.canvas[appendChild](el);
977 var p = new Element(el, SVG);
979 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
982 var addGradientFill = function (o, gradient, SVG) {
986 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
991 var dir = ((fy > .5) * 2 - 1);
992 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
993 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
995 (fy = fy.toFixed(5) - 1e-5 * dir);
999 gradient = gradient[split](/\s*\-\s*/);
1000 if (type == "linear") {
1001 var angle = gradient.shift();
1002 angle = -toFloat(angle);
1006 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1007 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1010 if (vector[2] < 0) {
1011 vector[0] = -vector[2];
1014 if (vector[3] < 0) {
1015 vector[1] = -vector[3];
1019 var dots = parseDots(gradient);
1023 var id = o.getAttribute(fillString);
1024 id = id.match(/^url\(#(.*)\)$/);
1025 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1027 var el = $(type + "Gradient");
1028 el.id = "r" + (R._id++)[toString](36);
1029 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1030 SVG.defs[appendChild](el);
1031 for (var i = 0, ii = dots[length]; i < ii; i++) {
1032 var stop = $("stop");
1034 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1035 "stop-color": dots[i].color || "#fff"
1037 el[appendChild](stop);
1040 fill: "url(#" + el.id + ")",
1049 var updatePosition = function (o) {
1050 var bbox = o.getBBox();
1051 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1053 var setFillAndStroke = function (o, params) {
1060 "-..": [3, 1, 1, 1, 1, 1],
1064 "- .": [4, 3, 1, 3],
1065 "--.": [8, 3, 1, 3],
1066 "--..": [8, 3, 1, 3, 1, 3]
1071 addDashes = function (o, value) {
1072 value = dasharray[lowerCase.call(value)];
1074 var width = o.attrs["stroke-width"] || "1",
1075 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1077 var i = value[length];
1079 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1081 $(node, {"stroke-dasharray": dashes[join](",")});
1084 params[has]("rotation") && (rot = params.rotation);
1085 var rotxy = Str(rot)[split](separator);
1086 if (!(rotxy.length - 1)) {
1089 rotxy[1] = +rotxy[1];
1090 rotxy[2] = +rotxy[2];
1092 toFloat(rot) && o.rotate(0, true);
1093 for (var att in params) {
1094 if (params[has](att)) {
1095 if (!availableAttrs[has](att)) {
1098 var value = params[att];
1105 o.rotate(value, true);
1110 var pn = node.parentNode;
1111 if (lowerCase.call(pn.tagName) != "a") {
1113 pn.insertBefore(hl, node);
1114 hl[appendChild](node);
1117 pn.setAttributeNS(o.paper.xlink, att, value);
1120 node.style.cursor = value;
1123 var rect = Str(value)[split](separator);
1124 if (rect[length] == 4) {
1125 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1126 var el = $("clipPath"),
1128 el.id = "r" + (R._id++)[toString](36);
1135 el[appendChild](rc);
1136 o.paper.defs[appendChild](el);
1137 $(node, {"clip-path": "url(#" + el.id + ")"});
1141 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1142 clip && clip.parentNode.removeChild(clip);
1143 $(node, {"clip-path": E});
1148 if (o.type == "path") {
1149 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1153 node[setAttribute](att, value);
1162 value = -attrs.x - (attrs.width || 0);
1165 if (att == "rx" && o.type == "rect") {
1169 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1170 node[setAttribute](att, value);
1171 o.pattern && updatePosition(o);
1174 node[setAttribute](att, value);
1183 value = -attrs.y - (attrs.height || 0);
1186 if (att == "ry" && o.type == "rect") {
1190 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1191 node[setAttribute](att, value);
1192 o.pattern && updatePosition(o);
1195 if (o.type == "rect") {
1196 $(node, {rx: value, ry: value});
1198 node[setAttribute](att, value);
1202 if (o.type == "image") {
1203 node.setAttributeNS(o.paper.xlink, "href", value);
1206 case "stroke-width":
1207 node.style.strokeWidth = value;
1208 // Need following line for Firefox
1209 node[setAttribute](att, value);
1210 if (attrs["stroke-dasharray"]) {
1211 addDashes(o, attrs["stroke-dasharray"]);
1214 case "stroke-dasharray":
1215 addDashes(o, value);
1218 var xy = Str(value)[split](separator);
1219 xy[0] = +xy[0] || 0;
1220 xy[1] = +xy[1] || 0;
1225 translate.call(o, xy[0], xy[1]);
1228 xy = Str(value)[split](separator);
1229 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1232 var isURL = Str(value).match(ISURL);
1235 var ig = $("image");
1236 el.id = "r" + (R._id++)[toString](36);
1237 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1238 $(ig, {x: 0, y: 0});
1239 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1240 el[appendChild](ig);
1242 var img = doc.createElement("img");
1243 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1244 img.onload = function () {
1245 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1246 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1247 doc.body.removeChild(this);
1250 doc.body[appendChild](img);
1252 o.paper.defs[appendChild](el);
1253 node.style.fill = "url(#" + el.id + ")";
1254 $(node, {fill: "url(#" + el.id + ")"});
1256 o.pattern && updatePosition(o);
1259 var clr = R.getRGB(value);
1261 delete params.gradient;
1262 delete attrs.gradient;
1263 !R.is(attrs.opacity, "undefined") &&
1264 R.is(params.opacity, "undefined") &&
1265 $(node, {opacity: attrs.opacity});
1266 !R.is(attrs["fill-opacity"], "undefined") &&
1267 R.is(params["fill-opacity"], "undefined") &&
1268 $(node, {"fill-opacity": attrs["fill-opacity"]});
1269 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1270 attrs.gradient = value;
1271 attrs.fill = "none";
1274 clr[has]("o") && $(node, {"fill-opacity": clr.o / 100});
1276 clr = R.getRGB(value);
1277 node[setAttribute](att, clr.hex);
1278 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o / 100});
1281 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1284 case "fill-opacity":
1285 if (attrs.gradient) {
1286 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1288 var stops = gradient.getElementsByTagName("stop");
1289 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1294 att == "font-size" && (value = toInt(value, 10) + "px");
1295 var cssrule = att[rp](/(\-.)/g, function (w) {
1296 return upperCase.call(w.substring(1));
1298 node.style[cssrule] = value;
1299 // Need following line for Firefox
1300 node[setAttribute](att, value);
1306 tuneText(o, params);
1308 o.rotate(rotxy.join(S));
1310 toFloat(rot) && o.rotate(rot, true);
1314 tuneText = function (el, params) {
1315 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1320 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1322 if (params[has]("text")) {
1323 a.text = params.text;
1324 while (node.firstChild) {
1325 node.removeChild(node.firstChild);
1327 var texts = Str(params.text)[split]("\n");
1328 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1329 var tspan = $("tspan");
1330 i && $(tspan, {dy: fontSize * leading, x: a.x});
1331 tspan[appendChild](doc.createTextNode(texts[i]));
1332 node[appendChild](tspan);
1335 texts = node.getElementsByTagName("tspan");
1336 for (i = 0, ii = texts[length]; i < ii; i++) {
1337 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1341 var bb = el.getBBox(),
1342 dif = a.y - (bb.y + bb.height / 2);
1343 dif && isFinite(dif) && $(node, {y: a.y + dif});
1345 Element = function (node, svg) {
1351 node.raphael = this;
1353 this.attrs = this.attrs || {};
1354 this.transformations = []; // rotate, translate, scale
1358 rt: {deg: 0, cx: 0, cy: 0},
1362 !svg.bottom && (svg.bottom = this);
1363 this.prev = svg.top;
1364 svg.top && (svg.top.next = this);
1368 Element[proto].rotate = function (deg, cx, cy) {
1374 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1376 return this._.rt.deg;
1378 var bbox = this.getBBox();
1379 deg = Str(deg)[split](separator);
1380 if (deg[length] - 1) {
1381 cx = toFloat(deg[1]);
1382 cy = toFloat(deg[2]);
1384 deg = toFloat(deg[0]);
1386 this._.rt.deg = deg;
1388 this._.rt.deg += deg;
1390 (cy == null) && (cx = null);
1393 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1394 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1395 if (this._.rt.deg) {
1396 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1397 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1399 this.transformations[0] = E;
1400 this.clip && $(this.clip, {transform: E});
1402 $(this.node, {transform: this.transformations[join](S)});
1405 Element[proto].hide = function () {
1406 !this.removed && (this.node.style.display = "none");
1409 Element[proto].show = function () {
1410 !this.removed && (this.node.style.display = "");
1413 Element[proto].remove = function () {
1417 tear(this, this.paper);
1418 this.node.parentNode.removeChild(this.node);
1419 for (var i in this) {
1422 this.removed = true;
1424 Element[proto].getBBox = function () {
1428 if (this.type == "path") {
1429 return pathDimensions(this.attrs.path);
1431 if (this.node.style.display == "none") {
1437 bbox = this.node.getBBox();
1439 // Firefox 3.0.x plays badly here
1443 if (this.type == "text") {
1444 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1445 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1446 var bb = this.node.getExtentOfChar(i);
1447 (bb.y < bbox.y) && (bbox.y = bb.y);
1448 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1449 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1452 hide && this.hide();
1455 Element[proto].attr = function (name, value) {
1461 for (var i in this.attrs) if (this.attrs[has](i)) {
1462 res[i] = this.attrs[i];
1464 this._.rt.deg && (res.rotation = this.rotate());
1465 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1466 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1469 if (value == null && R.is(name, string)) {
1470 if (name == "translation") {
1471 return translate.call(this);
1473 if (name == "rotation") {
1474 return this.rotate();
1476 if (name == "scale") {
1477 return this.scale();
1479 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1480 return this.attrs.gradient;
1482 return this.attrs[name];
1484 if (value == null && R.is(name, array)) {
1486 for (var j = 0, jj = name.length; j < jj; j++) {
1487 values[name[j]] = this.attr(name[j]);
1491 if (value != null) {
1493 params[name] = value;
1494 setFillAndStroke(this, params);
1495 } else if (name != null && R.is(name, "object")) {
1496 setFillAndStroke(this, name);
1500 Element[proto].toFront = function () {
1504 this.node.parentNode[appendChild](this.node);
1505 var svg = this.paper;
1506 svg.top != this && tofront(this, svg);
1509 Element[proto].toBack = function () {
1513 if (this.node.parentNode.firstChild != this.node) {
1514 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1515 toback(this, this.paper);
1516 var svg = this.paper;
1520 Element[proto].insertAfter = function (element) {
1524 var node = element.node || element[element.length].node;
1525 if (node.nextSibling) {
1526 node.parentNode.insertBefore(this.node, node.nextSibling);
1528 node.parentNode[appendChild](this.node);
1530 insertafter(this, element, this.paper);
1533 Element[proto].insertBefore = function (element) {
1537 var node = element.node || element[0].node;
1538 node.parentNode.insertBefore(this.node, node);
1539 insertbefore(this, element, this.paper);
1542 Element[proto].blur = function (size) {
1543 // Experimental. No Safari support. Use it on your own risk.
1546 var fltr = $("filter"),
1547 blur = $("feGaussianBlur");
1548 t.attrs.blur = size;
1549 fltr.id = "r" + (R._id++)[toString](36);
1550 $(blur, {stdDeviation: +size || 1.5});
1551 fltr.appendChild(blur);
1552 t.paper.defs.appendChild(fltr);
1554 $(t.node, {filter: "url(#" + fltr.id + ")"});
1557 t._blur.parentNode.removeChild(t._blur);
1559 delete t.attrs.blur;
1561 t.node.removeAttribute("filter");
1564 var theCircle = function (svg, x, y, r) {
1565 var el = $("circle");
1566 svg.canvas && svg.canvas[appendChild](el);
1567 var res = new Element(el, svg);
1568 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1569 res.type = "circle";
1573 var theRect = function (svg, x, y, w, h, r) {
1575 svg.canvas && svg.canvas[appendChild](el);
1576 var res = new Element(el, svg);
1577 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1582 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 pathlike = {path: 1, rect: 1},
1683 path2vml = function (path) {
1684 var total = /[ahqstv]/ig,
1685 command = pathToAbsolute;
1686 Str(path).match(total) && (command = path2curve);
1688 if (command == pathToAbsolute && !Str(path).match(total)) {
1689 var res = Str(path)[rp](bites, function (all, command, args) {
1691 isMove = lowerCase.call(command) == "m",
1693 args[rp](val, function (value) {
1694 if (isMove && vals[length] == 2) {
1695 res += vals + map[command == "m" ? "l" : "L"];
1698 vals[push](round(value * zoom));
1704 var pa = command(path), p, r;
1706 for (var i = 0, ii = pa[length]; i < ii; i++) {
1708 r = lowerCase.call(pa[i][0]);
1709 r == "z" && (r = "x");
1710 for (var j = 1, jj = p[length]; j < jj; j++) {
1711 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1715 return res[join](S);
1718 R[toString] = function () {
1719 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1721 thePath = function (pathString, vml) {
1722 var g = createNode("group");
1723 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1724 g.coordsize = vml.coordsize;
1725 g.coordorigin = vml.coordorigin;
1726 var el = createNode("shape"), ol = el.style;
1727 ol.width = vml.width + "px";
1728 ol.height = vml.height + "px";
1729 el.coordsize = coordsize;
1730 el.coordorigin = vml.coordorigin;
1732 var p = new Element(el, g, vml),
1733 attr = {fill: "none", stroke: "#000"};
1734 pathString && (attr.path = pathString);
1735 p.isAbsolute = true;
1739 setFillAndStroke(p, attr);
1740 vml.canvas[appendChild](g);
1743 setFillAndStroke = function (o, params) {
1744 o.attrs = o.attrs || {};
1749 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1752 for (var par in params) if (params[has](par)) {
1753 a[par] = params[par];
1756 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1762 params.href && (node.href = params.href);
1763 params.title && (node.title = params.title);
1764 params.target && (node.target = params.target);
1765 params.cursor && (s.cursor = params.cursor);
1766 "blur" in params && o.blur(params.blur);
1767 if (params.path && o.type == "path" || newpath) {
1768 node.path = path2vml(a.path);
1770 if (params.rotation != null) {
1771 o.rotate(params.rotation, true);
1773 if (params.translation) {
1774 xy = Str(params.translation)[split](separator);
1775 translate.call(o, xy[0], xy[1]);
1776 if (o._.rt.cx != null) {
1777 o._.rt.cx +=+ xy[0];
1778 o._.rt.cy +=+ xy[1];
1779 o.setBox(o.attrs, xy[0], xy[1]);
1783 xy = Str(params.scale)[split](separator);
1784 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1786 if ("clip-rect" in params) {
1787 var rect = Str(params["clip-rect"])[split](separator);
1788 if (rect[length] == 4) {
1789 rect[2] = +rect[2] + (+rect[0]);
1790 rect[3] = +rect[3] + (+rect[1]);
1791 var div = node.clipRect || doc.createElement("div"),
1793 group = node.parentNode;
1794 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1795 if (!node.clipRect) {
1796 dstyle.position = "absolute";
1799 dstyle.width = o.paper.width + "px";
1800 dstyle.height = o.paper.height + "px";
1801 group.parentNode.insertBefore(div, group);
1802 div[appendChild](group);
1803 node.clipRect = div;
1806 if (!params["clip-rect"]) {
1807 node.clipRect && (node.clipRect.style.clip = E);
1810 if (o.type == "image" && params.src) {
1811 node.src = params.src;
1813 if (o.type == "image" && params.opacity) {
1814 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1815 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1817 params.font && (s.font = params.font);
1818 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1819 params["font-size"] && (s.fontSize = params["font-size"]);
1820 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1821 params["font-style"] && (s.fontStyle = params["font-style"]);
1822 if (params.opacity != null ||
1823 params["stroke-width"] != null ||
1824 params.fill != null ||
1825 params.stroke != null ||
1826 params["stroke-width"] != null ||
1827 params["stroke-opacity"] != null ||
1828 params["fill-opacity"] != null ||
1829 params["stroke-dasharray"] != null ||
1830 params["stroke-miterlimit"] != null ||
1831 params["stroke-linejoin"] != null ||
1832 params["stroke-linecap"] != null) {
1833 node = o.shape || node;
1834 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1836 !fill && (newfill = fill = createNode(fillString));
1837 if ("fill-opacity" in params || "opacity" in params) {
1838 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1839 opacity < 0 && (opacity = 0);
1840 opacity > 1 && (opacity = 1);
1841 fill.opacity = opacity;
1843 params.fill && (fill.on = true);
1844 if (fill.on == null || params.fill == "none") {
1847 if (fill.on && params.fill) {
1848 var isURL = params.fill.match(ISURL);
1850 fill.src = isURL[1];
1853 fill.color = R.getRGB(params.fill).hex;
1855 fill.type = "solid";
1856 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
1858 a.gradient = params.fill;
1862 newfill && node[appendChild](fill);
1863 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1865 !stroke && (newstroke = stroke = createNode("stroke"));
1866 if ((params.stroke && params.stroke != "none") ||
1867 params["stroke-width"] ||
1868 params["stroke-opacity"] != null ||
1869 params["stroke-dasharray"] ||
1870 params["stroke-miterlimit"] ||
1871 params["stroke-linejoin"] ||
1872 params["stroke-linecap"]) {
1875 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1876 var strokeColor = R.getRGB(params.stroke);
1877 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
1878 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
1879 var width = (toFloat(params["stroke-width"]) || 1) * .75;
1880 opacity < 0 && (opacity = 0);
1881 opacity > 1 && (opacity = 1);
1882 params["stroke-width"] == null && (width = a["stroke-width"]);
1883 params["stroke-width"] && (stroke.weight = width);
1884 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1885 stroke.opacity = opacity;
1887 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1888 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1889 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1890 if (params["stroke-dasharray"]) {
1894 "-.": "shortdashdot",
1895 "-..": "shortdashdotdot",
1900 "--.": "longdashdot",
1901 "--..": "longdashdotdot"
1903 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1905 newstroke && node[appendChild](stroke);
1907 if (res.type == "text") {
1908 s = res.paper.span.style;
1909 a.font && (s.font = a.font);
1910 a["font-family"] && (s.fontFamily = a["font-family"]);
1911 a["font-size"] && (s.fontSize = a["font-size"]);
1912 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1913 a["font-style"] && (s.fontStyle = a["font-style"]);
1914 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1915 res.W = a.w = res.paper.span.offsetWidth;
1916 res.H = a.h = res.paper.span.offsetHeight;
1918 res.Y = a.y + round(res.H / 2);
1920 // text-anchor emulationm
1921 switch (a["text-anchor"]) {
1923 res.node.style["v-text-align"] = "left";
1924 res.bbx = round(res.W / 2);
1927 res.node.style["v-text-align"] = "right";
1928 res.bbx = -round(res.W / 2);
1931 res.node.style["v-text-align"] = "center";
1936 addGradientFill = function (o, gradient) {
1937 o.attrs = o.attrs || {};
1938 var attrs = o.attrs,
1942 o.attrs.gradient = gradient;
1943 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
1948 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1953 gradient = gradient[split](/\s*\-\s*/);
1954 if (type == "linear") {
1955 var angle = gradient.shift();
1956 angle = -toFloat(angle);
1961 var dots = parseDots(gradient);
1965 o = o.shape || o.node;
1966 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
1967 !fill.parentNode && o.appendChild(fill);
1970 fill.method = "none";
1971 fill.color = dots[0].color;
1972 fill.color2 = dots[dots[length] - 1].color;
1974 for (var i = 0, ii = dots[length]; i < ii; i++) {
1975 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1977 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
1978 if (type == "radial") {
1979 fill.type = "gradientradial";
1980 fill.focus = "100%";
1981 fill.focussize = fxfy;
1982 fill.focusposition = fxfy;
1984 fill.type = "gradient";
1985 fill.angle = (270 - angle) % 360;
1990 Element = function (node, group, vml) {
1998 node.raphael = this;
2011 !vml.bottom && (vml.bottom = this);
2012 this.prev = vml.top;
2013 vml.top && (vml.top.next = this);
2017 Element[proto].rotate = function (deg, cx, cy) {
2023 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2025 return this._.rt.deg;
2027 deg = Str(deg)[split](separator);
2028 if (deg[length] - 1) {
2029 cx = toFloat(deg[1]);
2030 cy = toFloat(deg[2]);
2032 deg = toFloat(deg[0]);
2034 this._.rt.deg = deg;
2036 this._.rt.deg += deg;
2038 cy == null && (cx = null);
2041 this.setBox(this.attrs, cx, cy);
2042 this.Group.style.rotation = this._.rt.deg;
2043 // gradient fix for rotation. TODO
2044 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2045 // fill = fill[0] || {};
2046 // var b = ((360 - this._.rt.deg) - 270) % 360;
2047 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2050 Element[proto].setBox = function (params, cx, cy) {
2054 var gs = this.Group.style,
2055 os = (this.shape && this.shape.style) || this.node.style;
2056 params = params || {};
2057 for (var i in params) if (params[has](i)) {
2058 this.attrs[i] = params[i];
2060 cx = cx || this._.rt.cx;
2061 cy = cy || this._.rt.cy;
2062 var attr = this.attrs,
2067 switch (this.type) {
2069 x = attr.cx - attr.r;
2070 y = attr.cy - attr.r;
2074 x = attr.cx - attr.rx;
2075 y = attr.cy - attr.ry;
2082 w = attr.width || 0;
2083 h = attr.height || 0;
2086 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2087 x = attr.x - round(this.W / 2);
2088 y = attr.y - this.H / 2;
2094 if (!this.attrs.path) {
2097 w = this.paper.width;
2098 h = this.paper.height;
2100 var dim = pathDimensions(this.attrs.path);
2110 w = this.paper.width;
2111 h = this.paper.height;
2114 cx = (cx == null) ? x + w / 2 : cx;
2115 cy = (cy == null) ? y + h / 2 : cy;
2116 var left = cx - this.paper.width / 2,
2117 top = cy - this.paper.height / 2, t;
2118 gs.left != (t = left + "px") && (gs.left = t);
2119 gs.top != (t = top + "px") && (gs.top = t);
2120 this.X = pathlike[has](this.type) ? -left : x;
2121 this.Y = pathlike[has](this.type) ? -top : y;
2124 if (pathlike[has](this.type)) {
2125 os.left != (t = -left * zoom + "px") && (os.left = t);
2126 os.top != (t = -top * zoom + "px") && (os.top = t);
2127 } else if (this.type == "text") {
2128 os.left != (t = -left + "px") && (os.left = t);
2129 os.top != (t = -top + "px") && (os.top = t);
2131 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2132 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2133 os.left != (t = x - left + "px") && (os.left = t);
2134 os.top != (t = y - top + "px") && (os.top = t);
2135 os.width != (t = w + "px") && (os.width = t);
2136 os.height != (t = h + "px") && (os.height = t);
2139 Element[proto].hide = function () {
2140 !this.removed && (this.Group.style.display = "none");
2143 Element[proto].show = function () {
2144 !this.removed && (this.Group.style.display = "block");
2147 Element[proto].getBBox = function () {
2151 if (pathlike[has](this.type)) {
2152 return pathDimensions(this.attrs.path);
2155 x: this.X + (this.bbx || 0),
2161 Element[proto].remove = function () {
2165 tear(this, this.paper);
2166 this.node.parentNode.removeChild(this.node);
2167 this.Group.parentNode.removeChild(this.Group);
2168 this.shape && this.shape.parentNode.removeChild(this.shape);
2169 for (var i in this) {
2172 this.removed = true;
2174 Element[proto].attr = function (name, value) {
2180 for (var i in this.attrs) if (this.attrs[has](i)) {
2181 res[i] = this.attrs[i];
2183 this._.rt.deg && (res.rotation = this.rotate());
2184 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2185 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2188 if (value == null && R.is(name, string)) {
2189 if (name == "translation") {
2190 return translate.call(this);
2192 if (name == "rotation") {
2193 return this.rotate();
2195 if (name == "scale") {
2196 return this.scale();
2198 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2199 return this.attrs.gradient;
2201 return this.attrs[name];
2203 if (this.attrs && value == null && R.is(name, array)) {
2204 var ii, values = {};
2205 for (i = 0, ii = name[length]; i < ii; i++) {
2206 values[name[i]] = this.attr(name[i]);
2211 if (value != null) {
2213 params[name] = value;
2215 value == null && R.is(name, "object") && (params = name);
2217 if (params.text && this.type == "text") {
2218 this.node.string = params.text;
2220 setFillAndStroke(this, params);
2221 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2222 addGradientFill(this, params.gradient);
2224 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2228 Element[proto].toFront = function () {
2229 !this.removed && this.Group.parentNode[appendChild](this.Group);
2230 this.paper.top != this && tofront(this, this.paper);
2233 Element[proto].toBack = function () {
2237 if (this.Group.parentNode.firstChild != this.Group) {
2238 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2239 toback(this, this.paper);
2243 Element[proto].insertAfter = function (element) {
2247 if (element.Group.nextSibling) {
2248 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2250 element.Group.parentNode[appendChild](this.Group);
2252 insertafter(this, element, this.paper);
2255 Element[proto].insertBefore = function (element) {
2259 element.Group.parentNode.insertBefore(this.Group, element.Group);
2260 insertbefore(this, element, this.paper);
2263 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2264 Element[proto].blur = function (size) {
2265 var s = this.node.runtimeStyle,
2267 f = f.replace(blurregexp, E);
2269 this.attrs.blur = size;
2270 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2271 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2275 delete this.attrs.blur;
2279 theCircle = function (vml, x, y, r) {
2280 var g = createNode("group"),
2281 o = createNode("oval"),
2283 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2284 g.coordsize = coordsize;
2285 g.coordorigin = vml.coordorigin;
2287 var res = new Element(o, g, vml);
2288 res.type = "circle";
2289 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2293 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2294 vml.canvas[appendChild](g);
2297 function rectPath(x, y, w, h, r) {
2299 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);
2301 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2304 theRect = function (vml, x, y, w, h, r) {
2305 var path = rectPath(x, y, w, h, r),
2306 res = vml.path(path),
2310 res.W = a.width = w;
2311 res.H = a.height = h;
2317 theEllipse = function (vml, x, y, rx, ry) {
2318 var g = createNode("group"),
2319 o = createNode("oval"),
2321 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2322 g.coordsize = coordsize;
2323 g.coordorigin = vml.coordorigin;
2325 var res = new Element(o, g, vml);
2326 res.type = "ellipse";
2327 setFillAndStroke(res, {stroke: "#000"});
2332 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2333 vml.canvas[appendChild](g);
2336 theImage = function (vml, src, x, y, w, h) {
2337 var g = createNode("group"),
2338 o = createNode("image"),
2340 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2341 g.coordsize = coordsize;
2342 g.coordorigin = vml.coordorigin;
2345 var res = new Element(o, g, vml);
2347 res.attrs.src = src;
2352 res.setBox({x: x, y: y, width: w, height: h});
2353 vml.canvas[appendChild](g);
2356 theText = function (vml, x, y, text) {
2357 var g = createNode("group"),
2358 el = createNode("shape"),
2360 path = createNode("path"),
2362 o = createNode("textpath");
2363 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2364 g.coordsize = coordsize;
2365 g.coordorigin = vml.coordorigin;
2366 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2367 path.textpathok = true;
2368 ol.width = vml.width;
2369 ol.height = vml.height;
2370 o.string = Str(text);
2373 el[appendChild](path);
2375 var res = new Element(o, g, vml);
2377 res.textpath = path;
2379 res.attrs.text = text;
2384 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2386 vml.canvas[appendChild](g);
2389 setSize = function (width, height) {
2390 var cs = this.canvas.style;
2391 width == +width && (width += "px");
2392 height == +height && (height += "px");
2395 cs.clip = "rect(0 " + width + " " + height + " 0)";
2399 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2401 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2402 createNode = function (tagName) {
2403 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2406 createNode = function (tagName) {
2407 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2410 create = function () {
2411 var con = getContainer[apply](0, arguments),
2412 container = con.container,
2413 height = con.height,
2419 throw new Error("VML container not found.");
2421 var res = new Paper,
2422 c = res.canvas = doc.createElement("div"),
2426 width = width || 512;
2427 height = height || 342;
2428 width == +width && (width += "px");
2429 height == +height && (height += "px");
2432 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2433 res.coordorigin = "0 0";
2434 res.span = doc.createElement("span");
2435 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2436 c[appendChild](res.span);
2437 cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2438 if (container == 1) {
2439 doc.body[appendChild](c);
2442 cs.position = "absolute";
2444 if (container.firstChild) {
2445 container.insertBefore(c, container.firstChild);
2447 container[appendChild](c);
2450 plugins.call(res, res, R.fn);
2453 Paper[proto].clear = function () {
2454 this.canvas.innerHTML = E;
2455 this.span = doc.createElement("span");
2456 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2457 this.canvas[appendChild](this.span);
2458 this.bottom = this.top = null;
2460 Paper[proto].remove = function () {
2461 this.canvas.parentNode.removeChild(this.canvas);
2462 for (var i in this) {
2463 this[i] = removed(i);
2470 // Safari or Chrome (WebKit) rendering bug workaround method
2471 if ((/^Apple|^Google/).test(win.navigator.vendor) && (!(win.navigator.userAgent.indexOf("Version/4.0") + 1) || win.navigator.platform.slice(0, 2) == "iP")) {
2472 Paper[proto].safari = function () {
2473 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2474 win.setTimeout(function () {rect.remove();});
2477 Paper[proto].safari = function () {};
2481 var preventDefault = function () {
2482 this.returnValue = false;
2484 preventTouch = function () {
2485 return this.originalEvent.preventDefault();
2487 stopPropagation = function () {
2488 this.cancelBubble = true;
2490 stopTouch = function () {
2491 return this.originalEvent.stopPropagation();
2493 addEvent = (function () {
2494 if (doc.addEventListener) {
2495 return function (obj, type, fn, element) {
2496 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2497 var f = function (e) {
2498 if (supportsTouch && touchMap[has](type)) {
2499 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2500 if (e.targetTouches[i].target == obj) {
2502 e = e.targetTouches[i];
2503 e.originalEvent = olde;
2504 e.preventDefault = preventTouch;
2505 e.stopPropagation = stopTouch;
2510 return fn.call(element, e);
2512 obj.addEventListener(realName, f, false);
2513 return function () {
2514 obj.removeEventListener(realName, f, false);
2518 } else if (doc.attachEvent) {
2519 return function (obj, type, fn, element) {
2520 var f = function (e) {
2522 e.preventDefault = e.preventDefault || preventDefault;
2523 e.stopPropagation = e.stopPropagation || stopPropagation;
2524 return fn.call(element, e);
2526 obj.attachEvent("on" + type, f);
2527 var detacher = function () {
2528 obj.detachEvent("on" + type, f);
2536 dragMove = function (e) {
2543 if (supportsTouch) {
2544 var i = e.touches.length,
2547 touch = e.touches[i];
2548 if (touch.identifier == dragi.el._drag.id) {
2551 (e.originalEvent ? e.originalEvent : e).preventDefault();
2558 dragi.move && dragi.move.call(dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y);
2561 dragUp = function () {
2562 R.unmousemove(dragMove).unmouseup(dragUp);
2563 var i = drag.length,
2567 dragi.el._drag = {};
2568 dragi.end && dragi.end.call(dragi.el);
2572 for (var i = events[length]; i--;) {
2573 (function (eventName) {
2574 R[eventName] = Element[proto][eventName] = function (fn) {
2575 if (R.is(fn, "function")) {
2576 this.events = this.events || [];
2577 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
2581 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2582 var events = this.events,
2584 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2586 events.splice(l, 1);
2587 !events.length && delete this.events;
2594 Element[proto].hover = function (f_in, f_out) {
2595 return this.mouseover(f_in).mouseout(f_out);
2597 Element[proto].unhover = function (f_in, f_out) {
2598 return this.unmouseover(f_in).unmouseout(f_out);
2600 Element[proto].drag = function (onmove, onstart, onend) {
2602 this.mousedown(function (e) {
2603 (e.originalEvent || e).preventDefault();
2604 this._drag.x = e.clientX;
2605 this._drag.y = e.clientY;
2606 this._drag.id = e.identifier;
2607 onstart && onstart.call(this, e.clientX, e.clientY);
2608 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2609 drag.push({el: this, move: onmove, end: onend});
2613 Element[proto].undrag = function (onmove, onstart, onend) {
2614 var i = drag.length;
2616 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i, 1);
2617 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2620 Paper[proto].circle = function (x, y, r) {
2621 return theCircle(this, x || 0, y || 0, r || 0);
2623 Paper[proto].rect = function (x, y, w, h, r) {
2624 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2626 Paper[proto].ellipse = function (x, y, rx, ry) {
2627 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2629 Paper[proto].path = function (pathString) {
2630 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2631 return thePath(R.format[apply](R, arguments), this);
2633 Paper[proto].image = function (src, x, y, w, h) {
2634 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2636 Paper[proto].text = function (x, y, text) {
2637 return theText(this, x || 0, y || 0, text || E);
2639 Paper[proto].set = function (itemsArray) {
2640 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2641 return new Set(itemsArray);
2643 Paper[proto].setSize = setSize;
2644 Paper[proto].top = Paper[proto].bottom = null;
2645 Paper[proto].raphael = R;
2647 return this.x + S + this.y;
2649 Element[proto].resetScale = function () {
2655 this.attrs.scale = "1 1";
2657 Element[proto].scale = function (x, y, cx, cy) {
2661 if (x == null && y == null) {
2676 var bb = this.getBBox(),
2677 rcx = bb.x + bb.width / 2,
2678 rcy = bb.y + bb.height / 2,
2681 cx = (+cx || cx == 0) ? cx : rcx;
2682 cy = (+cy || cy == 0) ? cy : rcy;
2683 var dirx = ~~(x / math.abs(x)),
2684 diry = ~~(y / math.abs(y)),
2685 s = this.node.style,
2686 ncx = cx + (rcx - cx) * kx,
2687 ncy = cy + (rcy - cy) * ky;
2688 switch (this.type) {
2691 var neww = a.width * dirx * kx,
2692 newh = a.height * diry * ky;
2695 r: a.r * mmin(dirx * kx, diry * ky),
2704 rx: a.rx * dirx * kx,
2705 ry: a.ry * diry * ky,
2706 r: a.r * mmin(dirx * kx, diry * ky),
2718 var path = pathToRelative(a.path),
2720 for (var i = 0, ii = path[length]; i < ii; i++) {
2722 P0 = upperCase.call(p[0]);
2723 if (P0 == "M" && skip) {
2729 p[path[i][length] - 2] *= kx;
2730 p[path[i][length] - 1] *= ky;
2733 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2734 } else if (P0 == "H") {
2735 for (var j = 1, jj = p[length]; j < jj; j++) {
2738 } else if (P0 == "V") {
2739 for (j = 1, jj = p[length]; j < jj; j++) {
2743 for (j = 1, jj = p[length]; j < jj; j++) {
2744 p[j] *= (j % 2) ? kx : ky;
2748 var dim2 = pathDimensions(path);
2749 dx = ncx - dim2.x - dim2.width / 2;
2750 dy = ncy - dim2.y - dim2.height / 2;
2753 this.attr({path: path});
2756 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2757 if (this.transformations) {
2758 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2759 this.node[setAttribute]("transform", this.transformations[join](S));
2760 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2761 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2762 this.attr({x: dx, y: dy});
2766 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2767 ", M12=0, M21=0, M22=", diry,
2768 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2769 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2772 if (this.transformations) {
2773 this.transformations[2] = E;
2774 this.node[setAttribute]("transform", this.transformations[join](S));
2778 this.node.filterMatrix = E;
2779 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2782 a.scale = [x, y, cx, cy][join](S);
2788 Element[proto].clone = function () {
2792 var attr = this.attr();
2794 delete attr.translation;
2795 return this.paper[this.type]().attr(attr);
2797 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2800 for (var i = 0; i < 1.01; i+=.01) {
2801 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2802 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2803 if (len >= length) {
2809 getLengthFactory = function (istotal, subpath) {
2810 return function (path, length, onlystart) {
2811 path = path2curve(path);
2812 var x, y, p, l, sp = "", subpaths = {}, point,
2814 for (var i = 0, ii = path.length; i < ii; i++) {
2820 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2821 if (len + l > length) {
2822 if (subpath && !subpaths.start) {
2823 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2824 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2825 if (onlystart) {return sp;}
2826 subpaths.start = sp;
2827 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2833 if (!istotal && !subpath) {
2834 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2835 return {x: point.x, y: point.y, alpha: point.alpha};
2845 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2846 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2850 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2851 var a = (p2y - p1y) / (p2x - p1x),
2852 b = (c2y - p1y) / (c2x - p1x),
2853 c = (c1y - p1y) / (c1x - p1x);
2854 if ((Math.abs(b - a) + Math.abs(b - c)) / 2 < .1) {
2855 return pow(pow(p1x - p2x, 2) + pow(p1y - p2y, 2), .5);
2857 var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, .5);
2858 return segmentLength(p1x, p1y, dot.start.x, dot.start.y, dot.m.x, dot.m.y, dot.x, dot.y) +
2859 segmentLength(dot.x, dot.y, dot.n.x, dot.n.y, dot.end.x, dot.end.y, p2x, p2y);
2862 var getTotalLength = getLengthFactory(1),
2863 getPointAtLength = getLengthFactory(),
2864 getSubpathsAtLength = getLengthFactory(0, 1);
2865 Element[proto].getTotalLength = function () {
2866 if (this.type != "path") {return;}
2867 if (this.node.getTotalLength) {
2868 return this.node.getTotalLength();
2870 return getTotalLength(this.attrs.path);
2872 Element[proto].getPointAtLength = function (length) {
2873 if (this.type != "path") {return;}
2874 if (this.node.getPointAtLength) {
2875 return this.node.getPointAtLength(length);
2877 return getPointAtLength(this.attrs.path, length);
2879 Element[proto].getSubpath = function (from, to) {
2880 if (this.type != "path") {return;}
2881 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2882 return getSubpathsAtLength(this.attrs.path, from).end;
2884 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2885 return from ? getSubpathsAtLength(a, from).end : a;
2888 // animation easing formulas
2889 R.easing_formulas = {
2890 linear: function (n) {
2897 return pow(n - 1, 3) + 1;
2899 "<>": function (n) {
2902 return pow(n, 3) / 2;
2905 return (pow(n, 3) + 2) / 2;
2907 backIn: function (n) {
2909 return n * n * ((s + 1) * n - s);
2911 backOut: function (n) {
2914 return n * n * ((s + 1) * n + s) + 1;
2916 elastic: function (n) {
2917 if (n == 0 || n == 1) {
2922 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2924 bounce: function (n) {
2933 l = s * n * n + .75;
2935 if (n < (2.5 / p)) {
2937 l = s * n * n + .9375;
2940 l = s * n * n + .984375;
2948 var animationElements = {length : 0},
2949 animation = function () {
2950 var Now = +new Date;
2951 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2952 var e = animationElements[l];
2953 if (e.stop || e.el.removed) {
2954 delete animationElements[l];
2955 animationElements[length]--;
2958 var time = Now - e.start,
2967 callback = e.callback,
2971 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2972 for (var attr in from) if (from[has](attr)) {
2973 switch (availableAnimAttrs[attr]) {
2975 now = pos * ms * diff[attr];
2976 to.back && (now = to.len - now);
2977 var point = getPointAtLength(to[attr], now);
2978 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2981 that.translate(point.x - diff.sx, point.y - diff.sy);
2982 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2985 now = +from[attr] + pos * ms * diff[attr];
2989 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2990 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2991 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2996 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2997 now[i] = [from[attr][i][0]];
2998 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2999 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3001 now[i] = now[i][join](S);
3008 var x = diff[attr][0] * (time - prev),
3009 y = diff[attr][1] * (time - prev);
3015 now = +from[attr][0] + pos * ms * diff[attr][0];
3016 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3019 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);
3025 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3034 that._run && that._run.call(that);
3037 point = getPointAtLength(to.along, to.len * !to.back);
3038 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3039 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3041 (t.x || t.y) && that.translate(-t.x, -t.y);
3042 to.scale && (to.scale += E);
3044 delete animationElements[l];
3045 animationElements[length]--;
3046 that.in_animation = null;
3047 R.is(callback, "function") && callback.call(that);
3051 R.svg && that && that.paper && that.paper.safari();
3052 animationElements[length] && win.setTimeout(animation);
3054 upto255 = function (color) {
3055 return mmax(mmin(color, 255), 0);
3057 translate = function (x, y) {
3059 return {x: this._.tx, y: this._.ty, toString: x_y};
3063 switch (this.type) {
3066 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3071 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3074 var path = pathToRelative(this.attrs.path);
3077 this.attr({path: path});
3082 Element[proto].animateWith = function (element, params, ms, easing, callback) {
3083 animationElements[element.id] && (params.start = animationElements[element.id].start);
3084 return this.animate(params, ms, easing, callback);
3086 Element[proto].animateAlong = along();
3087 Element[proto].animateAlongBack = along(1);
3088 function along(isBack) {
3089 return function (path, ms, rotate, callback) {
3090 var params = {back: isBack};
3091 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3092 path && path.constructor == Element && (path = path.attrs.path);
3093 path && (params.along = path);
3094 return this.animate(params, ms, callback);
3097 Element[proto].onAnimation = function (f) {
3101 Element[proto].animate = function (params, ms, easing, callback) {
3102 if (R.is(easing, "function") || !easing) {
3103 callback = easing || null;
3108 for (var attr in params) if (params[has](attr)) {
3109 if (availableAnimAttrs[has](attr)) {
3110 from[attr] = this.attr(attr);
3111 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3112 to[attr] = params[attr];
3113 switch (availableAnimAttrs[attr]) {
3115 var len = getTotalLength(params[attr]),
3116 point = getPointAtLength(params[attr], len * !!params.back),
3117 bb = this.getBBox();
3118 diff[attr] = len / ms;
3123 to.rot = params.rot;
3124 to.back = params.back;
3126 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3129 diff[attr] = (to[attr] - from[attr]) / ms;
3132 from[attr] = R.getRGB(from[attr]);
3133 var toColour = R.getRGB(to[attr]);
3135 r: (toColour.r - from[attr].r) / ms,
3136 g: (toColour.g - from[attr].g) / ms,
3137 b: (toColour.b - from[attr].b) / ms
3141 var pathes = path2curve(from[attr], to[attr]);
3142 from[attr] = pathes[0];
3143 var toPath = pathes[1];
3145 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3146 diff[attr][i] = [0];
3147 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3148 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3153 var values = Str(params[attr])[split](separator),
3154 from2 = Str(from[attr])[split](separator);
3157 from[attr] = [0, 0];
3158 diff[attr] = [values[0] / ms, values[1] / ms];
3161 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3162 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3165 params[attr] = values;
3166 from[attr] = Str(from[attr])[split](separator);
3167 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3170 from[attr] = Str(from[attr])[split](separator);
3174 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3183 this.in_animation = 1;
3184 animationElements[this.id] = {
3185 start: params.start || +new Date,
3195 ++animationElements[length] == 1 && animation();
3198 Element[proto].stop = function () {
3199 animationElements[this.id] && animationElements[length]--;
3200 delete animationElements[this.id];
3203 Element[proto].translate = function (x, y) {
3204 return this.attr({translation: x + " " + y});
3206 Element[proto][toString] = function () {
3207 return "Rapha\xebl\u2019s object";
3209 R.ae = animationElements;
3212 var Set = function (items) {
3217 for (var i = 0, ii = items[length]; i < ii; i++) {
3218 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3219 this[this.items[length]] = this.items[this.items[length]] = items[i];
3225 Set[proto][push] = function () {
3228 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3229 item = arguments[i];
3230 if (item && (item.constructor == Element || item.constructor == Set)) {
3231 len = this.items[length];
3232 this[len] = this.items[len] = item;
3238 Set[proto].pop = function () {
3239 delete this[this[length]--];
3240 return this.items.pop();
3242 for (var method in Element[proto]) if (Element[proto][has](method)) {
3243 Set[proto][method] = (function (methodname) {
3244 return function () {
3245 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3246 this.items[i][methodname][apply](this.items[i], arguments);
3252 Set[proto].attr = function (name, value) {
3253 if (name && R.is(name, array) && R.is(name[0], "object")) {
3254 for (var j = 0, jj = name[length]; j < jj; j++) {
3255 this.items[j].attr(name[j]);
3258 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3259 this.items[i].attr(name, value);
3264 Set[proto].animate = function (params, ms, easing, callback) {
3265 (R.is(easing, "function") || !easing) && (callback = easing || null);
3266 var len = this.items[length],
3271 callback && (collector = function () {
3272 !--len && callback.call(set);
3274 easing = R.is(easing, string) ? easing : collector;
3275 item = this.items[--i].animate(params, ms, easing, collector);
3277 this.items[i].animateWith(item, params, ms, easing, collector);
3281 Set[proto].insertAfter = function (el) {
3282 var i = this.items[length];
3284 this.items[i].insertAfter(el);
3288 Set[proto].getBBox = function () {
3293 for (var i = this.items[length]; i--;) {
3294 var box = this.items[i].getBBox();
3297 w[push](box.x + box.width);
3298 h[push](box.y + box.height);
3300 x = mmin[apply](0, x);
3301 y = mmin[apply](0, y);
3305 width: mmax[apply](0, w) - x,
3306 height: mmax[apply](0, h) - y
3309 Set[proto].clone = function (s) {
3311 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3312 s[push](this.items[i].clone());
3317 R.registerFont = function (font) {
3321 this.fonts = this.fonts || {};
3327 family = font.face["font-family"];
3328 for (var prop in font.face) if (font.face[has](prop)) {
3329 fontcopy.face[prop] = font.face[prop];
3331 if (this.fonts[family]) {
3332 this.fonts[family][push](fontcopy);
3334 this.fonts[family] = [fontcopy];
3337 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3338 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3339 var path = font.glyphs[glyph];
3340 fontcopy.glyphs[glyph] = {
3343 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3344 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3348 for (var k in path.k) if (path[has](k)) {
3349 fontcopy.glyphs[glyph].k[k] = path.k[k];
3356 Paper[proto].getFont = function (family, weight, style, stretch) {
3357 stretch = stretch || "normal";
3358 style = style || "normal";
3359 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3363 var font = R.fonts[family];
3365 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3366 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3367 if (name.test(fontName)) {
3368 font = R.fonts[fontName];
3375 for (var i = 0, ii = font[length]; i < ii; i++) {
3377 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3384 Paper[proto].print = function (x, y, string, font, size, origin) {
3385 origin = origin || "middle"; // baseline|middle
3386 var out = this.set(),
3387 letters = Str(string)[split](E),
3391 R.is(font, string) && (font = this.getFont(font));
3393 scale = (size || 16) / font.face["units-per-em"];
3394 var bb = font.face.bbox.split(separator),
3396 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3397 for (var i = 0, ii = letters[length]; i < ii; i++) {
3398 var prev = i && font.glyphs[letters[i - 1]] || {},
3399 curr = font.glyphs[letters[i]];
3400 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3401 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3403 out.scale(scale, scale, top, height).translate(x - top, y - height);
3408 var formatrg = /\{(\d+)\}/g;
3409 R.format = function (token, params) {
3410 var args = R.is(params, array) ? [0][concat](params) : arguments;
3411 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3412 return args[++i] == null ? E : args[i];
3416 R.ninja = function () {
3417 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3420 R.el = Element[proto];