2 * Raphael 1.4.4 - 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 touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
43 mousedown: "touchstart",
44 mousemove: "touchmove",
49 lowerCase = String[proto].toLowerCase,
56 toString = "toString",
58 objectToString = Object[proto][toString],
63 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
64 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+(?:\s*,\s*[\d\.]+)?)\s*\)|rgba?\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%(?:\s*,\s*[\d\.]+%))\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
66 setAttribute = "setAttribute",
69 ms = " progid:DXImageTransform.Microsoft",
70 upperCase = String[proto].toUpperCase,
71 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
72 availableAnimAttrs = {along: "along", blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
74 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
75 if (R.type == "VML") {
76 var d = doc.createElement("div");
77 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
78 if (d.childNodes[length] != 2) {
83 R.svg = !(R.vml = R.type == "VML");
84 Paper[proto] = R[proto];
88 R.is = function (o, type) {
89 type = lowerCase.call(type);
90 return (type == "object" && o === Object(o)) ||
91 (type == "undefined" && typeof o == type) ||
92 (type == "null" && o == null) ||
93 lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
96 R.setWindow = function (newwin) {
101 var toHex = function (color) {
103 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
104 var trim = /^\s+|\s+$/g;
105 toHex = cacher(function (color) {
107 color = (color + E)[rp](trim, E);
109 var docum = new win.ActiveXObject("htmlfile");
110 docum.write("<body>");
114 bod = win.createPopup().document.body;
116 var range = bod.createTextRange();
118 bod.style.color = color;
119 var value = range.queryCommandValue("ForeColor");
120 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
121 return "#" + ("000000" + value[toString](16)).slice(-6);
127 var i = doc.createElement("i");
128 i.title = "Rapha\xebl Colour Picker";
129 i.style.display = "none";
130 doc.body[appendChild](i);
131 toHex = cacher(function (color) {
132 i.style.color = color;
133 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
138 var hsbtoString = function () {
139 return "hsb(" + [this.h, this.s, this.b] + ")";
141 rgbtoString = function () {
144 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
145 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
153 if (brightness == 0) {
154 return {r: 0, g: 0, b: 0, hex: "#000"};
156 if (hue > 1 || saturation > 1 || brightness > 1) {
163 p = brightness * (1 - saturation),
164 q = brightness * (1 - (saturation * f)),
165 t = brightness * (1 - (saturation * (1 - f)));
166 red = [brightness, q, p, p, t, brightness, brightness][i];
167 green = [t, brightness, brightness, q, p, p, t][i];
168 blue = [p, p, t, brightness, brightness, q, p][i];
172 var rgb = {r: red, g: green, b: blue, toString: rgbtoString},
173 r = (~~red)[toString](16),
174 g = (~~green)[toString](16),
175 b = (~~blue)[toString](16);
179 rgb.hex = "#" + r + g + b;
182 R.rgb2hsb = cacher(function (red, green, blue) {
183 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
188 if (R.is(red, string)) {
189 var clr = R.getRGB(red);
194 if (red > 1 || green > 1 || blue > 1) {
199 var max = mmax(red, green, blue),
200 min = mmin(red, green, blue),
205 return {h: 0, s: 0, b: max};
207 var delta = (max - min);
208 saturation = delta / max;
210 hue = (green - blue) / delta;
211 } else if (green == max) {
212 hue = 2 + ((blue - red) / delta);
214 hue = 4 + ((red - green) / delta);
220 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
222 var p2s = /,?([achlmqrstvxz]),?/gi,
223 commaSpaces = /\s*,\s*/,
224 hsrg = {hs: 1, rg: 1};
225 R._path2string = function () {
226 return this.join(",")[rp](p2s, "$1");
228 function cacher(f, scope, postprocessor) {
230 var arg = Array[proto].slice.call(arguments, 0),
231 args = arg[join]("\u25ba"),
232 cache = newf.cache = newf.cache || {},
233 count = newf.count = newf.count || [];
234 if (cache[has](args)) {
235 return postprocessor ? postprocessor(cache[args]) : cache[args];
237 count[length] >= 1e3 && delete cache[count.shift()];
239 cache[args] = f[apply](scope, arg);
240 return postprocessor ? postprocessor(cache[args]) : cache[args];
245 R.getRGB = cacher(function (colour) {
246 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
247 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
249 if (colour == "none") {
250 return {r: -1, g: -1, b: -1, hex: "none"};
252 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
259 rgb = colour.match(colourRegExp);
262 blue = toInt(rgb[2].substring(5), 16);
263 green = toInt(rgb[2].substring(3, 5), 16);
264 red = toInt(rgb[2].substring(1, 3), 16);
267 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
268 green = toInt((t = rgb[3].charAt(2)) + t, 16);
269 red = toInt((t = rgb[3].charAt(1)) + t, 16);
272 rgb = rgb[4][split](commaSpaces);
273 red = toFloat(rgb[0]);
274 green = toFloat(rgb[1]);
275 blue = toFloat(rgb[2]);
276 opacity = toFloat(rgb[3]);
279 rgb = rgb[5][split](commaSpaces);
280 red = toFloat(rgb[0]) * 2.55;
281 green = toFloat(rgb[1]) * 2.55;
282 blue = toFloat(rgb[2]) * 2.55;
283 opacity = toFloat(rgb[3]);
286 rgb = rgb[6][split](commaSpaces);
287 red = toFloat(rgb[0]);
288 green = toFloat(rgb[1]);
289 blue = toFloat(rgb[2]);
290 return R.hsb2rgb(red, green, blue);
293 rgb = rgb[7][split](commaSpaces);
294 red = toFloat(rgb[0]) * 2.55;
295 green = toFloat(rgb[1]) * 2.55;
296 blue = toFloat(rgb[2]) * 2.55;
297 return R.hsb2rgb(red, green, blue);
299 rgb = {r: red, g: green, b: blue};
300 var r = (~~red)[toString](16),
301 g = (~~green)[toString](16),
302 b = (~~blue)[toString](16);
306 rgb.hex = "#" + r + g + b;
307 isFinite(toFloat(opacity)) && (rgb.o = opacity);
310 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
312 R.getColor = function (value) {
313 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
314 rgb = this.hsb2rgb(start.h, start.s, start.b);
319 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
323 R.getColor.reset = function () {
327 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
328 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
329 R.parsePathString = cacher(function (pathString) {
333 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
335 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
336 data = pathClone(pathString);
339 (pathString + E)[rp](pathCommand, function (a, b, c) {
341 name = lowerCase.call(b);
342 c[rp](pathValues, function (a, b) {
343 b && params[push](+b);
345 if (name == "m" && params[length] > 2) {
346 data[push]([b][concat](params.splice(0, 2)));
348 b = b == "m" ? "l" : "L";
350 while (params[length] >= paramCounts[name]) {
351 data[push]([b][concat](params.splice(0, paramCounts[name])));
352 if (!paramCounts[name]) {
358 data[toString] = R._path2string;
361 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
363 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
364 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
365 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
366 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
367 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
368 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
369 ax = (1 - t) * p1x + t * c1x,
370 ay = (1 - t) * p1y + t * c1y,
371 cx = (1 - t) * c2x + t * p2x,
372 cy = (1 - t) * c2y + t * p2y,
373 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
374 (mx > nx || my < ny) && (alpha += 180);
375 return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
377 var pathDimensions = cacher(function (path) {
379 return {x: 0, y: 0, width: 0, height: 0};
381 path = path2curve(path);
387 for (var i = 0, ii = path[length]; i < ii; i++) {
395 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
396 X = X[concat](dim.min.x, dim.max.x);
397 Y = Y[concat](dim.min.y, dim.max.y);
402 var xmin = mmin[apply](0, X),
403 ymin = mmin[apply](0, Y);
407 width: mmax[apply](0, X) - xmin,
408 height: mmax[apply](0, Y) - ymin
411 pathClone = function (pathArray) {
413 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
414 pathArray = R.parsePathString(pathArray);
416 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
418 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
419 res[i][j] = pathArray[i][j];
422 res[toString] = R._path2string;
425 pathToRelative = cacher(function (pathArray) {
426 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
427 pathArray = R.parsePathString(pathArray);
435 if (pathArray[0][0] == "M") {
441 res[push](["M", x, y]);
443 for (var i = start, ii = pathArray[length]; i < ii; i++) {
446 if (pa[0] != lowerCase.call(pa[0])) {
447 r[0] = lowerCase.call(pa[0]);
455 r[6] = +(pa[6] - x).toFixed(3);
456 r[7] = +(pa[7] - y).toFixed(3);
459 r[1] = +(pa[1] - y).toFixed(3);
465 for (var j = 1, jj = pa[length]; j < jj; j++) {
466 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
475 for (var k = 0, kk = pa[length]; k < kk; k++) {
479 var len = res[i][length];
486 x += +res[i][len - 1];
489 y += +res[i][len - 1];
492 x += +res[i][len - 2];
493 y += +res[i][len - 1];
496 res[toString] = R._path2string;
499 pathToAbsolute = cacher(function (pathArray) {
500 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
501 pathArray = R.parsePathString(pathArray);
509 if (pathArray[0][0] == "M") {
510 x = +pathArray[0][1];
511 y = +pathArray[0][2];
515 res[0] = ["M", x, y];
517 for (var i = start, ii = pathArray[length]; i < ii; i++) {
520 if (pa[0] != upperCase.call(pa[0])) {
521 r[0] = upperCase.call(pa[0]);
542 for (var j = 1, jj = pa[length]; j < jj; j++) {
543 r[j] = +pa[j] + ((j % 2) ? x : y);
547 for (var k = 0, kk = pa[length]; k < kk; k++) {
563 mx = res[i][res[i][length] - 2];
564 my = res[i][res[i][length] - 1];
566 x = res[i][res[i][length] - 2];
567 y = res[i][res[i][length] - 1];
570 res[toString] = R._path2string;
573 l2c = function (x1, y1, x2, y2) {
574 return [x1, y1, x2, y2, x2, y2];
576 q2c = function (x1, y1, ax, ay, x2, y2) {
588 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
589 // for more information of where this math came from visit:
590 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
592 _120 = PI * 120 / 180,
593 rad = PI / 180 * (+angle || 0),
596 rotate = cacher(function (x, y, rad) {
597 var X = x * math.cos(rad) - y * math.sin(rad),
598 Y = x * math.sin(rad) + y * math.cos(rad);
602 xy = rotate(x1, y1, -rad);
605 xy = rotate(x2, y2, -rad);
608 var cos = math.cos(PI / 180 * angle),
609 sin = math.sin(PI / 180 * angle),
612 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
620 k = (large_arc_flag == sweep_flag ? -1 : 1) *
621 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
622 cx = k * rx * y / ry + (x1 + x2) / 2,
623 cy = k * -ry * x / rx + (y1 + y2) / 2,
624 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
625 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
627 f1 = x1 < cx ? PI - f1 : f1;
628 f2 = x2 < cx ? PI - f2 : f2;
629 f1 < 0 && (f1 = PI * 2 + f1);
630 f2 < 0 && (f2 = PI * 2 + f2);
631 if (sweep_flag && f1 > f2) {
634 if (!sweep_flag && f2 > f1) {
644 if (math.abs(df) > _120) {
648 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
649 x2 = cx + rx * math.cos(f2);
650 y2 = cy + ry * math.sin(f2);
651 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
654 var c1 = math.cos(f1),
658 t = math.tan(df / 4),
662 m2 = [x1 + hx * s1, y1 - hy * c1],
663 m3 = [x2 + hx * s2, y2 - hy * c2],
665 m2[0] = 2 * m1[0] - m2[0];
666 m2[1] = 2 * m1[1] - m2[1];
668 return [m2, m3, m4][concat](res);
670 res = [m2, m3, m4][concat](res)[join]()[split](",");
672 for (var i = 0, ii = res[length]; i < ii; i++) {
673 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
678 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
681 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
682 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
685 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
686 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
687 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
689 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
690 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
694 math.abs(t1) > 1e12 && (t1 = .5);
695 math.abs(t2) > 1e12 && (t2 = .5);
696 if (t1 > 0 && t1 < 1) {
697 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
701 if (t2 > 0 && t2 < 1) {
702 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
706 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
707 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
709 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
710 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
711 math.abs(t1) > 1e12 && (t1 = .5);
712 math.abs(t2) > 1e12 && (t2 = .5);
713 if (t1 > 0 && t1 < 1) {
714 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
718 if (t2 > 0 && t2 < 1) {
719 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
724 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
725 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
728 path2curve = cacher(function (path, path2) {
729 var p = pathToAbsolute(path),
730 p2 = path2 && pathToAbsolute(path2),
731 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
732 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
733 processPath = function (path, d) {
736 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
738 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
745 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
748 nx = d.x + (d.x - (d.bx || d.x));
749 ny = d.y + (d.y - (d.by || d.y));
750 path = ["C", nx, ny][concat](path.slice(1));
753 d.qx = d.x + (d.x - (d.qx || d.x));
754 d.qy = d.y + (d.y - (d.qy || d.y));
755 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
760 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
763 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
766 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
769 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
772 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
777 fixArc = function (pp, i) {
778 if (pp[i][length] > 7) {
782 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
785 ii = mmax(p[length], p2 && p2[length] || 0);
788 fixM = function (path1, path2, a1, a2, i) {
789 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
790 path2.splice(i, 0, ["M", a2.x, a2.y]);
795 ii = mmax(p[length], p2 && p2[length] || 0);
798 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
799 p[i] = processPath(p[i], attrs);
801 p2 && (p2[i] = processPath(p2[i], attrs2));
803 fixM(p, p2, attrs, attrs2, i);
804 fixM(p2, p, attrs2, attrs, i);
807 seglen = seg[length],
808 seg2len = p2 && seg2[length];
809 attrs.x = seg[seglen - 2];
810 attrs.y = seg[seglen - 1];
811 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
812 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
813 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
814 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
815 attrs2.x = p2 && seg2[seg2len - 2];
816 attrs2.y = p2 && seg2[seg2len - 1];
818 return p2 ? [p, p2] : p;
820 parseDots = cacher(function (gradient) {
822 for (var i = 0, ii = gradient[length]; i < ii; i++) {
824 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
825 dot.color = R.getRGB(par[1]);
826 if (dot.color.error) {
829 dot.color = dot.color.hex;
830 par[2] && (dot.offset = par[2] + "%");
833 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
834 if (!dots[i].offset) {
835 var start = toFloat(dots[i - 1].offset || 0),
837 for (var j = i + 1; j < ii; j++) {
838 if (dots[j].offset) {
839 end = dots[j].offset;
848 var d = (end - start) / (j - i + 1);
851 dots[i].offset = start + "%";
857 getContainer = function (x, y, w, h) {
859 if (R.is(x, string) || R.is(x, "object")) {
860 container = R.is(x, string) ? doc.getElementById(x) : x;
861 if (container.tagName) {
864 container: container,
865 width: container.style.pixelWidth || container.offsetWidth,
866 height: container.style.pixelHeight || container.offsetHeight
869 return {container: container, width: y, height: w};
873 return {container: 1, x: x, y: y, width: w, height: h};
876 plugins = function (con, add) {
878 for (var prop in add) {
879 if (add[has](prop) && !(prop in con)) {
880 switch (typeof add[prop]) {
883 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
887 con[prop] = con[prop] || {};
888 plugins.call(this, con[prop], add[prop]);
891 con[prop] = add[prop];
897 tear = function (el, paper) {
898 el == paper.top && (paper.top = el.prev);
899 el == paper.bottom && (paper.bottom = el.next);
900 el.next && (el.next.prev = el.prev);
901 el.prev && (el.prev.next = el.next);
903 tofront = function (el, paper) {
904 if (paper.top === el) {
913 toback = function (el, paper) {
914 if (paper.bottom === el) {
918 el.next = paper.bottom;
920 paper.bottom.prev = el;
923 insertafter = function (el, el2, paper) {
925 el2 == paper.top && (paper.top = el);
926 el2.next && (el2.next.prev = el);
931 insertbefore = function (el, el2, paper) {
933 el2 == paper.bottom && (paper.bottom = el);
934 el2.prev && (el2.prev.next = el);
939 removed = function (methodname) {
941 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
944 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
948 Paper[proto].svgns = "http://www.w3.org/2000/svg";
949 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
950 round = function (num) {
951 return +num + (~~num === num) * .5;
953 var $ = function (el, attr) {
955 for (var key in attr) {
956 if (attr[has](key)) {
957 el[setAttribute](key, attr[key] + E);
961 el = doc.createElementNS(Paper[proto].svgns, el);
962 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
966 R[toString] = function () {
967 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
969 var thePath = function (pathString, SVG) {
971 SVG.canvas && SVG.canvas[appendChild](el);
972 var p = new Element(el, SVG);
974 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
977 var addGradientFill = function (o, gradient, SVG) {
981 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
986 var dir = ((fy > .5) * 2 - 1);
987 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
988 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
990 (fy = fy.toFixed(5) - 1e-5 * dir);
994 gradient = gradient[split](/\s*\-\s*/);
995 if (type == "linear") {
996 var angle = gradient.shift();
997 angle = -toFloat(angle);
1001 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1002 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1005 if (vector[2] < 0) {
1006 vector[0] = -vector[2];
1009 if (vector[3] < 0) {
1010 vector[1] = -vector[3];
1014 var dots = parseDots(gradient);
1018 var id = o.getAttribute(fillString);
1019 id = id.match(/^url\(#(.*)\)$/);
1020 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1022 var el = $(type + "Gradient");
1023 el.id = "r" + (R._id++)[toString](36);
1024 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1025 SVG.defs[appendChild](el);
1026 for (var i = 0, ii = dots[length]; i < ii; i++) {
1027 var stop = $("stop");
1029 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1030 "stop-color": dots[i].color || "#fff"
1032 el[appendChild](stop);
1035 fill: "url(#" + el.id + ")",
1044 var updatePosition = function (o) {
1045 var bbox = o.getBBox();
1046 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1048 var setFillAndStroke = function (o, params) {
1055 "-..": [3, 1, 1, 1, 1, 1],
1059 "- .": [4, 3, 1, 3],
1060 "--.": [8, 3, 1, 3],
1061 "--..": [8, 3, 1, 3, 1, 3]
1066 addDashes = function (o, value) {
1067 value = dasharray[lowerCase.call(value)];
1069 var width = o.attrs["stroke-width"] || "1",
1070 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1072 var i = value[length];
1074 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1076 $(node, {"stroke-dasharray": dashes[join](",")});
1079 params[has]("rotation") && (rot = params.rotation);
1080 var rotxy = (rot + E)[split](separator);
1081 if (!(rotxy.length - 1)) {
1084 rotxy[1] = +rotxy[1];
1085 rotxy[2] = +rotxy[2];
1087 toFloat(rot) && o.rotate(0, true);
1088 for (var att in params) {
1089 if (params[has](att)) {
1090 if (!availableAttrs[has](att)) {
1093 var value = params[att];
1100 o.rotate(value, true);
1105 var pn = node.parentNode;
1106 if (lowerCase.call(pn.tagName) != "a") {
1108 pn.insertBefore(hl, node);
1109 hl[appendChild](node);
1112 pn.setAttributeNS(o.paper.xlink, att, value);
1115 node.style.cursor = value;
1118 var rect = (value + E)[split](separator);
1119 if (rect[length] == 4) {
1120 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1121 var el = $("clipPath"),
1123 el.id = "r" + (R._id++)[toString](36);
1130 el[appendChild](rc);
1131 o.paper.defs[appendChild](el);
1132 $(node, {"clip-path": "url(#" + el.id + ")"});
1136 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1137 clip && clip.parentNode.removeChild(clip);
1138 $(node, {"clip-path": E});
1143 if (o.type == "path") {
1144 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1148 node[setAttribute](att, value);
1157 value = -attrs.x - (attrs.width || 0);
1160 if (att == "rx" && o.type == "rect") {
1164 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1165 node[setAttribute](att, round(value));
1166 o.pattern && updatePosition(o);
1169 node[setAttribute](att, value);
1178 value = -attrs.y - (attrs.height || 0);
1181 if (att == "ry" && o.type == "rect") {
1185 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1186 node[setAttribute](att, round(value));
1187 o.pattern && updatePosition(o);
1190 if (o.type == "rect") {
1191 $(node, {rx: value, ry: value});
1193 node[setAttribute](att, value);
1197 if (o.type == "image") {
1198 node.setAttributeNS(o.paper.xlink, "href", value);
1201 case "stroke-width":
1202 node.style.strokeWidth = value;
1203 // Need following line for Firefox
1204 node[setAttribute](att, value);
1205 if (attrs["stroke-dasharray"]) {
1206 addDashes(o, attrs["stroke-dasharray"]);
1209 case "stroke-dasharray":
1210 addDashes(o, value);
1213 var xy = (value + E)[split](separator);
1214 xy[0] = +xy[0] || 0;
1215 xy[1] = +xy[1] || 0;
1220 translate.call(o, xy[0], xy[1]);
1223 xy = (value + E)[split](separator);
1224 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1227 var isURL = (value + E).match(ISURL);
1230 var ig = $("image");
1231 el.id = "r" + (R._id++)[toString](36);
1232 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1233 $(ig, {x: 0, y: 0});
1234 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1235 el[appendChild](ig);
1237 var img = doc.createElement("img");
1238 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1239 img.onload = function () {
1240 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1241 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1242 doc.body.removeChild(this);
1245 doc.body[appendChild](img);
1247 o.paper.defs[appendChild](el);
1248 node.style.fill = "url(#" + el.id + ")";
1249 $(node, {fill: "url(#" + el.id + ")"});
1251 o.pattern && updatePosition(o);
1254 var clr = R.getRGB(value);
1256 delete params.gradient;
1257 delete attrs.gradient;
1258 !R.is(attrs.opacity, "undefined") &&
1259 R.is(params.opacity, "undefined") &&
1260 $(node, {opacity: attrs.opacity});
1261 !R.is(attrs["fill-opacity"], "undefined") &&
1262 R.is(params["fill-opacity"], "undefined") &&
1263 $(node, {"fill-opacity": attrs["fill-opacity"]});
1264 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1265 attrs.gradient = value;
1266 attrs.fill = "none";
1269 clr[has]("o") && $(node, {"fill-opacity": clr.o / 100});
1271 clr = R.getRGB(value);
1272 node[setAttribute](att, clr.hex);
1273 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o / 100});
1276 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1279 case "fill-opacity":
1280 if (attrs.gradient) {
1281 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1283 var stops = gradient.getElementsByTagName("stop");
1284 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1289 att == "font-size" && (value = toInt(value, 10) + "px");
1290 var cssrule = att[rp](/(\-.)/g, function (w) {
1291 return upperCase.call(w.substring(1));
1293 node.style[cssrule] = value;
1294 // Need following line for Firefox
1295 node[setAttribute](att, value);
1301 tuneText(o, params);
1303 o.rotate(rotxy.join(S));
1305 toFloat(rot) && o.rotate(rot, true);
1309 tuneText = function (el, params) {
1310 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1315 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1317 if (params[has]("text")) {
1318 a.text = params.text;
1319 while (node.firstChild) {
1320 node.removeChild(node.firstChild);
1322 var texts = (params.text + E)[split]("\n");
1323 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1324 var tspan = $("tspan");
1325 i && $(tspan, {dy: fontSize * leading, x: a.x});
1326 tspan[appendChild](doc.createTextNode(texts[i]));
1327 node[appendChild](tspan);
1330 texts = node.getElementsByTagName("tspan");
1331 for (i = 0, ii = texts[length]; i < ii; i++) {
1332 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1336 var bb = el.getBBox(),
1337 dif = a.y - (bb.y + bb.height / 2);
1338 dif && isFinite(dif) && $(node, {y: a.y + dif});
1340 Element = function (node, svg) {
1346 node.raphael = this;
1348 this.attrs = this.attrs || {};
1349 this.transformations = []; // rotate, translate, scale
1353 rt: {deg: 0, cx: 0, cy: 0},
1357 !svg.bottom && (svg.bottom = this);
1358 this.prev = svg.top;
1359 svg.top && (svg.top.next = this);
1363 Element[proto].rotate = function (deg, cx, cy) {
1369 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1371 return this._.rt.deg;
1373 var bbox = this.getBBox();
1374 deg = (deg + E)[split](separator);
1375 if (deg[length] - 1) {
1376 cx = toFloat(deg[1]);
1377 cy = toFloat(deg[2]);
1379 deg = toFloat(deg[0]);
1381 this._.rt.deg = deg;
1383 this._.rt.deg += deg;
1385 (cy == null) && (cx = null);
1388 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1389 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1390 if (this._.rt.deg) {
1391 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1392 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1394 this.transformations[0] = E;
1395 this.clip && $(this.clip, {transform: E});
1397 $(this.node, {transform: this.transformations[join](S)});
1400 Element[proto].hide = function () {
1401 !this.removed && (this.node.style.display = "none");
1404 Element[proto].show = function () {
1405 !this.removed && (this.node.style.display = "");
1408 Element[proto].remove = function () {
1412 tear(this, this.paper);
1413 this.node.parentNode.removeChild(this.node);
1414 for (var i in this) {
1417 this.removed = true;
1419 Element[proto].getBBox = function () {
1423 if (this.type == "path") {
1424 return pathDimensions(this.attrs.path);
1426 if (this.node.style.display == "none") {
1432 bbox = this.node.getBBox();
1434 // Firefox 3.0.x plays badly here
1438 if (this.type == "text") {
1439 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1440 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1441 var bb = this.node.getExtentOfChar(i);
1442 (bb.y < bbox.y) && (bbox.y = bb.y);
1443 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1444 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1447 hide && this.hide();
1450 Element[proto].attr = function (name, value) {
1456 for (var i in this.attrs) if (this.attrs[has](i)) {
1457 res[i] = this.attrs[i];
1459 this._.rt.deg && (res.rotation = this.rotate());
1460 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1461 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1464 if (value == null && R.is(name, string)) {
1465 if (name == "translation") {
1466 return translate.call(this);
1468 if (name == "rotation") {
1469 return this.rotate();
1471 if (name == "scale") {
1472 return this.scale();
1474 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1475 return this.attrs.gradient;
1477 return this.attrs[name];
1479 if (value == null && R.is(name, array)) {
1481 for (var j = 0, jj = name.length; j < jj; j++) {
1482 values[name[j]] = this.attr(name[j]);
1486 if (value != null) {
1488 params[name] = value;
1489 setFillAndStroke(this, params);
1490 } else if (name != null && R.is(name, "object")) {
1491 setFillAndStroke(this, name);
1495 Element[proto].toFront = function () {
1499 this.node.parentNode[appendChild](this.node);
1500 var svg = this.paper;
1501 svg.top != this && tofront(this, svg);
1504 Element[proto].toBack = function () {
1508 if (this.node.parentNode.firstChild != this.node) {
1509 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1510 toback(this, this.paper);
1511 var svg = this.paper;
1515 Element[proto].insertAfter = function (element) {
1519 var node = element.node;
1520 if (node.nextSibling) {
1521 node.parentNode.insertBefore(this.node, node.nextSibling);
1523 node.parentNode[appendChild](this.node);
1525 insertafter(this, element, this.paper);
1528 Element[proto].insertBefore = function (element) {
1532 var node = element.node;
1533 node.parentNode.insertBefore(this.node, node);
1534 insertbefore(this, element, this.paper);
1537 Element[proto].blur = function (size) {
1538 // Experimental. No Safari support. Use it on your own risk.
1541 var fltr = $("filter"),
1542 blur = $("feGaussianBlur");
1543 t.attrs.blur = size;
1544 fltr.id = "r" + (R._id++)[toString](36);
1545 $(blur, {stdDeviation: +size || 1.5});
1546 fltr.appendChild(blur);
1547 t.paper.defs.appendChild(fltr);
1549 $(t.node, {filter: "url(#" + fltr.id + ")"});
1552 t._blur.parentNode.removeChild(t._blur);
1554 delete t.attrs.blur;
1556 t.node.removeAttribute("filter");
1559 var theCircle = function (svg, x, y, r) {
1562 var el = $("circle");
1563 svg.canvas && svg.canvas[appendChild](el);
1564 var res = new Element(el, svg);
1565 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1566 res.type = "circle";
1570 var theRect = function (svg, x, y, w, h, r) {
1574 svg.canvas && svg.canvas[appendChild](el);
1575 var res = new Element(el, svg);
1576 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1581 var theEllipse = function (svg, x, y, rx, ry) {
1584 var el = $("ellipse");
1585 svg.canvas && svg.canvas[appendChild](el);
1586 var res = new Element(el, svg);
1587 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1588 res.type = "ellipse";
1592 var theImage = function (svg, src, x, y, w, h) {
1593 var el = $("image");
1594 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1595 el.setAttributeNS(svg.xlink, "href", src);
1596 svg.canvas && svg.canvas[appendChild](el);
1597 var res = new Element(el, svg);
1598 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1602 var theText = function (svg, x, y, text) {
1604 $(el, {x: x, y: y, "text-anchor": "middle"});
1605 svg.canvas && svg.canvas[appendChild](el);
1606 var res = new Element(el, svg);
1607 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1609 setFillAndStroke(res, res.attrs);
1612 var setSize = function (width, height) {
1613 this.width = width || this.width;
1614 this.height = height || this.height;
1615 this.canvas[setAttribute]("width", this.width);
1616 this.canvas[setAttribute]("height", this.height);
1619 var create = function () {
1620 var con = getContainer[apply](0, arguments),
1621 container = con && con.container,
1625 height = con.height;
1627 throw new Error("SVG container not found.");
1629 var cnvs = $("svg");
1632 width = width || 512;
1633 height = height || 342;
1635 xmlns: "http://www.w3.org/2000/svg",
1640 if (container == 1) {
1641 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1642 doc.body[appendChild](cnvs);
1644 if (container.firstChild) {
1645 container.insertBefore(cnvs, container.firstChild);
1647 container[appendChild](cnvs);
1650 container = new Paper;
1651 container.width = width;
1652 container.height = height;
1653 container.canvas = cnvs;
1654 plugins.call(container, container, R.fn);
1658 Paper[proto].clear = function () {
1659 var c = this.canvas;
1660 while (c.firstChild) {
1661 c.removeChild(c.firstChild);
1663 this.bottom = this.top = null;
1664 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1665 c[appendChild](this.desc);
1666 c[appendChild](this.defs = $("defs"));
1668 Paper[proto].remove = function () {
1669 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1670 for (var i in this) {
1671 this[i] = removed(i);
1678 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1679 bites = /([clmz]),?([^clmz]*)/gi,
1680 val = /-?[^,\s-]+/g,
1681 coordsize = 1e3 + S + 1e3,
1683 pathlike = {path: 1, rect: 1},
1684 path2vml = function (path) {
1685 var total = /[ahqstv]/ig,
1686 command = pathToAbsolute;
1687 (path + E).match(total) && (command = path2curve);
1689 if (command == pathToAbsolute && !(path + E).match(total)) {
1690 var res = (path + E)[rp](bites, function (all, command, args) {
1692 isMove = lowerCase.call(command) == "m",
1694 args[rp](val, function (value) {
1695 if (isMove && vals[length] == 2) {
1696 res += vals + map[command == "m" ? "l" : "L"];
1699 vals[push](round(value * zoom));
1705 var pa = command(path), p, r;
1707 for (var i = 0, ii = pa[length]; i < ii; i++) {
1709 r = lowerCase.call(pa[i][0]);
1710 r == "z" && (r = "x");
1711 for (var j = 1, jj = p[length]; j < jj; j++) {
1712 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1716 return res[join](S);
1719 R[toString] = function () {
1720 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1722 thePath = function (pathString, vml) {
1723 var g = createNode("group");
1724 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1725 g.coordsize = vml.coordsize;
1726 g.coordorigin = vml.coordorigin;
1727 var el = createNode("shape"), ol = el.style;
1728 ol.width = vml.width + "px";
1729 ol.height = vml.height + "px";
1730 el.coordsize = coordsize;
1731 el.coordorigin = vml.coordorigin;
1733 var p = new Element(el, g, vml),
1734 attr = {fill: "none", stroke: "#000"};
1735 pathString && (attr.path = pathString);
1736 p.isAbsolute = true;
1740 setFillAndStroke(p, attr);
1741 vml.canvas[appendChild](g);
1744 setFillAndStroke = function (o, params) {
1745 o.attrs = o.attrs || {};
1750 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1753 for (var par in params) if (params[has](par)) {
1754 a[par] = params[par];
1757 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1763 params.href && (node.href = params.href);
1764 params.title && (node.title = params.title);
1765 params.target && (node.target = params.target);
1766 params.cursor && (s.cursor = params.cursor);
1767 "blur" in params && o.blur(params.blur);
1768 if (params.path && o.type == "path" || newpath) {
1769 node.path = path2vml(a.path);
1771 if (params.rotation != null) {
1772 o.rotate(params.rotation, true);
1774 if (params.translation) {
1775 xy = (params.translation + E)[split](separator);
1776 translate.call(o, xy[0], xy[1]);
1777 if (o._.rt.cx != null) {
1778 o._.rt.cx +=+ xy[0];
1779 o._.rt.cy +=+ xy[1];
1780 o.setBox(o.attrs, xy[0], xy[1]);
1784 xy = (params.scale + E)[split](separator);
1785 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1787 if ("clip-rect" in params) {
1788 var rect = (params["clip-rect"] + E)[split](separator);
1789 if (rect[length] == 4) {
1790 rect[2] = +rect[2] + (+rect[0]);
1791 rect[3] = +rect[3] + (+rect[1]);
1792 var div = node.clipRect || doc.createElement("div"),
1794 group = node.parentNode;
1795 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1796 if (!node.clipRect) {
1797 dstyle.position = "absolute";
1800 dstyle.width = o.paper.width + "px";
1801 dstyle.height = o.paper.height + "px";
1802 group.parentNode.insertBefore(div, group);
1803 div[appendChild](group);
1804 node.clipRect = div;
1807 if (!params["clip-rect"]) {
1808 node.clipRect && (node.clipRect.style.clip = E);
1811 if (o.type == "image" && params.src) {
1812 node.src = params.src;
1814 if (o.type == "image" && params.opacity) {
1815 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1816 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1818 params.font && (s.font = params.font);
1819 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1820 params["font-size"] && (s.fontSize = params["font-size"]);
1821 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1822 params["font-style"] && (s.fontStyle = params["font-style"]);
1823 if (params.opacity != null ||
1824 params["stroke-width"] != null ||
1825 params.fill != null ||
1826 params.stroke != null ||
1827 params["stroke-width"] != null ||
1828 params["stroke-opacity"] != null ||
1829 params["fill-opacity"] != null ||
1830 params["stroke-dasharray"] != null ||
1831 params["stroke-miterlimit"] != null ||
1832 params["stroke-linejoin"] != null ||
1833 params["stroke-linecap"] != null) {
1834 node = o.shape || node;
1835 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1837 !fill && (newfill = fill = createNode(fillString));
1838 if ("fill-opacity" in params || "opacity" in params) {
1839 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1840 opacity < 0 && (opacity = 0);
1841 opacity > 1 && (opacity = 1);
1842 fill.opacity = opacity;
1844 params.fill && (fill.on = true);
1845 if (fill.on == null || params.fill == "none") {
1848 if (fill.on && params.fill) {
1849 var isURL = params.fill.match(ISURL);
1851 fill.src = isURL[1];
1854 fill.color = R.getRGB(params.fill).hex;
1856 fill.type = "solid";
1857 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1859 a.gradient = params.fill;
1863 newfill && node[appendChild](fill);
1864 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1866 !stroke && (newstroke = stroke = createNode("stroke"));
1867 if ((params.stroke && params.stroke != "none") ||
1868 params["stroke-width"] ||
1869 params["stroke-opacity"] != null ||
1870 params["stroke-dasharray"] ||
1871 params["stroke-miterlimit"] ||
1872 params["stroke-linejoin"] ||
1873 params["stroke-linecap"]) {
1876 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1877 var strokeColor = R.getRGB(params.stroke);
1878 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
1879 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
1880 var width = (toFloat(params["stroke-width"]) || 1) * .75;
1881 opacity < 0 && (opacity = 0);
1882 opacity > 1 && (opacity = 1);
1883 params["stroke-width"] == null && (width = a["stroke-width"]);
1884 params["stroke-width"] && (stroke.weight = width);
1885 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1886 stroke.opacity = opacity;
1888 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1889 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1890 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1891 if (params["stroke-dasharray"]) {
1895 "-.": "shortdashdot",
1896 "-..": "shortdashdotdot",
1901 "--.": "longdashdot",
1902 "--..": "longdashdotdot"
1904 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1906 newstroke && node[appendChild](stroke);
1908 if (res.type == "text") {
1909 s = res.paper.span.style;
1910 a.font && (s.font = a.font);
1911 a["font-family"] && (s.fontFamily = a["font-family"]);
1912 a["font-size"] && (s.fontSize = a["font-size"]);
1913 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1914 a["font-style"] && (s.fontStyle = a["font-style"]);
1915 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1916 res.W = a.w = res.paper.span.offsetWidth;
1917 res.H = a.h = res.paper.span.offsetHeight;
1919 res.Y = a.y + round(res.H / 2);
1921 // text-anchor emulationm
1922 switch (a["text-anchor"]) {
1924 res.node.style["v-text-align"] = "left";
1925 res.bbx = round(res.W / 2);
1928 res.node.style["v-text-align"] = "right";
1929 res.bbx = -round(res.W / 2);
1932 res.node.style["v-text-align"] = "center";
1937 addGradientFill = function (o, gradient) {
1938 o.attrs = o.attrs || {};
1939 var attrs = o.attrs,
1943 o.attrs.gradient = gradient;
1944 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1949 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1954 gradient = gradient[split](/\s*\-\s*/);
1955 if (type == "linear") {
1956 var angle = gradient.shift();
1957 angle = -toFloat(angle);
1962 var dots = parseDots(gradient);
1966 o = o.shape || o.node;
1967 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
1968 !fill.parentNode && o.appendChild(fill);
1971 fill.method = "none";
1972 fill.color = dots[0].color;
1973 fill.color2 = dots[dots[length] - 1].color;
1975 for (var i = 0, ii = dots[length]; i < ii; i++) {
1976 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1978 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
1979 if (type == "radial") {
1980 fill.type = "gradientradial";
1981 fill.focus = "100%";
1982 fill.focussize = fxfy;
1983 fill.focusposition = fxfy;
1985 fill.type = "gradient";
1986 fill.angle = (270 - angle) % 360;
1991 Element = function (node, group, vml) {
1999 node.raphael = this;
2012 !vml.bottom && (vml.bottom = this);
2013 this.prev = vml.top;
2014 vml.top && (vml.top.next = this);
2018 Element[proto].rotate = function (deg, cx, cy) {
2024 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2026 return this._.rt.deg;
2028 deg = (deg + E)[split](separator);
2029 if (deg[length] - 1) {
2030 cx = toFloat(deg[1]);
2031 cy = toFloat(deg[2]);
2033 deg = toFloat(deg[0]);
2035 this._.rt.deg = deg;
2037 this._.rt.deg += deg;
2039 cy == null && (cx = null);
2042 this.setBox(this.attrs, cx, cy);
2043 this.Group.style.rotation = this._.rt.deg;
2044 // gradient fix for rotation. TODO
2045 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2046 // fill = fill[0] || {};
2047 // var b = ((360 - this._.rt.deg) - 270) % 360;
2048 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2051 Element[proto].setBox = function (params, cx, cy) {
2055 var gs = this.Group.style,
2056 os = (this.shape && this.shape.style) || this.node.style;
2057 params = params || {};
2058 for (var i in params) if (params[has](i)) {
2059 this.attrs[i] = params[i];
2061 cx = cx || this._.rt.cx;
2062 cy = cy || this._.rt.cy;
2063 var attr = this.attrs,
2068 switch (this.type) {
2070 x = attr.cx - attr.r;
2071 y = attr.cy - attr.r;
2075 x = attr.cx - attr.rx;
2076 y = attr.cy - attr.ry;
2083 w = attr.width || 0;
2084 h = attr.height || 0;
2087 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2088 x = attr.x - round(this.W / 2);
2089 y = attr.y - this.H / 2;
2095 if (!this.attrs.path) {
2098 w = this.paper.width;
2099 h = this.paper.height;
2101 var dim = pathDimensions(this.attrs.path);
2111 w = this.paper.width;
2112 h = this.paper.height;
2115 cx = (cx == null) ? x + w / 2 : cx;
2116 cy = (cy == null) ? y + h / 2 : cy;
2117 var left = cx - this.paper.width / 2,
2118 top = cy - this.paper.height / 2, t;
2119 gs.left != (t = left + "px") && (gs.left = t);
2120 gs.top != (t = top + "px") && (gs.top = t);
2121 this.X = pathlike[has](this.type) ? -left : x;
2122 this.Y = pathlike[has](this.type) ? -top : y;
2125 if (pathlike[has](this.type)) {
2126 os.left != (t = -left * zoom + "px") && (os.left = t);
2127 os.top != (t = -top * zoom + "px") && (os.top = t);
2128 } else if (this.type == "text") {
2129 os.left != (t = -left + "px") && (os.left = t);
2130 os.top != (t = -top + "px") && (os.top = t);
2132 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2133 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2134 os.left != (t = x - left + "px") && (os.left = t);
2135 os.top != (t = y - top + "px") && (os.top = t);
2136 os.width != (t = w + "px") && (os.width = t);
2137 os.height != (t = h + "px") && (os.height = t);
2140 Element[proto].hide = function () {
2141 !this.removed && (this.Group.style.display = "none");
2144 Element[proto].show = function () {
2145 !this.removed && (this.Group.style.display = "block");
2148 Element[proto].getBBox = function () {
2152 if (pathlike[has](this.type)) {
2153 return pathDimensions(this.attrs.path);
2156 x: this.X + (this.bbx || 0),
2162 Element[proto].remove = function () {
2166 tear(this, this.paper);
2167 this.node.parentNode.removeChild(this.node);
2168 this.Group.parentNode.removeChild(this.Group);
2169 this.shape && this.shape.parentNode.removeChild(this.shape);
2170 for (var i in this) {
2173 this.removed = true;
2175 Element[proto].attr = function (name, value) {
2181 for (var i in this.attrs) if (this.attrs[has](i)) {
2182 res[i] = this.attrs[i];
2184 this._.rt.deg && (res.rotation = this.rotate());
2185 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2186 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2189 if (value == null && R.is(name, string)) {
2190 if (name == "translation") {
2191 return translate.call(this);
2193 if (name == "rotation") {
2194 return this.rotate();
2196 if (name == "scale") {
2197 return this.scale();
2199 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2200 return this.attrs.gradient;
2202 return this.attrs[name];
2204 if (this.attrs && value == null && R.is(name, array)) {
2205 var ii, values = {};
2206 for (i = 0, ii = name[length]; i < ii; i++) {
2207 values[name[i]] = this.attr(name[i]);
2212 if (value != null) {
2214 params[name] = value;
2216 value == null && R.is(name, "object") && (params = name);
2218 if (params.text && this.type == "text") {
2219 this.node.string = params.text;
2221 setFillAndStroke(this, params);
2222 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2223 addGradientFill(this, params.gradient);
2225 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2229 Element[proto].toFront = function () {
2230 !this.removed && this.Group.parentNode[appendChild](this.Group);
2231 this.paper.top != this && tofront(this, this.paper);
2234 Element[proto].toBack = function () {
2238 if (this.Group.parentNode.firstChild != this.Group) {
2239 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2240 toback(this, this.paper);
2244 Element[proto].insertAfter = function (element) {
2248 if (element.Group.nextSibling) {
2249 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2251 element.Group.parentNode[appendChild](this.Group);
2253 insertafter(this, element, this.paper);
2256 Element[proto].insertBefore = function (element) {
2260 element.Group.parentNode.insertBefore(this.Group, element.Group);
2261 insertbefore(this, element, this.paper);
2264 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2265 Element[proto].blur = function (size) {
2266 var s = this.node.runtimeStyle,
2268 f = f.replace(blurregexp, E);
2270 this.attrs.blur = size;
2271 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2272 s.margin = Raphael.format("-{0}px 0 0 -{0}px", Math.round(+size || 1.5));
2276 delete this.attrs.blur;
2280 theCircle = function (vml, x, y, r) {
2281 var g = createNode("group"),
2282 o = createNode("oval"),
2284 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2285 g.coordsize = coordsize;
2286 g.coordorigin = vml.coordorigin;
2288 var res = new Element(o, g, vml);
2289 res.type = "circle";
2290 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2294 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2295 vml.canvas[appendChild](g);
2298 function rectPath(x, y, w, h, r) {
2300 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);
2302 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2305 theRect = function (vml, x, y, w, h, r) {
2306 var path = rectPath(x, y, w, h, r),
2307 res = vml.path(path),
2311 res.W = a.width = w;
2312 res.H = a.height = h;
2318 theEllipse = function (vml, x, y, rx, ry) {
2319 var g = createNode("group"),
2320 o = createNode("oval"),
2322 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2323 g.coordsize = coordsize;
2324 g.coordorigin = vml.coordorigin;
2326 var res = new Element(o, g, vml);
2327 res.type = "ellipse";
2328 setFillAndStroke(res, {stroke: "#000"});
2333 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2334 vml.canvas[appendChild](g);
2337 theImage = function (vml, src, x, y, w, h) {
2338 var g = createNode("group"),
2339 o = createNode("image"),
2341 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2342 g.coordsize = coordsize;
2343 g.coordorigin = vml.coordorigin;
2346 var res = new Element(o, g, vml);
2348 res.attrs.src = src;
2353 res.setBox({x: x, y: y, width: w, height: h});
2354 vml.canvas[appendChild](g);
2357 theText = function (vml, x, y, text) {
2358 var g = createNode("group"),
2359 el = createNode("shape"),
2361 path = createNode("path"),
2363 o = createNode("textpath");
2364 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2365 g.coordsize = coordsize;
2366 g.coordorigin = vml.coordorigin;
2367 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2368 path.textpathok = true;
2369 ol.width = vml.width;
2370 ol.height = vml.height;
2371 o.string = text + E;
2374 el[appendChild](path);
2376 var res = new Element(o, g, vml);
2378 res.textpath = path;
2380 res.attrs.text = text;
2385 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2387 vml.canvas[appendChild](g);
2390 setSize = function (width, height) {
2391 var cs = this.canvas.style;
2392 width == +width && (width += "px");
2393 height == +height && (height += "px");
2396 cs.clip = "rect(0 " + width + " " + height + " 0)";
2400 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2402 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2403 createNode = function (tagName) {
2404 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2407 createNode = function (tagName) {
2408 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2411 create = function () {
2412 var con = getContainer[apply](0, arguments),
2413 container = con.container,
2414 height = con.height,
2420 throw new Error("VML container not found.");
2422 var res = new Paper,
2423 c = res.canvas = doc.createElement("div"),
2427 width = width || 512;
2428 height = height || 342;
2429 width == +width && (width += "px");
2430 height == +height && (height += "px");
2433 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2434 res.coordorigin = "0 0";
2435 res.span = doc.createElement("span");
2436 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2437 c[appendChild](res.span);
2438 cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2439 if (container == 1) {
2440 doc.body[appendChild](c);
2443 cs.position = "absolute";
2445 if (container.firstChild) {
2446 container.insertBefore(c, container.firstChild);
2448 container[appendChild](c);
2451 plugins.call(res, res, R.fn);
2454 Paper[proto].clear = function () {
2455 this.canvas.innerHTML = E;
2456 this.span = doc.createElement("span");
2457 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2458 this.canvas[appendChild](this.span);
2459 this.bottom = this.top = null;
2461 Paper[proto].remove = function () {
2462 this.canvas.parentNode.removeChild(this.canvas);
2463 for (var i in this) {
2464 this[i] = removed(i);
2471 // Safari or Chrome (WebKit) rendering bug workaround method
2472 if ((/^Apple|^Google/).test(win.navigator.vendor) && (!(win.navigator.userAgent.indexOf("Version/4.0") + 1) || win.navigator.platform.slice(0, 2) == "iP")) {
2473 Paper[proto].safari = function () {
2474 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2475 win.setTimeout(function () {rect.remove();});
2478 Paper[proto].safari = function () {};
2482 var preventDefault = function () {
2483 this.returnValue = false;
2485 preventTouch = function () {
2486 return this.originalEvent.preventDefault();
2488 stopPropagation = function () {
2489 this.cancelBubble = true;
2491 stopTouch = function () {
2492 return this.originalEvent.stopPropagation();
2494 addEvent = (function () {
2495 if (doc.addEventListener) {
2496 return function (obj, type, fn, element) {
2497 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2498 var f = function (e) {
2499 if (supportsTouch && touchMap[has](type)) {
2500 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2501 if (e.targetTouches[i].target == obj) {
2503 e = e.targetTouches[i];
2504 e.originalEvent = olde;
2505 e.preventDefault = preventTouch;
2506 e.stopPropagation = stopTouch;
2511 return fn.call(element, e);
2513 obj.addEventListener(realName, f, false);
2514 return function () {
2515 obj.removeEventListener(realName, f, false);
2519 } else if (doc.attachEvent) {
2520 return function (obj, type, fn, element) {
2521 var f = function (e) {
2523 e.preventDefault = e.preventDefault || preventDefault;
2524 e.stopPropagation = e.stopPropagation || stopPropagation;
2525 return fn.call(element, e);
2527 obj.attachEvent("on" + type, f);
2528 var detacher = function () {
2529 obj.detachEvent("on" + type, f);
2536 for (var i = events[length]; i--;) {
2537 (function (eventName) {
2538 R[eventName] = Element[proto][eventName] = function (fn) {
2539 if (R.is(fn, "function")) {
2540 this.events = this.events || [];
2541 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
2545 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2546 var events = this.events,
2548 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2550 events.splice(l, 1);
2551 !events.length && delete this.events;
2558 Element[proto].hover = function (f_in, f_out) {
2559 return this.mouseover(f_in).mouseout(f_out);
2561 Element[proto].unhover = function (f_in, f_out) {
2562 return this.unmouseover(f_in).unmouseout(f_out);
2564 Element[proto].drag = function (onmove, onstart, onend) {
2566 var el = this.mousedown(function (e) {
2567 (e.originalEvent ? e.originalEvent : e).preventDefault();
2568 this._drag.x = e.clientX;
2569 this._drag.y = e.clientY;
2570 this._drag.id = e.identifier;
2571 onstart && onstart.call(this, e.clientX, e.clientY);
2572 Raphael.mousemove(move).mouseup(up);
2574 move = function (e) {
2577 if (supportsTouch) {
2578 var i = e.touches.length,
2581 touch = e.touches[i];
2582 if (touch.identifier == el._drag.id) {
2585 (e.originalEvent ? e.originalEvent : e).preventDefault();
2592 onmove && onmove.call(el, x - el._drag.x, y - el._drag.y, x, y);
2596 Raphael.unmousemove(move).unmouseup(up);
2597 onend && onend.call(el);
2601 Paper[proto].circle = function (x, y, r) {
2602 return theCircle(this, x || 0, y || 0, r || 0);
2604 Paper[proto].rect = function (x, y, w, h, r) {
2605 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2607 Paper[proto].ellipse = function (x, y, rx, ry) {
2608 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2610 Paper[proto].path = function (pathString) {
2611 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2612 return thePath(R.format[apply](R, arguments), this);
2614 Paper[proto].image = function (src, x, y, w, h) {
2615 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2617 Paper[proto].text = function (x, y, text) {
2618 return theText(this, x || 0, y || 0, text || E);
2620 Paper[proto].set = function (itemsArray) {
2621 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2622 return new Set(itemsArray);
2624 Paper[proto].setSize = setSize;
2625 Paper[proto].top = Paper[proto].bottom = null;
2626 Paper[proto].raphael = R;
2628 return this.x + S + this.y;
2630 Element[proto].resetScale = function () {
2636 this.attrs.scale = "1 1";
2638 Element[proto].scale = function (x, y, cx, cy) {
2642 if (x == null && y == null) {
2657 var bb = this.getBBox(),
2658 rcx = bb.x + bb.width / 2,
2659 rcy = bb.y + bb.height / 2,
2662 cx = (+cx || cx == 0) ? cx : rcx;
2663 cy = (+cy || cy == 0) ? cy : rcy;
2664 var dirx = ~~(x / math.abs(x)),
2665 diry = ~~(y / math.abs(y)),
2666 s = this.node.style,
2667 ncx = cx + (rcx - cx) * kx,
2668 ncy = cy + (rcy - cy) * ky;
2669 switch (this.type) {
2672 var neww = a.width * dirx * kx,
2673 newh = a.height * diry * ky;
2676 r: a.r * mmin(dirx * kx, diry * ky),
2685 rx: a.rx * dirx * kx,
2686 ry: a.ry * diry * ky,
2687 r: a.r * mmin(dirx * kx, diry * ky),
2699 var path = pathToRelative(a.path),
2701 for (var i = 0, ii = path[length]; i < ii; i++) {
2703 P0 = upperCase.call(p[0]);
2704 if (P0 == "M" && skip) {
2710 p[path[i][length] - 2] *= kx;
2711 p[path[i][length] - 1] *= ky;
2714 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2715 } else if (P0 == "H") {
2716 for (var j = 1, jj = p[length]; j < jj; j++) {
2719 } else if (P0 == "V") {
2720 for (j = 1, jj = p[length]; j < jj; j++) {
2724 for (j = 1, jj = p[length]; j < jj; j++) {
2725 p[j] *= (j % 2) ? kx : ky;
2729 var dim2 = pathDimensions(path);
2730 dx = ncx - dim2.x - dim2.width / 2;
2731 dy = ncy - dim2.y - dim2.height / 2;
2734 this.attr({path: path});
2737 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2738 if (this.transformations) {
2739 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2740 this.node[setAttribute]("transform", this.transformations[join](S));
2741 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2742 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2743 this.attr({x: dx, y: dy});
2747 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2748 ", M12=0, M21=0, M22=", diry,
2749 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2750 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2753 if (this.transformations) {
2754 this.transformations[2] = E;
2755 this.node[setAttribute]("transform", this.transformations[join](S));
2759 this.node.filterMatrix = E;
2760 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2763 a.scale = [x, y, cx, cy][join](S);
2769 Element[proto].clone = function () {
2773 var attr = this.attr();
2775 delete attr.translation;
2776 return this.paper[this.type]().attr(attr);
2778 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2781 for (var i = 0; i < 1.001; i+=.001) {
2782 var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2783 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2784 if (len >= length) {
2790 getLengthFactory = function (istotal, subpath) {
2791 return function (path, length, onlystart) {
2792 path = path2curve(path);
2793 var x, y, p, l, sp = "", subpaths = {}, point,
2795 for (var i = 0, ii = path.length; i < ii; i++) {
2801 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2802 if (len + l > length) {
2803 if (subpath && !subpaths.start) {
2804 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2805 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2806 if (onlystart) {return sp;}
2807 subpaths.start = sp;
2808 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2814 if (!istotal && !subpath) {
2815 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2816 return {x: point.x, y: point.y, alpha: point.alpha};
2826 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2827 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2831 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2832 var old = {x: 0, y: 0},
2834 for (var i = 0; i < 1.01; i+=.01) {
2835 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2836 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2841 var getTotalLength = getLengthFactory(1),
2842 getPointAtLength = getLengthFactory(),
2843 getSubpathsAtLength = getLengthFactory(0, 1);
2844 Element[proto].getTotalLength = function () {
2845 if (this.type != "path") {return;}
2846 if (this.node.getTotalLength) {
2847 return this.node.getTotalLength();
2849 return getTotalLength(this.attrs.path);
2851 Element[proto].getPointAtLength = function (length) {
2852 if (this.type != "path") {return;}
2853 return getPointAtLength(this.attrs.path, length);
2855 Element[proto].getSubpath = function (from, to) {
2856 if (this.type != "path") {return;}
2857 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2858 return getSubpathsAtLength(this.attrs.path, from).end;
2860 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2861 return from ? getSubpathsAtLength(a, from).end : a;
2864 // animation easing formulas
2865 R.easing_formulas = {
2866 linear: function (n) {
2873 return pow(n - 1, 3) + 1;
2875 "<>": function (n) {
2878 return pow(n, 3) / 2;
2881 return (pow(n, 3) + 2) / 2;
2883 backIn: function (n) {
2885 return n * n * ((s + 1) * n - s);
2887 backOut: function (n) {
2890 return n * n * ((s + 1) * n + s) + 1;
2892 elastic: function (n) {
2893 if (n == 0 || n == 1) {
2898 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2900 bounce: function (n) {
2909 l = s * n * n + .75;
2911 if (n < (2.5 / p)) {
2913 l = s * n * n + .9375;
2916 l = s * n * n + .984375;
2924 var animationElements = {length : 0},
2925 animation = function () {
2926 var Now = +new Date;
2927 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2928 var e = animationElements[l];
2929 if (e.stop || e.el.removed) {
2930 delete animationElements[l];
2931 animationElements[length]--;
2934 var time = Now - e.start,
2943 callback = e.callback,
2947 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2948 for (var attr in from) if (from[has](attr)) {
2949 switch (availableAnimAttrs[attr]) {
2951 now = pos * ms * diff[attr];
2952 to.back && (now = to.len - now);
2953 var point = getPointAtLength(to[attr], now);
2954 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2957 that.translate(point.x - diff.sx, point.y - diff.sy);
2958 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2961 now = +from[attr] + pos * ms * diff[attr];
2965 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2966 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2967 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2972 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2973 now[i] = [from[attr][i][0]];
2974 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2975 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2977 now[i] = now[i][join](S);
2984 var x = diff[attr][0] * (time - prev),
2985 y = diff[attr][1] * (time - prev);
2991 now = +from[attr][0] + pos * ms * diff[attr][0];
2992 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2995 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);
3001 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3010 that._run && that._run.call(that);
3013 point = getPointAtLength(to.along, to.len * !to.back);
3014 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3015 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3017 (t.x || t.y) && that.translate(-t.x, -t.y);
3018 to.scale && (to.scale += E);
3020 delete animationElements[l];
3021 animationElements[length]--;
3022 that.in_animation = null;
3023 R.is(callback, "function") && callback.call(that);
3027 R.svg && that && that.paper && that.paper.safari();
3028 animationElements[length] && win.setTimeout(animation);
3030 upto255 = function (color) {
3031 return mmax(mmin(color, 255), 0);
3033 translate = function (x, y) {
3035 return {x: this._.tx, y: this._.ty, toString: x_y};
3039 switch (this.type) {
3042 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3047 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3050 var path = pathToRelative(this.attrs.path);
3053 this.attr({path: path});
3058 Element[proto].animateWith = function (element, params, ms, easing, callback) {
3059 animationElements[element.id] && (params.start = animationElements[element.id].start);
3060 return this.animate(params, ms, easing, callback);
3062 Element[proto].animateAlong = along();
3063 Element[proto].animateAlongBack = along(1);
3064 function along(isBack) {
3065 return function (path, ms, rotate, callback) {
3066 var params = {back: isBack};
3067 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3068 path && path.constructor == Element && (path = path.attrs.path);
3069 path && (params.along = path);
3070 return this.animate(params, ms, callback);
3073 Element[proto].onAnimation = function (f) {
3077 Element[proto].animate = function (params, ms, easing, callback) {
3078 if (R.is(easing, "function") || !easing) {
3079 callback = easing || null;
3084 for (var attr in params) if (params[has](attr)) {
3085 if (availableAnimAttrs[has](attr)) {
3086 from[attr] = this.attr(attr);
3087 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3088 to[attr] = params[attr];
3089 switch (availableAnimAttrs[attr]) {
3091 var len = getTotalLength(params[attr]),
3092 point = getPointAtLength(params[attr], len * !!params.back),
3093 bb = this.getBBox();
3094 diff[attr] = len / ms;
3099 to.rot = params.rot;
3100 to.back = params.back;
3102 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3105 diff[attr] = (to[attr] - from[attr]) / ms;
3108 from[attr] = R.getRGB(from[attr]);
3109 var toColour = R.getRGB(to[attr]);
3111 r: (toColour.r - from[attr].r) / ms,
3112 g: (toColour.g - from[attr].g) / ms,
3113 b: (toColour.b - from[attr].b) / ms
3117 var pathes = path2curve(from[attr], to[attr]);
3118 from[attr] = pathes[0];
3119 var toPath = pathes[1];
3121 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3122 diff[attr][i] = [0];
3123 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3124 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3129 var values = (params[attr] + E)[split](separator),
3130 from2 = (from[attr] + E)[split](separator);
3133 from[attr] = [0, 0];
3134 diff[attr] = [values[0] / ms, values[1] / ms];
3137 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3138 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3141 params[attr] = values;
3142 from[attr] = (from[attr] + E)[split](separator);
3143 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3146 from[attr] = (from[attr] + E)[split](separator);
3150 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3159 this.in_animation = 1;
3160 animationElements[this.id] = {
3161 start: params.start || +new Date,
3171 ++animationElements[length] == 1 && animation();
3174 Element[proto].stop = function () {
3175 animationElements[this.id] && animationElements[length]--;
3176 delete animationElements[this.id];
3179 Element[proto].translate = function (x, y) {
3180 return this.attr({translation: x + " " + y});
3182 Element[proto][toString] = function () {
3183 return "Rapha\xebl\u2019s object";
3185 R.ae = animationElements;
3188 var Set = function (items) {
3193 for (var i = 0, ii = items[length]; i < ii; i++) {
3194 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3195 this[this.items[length]] = this.items[this.items[length]] = items[i];
3201 Set[proto][push] = function () {
3204 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3205 item = arguments[i];
3206 if (item && (item.constructor == Element || item.constructor == Set)) {
3207 len = this.items[length];
3208 this[len] = this.items[len] = item;
3214 Set[proto].pop = function () {
3215 delete this[this[length]--];
3216 return this.items.pop();
3218 for (var method in Element[proto]) if (Element[proto][has](method)) {
3219 Set[proto][method] = (function (methodname) {
3220 return function () {
3221 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3222 this.items[i][methodname][apply](this.items[i], arguments);
3228 Set[proto].attr = function (name, value) {
3229 if (name && R.is(name, array) && R.is(name[0], "object")) {
3230 for (var j = 0, jj = name[length]; j < jj; j++) {
3231 this.items[j].attr(name[j]);
3234 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3235 this.items[i].attr(name, value);
3240 Set[proto].animate = function (params, ms, easing, callback) {
3241 (R.is(easing, "function") || !easing) && (callback = easing || null);
3242 var len = this.items[length],
3247 callback && (collector = function () {
3248 !--len && callback.call(set);
3250 easing = R.is(easing, string) ? easing : collector;
3251 item = this.items[--i].animate(params, ms, easing, collector);
3253 this.items[i].animateWith(item, params, ms, easing, collector);
3257 Set[proto].insertAfter = function (el) {
3258 var i = this.items[length];
3260 this.items[i].insertAfter(el);
3264 Set[proto].getBBox = function () {
3269 for (var i = this.items[length]; i--;) {
3270 var box = this.items[i].getBBox();
3273 w[push](box.x + box.width);
3274 h[push](box.y + box.height);
3276 x = mmin[apply](0, x);
3277 y = mmin[apply](0, y);
3281 width: mmax[apply](0, w) - x,
3282 height: mmax[apply](0, h) - y
3285 Set[proto].clone = function (s) {
3287 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3288 s[push](this.items[i].clone());
3293 R.registerFont = function (font) {
3297 this.fonts = this.fonts || {};
3303 family = font.face["font-family"];
3304 for (var prop in font.face) if (font.face[has](prop)) {
3305 fontcopy.face[prop] = font.face[prop];
3307 if (this.fonts[family]) {
3308 this.fonts[family][push](fontcopy);
3310 this.fonts[family] = [fontcopy];
3313 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3314 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3315 var path = font.glyphs[glyph];
3316 fontcopy.glyphs[glyph] = {
3319 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3320 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3324 for (var k in path.k) if (path[has](k)) {
3325 fontcopy.glyphs[glyph].k[k] = path.k[k];
3332 Paper[proto].getFont = function (family, weight, style, stretch) {
3333 stretch = stretch || "normal";
3334 style = style || "normal";
3335 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3339 var font = R.fonts[family];
3341 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3342 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3343 if (name.test(fontName)) {
3344 font = R.fonts[fontName];
3351 for (var i = 0, ii = font[length]; i < ii; i++) {
3353 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3360 Paper[proto].print = function (x, y, string, font, size, origin) {
3361 origin = origin || "middle"; // baseline|middle
3362 var out = this.set(),
3363 letters = (string + E)[split](E),
3367 R.is(font, string) && (font = this.getFont(font));
3369 scale = (size || 16) / font.face["units-per-em"];
3370 var bb = font.face.bbox.split(separator),
3372 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3373 for (var i = 0, ii = letters[length]; i < ii; i++) {
3374 var prev = i && font.glyphs[letters[i - 1]] || {},
3375 curr = font.glyphs[letters[i]];
3376 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3377 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3379 out.scale(scale, scale, top, height).translate(x - top, y - height);
3384 var formatrg = /\{(\d+)\}/g;
3385 R.format = function (token, params) {
3386 var args = R.is(params, array) ? [0][concat](params) : arguments;
3387 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3388 return args[++i] == null ? E : args[i];
3392 R.ninja = function () {
3393 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3396 R.el = Element[proto];