2 * Raphael 1.3.0dev - JavaScript Vector Library
4 * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 window.Raphael = (function () {
10 var separator = /[, ]+/,
11 elements = /^(circle|rect|path|ellipse|text|image)$/,
15 was: "Raphael" in win,
19 if (R.is(arguments[0], "array")) {
21 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
23 for (var i = 0, ii = a[length]; i < ii; i++) {
25 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
29 return create[apply](R, arguments);
31 Paper = function () {},
32 appendChild = "appendChild",
38 events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
39 has = "hasOwnProperty",
43 lowerCase = String[proto].toLowerCase,
48 toString = "toString",
49 objectToString = Object[proto][toString],
54 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
55 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
57 setAttribute = "setAttribute",
60 upperCase = String[proto].toUpperCase,
61 availableAttrs = {"clip-rect": "0 0 10e9 10e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
62 availableAnimAttrs = {"clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
64 R.version = "1.3.0dev";
65 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
66 if (R.type == "VML") {
67 var d = document.createElement("div");
68 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
69 if (d.childNodes[length] != 2) {
73 R.svg = !(R.vml = R.type == "VML");
74 Paper[proto] = R[proto];
78 R.is = function (o, type) {
79 type = lowerCase.call(type);
80 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
82 R.setWindow = function (newwin) {
87 var toHex = function (color) {
89 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
90 var trim = /^\s+|\s+$/g;
91 toHex = cacher(function (color) {
93 color = (color + E)[rp](trim, E);
95 var docum = new ActiveXObject("htmlfile");
96 docum.write("<body>");
100 bod = createPopup().document.body;
102 var range = bod.createTextRange();
104 bod.style.color = color;
105 var value = range.queryCommandValue("ForeColor");
106 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
107 return "#" + ("000000" + value[toString](16)).slice(-6);
113 var i = doc.createElement("i");
114 i.title = "Rapha\xebl Colour Picker";
115 i.style.display = "none";
116 doc.body[appendChild](i);
117 toHex = cacher(function (color) {
118 i.style.color = color;
119 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
124 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
125 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
133 if (brightness == 0) {
134 return {r: 0, g: 0, b: 0, hex: "#000"};
136 if (hue > 1 || saturation > 1 || brightness > 1) {
143 p = brightness * (1 - saturation),
144 q = brightness * (1 - (saturation * f)),
145 t = brightness * (1 - (saturation * (1 - f)));
146 red = [brightness, q, p, p, t, brightness, brightness][i];
147 green = [t, brightness, brightness, q, p, p, t][i];
148 blue = [p, p, t, brightness, brightness, q, p][i];
152 var rgb = {r: red, g: green, b: blue},
153 r = (~~red)[toString](16),
154 g = (~~green)[toString](16),
155 b = (~~blue)[toString](16);
159 rgb.hex = "#" + r + g + b;
162 R.rgb2hsb = cacher(function (red, green, blue) {
163 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
168 if (R.is(red, "string")) {
169 var clr = R.getRGB(red);
174 if (red > 1 || green > 1 || blue > 1) {
179 var max = mmax(red, green, blue),
180 min = mmin(red, green, blue),
185 return {h: 0, s: 0, b: max};
187 var delta = (max - min);
188 saturation = delta / max;
190 hue = (green - blue) / delta;
191 } else if (green == max) {
192 hue = 2 + ((blue - red) / delta);
194 hue = 4 + ((red - green) / delta);
200 return {h: hue, s: saturation, b: brightness};
202 var p2s = /,?([achlmqrstvxz]),?/gi;
203 R._path2string = function () {
204 return this.join(",")[rp](p2s, "$1");
206 function cacher(f, scope, postprocessor) {
208 var arg = Array[proto].slice.call(arguments, 0),
209 args = arg[join]("\u25ba"),
210 cache = newf.cache = newf.cache || {},
211 count = newf.count = newf.count || [];
212 if (cache[has](args)) {
213 return postprocessor ? postprocessor(cache[args]) : cache[args];
215 count[length] >= 1e3 && delete cache[count.shift()];
217 cache[args] = f[apply](scope, arg);
218 return postprocessor ? postprocessor(cache[args]) : cache[args];
223 R.getRGB = cacher(function (colour) {
224 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
225 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
227 if (colour == "none") {
228 return {r: -1, g: -1, b: -1, hex: "none"};
230 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
236 rgb = colour.match(colourRegExp);
239 blue = toInt(rgb[2].substring(5), 16);
240 green = toInt(rgb[2].substring(3, 5), 16);
241 red = toInt(rgb[2].substring(1, 3), 16);
244 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
245 green = toInt((t = rgb[3].charAt(2)) + t, 16);
246 red = toInt((t = rgb[3].charAt(1)) + t, 16);
249 rgb = rgb[4][split](/\s*,\s*/);
250 red = toFloat(rgb[0]);
251 green = toFloat(rgb[1]);
252 blue = toFloat(rgb[2]);
255 rgb = rgb[5][split](/\s*,\s*/);
256 red = toFloat(rgb[0]) * 2.55;
257 green = toFloat(rgb[1]) * 2.55;
258 blue = toFloat(rgb[2]) * 2.55;
261 rgb = rgb[6][split](/\s*,\s*/);
262 red = toFloat(rgb[0]);
263 green = toFloat(rgb[1]);
264 blue = toFloat(rgb[2]);
265 return R.hsb2rgb(red, green, blue);
268 rgb = rgb[7][split](/\s*,\s*/);
269 red = toFloat(rgb[0]) * 2.55;
270 green = toFloat(rgb[1]) * 2.55;
271 blue = toFloat(rgb[2]) * 2.55;
272 return R.hsb2rgb(red, green, blue);
274 rgb = {r: red, g: green, b: blue};
275 var r = (~~red)[toString](16),
276 g = (~~green)[toString](16),
277 b = (~~blue)[toString](16);
281 rgb.hex = "#" + r + g + b;
284 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
286 R.getColor = function (value) {
287 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
288 rgb = this.hsb2rgb(start.h, start.s, start.b);
293 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
297 R.getColor.reset = function () {
301 R.parsePathString = cacher(function (pathString) {
305 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
307 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
308 data = pathClone(pathString);
311 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
313 name = lowerCase.call(b);
314 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
315 b && params[push](+b);
317 while (params[length] >= paramCounts[name]) {
318 data[push]([b][concat](params.splice(0, paramCounts[name])));
319 if (!paramCounts[name]) {
325 data[toString] = R._path2string;
328 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
330 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
331 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
332 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
333 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
334 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
335 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
336 ax = (1 - t) * p1x + t * c1x,
337 ay = (1 - t) * p1y + t * c1y,
338 cx = (1 - t) * c2x + t * p2x,
339 cy = (1 - t) * c2y + t * p2y,
340 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI) % 360;
341 return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
343 var pathDimensions = cacher(function (path) {
345 return {x: 0, y: 0, width: 0, height: 0};
347 path = path2curve(path);
353 for (var i = 0, ii = path[length]; i < ii; i++) {
361 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
362 X = X[concat](dim.min.x, dim.max.x);
363 Y = Y[concat](dim.min.y, dim.max.y);
368 var xmin = mmin[apply](0, X),
369 ymin = mmin[apply](0, Y);
373 width: mmax[apply](0, X) - xmin,
374 height: mmax[apply](0, Y) - ymin
377 pathClone = function (pathArray) {
379 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
380 pathArray = R.parsePathString(pathArray);
382 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
384 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
385 res[i][j] = pathArray[i][j];
388 res[toString] = R._path2string;
391 pathToRelative = cacher(function (pathArray) {
392 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
393 pathArray = R.parsePathString(pathArray);
401 if (pathArray[0][0] == "M") {
407 res[push](["M", x, y]);
409 for (var i = start, ii = pathArray[length]; i < ii; i++) {
412 if (pa[0] != lowerCase.call(pa[0])) {
413 r[0] = lowerCase.call(pa[0]);
421 r[6] = +(pa[6] - x).toFixed(3);
422 r[7] = +(pa[7] - y).toFixed(3);
425 r[1] = +(pa[1] - y).toFixed(3);
431 for (var j = 1, jj = pa[length]; j < jj; j++) {
432 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
441 for (var k = 0, kk = pa[length]; k < kk; k++) {
445 var len = res[i][length];
452 x += +res[i][len - 1];
455 y += +res[i][len - 1];
458 x += +res[i][len - 2];
459 y += +res[i][len - 1];
462 res[toString] = R._path2string;
465 pathToAbsolute = cacher(function (pathArray) {
466 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
467 pathArray = R.parsePathString(pathArray);
475 if (pathArray[0][0] == "M") {
476 x = +pathArray[0][1];
477 y = +pathArray[0][2];
481 res[0] = ["M", x, y];
483 for (var i = start, ii = pathArray[length]; i < ii; i++) {
486 if (pa[0] != upperCase.call(pa[0])) {
487 r[0] = upperCase.call(pa[0]);
508 for (var j = 1, jj = pa[length]; j < jj; j++) {
509 r[j] = +pa[j] + ((j % 2) ? x : y);
513 for (var k = 0, kk = pa[length]; k < kk; k++) {
529 x = res[i][res[i][length] - 2];
530 y = res[i][res[i][length] - 1];
533 res[toString] = R._path2string;
536 l2c = function (x1, y1, x2, y2) {
537 return [x1, y1, x2, y2, x2, y2];
539 q2c = function (x1, y1, ax, ay, x2, y2) {
551 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
552 // for more information of where this math came from visit:
553 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
555 _120 = PI * 120 / 180,
556 rad = PI / 180 * (+angle || 0),
559 rotate = cacher(function (x, y, rad) {
560 var X = x * math.cos(rad) - y * math.sin(rad),
561 Y = x * math.sin(rad) + y * math.cos(rad);
565 xy = rotate(x1, y1, -rad);
568 xy = rotate(x2, y2, -rad);
571 var cos = math.cos(PI / 180 * angle),
572 sin = math.sin(PI / 180 * angle),
575 rx = mmax(rx, math.abs(x));
576 ry = mmax(ry, math.abs(y));
579 k = (large_arc_flag == sweep_flag ? -1 : 1) *
580 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
581 cx = k * rx * y / ry + (x1 + x2) / 2,
582 cy = k * -ry * x / rx + (y1 + y2) / 2,
583 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
584 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
586 f1 = x1 < cx ? PI - f1 : f1;
587 f2 = x2 < cx ? PI - f2 : f2;
588 f1 < 0 && (f1 = PI * 2 + f1);
589 f2 < 0 && (f2 = PI * 2 + f2);
590 if (sweep_flag && f1 > f2) {
593 if (!sweep_flag && f2 > f1) {
603 if (math.abs(df) > _120) {
607 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
608 x2 = cx + rx * math.cos(f2);
609 y2 = cy + ry * math.sin(f2);
610 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
613 var c1 = math.cos(f1),
617 t = math.tan(df / 4),
621 m2 = [x1 + hx * s1, y1 - hy * c1],
622 m3 = [x2 + hx * s2, y2 - hy * c2],
624 m2[0] = 2 * m1[0] - m2[0];
625 m2[1] = 2 * m1[1] - m2[1];
627 return [m2, m3, m4][concat](res);
629 res = [m2, m3, m4][concat](res)[join]()[split](",");
631 for (var i = 0, ii = res[length]; i < ii; i++) {
632 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
637 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
640 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
641 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
644 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
645 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
646 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
648 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
649 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
653 math.abs(t1) > 1e12 && (t1 = .5);
654 math.abs(t2) > 1e12 && (t2 = .5);
655 if (t1 > 0 && t1 < 1) {
656 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
660 if (t2 > 0 && t2 < 1) {
661 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
665 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
666 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
668 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
669 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
670 math.abs(t1) > 1e12 && (t1 = .5);
671 math.abs(t2) > 1e12 && (t2 = .5);
672 if (t1 > 0 && t1 < 1) {
673 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
677 if (t2 > 0 && t2 < 1) {
678 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
683 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
684 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
687 path2curve = cacher(function (path, path2) {
688 var p = pathToAbsolute(path),
689 p2 = path2 && pathToAbsolute(path2),
690 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
691 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
692 processPath = function (path, d) {
695 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
697 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
704 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
707 nx = d.x + (d.x - (d.bx || d.x));
708 ny = d.y + (d.y - (d.by || d.y));
709 path = ["C", nx, ny][concat](path.slice(1));
712 d.qx = d.x + (d.x - (d.qx || d.x));
713 d.qy = d.y + (d.y - (d.qy || d.y));
714 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
719 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
722 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
725 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
728 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
731 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
736 fixArc = function (pp, i) {
737 if (pp[i][length] > 7) {
741 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
744 ii = mmax(p[length], p2 && p2[length] || 0);
747 fixM = function (path1, path2, a1, a2, i) {
748 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
749 path2.splice(i, 0, ["M", a2.x, a2.y]);
754 ii = mmax(p[length], p2 && p2[length] || 0);
757 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
758 p[i] = processPath(p[i], attrs);
760 p2 && (p2[i] = processPath(p2[i], attrs2));
762 fixM(p, p2, attrs, attrs2, i);
763 fixM(p2, p, attrs2, attrs, i);
766 seglen = seg[length],
767 seg2len = p2 && seg2[length];
768 attrs.x = seg[seglen - 2];
769 attrs.y = seg[seglen - 1];
770 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
771 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
772 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
773 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
774 attrs2.x = p2 && seg2[seg2len - 2];
775 attrs2.y = p2 && seg2[seg2len - 1];
777 return p2 ? [p, p2] : p;
779 parseDots = cacher(function (gradient) {
781 for (var i = 0, ii = gradient[length]; i < ii; i++) {
783 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
784 dot.color = R.getRGB(par[1]);
785 if (dot.color.error) {
788 dot.color = dot.color.hex;
789 par[2] && (dot.offset = par[2] + "%");
792 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
793 if (!dots[i].offset) {
794 var start = toFloat(dots[i - 1].offset || 0),
796 for (var j = i + 1; j < ii; j++) {
797 if (dots[j].offset) {
798 end = dots[j].offset;
807 var d = (end - start) / (j - i + 1);
810 dots[i].offset = start + "%";
816 getContainer = function () {
822 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
823 if (R.is(arguments[0], "string")) {
824 container = doc.getElementById(arguments[0]);
826 container = arguments[0];
828 if (container.tagName) {
829 if (arguments[1] == null) {
831 container: container,
832 width: container.style.pixelWidth || container.offsetWidth,
833 height: container.style.pixelHeight || container.offsetHeight
836 return {container: container, width: arguments[1], height: arguments[2]};
839 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
840 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
843 plugins = function (con, add) {
845 for (var prop in add) if (add[has](prop) && !(prop in con)) {
846 switch (typeof add[prop]) {
849 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
853 con[prop] = con[prop] || {};
854 plugins.call(this, con[prop], add[prop]);
857 con[prop] = add[prop];
862 tear = function (el, paper) {
863 el == paper.top && (paper.top = el.prev);
864 el == paper.bottom && (paper.bottom = el.next);
865 el.next && (el.next.prev = el.prev);
866 el.prev && (el.prev.next = el.next);
868 tofront = function (el, paper) {
869 if (paper.top === el) {
878 toback = function (el, paper) {
879 if (paper.bottom === el) {
883 el.next = paper.bottom;
885 paper.bottom.prev = el;
888 insertafter = function (el, el2, paper) {
890 el2 == paper.top && (paper.top = el);
891 el2.next && (el2.next.prev = el);
896 insertbefore = function (el, el2, paper) {
898 el2 == paper.bottom && (paper.bottom = el);
899 el2.prev && (el2.prev.next = el);
904 removed = function (methodname) {
906 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
909 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
913 Paper[proto].svgns = "http://www.w3.org/2000/svg";
914 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
915 var round = function (num) {
916 return +num + (~~num === num) * .5;
918 roundPath = function (path) {
919 for (var i = 0, ii = path[length]; i < ii; i++) {
920 if (lowerCase.call(path[i][0]) != "a") {
921 for (var j = 1, jj = path[i][length]; j < jj; j++) {
922 path[i][j] = round(path[i][j]);
925 path[i][6] = round(path[i][6]);
926 path[i][7] = round(path[i][7]);
931 $ = function (el, attr) {
933 for (var key in attr) if (attr[has](key)) {
934 el[setAttribute](key, attr[key]);
937 return doc.createElementNS(Paper[proto].svgns, el);
940 R[toString] = function () {
941 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
943 var thePath = function (pathString, SVG) {
945 SVG.canvas && SVG.canvas[appendChild](el);
946 var p = new Element(el, SVG);
948 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
951 var addGradientFill = function (o, gradient, SVG) {
955 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
960 var dir = ((fy > .5) * 2 - 1);
961 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
962 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
964 (fy = fy.toFixed(5) - 1e-5 * dir);
968 gradient = gradient[split](/\s*\-\s*/);
969 if (type == "linear") {
970 var angle = gradient.shift();
971 angle = -toFloat(angle);
975 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
976 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
980 vector[0] = -vector[2];
984 vector[1] = -vector[3];
988 var dots = parseDots(gradient);
992 var el = $(type + "Gradient");
993 el.id = "r" + (R._id++)[toString](36);
994 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
995 SVG.defs[appendChild](el);
996 for (var i = 0, ii = dots[length]; i < ii; i++) {
997 var stop = $("stop");
999 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1000 "stop-color": dots[i].color || "#fff"
1002 el[appendChild](stop);
1005 fill: "url(#" + el.id + ")",
1014 var updatePosition = function (o) {
1015 var bbox = o.getBBox();
1016 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1018 var setFillAndStroke = function (o, params) {
1025 "-..": [3, 1, 1, 1, 1, 1],
1029 "- .": [4, 3, 1, 3],
1030 "--.": [8, 3, 1, 3],
1031 "--..": [8, 3, 1, 3, 1, 3]
1036 addDashes = function (o, value) {
1037 value = dasharray[lowerCase.call(value)];
1039 var width = o.attrs["stroke-width"] || "1",
1040 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1042 var i = value[length];
1044 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1046 $(node, {"stroke-dasharray": dashes[join](",")});
1049 params[has]("rotation") && (rot = params.rotation);
1050 var rotxy = (rot + E)[split](separator);
1051 if (!(rotxy.length - 1)) {
1054 rotxy[1] = +rotxy[1];
1055 rotxy[2] = +rotxy[2];
1057 toFloat(rot) && o.rotate(0, true);
1058 for (var att in params) if (params[has](att)) {
1059 if (!availableAttrs[has](att)) {
1062 var value = params[att];
1066 o.rotate(value, true);
1072 var pn = node.parentNode;
1073 if (lowerCase.call(pn.tagName) != "a") {
1075 pn.insertBefore(hl, node);
1076 hl[appendChild](node);
1079 pn.setAttributeNS(o.Paper[proto].xlink, att, value);
1082 node.style.cursor = value;
1085 var rect = (value + E)[split](separator);
1086 if (rect[length] == 4) {
1087 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1088 var el = $("clipPath"),
1090 el.id = "r" + (R._id++)[toString](36);
1097 el[appendChild](rc);
1098 o.paper.defs[appendChild](el);
1099 $(node, {"clip-path": "url(#" + el.id + ")"});
1103 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1104 clip && clip.parentNode.removeChild(clip);
1105 $(node, {"clip-path": E});
1110 if (value && o.type == "path") {
1111 attrs.path = roundPath(pathToAbsolute(value));
1112 $(node, {d: attrs.path});
1116 node[setAttribute](att, value);
1125 value = -attrs.x - (attrs.width || 0);
1128 if (att == "rx" && o.type == "rect") {
1132 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1133 node[setAttribute](att, round(value));
1134 o.pattern && updatePosition(o);
1137 node[setAttribute](att, value);
1146 value = -attrs.y - (attrs.height || 0);
1149 if (att == "ry" && o.type == "rect") {
1153 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1154 node[setAttribute](att, round(value));
1155 o.pattern && updatePosition(o);
1158 if (o.type == "rect") {
1159 $(node, {rx: value, ry: value});
1161 node[setAttribute](att, value);
1165 if (o.type == "image") {
1166 node.setAttributeNS(o.paper.xlink, "href", value);
1169 case "stroke-width":
1170 node.style.strokeWidth = value;
1171 // Need following line for Firefox
1172 node[setAttribute](att, value);
1173 if (attrs["stroke-dasharray"]) {
1174 addDashes(o, attrs["stroke-dasharray"]);
1177 case "stroke-dasharray":
1178 addDashes(o, value);
1181 var xy = (value + E)[split](separator);
1182 xy[0] = +xy[0] || 0;
1183 xy[1] = +xy[1] || 0;
1188 translate.call(o, xy[0], xy[1]);
1191 var xy = (value + E)[split](separator);
1192 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1195 var isURL = (value + E).match(ISURL);
1197 var el = $("pattern"),
1199 el.id = "r" + (R._id++)[toString](36);
1200 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1201 $(ig, {x: 0, y: 0});
1202 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1203 el[appendChild](ig);
1205 var img = doc.createElement("img");
1206 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1207 img.onload = function () {
1208 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1209 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1210 doc.body.removeChild(this);
1213 doc.body[appendChild](img);
1215 o.paper.defs[appendChild](el);
1216 node.style.fill = "url(#" + el.id + ")";
1217 $(node, {fill: "url(#" + el.id + ")"});
1219 o.pattern && updatePosition(o);
1222 if (!R.getRGB(value).error) {
1223 delete params.gradient;
1224 delete attrs.gradient;
1225 !R.is(attrs.opacity, "undefined") &&
1226 R.is(params.opacity, "undefined") &&
1227 $(node, {opacity: attrs.opacity});
1228 !R.is(attrs["fill-opacity"], "undefined") &&
1229 R.is(params["fill-opacity"], "undefined") &&
1230 $(node, {"fill-opacity": attrs["fill-opacity"]});
1231 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1232 attrs.gradient = value;
1233 attrs.fill = "none";
1237 node[setAttribute](att, R.getRGB(value).hex);
1240 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1243 case "fill-opacity":
1244 if (attrs.gradient) {
1245 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1247 var stops = gradient.getElementsByTagName("stop");
1248 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1253 att == "font-size" && (value = toInt(value, 10) + "px");
1254 var cssrule = att[rp](/(\-.)/g, function (w) {
1255 return upperCase.call(w.substring(1));
1257 node.style[cssrule] = value;
1258 // Need following line for Firefox
1259 node[setAttribute](att, value);
1264 tuneText(o, params);
1266 o.rotate(rotxy.join(S));
1268 toFloat(rot) && o.rotate(rot, true);
1272 var tuneText = function (el, params) {
1273 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1278 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1280 if (params[has]("text")) {
1281 a.text = params.text;
1282 while (node.firstChild) {
1283 node.removeChild(node.firstChild);
1285 var texts = (params.text + E)[split]("\n");
1286 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1287 var tspan = $("tspan");
1288 i && $(tspan, {dy: fontSize * leading, x: a.x});
1289 tspan[appendChild](doc.createTextNode(texts[i]));
1290 node[appendChild](tspan);
1293 var texts = node.getElementsByTagName("tspan");
1294 for (var i = 0, ii = texts[length]; i < ii; i++) {
1295 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1299 var bb = el.getBBox(),
1300 dif = a.y - (bb.y + bb.height / 2);
1301 dif && isFinite(dif) && $(node, {y: a.y + dif});
1303 var Element = function (node, svg) {
1309 node.raphael = this;
1311 this.attrs = this.attrs || {};
1312 this.transformations = []; // rotate, translate, scale
1316 rt: {deg: 0, cx: 0, cy: 0},
1320 !svg.bottom && (svg.bottom = this);
1321 this.prev = svg.top;
1322 svg.top && (svg.top.next = this);
1326 Element[proto].rotate = function (deg, cx, cy) {
1332 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1334 return this._.rt.deg;
1336 var bbox = this.getBBox();
1337 deg = (deg + E)[split](separator);
1338 if (deg[length] - 1) {
1339 cx = toFloat(deg[1]);
1340 cy = toFloat(deg[2]);
1342 deg = toFloat(deg[0]);
1344 this._.rt.deg = deg;
1346 this._.rt.deg += deg;
1348 (cy == null) && (cx = null);
1351 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1352 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1353 if (this._.rt.deg) {
1354 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1355 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1357 this.transformations[0] = E;
1358 this.clip && $(this.clip, {transform: E});
1360 $(this.node, {transform: this.transformations[join](S)});
1363 Element[proto].hide = function () {
1364 !this.removed && (this.node.style.display = "none");
1367 Element[proto].show = function () {
1368 !this.removed && (this.node.style.display = "");
1371 Element[proto].remove = function () {
1375 tear(this, this.paper);
1376 this.node.parentNode.removeChild(this.node);
1377 for (var i in this) {
1380 this.removed = true;
1382 Element[proto].getBBox = function () {
1386 if (this.type == "path") {
1387 return pathDimensions(this.attrs.path);
1389 if (this.node.style.display == "none") {
1395 bbox = this.node.getBBox();
1397 // Firefox 3.0.x plays badly here
1401 if (this.type == "text") {
1402 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1403 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1404 var bb = this.node.getExtentOfChar(i);
1405 (bb.y < bbox.y) && (bbox.y = bb.y);
1406 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1407 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1410 hide && this.hide();
1413 Element[proto].attr = function () {
1417 if (arguments[length] == 0) {
1419 for (var i in this.attrs) if (this.attrs[has](i)) {
1420 res[i] = this.attrs[i];
1422 this._.rt.deg && (res.rotation = this.rotate());
1423 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1424 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1427 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1428 if (arguments[0] == "translation") {
1429 return translate.call(this);
1431 if (arguments[0] == "rotation") {
1432 return this.rotate();
1434 if (arguments[0] == "scale") {
1435 return this.scale();
1437 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1438 return this.attrs.gradient;
1440 return this.attrs[arguments[0]];
1442 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1444 for (var j in arguments[0]) if (arguments[0][has](j)) {
1445 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1449 if (arguments[length] == 2) {
1451 params[arguments[0]] = arguments[1];
1452 setFillAndStroke(this, params);
1453 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1454 setFillAndStroke(this, arguments[0]);
1458 Element[proto].toFront = function () {
1462 this.node.parentNode[appendChild](this.node);
1463 var svg = this.paper;
1464 svg.top != this && tofront(this, svg);
1467 Element[proto].toBack = function () {
1471 if (this.node.parentNode.firstChild != this.node) {
1472 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1473 toback(this, this.paper);
1474 var svg = this.paper;
1478 Element[proto].insertAfter = function (element) {
1482 var svg = this.paper,
1483 node = element.node;
1484 if (node.nextSibling) {
1485 node.parentNode.insertBefore(this.node, node.nextSibling);
1487 node.parentNode[appendChild](this.node);
1489 insertafter(this, element, this.paper);
1492 Element[proto].insertBefore = function (element) {
1496 var node = element.node;
1497 node.parentNode.insertBefore(this.node, node);
1498 insertbefore(this, element, this.paper);
1501 // Element[proto].getTotalLength = function () {
1502 // if (this.type != "path") {
1505 // return this.node.getTotalLength();
1507 // Element[proto].getPointAtLength = function (length) {
1508 // if (this.type != "path") {
1511 // var point = this.node.getPointAtLength(length);
1512 // return {x: point.x, y: point.y};
1514 var theCircle = function (svg, x, y, r) {
1517 var el = $("circle");
1518 svg.canvas && svg.canvas[appendChild](el);
1519 var res = new Element(el, svg);
1520 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1521 res.type = "circle";
1525 var theRect = function (svg, x, y, w, h, r) {
1529 svg.canvas && svg.canvas[appendChild](el);
1530 var res = new Element(el, svg);
1531 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1536 var theEllipse = function (svg, x, y, rx, ry) {
1539 var el = $("ellipse");
1540 svg.canvas && svg.canvas[appendChild](el);
1541 var res = new Element(el, svg);
1542 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1543 res.type = "ellipse";
1547 var theImage = function (svg, src, x, y, w, h) {
1548 var el = $("image");
1549 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1550 el.setAttributeNS(svg.xlink, "href", src);
1551 svg.canvas && svg.canvas[appendChild](el);
1552 var res = new Element(el, svg);
1553 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1557 var theText = function (svg, x, y, text) {
1559 $(el, {x: x, y: y, "text-anchor": "middle"});
1560 svg.canvas && svg.canvas[appendChild](el);
1561 var res = new Element(el, svg);
1562 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1564 setFillAndStroke(res, res.attrs);
1567 var setSize = function (width, height) {
1568 this.width = width || this.width;
1569 this.height = height || this.height;
1570 this.canvas[setAttribute]("width", this.width);
1571 this.canvas[setAttribute]("height", this.height);
1574 var create = function () {
1575 var con = getContainer[apply](null, arguments),
1576 container = con && con.container,
1580 height = con.height;
1582 throw new Error("SVG container not found.");
1584 var cnvs = $("svg");
1585 width = width || 512;
1586 height = height || 342;
1588 xmlns: "http://www.w3.org/2000/svg",
1593 if (container == 1) {
1594 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1595 doc.body[appendChild](cnvs);
1597 if (container.firstChild) {
1598 container.insertBefore(cnvs, container.firstChild);
1600 container[appendChild](cnvs);
1603 container = new Paper;
1604 container.width = width;
1605 container.height = height;
1606 container.canvas = cnvs;
1607 plugins.call(container, container, R.fn);
1611 Paper[proto].clear = function () {
1612 var c = this.canvas;
1613 while (c.firstChild) {
1614 c.removeChild(c.firstChild);
1616 this.bottom = this.top = null;
1617 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1618 c[appendChild](this.desc);
1619 c[appendChild](this.defs = $("defs"));
1621 Paper[proto].remove = function () {
1622 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1623 for (var i in this) {
1624 this[i] = removed(i);
1631 var path2vml = function (path) {
1632 var total = /[ahqtv]/ig,
1633 command = pathToAbsolute;
1634 (path + E).match(total) && (command = path2curve);
1636 if (command == pathToAbsolute && !(path + E).match(total)) {
1637 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1638 bites = /([clmz]),?([^clmz]*)/gi,
1639 val = /-?[^,\s-]+/g;
1640 var res = (path + E)[rp](bites, function (all, command, args) {
1642 args[rp](val, function (value) {
1643 vals[push](round(value));
1645 return map[command] + vals;
1649 var pa = command(path), p, res = [], r;
1650 for (var i = 0, ii = pa[length]; i < ii; i++) {
1652 r = lowerCase.call(pa[i][0]);
1653 r == "z" && (r = "x");
1654 for (var j = 1, jj = p[length]; j < jj; j++) {
1655 r += round(p[j]) + (j != jj - 1 ? "," : E);
1659 return res[join](S);
1662 R[toString] = function () {
1663 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1665 var thePath = function (pathString, VML) {
1666 var g = createNode("group");
1667 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1668 g.coordsize = VML.coordsize;
1669 g.coordorigin = VML.coordorigin;
1670 var el = createNode("shape"), ol = el.style;
1671 ol.width = VML.width + "px";
1672 ol.height = VML.height + "px";
1673 el.coordsize = this.coordsize;
1674 el.coordorigin = this.coordorigin;
1676 var p = new Element(el, g, VML);
1677 p.isAbsolute = true;
1681 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1682 VML.canvas[appendChild](g);
1685 var setFillAndStroke = function (o, params) {
1686 o.attrs = o.attrs || {};
1692 for (var par in params) if (params[has](par)) {
1693 a[par] = params[par];
1695 params.href && (node.href = params.href);
1696 params.title && (node.title = params.title);
1697 params.target && (node.target = params.target);
1698 params.cursor && (s.cursor = params.cursor);
1699 if (params.path && o.type == "path") {
1700 a.path = params.path;
1701 node.path = path2vml(a.path);
1703 if (params.rotation != null) {
1704 o.rotate(params.rotation, true);
1706 if (params.translation) {
1707 xy = (params.translation + E)[split](separator);
1708 translate.call(o, xy[0], xy[1]);
1709 if (o._.rt.cx != null) {
1710 o._.rt.cx +=+ xy[0];
1711 o._.rt.cy +=+ xy[1];
1712 o.setBox(o.attrs, xy[0], xy[1]);
1716 xy = (params.scale + E)[split](separator);
1717 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1719 if ("clip-rect" in params) {
1720 var rect = (params["clip-rect"] + E)[split](separator);
1721 if (rect[length] == 4) {
1722 rect[2] = +rect[2] + (+rect[0]);
1723 rect[3] = +rect[3] + (+rect[1]);
1724 var div = node.clipRect || doc.createElement("div"),
1726 group = node.parentNode;
1727 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1728 if (!node.clipRect) {
1729 dstyle.position = "absolute";
1732 dstyle.width = o.paper.width + "px";
1733 dstyle.height = o.paper.height + "px";
1734 group.parentNode.insertBefore(div, group);
1735 div[appendChild](group);
1736 node.clipRect = div;
1739 if (!params["clip-rect"]) {
1740 node.clipRect && (node.clipRect.style.clip = E);
1743 if (o.type == "image" && params.src) {
1744 node.src = params.src;
1746 if (o.type == "image" && params.opacity) {
1747 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1748 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1750 params.font && (s.font = params.font);
1751 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1752 params["font-size"] && (s.fontSize = params["font-size"]);
1753 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1754 params["font-style"] && (s.fontStyle = params["font-style"]);
1755 if (params.opacity != null ||
1756 params["stroke-width"] != null ||
1757 params.fill != null ||
1758 params.stroke != null ||
1759 params["stroke-width"] != null ||
1760 params["stroke-opacity"] != null ||
1761 params["fill-opacity"] != null ||
1762 params["stroke-dasharray"] != null ||
1763 params["stroke-miterlimit"] != null ||
1764 params["stroke-linejoin"] != null ||
1765 params["stroke-linecap"] != null) {
1766 node = o.shape || node;
1767 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1769 !fill && (newfill = fill = createNode("fill"));
1770 if ("fill-opacity" in params || "opacity" in params) {
1771 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1772 opacity < 0 && (opacity = 0);
1773 opacity > 1 && (opacity = 1);
1774 fill.opacity = opacity;
1776 params.fill && (fill.on = true);
1777 if (fill.on == null || params.fill == "none") {
1780 if (fill.on && params.fill) {
1781 var isURL = params.fill.match(ISURL);
1783 fill.src = isURL[1];
1786 fill.color = R.getRGB(params.fill).hex;
1788 fill.type = "solid";
1789 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1791 a.gradient = params.fill;
1795 newfill && node[appendChild](fill);
1796 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1798 !stroke && (newstroke = stroke = createNode("stroke"));
1799 if ((params.stroke && params.stroke != "none") ||
1800 params["stroke-width"] ||
1801 params["stroke-opacity"] != null ||
1802 params["stroke-dasharray"] ||
1803 params["stroke-miterlimit"] ||
1804 params["stroke-linejoin"] ||
1805 params["stroke-linecap"]) {
1808 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1809 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1810 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1811 width = (toFloat(params["stroke-width"]) || 1) * .75;
1812 opacity < 0 && (opacity = 0);
1813 opacity > 1 && (opacity = 1);
1814 params["stroke-width"] == null && (width = a["stroke-width"]);
1815 params["stroke-width"] && (stroke.weight = width);
1816 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1817 stroke.opacity = opacity;
1819 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1820 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1821 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1822 if (params["stroke-dasharray"]) {
1826 "-.": "shortdashdot",
1827 "-..": "shortdashdotdot",
1832 "--.": "longdashdot",
1833 "--..": "longdashdotdot"
1835 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1837 newstroke && node[appendChild](stroke);
1839 if (res.type == "text") {
1840 var s = res.paper.span.style;
1841 a.font && (s.font = a.font);
1842 a["font-family"] && (s.fontFamily = a["font-family"]);
1843 a["font-size"] && (s.fontSize = a["font-size"]);
1844 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1845 a["font-style"] && (s.fontStyle = a["font-style"]);
1846 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1847 res.W = a.w = res.paper.span.offsetWidth;
1848 res.H = a.h = res.paper.span.offsetHeight;
1850 res.Y = a.y + round(res.H / 2);
1852 // text-anchor emulationm
1853 switch (a["text-anchor"]) {
1855 res.node.style["v-text-align"] = "left";
1856 res.bbx = round(res.W / 2);
1859 res.node.style["v-text-align"] = "right";
1860 res.bbx = -round(res.W / 2);
1863 res.node.style["v-text-align"] = "center";
1868 var addGradientFill = function (o, gradient) {
1869 o.attrs = o.attrs || {};
1870 var attrs = o.attrs,
1871 fill = o.node.getElementsByTagName("fill"),
1874 o.attrs.gradient = gradient;
1875 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1880 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1885 gradient = gradient[split](/\s*\-\s*/);
1886 if (type == "linear") {
1887 var angle = gradient.shift();
1888 angle = -toFloat(angle);
1893 var dots = parseDots(gradient);
1897 o = o.shape || o.node;
1898 fill = fill[0] || createNode("fill");
1901 fill.method = "none";
1902 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1903 fill.color = dots[0].color;
1904 fill.color2 = dots[dots[length] - 1].color;
1906 for (var i = 0, ii = dots[length]; i < ii; i++) {
1907 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1909 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1910 if (type == "radial") {
1911 fill.focus = "100%";
1912 fill.focussize = fxfy;
1913 fill.focusposition = fxfy;
1915 fill.angle = (270 - angle) % 360;
1920 var Element = function (node, group, vml) {
1928 node.raphael = this;
1941 !vml.bottom && (vml.bottom = this);
1942 this.prev = vml.top;
1943 vml.top && (vml.top.next = this);
1947 Element[proto].rotate = function (deg, cx, cy) {
1953 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1955 return this._.rt.deg;
1957 deg = (deg + E)[split](separator);
1958 if (deg[length] - 1) {
1959 cx = toFloat(deg[1]);
1960 cy = toFloat(deg[2]);
1962 deg = toFloat(deg[0]);
1964 this._.rt.deg = deg;
1966 this._.rt.deg += deg;
1968 cy == null && (cx = null);
1971 this.setBox(this.attrs, cx, cy);
1972 this.Group.style.rotation = this._.rt.deg;
1973 // gradient fix for rotation. TODO
1974 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1975 // fill = fill[0] || {};
1976 // var b = ((360 - this._.rt.deg) - 270) % 360;
1977 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1980 Element[proto].setBox = function (params, cx, cy) {
1984 var gs = this.Group.style,
1985 os = (this.shape && this.shape.style) || this.node.style;
1986 params = params || {};
1987 for (var i in params) if (params[has](i)) {
1988 this.attrs[i] = params[i];
1990 cx = cx || this._.rt.cx;
1991 cy = cy || this._.rt.cy;
1992 var attr = this.attrs,
1997 switch (this.type) {
1999 x = attr.cx - attr.r;
2000 y = attr.cy - attr.r;
2004 x = attr.cx - attr.rx;
2005 y = attr.cy - attr.ry;
2013 w = attr.width || 0;
2014 h = attr.height || 0;
2017 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2018 x = attr.x - round(this.W / 2);
2019 y = attr.y - this.H / 2;
2024 if (!this.attrs.path) {
2027 w = this.paper.width;
2028 h = this.paper.height;
2030 var dim = pathDimensions(this.attrs.path);
2040 w = this.paper.width;
2041 h = this.paper.height;
2044 cx = (cx == null) ? x + w / 2 : cx;
2045 cy = (cy == null) ? y + h / 2 : cy;
2046 var left = cx - this.paper.width / 2,
2047 top = cy - this.paper.height / 2;
2048 if (this.type == "path" || this.type == "text") {
2049 (gs.left != left + "px") && (gs.left = left + "px");
2050 (gs.top != top + "px") && (gs.top = top + "px");
2051 this.X = this.type == "text" ? x : -left;
2052 this.Y = this.type == "text" ? y : -top;
2055 (os.left != -left + "px") && (os.left = -left + "px");
2056 (os.top != -top + "px") && (os.top = -top + "px");
2058 (gs.left != left + "px") && (gs.left = left + "px");
2059 (gs.top != top + "px") && (gs.top = top + "px");
2064 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2065 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2066 (os.left != x - left + "px") && (os.left = x - left + "px");
2067 (os.top != y - top + "px") && (os.top = y - top + "px");
2068 (os.width != w + "px") && (os.width = w + "px");
2069 (os.height != h + "px") && (os.height = h + "px");
2070 var arcsize = (+params.r || 0) / mmin(w, h);
2071 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2072 // We should replace element with the new one
2073 var o = createNode("roundrect"),
2076 ii = this.events && this.events[length];
2077 o.arcsize = arcsize;
2079 this.Group[appendChild](o);
2080 this.Group.removeChild(this.node);
2081 this[0] = this.node = o;
2082 this.arcsize = arcsize;
2083 for (var i in attr) {
2088 if (this.events) for (; i < ii; i++) {
2089 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2094 Element[proto].hide = function () {
2095 !this.removed && (this.Group.style.display = "none");
2098 Element[proto].show = function () {
2099 !this.removed && (this.Group.style.display = "block");
2102 Element[proto].getBBox = function () {
2106 if (this.type == "path") {
2107 return pathDimensions(this.attrs.path);
2110 x: this.X + (this.bbx || 0),
2116 Element[proto].remove = function () {
2120 tear(this, this.paper);
2121 this.node.parentNode.removeChild(this.node);
2122 this.Group.parentNode.removeChild(this.Group);
2123 this.shape && this.shape.parentNode.removeChild(this.shape);
2124 for (var i in this) {
2127 this.removed = true;
2129 Element[proto].attr = function () {
2133 if (arguments[length] == 0) {
2135 for (var i in this.attrs) if (this.attrs[has](i)) {
2136 res[i] = this.attrs[i];
2138 this._.rt.deg && (res.rotation = this.rotate());
2139 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2140 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2143 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2144 if (arguments[0] == "translation") {
2145 return translate.call(this);
2147 if (arguments[0] == "rotation") {
2148 return this.rotate();
2150 if (arguments[0] == "scale") {
2151 return this.scale();
2153 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2154 return this.attrs.gradient;
2156 return this.attrs[arguments[0]];
2158 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2160 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2161 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2166 if (arguments[length] == 2) {
2168 params[arguments[0]] = arguments[1];
2170 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2172 if (params.text && this.type == "text") {
2173 this.node.string = params.text;
2175 setFillAndStroke(this, params);
2176 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2177 addGradientFill(this, params.gradient);
2179 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2183 Element[proto].toFront = function () {
2184 !this.removed && this.Group.parentNode[appendChild](this.Group);
2185 this.paper.top != this && tofront(this, this.paper);
2188 Element[proto].toBack = function () {
2192 if (this.Group.parentNode.firstChild != this.Group) {
2193 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2194 toback(this, this.paper);
2198 Element[proto].insertAfter = function (element) {
2202 if (element.Group.nextSibling) {
2203 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2205 element.Group.parentNode[appendChild](this.Group);
2207 insertafter(this, element, this.paper);
2210 Element[proto].insertBefore = function (element) {
2214 element.Group.parentNode.insertBefore(this.Group, element.Group);
2215 insertbefore(this, element, this.paper);
2219 var theCircle = function (vml, x, y, r) {
2220 var g = createNode("group"),
2221 o = createNode("oval"),
2223 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2224 g.coordsize = vml.coordsize;
2225 g.coordorigin = vml.coordorigin;
2227 var res = new Element(o, g, vml);
2228 res.type = "circle";
2229 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2233 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2234 vml.canvas[appendChild](g);
2237 theRect = function (vml, x, y, w, h, r) {
2238 var g = createNode("group"),
2239 o = createNode("roundrect"),
2240 arcsize = (+r || 0) / (mmin(w, h));
2241 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2242 g.coordsize = vml.coordsize;
2243 g.coordorigin = vml.coordorigin;
2245 o.arcsize = arcsize;
2246 var res = new Element(o, g, vml);
2248 setFillAndStroke(res, {stroke: "#000"});
2249 res.arcsize = arcsize;
2250 res.setBox({x: x, y: y, width: w, height: h, r: r});
2251 vml.canvas[appendChild](g);
2254 theEllipse = function (vml, x, y, rx, ry) {
2255 var g = createNode("group"),
2256 o = createNode("oval"),
2258 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2259 g.coordsize = vml.coordsize;
2260 g.coordorigin = vml.coordorigin;
2262 var res = new Element(o, g, vml);
2263 res.type = "ellipse";
2264 setFillAndStroke(res, {stroke: "#000"});
2269 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2270 vml.canvas[appendChild](g);
2273 theImage = function (vml, src, x, y, w, h) {
2274 var g = createNode("group"),
2275 o = createNode("image"),
2277 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2278 g.coordsize = vml.coordsize;
2279 g.coordorigin = vml.coordorigin;
2282 var res = new Element(o, g, vml);
2284 res.attrs.src = src;
2289 res.setBox({x: x, y: y, width: w, height: h});
2290 vml.canvas[appendChild](g);
2293 theText = function (vml, x, y, text) {
2294 var g = createNode("group"),
2295 el = createNode("shape"),
2297 path = createNode("path"),
2299 o = createNode("textpath");
2300 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2301 g.coordsize = vml.coordsize;
2302 g.coordorigin = vml.coordorigin;
2303 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2304 path.textpathok = true;
2305 ol.width = vml.width;
2306 ol.height = vml.height;
2307 o.string = text + E;
2310 el[appendChild](path);
2312 var res = new Element(o, g, vml);
2314 res.textpath = path;
2316 res.attrs.text = text;
2321 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2323 vml.canvas[appendChild](g);
2326 setSize = function (width, height) {
2327 var cs = this.canvas.style;
2328 width == +width && (width += "px");
2329 height == +height && (height += "px");
2332 cs.clip = "rect(0 " + width + " " + height + " 0)";
2336 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2338 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2339 createNode = function (tagName) {
2340 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2343 createNode = function (tagName) {
2344 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2347 var create = function () {
2348 var con = getContainer[apply](null, arguments),
2349 container = con.container,
2350 height = con.height,
2356 throw new Error("VML container not found.");
2358 var res = new Paper,
2359 c = res.canvas = doc.createElement("div"),
2361 width = width || 512;
2362 height = height || 342;
2363 width == +width && (width += "px");
2364 height == +height && (height += "px");
2367 res.coordsize = "1000 1000";
2368 res.coordorigin = "0 0";
2369 res.span = doc.createElement("span");
2370 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2371 c[appendChild](res.span);
2372 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2373 if (container == 1) {
2374 doc.body[appendChild](c);
2378 container.style.width = width;
2379 container.style.height = height;
2380 if (container.firstChild) {
2381 container.insertBefore(c, container.firstChild);
2383 container[appendChild](c);
2386 plugins.call(res, res, R.fn);
2389 Paper[proto].clear = function () {
2390 this.canvas.innerHTML = E;
2391 this.span = doc.createElement("span");
2392 this.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2393 this.canvas[appendChild](this.span);
2394 this.bottom = this.top = null;
2396 Paper[proto].remove = function () {
2397 this.canvas.parentNode.removeChild(this.canvas);
2398 for (var i in this) {
2399 this[i] = removed(i);
2405 // Safari or Chrome (WebKit) rendering bug workaround method
2406 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2407 Paper[proto].safari = function () {
2408 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2409 setTimeout(function () {rect.remove();});
2412 Paper[proto].safari = function () {};
2416 var addEvent = (function () {
2417 if (doc.addEventListener) {
2418 return function (obj, type, fn, element) {
2419 var f = function (e) {
2420 return fn.call(element, e);
2422 obj.addEventListener(type, f, false);
2423 return function () {
2424 obj.removeEventListener(type, f, false);
2428 } else if (doc.attachEvent) {
2429 return function (obj, type, fn, element) {
2430 var f = function (e) {
2431 return fn.call(element, e || win.event);
2433 obj.attachEvent("on" + type, f);
2434 var detacher = function () {
2435 obj.detachEvent("on" + type, f);
2442 for (var i = events[length]; i--;) {
2443 (function (eventName) {
2444 Element[proto][eventName] = function (fn) {
2445 if (R.is(fn, "function")) {
2446 this.events = this.events || [];
2447 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2451 Element[proto]["un" + eventName] = function (fn) {
2452 var events = this.events,
2454 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2456 events.splice(l, 1);
2457 !events.length && delete this.events;
2464 Element[proto].hover = function (f_in, f_out) {
2465 return this.mouseover(f_in).mouseout(f_out);
2467 Element[proto].unhover = function (f_in, f_out) {
2468 return this.unmouseover(f_in).unmouseout(f_out);
2470 Paper[proto].circle = function (x, y, r) {
2471 return theCircle(this, x || 0, y || 0, r || 0);
2473 Paper[proto].rect = function (x, y, w, h, r) {
2474 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2476 Paper[proto].ellipse = function (x, y, rx, ry) {
2477 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2479 Paper[proto].path = function (pathString) {
2480 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2481 return thePath(R.format[apply](R, arguments), this);
2483 Paper[proto].image = function (src, x, y, w, h) {
2484 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2486 Paper[proto].text = function (x, y, text) {
2487 return theText(this, x || 0, y || 0, text || E);
2489 Paper[proto].set = function (itemsArray) {
2490 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2491 return new Set(itemsArray);
2493 Paper[proto].setSize = setSize;
2494 Paper[proto].top = Paper[proto].bottom = null;
2495 Paper[proto].raphael = R;
2497 return this.x + S + this.y;
2499 Element[proto].scale = function (x, y, cx, cy) {
2500 if (x == null && y == null) {
2515 var bb = this.getBBox(),
2516 rcx = bb.x + bb.width / 2,
2517 rcy = bb.y + bb.height / 2,
2520 cx = (+cx || cx == 0) ? cx : rcx;
2521 cy = (+cy || cy == 0) ? cy : rcy;
2522 var dirx = ~~(x / math.abs(x)),
2523 diry = ~~(y / math.abs(y)),
2524 s = this.node.style,
2525 ncx = cx + (rcx - cx) * kx,
2526 ncy = cy + (rcy - cy) * ky;
2527 switch (this.type) {
2530 var neww = a.width * dirx * kx,
2531 hewh = a.height * diry * ky;
2534 r: a.r * mmin(dirx * kx, diry * ky),
2543 rx: a.rx * dirx * kx,
2544 ry: a.ry * diry * ky,
2545 r: a.r * mmin(dirx * kx, diry * ky),
2551 var path = pathToRelative(a.path),
2553 for (var i = 0, ii = path[length]; i < ii; i++) {
2556 P0 = upperCase.call(p[0]);
2557 if (P0 == "M" && skip) {
2563 p[path[i][length] - 2] *= kx;
2564 p[path[i][length] - 1] *= ky;
2567 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2568 } else if (P0 == "H") {
2569 for (j = 1, jj = p[length]; j < jj; j++) {
2572 } else if (P0 == "V") {
2573 for (j = 1, jj = p[length]; j < jj; j++) {
2577 for (j = 1, jj = p[length]; j < jj; j++) {
2578 p[j] *= (j % 2) ? kx : ky;
2582 var dim2 = pathDimensions(path),
2583 dx = ncx - dim2.x - dim2.width / 2,
2584 dy = ncy - dim2.y - dim2.height / 2;
2587 this.attr({path: path});
2590 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2591 if (this.transformations) {
2592 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2593 this.node[setAttribute]("transform", this.transformations[join](S));
2594 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2595 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2596 this.attr({x: dx, y: dy});
2600 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2601 ", M12=0, M21=0, M22=", diry,
2602 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2603 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2606 if (this.transformations) {
2607 this.transformations[2] = E;
2608 this.node[setAttribute]("transform", this.transformations[join](S));
2612 this.node.filterMatrix = E;
2613 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2616 a.scale = [x, y, cx, cy][join](S);
2622 Element[proto].clone = function () {
2623 var attr = this.attr();
2625 delete attr.translation;
2626 return this.paper[this.type]().attr(attr);
2628 var getLengthFactory = function (istotal) {
2629 return function (length) {
2630 if (this.type != "path") {
2633 var path = path2curve(this.attrs.path), x, y, p, l,
2635 for (var i = 0, ii = path.length; i < ii; i++) {
2641 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2642 if (!istotal && len + l > length) {
2643 return R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2650 return istotal ? len : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2653 segmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2654 var old = {x: 0, y: 0},
2656 for (var i = 0; i < 1.01; i+=.01) {
2657 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2658 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
2663 Element[proto].getTotalLength = getLengthFactory(1);
2664 Element[proto].getPointAtLength = getLengthFactory();
2666 // animation easing formulas
2667 R.easing_formulas = {
2668 linear: function (n) {
2675 return pow(n - 1, 3) + 1;
2677 "<>": function (n) {
2680 return pow(n, 3) / 2;
2683 return (pow(n, 3) + 2) / 2;
2685 backIn: function (n) {
2687 return n * n * ((s + 1) * n - s);
2689 backOut: function (n) {
2692 return n * n * ((s + 1) * n + s) + 1;
2694 elastic: function (n) {
2695 if (n == 0 || n == 1) {
2700 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2702 bounce: function (n) {
2711 l = s * n * n + .75;
2713 if (n < (2.5 / p)) {
2715 l = s * n * n + .9375;
2718 l = s * n * n + .984375;
2726 var animationElements = {length : 0},
2727 animation = function () {
2728 var Now = +new Date;
2729 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2730 var e = animationElements[l];
2732 delete animationElements[l];
2733 animationElements[length]--;
2736 var time = Now - e.start,
2745 callback = e.callback,
2749 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2750 for (var attr in from) if (from[has](attr)) {
2751 switch (availableAnimAttrs[attr]) {
2753 now = +from[attr] + pos * ms * diff[attr];
2757 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2758 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2759 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2764 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2765 now[i] = [from[attr][i][0]];
2766 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2767 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2769 now[i] = now[i][join](S);
2776 var x = diff[attr][0] * (time - prev),
2777 y = diff[attr][1] * (time - prev);
2783 now = +from[attr][0] + pos * ms * diff[attr][0];
2784 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2787 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);
2793 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2802 that._run && that._run.call(that);
2804 (t.x || t.y) && that.translate(-t.x, -t.y);
2805 to.scale && (to.scale = to.scale + E);
2807 delete animationElements[l];
2808 animationElements[length]--;
2809 that.in_animation = null;
2810 R.is(callback, "function") && callback.call(that);
2814 R.svg && that.paper.safari();
2815 animationElements[length] && setTimeout(animation);
2817 upto255 = function (color) {
2818 return color > 255 ? 255 : (color < 0 ? 0 : color);
2820 translate = function (x, y) {
2822 return {x: this._.tx, y: this._.ty, toString: x_y};
2826 switch (this.type) {
2829 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2834 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2837 var path = pathToRelative(this.attrs.path);
2840 this.attr({path: path});
2845 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2846 animationElements[element.id] && (params.start = animationElements[element.id].start);
2847 return this.animate(params, ms, easing, callback);
2849 Element[proto].onAnimation = function (f) {
2850 this._run = f || null;
2853 Element[proto].animate = function (params, ms, easing, callback) {
2854 if (R.is(easing, "function") || !easing) {
2855 callback = easing || null;
2860 for (var attr in params) if (params[has](attr)) {
2861 if (availableAnimAttrs[has](attr)) {
2862 from[attr] = this.attr(attr);
2863 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2864 to[attr] = params[attr];
2865 switch (availableAnimAttrs[attr]) {
2867 diff[attr] = (to[attr] - from[attr]) / ms;
2870 from[attr] = R.getRGB(from[attr]);
2871 var toColour = R.getRGB(to[attr]);
2873 r: (toColour.r - from[attr].r) / ms,
2874 g: (toColour.g - from[attr].g) / ms,
2875 b: (toColour.b - from[attr].b) / ms
2879 var pathes = path2curve(from[attr], to[attr]);
2880 from[attr] = pathes[0];
2881 var toPath = pathes[1];
2883 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2884 diff[attr][i] = [0];
2885 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2886 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2891 var values = (params[attr] + E)[split](separator),
2892 from2 = (from[attr] + E)[split](separator);
2895 from[attr] = [0, 0];
2896 diff[attr] = [values[0] / ms, values[1] / ms];
2899 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2900 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2903 params[attr] = values;
2904 from[attr] = (from[attr] + E)[split](separator);
2905 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2908 from[attr] = (from[attr] + E)[split](separator);
2912 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2921 this.in_animation = 1;
2922 animationElements[this.id] = {
2923 start: params.start || +new Date,
2933 ++animationElements[length] == 1 && animation();
2936 Element[proto].stop = function () {
2937 animationElements[this.id] && animationElements[length]--;
2938 delete animationElements[this.id];
2941 Element[proto].translate = function (x, y) {
2942 return this.attr({translation: x + " " + y});
2944 Element[proto][toString] = function () {
2945 return "Rapha\xebl\u2019s object";
2947 R.ae = animationElements;
2950 var Set = function (items) {
2954 for (var i = 0, ii = items[length]; i < ii; i++) {
2955 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2956 this[this.items[length]] = this.items[this.items[length]] = items[i];
2962 Set[proto][push] = function () {
2965 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2966 item = arguments[i];
2967 if (item && (item.constructor == Element || item.constructor == Set)) {
2968 len = this.items[length];
2969 this[len] = this.items[len] = item;
2975 Set[proto].pop = function () {
2976 delete this[this[length]--];
2977 return this.items.pop();
2979 for (var method in Element[proto]) if (Element[proto][has](method)) {
2980 Set[proto][method] = (function (methodname) {
2981 return function () {
2982 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2983 this.items[i][methodname][apply](this.items[i], arguments);
2989 Set[proto].attr = function (name, value) {
2990 if (name && R.is(name, "array") && R.is(name[0], "object")) {
2991 for (var j = 0, jj = name[length]; j < jj; j++) {
2992 this.items[j].attr(name[j]);
2995 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2996 this.items[i].attr[apply](this.items[i], arguments);
3001 Set[proto].animate = function (params, ms, easing, callback) {
3002 (R.is(easing, "function") || !easing) && (callback = easing || null);
3003 var len = this.items[length],
3007 callback && (collector = function () {
3008 !--len && callback.call(set);
3010 this.items[--i].animate(params, ms, easing || collector, collector);
3012 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3016 Set[proto].insertAfter = function (el) {
3017 var i = this.items[length];
3019 this.items[i].insertAfter(el);
3023 Set[proto].getBBox = function () {
3028 for (var i = this.items[length]; i--;) {
3029 var box = this.items[i].getBBox();
3032 w[push](box.x + box.width);
3033 h[push](box.y + box.height);
3035 x = mmin[apply](0, x);
3036 y = mmin[apply](0, y);
3040 width: mmax[apply](0, w) - x,
3041 height: mmax[apply](0, h) - y
3045 R.registerFont = function (font) {
3049 this.fonts = this.fonts || {};
3055 family = font.face["font-family"];
3056 for (var prop in font.face) if (font.face[has](prop)) {
3057 fontcopy.face[prop] = font.face[prop];
3059 if (this.fonts[family]) {
3060 this.fonts[family][push](fontcopy);
3062 this.fonts[family] = [fontcopy];
3065 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3066 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3067 var path = font.glyphs[glyph];
3068 fontcopy.glyphs[glyph] = {
3071 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3072 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3076 for (var k in path.k) if (path[has](k)) {
3077 fontcopy.glyphs[glyph].k[k] = path.k[k];
3084 Paper[proto].getFont = function (family, weight, style, stretch) {
3085 stretch = stretch || "normal";
3086 style = style || "normal";
3087 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3088 var font = R.fonts[family];
3090 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3091 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3092 if (name.test(fontName)) {
3093 font = R.fonts[fontName];
3100 for (var i = 0, ii = font[length]; i < ii; i++) {
3102 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3109 Paper[proto].print = function (x, y, string, font, size, origin) {
3110 origin = origin || "middle"; // baseline|middle
3111 var out = this.set(),
3112 letters = (string + E)[split](E),
3116 R.is(font, "string") && (font = this.getFont(font));
3118 scale = (size || 16) / font.face["units-per-em"];
3119 var bb = font.face.bbox.split(separator),
3121 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3122 for (var i = 0, ii = letters[length]; i < ii; i++) {
3123 var prev = i && font.glyphs[letters[i - 1]] || {},
3124 curr = font.glyphs[letters[i]];
3125 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3126 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3128 out.scale(scale, scale, top, height).translate(x - top, y - height);
3133 R.format = function (token) {
3134 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3136 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3137 return args[++i] == null ? E : args[i];
3141 R.ninja = function () {
3142 var r = win.Raphael, u;
3143 if (oldRaphael.was) {
3144 win.Raphael = oldRaphael.is;
3154 R.el = Element[proto];