2 * Raphael 1.4.4 - JavaScript Vector Library
4 * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
8 Raphael = (function () {
10 if (R.is(arguments[0], array)) {
12 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
14 for (var i = 0, ii = a[length]; i < ii; i++) {
16 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
20 return create[apply](R, arguments);
23 var separator = /[, ]+/,
24 elements = /^(circle|rect|path|ellipse|text|image)$/,
26 has = "hasOwnProperty",
30 was: Object[proto][has].call(win, "Raphael"),
33 Paper = function () {},
34 appendChild = "appendChild",
37 supportsTouch = "createTouch" in doc,
42 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
44 mousedown: "touchstart",
45 mousemove: "touchmove",
50 lowerCase = String[proto].toLowerCase,
57 toString = "toString",
59 objectToString = Object[proto][toString],
64 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
65 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+(?:\s*,\s*[\d\.]+)?)\s*\)|rgba?\(\s*([\d\.]+%\s*,\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,
67 setAttribute = "setAttribute",
70 ms = " progid:DXImageTransform.Microsoft",
71 upperCase = String[proto].toUpperCase,
72 availableAttrs = {blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", translation: "0 0", width: 0, x: 0, y: 0},
73 availableAnimAttrs = {along: "along", blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
75 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
76 if (R.type == "VML") {
77 var d = doc.createElement("div");
78 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
79 if (d.childNodes[length] != 2) {
84 R.svg = !(R.vml = R.type == "VML");
85 Paper[proto] = R[proto];
89 R.is = function (o, type) {
90 type = lowerCase.call(type);
91 return (type == "object" && o === Object(o)) ||
92 (type == "undefined" && typeof o == type) ||
93 (type == "null" && o == null) ||
94 lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
97 R.setWindow = function (newwin) {
102 var toHex = function (color) {
104 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
105 var trim = /^\s+|\s+$/g;
106 toHex = cacher(function (color) {
108 color = Str(color)[rp](trim, E);
110 var docum = new win.ActiveXObject("htmlfile");
111 docum.write("<body>");
115 bod = win.createPopup().document.body;
117 var range = bod.createTextRange();
119 bod.style.color = color;
120 var value = range.queryCommandValue("ForeColor");
121 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
122 return "#" + ("000000" + value[toString](16)).slice(-6);
128 var i = doc.createElement("i");
129 i.title = "Rapha\xebl Colour Picker";
130 i.style.display = "none";
131 doc.body[appendChild](i);
132 toHex = cacher(function (color) {
133 i.style.color = color;
134 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
139 var hsbtoString = function () {
140 return "hsb(" + [this.h, this.s, this.b] + ")";
142 rgbtoString = function () {
145 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
146 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
154 if (brightness == 0) {
155 return {r: 0, g: 0, b: 0, hex: "#000"};
157 if (hue > 1 || saturation > 1 || brightness > 1) {
164 p = brightness * (1 - saturation),
165 q = brightness * (1 - (saturation * f)),
166 t = brightness * (1 - (saturation * (1 - f)));
167 red = [brightness, q, p, p, t, brightness, brightness][i];
168 green = [t, brightness, brightness, q, p, p, t][i];
169 blue = [p, p, t, brightness, brightness, q, p][i];
173 var rgb = {r: red, g: green, b: blue, toString: rgbtoString},
174 r = (~~red)[toString](16),
175 g = (~~green)[toString](16),
176 b = (~~blue)[toString](16);
180 rgb.hex = "#" + r + g + b;
183 R.rgb2hsb = cacher(function (red, green, blue) {
184 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
189 if (R.is(red, string)) {
190 var clr = R.getRGB(red);
195 if (red > 1 || green > 1 || blue > 1) {
200 var max = mmax(red, green, blue),
201 min = mmin(red, green, blue),
206 return {h: 0, s: 0, b: max};
208 var delta = (max - min);
209 saturation = delta / max;
211 hue = (green - blue) / delta;
212 } else if (green == max) {
213 hue = 2 + ((blue - red) / delta);
215 hue = 4 + ((red - green) / delta);
221 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
223 var p2s = /,?([achlmqrstvxz]),?/gi,
224 commaSpaces = /\s*,\s*/,
225 hsrg = {hs: 1, rg: 1};
226 R._path2string = function () {
227 return this.join(",")[rp](p2s, "$1");
229 function cacher(f, scope, postprocessor) {
231 var arg = Array[proto].slice.call(arguments, 0),
232 args = arg[join]("\u25ba"),
233 cache = newf.cache = newf.cache || {},
234 count = newf.count = newf.count || [];
235 if (cache[has](args)) {
236 return postprocessor ? postprocessor(cache[args]) : cache[args];
238 count[length] >= 1e3 && delete cache[count.shift()];
240 cache[args] = f[apply](scope, arg);
241 return postprocessor ? postprocessor(cache[args]) : cache[args];
246 R.getRGB = cacher(function (colour) {
247 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
248 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
250 if (colour == "none") {
251 return {r: -1, g: -1, b: -1, hex: "none"};
253 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
260 rgb = colour.match(colourRegExp);
263 blue = toInt(rgb[2].substring(5), 16);
264 green = toInt(rgb[2].substring(3, 5), 16);
265 red = toInt(rgb[2].substring(1, 3), 16);
268 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
269 green = toInt((t = rgb[3].charAt(2)) + t, 16);
270 red = toInt((t = rgb[3].charAt(1)) + t, 16);
273 rgb = rgb[4][split](commaSpaces);
274 red = toFloat(rgb[0]);
275 green = toFloat(rgb[1]);
276 blue = toFloat(rgb[2]);
277 opacity = toFloat(rgb[3]);
280 rgb = rgb[5][split](commaSpaces);
281 red = toFloat(rgb[0]) * 2.55;
282 green = toFloat(rgb[1]) * 2.55;
283 blue = toFloat(rgb[2]) * 2.55;
284 opacity = toFloat(rgb[3]);
287 rgb = rgb[6][split](commaSpaces);
288 red = toFloat(rgb[0]);
289 green = toFloat(rgb[1]);
290 blue = toFloat(rgb[2]);
291 return R.hsb2rgb(red, green, blue);
294 rgb = rgb[7][split](commaSpaces);
295 red = toFloat(rgb[0]) * 2.55;
296 green = toFloat(rgb[1]) * 2.55;
297 blue = toFloat(rgb[2]) * 2.55;
298 return R.hsb2rgb(red, green, blue);
300 rgb = {r: red, g: green, b: blue};
301 var r = (~~red)[toString](16),
302 g = (~~green)[toString](16),
303 b = (~~blue)[toString](16);
307 rgb.hex = "#" + r + g + b;
308 isFinite(toFloat(opacity)) && (rgb.o = opacity);
311 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
313 R.getColor = function (value) {
314 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
315 rgb = this.hsb2rgb(start.h, start.s, start.b);
320 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
324 R.getColor.reset = function () {
328 var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
329 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
330 R.parsePathString = cacher(function (pathString) {
334 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
336 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
337 data = pathClone(pathString);
340 Str(pathString)[rp](pathCommand, function (a, b, c) {
342 name = lowerCase.call(b);
343 c[rp](pathValues, function (a, b) {
344 b && params[push](+b);
346 if (name == "m" && params[length] > 2) {
347 data[push]([b][concat](params.splice(0, 2)));
349 b = b == "m" ? "l" : "L";
351 while (params[length] >= paramCounts[name]) {
352 data[push]([b][concat](params.splice(0, paramCounts[name])));
353 if (!paramCounts[name]) {
359 data[toString] = R._path2string;
362 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
364 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
365 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
366 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
367 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
368 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
369 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
370 ax = (1 - t) * p1x + t * c1x,
371 ay = (1 - t) * p1y + t * c1y,
372 cx = (1 - t) * c2x + t * p2x,
373 cy = (1 - t) * c2y + t * p2y,
374 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
375 (mx > nx || my < ny) && (alpha += 180);
376 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};
378 var pathDimensions = cacher(function (path) {
380 return {x: 0, y: 0, width: 0, height: 0};
382 path = path2curve(path);
388 for (var i = 0, ii = path[length]; i < ii; i++) {
396 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
397 X = X[concat](dim.min.x, dim.max.x);
398 Y = Y[concat](dim.min.y, dim.max.y);
403 var xmin = mmin[apply](0, X),
404 ymin = mmin[apply](0, Y);
408 width: mmax[apply](0, X) - xmin,
409 height: mmax[apply](0, Y) - ymin
412 pathClone = function (pathArray) {
414 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
415 pathArray = R.parsePathString(pathArray);
417 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
419 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
420 res[i][j] = pathArray[i][j];
423 res[toString] = R._path2string;
426 pathToRelative = cacher(function (pathArray) {
427 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
428 pathArray = R.parsePathString(pathArray);
436 if (pathArray[0][0] == "M") {
442 res[push](["M", x, y]);
444 for (var i = start, ii = pathArray[length]; i < ii; i++) {
447 if (pa[0] != lowerCase.call(pa[0])) {
448 r[0] = lowerCase.call(pa[0]);
456 r[6] = +(pa[6] - x).toFixed(3);
457 r[7] = +(pa[7] - y).toFixed(3);
460 r[1] = +(pa[1] - y).toFixed(3);
466 for (var j = 1, jj = pa[length]; j < jj; j++) {
467 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
476 for (var k = 0, kk = pa[length]; k < kk; k++) {
480 var len = res[i][length];
487 x += +res[i][len - 1];
490 y += +res[i][len - 1];
493 x += +res[i][len - 2];
494 y += +res[i][len - 1];
497 res[toString] = R._path2string;
500 pathToAbsolute = cacher(function (pathArray) {
501 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
502 pathArray = R.parsePathString(pathArray);
510 if (pathArray[0][0] == "M") {
511 x = +pathArray[0][1];
512 y = +pathArray[0][2];
516 res[0] = ["M", x, y];
518 for (var i = start, ii = pathArray[length]; i < ii; i++) {
521 if (pa[0] != upperCase.call(pa[0])) {
522 r[0] = upperCase.call(pa[0]);
543 for (var j = 1, jj = pa[length]; j < jj; j++) {
544 r[j] = +pa[j] + ((j % 2) ? x : y);
548 for (var k = 0, kk = pa[length]; k < kk; k++) {
564 mx = res[i][res[i][length] - 2];
565 my = res[i][res[i][length] - 1];
567 x = res[i][res[i][length] - 2];
568 y = res[i][res[i][length] - 1];
571 res[toString] = R._path2string;
574 l2c = function (x1, y1, x2, y2) {
575 return [x1, y1, x2, y2, x2, y2];
577 q2c = function (x1, y1, ax, ay, x2, y2) {
589 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
590 // for more information of where this math came from visit:
591 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
593 _120 = PI * 120 / 180,
594 rad = PI / 180 * (+angle || 0),
597 rotate = cacher(function (x, y, rad) {
598 var X = x * math.cos(rad) - y * math.sin(rad),
599 Y = x * math.sin(rad) + y * math.cos(rad);
603 xy = rotate(x1, y1, -rad);
606 xy = rotate(x2, y2, -rad);
609 var cos = math.cos(PI / 180 * angle),
610 sin = math.sin(PI / 180 * angle),
613 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
621 k = (large_arc_flag == sweep_flag ? -1 : 1) *
622 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
623 cx = k * rx * y / ry + (x1 + x2) / 2,
624 cy = k * -ry * x / rx + (y1 + y2) / 2,
625 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
626 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
628 f1 = x1 < cx ? PI - f1 : f1;
629 f2 = x2 < cx ? PI - f2 : f2;
630 f1 < 0 && (f1 = PI * 2 + f1);
631 f2 < 0 && (f2 = PI * 2 + f2);
632 if (sweep_flag && f1 > f2) {
635 if (!sweep_flag && f2 > f1) {
645 if (math.abs(df) > _120) {
649 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
650 x2 = cx + rx * math.cos(f2);
651 y2 = cy + ry * math.sin(f2);
652 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
655 var c1 = math.cos(f1),
659 t = math.tan(df / 4),
663 m2 = [x1 + hx * s1, y1 - hy * c1],
664 m3 = [x2 + hx * s2, y2 - hy * c2],
666 m2[0] = 2 * m1[0] - m2[0];
667 m2[1] = 2 * m1[1] - m2[1];
669 return [m2, m3, m4][concat](res);
671 res = [m2, m3, m4][concat](res)[join]()[split](",");
673 for (var i = 0, ii = res[length]; i < ii; i++) {
674 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
679 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
682 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
683 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
686 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
687 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
688 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
690 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
691 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
695 math.abs(t1) > 1e12 && (t1 = .5);
696 math.abs(t2) > 1e12 && (t2 = .5);
697 if (t1 > 0 && t1 < 1) {
698 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
702 if (t2 > 0 && t2 < 1) {
703 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
707 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
708 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
710 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
711 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
712 math.abs(t1) > 1e12 && (t1 = .5);
713 math.abs(t2) > 1e12 && (t2 = .5);
714 if (t1 > 0 && t1 < 1) {
715 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
719 if (t2 > 0 && t2 < 1) {
720 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
725 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
726 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
729 path2curve = cacher(function (path, path2) {
730 var p = pathToAbsolute(path),
731 p2 = path2 && pathToAbsolute(path2),
732 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
733 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
734 processPath = function (path, d) {
737 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
739 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
746 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
749 nx = d.x + (d.x - (d.bx || d.x));
750 ny = d.y + (d.y - (d.by || d.y));
751 path = ["C", nx, ny][concat](path.slice(1));
754 d.qx = d.x + (d.x - (d.qx || d.x));
755 d.qy = d.y + (d.y - (d.qy || d.y));
756 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
761 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
764 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
767 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
770 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
773 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
778 fixArc = function (pp, i) {
779 if (pp[i][length] > 7) {
783 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
786 ii = mmax(p[length], p2 && p2[length] || 0);
789 fixM = function (path1, path2, a1, a2, i) {
790 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
791 path2.splice(i, 0, ["M", a2.x, a2.y]);
796 ii = mmax(p[length], p2 && p2[length] || 0);
799 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
800 p[i] = processPath(p[i], attrs);
802 p2 && (p2[i] = processPath(p2[i], attrs2));
804 fixM(p, p2, attrs, attrs2, i);
805 fixM(p2, p, attrs2, attrs, i);
808 seglen = seg[length],
809 seg2len = p2 && seg2[length];
810 attrs.x = seg[seglen - 2];
811 attrs.y = seg[seglen - 1];
812 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
813 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
814 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
815 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
816 attrs2.x = p2 && seg2[seg2len - 2];
817 attrs2.y = p2 && seg2[seg2len - 1];
819 return p2 ? [p, p2] : p;
821 parseDots = cacher(function (gradient) {
823 for (var i = 0, ii = gradient[length]; i < ii; i++) {
825 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
826 dot.color = R.getRGB(par[1]);
827 if (dot.color.error) {
830 dot.color = dot.color.hex;
831 par[2] && (dot.offset = par[2] + "%");
834 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
835 if (!dots[i].offset) {
836 var start = toFloat(dots[i - 1].offset || 0),
838 for (var j = i + 1; j < ii; j++) {
839 if (dots[j].offset) {
840 end = dots[j].offset;
849 var d = (end - start) / (j - i + 1);
852 dots[i].offset = start + "%";
858 getContainer = function (x, y, w, h) {
860 if (R.is(x, string) || R.is(x, "object")) {
861 container = R.is(x, string) ? doc.getElementById(x) : x;
862 if (container.tagName) {
865 container: container,
866 width: container.style.pixelWidth || container.offsetWidth,
867 height: container.style.pixelHeight || container.offsetHeight
870 return {container: container, width: y, height: w};
874 return {container: 1, x: x, y: y, width: w, height: h};
877 plugins = function (con, add) {
879 for (var prop in add) {
880 if (add[has](prop) && !(prop in con)) {
881 switch (typeof add[prop]) {
884 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
888 con[prop] = con[prop] || {};
889 plugins.call(this, con[prop], add[prop]);
892 con[prop] = add[prop];
898 tear = function (el, paper) {
899 el == paper.top && (paper.top = el.prev);
900 el == paper.bottom && (paper.bottom = el.next);
901 el.next && (el.next.prev = el.prev);
902 el.prev && (el.prev.next = el.next);
904 tofront = function (el, paper) {
905 if (paper.top === el) {
914 toback = function (el, paper) {
915 if (paper.bottom === el) {
919 el.next = paper.bottom;
921 paper.bottom.prev = el;
924 insertafter = function (el, el2, paper) {
926 el2 == paper.top && (paper.top = el);
927 el2.next && (el2.next.prev = el);
932 insertbefore = function (el, el2, paper) {
934 el2 == paper.bottom && (paper.bottom = el);
935 el2.prev && (el2.prev.next = el);
940 removed = function (methodname) {
942 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
945 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
946 R.pathToRelative = pathToRelative;
949 Paper[proto].svgns = "http://www.w3.org/2000/svg";
950 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
951 round = function (num) {
952 return +num + (~~num === num) * .5;
954 var $ = function (el, attr) {
956 for (var key in attr) {
957 if (attr[has](key)) {
958 el[setAttribute](key, Str(attr[key]));
962 el = doc.createElementNS(Paper[proto].svgns, el);
963 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
967 R[toString] = function () {
968 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
970 var thePath = function (pathString, SVG) {
972 SVG.canvas && SVG.canvas[appendChild](el);
973 var p = new Element(el, SVG);
975 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
978 var addGradientFill = function (o, gradient, SVG) {
982 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
987 var dir = ((fy > .5) * 2 - 1);
988 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
989 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
991 (fy = fy.toFixed(5) - 1e-5 * dir);
995 gradient = gradient[split](/\s*\-\s*/);
996 if (type == "linear") {
997 var angle = gradient.shift();
998 angle = -toFloat(angle);
1002 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1003 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1006 if (vector[2] < 0) {
1007 vector[0] = -vector[2];
1010 if (vector[3] < 0) {
1011 vector[1] = -vector[3];
1015 var dots = parseDots(gradient);
1019 var id = o.getAttribute(fillString);
1020 id = id.match(/^url\(#(.*)\)$/);
1021 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1023 var el = $(type + "Gradient");
1024 el.id = "r" + (R._id++)[toString](36);
1025 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1026 SVG.defs[appendChild](el);
1027 for (var i = 0, ii = dots[length]; i < ii; i++) {
1028 var stop = $("stop");
1030 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1031 "stop-color": dots[i].color || "#fff"
1033 el[appendChild](stop);
1036 fill: "url(#" + el.id + ")",
1045 var updatePosition = function (o) {
1046 var bbox = o.getBBox();
1047 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1049 var setFillAndStroke = function (o, params) {
1056 "-..": [3, 1, 1, 1, 1, 1],
1060 "- .": [4, 3, 1, 3],
1061 "--.": [8, 3, 1, 3],
1062 "--..": [8, 3, 1, 3, 1, 3]
1067 addDashes = function (o, value) {
1068 value = dasharray[lowerCase.call(value)];
1070 var width = o.attrs["stroke-width"] || "1",
1071 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1073 var i = value[length];
1075 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1077 $(node, {"stroke-dasharray": dashes[join](",")});
1080 params[has]("rotation") && (rot = params.rotation);
1081 var rotxy = Str(rot)[split](separator);
1082 if (!(rotxy.length - 1)) {
1085 rotxy[1] = +rotxy[1];
1086 rotxy[2] = +rotxy[2];
1088 toFloat(rot) && o.rotate(0, true);
1089 for (var att in params) {
1090 if (params[has](att)) {
1091 if (!availableAttrs[has](att)) {
1094 var value = params[att];
1101 o.rotate(value, true);
1106 var pn = node.parentNode;
1107 if (lowerCase.call(pn.tagName) != "a") {
1109 pn.insertBefore(hl, node);
1110 hl[appendChild](node);
1113 pn.setAttributeNS(o.paper.xlink, att, value);
1116 node.style.cursor = value;
1119 var rect = Str(value)[split](separator);
1120 if (rect[length] == 4) {
1121 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1122 var el = $("clipPath"),
1124 el.id = "r" + (R._id++)[toString](36);
1131 el[appendChild](rc);
1132 o.paper.defs[appendChild](el);
1133 $(node, {"clip-path": "url(#" + el.id + ")"});
1137 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1138 clip && clip.parentNode.removeChild(clip);
1139 $(node, {"clip-path": E});
1144 if (o.type == "path") {
1145 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1149 node[setAttribute](att, value);
1158 value = -attrs.x - (attrs.width || 0);
1161 if (att == "rx" && o.type == "rect") {
1165 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1166 node[setAttribute](att, round(value));
1167 o.pattern && updatePosition(o);
1170 node[setAttribute](att, value);
1179 value = -attrs.y - (attrs.height || 0);
1182 if (att == "ry" && o.type == "rect") {
1186 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1187 node[setAttribute](att, round(value));
1188 o.pattern && updatePosition(o);
1191 if (o.type == "rect") {
1192 $(node, {rx: value, ry: value});
1194 node[setAttribute](att, value);
1198 if (o.type == "image") {
1199 node.setAttributeNS(o.paper.xlink, "href", value);
1202 case "stroke-width":
1203 node.style.strokeWidth = value;
1204 // Need following line for Firefox
1205 node[setAttribute](att, value);
1206 if (attrs["stroke-dasharray"]) {
1207 addDashes(o, attrs["stroke-dasharray"]);
1210 case "stroke-dasharray":
1211 addDashes(o, value);
1214 var xy = Str(value)[split](separator);
1215 xy[0] = +xy[0] || 0;
1216 xy[1] = +xy[1] || 0;
1221 translate.call(o, xy[0], xy[1]);
1224 xy = Str(value)[split](separator);
1225 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1228 var isURL = Str(value).match(ISURL);
1231 var ig = $("image");
1232 el.id = "r" + (R._id++)[toString](36);
1233 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1234 $(ig, {x: 0, y: 0});
1235 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1236 el[appendChild](ig);
1238 var img = doc.createElement("img");
1239 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1240 img.onload = function () {
1241 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1242 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1243 doc.body.removeChild(this);
1246 doc.body[appendChild](img);
1248 o.paper.defs[appendChild](el);
1249 node.style.fill = "url(#" + el.id + ")";
1250 $(node, {fill: "url(#" + el.id + ")"});
1252 o.pattern && updatePosition(o);
1255 var clr = R.getRGB(value);
1257 delete params.gradient;
1258 delete attrs.gradient;
1259 !R.is(attrs.opacity, "undefined") &&
1260 R.is(params.opacity, "undefined") &&
1261 $(node, {opacity: attrs.opacity});
1262 !R.is(attrs["fill-opacity"], "undefined") &&
1263 R.is(params["fill-opacity"], "undefined") &&
1264 $(node, {"fill-opacity": attrs["fill-opacity"]});
1265 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1266 attrs.gradient = value;
1267 attrs.fill = "none";
1270 clr[has]("o") && $(node, {"fill-opacity": clr.o / 100});
1272 clr = R.getRGB(value);
1273 node[setAttribute](att, clr.hex);
1274 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o / 100});
1277 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1280 case "fill-opacity":
1281 if (attrs.gradient) {
1282 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1284 var stops = gradient.getElementsByTagName("stop");
1285 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1290 att == "font-size" && (value = toInt(value, 10) + "px");
1291 var cssrule = att[rp](/(\-.)/g, function (w) {
1292 return upperCase.call(w.substring(1));
1294 node.style[cssrule] = value;
1295 // Need following line for Firefox
1296 node[setAttribute](att, value);
1302 tuneText(o, params);
1304 o.rotate(rotxy.join(S));
1306 toFloat(rot) && o.rotate(rot, true);
1310 tuneText = function (el, params) {
1311 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1316 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1318 if (params[has]("text")) {
1319 a.text = params.text;
1320 while (node.firstChild) {
1321 node.removeChild(node.firstChild);
1323 var texts = Str(params.text)[split]("\n");
1324 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1325 var tspan = $("tspan");
1326 i && $(tspan, {dy: fontSize * leading, x: a.x});
1327 tspan[appendChild](doc.createTextNode(texts[i]));
1328 node[appendChild](tspan);
1331 texts = node.getElementsByTagName("tspan");
1332 for (i = 0, ii = texts[length]; i < ii; i++) {
1333 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1337 var bb = el.getBBox(),
1338 dif = a.y - (bb.y + bb.height / 2);
1339 dif && isFinite(dif) && $(node, {y: a.y + dif});
1341 Element = function (node, svg) {
1347 node.raphael = this;
1349 this.attrs = this.attrs || {};
1350 this.transformations = []; // rotate, translate, scale
1354 rt: {deg: 0, cx: 0, cy: 0},
1358 !svg.bottom && (svg.bottom = this);
1359 this.prev = svg.top;
1360 svg.top && (svg.top.next = this);
1364 Element[proto].rotate = function (deg, cx, cy) {
1370 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1372 return this._.rt.deg;
1374 var bbox = this.getBBox();
1375 deg = Str(deg)[split](separator);
1376 if (deg[length] - 1) {
1377 cx = toFloat(deg[1]);
1378 cy = toFloat(deg[2]);
1380 deg = toFloat(deg[0]);
1382 this._.rt.deg = deg;
1384 this._.rt.deg += deg;
1386 (cy == null) && (cx = null);
1389 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1390 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1391 if (this._.rt.deg) {
1392 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1393 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1395 this.transformations[0] = E;
1396 this.clip && $(this.clip, {transform: E});
1398 $(this.node, {transform: this.transformations[join](S)});
1401 Element[proto].hide = function () {
1402 !this.removed && (this.node.style.display = "none");
1405 Element[proto].show = function () {
1406 !this.removed && (this.node.style.display = "");
1409 Element[proto].remove = function () {
1413 tear(this, this.paper);
1414 this.node.parentNode.removeChild(this.node);
1415 for (var i in this) {
1418 this.removed = true;
1420 Element[proto].getBBox = function () {
1424 if (this.type == "path") {
1425 return pathDimensions(this.attrs.path);
1427 if (this.node.style.display == "none") {
1433 bbox = this.node.getBBox();
1435 // Firefox 3.0.x plays badly here
1439 if (this.type == "text") {
1440 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1441 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1442 var bb = this.node.getExtentOfChar(i);
1443 (bb.y < bbox.y) && (bbox.y = bb.y);
1444 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1445 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1448 hide && this.hide();
1451 Element[proto].attr = function (name, value) {
1457 for (var i in this.attrs) if (this.attrs[has](i)) {
1458 res[i] = this.attrs[i];
1460 this._.rt.deg && (res.rotation = this.rotate());
1461 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1462 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1465 if (value == null && R.is(name, string)) {
1466 if (name == "translation") {
1467 return translate.call(this);
1469 if (name == "rotation") {
1470 return this.rotate();
1472 if (name == "scale") {
1473 return this.scale();
1475 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1476 return this.attrs.gradient;
1478 return this.attrs[name];
1480 if (value == null && R.is(name, array)) {
1482 for (var j = 0, jj = name.length; j < jj; j++) {
1483 values[name[j]] = this.attr(name[j]);
1487 if (value != null) {
1489 params[name] = value;
1490 setFillAndStroke(this, params);
1491 } else if (name != null && R.is(name, "object")) {
1492 setFillAndStroke(this, name);
1496 Element[proto].toFront = function () {
1500 this.node.parentNode[appendChild](this.node);
1501 var svg = this.paper;
1502 svg.top != this && tofront(this, svg);
1505 Element[proto].toBack = function () {
1509 if (this.node.parentNode.firstChild != this.node) {
1510 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1511 toback(this, this.paper);
1512 var svg = this.paper;
1516 Element[proto].insertAfter = function (element) {
1520 var node = element.node;
1521 if (node.nextSibling) {
1522 node.parentNode.insertBefore(this.node, node.nextSibling);
1524 node.parentNode[appendChild](this.node);
1526 insertafter(this, element, this.paper);
1529 Element[proto].insertBefore = function (element) {
1533 var node = element.node;
1534 node.parentNode.insertBefore(this.node, node);
1535 insertbefore(this, element, this.paper);
1538 Element[proto].blur = function (size) {
1539 // Experimental. No Safari support. Use it on your own risk.
1542 var fltr = $("filter"),
1543 blur = $("feGaussianBlur");
1544 t.attrs.blur = size;
1545 fltr.id = "r" + (R._id++)[toString](36);
1546 $(blur, {stdDeviation: +size || 1.5});
1547 fltr.appendChild(blur);
1548 t.paper.defs.appendChild(fltr);
1550 $(t.node, {filter: "url(#" + fltr.id + ")"});
1553 t._blur.parentNode.removeChild(t._blur);
1555 delete t.attrs.blur;
1557 t.node.removeAttribute("filter");
1560 var theCircle = function (svg, x, y, r) {
1563 var el = $("circle");
1564 svg.canvas && svg.canvas[appendChild](el);
1565 var res = new Element(el, svg);
1566 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1567 res.type = "circle";
1571 var theRect = function (svg, x, y, w, h, r) {
1575 svg.canvas && svg.canvas[appendChild](el);
1576 var res = new Element(el, svg);
1577 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1582 var theEllipse = function (svg, x, y, rx, ry) {
1585 var el = $("ellipse");
1586 svg.canvas && svg.canvas[appendChild](el);
1587 var res = new Element(el, svg);
1588 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1589 res.type = "ellipse";
1593 var theImage = function (svg, src, x, y, w, h) {
1594 var el = $("image");
1595 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1596 el.setAttributeNS(svg.xlink, "href", src);
1597 svg.canvas && svg.canvas[appendChild](el);
1598 var res = new Element(el, svg);
1599 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1603 var theText = function (svg, x, y, text) {
1605 $(el, {x: x, y: y, "text-anchor": "middle"});
1606 svg.canvas && svg.canvas[appendChild](el);
1607 var res = new Element(el, svg);
1608 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1610 setFillAndStroke(res, res.attrs);
1613 var setSize = function (width, height) {
1614 this.width = width || this.width;
1615 this.height = height || this.height;
1616 this.canvas[setAttribute]("width", this.width);
1617 this.canvas[setAttribute]("height", this.height);
1620 var create = function () {
1621 var con = getContainer[apply](0, arguments),
1622 container = con && con.container,
1626 height = con.height;
1628 throw new Error("SVG container not found.");
1630 var cnvs = $("svg");
1633 width = width || 512;
1634 height = height || 342;
1636 xmlns: "http://www.w3.org/2000/svg",
1641 if (container == 1) {
1642 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1643 doc.body[appendChild](cnvs);
1645 if (container.firstChild) {
1646 container.insertBefore(cnvs, container.firstChild);
1648 container[appendChild](cnvs);
1651 container = new Paper;
1652 container.width = width;
1653 container.height = height;
1654 container.canvas = cnvs;
1655 plugins.call(container, container, R.fn);
1659 Paper[proto].clear = function () {
1660 var c = this.canvas;
1661 while (c.firstChild) {
1662 c.removeChild(c.firstChild);
1664 this.bottom = this.top = null;
1665 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1666 c[appendChild](this.desc);
1667 c[appendChild](this.defs = $("defs"));
1669 Paper[proto].remove = function () {
1670 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1671 for (var i in this) {
1672 this[i] = removed(i);
1679 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1680 bites = /([clmz]),?([^clmz]*)/gi,
1681 val = /-?[^,\s-]+/g,
1682 coordsize = 1e3 + S + 1e3,
1684 pathlike = {path: 1, rect: 1},
1685 path2vml = function (path) {
1686 var total = /[ahqstv]/ig,
1687 command = pathToAbsolute;
1688 Str(path).match(total) && (command = path2curve);
1690 if (command == pathToAbsolute && !Str(path).match(total)) {
1691 var res = Str(path)[rp](bites, function (all, command, args) {
1693 isMove = lowerCase.call(command) == "m",
1695 args[rp](val, function (value) {
1696 if (isMove && vals[length] == 2) {
1697 res += vals + map[command == "m" ? "l" : "L"];
1700 vals[push](round(value * zoom));
1706 var pa = command(path), p, r;
1708 for (var i = 0, ii = pa[length]; i < ii; i++) {
1710 r = lowerCase.call(pa[i][0]);
1711 r == "z" && (r = "x");
1712 for (var j = 1, jj = p[length]; j < jj; j++) {
1713 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1717 return res[join](S);
1720 R[toString] = function () {
1721 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1723 thePath = function (pathString, vml) {
1724 var g = createNode("group");
1725 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1726 g.coordsize = vml.coordsize;
1727 g.coordorigin = vml.coordorigin;
1728 var el = createNode("shape"), ol = el.style;
1729 ol.width = vml.width + "px";
1730 ol.height = vml.height + "px";
1731 el.coordsize = coordsize;
1732 el.coordorigin = vml.coordorigin;
1734 var p = new Element(el, g, vml),
1735 attr = {fill: "none", stroke: "#000"};
1736 pathString && (attr.path = pathString);
1737 p.isAbsolute = true;
1741 setFillAndStroke(p, attr);
1742 vml.canvas[appendChild](g);
1745 setFillAndStroke = function (o, params) {
1746 o.attrs = o.attrs || {};
1751 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1754 for (var par in params) if (params[has](par)) {
1755 a[par] = params[par];
1758 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1764 params.href && (node.href = params.href);
1765 params.title && (node.title = params.title);
1766 params.target && (node.target = params.target);
1767 params.cursor && (s.cursor = params.cursor);
1768 "blur" in params && o.blur(params.blur);
1769 if (params.path && o.type == "path" || newpath) {
1770 node.path = path2vml(a.path);
1772 if (params.rotation != null) {
1773 o.rotate(params.rotation, true);
1775 if (params.translation) {
1776 xy = Str(params.translation)[split](separator);
1777 translate.call(o, xy[0], xy[1]);
1778 if (o._.rt.cx != null) {
1779 o._.rt.cx +=+ xy[0];
1780 o._.rt.cy +=+ xy[1];
1781 o.setBox(o.attrs, xy[0], xy[1]);
1785 xy = Str(params.scale)[split](separator);
1786 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1788 if ("clip-rect" in params) {
1789 var rect = Str(params["clip-rect"])[split](separator);
1790 if (rect[length] == 4) {
1791 rect[2] = +rect[2] + (+rect[0]);
1792 rect[3] = +rect[3] + (+rect[1]);
1793 var div = node.clipRect || doc.createElement("div"),
1795 group = node.parentNode;
1796 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1797 if (!node.clipRect) {
1798 dstyle.position = "absolute";
1801 dstyle.width = o.paper.width + "px";
1802 dstyle.height = o.paper.height + "px";
1803 group.parentNode.insertBefore(div, group);
1804 div[appendChild](group);
1805 node.clipRect = div;
1808 if (!params["clip-rect"]) {
1809 node.clipRect && (node.clipRect.style.clip = E);
1812 if (o.type == "image" && params.src) {
1813 node.src = params.src;
1815 if (o.type == "image" && params.opacity) {
1816 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1817 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1819 params.font && (s.font = params.font);
1820 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1821 params["font-size"] && (s.fontSize = params["font-size"]);
1822 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1823 params["font-style"] && (s.fontStyle = params["font-style"]);
1824 if (params.opacity != null ||
1825 params["stroke-width"] != null ||
1826 params.fill != null ||
1827 params.stroke != null ||
1828 params["stroke-width"] != null ||
1829 params["stroke-opacity"] != null ||
1830 params["fill-opacity"] != null ||
1831 params["stroke-dasharray"] != null ||
1832 params["stroke-miterlimit"] != null ||
1833 params["stroke-linejoin"] != null ||
1834 params["stroke-linecap"] != null) {
1835 node = o.shape || node;
1836 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1838 !fill && (newfill = fill = createNode(fillString));
1839 if ("fill-opacity" in params || "opacity" in params) {
1840 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1841 opacity < 0 && (opacity = 0);
1842 opacity > 1 && (opacity = 1);
1843 fill.opacity = opacity;
1845 params.fill && (fill.on = true);
1846 if (fill.on == null || params.fill == "none") {
1849 if (fill.on && params.fill) {
1850 var isURL = params.fill.match(ISURL);
1852 fill.src = isURL[1];
1855 fill.color = R.getRGB(params.fill).hex;
1857 fill.type = "solid";
1858 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
1860 a.gradient = params.fill;
1864 newfill && node[appendChild](fill);
1865 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1867 !stroke && (newstroke = stroke = createNode("stroke"));
1868 if ((params.stroke && params.stroke != "none") ||
1869 params["stroke-width"] ||
1870 params["stroke-opacity"] != null ||
1871 params["stroke-dasharray"] ||
1872 params["stroke-miterlimit"] ||
1873 params["stroke-linejoin"] ||
1874 params["stroke-linecap"]) {
1877 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
1878 var strokeColor = R.getRGB(params.stroke);
1879 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
1880 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
1881 var width = (toFloat(params["stroke-width"]) || 1) * .75;
1882 opacity < 0 && (opacity = 0);
1883 opacity > 1 && (opacity = 1);
1884 params["stroke-width"] == null && (width = a["stroke-width"]);
1885 params["stroke-width"] && (stroke.weight = width);
1886 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
1887 stroke.opacity = opacity;
1889 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1890 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1891 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
1892 if (params["stroke-dasharray"]) {
1896 "-.": "shortdashdot",
1897 "-..": "shortdashdotdot",
1902 "--.": "longdashdot",
1903 "--..": "longdashdotdot"
1905 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
1907 newstroke && node[appendChild](stroke);
1909 if (res.type == "text") {
1910 s = res.paper.span.style;
1911 a.font && (s.font = a.font);
1912 a["font-family"] && (s.fontFamily = a["font-family"]);
1913 a["font-size"] && (s.fontSize = a["font-size"]);
1914 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1915 a["font-style"] && (s.fontStyle = a["font-style"]);
1916 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
1917 res.W = a.w = res.paper.span.offsetWidth;
1918 res.H = a.h = res.paper.span.offsetHeight;
1920 res.Y = a.y + round(res.H / 2);
1922 // text-anchor emulationm
1923 switch (a["text-anchor"]) {
1925 res.node.style["v-text-align"] = "left";
1926 res.bbx = round(res.W / 2);
1929 res.node.style["v-text-align"] = "right";
1930 res.bbx = -round(res.W / 2);
1933 res.node.style["v-text-align"] = "center";
1938 addGradientFill = function (o, gradient) {
1939 o.attrs = o.attrs || {};
1940 var attrs = o.attrs,
1944 o.attrs.gradient = gradient;
1945 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
1950 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
1955 gradient = gradient[split](/\s*\-\s*/);
1956 if (type == "linear") {
1957 var angle = gradient.shift();
1958 angle = -toFloat(angle);
1963 var dots = parseDots(gradient);
1967 o = o.shape || o.node;
1968 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
1969 !fill.parentNode && o.appendChild(fill);
1972 fill.method = "none";
1973 fill.color = dots[0].color;
1974 fill.color2 = dots[dots[length] - 1].color;
1976 for (var i = 0, ii = dots[length]; i < ii; i++) {
1977 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
1979 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
1980 if (type == "radial") {
1981 fill.type = "gradientradial";
1982 fill.focus = "100%";
1983 fill.focussize = fxfy;
1984 fill.focusposition = fxfy;
1986 fill.type = "gradient";
1987 fill.angle = (270 - angle) % 360;
1992 Element = function (node, group, vml) {
2000 node.raphael = this;
2013 !vml.bottom && (vml.bottom = this);
2014 this.prev = vml.top;
2015 vml.top && (vml.top.next = this);
2019 Element[proto].rotate = function (deg, cx, cy) {
2025 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2027 return this._.rt.deg;
2029 deg = Str(deg)[split](separator);
2030 if (deg[length] - 1) {
2031 cx = toFloat(deg[1]);
2032 cy = toFloat(deg[2]);
2034 deg = toFloat(deg[0]);
2036 this._.rt.deg = deg;
2038 this._.rt.deg += deg;
2040 cy == null && (cx = null);
2043 this.setBox(this.attrs, cx, cy);
2044 this.Group.style.rotation = this._.rt.deg;
2045 // gradient fix for rotation. TODO
2046 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2047 // fill = fill[0] || {};
2048 // var b = ((360 - this._.rt.deg) - 270) % 360;
2049 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2052 Element[proto].setBox = function (params, cx, cy) {
2056 var gs = this.Group.style,
2057 os = (this.shape && this.shape.style) || this.node.style;
2058 params = params || {};
2059 for (var i in params) if (params[has](i)) {
2060 this.attrs[i] = params[i];
2062 cx = cx || this._.rt.cx;
2063 cy = cy || this._.rt.cy;
2064 var attr = this.attrs,
2069 switch (this.type) {
2071 x = attr.cx - attr.r;
2072 y = attr.cy - attr.r;
2076 x = attr.cx - attr.rx;
2077 y = attr.cy - attr.ry;
2084 w = attr.width || 0;
2085 h = attr.height || 0;
2088 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2089 x = attr.x - round(this.W / 2);
2090 y = attr.y - this.H / 2;
2096 if (!this.attrs.path) {
2099 w = this.paper.width;
2100 h = this.paper.height;
2102 var dim = pathDimensions(this.attrs.path);
2112 w = this.paper.width;
2113 h = this.paper.height;
2116 cx = (cx == null) ? x + w / 2 : cx;
2117 cy = (cy == null) ? y + h / 2 : cy;
2118 var left = cx - this.paper.width / 2,
2119 top = cy - this.paper.height / 2, t;
2120 gs.left != (t = left + "px") && (gs.left = t);
2121 gs.top != (t = top + "px") && (gs.top = t);
2122 this.X = pathlike[has](this.type) ? -left : x;
2123 this.Y = pathlike[has](this.type) ? -top : y;
2126 if (pathlike[has](this.type)) {
2127 os.left != (t = -left * zoom + "px") && (os.left = t);
2128 os.top != (t = -top * zoom + "px") && (os.top = t);
2129 } else if (this.type == "text") {
2130 os.left != (t = -left + "px") && (os.left = t);
2131 os.top != (t = -top + "px") && (os.top = t);
2133 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2134 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2135 os.left != (t = x - left + "px") && (os.left = t);
2136 os.top != (t = y - top + "px") && (os.top = t);
2137 os.width != (t = w + "px") && (os.width = t);
2138 os.height != (t = h + "px") && (os.height = t);
2141 Element[proto].hide = function () {
2142 !this.removed && (this.Group.style.display = "none");
2145 Element[proto].show = function () {
2146 !this.removed && (this.Group.style.display = "block");
2149 Element[proto].getBBox = function () {
2153 if (pathlike[has](this.type)) {
2154 return pathDimensions(this.attrs.path);
2157 x: this.X + (this.bbx || 0),
2163 Element[proto].remove = function () {
2167 tear(this, this.paper);
2168 this.node.parentNode.removeChild(this.node);
2169 this.Group.parentNode.removeChild(this.Group);
2170 this.shape && this.shape.parentNode.removeChild(this.shape);
2171 for (var i in this) {
2174 this.removed = true;
2176 Element[proto].attr = function (name, value) {
2182 for (var i in this.attrs) if (this.attrs[has](i)) {
2183 res[i] = this.attrs[i];
2185 this._.rt.deg && (res.rotation = this.rotate());
2186 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2187 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2190 if (value == null && R.is(name, string)) {
2191 if (name == "translation") {
2192 return translate.call(this);
2194 if (name == "rotation") {
2195 return this.rotate();
2197 if (name == "scale") {
2198 return this.scale();
2200 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2201 return this.attrs.gradient;
2203 return this.attrs[name];
2205 if (this.attrs && value == null && R.is(name, array)) {
2206 var ii, values = {};
2207 for (i = 0, ii = name[length]; i < ii; i++) {
2208 values[name[i]] = this.attr(name[i]);
2213 if (value != null) {
2215 params[name] = value;
2217 value == null && R.is(name, "object") && (params = name);
2219 if (params.text && this.type == "text") {
2220 this.node.string = params.text;
2222 setFillAndStroke(this, params);
2223 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2224 addGradientFill(this, params.gradient);
2226 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2230 Element[proto].toFront = function () {
2231 !this.removed && this.Group.parentNode[appendChild](this.Group);
2232 this.paper.top != this && tofront(this, this.paper);
2235 Element[proto].toBack = function () {
2239 if (this.Group.parentNode.firstChild != this.Group) {
2240 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2241 toback(this, this.paper);
2245 Element[proto].insertAfter = function (element) {
2249 if (element.Group.nextSibling) {
2250 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2252 element.Group.parentNode[appendChild](this.Group);
2254 insertafter(this, element, this.paper);
2257 Element[proto].insertBefore = function (element) {
2261 element.Group.parentNode.insertBefore(this.Group, element.Group);
2262 insertbefore(this, element, this.paper);
2265 var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
2266 Element[proto].blur = function (size) {
2267 var s = this.node.runtimeStyle,
2269 f = f.replace(blurregexp, E);
2271 this.attrs.blur = size;
2272 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2273 s.margin = Raphael.format("-{0}px 0 0 -{0}px", Math.round(+size || 1.5));
2277 delete this.attrs.blur;
2281 theCircle = function (vml, x, y, r) {
2282 var g = createNode("group"),
2283 o = createNode("oval"),
2285 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2286 g.coordsize = coordsize;
2287 g.coordorigin = vml.coordorigin;
2289 var res = new Element(o, g, vml);
2290 res.type = "circle";
2291 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2295 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2296 vml.canvas[appendChild](g);
2299 function rectPath(x, y, w, h, r) {
2301 return R.format("M{0},{1}l{2},0a{3},{3},0,0,1,{3},{3}l0,{5}a{3},{3},0,0,1,{4},{3}l{6},0a{3},{3},0,0,1,{4},{4}l0,{7}a{3},{3},0,0,1,{3},{4}z", x + r, y, w - r * 2, r, -r, h - r * 2, r * 2 - w, r * 2 - h);
2303 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2306 theRect = function (vml, x, y, w, h, r) {
2307 var path = rectPath(x, y, w, h, r),
2308 res = vml.path(path),
2312 res.W = a.width = w;
2313 res.H = a.height = h;
2319 theEllipse = function (vml, x, y, rx, ry) {
2320 var g = createNode("group"),
2321 o = createNode("oval"),
2323 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2324 g.coordsize = coordsize;
2325 g.coordorigin = vml.coordorigin;
2327 var res = new Element(o, g, vml);
2328 res.type = "ellipse";
2329 setFillAndStroke(res, {stroke: "#000"});
2334 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2335 vml.canvas[appendChild](g);
2338 theImage = function (vml, src, x, y, w, h) {
2339 var g = createNode("group"),
2340 o = createNode("image"),
2342 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2343 g.coordsize = coordsize;
2344 g.coordorigin = vml.coordorigin;
2347 var res = new Element(o, g, vml);
2349 res.attrs.src = src;
2354 res.setBox({x: x, y: y, width: w, height: h});
2355 vml.canvas[appendChild](g);
2358 theText = function (vml, x, y, text) {
2359 var g = createNode("group"),
2360 el = createNode("shape"),
2362 path = createNode("path"),
2364 o = createNode("textpath");
2365 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2366 g.coordsize = coordsize;
2367 g.coordorigin = vml.coordorigin;
2368 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2369 path.textpathok = true;
2370 ol.width = vml.width;
2371 ol.height = vml.height;
2372 o.string = Str(text);
2375 el[appendChild](path);
2377 var res = new Element(o, g, vml);
2379 res.textpath = path;
2381 res.attrs.text = text;
2386 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2388 vml.canvas[appendChild](g);
2391 setSize = function (width, height) {
2392 var cs = this.canvas.style;
2393 width == +width && (width += "px");
2394 height == +height && (height += "px");
2397 cs.clip = "rect(0 " + width + " " + height + " 0)";
2401 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2403 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2404 createNode = function (tagName) {
2405 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2408 createNode = function (tagName) {
2409 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2412 create = function () {
2413 var con = getContainer[apply](0, arguments),
2414 container = con.container,
2415 height = con.height,
2421 throw new Error("VML container not found.");
2423 var res = new Paper,
2424 c = res.canvas = doc.createElement("div"),
2428 width = width || 512;
2429 height = height || 342;
2430 width == +width && (width += "px");
2431 height == +height && (height += "px");
2434 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2435 res.coordorigin = "0 0";
2436 res.span = doc.createElement("span");
2437 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2438 c[appendChild](res.span);
2439 cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2440 if (container == 1) {
2441 doc.body[appendChild](c);
2444 cs.position = "absolute";
2446 if (container.firstChild) {
2447 container.insertBefore(c, container.firstChild);
2449 container[appendChild](c);
2452 plugins.call(res, res, R.fn);
2455 Paper[proto].clear = function () {
2456 this.canvas.innerHTML = E;
2457 this.span = doc.createElement("span");
2458 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2459 this.canvas[appendChild](this.span);
2460 this.bottom = this.top = null;
2462 Paper[proto].remove = function () {
2463 this.canvas.parentNode.removeChild(this.canvas);
2464 for (var i in this) {
2465 this[i] = removed(i);
2472 // Safari or Chrome (WebKit) rendering bug workaround method
2473 if ((/^Apple|^Google/).test(win.navigator.vendor) && (!(win.navigator.userAgent.indexOf("Version/4.0") + 1) || win.navigator.platform.slice(0, 2) == "iP")) {
2474 Paper[proto].safari = function () {
2475 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
2476 win.setTimeout(function () {rect.remove();});
2479 Paper[proto].safari = function () {};
2483 var preventDefault = function () {
2484 this.returnValue = false;
2486 preventTouch = function () {
2487 return this.originalEvent.preventDefault();
2489 stopPropagation = function () {
2490 this.cancelBubble = true;
2492 stopTouch = function () {
2493 return this.originalEvent.stopPropagation();
2495 addEvent = (function () {
2496 if (doc.addEventListener) {
2497 return function (obj, type, fn, element) {
2498 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2499 var f = function (e) {
2500 if (supportsTouch && touchMap[has](type)) {
2501 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2502 if (e.targetTouches[i].target == obj) {
2504 e = e.targetTouches[i];
2505 e.originalEvent = olde;
2506 e.preventDefault = preventTouch;
2507 e.stopPropagation = stopTouch;
2512 return fn.call(element, e);
2514 obj.addEventListener(realName, f, false);
2515 return function () {
2516 obj.removeEventListener(realName, f, false);
2520 } else if (doc.attachEvent) {
2521 return function (obj, type, fn, element) {
2522 var f = function (e) {
2524 e.preventDefault = e.preventDefault || preventDefault;
2525 e.stopPropagation = e.stopPropagation || stopPropagation;
2526 return fn.call(element, e);
2528 obj.attachEvent("on" + type, f);
2529 var detacher = function () {
2530 obj.detachEvent("on" + type, f);
2537 for (var i = events[length]; i--;) {
2538 (function (eventName) {
2539 R[eventName] = Element[proto][eventName] = function (fn) {
2540 if (R.is(fn, "function")) {
2541 this.events = this.events || [];
2542 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
2546 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2547 var events = this.events,
2549 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2551 events.splice(l, 1);
2552 !events.length && delete this.events;
2559 Element[proto].hover = function (f_in, f_out) {
2560 return this.mouseover(f_in).mouseout(f_out);
2562 Element[proto].unhover = function (f_in, f_out) {
2563 return this.unmouseover(f_in).unmouseout(f_out);
2565 Element[proto].drag = function (onmove, onstart, onend) {
2567 var el = this.mousedown(function (e) {
2568 (e.originalEvent ? e.originalEvent : e).preventDefault();
2569 this._drag.x = e.clientX;
2570 this._drag.y = e.clientY;
2571 this._drag.id = e.identifier;
2572 onstart && onstart.call(this, e.clientX, e.clientY);
2573 Raphael.mousemove(move).mouseup(up);
2575 move = function (e) {
2578 if (supportsTouch) {
2579 var i = e.touches.length,
2582 touch = e.touches[i];
2583 if (touch.identifier == el._drag.id) {
2586 (e.originalEvent ? e.originalEvent : e).preventDefault();
2593 onmove && onmove.call(el, x - el._drag.x, y - el._drag.y, x, y);
2597 Raphael.unmousemove(move).unmouseup(up);
2598 onend && onend.call(el);
2602 Paper[proto].circle = function (x, y, r) {
2603 return theCircle(this, x || 0, y || 0, r || 0);
2605 Paper[proto].rect = function (x, y, w, h, r) {
2606 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2608 Paper[proto].ellipse = function (x, y, rx, ry) {
2609 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2611 Paper[proto].path = function (pathString) {
2612 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2613 return thePath(R.format[apply](R, arguments), this);
2615 Paper[proto].image = function (src, x, y, w, h) {
2616 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2618 Paper[proto].text = function (x, y, text) {
2619 return theText(this, x || 0, y || 0, text || E);
2621 Paper[proto].set = function (itemsArray) {
2622 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2623 return new Set(itemsArray);
2625 Paper[proto].setSize = setSize;
2626 Paper[proto].top = Paper[proto].bottom = null;
2627 Paper[proto].raphael = R;
2629 return this.x + S + this.y;
2631 Element[proto].resetScale = function () {
2637 this.attrs.scale = "1 1";
2639 Element[proto].scale = function (x, y, cx, cy) {
2643 if (x == null && y == null) {
2658 var bb = this.getBBox(),
2659 rcx = bb.x + bb.width / 2,
2660 rcy = bb.y + bb.height / 2,
2663 cx = (+cx || cx == 0) ? cx : rcx;
2664 cy = (+cy || cy == 0) ? cy : rcy;
2665 var dirx = ~~(x / math.abs(x)),
2666 diry = ~~(y / math.abs(y)),
2667 s = this.node.style,
2668 ncx = cx + (rcx - cx) * kx,
2669 ncy = cy + (rcy - cy) * ky;
2670 switch (this.type) {
2673 var neww = a.width * dirx * kx,
2674 newh = a.height * diry * ky;
2677 r: a.r * mmin(dirx * kx, diry * ky),
2686 rx: a.rx * dirx * kx,
2687 ry: a.ry * diry * ky,
2688 r: a.r * mmin(dirx * kx, diry * ky),
2700 var path = pathToRelative(a.path),
2702 for (var i = 0, ii = path[length]; i < ii; i++) {
2704 P0 = upperCase.call(p[0]);
2705 if (P0 == "M" && skip) {
2711 p[path[i][length] - 2] *= kx;
2712 p[path[i][length] - 1] *= ky;
2715 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2716 } else if (P0 == "H") {
2717 for (var j = 1, jj = p[length]; j < jj; j++) {
2720 } else if (P0 == "V") {
2721 for (j = 1, jj = p[length]; j < jj; j++) {
2725 for (j = 1, jj = p[length]; j < jj; j++) {
2726 p[j] *= (j % 2) ? kx : ky;
2730 var dim2 = pathDimensions(path);
2731 dx = ncx - dim2.x - dim2.width / 2;
2732 dy = ncy - dim2.y - dim2.height / 2;
2735 this.attr({path: path});
2738 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2739 if (this.transformations) {
2740 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2741 this.node[setAttribute]("transform", this.transformations[join](S));
2742 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2743 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2744 this.attr({x: dx, y: dy});
2748 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2749 ", M12=0, M21=0, M22=", diry,
2750 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2751 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2754 if (this.transformations) {
2755 this.transformations[2] = E;
2756 this.node[setAttribute]("transform", this.transformations[join](S));
2760 this.node.filterMatrix = E;
2761 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2764 a.scale = [x, y, cx, cy][join](S);
2770 Element[proto].clone = function () {
2774 var attr = this.attr();
2776 delete attr.translation;
2777 return this.paper[this.type]().attr(attr);
2779 var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2782 for (var i = 0; i < 1.001; i+=.001) {
2783 var dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2784 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2785 if (len >= length) {
2791 getLengthFactory = function (istotal, subpath) {
2792 return function (path, length, onlystart) {
2793 path = path2curve(path);
2794 var x, y, p, l, sp = "", subpaths = {}, point,
2796 for (var i = 0, ii = path.length; i < ii; i++) {
2802 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2803 if (len + l > length) {
2804 if (subpath && !subpaths.start) {
2805 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2806 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2807 if (onlystart) {return sp;}
2808 subpaths.start = sp;
2809 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2815 if (!istotal && !subpath) {
2816 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2817 return {x: point.x, y: point.y, alpha: point.alpha};
2827 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
2828 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
2832 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
2833 var old = {x: 0, y: 0},
2835 for (var i = 0; i < 1.01; i+=.01) {
2836 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
2837 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2842 var getTotalLength = getLengthFactory(1),
2843 getPointAtLength = getLengthFactory(),
2844 getSubpathsAtLength = getLengthFactory(0, 1);
2845 Element[proto].getTotalLength = function () {
2846 if (this.type != "path") {return;}
2847 if (this.node.getTotalLength) {
2848 return this.node.getTotalLength();
2850 return getTotalLength(this.attrs.path);
2852 Element[proto].getPointAtLength = function (length) {
2853 if (this.type != "path") {return;}
2854 return getPointAtLength(this.attrs.path, length);
2856 Element[proto].getSubpath = function (from, to) {
2857 if (this.type != "path") {return;}
2858 if (math.abs(this.getTotalLength() - to) < 1e-6) {
2859 return getSubpathsAtLength(this.attrs.path, from).end;
2861 var a = getSubpathsAtLength(this.attrs.path, to, 1);
2862 return from ? getSubpathsAtLength(a, from).end : a;
2865 // animation easing formulas
2866 R.easing_formulas = {
2867 linear: function (n) {
2874 return pow(n - 1, 3) + 1;
2876 "<>": function (n) {
2879 return pow(n, 3) / 2;
2882 return (pow(n, 3) + 2) / 2;
2884 backIn: function (n) {
2886 return n * n * ((s + 1) * n - s);
2888 backOut: function (n) {
2891 return n * n * ((s + 1) * n + s) + 1;
2893 elastic: function (n) {
2894 if (n == 0 || n == 1) {
2899 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
2901 bounce: function (n) {
2910 l = s * n * n + .75;
2912 if (n < (2.5 / p)) {
2914 l = s * n * n + .9375;
2917 l = s * n * n + .984375;
2925 var animationElements = {length : 0},
2926 animation = function () {
2927 var Now = +new Date;
2928 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
2929 var e = animationElements[l];
2930 if (e.stop || e.el.removed) {
2931 delete animationElements[l];
2932 animationElements[length]--;
2935 var time = Now - e.start,
2944 callback = e.callback,
2948 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
2949 for (var attr in from) if (from[has](attr)) {
2950 switch (availableAnimAttrs[attr]) {
2952 now = pos * ms * diff[attr];
2953 to.back && (now = to.len - now);
2954 var point = getPointAtLength(to[attr], now);
2955 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
2958 that.translate(point.x - diff.sx, point.y - diff.sy);
2959 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
2962 now = +from[attr] + pos * ms * diff[attr];
2966 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
2967 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
2968 upto255(round(from[attr].b + pos * ms * diff[attr].b))
2973 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
2974 now[i] = [from[attr][i][0]];
2975 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
2976 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
2978 now[i] = now[i][join](S);
2985 var x = diff[attr][0] * (time - prev),
2986 y = diff[attr][1] * (time - prev);
2992 now = +from[attr][0] + pos * ms * diff[attr][0];
2993 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2996 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);
3002 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3011 that._run && that._run.call(that);
3014 point = getPointAtLength(to.along, to.len * !to.back);
3015 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3016 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3018 (t.x || t.y) && that.translate(-t.x, -t.y);
3019 to.scale && (to.scale += E);
3021 delete animationElements[l];
3022 animationElements[length]--;
3023 that.in_animation = null;
3024 R.is(callback, "function") && callback.call(that);
3028 R.svg && that && that.paper && that.paper.safari();
3029 animationElements[length] && win.setTimeout(animation);
3031 upto255 = function (color) {
3032 return mmax(mmin(color, 255), 0);
3034 translate = function (x, y) {
3036 return {x: this._.tx, y: this._.ty, toString: x_y};
3040 switch (this.type) {
3043 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3048 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3051 var path = pathToRelative(this.attrs.path);
3054 this.attr({path: path});
3059 Element[proto].animateWith = function (element, params, ms, easing, callback) {
3060 animationElements[element.id] && (params.start = animationElements[element.id].start);
3061 return this.animate(params, ms, easing, callback);
3063 Element[proto].animateAlong = along();
3064 Element[proto].animateAlongBack = along(1);
3065 function along(isBack) {
3066 return function (path, ms, rotate, callback) {
3067 var params = {back: isBack};
3068 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3069 path && path.constructor == Element && (path = path.attrs.path);
3070 path && (params.along = path);
3071 return this.animate(params, ms, callback);
3074 Element[proto].onAnimation = function (f) {
3078 Element[proto].animate = function (params, ms, easing, callback) {
3079 if (R.is(easing, "function") || !easing) {
3080 callback = easing || null;
3085 for (var attr in params) if (params[has](attr)) {
3086 if (availableAnimAttrs[has](attr)) {
3087 from[attr] = this.attr(attr);
3088 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3089 to[attr] = params[attr];
3090 switch (availableAnimAttrs[attr]) {
3092 var len = getTotalLength(params[attr]),
3093 point = getPointAtLength(params[attr], len * !!params.back),
3094 bb = this.getBBox();
3095 diff[attr] = len / ms;
3100 to.rot = params.rot;
3101 to.back = params.back;
3103 params.rot && (diff.r = toFloat(this.rotate()) || 0);
3106 diff[attr] = (to[attr] - from[attr]) / ms;
3109 from[attr] = R.getRGB(from[attr]);
3110 var toColour = R.getRGB(to[attr]);
3112 r: (toColour.r - from[attr].r) / ms,
3113 g: (toColour.g - from[attr].g) / ms,
3114 b: (toColour.b - from[attr].b) / ms
3118 var pathes = path2curve(from[attr], to[attr]);
3119 from[attr] = pathes[0];
3120 var toPath = pathes[1];
3122 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3123 diff[attr][i] = [0];
3124 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3125 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3130 var values = Str(params[attr])[split](separator),
3131 from2 = Str(from[attr])[split](separator);
3134 from[attr] = [0, 0];
3135 diff[attr] = [values[0] / ms, values[1] / ms];
3138 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3139 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3142 params[attr] = values;
3143 from[attr] = Str(from[attr])[split](separator);
3144 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3147 from[attr] = Str(from[attr])[split](separator);
3151 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3160 this.in_animation = 1;
3161 animationElements[this.id] = {
3162 start: params.start || +new Date,
3172 ++animationElements[length] == 1 && animation();
3175 Element[proto].stop = function () {
3176 animationElements[this.id] && animationElements[length]--;
3177 delete animationElements[this.id];
3180 Element[proto].translate = function (x, y) {
3181 return this.attr({translation: x + " " + y});
3183 Element[proto][toString] = function () {
3184 return "Rapha\xebl\u2019s object";
3186 R.ae = animationElements;
3189 var Set = function (items) {
3194 for (var i = 0, ii = items[length]; i < ii; i++) {
3195 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3196 this[this.items[length]] = this.items[this.items[length]] = items[i];
3202 Set[proto][push] = function () {
3205 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3206 item = arguments[i];
3207 if (item && (item.constructor == Element || item.constructor == Set)) {
3208 len = this.items[length];
3209 this[len] = this.items[len] = item;
3215 Set[proto].pop = function () {
3216 delete this[this[length]--];
3217 return this.items.pop();
3219 for (var method in Element[proto]) if (Element[proto][has](method)) {
3220 Set[proto][method] = (function (methodname) {
3221 return function () {
3222 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3223 this.items[i][methodname][apply](this.items[i], arguments);
3229 Set[proto].attr = function (name, value) {
3230 if (name && R.is(name, array) && R.is(name[0], "object")) {
3231 for (var j = 0, jj = name[length]; j < jj; j++) {
3232 this.items[j].attr(name[j]);
3235 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3236 this.items[i].attr(name, value);
3241 Set[proto].animate = function (params, ms, easing, callback) {
3242 (R.is(easing, "function") || !easing) && (callback = easing || null);
3243 var len = this.items[length],
3248 callback && (collector = function () {
3249 !--len && callback.call(set);
3251 easing = R.is(easing, string) ? easing : collector;
3252 item = this.items[--i].animate(params, ms, easing, collector);
3254 this.items[i].animateWith(item, params, ms, easing, collector);
3258 Set[proto].insertAfter = function (el) {
3259 var i = this.items[length];
3261 this.items[i].insertAfter(el);
3265 Set[proto].getBBox = function () {
3270 for (var i = this.items[length]; i--;) {
3271 var box = this.items[i].getBBox();
3274 w[push](box.x + box.width);
3275 h[push](box.y + box.height);
3277 x = mmin[apply](0, x);
3278 y = mmin[apply](0, y);
3282 width: mmax[apply](0, w) - x,
3283 height: mmax[apply](0, h) - y
3286 Set[proto].clone = function (s) {
3288 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3289 s[push](this.items[i].clone());
3294 R.registerFont = function (font) {
3298 this.fonts = this.fonts || {};
3304 family = font.face["font-family"];
3305 for (var prop in font.face) if (font.face[has](prop)) {
3306 fontcopy.face[prop] = font.face[prop];
3308 if (this.fonts[family]) {
3309 this.fonts[family][push](fontcopy);
3311 this.fonts[family] = [fontcopy];
3314 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3315 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3316 var path = font.glyphs[glyph];
3317 fontcopy.glyphs[glyph] = {
3320 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3321 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3325 for (var k in path.k) if (path[has](k)) {
3326 fontcopy.glyphs[glyph].k[k] = path.k[k];
3333 Paper[proto].getFont = function (family, weight, style, stretch) {
3334 stretch = stretch || "normal";
3335 style = style || "normal";
3336 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3340 var font = R.fonts[family];
3342 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3343 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3344 if (name.test(fontName)) {
3345 font = R.fonts[fontName];
3352 for (var i = 0, ii = font[length]; i < ii; i++) {
3354 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3361 Paper[proto].print = function (x, y, string, font, size, origin) {
3362 origin = origin || "middle"; // baseline|middle
3363 var out = this.set(),
3364 letters = Str(string)[split](E),
3368 R.is(font, string) && (font = this.getFont(font));
3370 scale = (size || 16) / font.face["units-per-em"];
3371 var bb = font.face.bbox.split(separator),
3373 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3374 for (var i = 0, ii = letters[length]; i < ii; i++) {
3375 var prev = i && font.glyphs[letters[i - 1]] || {},
3376 curr = font.glyphs[letters[i]];
3377 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
3378 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3380 out.scale(scale, scale, top, height).translate(x - top, y - height);
3385 var formatrg = /\{(\d+)\}/g;
3386 R.format = function (token, params) {
3387 var args = R.is(params, array) ? [0][concat](params) : arguments;
3388 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3389 return args[++i] == null ? E : args[i];
3393 R.ninja = function () {
3394 oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
3397 R.el = Element[proto];