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 = {along: "along", "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
64 R.version = "1.3.0dev";
65 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
66 if (R.type == "VML") {
67 var d = document.createElement("div");
68 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
69 if (d.childNodes[length] != 2) {
73 R.svg = !(R.vml = R.type == "VML");
74 Paper[proto] = R[proto];
78 R.is = function (o, type) {
79 type = lowerCase.call(type);
80 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
82 R.setWindow = function (newwin) {
87 var toHex = function (color) {
89 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
90 var trim = /^\s+|\s+$/g;
91 toHex = cacher(function (color) {
93 color = (color + E)[rp](trim, E);
95 var docum = new ActiveXObject("htmlfile");
96 docum.write("<body>");
100 bod = createPopup().document.body;
102 var range = bod.createTextRange();
104 bod.style.color = color;
105 var value = range.queryCommandValue("ForeColor");
106 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
107 return "#" + ("000000" + value[toString](16)).slice(-6);
113 var i = doc.createElement("i");
114 i.title = "Rapha\xebl Colour Picker";
115 i.style.display = "none";
116 doc.body[appendChild](i);
117 toHex = cacher(function (color) {
118 i.style.color = color;
119 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
124 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
125 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
133 if (brightness == 0) {
134 return {r: 0, g: 0, b: 0, hex: "#000"};
136 if (hue > 1 || saturation > 1 || brightness > 1) {
143 p = brightness * (1 - saturation),
144 q = brightness * (1 - (saturation * f)),
145 t = brightness * (1 - (saturation * (1 - f)));
146 red = [brightness, q, p, p, t, brightness, brightness][i];
147 green = [t, brightness, brightness, q, p, p, t][i];
148 blue = [p, p, t, brightness, brightness, q, p][i];
152 var rgb = {r: red, g: green, b: blue},
153 r = (~~red)[toString](16),
154 g = (~~green)[toString](16),
155 b = (~~blue)[toString](16);
159 rgb.hex = "#" + r + g + b;
162 R.rgb2hsb = cacher(function (red, green, blue) {
163 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
168 if (R.is(red, "string")) {
169 var clr = R.getRGB(red);
174 if (red > 1 || green > 1 || blue > 1) {
179 var max = mmax(red, green, blue),
180 min = mmin(red, green, blue),
185 return {h: 0, s: 0, b: max};
187 var delta = (max - min);
188 saturation = delta / max;
190 hue = (green - blue) / delta;
191 } else if (green == max) {
192 hue = 2 + ((blue - red) / delta);
194 hue = 4 + ((red - green) / delta);
200 return {h: hue, s: saturation, b: brightness};
202 var p2s = /,?([achlmqrstvxz]),?/gi;
203 R._path2string = function () {
204 return this.join(",")[rp](p2s, "$1");
206 function cacher(f, scope, postprocessor) {
208 var arg = Array[proto].slice.call(arguments, 0),
209 args = arg[join]("\u25ba"),
210 cache = newf.cache = newf.cache || {},
211 count = newf.count = newf.count || [];
212 if (cache[has](args)) {
213 return postprocessor ? postprocessor(cache[args]) : cache[args];
215 count[length] >= 1e3 && delete cache[count.shift()];
217 cache[args] = f[apply](scope, arg);
218 return postprocessor ? postprocessor(cache[args]) : cache[args];
223 R.getRGB = cacher(function (colour) {
224 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
225 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
227 if (colour == "none") {
228 return {r: -1, g: -1, b: -1, hex: "none"};
230 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
236 rgb = colour.match(colourRegExp);
239 blue = toInt(rgb[2].substring(5), 16);
240 green = toInt(rgb[2].substring(3, 5), 16);
241 red = toInt(rgb[2].substring(1, 3), 16);
244 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
245 green = toInt((t = rgb[3].charAt(2)) + t, 16);
246 red = toInt((t = rgb[3].charAt(1)) + t, 16);
249 rgb = rgb[4][split](/\s*,\s*/);
250 red = toFloat(rgb[0]);
251 green = toFloat(rgb[1]);
252 blue = toFloat(rgb[2]);
255 rgb = rgb[5][split](/\s*,\s*/);
256 red = toFloat(rgb[0]) * 2.55;
257 green = toFloat(rgb[1]) * 2.55;
258 blue = toFloat(rgb[2]) * 2.55;
261 rgb = rgb[6][split](/\s*,\s*/);
262 red = toFloat(rgb[0]);
263 green = toFloat(rgb[1]);
264 blue = toFloat(rgb[2]);
265 return R.hsb2rgb(red, green, blue);
268 rgb = rgb[7][split](/\s*,\s*/);
269 red = toFloat(rgb[0]) * 2.55;
270 green = toFloat(rgb[1]) * 2.55;
271 blue = toFloat(rgb[2]) * 2.55;
272 return R.hsb2rgb(red, green, blue);
274 rgb = {r: red, g: green, b: blue};
275 var r = (~~red)[toString](16),
276 g = (~~green)[toString](16),
277 b = (~~blue)[toString](16);
281 rgb.hex = "#" + r + g + b;
284 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
286 R.getColor = function (value) {
287 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
288 rgb = this.hsb2rgb(start.h, start.s, start.b);
293 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
297 R.getColor.reset = function () {
301 R.parsePathString = cacher(function (pathString) {
305 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
307 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
308 data = pathClone(pathString);
311 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
313 name = lowerCase.call(b);
314 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
315 b && params[push](+b);
317 while (params[length] >= paramCounts[name]) {
318 data[push]([b][concat](params.splice(0, paramCounts[name])));
319 if (!paramCounts[name]) {
325 data[toString] = R._path2string;
328 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
330 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
331 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
332 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
333 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
334 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
335 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
336 ax = (1 - t) * p1x + t * c1x,
337 ay = (1 - t) * p1y + t * c1y,
338 cx = (1 - t) * c2x + t * p2x,
339 cy = (1 - t) * c2y + t * p2y,
340 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
341 (mx > nx || my < ny) && (alpha += 180);
342 return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
344 var pathDimensions = cacher(function (path) {
346 return {x: 0, y: 0, width: 0, height: 0};
348 path = path2curve(path);
354 for (var i = 0, ii = path[length]; i < ii; i++) {
362 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
363 X = X[concat](dim.min.x, dim.max.x);
364 Y = Y[concat](dim.min.y, dim.max.y);
369 var xmin = mmin[apply](0, X),
370 ymin = mmin[apply](0, Y);
374 width: mmax[apply](0, X) - xmin,
375 height: mmax[apply](0, Y) - ymin
378 pathClone = function (pathArray) {
380 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
381 pathArray = R.parsePathString(pathArray);
383 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
385 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
386 res[i][j] = pathArray[i][j];
389 res[toString] = R._path2string;
392 pathToRelative = cacher(function (pathArray) {
393 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
394 pathArray = R.parsePathString(pathArray);
402 if (pathArray[0][0] == "M") {
408 res[push](["M", x, y]);
410 for (var i = start, ii = pathArray[length]; i < ii; i++) {
413 if (pa[0] != lowerCase.call(pa[0])) {
414 r[0] = lowerCase.call(pa[0]);
422 r[6] = +(pa[6] - x).toFixed(3);
423 r[7] = +(pa[7] - y).toFixed(3);
426 r[1] = +(pa[1] - y).toFixed(3);
432 for (var j = 1, jj = pa[length]; j < jj; j++) {
433 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
442 for (var k = 0, kk = pa[length]; k < kk; k++) {
446 var len = res[i][length];
453 x += +res[i][len - 1];
456 y += +res[i][len - 1];
459 x += +res[i][len - 2];
460 y += +res[i][len - 1];
463 res[toString] = R._path2string;
466 pathToAbsolute = cacher(function (pathArray) {
467 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
468 pathArray = R.parsePathString(pathArray);
476 if (pathArray[0][0] == "M") {
477 x = +pathArray[0][1];
478 y = +pathArray[0][2];
482 res[0] = ["M", x, y];
484 for (var i = start, ii = pathArray[length]; i < ii; i++) {
487 if (pa[0] != upperCase.call(pa[0])) {
488 r[0] = upperCase.call(pa[0]);
509 for (var j = 1, jj = pa[length]; j < jj; j++) {
510 r[j] = +pa[j] + ((j % 2) ? x : y);
514 for (var k = 0, kk = pa[length]; k < kk; k++) {
530 x = res[i][res[i][length] - 2];
531 y = res[i][res[i][length] - 1];
534 res[toString] = R._path2string;
537 l2c = function (x1, y1, x2, y2) {
538 return [x1, y1, x2, y2, x2, y2];
540 q2c = function (x1, y1, ax, ay, x2, y2) {
552 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
553 // for more information of where this math came from visit:
554 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
556 _120 = PI * 120 / 180,
557 rad = PI / 180 * (+angle || 0),
560 rotate = cacher(function (x, y, rad) {
561 var X = x * math.cos(rad) - y * math.sin(rad),
562 Y = x * math.sin(rad) + y * math.cos(rad);
566 xy = rotate(x1, y1, -rad);
569 xy = rotate(x2, y2, -rad);
572 var cos = math.cos(PI / 180 * angle),
573 sin = math.sin(PI / 180 * angle),
576 rx = mmax(rx, math.abs(x));
577 ry = mmax(ry, math.abs(y));
580 k = (large_arc_flag == sweep_flag ? -1 : 1) *
581 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
582 cx = k * rx * y / ry + (x1 + x2) / 2,
583 cy = k * -ry * x / rx + (y1 + y2) / 2,
584 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
585 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
587 f1 = x1 < cx ? PI - f1 : f1;
588 f2 = x2 < cx ? PI - f2 : f2;
589 f1 < 0 && (f1 = PI * 2 + f1);
590 f2 < 0 && (f2 = PI * 2 + f2);
591 if (sweep_flag && f1 > f2) {
594 if (!sweep_flag && f2 > f1) {
604 if (math.abs(df) > _120) {
608 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
609 x2 = cx + rx * math.cos(f2);
610 y2 = cy + ry * math.sin(f2);
611 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
614 var c1 = math.cos(f1),
618 t = math.tan(df / 4),
622 m2 = [x1 + hx * s1, y1 - hy * c1],
623 m3 = [x2 + hx * s2, y2 - hy * c2],
625 m2[0] = 2 * m1[0] - m2[0];
626 m2[1] = 2 * m1[1] - m2[1];
628 return [m2, m3, m4][concat](res);
630 res = [m2, m3, m4][concat](res)[join]()[split](",");
632 for (var i = 0, ii = res[length]; i < ii; i++) {
633 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
638 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
641 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
642 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
645 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
646 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
647 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
649 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
650 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
654 math.abs(t1) > 1e12 && (t1 = .5);
655 math.abs(t2) > 1e12 && (t2 = .5);
656 if (t1 > 0 && t1 < 1) {
657 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
661 if (t2 > 0 && t2 < 1) {
662 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
666 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
667 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
669 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
670 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
671 math.abs(t1) > 1e12 && (t1 = .5);
672 math.abs(t2) > 1e12 && (t2 = .5);
673 if (t1 > 0 && t1 < 1) {
674 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
678 if (t2 > 0 && t2 < 1) {
679 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
684 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
685 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
688 path2curve = cacher(function (path, path2) {
689 var p = pathToAbsolute(path),
690 p2 = path2 && pathToAbsolute(path2),
691 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
692 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
693 processPath = function (path, d) {
696 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
698 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
705 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
708 nx = d.x + (d.x - (d.bx || d.x));
709 ny = d.y + (d.y - (d.by || d.y));
710 path = ["C", nx, ny][concat](path.slice(1));
713 d.qx = d.x + (d.x - (d.qx || d.x));
714 d.qy = d.y + (d.y - (d.qy || d.y));
715 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
720 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
723 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
726 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
729 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
732 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
737 fixArc = function (pp, i) {
738 if (pp[i][length] > 7) {
742 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
745 ii = mmax(p[length], p2 && p2[length] || 0);
748 fixM = function (path1, path2, a1, a2, i) {
749 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
750 path2.splice(i, 0, ["M", a2.x, a2.y]);
755 ii = mmax(p[length], p2 && p2[length] || 0);
758 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
759 p[i] = processPath(p[i], attrs);
761 p2 && (p2[i] = processPath(p2[i], attrs2));
763 fixM(p, p2, attrs, attrs2, i);
764 fixM(p2, p, attrs2, attrs, i);
767 seglen = seg[length],
768 seg2len = p2 && seg2[length];
769 attrs.x = seg[seglen - 2];
770 attrs.y = seg[seglen - 1];
771 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
772 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
773 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
774 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
775 attrs2.x = p2 && seg2[seg2len - 2];
776 attrs2.y = p2 && seg2[seg2len - 1];
778 return p2 ? [p, p2] : p;
780 parseDots = cacher(function (gradient) {
782 for (var i = 0, ii = gradient[length]; i < ii; i++) {
784 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
785 dot.color = R.getRGB(par[1]);
786 if (dot.color.error) {
789 dot.color = dot.color.hex;
790 par[2] && (dot.offset = par[2] + "%");
793 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
794 if (!dots[i].offset) {
795 var start = toFloat(dots[i - 1].offset || 0),
797 for (var j = i + 1; j < ii; j++) {
798 if (dots[j].offset) {
799 end = dots[j].offset;
808 var d = (end - start) / (j - i + 1);
811 dots[i].offset = start + "%";
817 getContainer = function () {
823 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
824 if (R.is(arguments[0], "string")) {
825 container = doc.getElementById(arguments[0]);
827 container = arguments[0];
829 if (container.tagName) {
830 if (arguments[1] == null) {
832 container: container,
833 width: container.style.pixelWidth || container.offsetWidth,
834 height: container.style.pixelHeight || container.offsetHeight
837 return {container: container, width: arguments[1], height: arguments[2]};
840 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
841 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
844 plugins = function (con, add) {
846 for (var prop in add) if (add[has](prop) && !(prop in con)) {
847 switch (typeof add[prop]) {
850 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
854 con[prop] = con[prop] || {};
855 plugins.call(this, con[prop], add[prop]);
858 con[prop] = add[prop];
863 tear = function (el, paper) {
864 el == paper.top && (paper.top = el.prev);
865 el == paper.bottom && (paper.bottom = el.next);
866 el.next && (el.next.prev = el.prev);
867 el.prev && (el.prev.next = el.next);
869 tofront = function (el, paper) {
870 if (paper.top === el) {
879 toback = function (el, paper) {
880 if (paper.bottom === el) {
884 el.next = paper.bottom;
886 paper.bottom.prev = el;
889 insertafter = function (el, el2, paper) {
891 el2 == paper.top && (paper.top = el);
892 el2.next && (el2.next.prev = el);
897 insertbefore = function (el, el2, paper) {
899 el2 == paper.bottom && (paper.bottom = el);
900 el2.prev && (el2.prev.next = el);
905 removed = function (methodname) {
907 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
910 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
914 Paper[proto].svgns = "http://www.w3.org/2000/svg";
915 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
916 var round = function (num) {
917 return +num + (~~num === num) * .5;
919 roundPath = function (path) {
920 for (var i = 0, ii = path[length]; i < ii; i++) {
921 if (lowerCase.call(path[i][0]) != "a") {
922 for (var j = 1, jj = path[i][length]; j < jj; j++) {
923 path[i][j] = round(path[i][j]);
926 path[i][6] = round(path[i][6]);
927 path[i][7] = round(path[i][7]);
932 $ = function (el, attr) {
934 for (var key in attr) if (attr[has](key)) {
935 el[setAttribute](key, attr[key]);
938 return doc.createElementNS(Paper[proto].svgns, el);
941 R[toString] = function () {
942 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
944 var thePath = function (pathString, SVG) {
946 SVG.canvas && SVG.canvas[appendChild](el);
947 var p = new Element(el, SVG);
949 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
952 var addGradientFill = function (o, gradient, SVG) {
956 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
961 var dir = ((fy > .5) * 2 - 1);
962 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
963 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
965 (fy = fy.toFixed(5) - 1e-5 * dir);
969 gradient = gradient[split](/\s*\-\s*/);
970 if (type == "linear") {
971 var angle = gradient.shift();
972 angle = -toFloat(angle);
976 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
977 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
981 vector[0] = -vector[2];
985 vector[1] = -vector[3];
989 var dots = parseDots(gradient);
993 var el = $(type + "Gradient");
994 el.id = "r" + (R._id++)[toString](36);
995 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
996 SVG.defs[appendChild](el);
997 for (var i = 0, ii = dots[length]; i < ii; i++) {
998 var stop = $("stop");
1000 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1001 "stop-color": dots[i].color || "#fff"
1003 el[appendChild](stop);
1006 fill: "url(#" + el.id + ")",
1015 var updatePosition = function (o) {
1016 var bbox = o.getBBox();
1017 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1019 var setFillAndStroke = function (o, params) {
1026 "-..": [3, 1, 1, 1, 1, 1],
1030 "- .": [4, 3, 1, 3],
1031 "--.": [8, 3, 1, 3],
1032 "--..": [8, 3, 1, 3, 1, 3]
1037 addDashes = function (o, value) {
1038 value = dasharray[lowerCase.call(value)];
1040 var width = o.attrs["stroke-width"] || "1",
1041 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1043 var i = value[length];
1045 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1047 $(node, {"stroke-dasharray": dashes[join](",")});
1050 params[has]("rotation") && (rot = params.rotation);
1051 var rotxy = (rot + E)[split](separator);
1052 if (!(rotxy.length - 1)) {
1055 rotxy[1] = +rotxy[1];
1056 rotxy[2] = +rotxy[2];
1058 toFloat(rot) && o.rotate(0, true);
1059 for (var att in params) if (params[has](att)) {
1060 if (!availableAttrs[has](att)) {
1063 var value = params[att];
1067 o.rotate(value, true);
1073 var pn = node.parentNode;
1074 if (lowerCase.call(pn.tagName) != "a") {
1076 pn.insertBefore(hl, node);
1077 hl[appendChild](node);
1080 pn.setAttributeNS(o.Paper[proto].xlink, att, value);
1083 node.style.cursor = value;
1086 var rect = (value + E)[split](separator);
1087 if (rect[length] == 4) {
1088 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1089 var el = $("clipPath"),
1091 el.id = "r" + (R._id++)[toString](36);
1098 el[appendChild](rc);
1099 o.paper.defs[appendChild](el);
1100 $(node, {"clip-path": "url(#" + el.id + ")"});
1104 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1105 clip && clip.parentNode.removeChild(clip);
1106 $(node, {"clip-path": E});
1111 if (value && o.type == "path") {
1112 attrs.path = roundPath(pathToAbsolute(value));
1113 $(node, {d: attrs.path});
1117 node[setAttribute](att, value);
1126 value = -attrs.x - (attrs.width || 0);
1129 if (att == "rx" && o.type == "rect") {
1133 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1134 node[setAttribute](att, round(value));
1135 o.pattern && updatePosition(o);
1138 node[setAttribute](att, value);
1147 value = -attrs.y - (attrs.height || 0);
1150 if (att == "ry" && o.type == "rect") {
1154 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1155 node[setAttribute](att, round(value));
1156 o.pattern && updatePosition(o);
1159 if (o.type == "rect") {
1160 $(node, {rx: value, ry: value});
1162 node[setAttribute](att, value);
1166 if (o.type == "image") {
1167 node.setAttributeNS(o.paper.xlink, "href", value);
1170 case "stroke-width":
1171 node.style.strokeWidth = value;
1172 // Need following line for Firefox
1173 node[setAttribute](att, value);
1174 if (attrs["stroke-dasharray"]) {
1175 addDashes(o, attrs["stroke-dasharray"]);
1178 case "stroke-dasharray":
1179 addDashes(o, value);
1182 var xy = (value + E)[split](separator);
1183 xy[0] = +xy[0] || 0;
1184 xy[1] = +xy[1] || 0;
1189 translate.call(o, xy[0], xy[1]);
1192 var xy = (value + E)[split](separator);
1193 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1196 var isURL = (value + E).match(ISURL);
1198 var el = $("pattern"),
1200 el.id = "r" + (R._id++)[toString](36);
1201 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1202 $(ig, {x: 0, y: 0});
1203 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1204 el[appendChild](ig);
1206 var img = doc.createElement("img");
1207 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1208 img.onload = function () {
1209 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1210 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1211 doc.body.removeChild(this);
1214 doc.body[appendChild](img);
1216 o.paper.defs[appendChild](el);
1217 node.style.fill = "url(#" + el.id + ")";
1218 $(node, {fill: "url(#" + el.id + ")"});
1220 o.pattern && updatePosition(o);
1223 if (!R.getRGB(value).error) {
1224 delete params.gradient;
1225 delete attrs.gradient;
1226 !R.is(attrs.opacity, "undefined") &&
1227 R.is(params.opacity, "undefined") &&
1228 $(node, {opacity: attrs.opacity});
1229 !R.is(attrs["fill-opacity"], "undefined") &&
1230 R.is(params["fill-opacity"], "undefined") &&
1231 $(node, {"fill-opacity": attrs["fill-opacity"]});
1232 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1233 attrs.gradient = value;
1234 attrs.fill = "none";
1238 node[setAttribute](att, R.getRGB(value).hex);
1241 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
1244 case "fill-opacity":
1245 if (attrs.gradient) {
1246 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
1248 var stops = gradient.getElementsByTagName("stop");
1249 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1254 att == "font-size" && (value = toInt(value, 10) + "px");
1255 var cssrule = att[rp](/(\-.)/g, function (w) {
1256 return upperCase.call(w.substring(1));
1258 node.style[cssrule] = value;
1259 // Need following line for Firefox
1260 node[setAttribute](att, value);
1265 tuneText(o, params);
1267 o.rotate(rotxy.join(S));
1269 toFloat(rot) && o.rotate(rot, true);
1273 var tuneText = function (el, params) {
1274 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1279 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1281 if (params[has]("text")) {
1282 a.text = params.text;
1283 while (node.firstChild) {
1284 node.removeChild(node.firstChild);
1286 var texts = (params.text + E)[split]("\n");
1287 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1288 var tspan = $("tspan");
1289 i && $(tspan, {dy: fontSize * leading, x: a.x});
1290 tspan[appendChild](doc.createTextNode(texts[i]));
1291 node[appendChild](tspan);
1294 var texts = node.getElementsByTagName("tspan");
1295 for (var i = 0, ii = texts[length]; i < ii; i++) {
1296 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1300 var bb = el.getBBox(),
1301 dif = a.y - (bb.y + bb.height / 2);
1302 dif && isFinite(dif) && $(node, {y: a.y + dif});
1304 var Element = function (node, svg) {
1310 node.raphael = this;
1312 this.attrs = this.attrs || {};
1313 this.transformations = []; // rotate, translate, scale
1317 rt: {deg: 0, cx: 0, cy: 0},
1321 !svg.bottom && (svg.bottom = this);
1322 this.prev = svg.top;
1323 svg.top && (svg.top.next = this);
1327 Element[proto].rotate = function (deg, cx, cy) {
1333 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1335 return this._.rt.deg;
1337 var bbox = this.getBBox();
1338 deg = (deg + E)[split](separator);
1339 if (deg[length] - 1) {
1340 cx = toFloat(deg[1]);
1341 cy = toFloat(deg[2]);
1343 deg = toFloat(deg[0]);
1345 this._.rt.deg = deg;
1347 this._.rt.deg += deg;
1349 (cy == null) && (cx = null);
1352 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1353 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1354 if (this._.rt.deg) {
1355 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1356 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1358 this.transformations[0] = E;
1359 this.clip && $(this.clip, {transform: E});
1361 $(this.node, {transform: this.transformations[join](S)});
1364 Element[proto].hide = function () {
1365 !this.removed && (this.node.style.display = "none");
1368 Element[proto].show = function () {
1369 !this.removed && (this.node.style.display = "");
1372 Element[proto].remove = function () {
1376 tear(this, this.paper);
1377 this.node.parentNode.removeChild(this.node);
1378 for (var i in this) {
1381 this.removed = true;
1383 Element[proto].getBBox = function () {
1387 if (this.type == "path") {
1388 return pathDimensions(this.attrs.path);
1390 if (this.node.style.display == "none") {
1396 bbox = this.node.getBBox();
1398 // Firefox 3.0.x plays badly here
1402 if (this.type == "text") {
1403 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1404 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1405 var bb = this.node.getExtentOfChar(i);
1406 (bb.y < bbox.y) && (bbox.y = bb.y);
1407 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1408 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1411 hide && this.hide();
1414 Element[proto].attr = function () {
1418 if (arguments[length] == 0) {
1420 for (var i in this.attrs) if (this.attrs[has](i)) {
1421 res[i] = this.attrs[i];
1423 this._.rt.deg && (res.rotation = this.rotate());
1424 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1425 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1428 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
1429 if (arguments[0] == "translation") {
1430 return translate.call(this);
1432 if (arguments[0] == "rotation") {
1433 return this.rotate();
1435 if (arguments[0] == "scale") {
1436 return this.scale();
1438 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1439 return this.attrs.gradient;
1441 return this.attrs[arguments[0]];
1443 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
1445 for (var j in arguments[0]) if (arguments[0][has](j)) {
1446 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1450 if (arguments[length] == 2) {
1452 params[arguments[0]] = arguments[1];
1453 setFillAndStroke(this, params);
1454 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
1455 setFillAndStroke(this, arguments[0]);
1459 Element[proto].toFront = function () {
1463 this.node.parentNode[appendChild](this.node);
1464 var svg = this.paper;
1465 svg.top != this && tofront(this, svg);
1468 Element[proto].toBack = function () {
1472 if (this.node.parentNode.firstChild != this.node) {
1473 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1474 toback(this, this.paper);
1475 var svg = this.paper;
1479 Element[proto].insertAfter = function (element) {
1483 var node = element.node;
1484 if (node.nextSibling) {
1485 node.parentNode.insertBefore(this.node, node.nextSibling);
1487 node.parentNode[appendChild](this.node);
1489 insertafter(this, element, this.paper);
1492 Element[proto].insertBefore = function (element) {
1496 var node = element.node;
1497 node.parentNode.insertBefore(this.node, node);
1498 insertbefore(this, element, this.paper);
1501 var theCircle = function (svg, x, y, r) {
1504 var el = $("circle");
1505 svg.canvas && svg.canvas[appendChild](el);
1506 var res = new Element(el, svg);
1507 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1508 res.type = "circle";
1512 var theRect = function (svg, x, y, w, h, r) {
1516 svg.canvas && svg.canvas[appendChild](el);
1517 var res = new Element(el, svg);
1518 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1523 var theEllipse = function (svg, x, y, rx, ry) {
1526 var el = $("ellipse");
1527 svg.canvas && svg.canvas[appendChild](el);
1528 var res = new Element(el, svg);
1529 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1530 res.type = "ellipse";
1534 var theImage = function (svg, src, x, y, w, h) {
1535 var el = $("image");
1536 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1537 el.setAttributeNS(svg.xlink, "href", src);
1538 svg.canvas && svg.canvas[appendChild](el);
1539 var res = new Element(el, svg);
1540 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1544 var theText = function (svg, x, y, text) {
1546 $(el, {x: x, y: y, "text-anchor": "middle"});
1547 svg.canvas && svg.canvas[appendChild](el);
1548 var res = new Element(el, svg);
1549 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1551 setFillAndStroke(res, res.attrs);
1554 var setSize = function (width, height) {
1555 this.width = width || this.width;
1556 this.height = height || this.height;
1557 this.canvas[setAttribute]("width", this.width);
1558 this.canvas[setAttribute]("height", this.height);
1561 var create = function () {
1562 var con = getContainer[apply](null, arguments),
1563 container = con && con.container,
1567 height = con.height;
1569 throw new Error("SVG container not found.");
1571 var cnvs = $("svg");
1572 width = width || 512;
1573 height = height || 342;
1575 xmlns: "http://www.w3.org/2000/svg",
1580 if (container == 1) {
1581 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1582 doc.body[appendChild](cnvs);
1584 if (container.firstChild) {
1585 container.insertBefore(cnvs, container.firstChild);
1587 container[appendChild](cnvs);
1590 container = new Paper;
1591 container.width = width;
1592 container.height = height;
1593 container.canvas = cnvs;
1594 plugins.call(container, container, R.fn);
1598 Paper[proto].clear = function () {
1599 var c = this.canvas;
1600 while (c.firstChild) {
1601 c.removeChild(c.firstChild);
1603 this.bottom = this.top = null;
1604 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1605 c[appendChild](this.desc);
1606 c[appendChild](this.defs = $("defs"));
1608 Paper[proto].remove = function () {
1609 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1610 for (var i in this) {
1611 this[i] = removed(i);
1618 var path2vml = function (path) {
1619 var total = /[ahqtv]/ig,
1620 command = pathToAbsolute;
1621 (path + E).match(total) && (command = path2curve);
1623 if (command == pathToAbsolute && !(path + E).match(total)) {
1624 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1625 bites = /([clmz]),?([^clmz]*)/gi,
1626 val = /-?[^,\s-]+/g;
1627 var res = (path + E)[rp](bites, function (all, command, args) {
1629 args[rp](val, function (value) {
1630 vals[push](round(value));
1632 return map[command] + vals;
1636 var pa = command(path), p, res = [], r;
1637 for (var i = 0, ii = pa[length]; i < ii; i++) {
1639 r = lowerCase.call(pa[i][0]);
1640 r == "z" && (r = "x");
1641 for (var j = 1, jj = p[length]; j < jj; j++) {
1642 r += round(p[j]) + (j != jj - 1 ? "," : E);
1646 return res[join](S);
1649 R[toString] = function () {
1650 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1652 var thePath = function (pathString, VML) {
1653 var g = createNode("group");
1654 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
1655 g.coordsize = VML.coordsize;
1656 g.coordorigin = VML.coordorigin;
1657 var el = createNode("shape"), ol = el.style;
1658 ol.width = VML.width + "px";
1659 ol.height = VML.height + "px";
1660 el.coordsize = this.coordsize;
1661 el.coordorigin = this.coordorigin;
1663 var p = new Element(el, g, VML);
1664 p.isAbsolute = true;
1668 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1669 VML.canvas[appendChild](g);
1672 var setFillAndStroke = function (o, params) {
1673 o.attrs = o.attrs || {};
1679 for (var par in params) if (params[has](par)) {
1680 a[par] = params[par];
1682 params.href && (node.href = params.href);
1683 params.title && (node.title = params.title);
1684 params.target && (node.target = params.target);
1685 params.cursor && (s.cursor = params.cursor);
1686 if (params.path && o.type == "path") {
1687 a.path = params.path;
1688 node.path = path2vml(a.path);
1690 if (params.rotation != null) {
1691 o.rotate(params.rotation, true);
1693 if (params.translation) {
1694 xy = (params.translation + E)[split](separator);
1695 translate.call(o, xy[0], xy[1]);
1696 if (o._.rt.cx != null) {
1697 o._.rt.cx +=+ xy[0];
1698 o._.rt.cy +=+ xy[1];
1699 o.setBox(o.attrs, xy[0], xy[1]);
1703 xy = (params.scale + E)[split](separator);
1704 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1706 if ("clip-rect" in params) {
1707 var rect = (params["clip-rect"] + E)[split](separator);
1708 if (rect[length] == 4) {
1709 rect[2] = +rect[2] + (+rect[0]);
1710 rect[3] = +rect[3] + (+rect[1]);
1711 var div = node.clipRect || doc.createElement("div"),
1713 group = node.parentNode;
1714 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1715 if (!node.clipRect) {
1716 dstyle.position = "absolute";
1719 dstyle.width = o.paper.width + "px";
1720 dstyle.height = o.paper.height + "px";
1721 group.parentNode.insertBefore(div, group);
1722 div[appendChild](group);
1723 node.clipRect = div;
1726 if (!params["clip-rect"]) {
1727 node.clipRect && (node.clipRect.style.clip = E);
1730 if (o.type == "image" && params.src) {
1731 node.src = params.src;
1733 if (o.type == "image" && params.opacity) {
1734 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1735 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1737 params.font && (s.font = params.font);
1738 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1739 params["font-size"] && (s.fontSize = params["font-size"]);
1740 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1741 params["font-style"] && (s.fontStyle = params["font-style"]);
1742 if (params.opacity != null ||
1743 params["stroke-width"] != null ||
1744 params.fill != null ||
1745 params.stroke != null ||
1746 params["stroke-width"] != null ||
1747 params["stroke-opacity"] != null ||
1748 params["fill-opacity"] != null ||
1749 params["stroke-dasharray"] != null ||
1750 params["stroke-miterlimit"] != null ||
1751 params["stroke-linejoin"] != null ||
1752 params["stroke-linecap"] != null) {
1753 node = o.shape || node;
1754 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
1756 !fill && (newfill = fill = createNode("fill"));
1757 if ("fill-opacity" in params || "opacity" in params) {
1758 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
1759 opacity < 0 && (opacity = 0);
1760 opacity > 1 && (opacity = 1);
1761 fill.opacity = opacity;
1763 params.fill && (fill.on = true);
1764 if (fill.on == null || params.fill == "none") {
1767 if (fill.on && params.fill) {
1768 var isURL = params.fill.match(ISURL);
1770 fill.src = isURL[1];
1773 fill.color = R.getRGB(params.fill).hex;
1775 fill.type = "solid";
1776 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
1778 a.gradient = params.fill;
1782 newfill && node[appendChild](fill);
1783 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1785 !stroke && (newstroke = stroke = createNode("stroke"));
1786 if ((params.stroke && params.stroke != "none") ||
1787 params["stroke-width"] ||
1788 params["stroke-opacity"] != null ||
1789 params["stroke-dasharray"] ||
1790 params["stroke-miterlimit"] ||
1791 params["stroke-linejoin"] ||
1792 params["stroke-linecap"]) {
1795 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1796 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
1797 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
1798 width = (toFloat(params["stroke-width"]) || 1) * .75;
1799 opacity < 0 && (opacity = 0);
1800 opacity > 1 && (opacity = 1);
1801 params["stroke-width"] == null && (width = a["stroke-width"]);
1802 params["stroke-width"] && (stroke.weight = width);
1803 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1804 stroke.opacity = opacity;
1806 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1807 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1808 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1809 if (params["stroke-dasharray"]) {
1813 "-.": "shortdashdot",
1814 "-..": "shortdashdotdot",
1819 "--.": "longdashdot",
1820 "--..": "longdashdotdot"
1822 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1824 newstroke && node[appendChild](stroke);
1826 if (res.type == "text") {
1827 var s = res.paper.span.style;
1828 a.font && (s.font = a.font);
1829 a["font-family"] && (s.fontFamily = a["font-family"]);
1830 a["font-size"] && (s.fontSize = a["font-size"]);
1831 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1832 a["font-style"] && (s.fontStyle = a["font-style"]);
1833 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1834 res.W = a.w = res.paper.span.offsetWidth;
1835 res.H = a.h = res.paper.span.offsetHeight;
1837 res.Y = a.y + round(res.H / 2);
1839 // text-anchor emulationm
1840 switch (a["text-anchor"]) {
1842 res.node.style["v-text-align"] = "left";
1843 res.bbx = round(res.W / 2);
1846 res.node.style["v-text-align"] = "right";
1847 res.bbx = -round(res.W / 2);
1850 res.node.style["v-text-align"] = "center";
1855 var addGradientFill = function (o, gradient) {
1856 o.attrs = o.attrs || {};
1857 var attrs = o.attrs,
1858 fill = o.node.getElementsByTagName("fill"),
1861 o.attrs.gradient = gradient;
1862 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
1867 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1872 gradient = gradient[split](/\s*\-\s*/);
1873 if (type == "linear") {
1874 var angle = gradient.shift();
1875 angle = -toFloat(angle);
1880 var dots = parseDots(gradient);
1884 o = o.shape || o.node;
1885 fill = fill[0] || createNode("fill");
1888 fill.method = "none";
1889 fill.type = (type == "radial") ? "gradientradial" : "gradient";
1890 fill.color = dots[0].color;
1891 fill.color2 = dots[dots[length] - 1].color;
1893 for (var i = 0, ii = dots[length]; i < ii; i++) {
1894 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1896 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
1897 if (type == "radial") {
1898 fill.focus = "100%";
1899 fill.focussize = fxfy;
1900 fill.focusposition = fxfy;
1902 fill.angle = (270 - angle) % 360;
1907 var Element = function (node, group, vml) {
1915 node.raphael = this;
1928 !vml.bottom && (vml.bottom = this);
1929 this.prev = vml.top;
1930 vml.top && (vml.top.next = this);
1934 Element[proto].rotate = function (deg, cx, cy) {
1940 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1942 return this._.rt.deg;
1944 deg = (deg + E)[split](separator);
1945 if (deg[length] - 1) {
1946 cx = toFloat(deg[1]);
1947 cy = toFloat(deg[2]);
1949 deg = toFloat(deg[0]);
1951 this._.rt.deg = deg;
1953 this._.rt.deg += deg;
1955 cy == null && (cx = null);
1958 this.setBox(this.attrs, cx, cy);
1959 this.Group.style.rotation = this._.rt.deg;
1960 // gradient fix for rotation. TODO
1961 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1962 // fill = fill[0] || {};
1963 // var b = ((360 - this._.rt.deg) - 270) % 360;
1964 // !R.is(fill.angle, "undefined") && (fill.angle = b);
1967 Element[proto].setBox = function (params, cx, cy) {
1971 var gs = this.Group.style,
1972 os = (this.shape && this.shape.style) || this.node.style;
1973 params = params || {};
1974 for (var i in params) if (params[has](i)) {
1975 this.attrs[i] = params[i];
1977 cx = cx || this._.rt.cx;
1978 cy = cy || this._.rt.cy;
1979 var attr = this.attrs,
1984 switch (this.type) {
1986 x = attr.cx - attr.r;
1987 y = attr.cy - attr.r;
1991 x = attr.cx - attr.rx;
1992 y = attr.cy - attr.ry;
2000 w = attr.width || 0;
2001 h = attr.height || 0;
2004 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2005 x = attr.x - round(this.W / 2);
2006 y = attr.y - this.H / 2;
2011 if (!this.attrs.path) {
2014 w = this.paper.width;
2015 h = this.paper.height;
2017 var dim = pathDimensions(this.attrs.path);
2027 w = this.paper.width;
2028 h = this.paper.height;
2031 cx = (cx == null) ? x + w / 2 : cx;
2032 cy = (cy == null) ? y + h / 2 : cy;
2033 var left = cx - this.paper.width / 2,
2034 top = cy - this.paper.height / 2;
2035 if (this.type == "path" || this.type == "text") {
2036 (gs.left != left + "px") && (gs.left = left + "px");
2037 (gs.top != top + "px") && (gs.top = top + "px");
2038 this.X = this.type == "text" ? x : -left;
2039 this.Y = this.type == "text" ? y : -top;
2042 (os.left != -left + "px") && (os.left = -left + "px");
2043 (os.top != -top + "px") && (os.top = -top + "px");
2045 (gs.left != left + "px") && (gs.left = left + "px");
2046 (gs.top != top + "px") && (gs.top = top + "px");
2051 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
2052 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
2053 (os.left != x - left + "px") && (os.left = x - left + "px");
2054 (os.top != y - top + "px") && (os.top = y - top + "px");
2055 (os.width != w + "px") && (os.width = w + "px");
2056 (os.height != h + "px") && (os.height = h + "px");
2057 var arcsize = (+params.r || 0) / mmin(w, h);
2058 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
2059 // We should replace element with the new one
2060 var o = createNode("roundrect"),
2063 ii = this.events && this.events[length];
2064 o.arcsize = arcsize;
2066 this.Group[appendChild](o);
2067 this.Group.removeChild(this.node);
2068 this[0] = this.node = o;
2069 this.arcsize = arcsize;
2070 for (var i in attr) {
2075 if (this.events) for (; i < ii; i++) {
2076 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
2081 Element[proto].hide = function () {
2082 !this.removed && (this.Group.style.display = "none");
2085 Element[proto].show = function () {
2086 !this.removed && (this.Group.style.display = "block");
2089 Element[proto].getBBox = function () {
2093 if (this.type == "path") {
2094 return pathDimensions(this.attrs.path);
2097 x: this.X + (this.bbx || 0),
2103 Element[proto].remove = function () {
2107 tear(this, this.paper);
2108 this.node.parentNode.removeChild(this.node);
2109 this.Group.parentNode.removeChild(this.Group);
2110 this.shape && this.shape.parentNode.removeChild(this.shape);
2111 for (var i in this) {
2114 this.removed = true;
2116 Element[proto].attr = function () {
2120 if (arguments[length] == 0) {
2122 for (var i in this.attrs) if (this.attrs[has](i)) {
2123 res[i] = this.attrs[i];
2125 this._.rt.deg && (res.rotation = this.rotate());
2126 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2127 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2130 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
2131 if (arguments[0] == "translation") {
2132 return translate.call(this);
2134 if (arguments[0] == "rotation") {
2135 return this.rotate();
2137 if (arguments[0] == "scale") {
2138 return this.scale();
2140 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
2141 return this.attrs.gradient;
2143 return this.attrs[arguments[0]];
2145 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
2147 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
2148 values[arguments[0][i]] = this.attrs[arguments[0][i]];
2153 if (arguments[length] == 2) {
2155 params[arguments[0]] = arguments[1];
2157 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
2159 if (params.text && this.type == "text") {
2160 this.node.string = params.text;
2162 setFillAndStroke(this, params);
2163 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
2164 addGradientFill(this, params.gradient);
2166 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
2170 Element[proto].toFront = function () {
2171 !this.removed && this.Group.parentNode[appendChild](this.Group);
2172 this.paper.top != this && tofront(this, this.paper);
2175 Element[proto].toBack = function () {
2179 if (this.Group.parentNode.firstChild != this.Group) {
2180 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2181 toback(this, this.paper);
2185 Element[proto].insertAfter = function (element) {
2189 if (element.Group.nextSibling) {
2190 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2192 element.Group.parentNode[appendChild](this.Group);
2194 insertafter(this, element, this.paper);
2197 Element[proto].insertBefore = function (element) {
2201 element.Group.parentNode.insertBefore(this.Group, element.Group);
2202 insertbefore(this, element, this.paper);
2206 var theCircle = function (vml, x, y, r) {
2207 var g = createNode("group"),
2208 o = createNode("oval"),
2210 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2211 g.coordsize = vml.coordsize;
2212 g.coordorigin = vml.coordorigin;
2214 var res = new Element(o, g, vml);
2215 res.type = "circle";
2216 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2220 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2221 vml.canvas[appendChild](g);
2224 theRect = function (vml, x, y, w, h, r) {
2225 var g = createNode("group"),
2226 o = createNode("roundrect"),
2227 arcsize = (+r || 0) / (mmin(w, h));
2228 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2229 g.coordsize = vml.coordsize;
2230 g.coordorigin = vml.coordorigin;
2232 o.arcsize = arcsize;
2233 var res = new Element(o, g, vml);
2235 setFillAndStroke(res, {stroke: "#000"});
2236 res.arcsize = arcsize;
2237 res.setBox({x: x, y: y, width: w, height: h, r: r});
2238 vml.canvas[appendChild](g);
2241 theEllipse = function (vml, x, y, rx, ry) {
2242 var g = createNode("group"),
2243 o = createNode("oval"),
2245 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2246 g.coordsize = vml.coordsize;
2247 g.coordorigin = vml.coordorigin;
2249 var res = new Element(o, g, vml);
2250 res.type = "ellipse";
2251 setFillAndStroke(res, {stroke: "#000"});
2256 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2257 vml.canvas[appendChild](g);
2260 theImage = function (vml, src, x, y, w, h) {
2261 var g = createNode("group"),
2262 o = createNode("image"),
2264 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2265 g.coordsize = vml.coordsize;
2266 g.coordorigin = vml.coordorigin;
2269 var res = new Element(o, g, vml);
2271 res.attrs.src = src;
2276 res.setBox({x: x, y: y, width: w, height: h});
2277 vml.canvas[appendChild](g);
2280 theText = function (vml, x, y, text) {
2281 var g = createNode("group"),
2282 el = createNode("shape"),
2284 path = createNode("path"),
2286 o = createNode("textpath");
2287 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2288 g.coordsize = vml.coordsize;
2289 g.coordorigin = vml.coordorigin;
2290 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
2291 path.textpathok = true;
2292 ol.width = vml.width;
2293 ol.height = vml.height;
2294 o.string = text + E;
2297 el[appendChild](path);
2299 var res = new Element(o, g, vml);
2301 res.textpath = path;
2303 res.attrs.text = text;
2308 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2310 vml.canvas[appendChild](g);
2313 setSize = function (width, height) {
2314 var cs = this.canvas.style;
2315 width == +width && (width += "px");
2316 height == +height && (height += "px");
2319 cs.clip = "rect(0 " + width + " " + height + " 0)";
2323 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2325 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2326 createNode = function (tagName) {
2327 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2330 createNode = function (tagName) {
2331 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2334 var create = function () {
2335 var con = getContainer[apply](null, arguments),
2336 container = con.container,
2337 height = con.height,
2343 throw new Error("VML container not found.");
2345 var res = new Paper,
2346 c = res.canvas = doc.createElement("div"),
2348 width = width || 512;
2349 height = height || 342;
2350 width == +width && (width += "px");
2351 height == +height && (height += "px");
2354 res.coordsize = "1000 1000";
2355 res.coordorigin = "0 0";
2356 res.span = doc.createElement("span");
2357 res.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2358 c[appendChild](res.span);
2359 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2360 if (container == 1) {
2361 doc.body[appendChild](c);
2365 container.style.width = width;
2366 container.style.height = height;
2367 if (container.firstChild) {
2368 container.insertBefore(c, container.firstChild);
2370 container[appendChild](c);
2373 plugins.call(res, res, R.fn);
2376 Paper[proto].clear = function () {
2377 this.canvas.innerHTML = E;
2378 this.span = doc.createElement("span");
2379 this.span.style.cssText = "position:absolute;left:-9999px;top:-9999px;padding:0;margin:0;line-height:1;display:inline;";
2380 this.canvas[appendChild](this.span);
2381 this.bottom = this.top = null;
2383 Paper[proto].remove = function () {
2384 this.canvas.parentNode.removeChild(this.canvas);
2385 for (var i in this) {
2386 this[i] = removed(i);
2392 // Safari or Chrome (WebKit) rendering bug workaround method
2393 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
2394 Paper[proto].safari = function () {
2395 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2396 setTimeout(function () {rect.remove();});
2399 Paper[proto].safari = function () {};
2403 var addEvent = (function () {
2404 if (doc.addEventListener) {
2405 return function (obj, type, fn, element) {
2406 var f = function (e) {
2407 return fn.call(element, e);
2409 obj.addEventListener(type, f, false);
2410 return function () {
2411 obj.removeEventListener(type, f, false);
2415 } else if (doc.attachEvent) {
2416 return function (obj, type, fn, element) {
2417 var f = function (e) {
2418 return fn.call(element, e || win.event);
2420 obj.attachEvent("on" + type, f);
2421 var detacher = function () {
2422 obj.detachEvent("on" + type, f);
2429 for (var i = events[length]; i--;) {
2430 (function (eventName) {
2431 Element[proto][eventName] = function (fn) {
2432 if (R.is(fn, "function")) {
2433 this.events = this.events || [];
2434 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
2438 Element[proto]["un" + eventName] = function (fn) {
2439 var events = this.events,
2441 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2443 events.splice(l, 1);
2444 !events.length && delete this.events;
2451 Element[proto].hover = function (f_in, f_out) {
2452 return this.mouseover(f_in).mouseout(f_out);
2454 Element[proto].unhover = function (f_in, f_out) {
2455 return this.unmouseover(f_in).unmouseout(f_out);
2457 Paper[proto].circle = function (x, y, r) {
2458 return theCircle(this, x || 0, y || 0, r || 0);
2460 Paper[proto].rect = function (x, y, w, h, r) {
2461 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2463 Paper[proto].ellipse = function (x, y, rx, ry) {
2464 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2466 Paper[proto].path = function (pathString) {
2467 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
2468 return thePath(R.format[apply](R, arguments), this);
2470 Paper[proto].image = function (src, x, y, w, h) {
2471 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2473 Paper[proto].text = function (x, y, text) {
2474 return theText(this, x || 0, y || 0, text || E);
2476 Paper[proto].set = function (itemsArray) {
2477 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2478 return new Set(itemsArray);
2480 Paper[proto].setSize = setSize;
2481 Paper[proto].top = Paper[proto].bottom = null;
2482 Paper[proto].raphael = R;
2484 return this.x + S + this.y;
2486 Element[proto].scale = function (x, y, cx, cy) {
2487 if (x == null && y == null) {
2502 var bb = this.getBBox(),
2503 rcx = bb.x + bb.width / 2,
2504 rcy = bb.y + bb.height / 2,
2507 cx = (+cx || cx == 0) ? cx : rcx;
2508 cy = (+cy || cy == 0) ? cy : rcy;
2509 var dirx = ~~(x / math.abs(x)),
2510 diry = ~~(y / math.abs(y)),
2511 s = this.node.style,
2512 ncx = cx + (rcx - cx) * kx,
2513 ncy = cy + (rcy - cy) * ky;
2514 switch (this.type) {
2517 var neww = a.width * dirx * kx,
2518 newh = a.height * diry * ky;
2521 r: a.r * mmin(dirx * kx, diry * ky),
2530 rx: a.rx * dirx * kx,
2531 ry: a.ry * diry * ky,
2532 r: a.r * mmin(dirx * kx, diry * ky),
2538 var path = pathToRelative(a.path),
2540 for (var i = 0, ii = path[length]; i < ii; i++) {
2543 P0 = upperCase.call(p[0]);
2544 if (P0 == "M" && skip) {
2550 p[path[i][length] - 2] *= kx;
2551 p[path[i][length] - 1] *= ky;
2554 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2555 } else if (P0 == "H") {
2556 for (j = 1, jj = p[length]; j < jj; j++) {
2559 } else if (P0 == "V") {
2560 for (j = 1, jj = p[length]; j < jj; j++) {
2564 for (j = 1, jj = p[length]; j < jj; j++) {
2565 p[j] *= (j % 2) ? kx : ky;
2569 var dim2 = pathDimensions(path),
2570 dx = ncx - dim2.x - dim2.width / 2,
2571 dy = ncy - dim2.y - dim2.height / 2;
2574 this.attr({path: path});
2577 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2578 if (this.transformations) {
2579 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2580 this.node[setAttribute]("transform", this.transformations[join](S));
2581 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2582 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2583 this.attr({x: dx, y: dy});
2587 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
2588 ", M12=0, M21=0, M22=", diry,
2589 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2590 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2593 if (this.transformations) {
2594 this.transformations[2] = E;
2595 this.node[setAttribute]("transform", this.transformations[join](S));
2599 this.node.filterMatrix = E;
2600 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2603 a.scale = [x, y, cx, cy][join](S);
2609 Element[proto].clone = function () {
2610 var attr = this.attr();
2612 delete attr.translation;
2613 return this.paper[this.type]().attr(attr);
2615 var getLengthFactory = function (istotal, subpath) {
2616 return function (length) {
2617 if (this.type != "path") {
2620 var path = path2curve(this.attrs.path), x, y, p, l, sp = "", subpaths = {}, point,
2622 for (var i = 0, ii = path.length; i < ii; i++) {
2628 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2629 if (len + l > length) {
2630 if (subpath && !subpaths.start) {
2631 point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2632 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2633 subpaths.start = sp;
2634 sp = ["M", point.x, point.y, "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2640 if (!istotal && !subpath) {
2641 point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
2642 return {x: point.x, y: point.y, alpha: point.alpha};
2652 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2653 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2657 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2658 var old = {x: 0, y: 0},
2660 for (var i = 0; i < 1.01; i+=.01) {
2661 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2662 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
2667 Element[proto].getTotalLength = getLengthFactory(1);
2668 Element[proto].getPointAtLength = getLengthFactory();
2669 Element[proto].getSubpathsAtLength = getLengthFactory(0, 1);
2671 // animation easing formulas
2672 R.easing_formulas = {
2673 linear: function (n) {
2680 return pow(n - 1, 3) + 1;
2682 "<>": function (n) {
2685 return pow(n, 3) / 2;
2688 return (pow(n, 3) + 2) / 2;
2690 backIn: function (n) {
2692 return n * n * ((s + 1) * n - s);
2694 backOut: function (n) {
2697 return n * n * ((s + 1) * n + s) + 1;
2699 elastic: function (n) {
2700 if (n == 0 || n == 1) {
2705 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2707 bounce: function (n) {
2716 l = s * n * n + .75;
2718 if (n < (2.5 / p)) {
2720 l = s * n * n + .9375;
2723 l = s * n * n + .984375;
2731 var animationElements = {length : 0},
2732 animation = function () {
2733 var Now = +new Date;
2734 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2735 var e = animationElements[l];
2737 delete animationElements[l];
2738 animationElements[length]--;
2741 var time = Now - e.start,
2750 callback = e.callback,
2754 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2755 for (var attr in from) if (from[has](attr)) {
2756 switch (availableAnimAttrs[attr]) {
2758 now = pos * ms * diff[attr];
2759 to.back && (now = to.len - now);
2760 var point = to[attr].getPointAtLength(now);
2761 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2764 that.translate(point.x - diff.sx, point.y - diff.sy);
2765 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2768 now = +from[attr] + pos * ms * diff[attr];
2772 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2773 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2774 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2779 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2780 now[i] = [from[attr][i][0]];
2781 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2782 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2784 now[i] = now[i][join](S);
2791 var x = diff[attr][0] * (time - prev),
2792 y = diff[attr][1] * (time - prev);
2798 now = +from[attr][0] + pos * ms * diff[attr][0];
2799 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2802 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);
2808 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
2817 that._run && that._run.call(that);
2820 var point = to.along.getPointAtLength(to.len * !to.back);
2821 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
2822 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2824 (t.x || t.y) && that.translate(-t.x, -t.y);
2825 to.scale && (to.scale = to.scale + E);
2827 delete animationElements[l];
2828 animationElements[length]--;
2829 that.in_animation = null;
2830 R.is(callback, "function") && callback.call(that);
2834 R.svg && that && that.paper.safari();
2835 animationElements[length] && setTimeout(animation);
2837 upto255 = function (color) {
2838 return color > 255 ? 255 : (color < 0 ? 0 : color);
2840 translate = function (x, y) {
2842 return {x: this._.tx, y: this._.ty, toString: x_y};
2846 switch (this.type) {
2849 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
2854 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
2857 var path = pathToRelative(this.attrs.path);
2860 this.attr({path: path});
2865 Element[proto].animateWith = function (element, params, ms, easing, callback) {
2866 animationElements[element.id] && (params.start = animationElements[element.id].start);
2867 return this.animate(params, ms, easing, callback);
2869 Element[proto].animateAlong = along();
2870 Element[proto].animateAlongBack = along(1);
2871 function along(isBack) {
2872 return function (path, ms, rotate, callback) {
2873 var params = {back: isBack};
2874 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
2875 if (R.is(path, "string") && path.constructor != Element) {
2876 path = r.path(path).attr({stroke: "none"});
2877 var f = function () {
2880 callback = R.is(callback, "function") ? function () { f(); callback.call(this); } : f;
2882 path.constructor == Element && (params.along = path);
2883 return this.animate(params, ms, callback);
2886 Element[proto].onAnimation = function (f) {
2890 Element[proto].animate = function (params, ms, easing, callback) {
2891 if (R.is(easing, "function") || !easing) {
2892 callback = easing || null;
2897 for (var attr in params) if (params[has](attr)) {
2898 if (availableAnimAttrs[has](attr)) {
2899 from[attr] = this.attr(attr);
2900 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
2901 to[attr] = params[attr];
2902 switch (availableAnimAttrs[attr]) {
2904 var len = params[attr].getTotalLength(),
2905 point = params[attr].getPointAtLength(len * !!params.back),
2906 bb = this.getBBox();
2907 diff[attr] = len / ms;
2912 to.rot = params.rot;
2913 to.back = params.back;
2915 params.rot && (diff.r = toFloat(this.rotate()) || 0);
2918 diff[attr] = (to[attr] - from[attr]) / ms;
2921 from[attr] = R.getRGB(from[attr]);
2922 var toColour = R.getRGB(to[attr]);
2924 r: (toColour.r - from[attr].r) / ms,
2925 g: (toColour.g - from[attr].g) / ms,
2926 b: (toColour.b - from[attr].b) / ms
2930 var pathes = path2curve(from[attr], to[attr]);
2931 from[attr] = pathes[0];
2932 var toPath = pathes[1];
2934 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2935 diff[attr][i] = [0];
2936 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2937 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
2942 var values = (params[attr] + E)[split](separator),
2943 from2 = (from[attr] + E)[split](separator);
2946 from[attr] = [0, 0];
2947 diff[attr] = [values[0] / ms, values[1] / ms];
2950 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2951 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2954 params[attr] = values;
2955 from[attr] = (from[attr] + E)[split](separator);
2956 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2959 from[attr] = (from[attr] + E)[split](separator);
2963 diff[attr][i] = (values[i] - from[attr][i]) / ms;
2972 this.in_animation = 1;
2973 animationElements[this.id] = {
2974 start: params.start || +new Date,
2984 ++animationElements[length] == 1 && animation();
2987 Element[proto].stop = function () {
2988 animationElements[this.id] && animationElements[length]--;
2989 delete animationElements[this.id];
2992 Element[proto].translate = function (x, y) {
2993 return this.attr({translation: x + " " + y});
2995 Element[proto][toString] = function () {
2996 return "Rapha\xebl\u2019s object";
2998 R.ae = animationElements;
3001 var Set = function (items) {
3005 for (var i = 0, ii = items[length]; i < ii; i++) {
3006 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3007 this[this.items[length]] = this.items[this.items[length]] = items[i];
3013 Set[proto][push] = function () {
3016 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3017 item = arguments[i];
3018 if (item && (item.constructor == Element || item.constructor == Set)) {
3019 len = this.items[length];
3020 this[len] = this.items[len] = item;
3026 Set[proto].pop = function () {
3027 delete this[this[length]--];
3028 return this.items.pop();
3030 for (var method in Element[proto]) if (Element[proto][has](method)) {
3031 Set[proto][method] = (function (methodname) {
3032 return function () {
3033 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3034 this.items[i][methodname][apply](this.items[i], arguments);
3040 Set[proto].attr = function (name, value) {
3041 if (name && R.is(name, "array") && R.is(name[0], "object")) {
3042 for (var j = 0, jj = name[length]; j < jj; j++) {
3043 this.items[j].attr(name[j]);
3046 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3047 this.items[i].attr[apply](this.items[i], arguments);
3052 Set[proto].animate = function (params, ms, easing, callback) {
3053 (R.is(easing, "function") || !easing) && (callback = easing || null);
3054 var len = this.items[length],
3058 callback && (collector = function () {
3059 !--len && callback.call(set);
3061 this.items[--i].animate(params, ms, easing || collector, collector);
3063 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
3067 Set[proto].insertAfter = function (el) {
3068 var i = this.items[length];
3070 this.items[i].insertAfter(el);
3074 Set[proto].getBBox = function () {
3079 for (var i = this.items[length]; i--;) {
3080 var box = this.items[i].getBBox();
3083 w[push](box.x + box.width);
3084 h[push](box.y + box.height);
3086 x = mmin[apply](0, x);
3087 y = mmin[apply](0, y);
3091 width: mmax[apply](0, w) - x,
3092 height: mmax[apply](0, h) - y
3096 R.registerFont = function (font) {
3100 this.fonts = this.fonts || {};
3106 family = font.face["font-family"];
3107 for (var prop in font.face) if (font.face[has](prop)) {
3108 fontcopy.face[prop] = font.face[prop];
3110 if (this.fonts[family]) {
3111 this.fonts[family][push](fontcopy);
3113 this.fonts[family] = [fontcopy];
3116 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3117 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3118 var path = font.glyphs[glyph];
3119 fontcopy.glyphs[glyph] = {
3122 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3123 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3127 for (var k in path.k) if (path[has](k)) {
3128 fontcopy.glyphs[glyph].k[k] = path.k[k];
3135 Paper[proto].getFont = function (family, weight, style, stretch) {
3136 stretch = stretch || "normal";
3137 style = style || "normal";
3138 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3139 var font = R.fonts[family];
3141 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3142 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3143 if (name.test(fontName)) {
3144 font = R.fonts[fontName];
3151 for (var i = 0, ii = font[length]; i < ii; i++) {
3153 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3160 Paper[proto].print = function (x, y, string, font, size, origin) {
3161 origin = origin || "middle"; // baseline|middle
3162 var out = this.set(),
3163 letters = (string + E)[split](E),
3167 R.is(font, "string") && (font = this.getFont(font));
3169 scale = (size || 16) / font.face["units-per-em"];
3170 var bb = font.face.bbox.split(separator),
3172 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3173 for (var i = 0, ii = letters[length]; i < ii; i++) {
3174 var prev = i && font.glyphs[letters[i - 1]] || {},
3175 curr = font.glyphs[letters[i]];
3176 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3177 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3179 out.scale(scale, scale, top, height).translate(x - top, y - height);
3184 R.format = function (token) {
3185 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
3187 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
3188 return args[++i] == null ? E : args[i];
3192 R.ninja = function () {
3193 var r = win.Raphael, u;
3194 if (oldRaphael.was) {
3195 win.Raphael = oldRaphael.is;
3205 R.el = Element[proto];