2 * Raphael 1.3.1 - 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 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 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},
62 availableAnimAttrs = {along: "along", "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},
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);
341 (mx > nx || my < ny) && (alpha += 180);
342 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};
344 var pathDimensions = cacher(function (path) {
346 return {x: 0, y: 0, width: 0, height: 0};
348 path = path2curve(path);
354 for (var i = 0, ii = path[length]; i < ii; i++) {
362 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
363 X = X[concat](dim.min.x, dim.max.x);
364 Y = Y[concat](dim.min.y, dim.max.y);
369 var xmin = mmin[apply](0, X),
370 ymin = mmin[apply](0, Y);
374 width: mmax[apply](0, X) - xmin,
375 height: mmax[apply](0, Y) - ymin
378 pathClone = function (pathArray) {
380 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
381 pathArray = R.parsePathString(pathArray);
383 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
385 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
386 res[i][j] = pathArray[i][j];
389 res[toString] = R._path2string;
392 pathToRelative = cacher(function (pathArray) {
393 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
394 pathArray = R.parsePathString(pathArray);
402 if (pathArray[0][0] == "M") {
408 res[push](["M", x, y]);
410 for (var i = start, ii = pathArray[length]; i < ii; i++) {
413 if (pa[0] != lowerCase.call(pa[0])) {
414 r[0] = lowerCase.call(pa[0]);
422 r[6] = +(pa[6] - x).toFixed(3);
423 r[7] = +(pa[7] - y).toFixed(3);
426 r[1] = +(pa[1] - y).toFixed(3);
432 for (var j = 1, jj = pa[length]; j < jj; j++) {
433 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
442 for (var k = 0, kk = pa[length]; k < kk; k++) {
446 var len = res[i][length];
453 x += +res[i][len - 1];
456 y += +res[i][len - 1];
459 x += +res[i][len - 2];
460 y += +res[i][len - 1];
463 res[toString] = R._path2string;
466 pathToAbsolute = cacher(function (pathArray) {
467 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
468 pathArray = R.parsePathString(pathArray);
476 if (pathArray[0][0] == "M") {
477 x = +pathArray[0][1];
478 y = +pathArray[0][2];
482 res[0] = ["M", x, y];
484 for (var i = start, ii = pathArray[length]; i < ii; i++) {
487 if (pa[0] != upperCase.call(pa[0])) {
488 r[0] = upperCase.call(pa[0]);
509 for (var j = 1, jj = pa[length]; j < jj; j++) {
510 r[j] = +pa[j] + ((j % 2) ? x : y);
514 for (var k = 0, kk = pa[length]; k < kk; k++) {
530 x = res[i][res[i][length] - 2];
531 y = res[i][res[i][length] - 1];
534 res[toString] = R._path2string;
537 l2c = function (x1, y1, x2, y2) {
538 return [x1, y1, x2, y2, x2, y2];
540 q2c = function (x1, y1, ax, ay, x2, y2) {
552 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
553 // for more information of where this math came from visit:
554 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
556 _120 = PI * 120 / 180,
557 rad = PI / 180 * (+angle || 0),
560 rotate = cacher(function (x, y, rad) {
561 var X = x * math.cos(rad) - y * math.sin(rad),
562 Y = x * math.sin(rad) + y * math.cos(rad);
566 xy = rotate(x1, y1, -rad);
569 xy = rotate(x2, y2, -rad);
572 var cos = math.cos(PI / 180 * angle),
573 sin = math.sin(PI / 180 * angle),
576 rx = mmax(rx, math.abs(x));
577 ry = mmax(ry, math.abs(y));
578 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
580 rx = math.sqrt(h) * rx;
581 ry = math.sqrt(h) * ry;
585 k = (large_arc_flag == sweep_flag ? -1 : 1) *
586 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
587 cx = k * rx * y / ry + (x1 + x2) / 2,
588 cy = k * -ry * x / rx + (y1 + y2) / 2,
589 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
590 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
592 f1 = x1 < cx ? PI - f1 : f1;
593 f2 = x2 < cx ? PI - f2 : f2;
594 f1 < 0 && (f1 = PI * 2 + f1);
595 f2 < 0 && (f2 = PI * 2 + f2);
596 if (sweep_flag && f1 > f2) {
599 if (!sweep_flag && f2 > f1) {
609 if (math.abs(df) > _120) {
613 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
614 x2 = cx + rx * math.cos(f2);
615 y2 = cy + ry * math.sin(f2);
616 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
619 var c1 = math.cos(f1),
623 t = math.tan(df / 4),
627 m2 = [x1 + hx * s1, y1 - hy * c1],
628 m3 = [x2 + hx * s2, y2 - hy * c2],
630 m2[0] = 2 * m1[0] - m2[0];
631 m2[1] = 2 * m1[1] - m2[1];
633 return [m2, m3, m4][concat](res);
635 res = [m2, m3, m4][concat](res)[join]()[split](",");
637 for (var i = 0, ii = res[length]; i < ii; i++) {
638 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
643 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
646 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
647 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
650 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
651 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
652 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
654 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
655 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
659 math.abs(t1) > 1e12 && (t1 = .5);
660 math.abs(t2) > 1e12 && (t2 = .5);
661 if (t1 > 0 && t1 < 1) {
662 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
666 if (t2 > 0 && t2 < 1) {
667 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
671 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
672 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
674 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
675 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
676 math.abs(t1) > 1e12 && (t1 = .5);
677 math.abs(t2) > 1e12 && (t2 = .5);
678 if (t1 > 0 && t1 < 1) {
679 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
683 if (t2 > 0 && t2 < 1) {
684 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
689 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
690 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
693 path2curve = cacher(function (path, path2) {
694 var p = pathToAbsolute(path),
695 p2 = path2 && pathToAbsolute(path2),
696 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
697 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
698 processPath = function (path, d) {
701 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
703 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
710 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
713 nx = d.x + (d.x - (d.bx || d.x));
714 ny = d.y + (d.y - (d.by || d.y));
715 path = ["C", nx, ny][concat](path.slice(1));
718 d.qx = d.x + (d.x - (d.qx || d.x));
719 d.qy = d.y + (d.y - (d.qy || d.y));
720 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
725 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
728 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
731 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
734 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
737 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
742 fixArc = function (pp, i) {
743 if (pp[i][length] > 7) {
747 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
750 ii = mmax(p[length], p2 && p2[length] || 0);
753 fixM = function (path1, path2, a1, a2, i) {
754 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
755 path2.splice(i, 0, ["M", a2.x, a2.y]);
760 ii = mmax(p[length], p2 && p2[length] || 0);
763 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
764 p[i] = processPath(p[i], attrs);
766 p2 && (p2[i] = processPath(p2[i], attrs2));
768 fixM(p, p2, attrs, attrs2, i);
769 fixM(p2, p, attrs2, attrs, i);
772 seglen = seg[length],
773 seg2len = p2 && seg2[length];
774 attrs.x = seg[seglen - 2];
775 attrs.y = seg[seglen - 1];
776 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
777 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
778 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
779 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
780 attrs2.x = p2 && seg2[seg2len - 2];
781 attrs2.y = p2 && seg2[seg2len - 1];
783 return p2 ? [p, p2] : p;
785 parseDots = cacher(function (gradient) {
787 for (var i = 0, ii = gradient[length]; i < ii; i++) {
789 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
790 dot.color = R.getRGB(par[1]);
791 if (dot.color.error) {
794 dot.color = dot.color.hex;
795 par[2] && (dot.offset = par[2] + "%");
798 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
799 if (!dots[i].offset) {
800 var start = toFloat(dots[i - 1].offset || 0),
802 for (var j = i + 1; j < ii; j++) {
803 if (dots[j].offset) {
804 end = dots[j].offset;
813 var d = (end - start) / (j - i + 1);
816 dots[i].offset = start + "%";
822 getContainer = function () {
828 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
829 if (R.is(arguments[0], "string")) {
830 container = doc.getElementById(arguments[0]);
832 container = arguments[0];
834 if (container.tagName) {
835 if (arguments[1] == null) {
837 container: container,
838 width: container.style.pixelWidth || container.offsetWidth,
839 height: container.style.pixelHeight || container.offsetHeight
842 return {container: container, width: arguments[1], height: arguments[2]};
845 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
846 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
849 plugins = function (con, add) {
851 for (var prop in add) if (add[has](prop) && !(prop in con)) {
852 switch (typeof add[prop]) {
855 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
859 con[prop] = con[prop] || {};
860 plugins.call(this, con[prop], add[prop]);
863 con[prop] = add[prop];
868 tear = function (el, paper) {
869 el == paper.top && (paper.top = el.prev);
870 el == paper.bottom && (paper.bottom = el.next);
871 el.next && (el.next.prev = el.prev);
872 el.prev && (el.prev.next = el.next);
874 tofront = function (el, paper) {
875 if (paper.top === el) {
884 toback = function (el, paper) {
885 if (paper.bottom === el) {
889 el.next = paper.bottom;
891 paper.bottom.prev = el;
894 insertafter = function (el, el2, paper) {
896 el2 == paper.top && (paper.top = el);
897 el2.next && (el2.next.prev = el);
902 insertbefore = function (el, el2, paper) {
904 el2 == paper.bottom && (paper.bottom = el);
905 el2.prev && (el2.prev.next = el);
910 removed = function (methodname) {
912 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
915 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
919 Paper[proto].svgns = "http://www.w3.org/2000/svg";
920 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
921 var round = function (num) {
922 return +num + (~~num === num) * .5;
924 roundPath = function (path) {
925 for (var i = 0, ii = path[length]; i < ii; i++) {
926 if (lowerCase.call(path[i][0]) != "a") {
927 for (var j = 1, jj = path[i][length]; j < jj; j++) {
928 path[i][j] = round(path[i][j]);
931 path[i][6] = round(path[i][6]);
932 path[i][7] = round(path[i][7]);
937 $ = function (el, attr) {
939 for (var key in attr) if (attr[has](key)) {
940 el[setAttribute](key, attr[key]);
943 return doc.createElementNS(Paper[proto].svgns, el);
946 R[toString] = function () {
947 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
949 var thePath = function (pathString, SVG) {
951 SVG.canvas && SVG.canvas[appendChild](el);
952 var p = new Element(el, SVG);
954 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
957 var addGradientFill = function (o, gradient, SVG) {
961 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
966 var dir = ((fy > .5) * 2 - 1);
967 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
968 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
970 (fy = fy.toFixed(5) - 1e-5 * dir);
974 gradient = gradient[split](/\s*\-\s*/);
975 if (type == "linear") {
976 var angle = gradient.shift();
977 angle = -toFloat(angle);
981 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
982 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
986 vector[0] = -vector[2];
990 vector[1] = -vector[3];
994 var dots = parseDots(gradient);
998 var el = $(type + "Gradient");
999 el.id = "r" + (R._id++)[toString](36);
1000 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1001 SVG.defs[appendChild](el);
1002 for (var i = 0, ii = dots[length]; i < ii; i++) {
1003 var stop = $("stop");
1005 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1006 "stop-color": dots[i].color || "#fff"
1008 el[appendChild](stop);
1011 fill: "url(#" + el.id + ")",
1020 var updatePosition = function (o) {
1021 var bbox = o.getBBox();
1022 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1024 var setFillAndStroke = function (o, params) {
1031 "-..": [3, 1, 1, 1, 1, 1],
1035 "- .": [4, 3, 1, 3],
1036 "--.": [8, 3, 1, 3],
1037 "--..": [8, 3, 1, 3, 1, 3]
1042 addDashes = function (o, value) {
1043 value = dasharray[lowerCase.call(value)];
1045 var width = o.attrs["stroke-width"] || "1",
1046 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1048 var i = value[length];
1050 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1052 $(node, {"stroke-dasharray": dashes[join](",")});
1055 params[has]("rotation") && (rot = params.rotation);
1056 var rotxy = (rot + E)[split](separator);
1057 if (!(rotxy.length - 1)) {
1060 rotxy[1] = +rotxy[1];
1061 rotxy[2] = +rotxy[2];
1063 toFloat(rot) && o.rotate(0, true);
1064 for (var att in params) if (params[has](att)) {
1065 if (!availableAttrs[has](att)) {
1068 var value = params[att];
1072 o.rotate(value, true);
1078 var pn = node.parentNode;
1079 if (lowerCase.call(pn.tagName) != "a") {
1081 pn.insertBefore(hl, node);
1082 hl[appendChild](node);
1085 pn.setAttributeNS(o.paper.xlink, att, value);
1088 node.style.cursor = value;
1091 var rect = (value + E)[split](separator);
1092 if (rect[length] == 4) {
1093 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1094 var el = $("clipPath"),
1096 el.id = "r" + (R._id++)[toString](36);
1103 el[appendChild](rc);
1104 o.paper.defs[appendChild](el);
1105 $(node, {"clip-path": "url(#" + el.id + ")"});
1109 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1110 clip && clip.parentNode.removeChild(clip);
1111 $(node, {"clip-path": E});
1116 if (value && o.type == "path") {
1117 attrs.path = roundPath(pathToAbsolute(value));
1118 $(node, {d: attrs.path});
1122 node[setAttribute](att, value);
1131 value = -attrs.x - (attrs.width || 0);
1134 if (att == "rx" && o.type == "rect") {
1138 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1139 node[setAttribute](att, round(value));
1140 o.pattern && updatePosition(o);
1143 node[setAttribute](att, value);
1152 value = -attrs.y - (attrs.height || 0);
1155 if (att == "ry" && o.type == "rect") {
1159 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1160 node[setAttribute](att, round(value));
1161 o.pattern && updatePosition(o);
1164 if (o.type == "rect") {
1165 $(node, {rx: value, ry: value});
1167 node[setAttribute](att, value);
1171 if (o.type == "image") {
1172 node.setAttributeNS(o.paper.xlink, "href", value);
1175 case "stroke-width":
1176 node.style.strokeWidth = value;
1177 // Need following line for Firefox
1178 node[setAttribute](att, value);
1179 if (attrs["stroke-dasharray"]) {
1180 addDashes(o, attrs["stroke-dasharray"]);
1183 case "stroke-dasharray":
1184 addDashes(o, value);
1187 var xy = (value + E)[split](separator);
1188 xy[0] = +xy[0] || 0;
1189 xy[1] = +xy[1] || 0;
1194 translate.call(o, xy[0], xy[1]);
1197 var xy = (value + E)[split](separator);
1198 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1201 var isURL = (value + E).match(ISURL);
1203 var el = $("pattern"),
1205 el.id = "r" + (R._id++)[toString](36);
1206 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1207 $(ig, {x: 0, y: 0});
1208 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1209 el[appendChild](ig);
1211 var img = doc.createElement("img");
1212 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1213 img.onload = function () {
1214 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1215 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1216 doc.body.removeChild(this);
1219 doc.body[appendChild](img);
1221 o.paper.defs[appendChild](el);
1222 node.style.fill = "url(#" + el.id + ")";
1223 $(node, {fill: "url(#" + el.id + ")"});
1225 o.pattern && updatePosition(o);
1228 if (!R.getRGB(value).error) {
1229 delete params.gradient;
1230 delete attrs.gradient;
1231 !R.is(attrs.opacity, "undefined") &&
1232 R.is(params.opacity, "undefined") &&
1233 $(node, {opacity: attrs.opacity});
1234 !R.is(attrs["fill-opacity"], "undefined") &&
1235 R.is(params["fill-opacity"], "undefined") &&
1236 $(node, {"fill-opacity": attrs["fill-opacity"]});
1237 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1238 attrs.gradient = value;
1239 attrs.fill = "none";
1243 node[setAttribute](att, R.getRGB(value).hex);
1246 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1249 case "fill-opacity":
1250 if (attrs.gradient) {
1251 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1253 var stops = gradient.getElementsByTagName("stop");
1254 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1259 att == "font-size" && (value = toInt(value, 10) + "px");
1260 var cssrule = att[rp](/(\-.)/g, function (w) {
1261 return upperCase.call(w.substring(1));
1263 node.style[cssrule] = value;
1264 // Need following line for Firefox
1265 node[setAttribute](att, value);
1270 tuneText(o, params);
1272 o.rotate(rotxy.join(S));
1274 toFloat(rot) && o.rotate(rot, true);
1278 var tuneText = function (el, params) {
1279 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1284 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1286 if (params[has]("text")) {
1287 a.text = params.text;
1288 while (node.firstChild) {
1289 node.removeChild(node.firstChild);
1291 var texts = (params.text + E)[split]("\n");
1292 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1293 var tspan = $("tspan");
1294 i && $(tspan, {dy: fontSize * leading, x: a.x});
1295 tspan[appendChild](doc.createTextNode(texts[i]));
1296 node[appendChild](tspan);
1299 var texts = node.getElementsByTagName("tspan");
1300 for (var i = 0, ii = texts[length]; i < ii; i++) {
1301 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1305 var bb = el.getBBox(),
1306 dif = a.y - (bb.y + bb.height / 2);
1307 dif && isFinite(dif) && $(node, {y: a.y + dif});
1309 var Element = function (node, svg) {
1315 node.raphael = this;
1317 this.attrs = this.attrs || {};
1318 this.transformations = []; // rotate, translate, scale
1322 rt: {deg: 0, cx: 0, cy: 0},
1326 !svg.bottom && (svg.bottom = this);
1327 this.prev = svg.top;
1328 svg.top && (svg.top.next = this);
1332 Element[proto].rotate = function (deg, cx, cy) {
1338 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1340 return this._.rt.deg;
1342 var bbox = this.getBBox();
1343 deg = (deg + E)[split](separator);
1344 if (deg[length] - 1) {
1345 cx = toFloat(deg[1]);
1346 cy = toFloat(deg[2]);
1348 deg = toFloat(deg[0]);
1350 this._.rt.deg = deg;
1352 this._.rt.deg += deg;
1354 (cy == null) && (cx = null);
1357 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1358 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1359 if (this._.rt.deg) {
1360 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1361 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1363 this.transformations[0] = E;
1364 this.clip && $(this.clip, {transform: E});
1366 $(this.node, {transform: this.transformations[join](S)});
1369 Element[proto].hide = function () {
1370 !this.removed && (this.node.style.display = "none");
1373 Element[proto].show = function () {
1374 !this.removed && (this.node.style.display = "");
1377 Element[proto].remove = function () {
1381 tear(this, this.paper);
1382 this.node.parentNode.removeChild(this.node);
1383 for (var i in this) {
1386 this.removed = true;
1388 Element[proto].getBBox = function () {
1392 if (this.type == "path") {
1393 return pathDimensions(this.attrs.path);
1395 if (this.node.style.display == "none") {
1401 bbox = this.node.getBBox();
1403 // Firefox 3.0.x plays badly here
1407 if (this.type == "text") {
1408 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1409 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1410 var bb = this.node.getExtentOfChar(i);
1411 (bb.y < bbox.y) && (bbox.y = bb.y);
1412 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1413 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1416 hide && this.hide();
1419 Element[proto].attr = function () {
1423 if (arguments[length] == 0) {
1425 for (var i in this.attrs) if (this.attrs[has](i)) {
1426 res[i] = this.attrs[i];
1428 this._.rt.deg && (res.rotation = this.rotate());
1429 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1430 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1433 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1434 if (arguments[0] == "translation") {
1435 return translate.call(this);
1437 if (arguments[0] == "rotation") {
1438 return this.rotate();
1440 if (arguments[0] == "scale") {
1441 return this.scale();
1443 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1444 return this.attrs.gradient;
1446 return this.attrs[arguments[0]];
1448 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1450 for (var j in arguments[0]) if (arguments[0][has](j)) {
1451 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1455 if (arguments[length] == 2) {
1457 params[arguments[0]] = arguments[1];
1458 setFillAndStroke(this, params);
1459 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1460 setFillAndStroke(this, arguments[0]);
1464 Element[proto].toFront = function () {
1468 this.node.parentNode[appendChild](this.node);
1469 var svg = this.paper;
1470 svg.top != this && tofront(this, svg);
1473 Element[proto].toBack = function () {
1477 if (this.node.parentNode.firstChild != this.node) {
1478 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1479 toback(this, this.paper);
1480 var svg = this.paper;
1484 Element[proto].insertAfter = function (element) {
1488 var node = element.node;
1489 if (node.nextSibling) {
1490 node.parentNode.insertBefore(this.node, node.nextSibling);
1492 node.parentNode[appendChild](this.node);
1494 insertafter(this, element, this.paper);
1497 Element[proto].insertBefore = function (element) {
1501 var node = element.node;
1502 node.parentNode.insertBefore(this.node, node);
1503 insertbefore(this, element, this.paper);
1506 var theCircle = function (svg, x, y, r) {
1509 var el = $("circle");
1510 svg.canvas && svg.canvas[appendChild](el);
1511 var res = new Element(el, svg);
1512 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1513 res.type = "circle";
1517 var theRect = function (svg, x, y, w, h, r) {
1521 svg.canvas && svg.canvas[appendChild](el);
1522 var res = new Element(el, svg);
1523 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1528 var theEllipse = function (svg, x, y, rx, ry) {
1531 var el = $("ellipse");
1532 svg.canvas && svg.canvas[appendChild](el);
1533 var res = new Element(el, svg);
1534 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1535 res.type = "ellipse";
1539 var theImage = function (svg, src, x, y, w, h) {
1540 var el = $("image");
1541 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1542 el.setAttributeNS(svg.xlink, "href", src);
1543 svg.canvas && svg.canvas[appendChild](el);
1544 var res = new Element(el, svg);
1545 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1549 var theText = function (svg, x, y, text) {
1551 $(el, {x: x, y: y, "text-anchor": "middle"});
1552 svg.canvas && svg.canvas[appendChild](el);
1553 var res = new Element(el, svg);
1554 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1556 setFillAndStroke(res, res.attrs);
1559 var setSize = function (width, height) {
1560 this.width = width || this.width;
1561 this.height = height || this.height;
1562 this.canvas[setAttribute]("width", this.width);
1563 this.canvas[setAttribute]("height", this.height);
1566 var create = function () {
1567 var con = getContainer[apply](null, arguments),
1568 container = con && con.container,
1572 height = con.height;
1574 throw new Error("SVG container not found.");
1576 var cnvs = $("svg");
1577 width = width || 512;
1578 height = height || 342;
1580 xmlns: "http://www.w3.org/2000/svg",
1585 if (container == 1) {
1586 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1587 doc.body[appendChild](cnvs);
1589 if (container.firstChild) {
1590 container.insertBefore(cnvs, container.firstChild);
1592 container[appendChild](cnvs);
1595 container = new Paper;
1596 container.width = width;
1597 container.height = height;
1598 container.canvas = cnvs;
1599 plugins.call(container, container, R.fn);
1603 Paper[proto].clear = function () {
1604 var c = this.canvas;
1605 while (c.firstChild) {
1606 c.removeChild(c.firstChild);
1608 this.bottom = this.top = null;
1609 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1610 c[appendChild](this.desc);
1611 c[appendChild](this.defs = $("defs"));
1613 Paper[proto].remove = function () {
1614 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1615 for (var i in this) {
1616 this[i] = removed(i);
1623 var path2vml = function (path) {
1624 var total = /[ahqstv]/ig,
1625 command = pathToAbsolute;
1626 (path + E).match(total) && (command = path2curve);
1628 if (command == pathToAbsolute && !(path + E).match(total)) {
1629 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1630 bites = /([clmz]),?([^clmz]*)/gi,
1631 val = /-?[^,\s-]+/g;
1632 var res = (path + E)[rp](bites, function (all, command, args) {
1634 args[rp](val, function (value) {
1635 vals[push](round(value));
1637 return map[command] + vals;
1641 var pa = command(path), p, res = [], r;
1642 for (var i = 0, ii = pa[length]; i < ii; i++) {
1644 r = lowerCase.call(pa[i][0]);
1645 r == "z" && (r = "x");
1646 for (var j = 1, jj = p[length]; j < jj; j++) {
1647 r += round(p[j]) + (j != jj - 1 ? "," : E);
1651 return res[join](S);
1654 R[toString] = function () {
1655 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1657 var thePath = function (pathString, VML) {
1658 var g = createNode("group");
1659 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1660 g.coordsize = VML.coordsize;
1661 g.coordorigin = VML.coordorigin;
1662 var el = createNode("shape"), ol = el.style;
1663 ol.width = VML.width + "px";
1664 ol.height = VML.height + "px";
1665 el.coordsize = this.coordsize;
1666 el.coordorigin = this.coordorigin;
1668 var p = new Element(el, g, VML);
1669 p.isAbsolute = true;
1673 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1674 VML.canvas[appendChild](g);
1677 var setFillAndStroke = function (o, params) {
1678 o.attrs = o.attrs || {};
1684 for (var par in params) if (params[has](par)) {
1685 a[par] = params[par];
1687 params.href && (node.href = params.href);
1688 params.title && (node.title = params.title);
1689 params.target && (node.target = params.target);
1690 params.cursor && (s.cursor = params.cursor);
1691 if (params.path && o.type == "path") {
1692 a.path = params.path;
1693 node.path = path2vml(a.path);
1695 if (params.rotation != null) {
1696 o.rotate(params.rotation, true);
1698 if (params.translation) {
1699 xy = (params.translation + E)[split](separator);
1700 translate.call(o, xy[0], xy[1]);
1701 if (o._.rt.cx != null) {
1702 o._.rt.cx +=+ xy[0];
1703 o._.rt.cy +=+ xy[1];
1704 o.setBox(o.attrs, xy[0], xy[1]);
1708 xy = (params.scale + E)[split](separator);
1709 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1711 if ("clip-rect" in params) {
1712 var rect = (params["clip-rect"] + E)[split](separator);
1713 if (rect[length] == 4) {
1714 rect[2] = +rect[2] + (+rect[0]);
1715 rect[3] = +rect[3] + (+rect[1]);
1716 var div = node.clipRect || doc.createElement("div"),
1718 group = node.parentNode;
1719 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1720 if (!node.clipRect) {
1721 dstyle.position = "absolute";
1724 dstyle.width = o.paper.width + "px";
1725 dstyle.height = o.paper.height + "px";
1726 group.parentNode.insertBefore(div, group);
1727 div[appendChild](group);
1728 node.clipRect = div;
1731 if (!params["clip-rect"]) {
1732 node.clipRect && (node.clipRect.style.clip = E);
1735 if (o.type == "image" && params.src) {
1736 node.src = params.src;
1738 if (o.type == "image" && params.opacity) {
1739 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1740 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1742 params.font && (s.font = params.font);
1743 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1744 params["font-size"] && (s.fontSize = params["font-size"]);
1745 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1746 params["font-style"] && (s.fontStyle = params["font-style"]);
1747 if (params.opacity != null ||
1748 params["stroke-width"] != null ||
1749 params.fill != null ||
1750 params.stroke != null ||
1751 params["stroke-width"] != null ||
1752 params["stroke-opacity"] != null ||
1753 params["fill-opacity"] != null ||
1754 params["stroke-dasharray"] != null ||
1755 params["stroke-miterlimit"] != null ||
1756 params["stroke-linejoin"] != null ||
1757 params["stroke-linecap"] != null) {
1758 node = o.shape || node;
1759 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1761 !fill && (newfill = fill = createNode("fill"));
1762 if ("fill-opacity" in params || "opacity" in params) {
1763 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1764 opacity < 0 && (opacity = 0);
1765 opacity > 1 && (opacity = 1);
1766 fill.opacity = opacity;
1768 params.fill && (fill.on = true);
1769 if (fill.on == null || params.fill == "none") {
1772 if (fill.on && params.fill) {
1773 var isURL = params.fill.match(ISURL);
1775 fill.src = isURL[1];
1778 fill.color = R.getRGB(params.fill).hex;
1780 fill.type = "solid";
1781 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1783 a.gradient = params.fill;
1787 newfill && node[appendChild](fill);
1788 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1790 !stroke && (newstroke = stroke = createNode("stroke"));
1791 if ((params.stroke && params.stroke != "none") ||
1792 params["stroke-width"] ||
1793 params["stroke-opacity"] != null ||
1794 params["stroke-dasharray"] ||
1795 params["stroke-miterlimit"] ||
1796 params["stroke-linejoin"] ||
1797 params["stroke-linecap"]) {
1800 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1801 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1802 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1803 width = (toFloat(params["stroke-width"]) || 1) * .75;
1804 opacity < 0 && (opacity = 0);
1805 opacity > 1 && (opacity = 1);
1806 params["stroke-width"] == null && (width = a["stroke-width"]);
1807 params["stroke-width"] && (stroke.weight = width);
1808 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1809 stroke.opacity = opacity;
1811 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1812 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1813 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1814 if (params["stroke-dasharray"]) {
1818 "-.": "shortdashdot",
1819 "-..": "shortdashdotdot",
1824 "--.": "longdashdot",
1825 "--..": "longdashdotdot"
1827 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1829 newstroke && node[appendChild](stroke);
1831 if (res.type == "text") {
1832 var s = res.paper.span.style;
1833 a.font && (s.font = a.font);
1834 a["font-family"] && (s.fontFamily = a["font-family"]);
1835 a["font-size"] && (s.fontSize = a["font-size"]);
1836 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1837 a["font-style"] && (s.fontStyle = a["font-style"]);
1838 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1839 res.W = a.w = res.paper.span.offsetWidth;
1840 res.H = a.h = res.paper.span.offsetHeight;
1842 res.Y = a.y + round(res.H / 2);
1844 // text-anchor emulationm
1845 switch (a["text-anchor"]) {
1847 res.node.style["v-text-align"] = "left";
1848 res.bbx = round(res.W / 2);
1851 res.node.style["v-text-align"] = "right";
1852 res.bbx = -round(res.W / 2);
1855 res.node.style["v-text-align"] = "center";
1860 var addGradientFill = function (o, gradient) {
1861 o.attrs = o.attrs || {};
1862 var attrs = o.attrs,
1863 fill = o.node.getElementsByTagName("fill"),
1866 o.attrs.gradient = gradient;
1867 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1872 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1877 gradient = gradient[split](/\s*\-\s*/);
1878 if (type == "linear") {
1879 var angle = gradient.shift();
1880 angle = -toFloat(angle);
1885 var dots = parseDots(gradient);
1889 o = o.shape || o.node;
1890 fill = fill[0] || createNode("fill");
1893 fill.method = "none";
1894 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1895 fill.color = dots[0].color;
1896 fill.color2 = dots[dots[length] - 1].color;
1898 for (var i = 0, ii = dots[length]; i < ii; i++) {
1899 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1901 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1902 if (type == "radial") {
1903 fill.focus = "100%";
1904 fill.focussize = fxfy;
1905 fill.focusposition = fxfy;
1907 fill.angle = (270 - angle) % 360;
1912 var Element = function (node, group, vml) {
1920 node.raphael = this;
1933 !vml.bottom && (vml.bottom = this);
1934 this.prev = vml.top;
1935 vml.top && (vml.top.next = this);
1939 Element[proto].rotate = function (deg, cx, cy) {
1945 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1947 return this._.rt.deg;
1949 deg = (deg + E)[split](separator);
1950 if (deg[length] - 1) {
1951 cx = toFloat(deg[1]);
1952 cy = toFloat(deg[2]);
1954 deg = toFloat(deg[0]);
1956 this._.rt.deg = deg;
1958 this._.rt.deg += deg;
1960 cy == null && (cx = null);
1963 this.setBox(this.attrs, cx, cy);
1964 this.Group.style.rotation = this._.rt.deg;
1965 // gradient fix for rotation. TODO
1966 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1967 // fill = fill[0] || {};
1968 // var b = ((360 - this._.rt.deg) - 270) % 360;
1969 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1972 Element[proto].setBox = function (params, cx, cy) {
1976 var gs = this.Group.style,
1977 os = (this.shape && this.shape.style) || this.node.style;
1978 params = params || {};
1979 for (var i in params) if (params[has](i)) {
1980 this.attrs[i] = params[i];
1982 cx = cx || this._.rt.cx;
1983 cy = cy || this._.rt.cy;
1984 var attr = this.attrs,
1989 switch (this.type) {
1991 x = attr.cx - attr.r;
1992 y = attr.cy - attr.r;
1996 x = attr.cx - attr.rx;
1997 y = attr.cy - attr.ry;
2005 w = attr.width || 0;
2006 h = attr.height || 0;
2009 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2010 x = attr.x - round(this.W / 2);
2011 y = attr.y - this.H / 2;
2016 if (!this.attrs.path) {
2019 w = this.paper.width;
2020 h = this.paper.height;
2022 var dim = pathDimensions(this.attrs.path);
2032 w = this.paper.width;
2033 h = this.paper.height;
2036 cx = (cx == null) ? x + w / 2 : cx;
2037 cy = (cy == null) ? y + h / 2 : cy;
2038 var left = cx - this.paper.width / 2,
2039 top = cy - this.paper.height / 2;
2040 if (this.type == "path" || this.type == "text") {
2041 (gs.left != left + "px") && (gs.left = left + "px");
2042 (gs.top != top + "px") && (gs.top = top + "px");
2043 this.X = this.type == "text" ? x : -left;
2044 this.Y = this.type == "text" ? y : -top;
2047 (os.left != -left + "px") && (os.left = -left + "px");
2048 (os.top != -top + "px") && (os.top = -top + "px");
2050 (gs.left != left + "px") && (gs.left = left + "px");
2051 (gs.top != top + "px") && (gs.top = top + "px");
2056 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2057 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2058 (os.left != x - left + "px") && (os.left = x - left + "px");
2059 (os.top != y - top + "px") && (os.top = y - top + "px");
2060 (os.width != w + "px") && (os.width = w + "px");
2061 (os.height != h + "px") && (os.height = h + "px");
2062 var arcsize = (+params.r || 0) / mmin(w, h);
2063 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2064 // We should replace element with the new one
2065 var o = createNode("roundrect"),
2068 ii = this.events && this.events[length];
2069 o.arcsize = arcsize;
2071 this.Group[appendChild](o);
2072 this.Group.removeChild(this.node);
2073 this[0] = this.node = o;
2074 this.arcsize = arcsize;
2075 for (var i in attr) {
2080 if (this.events) for (; i < ii; i++) {
2081 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2086 Element[proto].hide = function () {
2087 !this.removed && (this.Group.style.display = "none");
2090 Element[proto].show = function () {
2091 !this.removed && (this.Group.style.display = "block");
2094 Element[proto].getBBox = function () {
2098 if (this.type == "path") {
2099 return pathDimensions(this.attrs.path);
2102 x: this.X + (this.bbx || 0),
2108 Element[proto].remove = function () {
2112 tear(this, this.paper);
2113 this.node.parentNode.removeChild(this.node);
2114 this.Group.parentNode.removeChild(this.Group);
2115 this.shape && this.shape.parentNode.removeChild(this.shape);
2116 for (var i in this) {
2119 this.removed = true;
2121 Element[proto].attr = function () {
2125 if (arguments[length] == 0) {
2127 for (var i in this.attrs) if (this.attrs[has](i)) {
2128 res[i] = this.attrs[i];
2130 this._.rt.deg && (res.rotation = this.rotate());
2131 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2132 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2135 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2136 if (arguments[0] == "translation") {
2137 return translate.call(this);
2139 if (arguments[0] == "rotation") {
2140 return this.rotate();
2142 if (arguments[0] == "scale") {
2143 return this.scale();
2145 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2146 return this.attrs.gradient;
2148 return this.attrs[arguments[0]];
2150 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2152 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2153 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2158 if (arguments[length] == 2) {
2160 params[arguments[0]] = arguments[1];
2162 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2164 if (params.text && this.type == "text") {
2165 this.node.string = params.text;
2167 setFillAndStroke(this, params);
2168 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2169 addGradientFill(this, params.gradient);
2171 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2175 Element[proto].toFront = function () {
2176 !this.removed && this.Group.parentNode[appendChild](this.Group);
2177 this.paper.top != this && tofront(this, this.paper);
2180 Element[proto].toBack = function () {
2184 if (this.Group.parentNode.firstChild != this.Group) {
2185 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2186 toback(this, this.paper);
2190 Element[proto].insertAfter = function (element) {
2194 if (element.Group.nextSibling) {
2195 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2197 element.Group.parentNode[appendChild](this.Group);
2199 insertafter(this, element, this.paper);
2202 Element[proto].insertBefore = function (element) {
2206 element.Group.parentNode.insertBefore(this.Group, element.Group);
2207 insertbefore(this, element, this.paper);
2211 var theCircle = function (vml, x, y, r) {
2212 var g = createNode("group"),
2213 o = createNode("oval"),
2215 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2216 g.coordsize = vml.coordsize;
2217 g.coordorigin = vml.coordorigin;
2219 var res = new Element(o, g, vml);
2220 res.type = "circle";
2221 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2225 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2226 vml.canvas[appendChild](g);
2229 theRect = function (vml, x, y, w, h, r) {
2230 var g = createNode("group"),
2231 o = createNode("roundrect"),
2232 arcsize = (+r || 0) / (mmin(w, h));
2233 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2234 g.coordsize = vml.coordsize;
2235 g.coordorigin = vml.coordorigin;
2237 o.arcsize = arcsize;
2238 var res = new Element(o, g, vml);
2240 setFillAndStroke(res, {stroke: "#000"});
2241 res.arcsize = arcsize;
2242 res.setBox({x: x, y: y, width: w, height: h, r: r});
2243 vml.canvas[appendChild](g);
2246 theEllipse = function (vml, x, y, rx, ry) {
2247 var g = createNode("group"),
2248 o = createNode("oval"),
2250 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2251 g.coordsize = vml.coordsize;
2252 g.coordorigin = vml.coordorigin;
2254 var res = new Element(o, g, vml);
2255 res.type = "ellipse";
2256 setFillAndStroke(res, {stroke: "#000"});
2261 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2262 vml.canvas[appendChild](g);
2265 theImage = function (vml, src, x, y, w, h) {
2266 var g = createNode("group"),
2267 o = createNode("image"),
2269 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2270 g.coordsize = vml.coordsize;
2271 g.coordorigin = vml.coordorigin;
2274 var res = new Element(o, g, vml);
2276 res.attrs.src = src;
2281 res.setBox({x: x, y: y, width: w, height: h});
2282 vml.canvas[appendChild](g);
2285 theText = function (vml, x, y, text) {
2286 var g = createNode("group"),
2287 el = createNode("shape"),
2289 path = createNode("path"),
2291 o = createNode("textpath");
2292 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2293 g.coordsize = vml.coordsize;
2294 g.coordorigin = vml.coordorigin;
2295 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2296 path.textpathok = true;
2297 ol.width = vml.width;
2298 ol.height = vml.height;
2299 o.string = text + E;
2302 el[appendChild](path);
2304 var res = new Element(o, g, vml);
2306 res.textpath = path;
2308 res.attrs.text = text;
2313 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2315 vml.canvas[appendChild](g);
2318 setSize = function (width, height) {
2319 var cs = this.canvas.style;
2320 width == +width && (width += "px");
2321 height == +height && (height += "px");
2324 cs.clip = "rect(0 " + width + " " + height + " 0)";
2328 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2330 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2331 createNode = function (tagName) {
2332 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2335 createNode = function (tagName) {
2336 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2339 var create = function () {
2340 var con = getContainer[apply](null, arguments),
2341 container = con.container,
2342 height = con.height,
2348 throw new Error("VML container not found.");
2350 var res = new Paper,
2351 c = res.canvas = doc.createElement("div"),
2353 width = width || 512;
2354 height = height || 342;
2355 width == +width && (width += "px");
2356 height == +height && (height += "px");
2359 res.coordsize = "1000 1000";
2360 res.coordorigin = "0 0";
2361 res.span = doc.createElement("span");
2362 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2363 c[appendChild](res.span);
2364 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2365 if (container == 1) {
2366 doc.body[appendChild](c);
2370 container.style.width = width;
2371 container.style.height = height;
2372 if (container.firstChild) {
2373 container.insertBefore(c, container.firstChild);
2375 container[appendChild](c);
2378 plugins.call(res, res, R.fn);
2381 Paper[proto].clear = function () {
2382 this.canvas.innerHTML = E;
2383 this.span = doc.createElement("span");
2384 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2385 this.canvas[appendChild](this.span);
2386 this.bottom = this.top = null;
2388 Paper[proto].remove = function () {
2389 this.canvas.parentNode.removeChild(this.canvas);
2390 for (var i in this) {
2391 this[i] = removed(i);
2397 // Safari or Chrome (WebKit) rendering bug workaround method
2398 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2399 Paper[proto].safari = function () {
2400 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2401 setTimeout(function () {rect.remove();});
2404 Paper[proto].safari = function () {};
2408 var addEvent = (function () {
2409 if (doc.addEventListener) {
2410 return function (obj, type, fn, element) {
2411 var f = function (e) {
2412 return fn.call(element, e);
2414 obj.addEventListener(type, f, false);
2415 return function () {
2416 obj.removeEventListener(type, f, false);
2420 } else if (doc.attachEvent) {
2421 return function (obj, type, fn, element) {
2422 var f = function (e) {
2423 return fn.call(element, e || win.event);
2425 obj.attachEvent("on" + type, f);
2426 var detacher = function () {
2427 obj.detachEvent("on" + type, f);
2434 for (var i = events[length]; i--;) {
2435 (function (eventName) {
2436 Element[proto][eventName] = function (fn) {
2437 if (R.is(fn, "function")) {
2438 this.events = this.events || [];
2439 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2443 Element[proto]["un" + eventName] = function (fn) {
2444 var events = this.events,
2446 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2448 events.splice(l, 1);
2449 !events.length && delete this.events;
2456 Element[proto].hover = function (f_in, f_out) {
2457 return this.mouseover(f_in).mouseout(f_out);
2459 Element[proto].unhover = function (f_in, f_out) {
2460 return this.unmouseover(f_in).unmouseout(f_out);
2462 Paper[proto].circle = function (x, y, r) {
2463 return theCircle(this, x || 0, y || 0, r || 0);
2465 Paper[proto].rect = function (x, y, w, h, r) {
2466 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2468 Paper[proto].ellipse = function (x, y, rx, ry) {
2469 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2471 Paper[proto].path = function (pathString) {
2472 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2473 return thePath(R.format[apply](R, arguments), this);
2475 Paper[proto].image = function (src, x, y, w, h) {
2476 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2478 Paper[proto].text = function (x, y, text) {
2479 return theText(this, x || 0, y || 0, text || E);
2481 Paper[proto].set = function (itemsArray) {
2482 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2483 return new Set(itemsArray);
2485 Paper[proto].setSize = setSize;
2486 Paper[proto].top = Paper[proto].bottom = null;
2487 Paper[proto].raphael = R;
2489 return this.x + S + this.y;
2491 Element[proto].scale = function (x, y, cx, cy) {
2492 if (x == null && y == null) {
2507 var bb = this.getBBox(),
2508 rcx = bb.x + bb.width / 2,
2509 rcy = bb.y + bb.height / 2,
2512 cx = (+cx || cx == 0) ? cx : rcx;
2513 cy = (+cy || cy == 0) ? cy : rcy;
2514 var dirx = ~~(x / math.abs(x)),
2515 diry = ~~(y / math.abs(y)),
2516 s = this.node.style,
2517 ncx = cx + (rcx - cx) * kx,
2518 ncy = cy + (rcy - cy) * ky;
2519 switch (this.type) {
2522 var neww = a.width * dirx * kx,
2523 newh = a.height * diry * ky;
2526 r: a.r * mmin(dirx * kx, diry * ky),
2535 rx: a.rx * dirx * kx,
2536 ry: a.ry * diry * ky,
2537 r: a.r * mmin(dirx * kx, diry * ky),
2543 var path = pathToRelative(a.path),
2545 for (var i = 0, ii = path[length]; i < ii; i++) {
2548 P0 = upperCase.call(p[0]);
2549 if (P0 == "M" && skip) {
2555 p[path[i][length] - 2] *= kx;
2556 p[path[i][length] - 1] *= ky;
2559 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2560 } else if (P0 == "H") {
2561 for (j = 1, jj = p[length]; j < jj; j++) {
2564 } else if (P0 == "V") {
2565 for (j = 1, jj = p[length]; j < jj; j++) {
2569 for (j = 1, jj = p[length]; j < jj; j++) {
2570 p[j] *= (j % 2) ? kx : ky;
2574 var dim2 = pathDimensions(path),
2575 dx = ncx - dim2.x - dim2.width / 2,
2576 dy = ncy - dim2.y - dim2.height / 2;
2579 this.attr({path: path});
2582 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2583 if (this.transformations) {
2584 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2585 this.node[setAttribute]("transform", this.transformations[join](S));
2586 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2587 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2588 this.attr({x: dx, y: dy});
2592 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2593 ", M12=0, M21=0, M22=", diry,
2594 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2595 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2598 if (this.transformations) {
2599 this.transformations[2] = E;
2600 this.node[setAttribute]("transform", this.transformations[join](S));
2604 this.node.filterMatrix = E;
2605 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2608 a.scale = [x, y, cx, cy][join](S);
2614 Element[proto].clone = function () {
2615 var attr = this.attr();
2617 delete attr.translation;
2618 return this.paper[this.type]().attr(attr);
2620 var getLengthFactory = function (istotal, subpath) {
2621 return function (path, length, onlystart) {
2622 path = path2curve(path);
2623 var x, y, p, l, sp = "", subpaths = {}, point,
2625 for (var i = 0, ii = path.length; i < ii; i++) {
2631 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2632 if (len + l > length) {
2633 if (subpath && !subpaths.start) {
2634 point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2635 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2636 if (onlystart) return sp;
2637 subpaths.start = sp;
2638 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2644 if (!istotal && !subpath) {
2645 point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2646 return {x: point.x, y: point.y, alpha: point.alpha};
2656 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2657 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2661 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2662 var old = {x: 0, y: 0},
2664 for (var i = 0; i < 1.01; i+=.01) {
2665 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2666 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
2671 var getTotalLength = getLengthFactory(1),
2672 getPointAtLength = getLengthFactory(),
2673 getSubpathsAtLength = getLengthFactory(0, 1);
2674 Element[proto].getTotalLength = function () {
2675 if (this.type != "path") return;
2676 return getTotalLength(this.attrs.path);
2678 Element[proto].getPointAtLength = function (length) {
2679 if (this.type != "path") return;
2680 return getPointAtLength(this.attrs.path, length);
2682 Element[proto].getSubpath = function (from, to) {
2683 if (this.type != "path") return;
2684 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2685 return getSubpathsAtLength(this.attrs.path, from).end;
2687 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2688 return from ? getSubpathsAtLength(a, from).end : a;
2691 // animation easing formulas
2692 R.easing_formulas = {
2693 linear: function (n) {
2700 return pow(n - 1, 3) + 1;
2702 "<>": function (n) {
2705 return pow(n, 3) / 2;
2708 return (pow(n, 3) + 2) / 2;
2710 backIn: function (n) {
2712 return n * n * ((s + 1) * n - s);
2714 backOut: function (n) {
2717 return n * n * ((s + 1) * n + s) + 1;
2719 elastic: function (n) {
2720 if (n == 0 || n == 1) {
2725 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2727 bounce: function (n) {
2736 l = s * n * n + .75;
2738 if (n < (2.5 / p)) {
2740 l = s * n * n + .9375;
2743 l = s * n * n + .984375;
2751 var animationElements = {length : 0},
2752 animation = function () {
2753 var Now = +new Date;
2754 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2755 var e = animationElements[l];
2757 delete animationElements[l];
2758 animationElements[length]--;
2761 var time = Now - e.start,
2770 callback = e.callback,
2774 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2775 for (var attr in from) if (from[has](attr)) {
2776 switch (availableAnimAttrs[attr]) {
2778 now = pos * ms * diff[attr];
2779 to.back && (now = to.len - now);
2780 var point = getPointAtLength(to[attr], now);
2781 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2784 that.translate(point.x - diff.sx, point.y - diff.sy);
2785 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2788 now = +from[attr] + pos * ms * diff[attr];
2792 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2793 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2794 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2799 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2800 now[i] = [from[attr][i][0]];
2801 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2802 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2804 now[i] = now[i][join](S);
2811 var x = diff[attr][0] * (time - prev),
2812 y = diff[attr][1] * (time - prev);
2818 now = +from[attr][0] + pos * ms * diff[attr][0];
2819 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2822 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);
2828 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2837 that._run && that._run.call(that);
2840 var point = getPointAtLength(to.along, to.len * !to.back);
2841 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
2842 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2844 (t.x || t.y) && that.translate(-t.x, -t.y);
2845 to.scale && (to.scale = to.scale + E);
2847 delete animationElements[l];
2848 animationElements[length]--;
2849 that.in_animation = null;
2850 R.is(callback, "function") && callback.call(that);
2854 R.svg && that && that.paper.safari();
2855 animationElements[length] && setTimeout(animation);
2857 upto255 = function (color) {
2858 return color > 255 ? 255 : (color < 0 ? 0 : color);
2860 translate = function (x, y) {
2862 return {x: this._.tx, y: this._.ty, toString: x_y};
2866 switch (this.type) {
2869 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2874 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2877 var path = pathToRelative(this.attrs.path);
2880 this.attr({path: path});
2885 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2886 animationElements[element.id] && (params.start = animationElements[element.id].start);
2887 return this.animate(params, ms, easing, callback);
2889 Element[proto].animateAlong = along();
2890 Element[proto].animateAlongBack = along(1);
2891 function along(isBack) {
2892 return function (path, ms, rotate, callback) {
2893 var params = {back: isBack};
2894 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
2895 path && path.constructor == Element && (path = path.attrs.path);
2896 path && (params.along = path);
2897 return this.animate(params, ms, callback);
2900 Element[proto].onAnimation = function (f) {
2904 Element[proto].animate = function (params, ms, easing, callback) {
2905 if (R.is(easing, "function") || !easing) {
2906 callback = easing || null;
2911 for (var attr in params) if (params[has](attr)) {
2912 if (availableAnimAttrs[has](attr)) {
2913 from[attr] = this.attr(attr);
2914 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2915 to[attr] = params[attr];
2916 switch (availableAnimAttrs[attr]) {
2918 var len = getTotalLength(params[attr]),
2919 point = getPointAtLength(params[attr], len * !!params.back),
2920 bb = this.getBBox();
2921 diff[attr] = len / ms;
2926 to.rot = params.rot;
2927 to.back = params.back;
2929 params.rot && (diff.r = toFloat(this.rotate()) || 0);
2932 diff[attr] = (to[attr] - from[attr]) / ms;
2935 from[attr] = R.getRGB(from[attr]);
2936 var toColour = R.getRGB(to[attr]);
2938 r: (toColour.r - from[attr].r) / ms,
2939 g: (toColour.g - from[attr].g) / ms,
2940 b: (toColour.b - from[attr].b) / ms
2944 var pathes = path2curve(from[attr], to[attr]);
2945 from[attr] = pathes[0];
2946 var toPath = pathes[1];
2948 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2949 diff[attr][i] = [0];
2950 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2951 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2956 var values = (params[attr] + E)[split](separator),
2957 from2 = (from[attr] + E)[split](separator);
2960 from[attr] = [0, 0];
2961 diff[attr] = [values[0] / ms, values[1] / ms];
2964 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2965 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2968 params[attr] = values;
2969 from[attr] = (from[attr] + E)[split](separator);
2970 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2973 from[attr] = (from[attr] + E)[split](separator);
2977 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2986 this.in_animation = 1;
2987 animationElements[this.id] = {
2988 start: params.start || +new Date,
2998 ++animationElements[length] == 1 && animation();
3001 Element[proto].stop = function () {
3002 animationElements[this.id] && animationElements[length]--;
3003 delete animationElements[this.id];
3006 Element[proto].translate = function (x, y) {
3007 return this.attr({translation: x + " " + y});
3009 Element[proto][toString] = function () {
3010 return "Rapha\xebl\u2019s object";
3012 R.ae = animationElements;
3015 var Set = function (items) {
3019 for (var i = 0, ii = items[length]; i < ii; i++) {
3020 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3021 this[this.items[length]] = this.items[this.items[length]] = items[i];
3027 Set[proto][push] = function () {
3030 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3031 item = arguments[i];
3032 if (item && (item.constructor == Element || item.constructor == Set)) {
3033 len = this.items[length];
3034 this[len] = this.items[len] = item;
3040 Set[proto].pop = function () {
3041 delete this[this[length]--];
3042 return this.items.pop();
3044 for (var method in Element[proto]) if (Element[proto][has](method)) {
3045 Set[proto][method] = (function (methodname) {
3046 return function () {
3047 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3048 this.items[i][methodname][apply](this.items[i], arguments);
3054 Set[proto].attr = function (name, value) {
3055 if (name && R.is(name, "array") && R.is(name[0], "object")) {
3056 for (var j = 0, jj = name[length]; j < jj; j++) {
3057 this.items[j].attr(name[j]);
3060 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3061 this.items[i].attr[apply](this.items[i], arguments);
3066 Set[proto].animate = function (params, ms, easing, callback) {
3067 (R.is(easing, "function") || !easing) && (callback = easing || null);
3068 var len = this.items[length],
3072 callback && (collector = function () {
3073 !--len && callback.call(set);
3075 this.items[--i].animate(params, ms, easing || collector, collector);
3077 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3081 Set[proto].insertAfter = function (el) {
3082 var i = this.items[length];
3084 this.items[i].insertAfter(el);
3088 Set[proto].getBBox = function () {
3093 for (var i = this.items[length]; i--;) {
3094 var box = this.items[i].getBBox();
3097 w[push](box.x + box.width);
3098 h[push](box.y + box.height);
3100 x = mmin[apply](0, x);
3101 y = mmin[apply](0, y);
3105 width: mmax[apply](0, w) - x,
3106 height: mmax[apply](0, h) - y
3110 R.registerFont = function (font) {
3114 this.fonts = this.fonts || {};
3120 family = font.face["font-family"];
3121 for (var prop in font.face) if (font.face[has](prop)) {
3122 fontcopy.face[prop] = font.face[prop];
3124 if (this.fonts[family]) {
3125 this.fonts[family][push](fontcopy);
3127 this.fonts[family] = [fontcopy];
3130 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3131 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3132 var path = font.glyphs[glyph];
3133 fontcopy.glyphs[glyph] = {
3136 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3137 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3141 for (var k in path.k) if (path[has](k)) {
3142 fontcopy.glyphs[glyph].k[k] = path.k[k];
3149 Paper[proto].getFont = function (family, weight, style, stretch) {
3150 stretch = stretch || "normal";
3151 style = style || "normal";
3152 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3153 var font = R.fonts[family];
3155 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3156 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3157 if (name.test(fontName)) {
3158 font = R.fonts[fontName];
3165 for (var i = 0, ii = font[length]; i < ii; i++) {
3167 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3174 Paper[proto].print = function (x, y, string, font, size, origin) {
3175 origin = origin || "middle"; // baseline|middle
3176 var out = this.set(),
3177 letters = (string + E)[split](E),
3181 R.is(font, "string") && (font = this.getFont(font));
3183 scale = (size || 16) / font.face["units-per-em"];
3184 var bb = font.face.bbox.split(separator),
3186 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3187 for (var i = 0, ii = letters[length]; i < ii; i++) {
3188 var prev = i && font.glyphs[letters[i - 1]] || {},
3189 curr = font.glyphs[letters[i]];
3190 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3191 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3193 out.scale(scale, scale, top, height).translate(x - top, y - height);
3198 R.format = function (token) {
3199 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3201 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3202 return args[++i] == null ? E : args[i];
3206 R.ninja = function () {
3208 if (oldRaphael.was) {
3209 Raphael = oldRaphael.is;
3215 R.el = Element[proto];