2 * Raphael 1.3.0dev - JavaScript Vector Library
4 * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 window.Raphael = (function () {
10 var separator = /[, ]+/,
11 elements = /^(circle|rect|path|ellipse|text|image)$/,
15 was: "Raphael" in win,
19 if (R.is(arguments[0], "array")) {
21 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
23 for (var i = 0, ii = a[length]; i < ii; i++) {
25 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
29 return create[apply](R, arguments);
31 Paper = function () {},
32 appendChild = "appendChild",
38 events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
39 has = "hasOwnProperty",
43 lowerCase = String[proto].toLowerCase,
48 toString = "toString",
49 objectToString = Object[proto][toString],
54 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
55 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
57 setAttribute = "setAttribute",
60 upperCase = String[proto].toUpperCase,
61 availableAttrs = {"clip-rect": "0 0 10e9 10e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
62 availableAnimAttrs = {"clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
64 R.version = "1.3.0dev";
65 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
66 if (R.type == "VML") {
67 var d = document.createElement("div");
68 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
69 if (d.childNodes[length] != 2) {
73 R.svg = !(R.vml = R.type == "VML");
74 Paper[proto] = R[proto];
78 R.is = function (o, type) {
79 type = lowerCase.call(type);
80 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
82 R.setWindow = function (newwin) {
87 var toHex = function (color) {
89 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
90 var trim = /^\s+|\s+$/g;
91 toHex = cacher(function (color) {
93 color = (color + E)[rp](trim, E);
95 var docum = new ActiveXObject("htmlfile");
96 docum.write("<body>");
100 bod = createPopup().document.body;
102 var range = bod.createTextRange();
104 bod.style.color = color;
105 var value = range.queryCommandValue("ForeColor");
106 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
107 return "#" + ("000000" + value[toString](16)).slice(-6);
113 var i = doc.createElement("i");
114 i.title = "Rapha\xebl Colour Picker";
115 i.style.display = "none";
116 doc.body[appendChild](i);
117 toHex = cacher(function (color) {
118 i.style.color = color;
119 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
124 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
125 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
133 if (brightness == 0) {
134 return {r: 0, g: 0, b: 0, hex: "#000"};
136 if (hue > 1 || saturation > 1 || brightness > 1) {
143 p = brightness * (1 - saturation),
144 q = brightness * (1 - (saturation * f)),
145 t = brightness * (1 - (saturation * (1 - f)));
146 red = [brightness, q, p, p, t, brightness, brightness][i];
147 green = [t, brightness, brightness, q, p, p, t][i];
148 blue = [p, p, t, brightness, brightness, q, p][i];
152 var rgb = {r: red, g: green, b: blue},
153 r = (~~red)[toString](16),
154 g = (~~green)[toString](16),
155 b = (~~blue)[toString](16);
159 rgb.hex = "#" + r + g + b;
162 R.rgb2hsb = cacher(function (red, green, blue) {
163 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
168 if (R.is(red, "string")) {
169 var clr = R.getRGB(red);
174 if (red > 1 || green > 1 || blue > 1) {
179 var max = mmax(red, green, blue),
180 min = mmin(red, green, blue),
185 return {h: 0, s: 0, b: max};
187 var delta = (max - min);
188 saturation = delta / max;
190 hue = (green - blue) / delta;
191 } else if (green == max) {
192 hue = 2 + ((blue - red) / delta);
194 hue = 4 + ((red - green) / delta);
200 return {h: hue, s: saturation, b: brightness};
202 var p2s = /,?([achlmqrstvxz]),?/gi;
203 R._path2string = function () {
204 return this.join(",")[rp](p2s, "$1");
206 function cacher(f, scope, postprocessor) {
208 var arg = Array[proto].slice.call(arguments, 0),
209 args = arg[join]("\u25ba"),
210 cache = newf.cache = newf.cache || {},
211 count = newf.count = newf.count || [];
212 if (cache[has](args)) {
213 return postprocessor ? postprocessor(cache[args]) : cache[args];
215 count[length] >= 1e3 && delete cache[count.shift()];
217 cache[args] = f[apply](scope, arg);
218 return postprocessor ? postprocessor(cache[args]) : cache[args];
223 R.getRGB = cacher(function (colour) {
224 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
225 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
227 if (colour == "none") {
228 return {r: -1, g: -1, b: -1, hex: "none"};
230 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
236 rgb = colour.match(colourRegExp);
239 blue = toInt(rgb[2].substring(5), 16);
240 green = toInt(rgb[2].substring(3, 5), 16);
241 red = toInt(rgb[2].substring(1, 3), 16);
244 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
245 green = toInt((t = rgb[3].charAt(2)) + t, 16);
246 red = toInt((t = rgb[3].charAt(1)) + t, 16);
249 rgb = rgb[4][split](/\s*,\s*/);
250 red = toFloat(rgb[0]);
251 green = toFloat(rgb[1]);
252 blue = toFloat(rgb[2]);
255 rgb = rgb[5][split](/\s*,\s*/);
256 red = toFloat(rgb[0]) * 2.55;
257 green = toFloat(rgb[1]) * 2.55;
258 blue = toFloat(rgb[2]) * 2.55;
261 rgb = rgb[6][split](/\s*,\s*/);
262 red = toFloat(rgb[0]);
263 green = toFloat(rgb[1]);
264 blue = toFloat(rgb[2]);
265 return R.hsb2rgb(red, green, blue);
268 rgb = rgb[7][split](/\s*,\s*/);
269 red = toFloat(rgb[0]) * 2.55;
270 green = toFloat(rgb[1]) * 2.55;
271 blue = toFloat(rgb[2]) * 2.55;
272 return R.hsb2rgb(red, green, blue);
274 rgb = {r: red, g: green, b: blue};
275 var r = (~~red)[toString](16),
276 g = (~~green)[toString](16),
277 b = (~~blue)[toString](16);
281 rgb.hex = "#" + r + g + b;
284 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
286 R.getColor = function (value) {
287 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
288 rgb = this.hsb2rgb(start.h, start.s, start.b);
293 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
297 R.getColor.reset = function () {
301 R.parsePathString = cacher(function (pathString) {
305 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
307 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
308 data = pathClone(pathString);
311 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
313 name = lowerCase.call(b);
314 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
315 b && params[push](+b);
317 while (params[length] >= paramCounts[name]) {
318 data[push]([b][concat](params.splice(0, paramCounts[name])));
319 if (!paramCounts[name]) {
325 data[toString] = R._path2string;
328 R.findDotsAtSegment = cacher(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 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}};
342 var pathDimensions = cacher(function (path) {
344 return {x: 0, y: 0, width: 0, height: 0};
346 path = path2curve(path);
352 for (var i = 0, ii = path[length]; i < ii; i++) {
360 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
361 X = X[concat](dim.min.x, dim.max.x);
362 Y = Y[concat](dim.min.y, dim.max.y);
367 var xmin = mmin[apply](0, X),
368 ymin = mmin[apply](0, Y);
372 width: mmax[apply](0, X) - xmin,
373 height: mmax[apply](0, Y) - ymin
376 pathClone = function (pathArray) {
378 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
379 pathArray = R.parsePathString(pathArray);
381 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
383 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
384 res[i][j] = pathArray[i][j];
387 res[toString] = R._path2string;
390 pathToRelative = cacher(function (pathArray) {
391 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
392 pathArray = R.parsePathString(pathArray);
400 if (pathArray[0][0] == "M") {
406 res[push](["M", x, y]);
408 for (var i = start, ii = pathArray[length]; i < ii; i++) {
411 if (pa[0] != lowerCase.call(pa[0])) {
412 r[0] = lowerCase.call(pa[0]);
420 r[6] = +(pa[6] - x).toFixed(3);
421 r[7] = +(pa[7] - y).toFixed(3);
424 r[1] = +(pa[1] - y).toFixed(3);
430 for (var j = 1, jj = pa[length]; j < jj; j++) {
431 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
440 for (var k = 0, kk = pa[length]; k < kk; k++) {
444 var len = res[i][length];
451 x += +res[i][len - 1];
454 y += +res[i][len - 1];
457 x += +res[i][len - 2];
458 y += +res[i][len - 1];
461 res[toString] = R._path2string;
464 pathToAbsolute = cacher(function (pathArray) {
465 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
466 pathArray = R.parsePathString(pathArray);
474 if (pathArray[0][0] == "M") {
475 x = +pathArray[0][1];
476 y = +pathArray[0][2];
480 res[0] = ["M", x, y];
482 for (var i = start, ii = pathArray[length]; i < ii; i++) {
485 if (pa[0] != upperCase.call(pa[0])) {
486 r[0] = upperCase.call(pa[0]);
507 for (var j = 1, jj = pa[length]; j < jj; j++) {
508 r[j] = +pa[j] + ((j % 2) ? x : y);
512 for (var k = 0, kk = pa[length]; k < kk; k++) {
528 x = res[i][res[i][length] - 2];
529 y = res[i][res[i][length] - 1];
532 res[toString] = R._path2string;
535 l2c = function (x1, y1, x2, y2) {
536 return [x1, y1, x2, y2, x2, y2];
538 q2c = function (x1, y1, ax, ay, x2, y2) {
550 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
551 // for more information of where this math came from visit:
552 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
554 _120 = PI * 120 / 180,
555 rad = PI / 180 * (+angle || 0),
558 rotate = cacher(function (x, y, rad) {
559 var X = x * math.cos(rad) - y * math.sin(rad),
560 Y = x * math.sin(rad) + y * math.cos(rad);
564 xy = rotate(x1, y1, -rad);
567 xy = rotate(x2, y2, -rad);
570 var cos = math.cos(PI / 180 * angle),
571 sin = math.sin(PI / 180 * angle),
574 rx = mmax(rx, math.abs(x));
575 ry = mmax(ry, math.abs(y));
578 k = (large_arc_flag == sweep_flag ? -1 : 1) *
579 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
580 cx = k * rx * y / ry + (x1 + x2) / 2,
581 cy = k * -ry * x / rx + (y1 + y2) / 2,
582 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
583 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
585 f1 = x1 < cx ? PI - f1 : f1;
586 f2 = x2 < cx ? PI - f2 : f2;
587 f1 < 0 && (f1 = PI * 2 + f1);
588 f2 < 0 && (f2 = PI * 2 + f2);
589 if (sweep_flag && f1 > f2) {
592 if (!sweep_flag && f2 > f1) {
602 if (math.abs(df) > _120) {
606 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
607 x2 = cx + rx * math.cos(f2);
608 y2 = cy + ry * math.sin(f2);
609 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
612 var c1 = math.cos(f1),
616 t = math.tan(df / 4),
620 m2 = [x1 + hx * s1, y1 - hy * c1],
621 m3 = [x2 + hx * s2, y2 - hy * c2],
623 m2[0] = 2 * m1[0] - m2[0];
624 m2[1] = 2 * m1[1] - m2[1];
626 return [m2, m3, m4][concat](res);
628 res = [m2, m3, m4][concat](res)[join]()[split](",");
630 for (var i = 0, ii = res[length]; i < ii; i++) {
631 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
636 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
639 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
640 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
643 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
644 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
645 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
647 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
648 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
652 math.abs(t1) > 1e12 && (t1 = .5);
653 math.abs(t2) > 1e12 && (t2 = .5);
654 if (t1 > 0 && t1 < 1) {
655 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
659 if (t2 > 0 && t2 < 1) {
660 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
664 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
665 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
667 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
668 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
669 math.abs(t1) > 1e12 && (t1 = .5);
670 math.abs(t2) > 1e12 && (t2 = .5);
671 if (t1 > 0 && t1 < 1) {
672 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
676 if (t2 > 0 && t2 < 1) {
677 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
682 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
683 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
686 path2curve = cacher(function (path, path2) {
687 var p = pathToAbsolute(path),
688 p2 = path2 && pathToAbsolute(path2),
689 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
690 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
691 processPath = function (path, d) {
694 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
696 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
703 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
706 nx = d.x + (d.x - (d.bx || d.x));
707 ny = d.y + (d.y - (d.by || d.y));
708 path = ["C", nx, ny][concat](path.slice(1));
711 d.qx = d.x + (d.x - (d.qx || d.x));
712 d.qy = d.y + (d.y - (d.qy || d.y));
713 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
718 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
721 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
724 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
727 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
730 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
735 fixArc = function (pp, i) {
736 if (pp[i][length] > 7) {
740 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
743 ii = mmax(p[length], p2 && p2[length] || 0);
746 fixM = function (path1, path2, a1, a2, i) {
747 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
748 path2.splice(i, 0, ["M", a2.x, a2.y]);
753 ii = mmax(p[length], p2 && p2[length] || 0);
756 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
757 p[i] = processPath(p[i], attrs);
759 p2 && (p2[i] = processPath(p2[i], attrs2));
761 fixM(p, p2, attrs, attrs2, i);
762 fixM(p2, p, attrs2, attrs, i);
765 seglen = seg[length],
766 seg2len = p2 && seg2[length];
767 attrs.x = seg[seglen - 2];
768 attrs.y = seg[seglen - 1];
769 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
770 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
771 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
772 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
773 attrs2.x = p2 && seg2[seg2len - 2];
774 attrs2.y = p2 && seg2[seg2len - 1];
776 return p2 ? [p, p2] : p;
778 parseDots = cacher(function (gradient) {
780 for (var i = 0, ii = gradient[length]; i < ii; i++) {
782 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
783 dot.color = R.getRGB(par[1]);
784 if (dot.color.error) {
787 dot.color = dot.color.hex;
788 par[2] && (dot.offset = par[2] + "%");
791 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
792 if (!dots[i].offset) {
793 var start = toFloat(dots[i - 1].offset || 0),
795 for (var j = i + 1; j < ii; j++) {
796 if (dots[j].offset) {
797 end = dots[j].offset;
806 var d = (end - start) / (j - i + 1);
809 dots[i].offset = start + "%";
815 getContainer = function () {
821 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
822 if (R.is(arguments[0], "string")) {
823 container = doc.getElementById(arguments[0]);
825 container = arguments[0];
827 if (container.tagName) {
828 if (arguments[1] == null) {
830 container: container,
831 width: container.style.pixelWidth || container.offsetWidth,
832 height: container.style.pixelHeight || container.offsetHeight
835 return {container: container, width: arguments[1], height: arguments[2]};
838 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
839 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
842 plugins = function (con, add) {
844 for (var prop in add) if (add[has](prop) && !(prop in con)) {
845 switch (typeof add[prop]) {
848 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
852 con[prop] = con[prop] || {};
853 plugins.call(this, con[prop], add[prop]);
856 con[prop] = add[prop];
861 tear = function (el, paper) {
862 el == paper.top && (paper.top = el.prev);
863 el == paper.bottom && (paper.bottom = el.next);
864 el.next && (el.next.prev = el.prev);
865 el.prev && (el.prev.next = el.next);
867 tofront = function (el, paper) {
868 if (paper.top === el) {
877 toback = function (el, paper) {
878 if (paper.bottom === el) {
882 el.next = paper.bottom;
884 paper.bottom.prev = el;
887 insertafter = function (el, el2, paper) {
889 el2 == paper.top && (paper.top = el);
890 el2.next && (el2.next.prev = el);
895 insertbefore = function (el, el2, paper) {
897 el2 == paper.bottom && (paper.bottom = el);
898 el2.prev && (el2.prev.next = el);
903 removed = function (methodname) {
905 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
908 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
912 Paper[proto].svgns = "http://www.w3.org/2000/svg";
913 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
914 var round = function (num) {
915 return +num + (~~num === num) * .5;
917 roundPath = function (path) {
918 for (var i = 0, ii = path[length]; i < ii; i++) {
919 if (lowerCase.call(path[i][0]) != "a") {
920 for (var j = 1, jj = path[i][length]; j < jj; j++) {
921 path[i][j] = round(path[i][j]);
924 path[i][6] = round(path[i][6]);
925 path[i][7] = round(path[i][7]);
930 $ = function (el, attr) {
932 for (var key in attr) if (attr[has](key)) {
933 el[setAttribute](key, attr[key]);
936 return doc.createElementNS(Paper[proto].svgns, el);
939 R[toString] = function () {
940 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
942 var thePath = function (pathString, SVG) {
944 SVG.canvas && SVG.canvas[appendChild](el);
945 var p = new Element(el, SVG);
947 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
950 var addGradientFill = function (o, gradient, SVG) {
954 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
959 var dir = ((fy > .5) * 2 - 1);
960 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
961 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
963 (fy = fy.toFixed(5) - 1e-5 * dir);
967 gradient = gradient[split](/\s*\-\s*/);
968 if (type == "linear") {
969 var angle = gradient.shift();
970 angle = -toFloat(angle);
974 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
975 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
979 vector[0] = -vector[2];
983 vector[1] = -vector[3];
987 var dots = parseDots(gradient);
991 var el = $(type + "Gradient");
992 el.id = "r" + (R._id++)[toString](36);
993 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
994 SVG.defs[appendChild](el);
995 for (var i = 0, ii = dots[length]; i < ii; i++) {
996 var stop = $("stop");
998 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
999 "stop-color": dots[i].color || "#fff"
1001 el[appendChild](stop);
1004 fill: "url(#" + el.id + ")",
1013 var updatePosition = function (o) {
1014 var bbox = o.getBBox();
1015 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1017 var setFillAndStroke = function (o, params) {
1024 "-..": [3, 1, 1, 1, 1, 1],
1028 "- .": [4, 3, 1, 3],
1029 "--.": [8, 3, 1, 3],
1030 "--..": [8, 3, 1, 3, 1, 3]
1035 addDashes = function (o, value) {
1036 value = dasharray[lowerCase.call(value)];
1038 var width = o.attrs["stroke-width"] || "1",
1039 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1041 var i = value[length];
1043 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1045 $(node, {"stroke-dasharray": dashes[join](",")});
1048 params[has]("rotation") && (rot = params.rotation);
1049 var rotxy = (rot + E)[split](separator);
1050 if (!(rotxy.length - 1)) {
1053 rotxy[1] = +rotxy[1];
1054 rotxy[2] = +rotxy[2];
1056 toFloat(rot) && o.rotate(0, true);
1057 for (var att in params) if (params[has](att)) {
1058 if (!availableAttrs[has](att)) {
1061 var value = params[att];
1065 o.rotate(value, true);
1071 var pn = node.parentNode;
1072 if (lowerCase.call(pn.tagName) != "a") {
1074 pn.insertBefore(hl, node);
1075 hl[appendChild](node);
1078 pn.setAttributeNS(o.Paper[proto].xlink, att, value);
1081 node.style.cursor = value;
1084 var rect = (value + E)[split](separator);
1085 if (rect[length] == 4) {
1086 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1087 var el = $("clipPath"),
1089 el.id = "r" + (R._id++)[toString](36);
1096 el[appendChild](rc);
1097 o.paper.defs[appendChild](el);
1098 $(node, {"clip-path": "url(#" + el.id + ")"});
1102 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1103 clip && clip.parentNode.removeChild(clip);
1104 $(node, {"clip-path": E});
1109 if (value && o.type == "path") {
1110 attrs.path = roundPath(pathToAbsolute(value));
1111 $(node, {d: attrs.path});
1115 node[setAttribute](att, value);
1124 value = -attrs.x - (attrs.width || 0);
1127 if (att == "rx" && o.type == "rect") {
1131 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1132 node[setAttribute](att, round(value));
1133 o.pattern && updatePosition(o);
1136 node[setAttribute](att, value);
1145 value = -attrs.y - (attrs.height || 0);
1148 if (att == "ry" && o.type == "rect") {
1152 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1153 node[setAttribute](att, round(value));
1154 o.pattern && updatePosition(o);
1157 if (o.type == "rect") {
1158 $(node, {rx: value, ry: value});
1160 node[setAttribute](att, value);
1164 if (o.type == "image") {
1165 node.setAttributeNS(o.paper.xlink, "href", value);
1168 case "stroke-width":
1169 node.style.strokeWidth = value;
1170 // Need following line for Firefox
1171 node[setAttribute](att, value);
1172 if (attrs["stroke-dasharray"]) {
1173 addDashes(o, attrs["stroke-dasharray"]);
1176 case "stroke-dasharray":
1177 addDashes(o, value);
1180 var xy = (value + E)[split](separator);
1181 xy[0] = +xy[0] || 0;
1182 xy[1] = +xy[1] || 0;
1187 translate.call(o, xy[0], xy[1]);
1190 var xy = (value + E)[split](separator);
1191 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1194 var isURL = (value + E).match(ISURL);
1196 var el = $("pattern"),
1198 el.id = "r" + (R._id++)[toString](36);
1199 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1200 $(ig, {x: 0, y: 0});
1201 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1202 el[appendChild](ig);
1204 var img = doc.createElement("img");
1205 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1206 img.onload = function () {
1207 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1208 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1209 doc.body.removeChild(this);
1212 doc.body[appendChild](img);
1214 o.paper.defs[appendChild](el);
1215 node.style.fill = "url(#" + el.id + ")";
1216 $(node, {fill: "url(#" + el.id + ")"});
1218 o.pattern && updatePosition(o);
1221 if (!R.getRGB(value).error) {
1222 delete params.gradient;
1223 delete attrs.gradient;
1224 !R.is(attrs.opacity, "undefined") &&
1225 R.is(params.opacity, "undefined") &&
1226 $(node, {opacity: attrs.opacity});
1227 !R.is(attrs["fill-opacity"], "undefined") &&
1228 R.is(params["fill-opacity"], "undefined") &&
1229 $(node, {"fill-opacity": attrs["fill-opacity"]});
1230 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1231 attrs.gradient = value;
1232 attrs.fill = "none";
1236 node[setAttribute](att, R.getRGB(value).hex);
1239 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1242 case "fill-opacity":
1243 if (attrs.gradient) {
1244 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1246 var stops = gradient.getElementsByTagName("stop");
1247 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1252 att == "font-size" && (value = toInt(value, 10) + "px");
1253 var cssrule = att[rp](/(\-.)/g, function (w) {
1254 return upperCase.call(w.substring(1));
1256 node.style[cssrule] = value;
1257 // Need following line for Firefox
1258 node[setAttribute](att, value);
1263 tuneText(o, params);
1265 o.rotate(rotxy.join(S));
1267 toFloat(rot) && o.rotate(rot, true);
1271 var tuneText = function (el, params) {
1272 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1277 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1279 if (params[has]("text")) {
1280 a.text = params.text;
1281 while (node.firstChild) {
1282 node.removeChild(node.firstChild);
1284 var texts = (params.text + E)[split]("\n");
1285 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1286 var tspan = $("tspan");
1287 i && $(tspan, {dy: fontSize * leading, x: a.x});
1288 tspan[appendChild](doc.createTextNode(texts[i]));
1289 node[appendChild](tspan);
1292 var texts = node.getElementsByTagName("tspan");
1293 for (var i = 0, ii = texts[length]; i < ii; i++) {
1294 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1298 var bb = el.getBBox(),
1299 dif = a.y - (bb.y + bb.height / 2);
1300 dif && isFinite(dif) && $(node, {y: a.y + dif});
1302 var Element = function (node, svg) {
1308 node.raphael = this;
1310 this.attrs = this.attrs || {};
1311 this.transformations = []; // rotate, translate, scale
1315 rt: {deg: 0, cx: 0, cy: 0},
1319 !svg.bottom && (svg.bottom = this);
1320 this.prev = svg.top;
1321 svg.top && (svg.top.next = this);
1325 Element[proto].rotate = function (deg, cx, cy) {
1331 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1333 return this._.rt.deg;
1335 var bbox = this.getBBox();
1336 deg = (deg + E)[split](separator);
1337 if (deg[length] - 1) {
1338 cx = toFloat(deg[1]);
1339 cy = toFloat(deg[2]);
1341 deg = toFloat(deg[0]);
1343 this._.rt.deg = deg;
1345 this._.rt.deg += deg;
1347 (cy == null) && (cx = null);
1350 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1351 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1352 if (this._.rt.deg) {
1353 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1354 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1356 this.transformations[0] = E;
1357 this.clip && $(this.clip, {transform: E});
1359 $(this.node, {transform: this.transformations[join](S)});
1362 Element[proto].hide = function () {
1363 !this.removed && (this.node.style.display = "none");
1366 Element[proto].show = function () {
1367 !this.removed && (this.node.style.display = "");
1370 Element[proto].remove = function () {
1374 tear(this, this.paper);
1375 this.node.parentNode.removeChild(this.node);
1376 for (var i in this) {
1379 this.removed = true;
1381 Element[proto].getBBox = function () {
1385 if (this.type == "path") {
1386 return pathDimensions(this.attrs.path);
1388 if (this.node.style.display == "none") {
1394 bbox = this.node.getBBox();
1396 // Firefox 3.0.x plays badly here
1400 if (this.type == "text") {
1401 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1402 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1403 var bb = this.node.getExtentOfChar(i);
1404 (bb.y < bbox.y) && (bbox.y = bb.y);
1405 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1406 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1409 hide && this.hide();
1412 Element[proto].attr = function () {
1416 if (arguments[length] == 0) {
1418 for (var i in this.attrs) if (this.attrs[has](i)) {
1419 res[i] = this.attrs[i];
1421 this._.rt.deg && (res.rotation = this.rotate());
1422 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1423 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1426 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1427 if (arguments[0] == "translation") {
1428 return translate.call(this);
1430 if (arguments[0] == "rotation") {
1431 return this.rotate();
1433 if (arguments[0] == "scale") {
1434 return this.scale();
1436 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1437 return this.attrs.gradient;
1439 return this.attrs[arguments[0]];
1441 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1443 for (var j in arguments[0]) if (arguments[0][has](j)) {
1444 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1448 if (arguments[length] == 2) {
1450 params[arguments[0]] = arguments[1];
1451 setFillAndStroke(this, params);
1452 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1453 setFillAndStroke(this, arguments[0]);
1457 Element[proto].toFront = function () {
1461 this.node.parentNode[appendChild](this.node);
1462 var svg = this.paper;
1463 svg.top != this && tofront(this, svg);
1466 Element[proto].toBack = function () {
1470 if (this.node.parentNode.firstChild != this.node) {
1471 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1472 toback(this, this.paper);
1473 var svg = this.paper;
1477 Element[proto].insertAfter = function (element) {
1481 var svg = this.paper,
1482 node = element.node;
1483 if (node.nextSibling) {
1484 node.parentNode.insertBefore(this.node, node.nextSibling);
1486 node.parentNode[appendChild](this.node);
1488 insertafter(this, element, this.paper);
1491 Element[proto].insertBefore = function (element) {
1495 var node = element.node;
1496 node.parentNode.insertBefore(this.node, node);
1497 insertbefore(this, element, this.paper);
1500 Element[proto].getTotalLength = function () {
1501 if (this.type != "path") {
1504 return this.node.getTotalLength();
1506 Element[proto].getPointAtLength = function (length) {
1507 if (this.type != "path") {
1510 var point = this.node.getPointAtLength(length);
1511 return {x: point.x, y: point.y};
1513 var theCircle = function (svg, x, y, r) {
1516 var el = $("circle");
1517 svg.canvas && svg.canvas[appendChild](el);
1518 var res = new Element(el, svg);
1519 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1520 res.type = "circle";
1524 var theRect = function (svg, x, y, w, h, r) {
1528 svg.canvas && svg.canvas[appendChild](el);
1529 var res = new Element(el, svg);
1530 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1535 var theEllipse = function (svg, x, y, rx, ry) {
1538 var el = $("ellipse");
1539 svg.canvas && svg.canvas[appendChild](el);
1540 var res = new Element(el, svg);
1541 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1542 res.type = "ellipse";
1546 var theImage = function (svg, src, x, y, w, h) {
1547 var el = $("image");
1548 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1549 el.setAttributeNS(svg.xlink, "href", src);
1550 svg.canvas && svg.canvas[appendChild](el);
1551 var res = new Element(el, svg);
1552 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1556 var theText = function (svg, x, y, text) {
1558 $(el, {x: x, y: y, "text-anchor": "middle"});
1559 svg.canvas && svg.canvas[appendChild](el);
1560 var res = new Element(el, svg);
1561 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1563 setFillAndStroke(res, res.attrs);
1566 var setSize = function (width, height) {
1567 this.width = width || this.width;
1568 this.height = height || this.height;
1569 this.canvas[setAttribute]("width", this.width);
1570 this.canvas[setAttribute]("height", this.height);
1573 var create = function () {
1574 var con = getContainer[apply](null, arguments),
1575 container = con && con.container,
1579 height = con.height;
1581 throw new Error("SVG container not found.");
1583 var cnvs = $("svg");
1584 width = width || 512;
1585 height = height || 342;
1587 xmlns: "http://www.w3.org/2000/svg",
1592 if (container == 1) {
1593 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1594 doc.body[appendChild](cnvs);
1596 if (container.firstChild) {
1597 container.insertBefore(cnvs, container.firstChild);
1599 container[appendChild](cnvs);
1602 container = new Paper;
1603 container.width = width;
1604 container.height = height;
1605 container.canvas = cnvs;
1606 plugins.call(container, container, R.fn);
1610 Paper[proto].clear = function () {
1611 var c = this.canvas;
1612 while (c.firstChild) {
1613 c.removeChild(c.firstChild);
1615 this.bottom = this.top = null;
1616 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1617 c[appendChild](this.desc);
1618 c[appendChild](this.defs = $("defs"));
1620 Paper[proto].remove = function () {
1621 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1622 for (var i in this) {
1623 this[i] = removed(i);
1630 var path2vml = function (path) {
1631 var total = /[ahqtv]/ig,
1632 command = pathToAbsolute;
1633 (path + E).match(total) && (command = path2curve);
1635 if (command == pathToAbsolute && !(path + E).match(total)) {
1636 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1637 bites = /([clmz]),?([^clmz]*)/gi,
1638 val = /-?[^,\s-]+/g;
1639 var res = (path + E)[rp](bites, function (all, command, args) {
1641 args[rp](val, function (value) {
1642 vals[push](round(value));
1644 return map[command] + vals;
1648 var pa = command(path), p, res = [], r;
1649 for (var i = 0, ii = pa[length]; i < ii; i++) {
1651 r = lowerCase.call(pa[i][0]);
1652 r == "z" && (r = "x");
1653 for (var j = 1, jj = p[length]; j < jj; j++) {
1654 r += round(p[j]) + (j != jj - 1 ? "," : E);
1658 return res[join](S);
1660 segmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
1661 var old = {x: 0, y: 0},
1663 for (var i = 0; i < 1.01; i+=.01) {
1664 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
1665 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
1671 R[toString] = function () {
1672 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1674 var thePath = function (pathString, VML) {
1675 var g = createNode("group");
1676 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1677 g.coordsize = VML.coordsize;
1678 g.coordorigin = VML.coordorigin;
1679 var el = createNode("shape"), ol = el.style;
1680 ol.width = VML.width + "px";
1681 ol.height = VML.height + "px";
1682 el.coordsize = this.coordsize;
1683 el.coordorigin = this.coordorigin;
1685 var p = new Element(el, g, VML);
1686 p.isAbsolute = true;
1690 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1691 VML.canvas[appendChild](g);
1694 var setFillAndStroke = function (o, params) {
1695 o.attrs = o.attrs || {};
1701 for (var par in params) if (params[has](par)) {
1702 a[par] = params[par];
1704 params.href && (node.href = params.href);
1705 params.title && (node.title = params.title);
1706 params.target && (node.target = params.target);
1707 params.cursor && (s.cursor = params.cursor);
1708 if (params.path && o.type == "path") {
1709 a.path = params.path;
1710 node.path = path2vml(a.path);
1712 if (params.rotation != null) {
1713 o.rotate(params.rotation, true);
1715 if (params.translation) {
1716 xy = (params.translation + E)[split](separator);
1717 translate.call(o, xy[0], xy[1]);
1718 if (o._.rt.cx != null) {
1719 o._.rt.cx +=+ xy[0];
1720 o._.rt.cy +=+ xy[1];
1721 o.setBox(o.attrs, xy[0], xy[1]);
1725 xy = (params.scale + E)[split](separator);
1726 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1728 if ("clip-rect" in params) {
1729 var rect = (params["clip-rect"] + E)[split](separator);
1730 if (rect[length] == 4) {
1731 rect[2] = +rect[2] + (+rect[0]);
1732 rect[3] = +rect[3] + (+rect[1]);
1733 var div = node.clipRect || doc.createElement("div"),
1735 group = node.parentNode;
1736 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1737 if (!node.clipRect) {
1738 dstyle.position = "absolute";
1741 dstyle.width = o.paper.width + "px";
1742 dstyle.height = o.paper.height + "px";
1743 group.parentNode.insertBefore(div, group);
1744 div[appendChild](group);
1745 node.clipRect = div;
1748 if (!params["clip-rect"]) {
1749 node.clipRect && (node.clipRect.style.clip = E);
1752 if (o.type == "image" && params.src) {
1753 node.src = params.src;
1755 if (o.type == "image" && params.opacity) {
1756 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1757 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1759 params.font && (s.font = params.font);
1760 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1761 params["font-size"] && (s.fontSize = params["font-size"]);
1762 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1763 params["font-style"] && (s.fontStyle = params["font-style"]);
1764 if (params.opacity != null ||
1765 params["stroke-width"] != null ||
1766 params.fill != null ||
1767 params.stroke != null ||
1768 params["stroke-width"] != null ||
1769 params["stroke-opacity"] != null ||
1770 params["fill-opacity"] != null ||
1771 params["stroke-dasharray"] != null ||
1772 params["stroke-miterlimit"] != null ||
1773 params["stroke-linejoin"] != null ||
1774 params["stroke-linecap"] != null) {
1775 node = o.shape || node;
1776 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1778 !fill && (newfill = fill = createNode("fill"));
1779 if ("fill-opacity" in params || "opacity" in params) {
1780 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1781 opacity < 0 && (opacity = 0);
1782 opacity > 1 && (opacity = 1);
1783 fill.opacity = opacity;
1785 params.fill && (fill.on = true);
1786 if (fill.on == null || params.fill == "none") {
1789 if (fill.on && params.fill) {
1790 var isURL = params.fill.match(ISURL);
1792 fill.src = isURL[1];
1795 fill.color = R.getRGB(params.fill).hex;
1797 fill.type = "solid";
1798 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1800 a.gradient = params.fill;
1804 newfill && node[appendChild](fill);
1805 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1807 !stroke && (newstroke = stroke = createNode("stroke"));
1808 if ((params.stroke && params.stroke != "none") ||
1809 params["stroke-width"] ||
1810 params["stroke-opacity"] != null ||
1811 params["stroke-dasharray"] ||
1812 params["stroke-miterlimit"] ||
1813 params["stroke-linejoin"] ||
1814 params["stroke-linecap"]) {
1817 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1818 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1819 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1820 width = (toFloat(params["stroke-width"]) || 1) * .75;
1821 opacity < 0 && (opacity = 0);
1822 opacity > 1 && (opacity = 1);
1823 params["stroke-width"] == null && (width = a["stroke-width"]);
1824 params["stroke-width"] && (stroke.weight = width);
1825 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1826 stroke.opacity = opacity;
1828 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1829 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1830 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1831 if (params["stroke-dasharray"]) {
1835 "-.": "shortdashdot",
1836 "-..": "shortdashdotdot",
1841 "--.": "longdashdot",
1842 "--..": "longdashdotdot"
1844 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1846 newstroke && node[appendChild](stroke);
1848 if (res.type == "text") {
1849 var s = res.paper.span.style;
1850 a.font && (s.font = a.font);
1851 a["font-family"] && (s.fontFamily = a["font-family"]);
1852 a["font-size"] && (s.fontSize = a["font-size"]);
1853 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1854 a["font-style"] && (s.fontStyle = a["font-style"]);
1855 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1856 res.W = a.w = res.paper.span.offsetWidth;
1857 res.H = a.h = res.paper.span.offsetHeight;
1859 res.Y = a.y + round(res.H / 2);
1861 // text-anchor emulationm
1862 switch (a["text-anchor"]) {
1864 res.node.style["v-text-align"] = "left";
1865 res.bbx = round(res.W / 2);
1868 res.node.style["v-text-align"] = "right";
1869 res.bbx = -round(res.W / 2);
1872 res.node.style["v-text-align"] = "center";
1877 var addGradientFill = function (o, gradient) {
1878 o.attrs = o.attrs || {};
1879 var attrs = o.attrs,
1880 fill = o.node.getElementsByTagName("fill"),
1883 o.attrs.gradient = gradient;
1884 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1889 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1894 gradient = gradient[split](/\s*\-\s*/);
1895 if (type == "linear") {
1896 var angle = gradient.shift();
1897 angle = -toFloat(angle);
1902 var dots = parseDots(gradient);
1906 o = o.shape || o.node;
1907 fill = fill[0] || createNode("fill");
1910 fill.method = "none";
1911 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1912 fill.color = dots[0].color;
1913 fill.color2 = dots[dots[length] - 1].color;
1915 for (var i = 0, ii = dots[length]; i < ii; i++) {
1916 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1918 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1919 if (type == "radial") {
1920 fill.focus = "100%";
1921 fill.focussize = fxfy;
1922 fill.focusposition = fxfy;
1924 fill.angle = (270 - angle) % 360;
1929 var Element = function (node, group, vml) {
1937 node.raphael = this;
1950 !vml.bottom && (vml.bottom = this);
1951 this.prev = vml.top;
1952 vml.top && (vml.top.next = this);
1956 Element[proto].rotate = function (deg, cx, cy) {
1962 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1964 return this._.rt.deg;
1966 deg = (deg + E)[split](separator);
1967 if (deg[length] - 1) {
1968 cx = toFloat(deg[1]);
1969 cy = toFloat(deg[2]);
1971 deg = toFloat(deg[0]);
1973 this._.rt.deg = deg;
1975 this._.rt.deg += deg;
1977 cy == null && (cx = null);
1980 this.setBox(this.attrs, cx, cy);
1981 this.Group.style.rotation = this._.rt.deg;
1982 // gradient fix for rotation. TODO
1983 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1984 // fill = fill[0] || {};
1985 // var b = ((360 - this._.rt.deg) - 270) % 360;
1986 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1989 Element[proto].setBox = function (params, cx, cy) {
1993 var gs = this.Group.style,
1994 os = (this.shape && this.shape.style) || this.node.style;
1995 params = params || {};
1996 for (var i in params) if (params[has](i)) {
1997 this.attrs[i] = params[i];
1999 cx = cx || this._.rt.cx;
2000 cy = cy || this._.rt.cy;
2001 var attr = this.attrs,
2006 switch (this.type) {
2008 x = attr.cx - attr.r;
2009 y = attr.cy - attr.r;
2013 x = attr.cx - attr.rx;
2014 y = attr.cy - attr.ry;
2022 w = attr.width || 0;
2023 h = attr.height || 0;
2026 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2027 x = attr.x - round(this.W / 2);
2028 y = attr.y - this.H / 2;
2033 if (!this.attrs.path) {
2036 w = this.paper.width;
2037 h = this.paper.height;
2039 var dim = pathDimensions(this.attrs.path);
2049 w = this.paper.width;
2050 h = this.paper.height;
2053 cx = (cx == null) ? x + w / 2 : cx;
2054 cy = (cy == null) ? y + h / 2 : cy;
2055 var left = cx - this.paper.width / 2,
2056 top = cy - this.paper.height / 2;
2057 if (this.type == "path" || this.type == "text") {
2058 (gs.left != left + "px") && (gs.left = left + "px");
2059 (gs.top != top + "px") && (gs.top = top + "px");
2060 this.X = this.type == "text" ? x : -left;
2061 this.Y = this.type == "text" ? y : -top;
2064 (os.left != -left + "px") && (os.left = -left + "px");
2065 (os.top != -top + "px") && (os.top = -top + "px");
2067 (gs.left != left + "px") && (gs.left = left + "px");
2068 (gs.top != top + "px") && (gs.top = top + "px");
2073 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2074 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2075 (os.left != x - left + "px") && (os.left = x - left + "px");
2076 (os.top != y - top + "px") && (os.top = y - top + "px");
2077 (os.width != w + "px") && (os.width = w + "px");
2078 (os.height != h + "px") && (os.height = h + "px");
2079 var arcsize = (+params.r || 0) / mmin(w, h);
2080 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2081 // We should replace element with the new one
2082 var o = createNode("roundrect"),
2085 ii = this.events && this.events[length];
2086 o.arcsize = arcsize;
2088 this.Group[appendChild](o);
2089 this.Group.removeChild(this.node);
2090 this[0] = this.node = o;
2091 this.arcsize = arcsize;
2092 for (var i in attr) {
2097 if (this.events) for (; i < ii; i++) {
2098 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2103 Element[proto].hide = function () {
2104 !this.removed && (this.Group.style.display = "none");
2107 Element[proto].show = function () {
2108 !this.removed && (this.Group.style.display = "block");
2111 Element[proto].getBBox = function () {
2115 if (this.type == "path") {
2116 return pathDimensions(this.attrs.path);
2119 x: this.X + (this.bbx || 0),
2125 Element[proto].remove = function () {
2129 tear(this, this.paper);
2130 this.node.parentNode.removeChild(this.node);
2131 this.Group.parentNode.removeChild(this.Group);
2132 this.shape && this.shape.parentNode.removeChild(this.shape);
2133 for (var i in this) {
2136 this.removed = true;
2138 Element[proto].attr = function () {
2142 if (arguments[length] == 0) {
2144 for (var i in this.attrs) if (this.attrs[has](i)) {
2145 res[i] = this.attrs[i];
2147 this._.rt.deg && (res.rotation = this.rotate());
2148 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2149 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2152 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2153 if (arguments[0] == "translation") {
2154 return translate.call(this);
2156 if (arguments[0] == "rotation") {
2157 return this.rotate();
2159 if (arguments[0] == "scale") {
2160 return this.scale();
2162 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2163 return this.attrs.gradient;
2165 return this.attrs[arguments[0]];
2167 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2169 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2170 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2175 if (arguments[length] == 2) {
2177 params[arguments[0]] = arguments[1];
2179 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2181 if (params.text && this.type == "text") {
2182 this.node.string = params.text;
2184 setFillAndStroke(this, params);
2185 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2186 addGradientFill(this, params.gradient);
2188 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2192 Element[proto].toFront = function () {
2193 !this.removed && this.Group.parentNode[appendChild](this.Group);
2194 this.paper.top != this && tofront(this, this.paper);
2197 Element[proto].toBack = function () {
2201 if (this.Group.parentNode.firstChild != this.Group) {
2202 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2203 toback(this, this.paper);
2207 Element[proto].insertAfter = function (element) {
2211 if (element.Group.nextSibling) {
2212 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2214 element.Group.parentNode[appendChild](this.Group);
2216 insertafter(this, element, this.paper);
2219 Element[proto].insertBefore = function (element) {
2223 element.Group.parentNode.insertBefore(this.Group, element.Group);
2224 insertbefore(this, element, this.paper);
2227 Element[proto].getTotalLength = function () {
2228 if (this.type != "path") {
2231 var path = path2curve(this.attrs.path), x, y, p,
2233 for (var i = 0, ii = path.length; i < ii; i++) {
2239 len += segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2246 Element[proto].getPointAtLength = function (length) {
2247 if (this.type != "path") {
2250 var path = path2curve(this.attrs.path), x, y, p, l,
2252 for (var i = 0, ii = path.length; i < ii; i++) {
2258 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2259 if (len + l > length) {
2260 return findDotAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2267 return {x: x, y: y};
2270 var theCircle = function (vml, x, y, r) {
2271 var g = createNode("group"),
2272 o = createNode("oval"),
2274 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2275 g.coordsize = vml.coordsize;
2276 g.coordorigin = vml.coordorigin;
2278 var res = new Element(o, g, vml);
2279 res.type = "circle";
2280 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2284 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2285 vml.canvas[appendChild](g);
2288 theRect = function (vml, x, y, w, h, r) {
2289 var g = createNode("group"),
2290 o = createNode("roundrect"),
2291 arcsize = (+r || 0) / (mmin(w, h));
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;
2296 o.arcsize = arcsize;
2297 var res = new Element(o, g, vml);
2299 setFillAndStroke(res, {stroke: "#000"});
2300 res.arcsize = arcsize;
2301 res.setBox({x: x, y: y, width: w, height: h, r: r});
2302 vml.canvas[appendChild](g);
2305 theEllipse = function (vml, x, y, rx, ry) {
2306 var g = createNode("group"),
2307 o = createNode("oval"),
2309 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2310 g.coordsize = vml.coordsize;
2311 g.coordorigin = vml.coordorigin;
2313 var res = new Element(o, g, vml);
2314 res.type = "ellipse";
2315 setFillAndStroke(res, {stroke: "#000"});
2320 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2321 vml.canvas[appendChild](g);
2324 theImage = function (vml, src, x, y, w, h) {
2325 var g = createNode("group"),
2326 o = createNode("image"),
2328 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2329 g.coordsize = vml.coordsize;
2330 g.coordorigin = vml.coordorigin;
2333 var res = new Element(o, g, vml);
2335 res.attrs.src = src;
2340 res.setBox({x: x, y: y, width: w, height: h});
2341 vml.canvas[appendChild](g);
2344 theText = function (vml, x, y, text) {
2345 var g = createNode("group"),
2346 el = createNode("shape"),
2348 path = createNode("path"),
2350 o = createNode("textpath");
2351 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2352 g.coordsize = vml.coordsize;
2353 g.coordorigin = vml.coordorigin;
2354 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2355 path.textpathok = true;
2356 ol.width = vml.width;
2357 ol.height = vml.height;
2358 o.string = text + E;
2361 el[appendChild](path);
2363 var res = new Element(o, g, vml);
2365 res.textpath = path;
2367 res.attrs.text = text;
2372 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2374 vml.canvas[appendChild](g);
2377 setSize = function (width, height) {
2378 var cs = this.canvas.style;
2379 width == +width && (width += "px");
2380 height == +height && (height += "px");
2383 cs.clip = "rect(0 " + width + " " + height + " 0)";
2387 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2389 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2390 createNode = function (tagName) {
2391 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2394 createNode = function (tagName) {
2395 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2398 var create = function () {
2399 var con = getContainer[apply](null, arguments),
2400 container = con.container,
2401 height = con.height,
2407 throw new Error("VML container not found.");
2409 var res = new Paper,
2410 c = res.canvas = doc.createElement("div"),
2412 width = width || 512;
2413 height = height || 342;
2414 width == +width && (width += "px");
2415 height == +height && (height += "px");
2418 res.coordsize = "1000 1000";
2419 res.coordorigin = "0 0";
2420 res.span = doc.createElement("span");
2421 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2422 c[appendChild](res.span);
2423 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2424 if (container == 1) {
2425 doc.body[appendChild](c);
2429 container.style.width = width;
2430 container.style.height = height;
2431 if (container.firstChild) {
2432 container.insertBefore(c, container.firstChild);
2434 container[appendChild](c);
2437 plugins.call(res, res, R.fn);
2440 Paper[proto].clear = function () {
2441 this.canvas.innerHTML = E;
2442 this.bottom = this.top = null;
2444 Paper[proto].remove = function () {
2445 this.canvas.parentNode.removeChild(this.canvas);
2446 for (var i in this) {
2447 this[i] = removed(i);
2453 // Safari or Chrome (WebKit) rendering bug workaround method
2454 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2455 Paper[proto].safari = function () {
2456 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2457 setTimeout(function () {rect.remove();});
2460 Paper[proto].safari = function () {};
2464 var addEvent = (function () {
2465 if (doc.addEventListener) {
2466 return function (obj, type, fn, element) {
2467 var f = function (e) {
2468 return fn.call(element, e);
2470 obj.addEventListener(type, f, false);
2471 return function () {
2472 obj.removeEventListener(type, f, false);
2476 } else if (doc.attachEvent) {
2477 return function (obj, type, fn, element) {
2478 var f = function (e) {
2479 return fn.call(element, e || win.event);
2481 obj.attachEvent("on" + type, f);
2482 var detacher = function () {
2483 obj.detachEvent("on" + type, f);
2490 for (var i = events[length]; i--;) {
2491 (function (eventName) {
2492 Element[proto][eventName] = function (fn) {
2493 if (R.is(fn, "function")) {
2494 this.events = this.events || [];
2495 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2499 Element[proto]["un" + eventName] = function (fn) {
2500 var events = this.events,
2502 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2504 events.splice(l, 1);
2505 !events.length && delete this.events;
2512 Element[proto].hover = function (f_in, f_out) {
2513 return this.mouseover(f_in).mouseout(f_out);
2515 Element[proto].unhover = function (f_in, f_out) {
2516 return this.unmouseover(f_in).unmouseout(f_out);
2518 Paper[proto].circle = function (x, y, r) {
2519 return theCircle(this, x || 0, y || 0, r || 0);
2521 Paper[proto].rect = function (x, y, w, h, r) {
2522 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2524 Paper[proto].ellipse = function (x, y, rx, ry) {
2525 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2527 Paper[proto].path = function (pathString) {
2528 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2529 return thePath(R.format[apply](R, arguments), this);
2531 Paper[proto].image = function (src, x, y, w, h) {
2532 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2534 Paper[proto].text = function (x, y, text) {
2535 return theText(this, x || 0, y || 0, text || E);
2537 Paper[proto].set = function (itemsArray) {
2538 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2539 return new Set(itemsArray);
2541 Paper[proto].setSize = setSize;
2542 Paper[proto].top = Paper[proto].bottom = null;
2543 Paper[proto].raphael = R;
2545 return this.x + S + this.y;
2547 Element[proto].scale = function (x, y, cx, cy) {
2548 if (x == null && y == null) {
2563 var bb = this.getBBox(),
2564 rcx = bb.x + bb.width / 2,
2565 rcy = bb.y + bb.height / 2,
2568 cx = (+cx || cx == 0) ? cx : rcx;
2569 cy = (+cy || cy == 0) ? cy : rcy;
2570 var dirx = ~~(x / math.abs(x)),
2571 diry = ~~(y / math.abs(y)),
2572 s = this.node.style,
2573 ncx = cx + (rcx - cx) * kx,
2574 ncy = cy + (rcy - cy) * ky;
2575 switch (this.type) {
2578 var neww = a.width * dirx * kx,
2579 hewh = a.height * diry * ky;
2582 r: a.r * mmin(dirx * kx, diry * ky),
2591 rx: a.rx * dirx * kx,
2592 ry: a.ry * diry * ky,
2593 r: a.r * mmin(dirx * kx, diry * ky),
2599 var path = pathToRelative(a.path),
2601 for (var i = 0, ii = path[length]; i < ii; i++) {
2604 P0 = upperCase.call(p[0]);
2605 if (P0 == "M" && skip) {
2611 p[path[i][length] - 2] *= kx;
2612 p[path[i][length] - 1] *= ky;
2615 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2616 } else if (P0 == "H") {
2617 for (j = 1, jj = p[length]; j < jj; j++) {
2620 } else if (P0 == "V") {
2621 for (j = 1, jj = p[length]; j < jj; j++) {
2625 for (j = 1, jj = p[length]; j < jj; j++) {
2626 p[j] *= (j % 2) ? kx : ky;
2630 var dim2 = pathDimensions(path),
2631 dx = ncx - dim2.x - dim2.width / 2,
2632 dy = ncy - dim2.y - dim2.height / 2;
2635 this.attr({path: path});
2638 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2639 if (this.transformations) {
2640 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2641 this.node[setAttribute]("transform", this.transformations[join](S));
2642 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2643 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2644 this.attr({x: dx, y: dy});
2648 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2649 ", M12=0, M21=0, M22=", diry,
2650 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2651 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2654 if (this.transformations) {
2655 this.transformations[2] = E;
2656 this.node[setAttribute]("transform", this.transformations[join](S));
2660 this.node.filterMatrix = E;
2661 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2664 a.scale = [x, y, cx, cy][join](S);
2670 Element[proto].clone = function () {
2671 var attr = this.attr();
2673 delete attr.translation;
2674 return this.paper[this.type]().attr(attr);
2677 // animation easing formulas
2678 R.easing_formulas = {
2679 linear: function (n) {
2686 return pow(n - 1, 3) + 1;
2688 "<>": function (n) {
2691 return pow(n, 3) / 2;
2694 return (pow(n, 3) + 2) / 2;
2696 backIn: function (n) {
2698 return n * n * ((s + 1) * n - s);
2700 backOut: function (n) {
2703 return n * n * ((s + 1) * n + s) + 1;
2705 elastic: function (n) {
2706 if (n == 0 || n == 1) {
2711 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2713 bounce: function (n) {
2722 l = s * n * n + .75;
2724 if (n < (2.5 / p)) {
2726 l = s * n * n + .9375;
2729 l = s * n * n + .984375;
2737 var animationElements = {length : 0},
2738 animation = function () {
2739 var Now = +new Date;
2740 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2741 var e = animationElements[l];
2743 delete animationElements[l];
2744 animationElements[length]--;
2747 var time = Now - e.start,
2756 callback = e.callback,
2760 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2761 for (var attr in from) if (from[has](attr)) {
2762 switch (availableAnimAttrs[attr]) {
2764 now = +from[attr] + pos * ms * diff[attr];
2768 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2769 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2770 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2775 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2776 now[i] = [from[attr][i][0]];
2777 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2778 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2780 now[i] = now[i][join](S);
2787 var x = diff[attr][0] * (time - prev),
2788 y = diff[attr][1] * (time - prev);
2794 now = +from[attr][0] + pos * ms * diff[attr][0];
2795 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2798 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);
2804 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2813 that._run && that._run.call(that);
2815 (t.x || t.y) && that.translate(-t.x, -t.y);
2816 to.scale && (to.scale = to.scale + E);
2818 delete animationElements[l];
2819 animationElements[length]--;
2820 that.in_animation = null;
2821 R.is(callback, "function") && callback.call(that);
2825 R.svg && that.paper.safari();
2826 animationElements[length] && setTimeout(animation);
2828 upto255 = function (color) {
2829 return color > 255 ? 255 : (color < 0 ? 0 : color);
2831 translate = function (x, y) {
2833 return {x: this._.tx, y: this._.ty, toString: x_y};
2837 switch (this.type) {
2840 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2845 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2848 var path = pathToRelative(this.attrs.path);
2851 this.attr({path: path});
2856 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2857 animationElements[element.id] && (params.start = animationElements[element.id].start);
2858 return this.animate(params, ms, easing, callback);
2860 Element[proto].onAnimation = function (f) {
2861 this._run = f || null;
2864 Element[proto].animate = function (params, ms, easing, callback) {
2865 if (R.is(easing, "function") || !easing) {
2866 callback = easing || null;
2871 for (var attr in params) if (params[has](attr)) {
2872 if (availableAnimAttrs[has](attr)) {
2873 from[attr] = this.attr(attr);
2874 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2875 to[attr] = params[attr];
2876 switch (availableAnimAttrs[attr]) {
2878 diff[attr] = (to[attr] - from[attr]) / ms;
2881 from[attr] = R.getRGB(from[attr]);
2882 var toColour = R.getRGB(to[attr]);
2884 r: (toColour.r - from[attr].r) / ms,
2885 g: (toColour.g - from[attr].g) / ms,
2886 b: (toColour.b - from[attr].b) / ms
2890 var pathes = path2curve(from[attr], to[attr]);
2891 from[attr] = pathes[0];
2892 var toPath = pathes[1];
2894 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2895 diff[attr][i] = [0];
2896 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2897 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2902 var values = (params[attr] + E)[split](separator),
2903 from2 = (from[attr] + E)[split](separator);
2906 from[attr] = [0, 0];
2907 diff[attr] = [values[0] / ms, values[1] / ms];
2910 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2911 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2914 params[attr] = values;
2915 from[attr] = (from[attr] + E)[split](separator);
2916 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2919 from[attr] = (from[attr] + E)[split](separator);
2923 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2932 this.in_animation = 1;
2933 animationElements[this.id] = {
2934 start: params.start || +new Date,
2944 ++animationElements[length] == 1 && animation();
2947 Element[proto].stop = function () {
2948 animationElements[this.id] && animationElements[length]--;
2949 delete animationElements[this.id];
2952 Element[proto].translate = function (x, y) {
2953 return this.attr({translation: x + " " + y});
2955 Element[proto][toString] = function () {
2956 return "Rapha\xebl\u2019s object";
2958 R.ae = animationElements;
2961 var Set = function (items) {
2965 for (var i = 0, ii = items[length]; i < ii; i++) {
2966 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
2967 this[this.items[length]] = this.items[this.items[length]] = items[i];
2973 Set[proto][push] = function () {
2976 for (var i = 0, ii = arguments[length]; i < ii; i++) {
2977 item = arguments[i];
2978 if (item && (item.constructor == Element || item.constructor == Set)) {
2979 len = this.items[length];
2980 this[len] = this.items[len] = item;
2986 Set[proto].pop = function () {
2987 delete this[this[length]--];
2988 return this.items.pop();
2990 for (var method in Element[proto]) if (Element[proto][has](method)) {
2991 Set[proto][method] = (function (methodname) {
2992 return function () {
2993 for (var i = 0, ii = this.items[length]; i < ii; i++) {
2994 this.items[i][methodname][apply](this.items[i], arguments);
3000 Set[proto].attr = function (name, value) {
3001 if (name && R.is(name, "array") && R.is(name[0], "object")) {
3002 for (var j = 0, jj = name[length]; j < jj; j++) {
3003 this.items[j].attr(name[j]);
3006 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3007 this.items[i].attr[apply](this.items[i], arguments);
3012 Set[proto].animate = function (params, ms, easing, callback) {
3013 (R.is(easing, "function") || !easing) && (callback = easing || null);
3014 var len = this.items[length],
3018 callback && (collector = function () {
3019 !--len && callback.call(set);
3021 this.items[--i].animate(params, ms, easing || collector, collector);
3023 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3027 Set[proto].insertAfter = function (el) {
3028 var i = this.items[length];
3030 this.items[i].insertAfter(el);
3034 Set[proto].getBBox = function () {
3039 for (var i = this.items[length]; i--;) {
3040 var box = this.items[i].getBBox();
3043 w[push](box.x + box.width);
3044 h[push](box.y + box.height);
3046 x = mmin[apply](0, x);
3047 y = mmin[apply](0, y);
3051 width: mmax[apply](0, w) - x,
3052 height: mmax[apply](0, h) - y
3056 R.registerFont = function (font) {
3060 this.fonts = this.fonts || {};
3066 family = font.face["font-family"];
3067 for (var prop in font.face) if (font.face[has](prop)) {
3068 fontcopy.face[prop] = font.face[prop];
3070 if (this.fonts[family]) {
3071 this.fonts[family][push](fontcopy);
3073 this.fonts[family] = [fontcopy];
3076 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3077 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3078 var path = font.glyphs[glyph];
3079 fontcopy.glyphs[glyph] = {
3082 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3083 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3087 for (var k in path.k) if (path[has](k)) {
3088 fontcopy.glyphs[glyph].k[k] = path.k[k];
3095 Paper[proto].getFont = function (family, weight, style, stretch) {
3096 stretch = stretch || "normal";
3097 style = style || "normal";
3098 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3099 var font = R.fonts[family];
3101 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3102 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3103 if (name.test(fontName)) {
3104 font = R.fonts[fontName];
3111 for (var i = 0, ii = font[length]; i < ii; i++) {
3113 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3120 Paper[proto].print = function (x, y, string, font, size, origin) {
3121 origin = origin || "middle"; // baseline|middle
3122 var out = this.set(),
3123 letters = (string + E)[split](E),
3127 R.is(font, "string") && (font = this.getFont(font));
3129 scale = (size || 16) / font.face["units-per-em"];
3130 var bb = font.face.bbox.split(separator),
3132 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3133 for (var i = 0, ii = letters[length]; i < ii; i++) {
3134 var prev = i && font.glyphs[letters[i - 1]] || {},
3135 curr = font.glyphs[letters[i]];
3136 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3137 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3139 out.scale(scale, scale, top, height).translate(x - top, y - height);
3144 R.format = function (token) {
3145 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3147 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3148 return args[++i] == null ? E : args[i];
3152 R.ninja = function () {
3153 var r = win.Raphael, u;
3154 if (oldRaphael.was) {
3155 win.Raphael = oldRaphael.is;
3165 R.el = Element[proto];