2 * Raphael 1.5.0 - 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.
9 if (R.is(arguments[0], array)) {
11 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
13 for (var i = 0, ii = a[length]; i < ii; i++) {
15 elements[has](j.type) && res[push](cnv[j.type]().attr(j));
19 return create[apply](R, arguments);
22 var separator = /[, ]+/,
23 elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
24 formatrg = /\{(\d+)\}/g,
26 has = "hasOwnProperty",
30 was: Object[proto][has].call(win, "Raphael"),
34 this.customAttributes = {};
37 appendChild = "appendChild",
40 supportsTouch = "createTouch" in doc,
45 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
47 mousedown: "touchstart",
48 mousemove: "touchmove",
53 lowerCase = Str[proto].toLowerCase,
61 toString = "toString",
63 objectToString = Object[proto][toString],
67 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
68 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*\)|hsb\(\s*([\d\.]+(?:deg|\xb0)?\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsb\(\s*([\d\.]+(?:deg|\xb0|%)\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hsl\(\s*([\d\.]+(?:deg|\xb0)?\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsl\(\s*([\d\.]+(?:deg|\xb0|%)\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
69 isnan = /^(NaN|-?Infinity)$/,
70 bezierrg = /^cubic-bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
72 setAttribute = "setAttribute",
75 ms = " progid:DXImageTransform.Microsoft",
76 upperCase = Str[proto].toUpperCase,
77 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},
78 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},
80 p2s = /,?([achlmqrstvxz]),?/gi,
81 commaSpaces = /\s*,\s*/,
82 hsrg = {hs: 1, rg: 1},
83 animKeyFrames= /^(from|to|\d+%)$/,
84 pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
85 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
86 sortByKey = function (a, b) {
90 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
91 if (R.type == "VML") {
92 var d = doc.createElement("div"),
94 d.innerHTML = '<v:shape adj="1"/>';
96 b.style.behavior = "url(#default#VML)";
97 if (!(b && typeof b.adj == "object")) {
102 R.svg = !(R.vml = R.type == "VML");
103 Paper[proto] = R[proto];
104 paperproto = Paper[proto];
108 R.is = function (o, type) {
109 type = lowerCase.call(type);
110 if (type == "finite") {
111 return !isnan.test(+o);
113 return (type == "null" && o === null) ||
114 (type == typeof o) ||
115 (type == "object" && o === Object(o)) ||
116 (type == "array" && Array.isArray && Array.isArray(o)) ||
117 objectToString.call(o).slice(8, -1).toLowerCase() == type;
119 R.angle = function (x1, y1, x2, y2, x3, y3) {
126 return ((x < 0) * 180 + math.atan(-y / -x) * 180 / math.PI + 360) % 360;
128 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
131 R.snapTo = function (values, value, tolerance) {
132 tolerance = tolerance || 10;
133 values = [][concat](values);
134 var i = values.length;
135 while (i--) if (math.abs(values[i] - value) <= tolerance) {
141 R.setWindow = function (newwin) {
146 var toHex = function (color) {
148 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
149 var trim = /^\s+|\s+$/g;
152 var docum = new ActiveXObject("htmlfile");
153 docum.write("<body>");
157 bod = createPopup().document.body;
159 var range = bod.createTextRange();
160 toHex = cacher(function (color) {
162 bod.style.color = Str(color)[rp](trim, E);
163 var value = range.queryCommandValue("ForeColor");
164 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
165 return "#" + ("000000" + value[toString](16)).slice(-6);
171 var i = doc.createElement("i");
172 i.title = "Rapha\xebl Colour Picker";
173 i.style.display = "none";
174 doc.body[appendChild](i);
175 toHex = cacher(function (color) {
176 i.style.color = color;
177 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
182 hsbtoString = function () {
183 return "hsb(" + [this.h, this.s, this.b] + ")";
185 hsltoString = function () {
186 return "hsl(" + [this.h, this.s, this.l] + ")";
188 rgbtoString = function () {
191 R.hsb2rgb = function (h, s, b) {
192 if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
197 return R.hsl2rgb(h, s, b / 2);
199 R.hsl2rgb = function (h, s, l) {
200 if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
205 if (h > 1 || s > 1 || l > 1) {
211 channels = ["r", "g", "b"],
226 for (var i = 0, ii = channels.length; i < ii; i++) {
227 t3 = h + 1 / 3 * -(i - 1);
231 rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
232 } else if (t3 * 2 < 1) {
233 rgb[channels[i]] = t2;
234 } else if (t3 * 3 < 2) {
235 rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
237 rgb[channels[i]] = t1;
244 r = (~~rgb.r)[toString](16);
245 g = (~~rgb.g)[toString](16);
246 b = (~~rgb.b)[toString](16);
250 rgb.hex = "#" + r + g + b;
251 rgb.toString = rgbtoString;
254 R.rgb2hsb = function (red, green, blue) {
255 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
260 if (green == null && R.is(red, string)) {
261 var clr = R.getRGB(red);
266 if (red > 1 || green > 1 || blue > 1) {
271 var max = mmax(red, green, blue),
272 min = mmin(red, green, blue),
277 return {h: 0, s: 0, b: max, toString: hsbtoString};
279 var delta = (max - min);
280 saturation = delta / max;
282 hue = (green - blue) / delta;
283 } else if (green == max) {
284 hue = 2 + ((blue - red) / delta);
286 hue = 4 + ((red - green) / delta);
292 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
294 R.rgb2hsl = function (red, green, blue) {
295 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
300 if (green == null && R.is(red, string)) {
301 var clr = R.getRGB(red);
306 if (red > 1 || green > 1 || blue > 1) {
311 var max = mmax(red, green, blue),
312 min = mmin(red, green, blue),
318 hsl = {h: 0, s: 0, l: l};
320 var delta = max - min;
321 s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
323 h = (green - blue) / delta;
324 } else if (green == max) {
325 h = 2 + (blue - red) / delta;
327 h = 4 + (red - green) / delta;
332 hsl = {h: h, s: s, l: l};
334 hsl.toString = hsltoString;
337 R._path2string = function () {
338 return this.join(",")[rp](p2s, "$1");
340 function cacher(f, scope, postprocessor) {
342 var arg = Array[proto].slice.call(arguments, 0),
343 args = arg[join]("\u25ba"),
344 cache = newf.cache = newf.cache || {},
345 count = newf.count = newf.count || [];
346 if (cache[has](args)) {
347 return postprocessor ? postprocessor(cache[args]) : cache[args];
349 count[length] >= 1e3 && delete cache[count.shift()];
351 cache[args] = f[apply](scope, arg);
352 return postprocessor ? postprocessor(cache[args]) : cache[args];
357 R.getRGB = cacher(function (colour) {
358 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
359 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
361 if (colour == "none") {
362 return {r: -1, g: -1, b: -1, hex: "none"};
364 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
371 rgb = colour.match(colourRegExp);
374 blue = toInt(rgb[2].substring(5), 16);
375 green = toInt(rgb[2].substring(3, 5), 16);
376 red = toInt(rgb[2].substring(1, 3), 16);
379 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
380 green = toInt((t = rgb[3].charAt(2)) + t, 16);
381 red = toInt((t = rgb[3].charAt(1)) + t, 16);
384 rgb = rgb[4][split](commaSpaces);
385 red = toFloat(rgb[0]);
386 green = toFloat(rgb[1]);
387 blue = toFloat(rgb[2]);
388 opacity = toFloat(rgb[3]);
391 rgb = rgb[5][split](commaSpaces);
392 red = toFloat(rgb[0]) * 2.55;
393 green = toFloat(rgb[1]) * 2.55;
394 blue = toFloat(rgb[2]) * 2.55;
395 opacity = toFloat(rgb[3]);
398 rgb = rgb[6][split](commaSpaces);
399 red = toFloat(rgb[0]);
400 green = toFloat(rgb[1]);
401 blue = toFloat(rgb[2]);
402 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
403 return R.hsb2rgb(red, green, blue);
406 rgb = rgb[7][split](commaSpaces);
407 red = toFloat(rgb[0]) * 2.55;
408 green = toFloat(rgb[1]) * 2.55;
409 blue = toFloat(rgb[2]) * 2.55;
410 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
411 return R.hsb2rgb(red, green, blue);
414 rgb = rgb[8][split](commaSpaces);
415 red = toFloat(rgb[0]);
416 green = toFloat(rgb[1]);
417 blue = toFloat(rgb[2]);
418 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
419 return R.hsl2rgb(red, green, blue);
422 rgb = rgb[9][split](commaSpaces);
423 red = toFloat(rgb[0]) * 2.55;
424 green = toFloat(rgb[1]) * 2.55;
425 blue = toFloat(rgb[2]) * 2.55;
426 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
427 return R.hsl2rgb(red, green, blue);
429 rgb = {r: red, g: green, b: blue};
430 var r = (~~red)[toString](16),
431 g = (~~green)[toString](16),
432 b = (~~blue)[toString](16);
436 rgb.hex = "#" + r + g + b;
437 isFinite(toFloat(opacity)) && (rgb.o = opacity);
440 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
442 R.getColor = function (value) {
443 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
444 rgb = this.hsb2rgb(start.h, start.s, start.b);
449 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
453 R.getColor.reset = function () {
457 R.parsePathString = cacher(function (pathString) {
461 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
463 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
464 data = pathClone(pathString);
467 Str(pathString)[rp](pathCommand, function (a, b, c) {
469 name = lowerCase.call(b);
470 c[rp](pathValues, function (a, b) {
471 b && params[push](+b);
473 if (name == "m" && params[length] > 2) {
474 data[push]([b][concat](params.splice(0, 2)));
476 b = b == "m" ? "l" : "L";
478 while (params[length] >= paramCounts[name]) {
479 data[push]([b][concat](params.splice(0, paramCounts[name])));
480 if (!paramCounts[name]) {
486 data[toString] = R._path2string;
489 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
491 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
492 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
493 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
494 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
495 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
496 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
497 ax = (1 - t) * p1x + t * c1x,
498 ay = (1 - t) * p1y + t * c1y,
499 cx = (1 - t) * c2x + t * p2x,
500 cy = (1 - t) * c2y + t * p2y,
501 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
502 (mx > nx || my < ny) && (alpha += 180);
503 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};
505 var pathDimensions = cacher(function (path) {
507 return {x: 0, y: 0, width: 0, height: 0};
509 path = path2curve(path);
515 for (var i = 0, ii = path[length]; i < ii; i++) {
523 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
524 X = X[concat](dim.min.x, dim.max.x);
525 Y = Y[concat](dim.min.y, dim.max.y);
530 var xmin = mmin[apply](0, X),
531 ymin = mmin[apply](0, Y);
535 width: mmax[apply](0, X) - xmin,
536 height: mmax[apply](0, Y) - ymin
539 pathClone = function (pathArray) {
541 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
542 pathArray = R.parsePathString(pathArray);
544 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
546 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
547 res[i][j] = pathArray[i][j];
550 res[toString] = R._path2string;
553 pathToRelative = cacher(function (pathArray) {
554 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
555 pathArray = R.parsePathString(pathArray);
563 if (pathArray[0][0] == "M") {
569 res[push](["M", x, y]);
571 for (var i = start, ii = pathArray[length]; i < ii; i++) {
574 if (pa[0] != lowerCase.call(pa[0])) {
575 r[0] = lowerCase.call(pa[0]);
583 r[6] = +(pa[6] - x).toFixed(3);
584 r[7] = +(pa[7] - y).toFixed(3);
587 r[1] = +(pa[1] - y).toFixed(3);
593 for (var j = 1, jj = pa[length]; j < jj; j++) {
594 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
603 for (var k = 0, kk = pa[length]; k < kk; k++) {
607 var len = res[i][length];
614 x += +res[i][len - 1];
617 y += +res[i][len - 1];
620 x += +res[i][len - 2];
621 y += +res[i][len - 1];
624 res[toString] = R._path2string;
627 pathToAbsolute = cacher(function (pathArray) {
628 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
629 pathArray = R.parsePathString(pathArray);
637 if (pathArray[0][0] == "M") {
638 x = +pathArray[0][1];
639 y = +pathArray[0][2];
643 res[0] = ["M", x, y];
645 for (var i = start, ii = pathArray[length]; i < ii; i++) {
648 if (pa[0] != upperCase.call(pa[0])) {
649 r[0] = upperCase.call(pa[0]);
670 for (var j = 1, jj = pa[length]; j < jj; j++) {
671 r[j] = +pa[j] + ((j % 2) ? x : y);
675 for (var k = 0, kk = pa[length]; k < kk; k++) {
691 mx = res[i][res[i][length] - 2];
692 my = res[i][res[i][length] - 1];
694 x = res[i][res[i][length] - 2];
695 y = res[i][res[i][length] - 1];
698 res[toString] = R._path2string;
701 l2c = function (x1, y1, x2, y2) {
702 return [x1, y1, x2, y2, x2, y2];
704 q2c = function (x1, y1, ax, ay, x2, y2) {
716 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
717 // for more information of where this math came from visit:
718 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
720 _120 = PI * 120 / 180,
721 rad = PI / 180 * (+angle || 0),
724 rotate = cacher(function (x, y, rad) {
725 var X = x * math.cos(rad) - y * math.sin(rad),
726 Y = x * math.sin(rad) + y * math.cos(rad);
730 xy = rotate(x1, y1, -rad);
733 xy = rotate(x2, y2, -rad);
736 var cos = math.cos(PI / 180 * angle),
737 sin = math.sin(PI / 180 * angle),
740 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
748 k = (large_arc_flag == sweep_flag ? -1 : 1) *
749 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
750 cx = k * rx * y / ry + (x1 + x2) / 2,
751 cy = k * -ry * x / rx + (y1 + y2) / 2,
752 f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
753 f2 = math.asin(((y2 - cy) / ry).toFixed(9));
755 f1 = x1 < cx ? PI - f1 : f1;
756 f2 = x2 < cx ? PI - f2 : f2;
757 f1 < 0 && (f1 = PI * 2 + f1);
758 f2 < 0 && (f2 = PI * 2 + f2);
759 if (sweep_flag && f1 > f2) {
762 if (!sweep_flag && f2 > f1) {
772 if (math.abs(df) > _120) {
776 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
777 x2 = cx + rx * math.cos(f2);
778 y2 = cy + ry * math.sin(f2);
779 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
782 var c1 = math.cos(f1),
786 t = math.tan(df / 4),
790 m2 = [x1 + hx * s1, y1 - hy * c1],
791 m3 = [x2 + hx * s2, y2 - hy * c2],
793 m2[0] = 2 * m1[0] - m2[0];
794 m2[1] = 2 * m1[1] - m2[1];
796 return [m2, m3, m4][concat](res);
798 res = [m2, m3, m4][concat](res)[join]()[split](",");
800 for (var i = 0, ii = res[length]; i < ii; i++) {
801 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
806 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
809 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
810 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
813 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
814 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
815 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
817 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
818 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
822 math.abs(t1) > "1e12" && (t1 = .5);
823 math.abs(t2) > "1e12" && (t2 = .5);
824 if (t1 > 0 && t1 < 1) {
825 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
829 if (t2 > 0 && t2 < 1) {
830 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
834 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
835 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
837 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
838 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
839 math.abs(t1) > "1e12" && (t1 = .5);
840 math.abs(t2) > "1e12" && (t2 = .5);
841 if (t1 > 0 && t1 < 1) {
842 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
846 if (t2 > 0 && t2 < 1) {
847 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
852 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
853 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
856 path2curve = cacher(function (path, path2) {
857 var p = pathToAbsolute(path),
858 p2 = path2 && pathToAbsolute(path2),
859 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
860 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
861 processPath = function (path, d) {
864 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
866 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
873 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
876 nx = d.x + (d.x - (d.bx || d.x));
877 ny = d.y + (d.y - (d.by || d.y));
878 path = ["C", nx, ny][concat](path.slice(1));
881 d.qx = d.x + (d.x - (d.qx || d.x));
882 d.qy = d.y + (d.y - (d.qy || d.y));
883 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
888 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
891 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
894 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
897 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
900 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
905 fixArc = function (pp, i) {
906 if (pp[i][length] > 7) {
910 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
913 ii = mmax(p[length], p2 && p2[length] || 0);
916 fixM = function (path1, path2, a1, a2, i) {
917 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
918 path2.splice(i, 0, ["M", a2.x, a2.y]);
923 ii = mmax(p[length], p2 && p2[length] || 0);
926 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
927 p[i] = processPath(p[i], attrs);
929 p2 && (p2[i] = processPath(p2[i], attrs2));
931 fixM(p, p2, attrs, attrs2, i);
932 fixM(p2, p, attrs2, attrs, i);
935 seglen = seg[length],
936 seg2len = p2 && seg2[length];
937 attrs.x = seg[seglen - 2];
938 attrs.y = seg[seglen - 1];
939 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
940 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
941 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
942 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
943 attrs2.x = p2 && seg2[seg2len - 2];
944 attrs2.y = p2 && seg2[seg2len - 1];
946 return p2 ? [p, p2] : p;
948 parseDots = cacher(function (gradient) {
950 for (var i = 0, ii = gradient[length]; i < ii; i++) {
952 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
953 dot.color = R.getRGB(par[1]);
954 if (dot.color.error) {
957 dot.color = dot.color.hex;
958 par[2] && (dot.offset = par[2] + "%");
961 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
962 if (!dots[i].offset) {
963 var start = toFloat(dots[i - 1].offset || 0),
965 for (var j = i + 1; j < ii; j++) {
966 if (dots[j].offset) {
967 end = dots[j].offset;
976 var d = (end - start) / (j - i + 1);
979 dots[i].offset = start + "%";
985 getContainer = function (x, y, w, h) {
987 if (R.is(x, string) || R.is(x, "object")) {
988 container = R.is(x, string) ? doc.getElementById(x) : x;
989 if (container.tagName) {
992 container: container,
993 width: container.style.pixelWidth || container.offsetWidth,
994 height: container.style.pixelHeight || container.offsetHeight
997 return {container: container, width: y, height: w};
1001 return {container: 1, x: x, y: y, width: w, height: h};
1004 plugins = function (con, add) {
1006 for (var prop in add) {
1007 if (add[has](prop) && !(prop in con)) {
1008 switch (typeof add[prop]) {
1011 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1015 con[prop] = con[prop] || {};
1016 plugins.call(this, con[prop], add[prop]);
1019 con[prop] = add[prop];
1025 tear = function (el, paper) {
1026 el == paper.top && (paper.top = el.prev);
1027 el == paper.bottom && (paper.bottom = el.next);
1028 el.next && (el.next.prev = el.prev);
1029 el.prev && (el.prev.next = el.next);
1031 tofront = function (el, paper) {
1032 if (paper.top === el) {
1037 el.prev = paper.top;
1038 paper.top.next = el;
1041 toback = function (el, paper) {
1042 if (paper.bottom === el) {
1046 el.next = paper.bottom;
1048 paper.bottom.prev = el;
1051 insertafter = function (el, el2, paper) {
1053 el2 == paper.top && (paper.top = el);
1054 el2.next && (el2.next.prev = el);
1059 insertbefore = function (el, el2, paper) {
1061 el2 == paper.bottom && (paper.bottom = el);
1062 el2.prev && (el2.prev.next = el);
1067 removed = function (methodname) {
1068 return function () {
1069 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1072 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
1073 R.pathToRelative = pathToRelative;
1076 paperproto.svgns = "http://www.w3.org/2000/svg";
1077 paperproto.xlink = "http://www.w3.org/1999/xlink";
1078 round = function (num) {
1079 return +num + (~~num === num) * .5;
1081 var $ = function (el, attr) {
1083 for (var key in attr) {
1084 if (attr[has](key)) {
1085 el[setAttribute](key, Str(attr[key]));
1089 el = doc.createElementNS(paperproto.svgns, el);
1090 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1094 R[toString] = function () {
1095 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1097 var thePath = function (pathString, SVG) {
1099 SVG.canvas && SVG.canvas[appendChild](el);
1100 var p = new Element(el, SVG);
1102 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1105 var addGradientFill = function (o, gradient, SVG) {
1106 var type = "linear",
1109 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1114 var dir = ((fy > .5) * 2 - 1);
1115 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1116 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1118 (fy = fy.toFixed(5) - 1e-5 * dir);
1122 gradient = gradient[split](/\s*\-\s*/);
1123 if (type == "linear") {
1124 var angle = gradient.shift();
1125 angle = -toFloat(angle);
1129 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1130 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
1133 if (vector[2] < 0) {
1134 vector[0] = -vector[2];
1137 if (vector[3] < 0) {
1138 vector[1] = -vector[3];
1142 var dots = parseDots(gradient);
1146 var id = o.getAttribute(fillString);
1147 id = id.match(/^url\(#(.*)\)$/);
1148 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1150 var el = $(type + "Gradient");
1151 el.id = "r" + (R._id++)[toString](36);
1152 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1153 SVG.defs[appendChild](el);
1154 for (var i = 0, ii = dots[length]; i < ii; i++) {
1155 var stop = $("stop");
1157 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1158 "stop-color": dots[i].color || "#fff"
1160 el[appendChild](stop);
1163 fill: "url(#" + el.id + ")",
1172 var updatePosition = function (o) {
1173 var bbox = o.getBBox();
1174 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1176 var setFillAndStroke = function (o, params) {
1183 "-..": [3, 1, 1, 1, 1, 1],
1187 "- .": [4, 3, 1, 3],
1188 "--.": [8, 3, 1, 3],
1189 "--..": [8, 3, 1, 3, 1, 3]
1194 addDashes = function (o, value) {
1195 value = dasharray[lowerCase.call(value)];
1197 var width = o.attrs["stroke-width"] || "1",
1198 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1200 var i = value[length];
1202 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1204 $(node, {"stroke-dasharray": dashes[join](",")});
1207 params[has]("rotation") && (rot = params.rotation);
1208 var rotxy = Str(rot)[split](separator);
1209 if (!(rotxy.length - 1)) {
1212 rotxy[1] = +rotxy[1];
1213 rotxy[2] = +rotxy[2];
1215 toFloat(rot) && o.rotate(0, true);
1216 for (var att in params) {
1217 if (params[has](att)) {
1218 if (!availableAttrs[has](att)) {
1221 var value = params[att];
1228 o.rotate(value, true);
1233 var pn = node.parentNode;
1234 if (lowerCase.call(pn.tagName) != "a") {
1236 pn.insertBefore(hl, node);
1237 hl[appendChild](node);
1240 if (att == "target" && value == "blank") {
1241 pn.setAttributeNS(o.paper.xlink, "show", "new");
1243 pn.setAttributeNS(o.paper.xlink, att, value);
1247 node.style.cursor = value;
1250 var rect = Str(value)[split](separator);
1251 if (rect[length] == 4) {
1252 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1253 var el = $("clipPath"),
1255 el.id = "r" + (R._id++)[toString](36);
1262 el[appendChild](rc);
1263 o.paper.defs[appendChild](el);
1264 $(node, {"clip-path": "url(#" + el.id + ")"});
1268 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1269 clip && clip.parentNode.removeChild(clip);
1270 $(node, {"clip-path": E});
1275 if (o.type == "path") {
1276 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1280 node[setAttribute](att, value);
1289 value = -attrs.x - (attrs.width || 0);
1292 if (att == "rx" && o.type == "rect") {
1296 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1297 node[setAttribute](att, value);
1298 o.pattern && updatePosition(o);
1301 node[setAttribute](att, value);
1310 value = -attrs.y - (attrs.height || 0);
1313 if (att == "ry" && o.type == "rect") {
1317 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1318 node[setAttribute](att, value);
1319 o.pattern && updatePosition(o);
1322 if (o.type == "rect") {
1323 $(node, {rx: value, ry: value});
1325 node[setAttribute](att, value);
1329 if (o.type == "image") {
1330 node.setAttributeNS(o.paper.xlink, "href", value);
1333 case "stroke-width":
1334 node.style.strokeWidth = value;
1335 // Need following line for Firefox
1336 node[setAttribute](att, value);
1337 if (attrs["stroke-dasharray"]) {
1338 addDashes(o, attrs["stroke-dasharray"]);
1341 case "stroke-dasharray":
1342 addDashes(o, value);
1345 var xy = Str(value)[split](separator);
1346 xy[0] = +xy[0] || 0;
1347 xy[1] = +xy[1] || 0;
1352 translate.call(o, xy[0], xy[1]);
1355 xy = Str(value)[split](separator);
1356 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1359 var isURL = Str(value).match(ISURL);
1362 var ig = $("image");
1363 el.id = "r" + (R._id++)[toString](36);
1364 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1365 $(ig, {x: 0, y: 0});
1366 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1367 el[appendChild](ig);
1369 var img = doc.createElement("img");
1370 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1371 img.onload = function () {
1372 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1373 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1374 doc.body.removeChild(this);
1377 doc.body[appendChild](img);
1379 o.paper.defs[appendChild](el);
1380 node.style.fill = "url(#" + el.id + ")";
1381 $(node, {fill: "url(#" + el.id + ")"});
1383 o.pattern && updatePosition(o);
1386 var clr = R.getRGB(value);
1388 delete params.gradient;
1389 delete attrs.gradient;
1390 !R.is(attrs.opacity, "undefined") &&
1391 R.is(params.opacity, "undefined") &&
1392 $(node, {opacity: attrs.opacity});
1393 !R.is(attrs["fill-opacity"], "undefined") &&
1394 R.is(params["fill-opacity"], "undefined") &&
1395 $(node, {"fill-opacity": attrs["fill-opacity"]});
1396 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1397 attrs.gradient = value;
1398 attrs.fill = "none";
1401 clr[has]("o") && $(node, {"fill-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
1403 clr = R.getRGB(value);
1404 node[setAttribute](att, clr.hex);
1405 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
1408 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1411 case "fill-opacity":
1412 if (attrs.gradient) {
1413 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1415 var stops = gradient.getElementsByTagName("stop");
1416 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1421 att == "font-size" && (value = toInt(value, 10) + "px");
1422 var cssrule = att[rp](/(\-.)/g, function (w) {
1423 return upperCase.call(w.substring(1));
1425 node.style[cssrule] = value;
1426 // Need following line for Firefox
1427 node[setAttribute](att, value);
1433 tuneText(o, params);
1435 o.rotate(rotxy.join(S));
1437 toFloat(rot) && o.rotate(rot, true);
1441 tuneText = function (el, params) {
1442 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1447 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1449 if (params[has]("text")) {
1450 a.text = params.text;
1451 while (node.firstChild) {
1452 node.removeChild(node.firstChild);
1454 var texts = Str(params.text)[split]("\n");
1455 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1456 var tspan = $("tspan");
1457 i && $(tspan, {dy: fontSize * leading, x: a.x});
1458 tspan[appendChild](doc.createTextNode(texts[i]));
1459 node[appendChild](tspan);
1462 texts = node.getElementsByTagName("tspan");
1463 for (i = 0, ii = texts[length]; i < ii; i++) {
1464 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1468 var bb = el.getBBox(),
1469 dif = a.y - (bb.y + bb.height / 2);
1470 dif && isFinite(dif) && $(node, {y: a.y + dif});
1472 Element = function (node, svg) {
1478 node.raphael = this;
1480 this.attrs = this.attrs || {};
1481 this.transformations = []; // rotate, translate, scale
1485 rt: {deg: 0, cx: 0, cy: 0},
1489 !svg.bottom && (svg.bottom = this);
1490 this.prev = svg.top;
1491 svg.top && (svg.top.next = this);
1495 var elproto = Element[proto];
1496 Element[proto].rotate = function (deg, cx, cy) {
1502 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1504 return this._.rt.deg;
1506 var bbox = this.getBBox();
1507 deg = Str(deg)[split](separator);
1508 if (deg[length] - 1) {
1509 cx = toFloat(deg[1]);
1510 cy = toFloat(deg[2]);
1512 deg = toFloat(deg[0]);
1513 if (cx != null && cx !== false) {
1514 this._.rt.deg = deg;
1516 this._.rt.deg += deg;
1518 (cy == null) && (cx = null);
1521 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1522 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1523 if (this._.rt.deg) {
1524 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1525 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1527 this.transformations[0] = E;
1528 this.clip && $(this.clip, {transform: E});
1530 $(this.node, {transform: this.transformations[join](S)});
1533 Element[proto].hide = function () {
1534 !this.removed && (this.node.style.display = "none");
1537 Element[proto].show = function () {
1538 !this.removed && (this.node.style.display = "");
1541 Element[proto].remove = function () {
1545 tear(this, this.paper);
1546 this.node.parentNode.removeChild(this.node);
1547 for (var i in this) {
1550 this.removed = true;
1552 Element[proto].getBBox = function () {
1556 if (this.type == "path") {
1557 return pathDimensions(this.attrs.path);
1559 if (this.node.style.display == "none") {
1565 bbox = this.node.getBBox();
1567 // Firefox 3.0.x plays badly here
1571 if (this.type == "text") {
1572 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1573 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1574 var bb = this.node.getExtentOfChar(i);
1575 (bb.y < bbox.y) && (bbox.y = bb.y);
1576 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1577 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1580 hide && this.hide();
1583 Element[proto].attr = function (name, value) {
1589 for (var i in this.attrs) if (this.attrs[has](i)) {
1590 res[i] = this.attrs[i];
1592 this._.rt.deg && (res.rotation = this.rotate());
1593 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1594 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1597 if (value == null && R.is(name, string)) {
1598 if (name == "translation") {
1599 return translate.call(this);
1601 if (name == "rotation") {
1602 return this.rotate();
1604 if (name == "scale") {
1605 return this.scale();
1607 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1608 return this.attrs.gradient;
1610 return this.attrs[name];
1612 if (value == null && R.is(name, array)) {
1614 for (var j = 0, jj = name.length; j < jj; j++) {
1615 values[name[j]] = this.attr(name[j]);
1619 if (value != null) {
1621 params[name] = value;
1622 } else if (name != null && R.is(name, "object")) {
1625 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1626 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1627 this.attrs[key] = params[key];
1628 for (var subkey in par) if (par[has](subkey)) {
1629 params[subkey] = par[subkey];
1632 setFillAndStroke(this, params);
1635 Element[proto].toFront = function () {
1639 this.node.parentNode[appendChild](this.node);
1640 var svg = this.paper;
1641 svg.top != this && tofront(this, svg);
1644 Element[proto].toBack = function () {
1648 if (this.node.parentNode.firstChild != this.node) {
1649 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1650 toback(this, this.paper);
1651 var svg = this.paper;
1655 Element[proto].insertAfter = function (element) {
1659 var node = element.node || element[element.length - 1].node;
1660 if (node.nextSibling) {
1661 node.parentNode.insertBefore(this.node, node.nextSibling);
1663 node.parentNode[appendChild](this.node);
1665 insertafter(this, element, this.paper);
1668 Element[proto].insertBefore = function (element) {
1672 var node = element.node || element[0].node;
1673 node.parentNode.insertBefore(this.node, node);
1674 insertbefore(this, element, this.paper);
1677 Element[proto].blur = function (size) {
1678 // Experimental. No Safari support. Use it on your own risk.
1681 var fltr = $("filter"),
1682 blur = $("feGaussianBlur");
1683 t.attrs.blur = size;
1684 fltr.id = "r" + (R._id++)[toString](36);
1685 $(blur, {stdDeviation: +size || 1.5});
1686 fltr.appendChild(blur);
1687 t.paper.defs.appendChild(fltr);
1689 $(t.node, {filter: "url(#" + fltr.id + ")"});
1692 t._blur.parentNode.removeChild(t._blur);
1694 delete t.attrs.blur;
1696 t.node.removeAttribute("filter");
1699 var theCircle = function (svg, x, y, r) {
1700 var el = $("circle");
1701 svg.canvas && svg.canvas[appendChild](el);
1702 var res = new Element(el, svg);
1703 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1704 res.type = "circle";
1708 theRect = function (svg, x, y, w, h, r) {
1710 svg.canvas && svg.canvas[appendChild](el);
1711 var res = new Element(el, svg);
1712 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1717 theEllipse = function (svg, x, y, rx, ry) {
1718 var el = $("ellipse");
1719 svg.canvas && svg.canvas[appendChild](el);
1720 var res = new Element(el, svg);
1721 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1722 res.type = "ellipse";
1726 theImage = function (svg, src, x, y, w, h) {
1727 var el = $("image");
1728 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1729 el.setAttributeNS(svg.xlink, "href", src);
1730 svg.canvas && svg.canvas[appendChild](el);
1731 var res = new Element(el, svg);
1732 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1736 theText = function (svg, x, y, text) {
1738 $(el, {x: x, y: y, "text-anchor": "middle"});
1739 svg.canvas && svg.canvas[appendChild](el);
1740 var res = new Element(el, svg);
1741 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1743 setFillAndStroke(res, res.attrs);
1746 setSize = function (width, height) {
1747 this.width = width || this.width;
1748 this.height = height || this.height;
1749 this.canvas[setAttribute]("width", this.width);
1750 this.canvas[setAttribute]("height", this.height);
1753 create = function () {
1754 var con = getContainer[apply](0, arguments),
1755 container = con && con.container,
1759 height = con.height;
1761 throw new Error("SVG container not found.");
1763 var cnvs = $("svg");
1766 width = width || 512;
1767 height = height || 342;
1769 xmlns: "http://www.w3.org/2000/svg",
1774 if (container == 1) {
1775 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1776 doc.body[appendChild](cnvs);
1778 if (container.firstChild) {
1779 container.insertBefore(cnvs, container.firstChild);
1781 container[appendChild](cnvs);
1784 container = new Paper;
1785 container.width = width;
1786 container.height = height;
1787 container.canvas = cnvs;
1788 plugins.call(container, container, R.fn);
1792 paperproto.clear = function () {
1793 var c = this.canvas;
1794 while (c.firstChild) {
1795 c.removeChild(c.firstChild);
1797 this.bottom = this.top = null;
1798 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1799 c[appendChild](this.desc);
1800 c[appendChild](this.defs = $("defs"));
1802 paperproto.remove = function () {
1803 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1804 for (var i in this) {
1805 this[i] = removed(i);
1812 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1813 bites = /([clmz]),?([^clmz]*)/gi,
1814 blurregexp = / progid:\S+Blur\([^\)]+\)/g,
1815 val = /-?[^,\s-]+/g,
1816 coordsize = 1e3 + S + 1e3,
1818 pathlike = {path: 1, rect: 1},
1819 path2vml = function (path) {
1820 var total = /[ahqstv]/ig,
1821 command = pathToAbsolute;
1822 Str(path).match(total) && (command = path2curve);
1824 if (command == pathToAbsolute && !Str(path).match(total)) {
1825 var res = Str(path)[rp](bites, function (all, command, args) {
1827 isMove = lowerCase.call(command) == "m",
1829 args[rp](val, function (value) {
1830 if (isMove && vals[length] == 2) {
1831 res += vals + map[command == "m" ? "l" : "L"];
1834 vals[push](round(value * zoom));
1840 var pa = command(path), p, r;
1842 for (var i = 0, ii = pa[length]; i < ii; i++) {
1844 r = lowerCase.call(pa[i][0]);
1845 r == "z" && (r = "x");
1846 for (var j = 1, jj = p[length]; j < jj; j++) {
1847 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1851 return res[join](S);
1854 R[toString] = function () {
1855 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1857 thePath = function (pathString, vml) {
1858 var g = createNode("group");
1859 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1860 g.coordsize = vml.coordsize;
1861 g.coordorigin = vml.coordorigin;
1862 var el = createNode("shape"), ol = el.style;
1863 ol.width = vml.width + "px";
1864 ol.height = vml.height + "px";
1865 el.coordsize = coordsize;
1866 el.coordorigin = vml.coordorigin;
1868 var p = new Element(el, g, vml),
1869 attr = {fill: "none", stroke: "#000"};
1870 pathString && (attr.path = pathString);
1874 setFillAndStroke(p, attr);
1875 vml.canvas[appendChild](g);
1878 setFillAndStroke = function (o, params) {
1879 o.attrs = o.attrs || {};
1884 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1887 for (var par in params) if (params[has](par)) {
1888 a[par] = params[par];
1891 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1897 params.href && (node.href = params.href);
1898 params.title && (node.title = params.title);
1899 params.target && (node.target = params.target);
1900 params.cursor && (s.cursor = params.cursor);
1901 "blur" in params && o.blur(params.blur);
1902 if (params.path && o.type == "path" || newpath) {
1903 node.path = path2vml(a.path);
1905 if (params.rotation != null) {
1906 o.rotate(params.rotation, true);
1908 if (params.translation) {
1909 xy = Str(params.translation)[split](separator);
1910 translate.call(o, xy[0], xy[1]);
1911 if (o._.rt.cx != null) {
1912 o._.rt.cx +=+ xy[0];
1913 o._.rt.cy +=+ xy[1];
1914 o.setBox(o.attrs, xy[0], xy[1]);
1918 xy = Str(params.scale)[split](separator);
1919 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1921 if ("clip-rect" in params) {
1922 var rect = Str(params["clip-rect"])[split](separator);
1923 if (rect[length] == 4) {
1924 rect[2] = +rect[2] + (+rect[0]);
1925 rect[3] = +rect[3] + (+rect[1]);
1926 var div = node.clipRect || doc.createElement("div"),
1928 group = node.parentNode;
1929 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1930 if (!node.clipRect) {
1931 dstyle.position = "absolute";
1934 dstyle.width = o.paper.width + "px";
1935 dstyle.height = o.paper.height + "px";
1936 group.parentNode.insertBefore(div, group);
1937 div[appendChild](group);
1938 node.clipRect = div;
1941 if (!params["clip-rect"]) {
1942 node.clipRect && (node.clipRect.style.clip = E);
1945 if (o.type == "image" && params.src) {
1946 node.src = params.src;
1948 if (o.type == "image" && params.opacity) {
1949 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1950 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1952 params.font && (s.font = params.font);
1953 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1954 params["font-size"] && (s.fontSize = params["font-size"]);
1955 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1956 params["font-style"] && (s.fontStyle = params["font-style"]);
1957 if (params.opacity != null ||
1958 params["stroke-width"] != null ||
1959 params.fill != null ||
1960 params.stroke != null ||
1961 params["stroke-width"] != null ||
1962 params["stroke-opacity"] != null ||
1963 params["fill-opacity"] != null ||
1964 params["stroke-dasharray"] != null ||
1965 params["stroke-miterlimit"] != null ||
1966 params["stroke-linejoin"] != null ||
1967 params["stroke-linecap"] != null) {
1968 node = o.shape || node;
1969 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1971 !fill && (newfill = fill = createNode(fillString));
1972 if ("fill-opacity" in params || "opacity" in params) {
1973 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1974 opacity = mmin(mmax(opacity, 0), 1);
1975 fill.opacity = opacity;
1977 params.fill && (fill.on = true);
1978 if (fill.on == null || params.fill == "none") {
1981 if (fill.on && params.fill) {
1982 var isURL = params.fill.match(ISURL);
1984 fill.src = isURL[1];
1987 fill.color = R.getRGB(params.fill).hex;
1989 fill.type = "solid";
1990 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
1992 a.gradient = params.fill;
1996 newfill && node[appendChild](fill);
1997 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
1999 !stroke && (newstroke = stroke = createNode("stroke"));
2000 if ((params.stroke && params.stroke != "none") ||
2001 params["stroke-width"] ||
2002 params["stroke-opacity"] != null ||
2003 params["stroke-dasharray"] ||
2004 params["stroke-miterlimit"] ||
2005 params["stroke-linejoin"] ||
2006 params["stroke-linecap"]) {
2009 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
2010 var strokeColor = R.getRGB(params.stroke);
2011 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
2012 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2013 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2014 opacity = mmin(mmax(opacity, 0), 1);
2015 params["stroke-width"] == null && (width = a["stroke-width"]);
2016 params["stroke-width"] && (stroke.weight = width);
2017 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2018 stroke.opacity = opacity;
2020 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2021 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2022 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2023 if (params["stroke-dasharray"]) {
2027 "-.": "shortdashdot",
2028 "-..": "shortdashdotdot",
2033 "--.": "longdashdot",
2034 "--..": "longdashdotdot"
2036 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2038 newstroke && node[appendChild](stroke);
2040 if (res.type == "text") {
2041 s = res.paper.span.style;
2042 a.font && (s.font = a.font);
2043 a["font-family"] && (s.fontFamily = a["font-family"]);
2044 a["font-size"] && (s.fontSize = a["font-size"]);
2045 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2046 a["font-style"] && (s.fontStyle = a["font-style"]);
2047 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
2048 res.W = a.w = res.paper.span.offsetWidth;
2049 res.H = a.h = res.paper.span.offsetHeight;
2051 res.Y = a.y + round(res.H / 2);
2053 // text-anchor emulationm
2054 switch (a["text-anchor"]) {
2056 res.node.style["v-text-align"] = "left";
2057 res.bbx = round(res.W / 2);
2060 res.node.style["v-text-align"] = "right";
2061 res.bbx = -round(res.W / 2);
2064 res.node.style["v-text-align"] = "center";
2069 addGradientFill = function (o, gradient) {
2070 o.attrs = o.attrs || {};
2071 var attrs = o.attrs,
2075 o.attrs.gradient = gradient;
2076 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2081 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2086 gradient = gradient[split](/\s*\-\s*/);
2087 if (type == "linear") {
2088 var angle = gradient.shift();
2089 angle = -toFloat(angle);
2094 var dots = parseDots(gradient);
2098 o = o.shape || o.node;
2099 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2100 !fill.parentNode && o.appendChild(fill);
2103 fill.method = "none";
2104 fill.color = dots[0].color;
2105 fill.color2 = dots[dots[length] - 1].color;
2107 for (var i = 0, ii = dots[length]; i < ii; i++) {
2108 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2110 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2111 if (type == "radial") {
2112 fill.type = "gradientradial";
2113 fill.focus = "100%";
2114 fill.focussize = fxfy;
2115 fill.focusposition = fxfy;
2117 fill.type = "gradient";
2118 fill.angle = (270 - angle) % 360;
2123 Element = function (node, group, vml) {
2131 node.raphael = this;
2144 !vml.bottom && (vml.bottom = this);
2145 this.prev = vml.top;
2146 vml.top && (vml.top.next = this);
2150 elproto = Element[proto];
2151 elproto.rotate = function (deg, cx, cy) {
2157 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2159 return this._.rt.deg;
2161 deg = Str(deg)[split](separator);
2162 if (deg[length] - 1) {
2163 cx = toFloat(deg[1]);
2164 cy = toFloat(deg[2]);
2166 deg = toFloat(deg[0]);
2168 this._.rt.deg = deg;
2170 this._.rt.deg += deg;
2172 cy == null && (cx = null);
2175 this.setBox(this.attrs, cx, cy);
2176 this.Group.style.rotation = this._.rt.deg;
2177 // gradient fix for rotation. TODO
2178 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2179 // fill = fill[0] || {};
2180 // var b = ((360 - this._.rt.deg) - 270) % 360;
2181 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2184 elproto.setBox = function (params, cx, cy) {
2188 var gs = this.Group.style,
2189 os = (this.shape && this.shape.style) || this.node.style;
2190 params = params || {};
2191 for (var i in params) if (params[has](i)) {
2192 this.attrs[i] = params[i];
2194 cx = cx || this._.rt.cx;
2195 cy = cy || this._.rt.cy;
2196 var attr = this.attrs,
2201 switch (this.type) {
2203 x = attr.cx - attr.r;
2204 y = attr.cy - attr.r;
2208 x = attr.cx - attr.rx;
2209 y = attr.cy - attr.ry;
2216 w = attr.width || 0;
2217 h = attr.height || 0;
2220 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2221 x = attr.x - round(this.W / 2);
2222 y = attr.y - this.H / 2;
2228 if (!this.attrs.path) {
2231 w = this.paper.width;
2232 h = this.paper.height;
2234 var dim = pathDimensions(this.attrs.path);
2244 w = this.paper.width;
2245 h = this.paper.height;
2248 cx = (cx == null) ? x + w / 2 : cx;
2249 cy = (cy == null) ? y + h / 2 : cy;
2250 var left = cx - this.paper.width / 2,
2251 top = cy - this.paper.height / 2, t;
2252 gs.left != (t = left + "px") && (gs.left = t);
2253 gs.top != (t = top + "px") && (gs.top = t);
2254 this.X = pathlike[has](this.type) ? -left : x;
2255 this.Y = pathlike[has](this.type) ? -top : y;
2258 if (pathlike[has](this.type)) {
2259 os.left != (t = -left * zoom + "px") && (os.left = t);
2260 os.top != (t = -top * zoom + "px") && (os.top = t);
2261 } else if (this.type == "text") {
2262 os.left != (t = -left + "px") && (os.left = t);
2263 os.top != (t = -top + "px") && (os.top = t);
2265 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2266 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2267 os.left != (t = x - left + "px") && (os.left = t);
2268 os.top != (t = y - top + "px") && (os.top = t);
2269 os.width != (t = w + "px") && (os.width = t);
2270 os.height != (t = h + "px") && (os.height = t);
2273 elproto.hide = function () {
2274 !this.removed && (this.Group.style.display = "none");
2277 elproto.show = function () {
2278 !this.removed && (this.Group.style.display = "block");
2281 elproto.getBBox = function () {
2285 if (pathlike[has](this.type)) {
2286 return pathDimensions(this.attrs.path);
2289 x: this.X + (this.bbx || 0),
2295 elproto.remove = function () {
2299 tear(this, this.paper);
2300 this.node.parentNode.removeChild(this.node);
2301 this.Group.parentNode.removeChild(this.Group);
2302 this.shape && this.shape.parentNode.removeChild(this.shape);
2303 for (var i in this) {
2306 this.removed = true;
2308 elproto.attr = function (name, value) {
2314 for (var i in this.attrs) if (this.attrs[has](i)) {
2315 res[i] = this.attrs[i];
2317 this._.rt.deg && (res.rotation = this.rotate());
2318 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2319 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2322 if (value == null && R.is(name, "string")) {
2323 if (name == "translation") {
2324 return translate.call(this);
2326 if (name == "rotation") {
2327 return this.rotate();
2329 if (name == "scale") {
2330 return this.scale();
2332 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2333 return this.attrs.gradient;
2335 return this.attrs[name];
2337 if (this.attrs && value == null && R.is(name, array)) {
2338 var ii, values = {};
2339 for (i = 0, ii = name[length]; i < ii; i++) {
2340 values[name[i]] = this.attr(name[i]);
2345 if (value != null) {
2347 params[name] = value;
2349 value == null && R.is(name, "object") && (params = name);
2351 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2352 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2353 this.attrs[key] = params[key];
2354 for (var subkey in par) if (par[has](subkey)) {
2355 params[subkey] = par[subkey];
2358 if (params.text && this.type == "text") {
2359 this.node.string = params.text;
2361 setFillAndStroke(this, params);
2362 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2363 addGradientFill(this, params.gradient);
2365 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2369 elproto.toFront = function () {
2370 !this.removed && this.Group.parentNode[appendChild](this.Group);
2371 this.paper.top != this && tofront(this, this.paper);
2374 elproto.toBack = function () {
2378 if (this.Group.parentNode.firstChild != this.Group) {
2379 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2380 toback(this, this.paper);
2384 elproto.insertAfter = function (element) {
2388 if (element.constructor == Set) {
2389 element = element[element.length - 1];
2391 if (element.Group.nextSibling) {
2392 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2394 element.Group.parentNode[appendChild](this.Group);
2396 insertafter(this, element, this.paper);
2399 elproto.insertBefore = function (element) {
2403 if (element.constructor == Set) {
2404 element = element[0];
2406 element.Group.parentNode.insertBefore(this.Group, element.Group);
2407 insertbefore(this, element, this.paper);
2410 elproto.blur = function (size) {
2411 var s = this.node.runtimeStyle,
2413 f = f.replace(blurregexp, E);
2415 this.attrs.blur = size;
2416 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2417 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2421 delete this.attrs.blur;
2425 theCircle = function (vml, x, y, r) {
2426 var g = createNode("group"),
2427 o = createNode("oval"),
2429 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2430 g.coordsize = coordsize;
2431 g.coordorigin = vml.coordorigin;
2433 var res = new Element(o, g, vml);
2434 res.type = "circle";
2435 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2439 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2440 vml.canvas[appendChild](g);
2443 function rectPath(x, y, w, h, r) {
2445 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);
2447 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2450 theRect = function (vml, x, y, w, h, r) {
2451 var path = rectPath(x, y, w, h, r),
2452 res = vml.path(path),
2456 res.W = a.width = w;
2457 res.H = a.height = h;
2463 theEllipse = function (vml, x, y, rx, ry) {
2464 var g = createNode("group"),
2465 o = createNode("oval"),
2467 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2468 g.coordsize = coordsize;
2469 g.coordorigin = vml.coordorigin;
2471 var res = new Element(o, g, vml);
2472 res.type = "ellipse";
2473 setFillAndStroke(res, {stroke: "#000"});
2478 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2479 vml.canvas[appendChild](g);
2482 theImage = function (vml, src, x, y, w, h) {
2483 var g = createNode("group"),
2484 o = createNode("image");
2485 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2486 g.coordsize = coordsize;
2487 g.coordorigin = vml.coordorigin;
2490 var res = new Element(o, g, vml);
2492 res.attrs.src = src;
2497 res.setBox({x: x, y: y, width: w, height: h});
2498 vml.canvas[appendChild](g);
2501 theText = function (vml, x, y, text) {
2502 var g = createNode("group"),
2503 el = createNode("shape"),
2505 path = createNode("path"),
2507 o = createNode("textpath");
2508 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2509 g.coordsize = coordsize;
2510 g.coordorigin = vml.coordorigin;
2511 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2512 path.textpathok = true;
2513 ol.width = vml.width;
2514 ol.height = vml.height;
2515 o.string = Str(text);
2518 el[appendChild](path);
2520 var res = new Element(o, g, vml);
2522 res.textpath = path;
2524 res.attrs.text = text;
2529 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2531 vml.canvas[appendChild](g);
2534 setSize = function (width, height) {
2535 var cs = this.canvas.style;
2536 width == +width && (width += "px");
2537 height == +height && (height += "px");
2540 cs.clip = "rect(0 " + width + " " + height + " 0)";
2544 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2546 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2547 createNode = function (tagName) {
2548 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2551 createNode = function (tagName) {
2552 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2555 create = function () {
2556 var con = getContainer[apply](0, arguments),
2557 container = con.container,
2558 height = con.height,
2564 throw new Error("VML container not found.");
2566 var res = new Paper,
2567 c = res.canvas = doc.createElement("div"),
2571 width = width || 512;
2572 height = height || 342;
2573 width == +width && (width += "px");
2574 height == +height && (height += "px");
2577 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2578 res.coordorigin = "0 0";
2579 res.span = doc.createElement("span");
2580 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2581 c[appendChild](res.span);
2582 cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
2583 if (container == 1) {
2584 doc.body[appendChild](c);
2587 cs.position = "absolute";
2589 if (container.firstChild) {
2590 container.insertBefore(c, container.firstChild);
2592 container[appendChild](c);
2595 plugins.call(res, res, R.fn);
2598 paperproto.clear = function () {
2599 this.canvas.innerHTML = E;
2600 this.span = doc.createElement("span");
2601 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2602 this.canvas[appendChild](this.span);
2603 this.bottom = this.top = null;
2605 paperproto.remove = function () {
2606 this.canvas.parentNode.removeChild(this.canvas);
2607 for (var i in this) {
2608 this[i] = removed(i);
2615 // WebKit rendering bug workaround method
2616 var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2617 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2618 paperproto.safari = function () {
2619 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2620 win.setTimeout(function () {rect.remove();});
2623 paperproto.safari = function () {};
2627 var preventDefault = function () {
2628 this.returnValue = false;
2630 preventTouch = function () {
2631 return this.originalEvent.preventDefault();
2633 stopPropagation = function () {
2634 this.cancelBubble = true;
2636 stopTouch = function () {
2637 return this.originalEvent.stopPropagation();
2639 addEvent = (function () {
2640 if (doc.addEventListener) {
2641 return function (obj, type, fn, element) {
2642 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2643 var f = function (e) {
2644 if (supportsTouch && touchMap[has](type)) {
2645 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2646 if (e.targetTouches[i].target == obj) {
2648 e = e.targetTouches[i];
2649 e.originalEvent = olde;
2650 e.preventDefault = preventTouch;
2651 e.stopPropagation = stopTouch;
2656 return fn.call(element, e);
2658 obj.addEventListener(realName, f, false);
2659 return function () {
2660 obj.removeEventListener(realName, f, false);
2664 } else if (doc.attachEvent) {
2665 return function (obj, type, fn, element) {
2666 var f = function (e) {
2668 e.preventDefault = e.preventDefault || preventDefault;
2669 e.stopPropagation = e.stopPropagation || stopPropagation;
2670 return fn.call(element, e);
2672 obj.attachEvent("on" + type, f);
2673 var detacher = function () {
2674 obj.detachEvent("on" + type, f);
2682 dragMove = function (e) {
2685 scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2686 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2691 if (supportsTouch) {
2692 var i = e.touches.length,
2695 touch = e.touches[i];
2696 if (touch.identifier == dragi.el._drag.id) {
2699 (e.originalEvent ? e.originalEvent : e).preventDefault();
2708 dragi.move && dragi.move.call(dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y);
2711 dragUp = function () {
2712 R.unmousemove(dragMove).unmouseup(dragUp);
2713 var i = drag.length,
2717 dragi.el._drag = {};
2718 dragi.end && dragi.end.call(dragi.el);
2722 for (var i = events[length]; i--;) {
2723 (function (eventName) {
2724 R[eventName] = Element[proto][eventName] = function (fn) {
2725 if (R.is(fn, "function")) {
2726 this.events = this.events || [];
2727 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
2731 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2732 var events = this.events,
2734 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2736 events.splice(l, 1);
2737 !events.length && delete this.events;
2744 elproto.hover = function (f_in, f_out) {
2745 return this.mouseover(f_in).mouseout(f_out);
2747 elproto.unhover = function (f_in, f_out) {
2748 return this.unmouseover(f_in).unmouseout(f_out);
2750 elproto.drag = function (onmove, onstart, onend) {
2752 this.mousedown(function (e) {
2753 (e.originalEvent || e).preventDefault();
2754 var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2755 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2756 this._drag.x = e.clientX + scrollX;
2757 this._drag.y = e.clientY + scrollY;
2758 this._drag.id = e.identifier;
2759 onstart && onstart.call(this, e.clientX + scrollX, e.clientY + scrollY);
2760 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2761 drag.push({el: this, move: onmove, end: onend});
2765 elproto.undrag = function (onmove, onstart, onend) {
2766 var i = drag.length;
2768 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i, 1);
2769 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2772 paperproto.circle = function (x, y, r) {
2773 return theCircle(this, x || 0, y || 0, r || 0);
2775 paperproto.rect = function (x, y, w, h, r) {
2776 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2778 paperproto.ellipse = function (x, y, rx, ry) {
2779 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2781 paperproto.path = function (pathString) {
2782 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2783 return thePath(R.format[apply](R, arguments), this);
2785 paperproto.image = function (src, x, y, w, h) {
2786 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2788 paperproto.text = function (x, y, text) {
2789 return theText(this, x || 0, y || 0, Str(text));
2791 paperproto.set = function (itemsArray) {
2792 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2793 return new Set(itemsArray);
2795 paperproto.setSize = setSize;
2796 paperproto.top = paperproto.bottom = null;
2797 paperproto.raphael = R;
2799 return this.x + S + this.y;
2801 elproto.resetScale = function () {
2807 this.attrs.scale = "1 1";
2809 elproto.scale = function (x, y, cx, cy) {
2813 if (x == null && y == null) {
2828 var bb = this.getBBox(),
2829 rcx = bb.x + bb.width / 2,
2830 rcy = bb.y + bb.height / 2,
2833 cx = (+cx || cx == 0) ? cx : rcx;
2834 cy = (+cy || cy == 0) ? cy : rcy;
2835 var dirx = ~~(x / math.abs(x)),
2836 diry = ~~(y / math.abs(y)),
2837 s = this.node.style,
2838 ncx = cx + (rcx - cx) * kx,
2839 ncy = cy + (rcy - cy) * ky;
2840 switch (this.type) {
2843 var neww = a.width * dirx * kx,
2844 newh = a.height * diry * ky;
2847 r: a.r * mmin(dirx * kx, diry * ky),
2856 rx: a.rx * dirx * kx,
2857 ry: a.ry * diry * ky,
2858 r: a.r * mmin(dirx * kx, diry * ky),
2870 var path = pathToRelative(a.path),
2872 for (var i = 0, ii = path[length]; i < ii; i++) {
2874 P0 = upperCase.call(p[0]);
2875 if (P0 == "M" && skip) {
2881 p[path[i][length] - 2] *= kx;
2882 p[path[i][length] - 1] *= ky;
2885 p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
2886 } else if (P0 == "H") {
2887 for (var j = 1, jj = p[length]; j < jj; j++) {
2890 } else if (P0 == "V") {
2891 for (j = 1, jj = p[length]; j < jj; j++) {
2895 for (j = 1, jj = p[length]; j < jj; j++) {
2896 p[j] *= (j % 2) ? kx : ky;
2900 var dim2 = pathDimensions(path);
2901 dx = ncx - dim2.x - dim2.width / 2;
2902 dy = ncy - dim2.y - dim2.height / 2;
2905 this.attr({path: path});
2908 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2909 if (this.transformations) {
2910 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2911 this.node[setAttribute]("transform", this.transformations[join](S));
2912 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2913 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2914 this.attr({x: dx, y: dy});
2918 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2919 ", M12=0, M21=0, M22=", diry,
2920 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2921 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2924 if (this.transformations) {
2925 this.transformations[2] = E;
2926 this.node[setAttribute]("transform", this.transformations[join](S));
2930 this.node.filterMatrix = E;
2931 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2934 a.scale = [x, y, cx, cy][join](S);
2940 elproto.clone = function () {
2944 var attr = this.attr();
2946 delete attr.translation;
2947 return this.paper[this.type]().attr(attr);
2949 var curveslengths = {},
2950 getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2952 name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
2953 cache = curveslengths[name],
2955 !cache && (curveslengths[name] = cache = {data: []});
2956 cache.timer && clearTimeout(cache.timer);
2957 cache.timer = setTimeout(function () {delete curveslengths[name];}, 2000);
2958 for (var i = 0; i < 101; i++) {
2959 if (cache.data[length] > i) {
2960 dot = cache.data[i * 100];
2962 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / 100);
2963 cache.data[i] = dot;
2965 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2966 if (length != null && len >= length) {
2971 if (length == null) {
2975 getLengthFactory = function (istotal, subpath) {
2976 return function (path, length, onlystart) {
2977 path = path2curve(path);
2978 var x, y, p, l, sp = "", subpaths = {}, point,
2980 for (var i = 0, ii = path.length; i < ii; i++) {
2986 l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
2987 if (len + l > length) {
2988 if (subpath && !subpaths.start) {
2989 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
2990 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
2991 if (onlystart) {return sp;}
2992 subpaths.start = sp;
2993 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
2999 if (!istotal && !subpath) {
3000 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3001 return {x: point.x, y: point.y, alpha: point.alpha};
3011 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
3012 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
3016 var getTotalLength = getLengthFactory(1),
3017 getPointAtLength = getLengthFactory(),
3018 getSubpathsAtLength = getLengthFactory(0, 1);
3019 elproto.getTotalLength = function () {
3020 if (this.type != "path") {return;}
3021 if (this.node.getTotalLength) {
3022 return this.node.getTotalLength();
3024 return getTotalLength(this.attrs.path);
3026 elproto.getPointAtLength = function (length) {
3027 if (this.type != "path") {return;}
3028 if (this.node.getPointAtLength) {
3029 return this.node.getPointAtLength(length);
3031 return getPointAtLength(this.attrs.path, length);
3033 elproto.getSubpath = function (from, to) {
3034 if (this.type != "path") {return;}
3035 if (math.abs(this.getTotalLength() - to) < "1e-6") {
3036 return getSubpathsAtLength(this.attrs.path, from).end;
3038 var a = getSubpathsAtLength(this.attrs.path, to, 1);
3039 return from ? getSubpathsAtLength(a, from).end : a;
3042 // animation easing formulas
3043 R.easing_formulas = {
3044 linear: function (n) {
3051 return pow(n - 1, 3) + 1;
3053 "<>": function (n) {
3056 return pow(n, 3) / 2;
3059 return (pow(n, 3) + 2) / 2;
3061 backIn: function (n) {
3063 return n * n * ((s + 1) * n - s);
3065 backOut: function (n) {
3068 return n * n * ((s + 1) * n + s) + 1;
3070 elastic: function (n) {
3071 if (n == 0 || n == 1) {
3076 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
3078 bounce: function (n) {
3087 l = s * n * n + .75;
3089 if (n < (2.5 / p)) {
3091 l = s * n * n + .9375;
3094 l = s * n * n + .984375;
3102 var animationElements = [],
3103 animation = function () {
3104 var Now = +new Date;
3105 for (var l = 0; l < animationElements[length]; l++) {
3106 var e = animationElements[l];
3107 if (e.stop || e.el.removed) {
3110 var time = Now - e.start,
3121 var pos = easing(time / ms);
3122 for (var attr in from) if (from[has](attr)) {
3123 switch (availableAnimAttrs[attr]) {
3125 now = pos * ms * diff[attr];
3126 to.back && (now = to.len - now);
3127 var point = getPointAtLength(to[attr], now);
3128 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3131 that.translate(point.x - diff.sx, point.y - diff.sy);
3132 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3135 now = +from[attr] + pos * ms * diff[attr];
3139 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3140 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3141 upto255(round(from[attr].b + pos * ms * diff[attr].b))
3146 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3147 now[i] = [from[attr][i][0]];
3148 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3149 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3151 now[i] = now[i][join](S);
3158 var x = pos * ms * diff[attr][0] - t.x,
3159 y = pos * ms * diff[attr][1] - t.y;
3165 now = +from[attr][0] + pos * ms * diff[attr][0];
3166 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3169 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);
3175 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3181 var from2 = [].concat(from[attr]);
3183 i = that.paper.customAttributes[attr].length;
3185 now[i] = +from2[i] + pos * ms * diff[attr][i];
3192 that._run && that._run.call(that);
3195 point = getPointAtLength(to.along, to.len * !to.back);
3196 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3197 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3199 (t.x || t.y) && that.translate(-t.x, -t.y);
3200 to.scale && (to.scale += E);
3202 animationElements.splice(l--, 1);
3205 R.svg && that && that.paper && that.paper.safari();
3206 animationElements[length] && setTimeout(animation);
3208 keyframesRun = function (attr, element, time, prev, prevcallback) {
3209 var dif = time - prev;
3210 element.timeouts.push(setTimeout(function () {
3211 R.is(prevcallback, "function") && prevcallback.call(element);
3212 element.animate(attr, dif, attr.easing);
3215 upto255 = function (color) {
3216 return mmax(mmin(color, 255), 0);
3218 translate = function (x, y) {
3220 return {x: this._.tx, y: this._.ty, toString: x_y};
3224 switch (this.type) {
3227 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3232 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3235 var path = pathToRelative(this.attrs.path);
3238 this.attr({path: path});
3243 elproto.animateWith = function (element, params, ms, easing, callback) {
3244 for (var i = 0, ii = animationElements.length; i < ii; i++) {
3245 if (animationElements[i].el.id == element.id) {
3246 params.start = animationElements[i].start;
3249 return this.animate(params, ms, easing, callback);
3251 elproto.animateAlong = along();
3252 elproto.animateAlongBack = along(1);
3253 function along(isBack) {
3254 return function (path, ms, rotate, callback) {
3255 var params = {back: isBack};
3256 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3257 path && path.constructor == Element && (path = path.attrs.path);
3258 path && (params.along = path);
3259 return this.animate(params, ms, callback);
3262 function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
3264 bx = 3 * (p2x - p1x) - cx,
3267 by = 3 * (p2y - p1y) - cy,
3269 function sampleCurveX(t) {
3270 return ((ax * t + bx) * t + cx) * t;
3272 function solve(x, epsilon) {
3273 var t = solveCurveX(x, epsilon);
3274 return ((ay * t + by) * t + cy) * t;
3276 function solveCurveX(x, epsilon) {
3277 var t0, t1, t2, x2, d2, i;
3278 for(t2 = x, i = 0; i < 8; i++) {
3279 x2 = sampleCurveX(t2) - x;
3280 if (math.abs(x2) < epsilon) {
3283 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
3284 if (math.abs(d2) < 1e-6) {
3299 x2 = sampleCurveX(t2);
3300 if (math.abs(x2 - x) < epsilon) {
3308 t2 = (t1 - t0) / 2 + t0;
3312 return solve(t, 1 / (200 * duration));
3314 elproto.onAnimation = function (f) {
3318 elproto.animate = function (params, ms, easing, callback) {
3320 element.timeouts = element.timeouts || [];
3321 if (R.is(easing, "function") || !easing) {
3322 callback = easing || null;
3324 if (element.removed) {
3325 callback && callback.call(element);
3330 animateable = false,
3332 for (var attr in params) if (params[has](attr)) {
3333 if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
3335 from[attr] = element.attr(attr);
3336 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3337 to[attr] = params[attr];
3338 switch (availableAnimAttrs[attr]) {
3340 var len = getTotalLength(params[attr]);
3341 var point = getPointAtLength(params[attr], len * !!params.back);
3342 var bb = element.getBBox();
3343 diff[attr] = len / ms;
3348 to.rot = params.rot;
3349 to.back = params.back;
3351 params.rot && (diff.r = toFloat(element.rotate()) || 0);
3354 diff[attr] = (to[attr] - from[attr]) / ms;
3357 from[attr] = R.getRGB(from[attr]);
3358 var toColour = R.getRGB(to[attr]);
3360 r: (toColour.r - from[attr].r) / ms,
3361 g: (toColour.g - from[attr].g) / ms,
3362 b: (toColour.b - from[attr].b) / ms
3366 var pathes = path2curve(from[attr], to[attr]);
3367 from[attr] = pathes[0];
3368 var toPath = pathes[1];
3370 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3371 diff[attr][i] = [0];
3372 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3373 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3378 var values = Str(params[attr])[split](separator),
3379 from2 = Str(from[attr])[split](separator);
3382 from[attr] = [0, 0];
3383 diff[attr] = [values[0] / ms, values[1] / ms];
3386 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3387 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3390 params[attr] = values;
3391 from[attr] = Str(from[attr])[split](separator);
3392 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3395 from[attr] = Str(from[attr])[split](separator);
3399 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3406 values = [].concat(params[attr]);
3407 from2 = [].concat(from[attr]);
3409 i = element.paper.customAttributes[attr][length];
3411 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3420 for (var key in params) if (params[has](key) && animKeyFrames.test(key)) {
3421 attr = {value: params[key]};
3422 key == "from" && (key = 0);
3423 key == "to" && (key = 100);
3424 attr.key = toInt(key, 10);
3427 attrs.sort(sortByKey);
3429 attrs.unshift({key: 0, value: element.attrs});
3431 for (i = 0, ii = attrs[length]; i < ii; i++) {
3432 keyframesRun(attrs[i].value, element, ms / 100 * attrs[i].key, ms / 100 * (attrs[i - 1] && attrs[i - 1].key || 0), attrs[i - 1] && attrs[i - 1].value.callback);
3434 lastcall = attrs[attrs[length] - 1].value.callback;
3436 element.timeouts.push(setTimeout(function () {lastcall.call(element);}, ms));
3439 var easyeasy = R.easing_formulas[easing];
3441 easyeasy = Str(easing).match(bezierrg);
3442 if (easyeasy && easyeasy[length] == 5) {
3443 var curve = easyeasy;
3444 easyeasy = function (t) {
3445 return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
3448 easyeasy = function (t) {
3453 animationElements.push({
3454 start: params.start || +new Date,
3463 R.is(callback, "function") && (element._ac = setTimeout(function () {
3464 callback.call(element);
3466 animationElements[length] == 1 && setTimeout(animation);
3470 elproto.stop = function () {
3471 for (var i = 0; i < animationElements.length; i++) {
3472 animationElements[i].el.id == this.id && animationElements.splice(i--, 1);
3474 for (i = 0, ii = this.timeouts && this.timeouts.length; i < ii; i++) {
3475 clearTimeout(this.timeouts[i]);
3478 clearTimeout(this._ac);
3482 elproto.translate = function (x, y) {
3483 return this.attr({translation: x + " " + y});
3485 elproto[toString] = function () {
3486 return "Rapha\xebl\u2019s object";
3488 R.ae = animationElements;
3491 var Set = function (items) {
3496 for (var i = 0, ii = items[length]; i < ii; i++) {
3497 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3498 this[this.items[length]] = this.items[this.items[length]] = items[i];
3504 Set[proto][push] = function () {
3507 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3508 item = arguments[i];
3509 if (item && (item.constructor == Element || item.constructor == Set)) {
3510 len = this.items[length];
3511 this[len] = this.items[len] = item;
3517 Set[proto].pop = function () {
3518 delete this[this[length]--];
3519 return this.items.pop();
3521 for (var method in elproto) if (elproto[has](method)) {
3522 Set[proto][method] = (function (methodname) {
3523 return function () {
3524 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3525 this.items[i][methodname][apply](this.items[i], arguments);
3531 Set[proto].attr = function (name, value) {
3532 if (name && R.is(name, array) && R.is(name[0], "object")) {
3533 for (var j = 0, jj = name[length]; j < jj; j++) {
3534 this.items[j].attr(name[j]);
3537 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3538 this.items[i].attr(name, value);
3543 Set[proto].animate = function (params, ms, easing, callback) {
3544 (R.is(easing, "function") || !easing) && (callback = easing || null);
3545 var len = this.items[length],
3550 callback && (collector = function () {
3551 !--len && callback.call(set);
3553 easing = R.is(easing, string) ? easing : collector;
3554 item = this.items[--i].animate(params, ms, easing, collector);
3556 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3560 Set[proto].insertAfter = function (el) {
3561 var i = this.items[length];
3563 this.items[i].insertAfter(el);
3567 Set[proto].getBBox = function () {
3572 for (var i = this.items[length]; i--;) {
3573 var box = this.items[i].getBBox();
3576 w[push](box.x + box.width);
3577 h[push](box.y + box.height);
3579 x = mmin[apply](0, x);
3580 y = mmin[apply](0, y);
3584 width: mmax[apply](0, w) - x,
3585 height: mmax[apply](0, h) - y
3588 Set[proto].clone = function (s) {
3590 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3591 s[push](this.items[i].clone());
3596 R.registerFont = function (font) {
3600 this.fonts = this.fonts || {};
3606 family = font.face["font-family"];
3607 for (var prop in font.face) if (font.face[has](prop)) {
3608 fontcopy.face[prop] = font.face[prop];
3610 if (this.fonts[family]) {
3611 this.fonts[family][push](fontcopy);
3613 this.fonts[family] = [fontcopy];
3616 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3617 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3618 var path = font.glyphs[glyph];
3619 fontcopy.glyphs[glyph] = {
3622 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3623 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3627 for (var k in path.k) if (path[has](k)) {
3628 fontcopy.glyphs[glyph].k[k] = path.k[k];
3635 paperproto.getFont = function (family, weight, style, stretch) {
3636 stretch = stretch || "normal";
3637 style = style || "normal";
3638 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3642 var font = R.fonts[family];
3644 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3645 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3646 if (name.test(fontName)) {
3647 font = R.fonts[fontName];
3654 for (var i = 0, ii = font[length]; i < ii; i++) {
3656 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3663 paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
3664 origin = origin || "middle"; // baseline|middle
3665 letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
3666 var out = this.set(),
3667 letters = Str(string)[split](E),
3671 R.is(font, string) && (font = this.getFont(font));
3673 scale = (size || 16) / font.face["units-per-em"];
3674 var bb = font.face.bbox.split(separator),
3676 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3677 for (var i = 0, ii = letters[length]; i < ii; i++) {
3678 var prev = i && font.glyphs[letters[i - 1]] || {},
3679 curr = font.glyphs[letters[i]];
3680 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
3681 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3683 out.scale(scale, scale, top, height).translate(x - top, y - height);
3688 R.format = function (token, params) {
3689 var args = R.is(params, array) ? [0][concat](params) : arguments;
3690 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3691 return args[++i] == null ? E : args[i];
3695 R.ninja = function () {
3696 oldRaphael.was ? (win.Raphael = oldRaphael.is) : delete Raphael;
3702 oldRaphael.was ? (win.Raphael = R) : (Raphael = R);