2 * Raphael 1.3.2 - JavaScript Vector Library
4 * Copyright (c) 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
8 Raphael = (function () {
9 var separator = /[, ]+/,
10 elements = /^(circle|rect|path|ellipse|text|image)$/,
12 has = "hasOwnProperty",
16 was: Object[proto][has].call(win, "Raphael"),
20 if (R.is(arguments[0], "array")) {
22 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
24 for (var i = 0, ii = a[length]; i < ii; i++) {
26 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
30 return create[apply](R, arguments);
32 Paper = function () {},
33 appendChild = "appendChild",
39 events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
42 lowerCase = String[proto].toLowerCase,
47 toString = "toString",
48 objectToString = Object[proto][toString],
53 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
54 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,
56 setAttribute = "setAttribute",
59 upperCase = String[proto].toUpperCase,
60 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
61 availableAnimAttrs = {along: "along", blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
64 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
65 if (R.type == "VML") {
66 var d = doc.createElement("div");
67 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
68 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 win.ActiveXObject("htmlfile");
96 docum.write("<body>");
100 bod = win.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 var hsbtoString = function () {
125 return "hsb(" + [this.h, this.s, this.b] + ")";
127 rgbtoString = function () {
130 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
131 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
139 if (brightness == 0) {
140 return {r: 0, g: 0, b: 0, hex: "#000"};
142 if (hue > 1 || saturation > 1 || brightness > 1) {
149 p = brightness * (1 - saturation),
150 q = brightness * (1 - (saturation * f)),
151 t = brightness * (1 - (saturation * (1 - f)));
152 red = [brightness, q, p, p, t, brightness, brightness][i];
153 green = [t, brightness, brightness, q, p, p, t][i];
154 blue = [p, p, t, brightness, brightness, q, p][i];
158 var rgb = {r: red, g: green, b: blue, toString: rgbtoString},
159 r = (~~red)[toString](16),
160 g = (~~green)[toString](16),
161 b = (~~blue)[toString](16);
165 rgb.hex = "#" + r + g + b;
168 R.rgb2hsb = cacher(function (red, green, blue) {
169 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
174 if (R.is(red, "string")) {
175 var clr = R.getRGB(red);
180 if (red > 1 || green > 1 || blue > 1) {
185 var max = mmax(red, green, blue),
186 min = mmin(red, green, blue),
191 return {h: 0, s: 0, b: max};
193 var delta = (max - min);
194 saturation = delta / max;
196 hue = (green - blue) / delta;
197 } else if (green == max) {
198 hue = 2 + ((blue - red) / delta);
200 hue = 4 + ((red - green) / delta);
206 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
208 var p2s = /,?([achlmqrstvxz]),?/gi;
209 R._path2string = function () {
210 return this.join(",")[rp](p2s, "$1");
212 function cacher(f, scope, postprocessor) {
214 var arg = Array[proto].slice.call(arguments, 0),
215 args = arg[join]("\u25ba"),
216 cache = newf.cache = newf.cache || {},
217 count = newf.count = newf.count || [];
218 if (cache[has](args)) {
219 return postprocessor ? postprocessor(cache[args]) : cache[args];
221 count[length] >= 1e3 && delete cache[count.shift()];
223 cache[args] = f[apply](scope, arg);
224 return postprocessor ? postprocessor(cache[args]) : cache[args];
229 R.getRGB = cacher(function (colour) {
230 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
231 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
233 if (colour == "none") {
234 return {r: -1, g: -1, b: -1, hex: "none"};
236 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
242 rgb = colour.match(colourRegExp);
245 blue = toInt(rgb[2].substring(5), 16);
246 green = toInt(rgb[2].substring(3, 5), 16);
247 red = toInt(rgb[2].substring(1, 3), 16);
250 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
251 green = toInt((t = rgb[3].charAt(2)) + t, 16);
252 red = toInt((t = rgb[3].charAt(1)) + t, 16);
255 rgb = rgb[4][split](/\s*,\s*/);
256 red = toFloat(rgb[0]);
257 green = toFloat(rgb[1]);
258 blue = toFloat(rgb[2]);
261 rgb = rgb[5][split](/\s*,\s*/);
262 red = toFloat(rgb[0]) * 2.55;
263 green = toFloat(rgb[1]) * 2.55;
264 blue = toFloat(rgb[2]) * 2.55;
267 rgb = rgb[6][split](/\s*,\s*/);
268 red = toFloat(rgb[0]);
269 green = toFloat(rgb[1]);
270 blue = toFloat(rgb[2]);
271 return R.hsb2rgb(red, green, blue);
274 rgb = rgb[7][split](/\s*,\s*/);
275 red = toFloat(rgb[0]) * 2.55;
276 green = toFloat(rgb[1]) * 2.55;
277 blue = toFloat(rgb[2]) * 2.55;
278 return R.hsb2rgb(red, green, blue);
280 rgb = {r: red, g: green, b: blue};
281 var r = (~~red)[toString](16),
282 g = (~~green)[toString](16),
283 b = (~~blue)[toString](16);
287 rgb.hex = "#" + r + g + b;
290 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
292 R.getColor = function (value) {
293 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
294 rgb = this.hsb2rgb(start.h, start.s, start.b);
299 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
303 R.getColor.reset = function () {
307 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
308 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
309 R.parsePathString = cacher(function (pathString) {
313 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
315 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
316 data = pathClone(pathString);
319 (pathString + E)[rp](pathCommand, function (a, b, c) {
321 name = lowerCase.call(b);
322 c[rp](pathValues, function (a, b) {
323 b && params[push](+b);
325 if (name == "m" && params[length] > 2) {
326 data[push]([b][concat](params.splice(0, 2)));
328 b = b == "m" ? "l" : "L";
330 while (params[length] >= paramCounts[name]) {
331 data[push]([b][concat](params.splice(0, paramCounts[name])));
332 if (!paramCounts[name]) {
338 data[toString] = R._path2string;
341 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
343 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
344 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
345 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
346 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
347 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
348 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
349 ax = (1 - t) * p1x + t * c1x,
350 ay = (1 - t) * p1y + t * c1y,
351 cx = (1 - t) * c2x + t * p2x,
352 cy = (1 - t) * c2y + t * p2y,
353 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
354 (mx > nx || my < ny) && (alpha += 180);
355 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};
357 var pathDimensions = cacher(function (path) {
359 return {x: 0, y: 0, width: 0, height: 0};
361 path = path2curve(path);
367 for (var i = 0, ii = path[length]; i < ii; i++) {
375 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
376 X = X[concat](dim.min.x, dim.max.x);
377 Y = Y[concat](dim.min.y, dim.max.y);
382 var xmin = mmin[apply](0, X),
383 ymin = mmin[apply](0, Y);
387 width: mmax[apply](0, X) - xmin,
388 height: mmax[apply](0, Y) - ymin
391 pathClone = function (pathArray) {
393 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
394 pathArray = R.parsePathString(pathArray);
396 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
398 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
399 res[i][j] = pathArray[i][j];
402 res[toString] = R._path2string;
405 pathToRelative = cacher(function (pathArray) {
406 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
407 pathArray = R.parsePathString(pathArray);
415 if (pathArray[0][0] == "M") {
421 res[push](["M", x, y]);
423 for (var i = start, ii = pathArray[length]; i < ii; i++) {
426 if (pa[0] != lowerCase.call(pa[0])) {
427 r[0] = lowerCase.call(pa[0]);
435 r[6] = +(pa[6] - x).toFixed(3);
436 r[7] = +(pa[7] - y).toFixed(3);
439 r[1] = +(pa[1] - y).toFixed(3);
445 for (var j = 1, jj = pa[length]; j < jj; j++) {
446 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
455 for (var k = 0, kk = pa[length]; k < kk; k++) {
459 var len = res[i][length];
466 x += +res[i][len - 1];
469 y += +res[i][len - 1];
472 x += +res[i][len - 2];
473 y += +res[i][len - 1];
476 res[toString] = R._path2string;
479 pathToAbsolute = cacher(function (pathArray) {
480 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
481 pathArray = R.parsePathString(pathArray);
489 if (pathArray[0][0] == "M") {
490 x = +pathArray[0][1];
491 y = +pathArray[0][2];
495 res[0] = ["M", x, y];
497 for (var i = start, ii = pathArray[length]; i < ii; i++) {
500 if (pa[0] != upperCase.call(pa[0])) {
501 r[0] = upperCase.call(pa[0]);
522 for (var j = 1, jj = pa[length]; j < jj; j++) {
523 r[j] = +pa[j] + ((j % 2) ? x : y);
527 for (var k = 0, kk = pa[length]; k < kk; k++) {
543 x = res[i][res[i][length] - 2];
544 y = res[i][res[i][length] - 1];
547 res[toString] = R._path2string;
550 l2c = function (x1, y1, x2, y2) {
551 return [x1, y1, x2, y2, x2, y2];
553 q2c = function (x1, y1, ax, ay, x2, y2) {
565 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
566 // for more information of where this math came from visit:
567 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
569 _120 = PI * 120 / 180,
570 rad = PI / 180 * (+angle || 0),
573 rotate = cacher(function (x, y, rad) {
574 var X = x * math.cos(rad) - y * math.sin(rad),
575 Y = x * math.sin(rad) + y * math.cos(rad);
579 xy = rotate(x1, y1, -rad);
582 xy = rotate(x2, y2, -rad);
585 var cos = math.cos(PI / 180 * angle),
586 sin = math.sin(PI / 180 * angle),
589 // rx = mmax(rx, math.abs(x));
590 // ry = mmax(ry, math.abs(y));
591 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
599 k = (large_arc_flag == sweep_flag ? -1 : 1) *
600 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
601 cx = k * rx * y / ry + (x1 + x2) / 2,
602 cy = k * -ry * x / rx + (y1 + y2) / 2,
603 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
604 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
606 f1 = x1 < cx ? PI - f1 : f1;
607 f2 = x2 < cx ? PI - f2 : f2;
608 f1 < 0 && (f1 = PI * 2 + f1);
609 f2 < 0 && (f2 = PI * 2 + f2);
610 if (sweep_flag && f1 > f2) {
613 if (!sweep_flag && f2 > f1) {
623 if (math.abs(df) > _120) {
627 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
628 x2 = cx + rx * math.cos(f2);
629 y2 = cy + ry * math.sin(f2);
630 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
633 var c1 = math.cos(f1),
637 t = math.tan(df / 4),
641 m2 = [x1 + hx * s1, y1 - hy * c1],
642 m3 = [x2 + hx * s2, y2 - hy * c2],
644 m2[0] = 2 * m1[0] - m2[0];
645 m2[1] = 2 * m1[1] - m2[1];
647 return [m2, m3, m4][concat](res);
649 res = [m2, m3, m4][concat](res)[join]()[split](",");
651 for (var i = 0, ii = res[length]; i < ii; i++) {
652 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
658 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
661 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
662 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
665 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
666 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
667 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
669 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
670 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
674 math.abs(t1) > 1e12 && (t1 = .5);
675 math.abs(t2) > 1e12 && (t2 = .5);
676 if (t1 > 0 && t1 < 1) {
677 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
681 if (t2 > 0 && t2 < 1) {
682 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
686 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
687 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
689 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
690 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
691 math.abs(t1) > 1e12 && (t1 = .5);
692 math.abs(t2) > 1e12 && (t2 = .5);
693 if (t1 > 0 && t1 < 1) {
694 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
698 if (t2 > 0 && t2 < 1) {
699 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
704 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
705 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
708 path2curve = cacher(function (path, path2) {
709 var p = pathToAbsolute(path),
710 p2 = path2 && pathToAbsolute(path2),
711 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
712 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
713 processPath = function (path, d) {
716 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
718 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
725 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
728 nx = d.x + (d.x - (d.bx || d.x));
729 ny = d.y + (d.y - (d.by || d.y));
730 path = ["C", nx, ny][concat](path.slice(1));
733 d.qx = d.x + (d.x - (d.qx || d.x));
734 d.qy = d.y + (d.y - (d.qy || d.y));
735 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
740 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
743 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
746 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
749 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
752 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
757 fixArc = function (pp, i) {
758 if (pp[i][length] > 7) {
762 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
765 ii = mmax(p[length], p2 && p2[length] || 0);
768 fixM = function (path1, path2, a1, a2, i) {
769 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
770 path2.splice(i, 0, ["M", a2.x, a2.y]);
775 ii = mmax(p[length], p2 && p2[length] || 0);
778 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
779 p[i] = processPath(p[i], attrs);
781 p2 && (p2[i] = processPath(p2[i], attrs2));
783 fixM(p, p2, attrs, attrs2, i);
784 fixM(p2, p, attrs2, attrs, i);
787 seglen = seg[length],
788 seg2len = p2 && seg2[length];
789 attrs.x = seg[seglen - 2];
790 attrs.y = seg[seglen - 1];
791 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
792 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
793 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
794 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
795 attrs2.x = p2 && seg2[seg2len - 2];
796 attrs2.y = p2 && seg2[seg2len - 1];
798 return p2 ? [p, p2] : p;
800 parseDots = cacher(function (gradient) {
802 for (var i = 0, ii = gradient[length]; i < ii; i++) {
804 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
805 dot.color = R.getRGB(par[1]);
806 if (dot.color.error) {
809 dot.color = dot.color.hex;
810 par[2] && (dot.offset = par[2] + "%");
813 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
814 if (!dots[i].offset) {
815 var start = toFloat(dots[i - 1].offset || 0),
817 for (var j = i + 1; j < ii; j++) {
818 if (dots[j].offset) {
819 end = dots[j].offset;
828 var d = (end - start) / (j - i + 1);
831 dots[i].offset = start + "%";
837 getContainer = function (x, y, w, h) {
839 if (R.is(x, "string") || R.is(x, "object")) {
840 container = R.is(x, "string") ? doc.getElementById(x) : x;
841 if (container.tagName) {
844 container: container,
845 width: container.style.pixelWidth || container.offsetWidth,
846 height: container.style.pixelHeight || container.offsetHeight
849 return {container: container, width: y, height: w};
852 } else if (R.is(x, nu) && h != null) {
853 return {container: 1, x: x, y: y, width: w, height: h};
856 plugins = function (con, add) {
858 for (var prop in add) {
859 if (add[has](prop) && !(prop in con)) {
860 switch (typeof add[prop]) {
863 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
867 con[prop] = con[prop] || {};
868 plugins.call(this, con[prop], add[prop]);
871 con[prop] = add[prop];
877 tear = function (el, paper) {
878 el == paper.top && (paper.top = el.prev);
879 el == paper.bottom && (paper.bottom = el.next);
880 el.next && (el.next.prev = el.prev);
881 el.prev && (el.prev.next = el.next);
883 tofront = function (el, paper) {
884 if (paper.top === el) {
893 toback = function (el, paper) {
894 if (paper.bottom === el) {
898 el.next = paper.bottom;
900 paper.bottom.prev = el;
903 insertafter = function (el, el2, paper) {
905 el2 == paper.top && (paper.top = el);
906 el2.next && (el2.next.prev = el);
911 insertbefore = function (el, el2, paper) {
913 el2 == paper.bottom && (paper.bottom = el);
914 el2.prev && (el2.prev.next = el);
919 removed = function (methodname) {
921 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
924 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
928 Paper[proto].svgns = "http://www.w3.org/2000/svg";
929 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
930 round = function (num) {
931 return +num + (~~num === num) * .5;
933 var roundPath = function (path) {
934 for (var i = 0, ii = path[length]; i < ii; i++) {
935 if (lowerCase.call(path[i][0]) != "a") {
936 for (var j = 1, jj = path[i][length]; j < jj; j++) {
937 path[i][j] = round(path[i][j]);
940 path[i][6] = round(path[i][6]);
941 path[i][7] = round(path[i][7]);
946 $ = function (el, attr) {
948 for (var key in attr) {
949 if (attr[has](key)) {
950 el[setAttribute](key, attr[key] + E);
954 return doc.createElementNS(Paper[proto].svgns, el);
957 R[toString] = function () {
958 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
960 var thePath = function (pathString, SVG) {
962 SVG.canvas && SVG.canvas[appendChild](el);
963 var p = new Element(el, SVG);
965 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
968 var addGradientFill = function (o, gradient, SVG) {
972 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
977 var dir = ((fy > .5) * 2 - 1);
978 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
979 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
981 (fy = fy.toFixed(5) - 1e-5 * dir);
985 gradient = gradient[split](/\s*\-\s*/);
986 if (type == "linear") {
987 var angle = gradient.shift();
988 angle = -toFloat(angle);
992 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
993 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
997 vector[0] = -vector[2];
1000 if (vector[3] < 0) {
1001 vector[1] = -vector[3];
1005 var dots = parseDots(gradient);
1009 var id = o.getAttribute("fill");
1010 id = id.match(/^url\(#(.*)\)$/);
1011 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1013 var el = $(type + "Gradient");
1014 el.id = "r" + (R._id++)[toString](36);
1015 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1016 SVG.defs[appendChild](el);
1017 for (var i = 0, ii = dots[length]; i < ii; i++) {
1018 var stop = $("stop");
1020 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1021 "stop-color": dots[i].color || "#fff"
1023 el[appendChild](stop);
1026 fill: "url(#" + el.id + ")",
1035 var updatePosition = function (o) {
1036 var bbox = o.getBBox();
1037 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1039 var setFillAndStroke = function (o, params) {
1046 "-..": [3, 1, 1, 1, 1, 1],
1050 "- .": [4, 3, 1, 3],
1051 "--.": [8, 3, 1, 3],
1052 "--..": [8, 3, 1, 3, 1, 3]
1057 addDashes = function (o, value) {
1058 value = dasharray[lowerCase.call(value)];
1060 var width = o.attrs["stroke-width"] || "1",
1061 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1063 var i = value[length];
1065 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1067 $(node, {"stroke-dasharray": dashes[join](",")});
1070 params[has]("rotation") && (rot = params.rotation);
1071 var rotxy = (rot + E)[split](separator);
1072 if (!(rotxy.length - 1)) {
1075 rotxy[1] = +rotxy[1];
1076 rotxy[2] = +rotxy[2];
1078 toFloat(rot) && o.rotate(0, true);
1079 for (var att in params) {
1080 if (params[has](att)) {
1081 if (!availableAttrs[has](att)) {
1084 var value = params[att];
1091 o.rotate(value, true);
1097 var pn = node.parentNode;
1098 if (lowerCase.call(pn.tagName) != "a") {
1100 pn.insertBefore(hl, node);
1101 hl[appendChild](node);
1104 pn.setAttributeNS(o.paper.xlink, att, value);
1107 node.style.cursor = value;
1110 var rect = (value + E)[split](separator);
1111 if (rect[length] == 4) {
1112 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1113 var el = $("clipPath"),
1115 el.id = "r" + (R._id++)[toString](36);
1122 el[appendChild](rc);
1123 o.paper.defs[appendChild](el);
1124 $(node, {"clip-path": "url(#" + el.id + ")"});
1128 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1129 clip && clip.parentNode.removeChild(clip);
1130 $(node, {"clip-path": E});
1135 if (o.type == "path") {
1136 $(node, {d: value ? attrs.path = roundPath(pathToAbsolute(value)) : "M0,0"});
1140 node[setAttribute](att, value);
1149 value = -attrs.x - (attrs.width || 0);
1152 if (att == "rx" && o.type == "rect") {
1156 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1157 node[setAttribute](att, round(value));
1158 o.pattern && updatePosition(o);
1161 node[setAttribute](att, value);
1170 value = -attrs.y - (attrs.height || 0);
1173 if (att == "ry" && o.type == "rect") {
1177 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1178 node[setAttribute](att, round(value));
1179 o.pattern && updatePosition(o);
1182 if (o.type == "rect") {
1183 $(node, {rx: value, ry: value});
1185 node[setAttribute](att, value);
1189 if (o.type == "image") {
1190 node.setAttributeNS(o.paper.xlink, "href", value);
1193 case "stroke-width":
1194 node.style.strokeWidth = value;
1195 // Need following line for Firefox
1196 node[setAttribute](att, value);
1197 if (attrs["stroke-dasharray"]) {
1198 addDashes(o, attrs["stroke-dasharray"]);
1201 case "stroke-dasharray":
1202 addDashes(o, value);
1205 var xy = (value + E)[split](separator);
1206 xy[0] = +xy[0] || 0;
1207 xy[1] = +xy[1] || 0;
1212 translate.call(o, xy[0], xy[1]);
1215 xy = (value + E)[split](separator);
1216 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1219 var isURL = (value + E).match(ISURL);
1222 var ig = $("image");
1223 el.id = "r" + (R._id++)[toString](36);
1224 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1225 $(ig, {x: 0, y: 0});
1226 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1227 el[appendChild](ig);
1229 var img = doc.createElement("img");
1230 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1231 img.onload = function () {
1232 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1233 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1234 doc.body.removeChild(this);
1237 doc.body[appendChild](img);
1239 o.paper.defs[appendChild](el);
1240 node.style.fill = "url(#" + el.id + ")";
1241 $(node, {fill: "url(#" + el.id + ")"});
1243 o.pattern && updatePosition(o);
1246 if (!R.getRGB(value).error) {
1247 delete params.gradient;
1248 delete attrs.gradient;
1249 !R.is(attrs.opacity, "undefined") &&
1250 R.is(params.opacity, "undefined") &&
1251 $(node, {opacity: attrs.opacity});
1252 !R.is(attrs["fill-opacity"], "undefined") &&
1253 R.is(params["fill-opacity"], "undefined") &&
1254 $(node, {"fill-opacity": attrs["fill-opacity"]});
1255 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1256 attrs.gradient = value;
1257 attrs.fill = "none";
1261 node[setAttribute](att, R.getRGB(value).hex);
1264 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1267 case "fill-opacity":
1268 if (attrs.gradient) {
1269 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1271 var stops = gradient.getElementsByTagName("stop");
1272 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1277 att == "font-size" && (value = toInt(value, 10) + "px");
1278 var cssrule = att[rp](/(\-.)/g, function (w) {
1279 return upperCase.call(w.substring(1));
1281 node.style[cssrule] = value;
1282 // Need following line for Firefox
1283 node[setAttribute](att, value);
1289 tuneText(o, params);
1291 o.rotate(rotxy.join(S));
1293 toFloat(rot) && o.rotate(rot, true);
1297 tuneText = function (el, params) {
1298 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1303 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1305 if (params[has]("text")) {
1306 a.text = params.text;
1307 while (node.firstChild) {
1308 node.removeChild(node.firstChild);
1310 var texts = (params.text + E)[split]("\n");
1311 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1312 var tspan = $("tspan");
1313 i && $(tspan, {dy: fontSize * leading, x: a.x});
1314 tspan[appendChild](doc.createTextNode(texts[i]));
1315 node[appendChild](tspan);
1318 texts = node.getElementsByTagName("tspan");
1319 for (i = 0, ii = texts[length]; i < ii; i++) {
1320 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1324 var bb = el.getBBox(),
1325 dif = a.y - (bb.y + bb.height / 2);
1326 dif && isFinite(dif) && $(node, {y: a.y + dif});
1328 Element = function (node, svg) {
1334 node.raphael = this;
1336 this.attrs = this.attrs || {};
1337 this.transformations = []; // rotate, translate, scale
1341 rt: {deg: 0, cx: 0, cy: 0},
1345 !svg.bottom && (svg.bottom = this);
1346 this.prev = svg.top;
1347 svg.top && (svg.top.next = this);
1351 Element[proto].rotate = function (deg, cx, cy) {
1357 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1359 return this._.rt.deg;
1361 var bbox = this.getBBox();
1362 deg = (deg + E)[split](separator);
1363 if (deg[length] - 1) {
1364 cx = toFloat(deg[1]);
1365 cy = toFloat(deg[2]);
1367 deg = toFloat(deg[0]);
1369 this._.rt.deg = deg;
1371 this._.rt.deg += deg;
1373 (cy == null) && (cx = null);
1376 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1377 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1378 if (this._.rt.deg) {
1379 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1380 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1382 this.transformations[0] = E;
1383 this.clip && $(this.clip, {transform: E});
1385 $(this.node, {transform: this.transformations[join](S)});
1388 Element[proto].hide = function () {
1389 !this.removed && (this.node.style.display = "none");
1392 Element[proto].show = function () {
1393 !this.removed && (this.node.style.display = "");
1396 Element[proto].remove = function () {
1400 tear(this, this.paper);
1401 this.node.parentNode.removeChild(this.node);
1402 for (var i in this) {
1405 this.removed = true;
1407 Element[proto].getBBox = function () {
1411 if (this.type == "path") {
1412 return pathDimensions(this.attrs.path);
1414 if (this.node.style.display == "none") {
1420 bbox = this.node.getBBox();
1422 // Firefox 3.0.x plays badly here
1426 if (this.type == "text") {
1427 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1428 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1429 var bb = this.node.getExtentOfChar(i);
1430 (bb.y < bbox.y) && (bbox.y = bb.y);
1431 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1432 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1435 hide && this.hide();
1438 Element[proto].attr = function (name, value) {
1444 for (var i in this.attrs) if (this.attrs[has](i)) {
1445 res[i] = this.attrs[i];
1447 this._.rt.deg && (res.rotation = this.rotate());
1448 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1449 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1452 if (value == null && R.is(name, "string")) {
1453 if (name == "translation") {
1454 return translate.call(this);
1456 if (name == "rotation") {
1457 return this.rotate();
1459 if (name == "scale") {
1460 return this.scale();
1462 if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1463 return this.attrs.gradient;
1465 return this.attrs[name];
1467 if (value == null && R.is(name, "array")) {
1469 for (var j = 0, jj = name.length; j < jj; j++) {
1470 values[name[j]] = this.attr(name[j]);
1474 if (value != null) {
1476 params[name] = value;
1477 setFillAndStroke(this, params);
1478 } else if (name != null && R.is(name, "object")) {
1479 setFillAndStroke(this, name);
1483 Element[proto].toFront = function () {
1487 this.node.parentNode[appendChild](this.node);
1488 var svg = this.paper;
1489 svg.top != this && tofront(this, svg);
1492 Element[proto].toBack = function () {
1496 if (this.node.parentNode.firstChild != this.node) {
1497 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1498 toback(this, this.paper);
1499 var svg = this.paper;
1503 Element[proto].insertAfter = function (element) {
1507 var node = element.node;
1508 if (node.nextSibling) {
1509 node.parentNode.insertBefore(this.node, node.nextSibling);
1511 node.parentNode[appendChild](this.node);
1513 insertafter(this, element, this.paper);
1516 Element[proto].insertBefore = function (element) {
1520 var node = element.node;
1521 node.parentNode.insertBefore(this.node, node);
1522 insertbefore(this, element, this.paper);
1525 Element[proto].blur = function (size) {
1526 // Experimental. No Safari support. Use it on your own risk.
1529 var fltr = $("filter"),
1530 blur = $("feGaussianBlur");
1531 t.attrs.blur = size;
1532 fltr.id = "r" + (R._id++)[toString](36);
1533 $(blur, {stdDeviation: +size || 1.5});
1534 fltr.appendChild(blur);
1535 t.paper.defs.appendChild(fltr);
1537 $(t.node, {filter: "url(#" + fltr.id + ")"});
1540 t._blur.parentNode.removeChild(t._blur);
1542 delete t.attrs.blur;
1544 t.node.removeAttribute("filter");
1547 var theCircle = function (svg, x, y, r) {
1550 var el = $("circle");
1551 svg.canvas && svg.canvas[appendChild](el);
1552 var res = new Element(el, svg);
1553 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1554 res.type = "circle";
1558 var theRect = function (svg, x, y, w, h, r) {
1562 svg.canvas && svg.canvas[appendChild](el);
1563 var res = new Element(el, svg);
1564 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1569 var theEllipse = function (svg, x, y, rx, ry) {
1572 var el = $("ellipse");
1573 svg.canvas && svg.canvas[appendChild](el);
1574 var res = new Element(el, svg);
1575 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1576 res.type = "ellipse";
1580 var theImage = function (svg, src, x, y, w, h) {
1581 var el = $("image");
1582 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1583 el.setAttributeNS(svg.xlink, "href", src);
1584 svg.canvas && svg.canvas[appendChild](el);
1585 var res = new Element(el, svg);
1586 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1590 var theText = function (svg, x, y, text) {
1592 $(el, {x: x, y: y, "text-anchor": "middle"});
1593 svg.canvas && svg.canvas[appendChild](el);
1594 var res = new Element(el, svg);
1595 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1597 setFillAndStroke(res, res.attrs);
1600 var setSize = function (width, height) {
1601 this.width = width || this.width;
1602 this.height = height || this.height;
1603 this.canvas[setAttribute]("width", this.width);
1604 this.canvas[setAttribute]("height", this.height);
1607 var create = function () {
1608 var con = getContainer[apply](0, arguments),
1609 container = con && con.container,
1613 height = con.height;
1615 throw new Error("SVG container not found.");
1617 var cnvs = $("svg");
1618 width = width || 512;
1619 height = height || 342;
1621 xmlns: "http://www.w3.org/2000/svg",
1626 if (container == 1) {
1627 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1628 doc.body[appendChild](cnvs);
1630 if (container.firstChild) {
1631 container.insertBefore(cnvs, container.firstChild);
1633 container[appendChild](cnvs);
1636 container = new Paper;
1637 container.width = width;
1638 container.height = height;
1639 container.canvas = cnvs;
1640 plugins.call(container, container, R.fn);
1644 Paper[proto].clear = function () {
1645 var c = this.canvas;
1646 while (c.firstChild) {
1647 c.removeChild(c.firstChild);
1649 this.bottom = this.top = null;
1650 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1651 c[appendChild](this.desc);
1652 c[appendChild](this.defs = $("defs"));
1654 Paper[proto].remove = function () {
1655 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1656 for (var i in this) {
1657 this[i] = removed(i);
1664 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1665 bites = /([clmz]),?([^clmz]*)/gi,
1666 val = /-?[^,\s-]+/g,
1667 coordsize = 1e3 + S + 1e3,
1669 path2vml = function (path) {
1670 var total = /[ahqstv]/ig,
1671 command = pathToAbsolute;
1672 (path + E).match(total) && (command = path2curve);
1674 if (command == pathToAbsolute && !(path + E).match(total)) {
1675 var res = (path + E)[rp](bites, function (all, command, args) {
1677 isMove = lowerCase.call(command) == "m",
1679 args[rp](val, function (value) {
1680 if (isMove && vals[length] == 2) {
1681 res += vals + map[command == "m" ? "l" : "L"];
1684 vals[push](round(value * zoom));
1690 var pa = command(path), p, r;
1692 for (var i = 0, ii = pa[length]; i < ii; i++) {
1694 r = lowerCase.call(pa[i][0]);
1695 r == "z" && (r = "x");
1696 for (var j = 1, jj = p[length]; j < jj; j++) {
1697 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1701 return res[join](S);
1704 R[toString] = function () {
1705 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1707 thePath = function (pathString, vml) {
1708 var g = createNode("group");
1709 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1710 g.coordsize = vml.coordsize;
1711 g.coordorigin = vml.coordorigin;
1712 var el = createNode("shape"), ol = el.style;
1713 ol.width = vml.width + "px";
1714 ol.height = vml.height + "px";
1715 el.coordsize = coordsize;
1716 el.coordorigin = vml.coordorigin;
1718 var p = new Element(el, g, vml),
1719 attr = {fill: "none", stroke: "#000"};
1720 pathString && (attr.path = pathString);
1721 p.isAbsolute = true;
1725 setFillAndStroke(p, attr);
1726 vml.canvas[appendChild](g);
1729 setFillAndStroke = function (o, params) {
1730 o.attrs = o.attrs || {};
1736 for (var par in params) if (params[has](par)) {
1737 a[par] = params[par];
1739 params.href && (node.href = params.href);
1740 params.title && (node.title = params.title);
1741 params.target && (node.target = params.target);
1742 params.cursor && (s.cursor = params.cursor);
1743 "blur" in params && o.blur(params.blur);
1744 if (params.path && o.type == "path") {
1745 a.path = params.path;
1746 node.path = path2vml(a.path);
1748 if (params.rotation != null) {
1749 o.rotate(params.rotation, true);
1751 if (params.translation) {
1752 xy = (params.translation + E)[split](separator);
1753 translate.call(o, xy[0], xy[1]);
1754 if (o._.rt.cx != null) {
1755 o._.rt.cx +=+ xy[0];
1756 o._.rt.cy +=+ xy[1];
1757 o.setBox(o.attrs, xy[0], xy[1]);
1761 xy = (params.scale + E)[split](separator);
1762 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1764 if ("clip-rect" in params) {
1765 var rect = (params["clip-rect"] + E)[split](separator);
1766 if (rect[length] == 4) {
1767 rect[2] = +rect[2] + (+rect[0]);
1768 rect[3] = +rect[3] + (+rect[1]);
1769 var div = node.clipRect || doc.createElement("div"),
1771 group = node.parentNode;
1772 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1773 if (!node.clipRect) {
1774 dstyle.position = "absolute";
1777 dstyle.width = o.paper.width + "px";
1778 dstyle.height = o.paper.height + "px";
1779 group.parentNode.insertBefore(div, group);
1780 div[appendChild](group);
1781 node.clipRect = div;
1784 if (!params["clip-rect"]) {
1785 node.clipRect && (node.clipRect.style.clip = E);
1788 if (o.type == "image" && params.src) {
1789 node.src = params.src;
1791 if (o.type == "image" && params.opacity) {
1792 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1793 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1795 params.font && (s.font = params.font);
1796 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1797 params["font-size"] && (s.fontSize = params["font-size"]);
1798 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1799 params["font-style"] && (s.fontStyle = params["font-style"]);
1800 if (params.opacity != null ||
1801 params["stroke-width"] != null ||
1802 params.fill != null ||
1803 params.stroke != null ||
1804 params["stroke-width"] != null ||
1805 params["stroke-opacity"] != null ||
1806 params["fill-opacity"] != null ||
1807 params["stroke-dasharray"] != null ||
1808 params["stroke-miterlimit"] != null ||
1809 params["stroke-linejoin"] != null ||
1810 params["stroke-linecap"] != null) {
1811 node = o.shape || node;
1812 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1814 !fill && (newfill = fill = createNode("fill"));
1815 if ("fill-opacity" in params || "opacity" in params) {
1816 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1817 opacity < 0 && (opacity = 0);
1818 opacity > 1 && (opacity = 1);
1819 fill.opacity = opacity;
1821 params.fill && (fill.on = true);
1822 if (fill.on == null || params.fill == "none") {
1825 if (fill.on && params.fill) {
1826 var isURL = params.fill.match(ISURL);
1828 fill.src = isURL[1];
1831 fill.color = R.getRGB(params.fill).hex;
1833 fill.type = "solid";
1834 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1836 a.gradient = params.fill;
1840 newfill && node[appendChild](fill);
1841 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1843 !stroke && (newstroke = stroke = createNode("stroke"));
1844 if ((params.stroke && params.stroke != "none") ||
1845 params["stroke-width"] ||
1846 params["stroke-opacity"] != null ||
1847 params["stroke-dasharray"] ||
1848 params["stroke-miterlimit"] ||
1849 params["stroke-linejoin"] ||
1850 params["stroke-linecap"]) {
1853 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1854 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1855 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1856 var width = (toFloat(params["stroke-width"]) || 1) * .75;
1857 opacity < 0 && (opacity = 0);
1858 opacity > 1 && (opacity = 1);
1859 params["stroke-width"] == null && (width = a["stroke-width"]);
1860 params["stroke-width"] && (stroke.weight = width);
1861 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1862 stroke.opacity = opacity;
1864 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1865 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1866 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1867 if (params["stroke-dasharray"]) {
1871 "-.": "shortdashdot",
1872 "-..": "shortdashdotdot",
1877 "--.": "longdashdot",
1878 "--..": "longdashdotdot"
1880 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1882 newstroke && node[appendChild](stroke);
1884 if (res.type == "text") {
1885 s = res.paper.span.style;
1886 a.font && (s.font = a.font);
1887 a["font-family"] && (s.fontFamily = a["font-family"]);
1888 a["font-size"] && (s.fontSize = a["font-size"]);
1889 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1890 a["font-style"] && (s.fontStyle = a["font-style"]);
1891 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1892 res.W = a.w = res.paper.span.offsetWidth;
1893 res.H = a.h = res.paper.span.offsetHeight;
1895 res.Y = a.y + round(res.H / 2);
1897 // text-anchor emulationm
1898 switch (a["text-anchor"]) {
1900 res.node.style["v-text-align"] = "left";
1901 res.bbx = round(res.W / 2);
1904 res.node.style["v-text-align"] = "right";
1905 res.bbx = -round(res.W / 2);
1908 res.node.style["v-text-align"] = "center";
1913 addGradientFill = function (o, gradient) {
1914 o.attrs = o.attrs || {};
1915 var attrs = o.attrs,
1916 fill = o.node.getElementsByTagName("fill"),
1919 o.attrs.gradient = gradient;
1920 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1925 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1930 gradient = gradient[split](/\s*\-\s*/);
1931 if (type == "linear") {
1932 var angle = gradient.shift();
1933 angle = -toFloat(angle);
1938 var dots = parseDots(gradient);
1942 o = o.shape || o.node;
1943 fill = fill[0] || createNode("fill");
1946 fill.method = "none";
1947 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1948 fill.color = dots[0].color;
1949 fill.color2 = dots[dots[length] - 1].color;
1951 for (var i = 0, ii = dots[length]; i < ii; i++) {
1952 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1954 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1955 if (type == "radial") {
1956 fill.focus = "100%";
1957 fill.focussize = fxfy;
1958 fill.focusposition = fxfy;
1960 fill.angle = (270 - angle) % 360;
1965 Element = function (node, group, vml) {
1973 node.raphael = this;
1986 !vml.bottom && (vml.bottom = this);
1987 this.prev = vml.top;
1988 vml.top && (vml.top.next = this);
1992 Element[proto].rotate = function (deg, cx, cy) {
1998 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2000 return this._.rt.deg;
2002 deg = (deg + E)[split](separator);
2003 if (deg[length] - 1) {
2004 cx = toFloat(deg[1]);
2005 cy = toFloat(deg[2]);
2007 deg = toFloat(deg[0]);
2009 this._.rt.deg = deg;
2011 this._.rt.deg += deg;
2013 cy == null && (cx = null);
2016 this.setBox(this.attrs, cx, cy);
2017 this.Group.style.rotation = this._.rt.deg;
2018 // gradient fix for rotation. TODO
2019 // var fill = (this.shape || this.node).getElementsByTagName("fill");
2020 // fill = fill[0] || {};
2021 // var b = ((360 - this._.rt.deg) - 270) % 360;
2022 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2025 Element[proto].setBox = function (params, cx, cy) {
2029 var gs = this.Group.style,
2030 os = (this.shape && this.shape.style) || this.node.style;
2031 params = params || {};
2032 for (var i in params) if (params[has](i)) {
2033 this.attrs[i] = params[i];
2035 cx = cx || this._.rt.cx;
2036 cy = cy || this._.rt.cy;
2037 var attr = this.attrs,
2042 switch (this.type) {
2044 x = attr.cx - attr.r;
2045 y = attr.cy - attr.r;
2049 x = attr.cx - attr.rx;
2050 y = attr.cy - attr.ry;
2058 w = attr.width || 0;
2059 h = attr.height || 0;
2062 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2063 x = attr.x - round(this.W / 2);
2064 y = attr.y - this.H / 2;
2069 if (!this.attrs.path) {
2072 w = this.paper.width;
2073 h = this.paper.height;
2075 var dim = pathDimensions(this.attrs.path);
2085 w = this.paper.width;
2086 h = this.paper.height;
2089 cx = (cx == null) ? x + w / 2 : cx;
2090 cy = (cy == null) ? y + h / 2 : cy;
2091 var left = cx - this.paper.width / 2,
2092 top = cy - this.paper.height / 2, t;
2093 gs.left != (t = left + "px") && (gs.left = t);
2094 gs.top != (t = top + "px") && (gs.top = t);
2095 this.X = this.type == "path" ? -left : x;
2096 this.Y = this.type == "path" ? -top : y;
2099 if (this.type == "path") {
2100 os.left != (t = -left * zoom + "px") && (os.left = t);
2101 os.top != (t = -top * zoom + "px") && (os.top = t);
2102 } else if (this.type == "text") {
2103 os.left != (t = -left + "px") && (os.left = t);
2104 os.top != (t = -top + "px") && (os.top = t);
2106 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2107 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2108 os.left != (t = x - left + "px") && (os.left = t);
2109 os.top != (t = y - top + "px") && (os.top = t);
2110 os.width != (t = w + "px") && (os.width = t);
2111 os.height != (t = h + "px") && (os.height = t);
2112 var arcsize = (+params.r || 0) / mmin(w, h);
2113 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2114 // We should replace element with the new one
2115 var o = createNode("roundrect"),
2117 ii = this.events && this.events[length];
2119 o.arcsize = arcsize;
2121 this.Group[appendChild](o);
2122 this.Group.removeChild(this.node);
2123 this[0] = this.node = o;
2124 this.arcsize = arcsize;
2130 if (this.events) for (; i < ii; i++) {
2131 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2136 Element[proto].hide = function () {
2137 !this.removed && (this.Group.style.display = "none");
2140 Element[proto].show = function () {
2141 !this.removed && (this.Group.style.display = "block");
2144 Element[proto].getBBox = function () {
2148 if (this.type == "path") {
2149 return pathDimensions(this.attrs.path);
2152 x: this.X + (this.bbx || 0),
2158 Element[proto].remove = function () {
2162 tear(this, this.paper);
2163 this.node.parentNode.removeChild(this.node);
2164 this.Group.parentNode.removeChild(this.Group);
2165 this.shape && this.shape.parentNode.removeChild(this.shape);
2166 for (var i in this) {
2169 this.removed = true;
2171 Element[proto].attr = function (name, value) {
2177 for (var i in this.attrs) if (this.attrs[has](i)) {
2178 res[i] = this.attrs[i];
2180 this._.rt.deg && (res.rotation = this.rotate());
2181 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2182 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2185 if (value == null && R.is(name, "string")) {
2186 if (name == "translation") {
2187 return translate.call(this);
2189 if (name == "rotation") {
2190 return this.rotate();
2192 if (name == "scale") {
2193 return this.scale();
2195 if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2196 return this.attrs.gradient;
2198 return this.attrs[name];
2200 if (this.attrs && value == null && R.is(name, "array")) {
2201 var ii, values = {};
2202 for (i = 0, ii = name[length]; i < ii; i++) {
2203 values[name[i]] = this.attr(name[i]);
2208 if (value != null) {
2210 params[name] = value;
2212 value == null && R.is(name, "object") && (params = name);
2214 if (params.text && this.type == "text") {
2215 this.node.string = params.text;
2217 setFillAndStroke(this, params);
2218 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2219 addGradientFill(this, params.gradient);
2221 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2225 Element[proto].toFront = function () {
2226 !this.removed && this.Group.parentNode[appendChild](this.Group);
2227 this.paper.top != this && tofront(this, this.paper);
2230 Element[proto].toBack = function () {
2234 if (this.Group.parentNode.firstChild != this.Group) {
2235 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2236 toback(this, this.paper);
2240 Element[proto].insertAfter = function (element) {
2244 if (element.Group.nextSibling) {
2245 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2247 element.Group.parentNode[appendChild](this.Group);
2249 insertafter(this, element, this.paper);
2252 Element[proto].insertBefore = function (element) {
2256 element.Group.parentNode.insertBefore(this.Group, element.Group);
2257 insertbefore(this, element, this.paper);
2260 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2261 Element[proto].blur = function (size) {
2262 var s = this.node.style,
2264 f = f.replace(blurregexp, "");
2266 this.attrs.blur = size;
2267 s.filter = f + " progid:DXImageTransform.Microsoft.Blur(pixelradius=" + (+size || 1.5) + ")";
2268 s.margin = Raphael.format("-{0}px 0 0 -{0}px", Math.round(+size || 1.5));
2272 delete this.attrs.blur;
2276 theCircle = function (vml, x, y, r) {
2277 var g = createNode("group"),
2278 o = createNode("oval"),
2280 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2281 g.coordsize = coordsize;
2282 g.coordorigin = vml.coordorigin;
2284 var res = new Element(o, g, vml);
2285 res.type = "circle";
2286 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2290 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2291 vml.canvas[appendChild](g);
2294 theRect = function (vml, x, y, w, h, r) {
2295 var g = createNode("group"),
2296 o = createNode("roundrect"),
2297 arcsize = (+r || 0) / (mmin(w, h));
2298 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2299 g.coordsize = coordsize;
2300 g.coordorigin = vml.coordorigin;
2302 o.arcsize = arcsize;
2303 var res = new Element(o, g, vml);
2305 setFillAndStroke(res, {stroke: "#000"});
2306 res.arcsize = arcsize;
2307 res.setBox({x: x, y: y, width: w, height: h, r: r});
2308 vml.canvas[appendChild](g);
2311 theEllipse = function (vml, x, y, rx, ry) {
2312 var g = createNode("group"),
2313 o = createNode("oval"),
2315 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2316 g.coordsize = coordsize;
2317 g.coordorigin = vml.coordorigin;
2319 var res = new Element(o, g, vml);
2320 res.type = "ellipse";
2321 setFillAndStroke(res, {stroke: "#000"});
2326 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2327 vml.canvas[appendChild](g);
2330 theImage = function (vml, src, x, y, w, h) {
2331 var g = createNode("group"),
2332 o = createNode("image"),
2334 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2335 g.coordsize = coordsize;
2336 g.coordorigin = vml.coordorigin;
2339 var res = new Element(o, g, vml);
2341 res.attrs.src = src;
2346 res.setBox({x: x, y: y, width: w, height: h});
2347 vml.canvas[appendChild](g);
2350 theText = function (vml, x, y, text) {
2351 var g = createNode("group"),
2352 el = createNode("shape"),
2354 path = createNode("path"),
2356 o = createNode("textpath");
2357 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2358 g.coordsize = coordsize;
2359 g.coordorigin = vml.coordorigin;
2360 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2361 path.textpathok = true;
2362 ol.width = vml.width;
2363 ol.height = vml.height;
2364 o.string = text + E;
2367 el[appendChild](path);
2369 var res = new Element(o, g, vml);
2371 res.textpath = path;
2373 res.attrs.text = text;
2378 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2380 vml.canvas[appendChild](g);
2383 setSize = function (width, height) {
2384 var cs = this.canvas.style;
2385 width == +width && (width += "px");
2386 height == +height && (height += "px");
2389 cs.clip = "rect(0 " + width + " " + height + " 0)";
2393 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2395 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2396 createNode = function (tagName) {
2397 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2400 createNode = function (tagName) {
2401 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2404 create = function () {
2405 var con = getContainer[apply](0, arguments),
2406 container = con.container,
2407 height = con.height,
2413 throw new Error("VML container not found.");
2415 var res = new Paper,
2416 c = res.canvas = doc.createElement("div"),
2418 width = width || 512;
2419 height = height || 342;
2420 width == +width && (width += "px");
2421 height == +height && (height += "px");
2424 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2425 res.coordorigin = "0 0";
2426 res.span = doc.createElement("span");
2427 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2428 c[appendChild](res.span);
2429 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2430 if (container == 1) {
2431 doc.body[appendChild](c);
2435 container.style.width = width;
2436 container.style.height = height;
2437 if (container.firstChild) {
2438 container.insertBefore(c, container.firstChild);
2440 container[appendChild](c);
2443 plugins.call(res, res, R.fn);
2446 Paper[proto].clear = function () {
2447 this.canvas.innerHTML = E;
2448 this.span = doc.createElement("span");
2449 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2450 this.canvas[appendChild](this.span);
2451 this.bottom = this.top = null;
2453 Paper[proto].remove = function () {
2454 this.canvas.parentNode.removeChild(this.canvas);
2455 for (var i in this) {
2456 this[i] = removed(i);
2463 // Safari or Chrome (WebKit) rendering bug workaround method
2464 if ((/^Apple|^Google/).test(win.navigator.vendor) && !(win.navigator.userAgent.indexOf("Version/4.0") + 1)) {
2465 Paper[proto].safari = function () {
2466 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2467 win.setTimeout(function () {rect.remove();});
2470 Paper[proto].safari = function () {};
2474 var addEvent = (function () {
2475 if (doc.addEventListener) {
2476 return function (obj, type, fn, element) {
2477 var f = function (e) {
2478 return fn.call(element, e);
2480 obj.addEventListener(type, f, false);
2481 return function () {
2482 obj.removeEventListener(type, f, false);
2486 } else if (doc.attachEvent) {
2487 return function (obj, type, fn, element) {
2488 var f = function (e) {
2489 return fn.call(element, e || win.event);
2491 obj.attachEvent("on" + type, f);
2492 var detacher = function () {
2493 obj.detachEvent("on" + type, f);
2500 for (var i = events[length]; i--;) {
2501 (function (eventName) {
2502 Element[proto][eventName] = function (fn) {
2503 if (R.is(fn, "function")) {
2504 this.events = this.events || [];
2505 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2509 Element[proto]["un" + eventName] = function (fn) {
2510 var events = this.events,
2512 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2514 events.splice(l, 1);
2515 !events.length && delete this.events;
2522 Element[proto].hover = function (f_in, f_out) {
2523 return this.mouseover(f_in).mouseout(f_out);
2525 Element[proto].unhover = function (f_in, f_out) {
2526 return this.unmouseover(f_in).unmouseout(f_out);
2528 Paper[proto].circle = function (x, y, r) {
2529 return theCircle(this, x || 0, y || 0, r || 0);
2531 Paper[proto].rect = function (x, y, w, h, r) {
2532 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2534 Paper[proto].ellipse = function (x, y, rx, ry) {
2535 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2537 Paper[proto].path = function (pathString) {
2538 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2539 return thePath(R.format[apply](R, arguments), this);
2541 Paper[proto].image = function (src, x, y, w, h) {
2542 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2544 Paper[proto].text = function (x, y, text) {
2545 return theText(this, x || 0, y || 0, text || E);
2547 Paper[proto].set = function (itemsArray) {
2548 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2549 return new Set(itemsArray);
2551 Paper[proto].setSize = setSize;
2552 Paper[proto].top = Paper[proto].bottom = null;
2553 Paper[proto].raphael = R;
2555 return this.x + S + this.y;
2557 Element[proto].scale = function (x, y, cx, cy) {
2558 if (x == null && y == null) {
2573 var bb = this.getBBox(),
2574 rcx = bb.x + bb.width / 2,
2575 rcy = bb.y + bb.height / 2,
2578 cx = (+cx || cx == 0) ? cx : rcx;
2579 cy = (+cy || cy == 0) ? cy : rcy;
2580 var dirx = ~~(x / math.abs(x)),
2581 diry = ~~(y / math.abs(y)),
2582 s = this.node.style,
2583 ncx = cx + (rcx - cx) * kx,
2584 ncy = cy + (rcy - cy) * ky;
2585 switch (this.type) {
2588 var neww = a.width * dirx * kx,
2589 newh = a.height * diry * ky;
2592 r: a.r * mmin(dirx * kx, diry * ky),
2601 rx: a.rx * dirx * kx,
2602 ry: a.ry * diry * ky,
2603 r: a.r * mmin(dirx * kx, diry * ky),
2609 var path = pathToRelative(a.path),
2611 for (var i = 0, ii = path[length]; i < ii; i++) {
2613 P0 = upperCase.call(p[0]);
2614 if (P0 == "M" && skip) {
2620 p[path[i][length] - 2] *= kx;
2621 p[path[i][length] - 1] *= ky;
2624 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2625 } else if (P0 == "H") {
2626 for (var j = 1, jj = p[length]; j < jj; j++) {
2629 } else if (P0 == "V") {
2630 for (j = 1, jj = p[length]; j < jj; j++) {
2634 for (j = 1, jj = p[length]; j < jj; j++) {
2635 p[j] *= (j % 2) ? kx : ky;
2639 var dim2 = pathDimensions(path);
2640 dx = ncx - dim2.x - dim2.width / 2;
2641 dy = ncy - dim2.y - dim2.height / 2;
2644 this.attr({path: path});
2647 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2648 if (this.transformations) {
2649 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2650 this.node[setAttribute]("transform", this.transformations[join](S));
2651 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2652 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2653 this.attr({x: dx, y: dy});
2657 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2658 ", M12=0, M21=0, M22=", diry,
2659 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2660 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2663 if (this.transformations) {
2664 this.transformations[2] = E;
2665 this.node[setAttribute]("transform", this.transformations[join](S));
2669 this.node.filterMatrix = E;
2670 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2673 a.scale = [x, y, cx, cy][join](S);
2679 Element[proto].clone = function () {
2680 var attr = this.attr();
2682 delete attr.translation;
2683 return this.paper[this.type]().attr(attr);
2685 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2688 for (var i = 0; i < 1.001; i+=.001) {
2689 var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2690 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2691 if (len >= length) {
2697 getLengthFactory = function (istotal, subpath) {
2698 return function (path, length, onlystart) {
2699 path = path2curve(path);
2700 var x, y, p, l, sp = "", subpaths = {}, point,
2702 for (var i = 0, ii = path.length; i < ii; i++) {
2708 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2709 if (len + l > length) {
2710 if (subpath && !subpaths.start) {
2711 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2712 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2713 if (onlystart) {return sp;}
2714 subpaths.start = sp;
2715 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2721 if (!istotal && !subpath) {
2722 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2723 return {x: point.x, y: point.y, alpha: point.alpha};
2733 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2734 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2738 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2739 var old = {x: 0, y: 0},
2741 for (var i = 0; i < 1.01; i+=.01) {
2742 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2743 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2748 var getTotalLength = getLengthFactory(1),
2749 getPointAtLength = getLengthFactory(),
2750 getSubpathsAtLength = getLengthFactory(0, 1);
2751 Element[proto].getTotalLength = function () {
2752 if (this.type != "path") {return;}
2753 return getTotalLength(this.attrs.path);
2755 Element[proto].getPointAtLength = function (length) {
2756 if (this.type != "path") {return;}
2757 return getPointAtLength(this.attrs.path, length);
2759 Element[proto].getSubpath = function (from, to) {
2760 if (this.type != "path") {return;}
2761 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2762 return getSubpathsAtLength(this.attrs.path, from).end;
2764 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2765 return from ? getSubpathsAtLength(a, from).end : a;
2768 // animation easing formulas
2769 R.easing_formulas = {
2770 linear: function (n) {
2777 return pow(n - 1, 3) + 1;
2779 "<>": function (n) {
2782 return pow(n, 3) / 2;
2785 return (pow(n, 3) + 2) / 2;
2787 backIn: function (n) {
2789 return n * n * ((s + 1) * n - s);
2791 backOut: function (n) {
2794 return n * n * ((s + 1) * n + s) + 1;
2796 elastic: function (n) {
2797 if (n == 0 || n == 1) {
2802 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2804 bounce: function (n) {
2813 l = s * n * n + .75;
2815 if (n < (2.5 / p)) {
2817 l = s * n * n + .9375;
2820 l = s * n * n + .984375;
2828 var animationElements = {length : 0},
2829 animation = function () {
2830 var Now = +new Date;
2831 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2832 var e = animationElements[l];
2833 if (e.stop || e.el.removed) {
2834 delete animationElements[l];
2835 animationElements[length]--;
2838 var time = Now - e.start,
2847 callback = e.callback,
2851 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2852 for (var attr in from) if (from[has](attr)) {
2853 switch (availableAnimAttrs[attr]) {
2855 now = pos * ms * diff[attr];
2856 to.back && (now = to.len - now);
2857 var point = getPointAtLength(to[attr], now);
2858 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2861 that.translate(point.x - diff.sx, point.y - diff.sy);
2862 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2865 now = +from[attr] + pos * ms * diff[attr];
2869 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2870 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2871 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2876 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2877 now[i] = [from[attr][i][0]];
2878 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2879 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2881 now[i] = now[i][join](S);
2888 var x = diff[attr][0] * (time - prev),
2889 y = diff[attr][1] * (time - prev);
2895 now = +from[attr][0] + pos * ms * diff[attr][0];
2896 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2899 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);
2905 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2914 that._run && that._run.call(that);
2917 point = getPointAtLength(to.along, to.len * !to.back);
2918 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
2919 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2921 (t.x || t.y) && that.translate(-t.x, -t.y);
2922 to.scale && (to.scale = to.scale + E);
2924 delete animationElements[l];
2925 animationElements[length]--;
2926 that.in_animation = null;
2927 R.is(callback, "function") && callback.call(that);
2931 R.svg && that && that.paper.safari();
2932 animationElements[length] && win.setTimeout(animation);
2934 upto255 = function (color) {
2935 return color > 255 ? 255 : (color < 0 ? 0 : color);
2937 translate = function (x, y) {
2939 return {x: this._.tx, y: this._.ty, toString: x_y};
2943 switch (this.type) {
2946 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2951 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2954 var path = pathToRelative(this.attrs.path);
2957 this.attr({path: path});
2962 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2963 animationElements[element.id] && (params.start = animationElements[element.id].start);
2964 return this.animate(params, ms, easing, callback);
2966 Element[proto].animateAlong = along();
2967 Element[proto].animateAlongBack = along(1);
2968 function along(isBack) {
2969 return function (path, ms, rotate, callback) {
2970 var params = {back: isBack};
2971 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
2972 path && path.constructor == Element && (path = path.attrs.path);
2973 path && (params.along = path);
2974 return this.animate(params, ms, callback);
2977 Element[proto].onAnimation = function (f) {
2981 Element[proto].animate = function (params, ms, easing, callback) {
2982 if (R.is(easing, "function") || !easing) {
2983 callback = easing || null;
2988 for (var attr in params) if (params[has](attr)) {
2989 if (availableAnimAttrs[has](attr)) {
2990 from[attr] = this.attr(attr);
2991 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2992 to[attr] = params[attr];
2993 switch (availableAnimAttrs[attr]) {
2995 var len = getTotalLength(params[attr]),
2996 point = getPointAtLength(params[attr], len * !!params.back),
2997 bb = this.getBBox();
2998 diff[attr] = len / ms;
3003 to.rot = params.rot;
3004 to.back = params.back;
3006 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3009 diff[attr] = (to[attr] - from[attr]) / ms;
3012 from[attr] = R.getRGB(from[attr]);
3013 var toColour = R.getRGB(to[attr]);
3015 r: (toColour.r - from[attr].r) / ms,
3016 g: (toColour.g - from[attr].g) / ms,
3017 b: (toColour.b - from[attr].b) / ms
3021 var pathes = path2curve(from[attr], to[attr]);
3022 from[attr] = pathes[0];
3023 var toPath = pathes[1];
3025 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3026 diff[attr][i] = [0];
3027 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3028 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3033 var values = (params[attr] + E)[split](separator),
3034 from2 = (from[attr] + E)[split](separator);
3037 from[attr] = [0, 0];
3038 diff[attr] = [values[0] / ms, values[1] / ms];
3041 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3042 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3045 params[attr] = values;
3046 from[attr] = (from[attr] + E)[split](separator);
3047 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3050 from[attr] = (from[attr] + E)[split](separator);
3054 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3063 this.in_animation = 1;
3064 animationElements[this.id] = {
3065 start: params.start || +new Date,
3075 ++animationElements[length] == 1 && animation();
3078 Element[proto].stop = function () {
3079 animationElements[this.id] && animationElements[length]--;
3080 delete animationElements[this.id];
3083 Element[proto].translate = function (x, y) {
3084 return this.attr({translation: x + " " + y});
3086 Element[proto][toString] = function () {
3087 return "Rapha\xebl\u2019s object";
3089 R.ae = animationElements;
3092 var Set = function (items) {
3096 for (var i = 0, ii = items[length]; i < ii; i++) {
3097 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3098 this[this.items[length]] = this.items[this.items[length]] = items[i];
3104 Set[proto][push] = function () {
3107 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3108 item = arguments[i];
3109 if (item && (item.constructor == Element || item.constructor == Set)) {
3110 len = this.items[length];
3111 this[len] = this.items[len] = item;
3117 Set[proto].pop = function () {
3118 delete this[this[length]--];
3119 return this.items.pop();
3121 for (var method in Element[proto]) if (Element[proto][has](method)) {
3122 Set[proto][method] = (function (methodname) {
3123 return function () {
3124 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3125 this.items[i][methodname][apply](this.items[i], arguments);
3131 Set[proto].attr = function (name, value) {
3132 if (name && R.is(name, "array") && R.is(name[0], "object")) {
3133 for (var j = 0, jj = name[length]; j < jj; j++) {
3134 this.items[j].attr(name[j]);
3137 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3138 this.items[i].attr(name, value);
3143 Set[proto].animate = function (params, ms, easing, callback) {
3144 (R.is(easing, "function") || !easing) && (callback = easing || null);
3145 var len = this.items[length],
3149 callback && (collector = function () {
3150 !--len && callback.call(set);
3152 this.items[--i].animate(params, ms, easing || collector, collector);
3154 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3158 Set[proto].insertAfter = function (el) {
3159 var i = this.items[length];
3161 this.items[i].insertAfter(el);
3165 Set[proto].getBBox = function () {
3170 for (var i = this.items[length]; i--;) {
3171 var box = this.items[i].getBBox();
3174 w[push](box.x + box.width);
3175 h[push](box.y + box.height);
3177 x = mmin[apply](0, x);
3178 y = mmin[apply](0, y);
3182 width: mmax[apply](0, w) - x,
3183 height: mmax[apply](0, h) - y
3186 Set[proto].clone = function (s) {
3188 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3189 s[push](this.items[i].clone());
3194 R.registerFont = function (font) {
3198 this.fonts = this.fonts || {};
3204 family = font.face["font-family"];
3205 for (var prop in font.face) if (font.face[has](prop)) {
3206 fontcopy.face[prop] = font.face[prop];
3208 if (this.fonts[family]) {
3209 this.fonts[family][push](fontcopy);
3211 this.fonts[family] = [fontcopy];
3214 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3215 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3216 var path = font.glyphs[glyph];
3217 fontcopy.glyphs[glyph] = {
3220 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3221 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3225 for (var k in path.k) if (path[has](k)) {
3226 fontcopy.glyphs[glyph].k[k] = path.k[k];
3233 Paper[proto].getFont = function (family, weight, style, stretch) {
3234 stretch = stretch || "normal";
3235 style = style || "normal";
3236 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3237 var font = R.fonts[family];
3239 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3240 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3241 if (name.test(fontName)) {
3242 font = R.fonts[fontName];
3249 for (var i = 0, ii = font[length]; i < ii; i++) {
3251 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3258 Paper[proto].print = function (x, y, string, font, size, origin) {
3259 origin = origin || "middle"; // baseline|middle
3260 var out = this.set(),
3261 letters = (string + E)[split](E),
3265 R.is(font, "string") && (font = this.getFont(font));
3267 scale = (size || 16) / font.face["units-per-em"];
3268 var bb = font.face.bbox.split(separator),
3270 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3271 for (var i = 0, ii = letters[length]; i < ii; i++) {
3272 var prev = i && font.glyphs[letters[i - 1]] || {},
3273 curr = font.glyphs[letters[i]];
3274 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3275 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3277 out.scale(scale, scale, top, height).translate(x - top, y - height);
3282 var formatrg = /\{(\d+)\}/g;
3283 R.format = function (token, array) {
3284 var args = R.is(array, "array") ? [0][concat](array) : arguments;
3285 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3286 return args[++i] == null ? E : args[i];
3290 R.ninja = function () {
3291 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3294 R.el = Element[proto];