2 * Raphael 1.5.1 - 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,
63 toString = "toString",
65 objectToString = Object[proto][toString],
68 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
69 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+%?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsba?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsla?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\))\s*$/i,
70 isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
71 bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
73 setAttribute = "setAttribute",
76 ms = " progid:DXImageTransform.Microsoft",
77 upperCase = Str[proto].toUpperCase,
78 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},
79 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},
81 animKeyFrames= /^(from|to|\d+%?)$/,
82 commaSpaces = /\s*,\s*/,
83 hsrg = {hs: 1, rg: 1},
84 p2s = /,?([achlmqrstvxz]),?/gi,
85 pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
86 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
87 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/,
88 sortByKey = function (a, b) {
92 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
93 if (R.type == "VML") {
94 var d = doc.createElement("div"),
96 d.innerHTML = '<v:shape adj="1"/>';
98 b.style.behavior = "url(#default#VML)";
99 if (!(b && typeof b.adj == "object")) {
100 return R.type = null;
104 R.svg = !(R.vml = R.type == "VML");
105 Paper[proto] = R[proto];
106 paperproto = Paper[proto];
110 R.is = function (o, type) {
111 type = lowerCase.call(type);
112 if (type == "finite") {
113 return !isnan[has](+o);
115 return (type == "null" && o === null) ||
116 (type == typeof o) ||
117 (type == "object" && o === Object(o)) ||
118 (type == "array" && Array.isArray && Array.isArray(o)) ||
119 objectToString.call(o).slice(8, -1).toLowerCase() == type;
121 R.angle = function (x1, y1, x2, y2, x3, y3) {
128 return ((x < 0) * 180 + math.atan(-y / -x) * 180 / PI + 360) % 360;
130 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
133 R.rad = function (deg) {
134 return deg % 360 * PI / 180;
136 R.deg = function (rad) {
137 return rad * 180 / PI % 360;
139 R.snapTo = function (values, value, tolerance) {
140 tolerance = R.is(tolerance, "finite") ? tolerance : 10;
141 if (R.is(values, array)) {
142 var i = values.length;
143 while (i--) if (abs(values[i] - value) <= tolerance) {
148 var rem = value % values;
149 if (rem < tolerance) {
152 if (rem > values - tolerance) {
153 return value - rem + values;
158 function createUUID() {
159 // http://www.ietf.org/rfc/rfc4122.txt
162 for (; i < 32; i++) {
163 s[i] = (~~(math.random() * 16))[toString](16);
165 s[12] = 4; // bits 12-15 of the time_hi_and_version field to 0010
166 s[16] = ((s[16] & 3) | 8)[toString](16); // bits 6-7 of the clock_seq_hi_and_reserved to 01
167 return "r-" + s[join]("");
170 R.setWindow = function (newwin) {
175 var toHex = function (color) {
177 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
178 var trim = /^\s+|\s+$/g;
181 var docum = new ActiveXObject("htmlfile");
182 docum.write("<body>");
186 bod = createPopup().document.body;
188 var range = bod.createTextRange();
189 toHex = cacher(function (color) {
191 bod.style.color = Str(color)[rp](trim, E);
192 var value = range.queryCommandValue("ForeColor");
193 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
194 return "#" + ("000000" + value[toString](16)).slice(-6);
200 var i = doc.createElement("i");
201 i.title = "Rapha\xebl Colour Picker";
202 i.style.display = "none";
203 doc.body[appendChild](i);
204 toHex = cacher(function (color) {
205 i.style.color = color;
206 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
211 hsbtoString = function () {
212 return "hsb(" + [this.h, this.s, this.b] + ")";
214 hsltoString = function () {
215 return "hsl(" + [this.h, this.s, this.l] + ")";
217 rgbtoString = function () {
220 R.hsb2rgb = function (h, s, b, o) {
221 if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
227 return R.hsl2rgb(h, s, b / 2, o);
229 R.hsl2rgb = function (h, s, l, o) {
230 if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
235 if (h > 1 || s > 1 || l > 1) {
241 channels = ["r", "g", "b"],
256 for (var i = 0; i < 3; i++) {
257 t3 = h + 1 / 3 * -(i - 1);
261 rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
262 } else if (t3 * 2 < 1) {
263 rgb[channels[i]] = t2;
264 } else if (t3 * 3 < 2) {
265 rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
267 rgb[channels[i]] = t1;
274 rgb.hex = "#" + (16777216 | rgb.b | (rgb.g << 8) | (rgb.r << 16)).toString(16).slice(1);
275 R.is(o, "finite") && (rgb.opacity = o);
276 rgb.toString = rgbtoString;
279 R.rgb2hsb = function (red, green, blue) {
280 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
285 if (green == null && R.is(red, string)) {
286 var clr = R.getRGB(red);
291 if (red > 1 || green > 1 || blue > 1) {
296 var max = mmax(red, green, blue),
297 min = mmin(red, green, blue),
302 return {h: 0, s: 0, b: max, toString: hsbtoString};
304 var delta = (max - min);
305 saturation = delta / max;
307 hue = (green - blue) / delta;
308 } else if (green == max) {
309 hue = 2 + ((blue - red) / delta);
311 hue = 4 + ((red - green) / delta);
317 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
319 R.rgb2hsl = function (red, green, blue) {
320 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
325 if (green == null && R.is(red, string)) {
326 var clr = R.getRGB(red);
331 if (red > 1 || green > 1 || blue > 1) {
336 var max = mmax(red, green, blue),
337 min = mmin(red, green, blue),
343 hsl = {h: 0, s: 0, l: l};
345 var delta = max - min;
346 s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
348 h = (green - blue) / delta;
349 } else if (green == max) {
350 h = 2 + (blue - red) / delta;
352 h = 4 + (red - green) / delta;
357 hsl = {h: h, s: s, l: l};
359 hsl.toString = hsltoString;
362 R._path2string = function () {
363 return this.join(",")[rp](p2s, "$1");
365 function cacher(f, scope, postprocessor) {
367 var arg = Array[proto].slice.call(arguments, 0),
368 args = arg[join]("\u25ba"),
369 cache = newf.cache = newf.cache || {},
370 count = newf.count = newf.count || [];
371 if (cache[has](args)) {
372 return postprocessor ? postprocessor(cache[args]) : cache[args];
374 count[length] >= 1e3 && delete cache[count.shift()];
376 cache[args] = f[apply](scope, arg);
377 return postprocessor ? postprocessor(cache[args]) : cache[args];
382 R.getRGB = cacher(function (colour) {
383 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
384 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
386 if (colour == "none") {
387 return {r: -1, g: -1, b: -1, hex: "none"};
389 !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
397 rgb = colour.match(colourRegExp);
400 blue = toInt(rgb[2].substring(5), 16);
401 green = toInt(rgb[2].substring(3, 5), 16);
402 red = toInt(rgb[2].substring(1, 3), 16);
405 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
406 green = toInt((t = rgb[3].charAt(2)) + t, 16);
407 red = toInt((t = rgb[3].charAt(1)) + t, 16);
410 values = rgb[4][split](commaSpaces);
411 red = toFloat(values[0]);
412 values[0].slice(-1) == "%" && (red *= 2.55);
413 green = toFloat(values[1]);
414 values[1].slice(-1) == "%" && (green *= 2.55);
415 blue = toFloat(values[2]);
416 values[2].slice(-1) == "%" && (blue *= 2.55);
417 rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
418 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
421 values = rgb[5][split](commaSpaces);
422 red = toFloat(values[0]);
423 values[0].slice(-1) == "%" && (red *= 2.55);
424 green = toFloat(values[1]);
425 values[1].slice(-1) == "%" && (green *= 2.55);
426 blue = toFloat(values[2]);
427 values[2].slice(-1) == "%" && (blue *= 2.55);
428 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
429 rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
430 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
431 return R.hsb2rgb(red, green, blue, opacity);
434 values = rgb[6][split](commaSpaces);
435 red = toFloat(values[0]);
436 values[0].slice(-1) == "%" && (red *= 2.55);
437 green = toFloat(values[1]);
438 values[1].slice(-1) == "%" && (green *= 2.55);
439 blue = toFloat(values[2]);
440 values[2].slice(-1) == "%" && (blue *= 2.55);
441 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
442 rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
443 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
444 return R.hsl2rgb(red, green, blue, opacity);
446 rgb = {r: red, g: green, b: blue};
447 rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
448 R.is(opacity, "finite") && (rgb.opacity = opacity);
451 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
453 R.getColor = function (value) {
454 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
455 rgb = this.hsb2rgb(start.h, start.s, start.b);
460 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
464 R.getColor.reset = function () {
468 R.parsePathString = cacher(function (pathString) {
472 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
474 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
475 data = pathClone(pathString);
478 Str(pathString)[rp](pathCommand, function (a, b, c) {
480 name = lowerCase.call(b);
481 c[rp](pathValues, function (a, b) {
482 b && params[push](+b);
484 if (name == "m" && params[length] > 2) {
485 data[push]([b][concat](params.splice(0, 2)));
487 b = b == "m" ? "l" : "L";
489 while (params[length] >= paramCounts[name]) {
490 data[push]([b][concat](params.splice(0, paramCounts[name])));
491 if (!paramCounts[name]) {
497 data[toString] = R._path2string;
500 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
502 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
503 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
504 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
505 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
506 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
507 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
508 ax = (1 - t) * p1x + t * c1x,
509 ay = (1 - t) * p1y + t * c1y,
510 cx = (1 - t) * c2x + t * p2x,
511 cy = (1 - t) * c2y + t * p2y,
512 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / PI);
513 (mx > nx || my < ny) && (alpha += 180);
514 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};
516 var pathDimensions = cacher(function (path) {
518 return {x: 0, y: 0, width: 0, height: 0};
520 path = path2curve(path);
526 for (var i = 0, ii = path[length]; i < ii; i++) {
534 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
535 X = X[concat](dim.min.x, dim.max.x);
536 Y = Y[concat](dim.min.y, dim.max.y);
541 var xmin = mmin[apply](0, X),
542 ymin = mmin[apply](0, Y);
546 width: mmax[apply](0, X) - xmin,
547 height: mmax[apply](0, Y) - ymin
550 pathClone = function (pathArray) {
552 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
553 pathArray = R.parsePathString(pathArray);
555 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
557 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
558 res[i][j] = pathArray[i][j];
561 res[toString] = R._path2string;
564 pathToRelative = cacher(function (pathArray) {
565 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
566 pathArray = R.parsePathString(pathArray);
574 if (pathArray[0][0] == "M") {
580 res[push](["M", x, y]);
582 for (var i = start, ii = pathArray[length]; i < ii; i++) {
585 if (pa[0] != lowerCase.call(pa[0])) {
586 r[0] = lowerCase.call(pa[0]);
594 r[6] = +(pa[6] - x).toFixed(3);
595 r[7] = +(pa[7] - y).toFixed(3);
598 r[1] = +(pa[1] - y).toFixed(3);
604 for (var j = 1, jj = pa[length]; j < jj; j++) {
605 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
614 for (var k = 0, kk = pa[length]; k < kk; k++) {
618 var len = res[i][length];
625 x += +res[i][len - 1];
628 y += +res[i][len - 1];
631 x += +res[i][len - 2];
632 y += +res[i][len - 1];
635 res[toString] = R._path2string;
638 pathToAbsolute = cacher(function (pathArray) {
639 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
640 pathArray = R.parsePathString(pathArray);
648 if (pathArray[0][0] == "M") {
649 x = +pathArray[0][1];
650 y = +pathArray[0][2];
654 res[0] = ["M", x, y];
656 for (var i = start, ii = pathArray[length]; i < ii; i++) {
659 if (pa[0] != upperCase.call(pa[0])) {
660 r[0] = upperCase.call(pa[0]);
681 for (var j = 1, jj = pa[length]; j < jj; j++) {
682 r[j] = +pa[j] + ((j % 2) ? x : y);
686 for (var k = 0, kk = pa[length]; k < kk; k++) {
702 mx = res[i][res[i][length] - 2];
703 my = res[i][res[i][length] - 1];
705 x = res[i][res[i][length] - 2];
706 y = res[i][res[i][length] - 1];
709 res[toString] = R._path2string;
712 l2c = function (x1, y1, x2, y2) {
713 return [x1, y1, x2, y2, x2, y2];
715 q2c = function (x1, y1, ax, ay, x2, y2) {
727 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
728 // for more information of where this math came from visit:
729 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
730 var _120 = PI * 120 / 180,
731 rad = PI / 180 * (+angle || 0),
734 rotate = cacher(function (x, y, rad) {
735 var X = x * math.cos(rad) - y * math.sin(rad),
736 Y = x * math.sin(rad) + y * math.cos(rad);
740 xy = rotate(x1, y1, -rad);
743 xy = rotate(x2, y2, -rad);
746 var cos = math.cos(PI / 180 * angle),
747 sin = math.sin(PI / 180 * angle),
750 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
758 k = (large_arc_flag == sweep_flag ? -1 : 1) *
759 math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
760 cx = k * rx * y / ry + (x1 + x2) / 2,
761 cy = k * -ry * x / rx + (y1 + y2) / 2,
762 f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
763 f2 = math.asin(((y2 - cy) / ry).toFixed(9));
765 f1 = x1 < cx ? PI - f1 : f1;
766 f2 = x2 < cx ? PI - f2 : f2;
767 f1 < 0 && (f1 = PI * 2 + f1);
768 f2 < 0 && (f2 = PI * 2 + f2);
769 if (sweep_flag && f1 > f2) {
772 if (!sweep_flag && f2 > f1) {
782 if (abs(df) > _120) {
786 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
787 x2 = cx + rx * math.cos(f2);
788 y2 = cy + ry * math.sin(f2);
789 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
792 var c1 = math.cos(f1),
796 t = math.tan(df / 4),
800 m2 = [x1 + hx * s1, y1 - hy * c1],
801 m3 = [x2 + hx * s2, y2 - hy * c2],
803 m2[0] = 2 * m1[0] - m2[0];
804 m2[1] = 2 * m1[1] - m2[1];
806 return [m2, m3, m4][concat](res);
808 res = [m2, m3, m4][concat](res)[join]()[split](",");
810 for (var i = 0, ii = res[length]; i < ii; i++) {
811 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
816 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
819 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
820 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
823 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
824 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
825 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
827 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
828 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
832 abs(t1) > "1e12" && (t1 = .5);
833 abs(t2) > "1e12" && (t2 = .5);
834 if (t1 > 0 && t1 < 1) {
835 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
839 if (t2 > 0 && t2 < 1) {
840 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
844 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
845 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
847 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
848 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
849 abs(t1) > "1e12" && (t1 = .5);
850 abs(t2) > "1e12" && (t2 = .5);
851 if (t1 > 0 && t1 < 1) {
852 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
856 if (t2 > 0 && t2 < 1) {
857 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
862 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
863 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
866 path2curve = cacher(function (path, path2) {
867 var p = pathToAbsolute(path),
868 p2 = path2 && pathToAbsolute(path2),
869 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
870 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
871 processPath = function (path, d) {
874 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
876 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
883 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
886 nx = d.x + (d.x - (d.bx || d.x));
887 ny = d.y + (d.y - (d.by || d.y));
888 path = ["C", nx, ny][concat](path.slice(1));
891 d.qx = d.x + (d.x - (d.qx || d.x));
892 d.qy = d.y + (d.y - (d.qy || d.y));
893 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
898 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
901 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
904 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
907 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
910 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
915 fixArc = function (pp, i) {
916 if (pp[i][length] > 7) {
920 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
923 ii = mmax(p[length], p2 && p2[length] || 0);
926 fixM = function (path1, path2, a1, a2, i) {
927 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
928 path2.splice(i, 0, ["M", a2.x, a2.y]);
933 ii = mmax(p[length], p2 && p2[length] || 0);
936 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
937 p[i] = processPath(p[i], attrs);
939 p2 && (p2[i] = processPath(p2[i], attrs2));
941 fixM(p, p2, attrs, attrs2, i);
942 fixM(p2, p, attrs2, attrs, i);
945 seglen = seg[length],
946 seg2len = p2 && seg2[length];
947 attrs.x = seg[seglen - 2];
948 attrs.y = seg[seglen - 1];
949 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
950 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
951 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
952 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
953 attrs2.x = p2 && seg2[seg2len - 2];
954 attrs2.y = p2 && seg2[seg2len - 1];
956 return p2 ? [p, p2] : p;
958 parseDots = cacher(function (gradient) {
960 for (var i = 0, ii = gradient[length]; i < ii; i++) {
962 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
963 dot.color = R.getRGB(par[1]);
964 if (dot.color.error) {
967 dot.color = dot.color.hex;
968 par[2] && (dot.offset = par[2] + "%");
971 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
972 if (!dots[i].offset) {
973 var start = toFloat(dots[i - 1].offset || 0),
975 for (var j = i + 1; j < ii; j++) {
976 if (dots[j].offset) {
977 end = dots[j].offset;
986 var d = (end - start) / (j - i + 1);
989 dots[i].offset = start + "%";
995 getContainer = function (x, y, w, h) {
997 if (R.is(x, string) || R.is(x, "object")) {
998 container = R.is(x, string) ? doc.getElementById(x) : x;
999 if (container.tagName) {
1002 container: container,
1003 width: container.style.pixelWidth || container.offsetWidth,
1004 height: container.style.pixelHeight || container.offsetHeight
1007 return {container: container, width: y, height: w};
1011 return {container: 1, x: x, y: y, width: w, height: h};
1014 plugins = function (con, add) {
1016 for (var prop in add) {
1017 if (add[has](prop) && !(prop in con)) {
1018 switch (typeof add[prop]) {
1021 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1025 con[prop] = con[prop] || {};
1026 plugins.call(this, con[prop], add[prop]);
1029 con[prop] = add[prop];
1035 tear = function (el, paper) {
1036 el == paper.top && (paper.top = el.prev);
1037 el == paper.bottom && (paper.bottom = el.next);
1038 el.next && (el.next.prev = el.prev);
1039 el.prev && (el.prev.next = el.next);
1041 tofront = function (el, paper) {
1042 if (paper.top === el) {
1047 el.prev = paper.top;
1048 paper.top.next = el;
1051 toback = function (el, paper) {
1052 if (paper.bottom === el) {
1056 el.next = paper.bottom;
1058 paper.bottom.prev = el;
1061 insertafter = function (el, el2, paper) {
1063 el2 == paper.top && (paper.top = el);
1064 el2.next && (el2.next.prev = el);
1069 insertbefore = function (el, el2, paper) {
1071 el2 == paper.bottom && (paper.bottom = el);
1072 el2.prev && (el2.prev.next = el);
1077 removed = function (methodname) {
1078 return function () {
1079 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1082 R.pathToRelative = pathToRelative;
1085 paperproto.svgns = "http://www.w3.org/2000/svg";
1086 paperproto.xlink = "http://www.w3.org/1999/xlink";
1087 round = function (num) {
1088 return +num + (~~num === num) * .5;
1090 var $ = function (el, attr) {
1092 for (var key in attr) {
1093 if (attr[has](key)) {
1094 el[setAttribute](key, Str(attr[key]));
1098 el = doc.createElementNS(paperproto.svgns, el);
1099 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1103 R[toString] = function () {
1104 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1106 var thePath = function (pathString, SVG) {
1108 SVG.canvas && SVG.canvas[appendChild](el);
1109 var p = new Element(el, SVG);
1111 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1114 var addGradientFill = function (o, gradient, SVG) {
1115 var type = "linear",
1118 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1123 var dir = ((fy > .5) * 2 - 1);
1124 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1125 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1127 (fy = fy.toFixed(5) - 1e-5 * dir);
1131 gradient = gradient[split](/\s*\-\s*/);
1132 if (type == "linear") {
1133 var angle = gradient.shift();
1134 angle = -toFloat(angle);
1138 var vector = [0, 0, math.cos(angle * PI / 180), math.sin(angle * PI / 180)],
1139 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
1142 if (vector[2] < 0) {
1143 vector[0] = -vector[2];
1146 if (vector[3] < 0) {
1147 vector[1] = -vector[3];
1151 var dots = parseDots(gradient);
1155 var id = o.getAttribute(fillString);
1156 id = id.match(/^url\(#(.*)\)$/);
1157 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1159 var el = $(type + "Gradient");
1160 el.id = createUUID();
1161 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1162 SVG.defs[appendChild](el);
1163 for (var i = 0, ii = dots[length]; i < ii; i++) {
1164 var stop = $("stop");
1166 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1167 "stop-color": dots[i].color || "#fff"
1169 el[appendChild](stop);
1172 fill: "url(#" + el.id + ")",
1181 var updatePosition = function (o) {
1182 var bbox = o.getBBox();
1183 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1185 var setFillAndStroke = function (o, params) {
1192 "-..": [3, 1, 1, 1, 1, 1],
1196 "- .": [4, 3, 1, 3],
1197 "--.": [8, 3, 1, 3],
1198 "--..": [8, 3, 1, 3, 1, 3]
1203 addDashes = function (o, value) {
1204 value = dasharray[lowerCase.call(value)];
1206 var width = o.attrs["stroke-width"] || "1",
1207 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1209 var i = value[length];
1211 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1213 $(node, {"stroke-dasharray": dashes[join](",")});
1216 params[has]("rotation") && (rot = params.rotation);
1217 var rotxy = Str(rot)[split](separator);
1218 if (!(rotxy.length - 1)) {
1221 rotxy[1] = +rotxy[1];
1222 rotxy[2] = +rotxy[2];
1224 toFloat(rot) && o.rotate(0, true);
1225 for (var att in params) {
1226 if (params[has](att)) {
1227 if (!availableAttrs[has](att)) {
1230 var value = params[att];
1237 o.rotate(value, true);
1242 var pn = node.parentNode;
1243 if (lowerCase.call(pn.tagName) != "a") {
1245 pn.insertBefore(hl, node);
1246 hl[appendChild](node);
1249 if (att == "target" && value == "blank") {
1250 pn.setAttributeNS(o.paper.xlink, "show", "new");
1252 pn.setAttributeNS(o.paper.xlink, att, value);
1256 node.style.cursor = value;
1259 var rect = Str(value)[split](separator);
1260 if (rect[length] == 4) {
1261 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1262 var el = $("clipPath"),
1264 el.id = createUUID();
1271 el[appendChild](rc);
1272 o.paper.defs[appendChild](el);
1273 $(node, {"clip-path": "url(#" + el.id + ")"});
1277 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1278 clip && clip.parentNode.removeChild(clip);
1279 $(node, {"clip-path": E});
1284 if (o.type == "path") {
1285 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1289 node[setAttribute](att, value);
1298 value = -attrs.x - (attrs.width || 0);
1301 if (att == "rx" && o.type == "rect") {
1305 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1306 node[setAttribute](att, value);
1307 o.pattern && updatePosition(o);
1310 node[setAttribute](att, value);
1319 value = -attrs.y - (attrs.height || 0);
1322 if (att == "ry" && o.type == "rect") {
1326 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1327 node[setAttribute](att, value);
1328 o.pattern && updatePosition(o);
1331 if (o.type == "rect") {
1332 $(node, {rx: value, ry: value});
1334 node[setAttribute](att, value);
1338 if (o.type == "image") {
1339 node.setAttributeNS(o.paper.xlink, "href", value);
1342 case "stroke-width":
1343 node.style.strokeWidth = value;
1344 // Need following line for Firefox
1345 node[setAttribute](att, value);
1346 if (attrs["stroke-dasharray"]) {
1347 addDashes(o, attrs["stroke-dasharray"]);
1350 case "stroke-dasharray":
1351 addDashes(o, value);
1354 var xy = Str(value)[split](separator);
1355 xy[0] = +xy[0] || 0;
1356 xy[1] = +xy[1] || 0;
1361 translate.call(o, xy[0], xy[1]);
1364 xy = Str(value)[split](separator);
1365 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1368 var isURL = Str(value).match(ISURL);
1371 var ig = $("image");
1372 el.id = createUUID();
1373 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1374 $(ig, {x: 0, y: 0});
1375 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1376 el[appendChild](ig);
1378 var img = doc.createElement("img");
1379 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1380 img.onload = function () {
1381 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1382 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1383 doc.body.removeChild(this);
1386 doc.body[appendChild](img);
1388 o.paper.defs[appendChild](el);
1389 node.style.fill = "url(#" + el.id + ")";
1390 $(node, {fill: "url(#" + el.id + ")"});
1392 o.pattern && updatePosition(o);
1395 var clr = R.getRGB(value);
1397 delete params.gradient;
1398 delete attrs.gradient;
1399 !R.is(attrs.opacity, "undefined") &&
1400 R.is(params.opacity, "undefined") &&
1401 $(node, {opacity: attrs.opacity});
1402 !R.is(attrs["fill-opacity"], "undefined") &&
1403 R.is(params["fill-opacity"], "undefined") &&
1404 $(node, {"fill-opacity": attrs["fill-opacity"]});
1405 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1406 attrs.gradient = value;
1407 attrs.fill = "none";
1410 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1412 clr = R.getRGB(value);
1413 node[setAttribute](att, clr.hex);
1414 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
1417 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1420 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
1421 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
1424 case "fill-opacity":
1425 if (attrs.gradient) {
1426 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1428 var stops = gradient.getElementsByTagName("stop");
1429 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1434 att == "font-size" && (value = toInt(value, 10) + "px");
1435 var cssrule = att[rp](/(\-.)/g, function (w) {
1436 return upperCase.call(w.substring(1));
1438 node.style[cssrule] = value;
1439 // Need following line for Firefox
1440 node[setAttribute](att, value);
1446 tuneText(o, params);
1448 o.rotate(rotxy.join(S));
1450 toFloat(rot) && o.rotate(rot, true);
1454 tuneText = function (el, params) {
1455 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1460 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1462 if (params[has]("text")) {
1463 a.text = params.text;
1464 while (node.firstChild) {
1465 node.removeChild(node.firstChild);
1467 var texts = Str(params.text)[split]("\n");
1468 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1469 var tspan = $("tspan");
1470 i && $(tspan, {dy: fontSize * leading, x: a.x});
1471 tspan[appendChild](doc.createTextNode(texts[i]));
1472 node[appendChild](tspan);
1475 texts = node.getElementsByTagName("tspan");
1476 for (i = 0, ii = texts[length]; i < ii; i++) {
1477 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1481 var bb = el.getBBox(),
1482 dif = a.y - (bb.y + bb.height / 2);
1483 dif && R.is(dif, "finite") && $(node, {y: a.y + dif});
1485 Element = function (node, svg) {
1491 node.raphael = this;
1493 this.attrs = this.attrs || {};
1494 this.transformations = []; // rotate, translate, scale
1498 rt: {deg: 0, cx: 0, cy: 0},
1502 !svg.bottom && (svg.bottom = this);
1503 this.prev = svg.top;
1504 svg.top && (svg.top.next = this);
1508 var elproto = Element[proto];
1509 Element[proto].rotate = function (deg, cx, cy) {
1515 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1517 return this._.rt.deg;
1519 var bbox = this.getBBox();
1520 deg = Str(deg)[split](separator);
1521 if (deg[length] - 1) {
1522 cx = toFloat(deg[1]);
1523 cy = toFloat(deg[2]);
1525 deg = toFloat(deg[0]);
1526 if (cx != null && cx !== false) {
1527 this._.rt.deg = deg;
1529 this._.rt.deg += deg;
1531 (cy == null) && (cx = null);
1534 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1535 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1536 if (this._.rt.deg) {
1537 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1538 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1540 this.transformations[0] = E;
1541 this.clip && $(this.clip, {transform: E});
1543 $(this.node, {transform: this.transformations[join](S)});
1546 Element[proto].hide = function () {
1547 !this.removed && (this.node.style.display = "none");
1550 Element[proto].show = function () {
1551 !this.removed && (this.node.style.display = "");
1554 Element[proto].remove = function () {
1558 tear(this, this.paper);
1559 this.node.parentNode.removeChild(this.node);
1560 for (var i in this) {
1563 this.removed = true;
1565 Element[proto].getBBox = function () {
1569 if (this.type == "path") {
1570 return pathDimensions(this.attrs.path);
1572 if (this.node.style.display == "none") {
1578 bbox = this.node.getBBox();
1580 // Firefox 3.0.x plays badly here
1584 if (this.type == "text") {
1585 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1586 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1587 var bb = this.node.getExtentOfChar(i);
1588 (bb.y < bbox.y) && (bbox.y = bb.y);
1589 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1590 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1593 hide && this.hide();
1596 Element[proto].attr = function (name, value) {
1602 for (var i in this.attrs) if (this.attrs[has](i)) {
1603 res[i] = this.attrs[i];
1605 this._.rt.deg && (res.rotation = this.rotate());
1606 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1607 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1610 if (value == null && R.is(name, string)) {
1611 if (name == "translation") {
1612 return translate.call(this);
1614 if (name == "rotation") {
1615 return this.rotate();
1617 if (name == "scale") {
1618 return this.scale();
1620 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1621 return this.attrs.gradient;
1623 return this.attrs[name];
1625 if (value == null && R.is(name, array)) {
1627 for (var j = 0, jj = name.length; j < jj; j++) {
1628 values[name[j]] = this.attr(name[j]);
1632 if (value != null) {
1634 params[name] = value;
1635 } else if (name != null && R.is(name, "object")) {
1638 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1639 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1640 this.attrs[key] = params[key];
1641 for (var subkey in par) if (par[has](subkey)) {
1642 params[subkey] = par[subkey];
1645 setFillAndStroke(this, params);
1648 Element[proto].toFront = function () {
1652 this.node.parentNode[appendChild](this.node);
1653 var svg = this.paper;
1654 svg.top != this && tofront(this, svg);
1657 Element[proto].toBack = function () {
1661 if (this.node.parentNode.firstChild != this.node) {
1662 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1663 toback(this, this.paper);
1664 var svg = this.paper;
1668 Element[proto].insertAfter = function (element) {
1672 var node = element.node || element[element.length - 1].node;
1673 if (node.nextSibling) {
1674 node.parentNode.insertBefore(this.node, node.nextSibling);
1676 node.parentNode[appendChild](this.node);
1678 insertafter(this, element, this.paper);
1681 Element[proto].insertBefore = function (element) {
1685 var node = element.node || element[0].node;
1686 node.parentNode.insertBefore(this.node, node);
1687 insertbefore(this, element, this.paper);
1690 Element[proto].blur = function (size) {
1691 // Experimental. No Safari support. Use it on your own risk.
1694 var fltr = $("filter"),
1695 blur = $("feGaussianBlur");
1696 t.attrs.blur = size;
1697 fltr.id = createUUID();
1698 $(blur, {stdDeviation: +size || 1.5});
1699 fltr.appendChild(blur);
1700 t.paper.defs.appendChild(fltr);
1702 $(t.node, {filter: "url(#" + fltr.id + ")"});
1705 t._blur.parentNode.removeChild(t._blur);
1707 delete t.attrs.blur;
1709 t.node.removeAttribute("filter");
1712 var theCircle = function (svg, x, y, r) {
1713 var el = $("circle");
1714 svg.canvas && svg.canvas[appendChild](el);
1715 var res = new Element(el, svg);
1716 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1717 res.type = "circle";
1721 theRect = function (svg, x, y, w, h, r) {
1723 svg.canvas && svg.canvas[appendChild](el);
1724 var res = new Element(el, svg);
1725 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1730 theEllipse = function (svg, x, y, rx, ry) {
1731 var el = $("ellipse");
1732 svg.canvas && svg.canvas[appendChild](el);
1733 var res = new Element(el, svg);
1734 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1735 res.type = "ellipse";
1739 theImage = function (svg, src, x, y, w, h) {
1740 var el = $("image");
1741 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1742 el.setAttributeNS(svg.xlink, "href", src);
1743 svg.canvas && svg.canvas[appendChild](el);
1744 var res = new Element(el, svg);
1745 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1749 theText = function (svg, x, y, text) {
1751 $(el, {x: x, y: y, "text-anchor": "middle"});
1752 svg.canvas && svg.canvas[appendChild](el);
1753 var res = new Element(el, svg);
1754 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1756 setFillAndStroke(res, res.attrs);
1759 setSize = function (width, height) {
1760 this.width = width || this.width;
1761 this.height = height || this.height;
1762 this.canvas[setAttribute]("width", this.width);
1763 this.canvas[setAttribute]("height", this.height);
1766 create = function () {
1767 var con = getContainer[apply](0, arguments),
1768 container = con && con.container,
1772 height = con.height;
1774 throw new Error("SVG container not found.");
1776 var cnvs = $("svg");
1779 width = width || 512;
1780 height = height || 342;
1782 xmlns: "http://www.w3.org/2000/svg",
1787 if (container == 1) {
1788 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1789 doc.body[appendChild](cnvs);
1791 if (container.firstChild) {
1792 container.insertBefore(cnvs, container.firstChild);
1794 container[appendChild](cnvs);
1797 container = new Paper;
1798 container.width = width;
1799 container.height = height;
1800 container.canvas = cnvs;
1801 plugins.call(container, container, R.fn);
1805 paperproto.clear = function () {
1806 var c = this.canvas;
1807 while (c.firstChild) {
1808 c.removeChild(c.firstChild);
1810 this.bottom = this.top = null;
1811 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1812 c[appendChild](this.desc);
1813 c[appendChild](this.defs = $("defs"));
1815 paperproto.remove = function () {
1816 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1817 for (var i in this) {
1818 this[i] = removed(i);
1825 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1826 bites = /([clmz]),?([^clmz]*)/gi,
1827 blurregexp = / progid:\S+Blur\([^\)]+\)/g,
1828 val = /-?[^,\s-]+/g,
1829 coordsize = 1e3 + S + 1e3,
1831 pathlike = {path: 1, rect: 1},
1832 path2vml = function (path) {
1833 var total = /[ahqstv]/ig,
1834 command = pathToAbsolute;
1835 Str(path).match(total) && (command = path2curve);
1837 if (command == pathToAbsolute && !Str(path).match(total)) {
1838 var res = Str(path)[rp](bites, function (all, command, args) {
1840 isMove = lowerCase.call(command) == "m",
1842 args[rp](val, function (value) {
1843 if (isMove && vals[length] == 2) {
1844 res += vals + map[command == "m" ? "l" : "L"];
1847 vals[push](round(value * zoom));
1853 var pa = command(path), p, r;
1855 for (var i = 0, ii = pa[length]; i < ii; i++) {
1857 r = lowerCase.call(pa[i][0]);
1858 r == "z" && (r = "x");
1859 for (var j = 1, jj = p[length]; j < jj; j++) {
1860 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1864 return res[join](S);
1867 R[toString] = function () {
1868 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1870 thePath = function (pathString, vml) {
1871 var g = createNode("group");
1872 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1873 g.coordsize = vml.coordsize;
1874 g.coordorigin = vml.coordorigin;
1875 var el = createNode("shape"), ol = el.style;
1876 ol.width = vml.width + "px";
1877 ol.height = vml.height + "px";
1878 el.coordsize = coordsize;
1879 el.coordorigin = vml.coordorigin;
1881 var p = new Element(el, g, vml),
1882 attr = {fill: "none", stroke: "#000"};
1883 pathString && (attr.path = pathString);
1887 setFillAndStroke(p, attr);
1888 vml.canvas[appendChild](g);
1891 setFillAndStroke = function (o, params) {
1892 o.attrs = o.attrs || {};
1897 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1900 for (var par in params) if (params[has](par)) {
1901 a[par] = params[par];
1904 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1910 params.href && (node.href = params.href);
1911 params.title && (node.title = params.title);
1912 params.target && (node.target = params.target);
1913 params.cursor && (s.cursor = params.cursor);
1914 "blur" in params && o.blur(params.blur);
1915 if (params.path && o.type == "path" || newpath) {
1916 node.path = path2vml(a.path);
1918 if (params.rotation != null) {
1919 o.rotate(params.rotation, true);
1921 if (params.translation) {
1922 xy = Str(params.translation)[split](separator);
1923 translate.call(o, xy[0], xy[1]);
1924 if (o._.rt.cx != null) {
1925 o._.rt.cx +=+ xy[0];
1926 o._.rt.cy +=+ xy[1];
1927 o.setBox(o.attrs, xy[0], xy[1]);
1931 xy = Str(params.scale)[split](separator);
1932 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1934 if ("clip-rect" in params) {
1935 var rect = Str(params["clip-rect"])[split](separator);
1936 if (rect[length] == 4) {
1937 rect[2] = +rect[2] + (+rect[0]);
1938 rect[3] = +rect[3] + (+rect[1]);
1939 var div = node.clipRect || doc.createElement("div"),
1941 group = node.parentNode;
1942 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1943 if (!node.clipRect) {
1944 dstyle.position = "absolute";
1947 dstyle.width = o.paper.width + "px";
1948 dstyle.height = o.paper.height + "px";
1949 group.parentNode.insertBefore(div, group);
1950 div[appendChild](group);
1951 node.clipRect = div;
1954 if (!params["clip-rect"]) {
1955 node.clipRect && (node.clipRect.style.clip = E);
1958 if (o.type == "image" && params.src) {
1959 node.src = params.src;
1961 if (o.type == "image" && params.opacity) {
1962 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1963 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1965 params.font && (s.font = params.font);
1966 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1967 params["font-size"] && (s.fontSize = params["font-size"]);
1968 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1969 params["font-style"] && (s.fontStyle = params["font-style"]);
1970 if (params.opacity != null ||
1971 params["stroke-width"] != null ||
1972 params.fill != null ||
1973 params.stroke != null ||
1974 params["stroke-width"] != null ||
1975 params["stroke-opacity"] != null ||
1976 params["fill-opacity"] != null ||
1977 params["stroke-dasharray"] != null ||
1978 params["stroke-miterlimit"] != null ||
1979 params["stroke-linejoin"] != null ||
1980 params["stroke-linecap"] != null) {
1981 node = o.shape || node;
1982 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1984 !fill && (newfill = fill = createNode(fillString));
1985 if ("fill-opacity" in params || "opacity" in params) {
1986 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1987 opacity = mmin(mmax(opacity, 0), 1);
1988 fill.opacity = opacity;
1990 params.fill && (fill.on = true);
1991 if (fill.on == null || params.fill == "none") {
1994 if (fill.on && params.fill) {
1995 var isURL = params.fill.match(ISURL);
1997 fill.src = isURL[1];
2000 fill.color = R.getRGB(params.fill).hex;
2002 fill.type = "solid";
2003 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
2005 a.gradient = params.fill;
2009 newfill && node[appendChild](fill);
2010 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
2012 !stroke && (newstroke = stroke = createNode("stroke"));
2013 if ((params.stroke && params.stroke != "none") ||
2014 params["stroke-width"] ||
2015 params["stroke-opacity"] != null ||
2016 params["stroke-dasharray"] ||
2017 params["stroke-miterlimit"] ||
2018 params["stroke-linejoin"] ||
2019 params["stroke-linecap"]) {
2022 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
2023 var strokeColor = R.getRGB(params.stroke);
2024 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
2025 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2026 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2027 opacity = mmin(mmax(opacity, 0), 1);
2028 params["stroke-width"] == null && (width = a["stroke-width"]);
2029 params["stroke-width"] && (stroke.weight = width);
2030 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2031 stroke.opacity = opacity;
2033 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2034 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2035 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2036 if (params["stroke-dasharray"]) {
2040 "-.": "shortdashdot",
2041 "-..": "shortdashdotdot",
2046 "--.": "longdashdot",
2047 "--..": "longdashdotdot"
2049 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2051 newstroke && node[appendChild](stroke);
2053 if (res.type == "text") {
2054 s = res.paper.span.style;
2055 a.font && (s.font = a.font);
2056 a["font-family"] && (s.fontFamily = a["font-family"]);
2057 a["font-size"] && (s.fontSize = a["font-size"]);
2058 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2059 a["font-style"] && (s.fontStyle = a["font-style"]);
2060 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
2061 res.W = a.w = res.paper.span.offsetWidth;
2062 res.H = a.h = res.paper.span.offsetHeight;
2064 res.Y = a.y + round(res.H / 2);
2066 // text-anchor emulationm
2067 switch (a["text-anchor"]) {
2069 res.node.style["v-text-align"] = "left";
2070 res.bbx = round(res.W / 2);
2073 res.node.style["v-text-align"] = "right";
2074 res.bbx = -round(res.W / 2);
2077 res.node.style["v-text-align"] = "center";
2082 addGradientFill = function (o, gradient) {
2083 o.attrs = o.attrs || {};
2084 var attrs = o.attrs,
2088 o.attrs.gradient = gradient;
2089 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2094 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2099 gradient = gradient[split](/\s*\-\s*/);
2100 if (type == "linear") {
2101 var angle = gradient.shift();
2102 angle = -toFloat(angle);
2107 var dots = parseDots(gradient);
2111 o = o.shape || o.node;
2112 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2113 !fill.parentNode && o.appendChild(fill);
2116 fill.method = "none";
2117 fill.color = dots[0].color;
2118 fill.color2 = dots[dots[length] - 1].color;
2120 for (var i = 0, ii = dots[length]; i < ii; i++) {
2121 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2123 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2124 if (type == "radial") {
2125 fill.type = "gradientradial";
2126 fill.focus = "100%";
2127 fill.focussize = fxfy;
2128 fill.focusposition = fxfy;
2130 fill.type = "gradient";
2131 fill.angle = (270 - angle) % 360;
2136 Element = function (node, group, vml) {
2144 node.raphael = this;
2157 !vml.bottom && (vml.bottom = this);
2158 this.prev = vml.top;
2159 vml.top && (vml.top.next = this);
2163 elproto = Element[proto];
2164 elproto.rotate = function (deg, cx, cy) {
2170 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2172 return this._.rt.deg;
2174 deg = Str(deg)[split](separator);
2175 if (deg[length] - 1) {
2176 cx = toFloat(deg[1]);
2177 cy = toFloat(deg[2]);
2179 deg = toFloat(deg[0]);
2181 this._.rt.deg = deg;
2183 this._.rt.deg += deg;
2185 cy == null && (cx = null);
2188 this.setBox(this.attrs, cx, cy);
2189 this.Group.style.rotation = this._.rt.deg;
2190 // gradient fix for rotation. TODO
2191 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2192 // fill = fill[0] || {};
2193 // var b = ((360 - this._.rt.deg) - 270) % 360;
2194 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2197 elproto.setBox = function (params, cx, cy) {
2201 var gs = this.Group.style,
2202 os = (this.shape && this.shape.style) || this.node.style;
2203 params = params || {};
2204 for (var i in params) if (params[has](i)) {
2205 this.attrs[i] = params[i];
2207 cx = cx || this._.rt.cx;
2208 cy = cy || this._.rt.cy;
2209 var attr = this.attrs,
2214 switch (this.type) {
2216 x = attr.cx - attr.r;
2217 y = attr.cy - attr.r;
2221 x = attr.cx - attr.rx;
2222 y = attr.cy - attr.ry;
2229 w = attr.width || 0;
2230 h = attr.height || 0;
2233 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2234 x = attr.x - round(this.W / 2);
2235 y = attr.y - this.H / 2;
2241 if (!this.attrs.path) {
2244 w = this.paper.width;
2245 h = this.paper.height;
2247 var dim = pathDimensions(this.attrs.path);
2257 w = this.paper.width;
2258 h = this.paper.height;
2261 cx = (cx == null) ? x + w / 2 : cx;
2262 cy = (cy == null) ? y + h / 2 : cy;
2263 var left = cx - this.paper.width / 2,
2264 top = cy - this.paper.height / 2, t;
2265 gs.left != (t = left + "px") && (gs.left = t);
2266 gs.top != (t = top + "px") && (gs.top = t);
2267 this.X = pathlike[has](this.type) ? -left : x;
2268 this.Y = pathlike[has](this.type) ? -top : y;
2271 if (pathlike[has](this.type)) {
2272 os.left != (t = -left * zoom + "px") && (os.left = t);
2273 os.top != (t = -top * zoom + "px") && (os.top = t);
2274 } else if (this.type == "text") {
2275 os.left != (t = -left + "px") && (os.left = t);
2276 os.top != (t = -top + "px") && (os.top = t);
2278 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2279 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2280 os.left != (t = x - left + "px") && (os.left = t);
2281 os.top != (t = y - top + "px") && (os.top = t);
2282 os.width != (t = w + "px") && (os.width = t);
2283 os.height != (t = h + "px") && (os.height = t);
2286 elproto.hide = function () {
2287 !this.removed && (this.Group.style.display = "none");
2290 elproto.show = function () {
2291 !this.removed && (this.Group.style.display = "block");
2294 elproto.getBBox = function () {
2298 if (pathlike[has](this.type)) {
2299 return pathDimensions(this.attrs.path);
2302 x: this.X + (this.bbx || 0),
2308 elproto.remove = function () {
2312 tear(this, this.paper);
2313 this.node.parentNode.removeChild(this.node);
2314 this.Group.parentNode.removeChild(this.Group);
2315 this.shape && this.shape.parentNode.removeChild(this.shape);
2316 for (var i in this) {
2319 this.removed = true;
2321 elproto.attr = function (name, value) {
2327 for (var i in this.attrs) if (this.attrs[has](i)) {
2328 res[i] = this.attrs[i];
2330 this._.rt.deg && (res.rotation = this.rotate());
2331 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2332 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2335 if (value == null && R.is(name, "string")) {
2336 if (name == "translation") {
2337 return translate.call(this);
2339 if (name == "rotation") {
2340 return this.rotate();
2342 if (name == "scale") {
2343 return this.scale();
2345 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2346 return this.attrs.gradient;
2348 return this.attrs[name];
2350 if (this.attrs && value == null && R.is(name, array)) {
2351 var ii, values = {};
2352 for (i = 0, ii = name[length]; i < ii; i++) {
2353 values[name[i]] = this.attr(name[i]);
2358 if (value != null) {
2360 params[name] = value;
2362 value == null && R.is(name, "object") && (params = name);
2364 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2365 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2366 this.attrs[key] = params[key];
2367 for (var subkey in par) if (par[has](subkey)) {
2368 params[subkey] = par[subkey];
2371 if (params.text && this.type == "text") {
2372 this.node.string = params.text;
2374 setFillAndStroke(this, params);
2375 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2376 addGradientFill(this, params.gradient);
2378 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2382 elproto.toFront = function () {
2383 !this.removed && this.Group.parentNode[appendChild](this.Group);
2384 this.paper.top != this && tofront(this, this.paper);
2387 elproto.toBack = function () {
2391 if (this.Group.parentNode.firstChild != this.Group) {
2392 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2393 toback(this, this.paper);
2397 elproto.insertAfter = function (element) {
2401 if (element.constructor == Set) {
2402 element = element[element.length - 1];
2404 if (element.Group.nextSibling) {
2405 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2407 element.Group.parentNode[appendChild](this.Group);
2409 insertafter(this, element, this.paper);
2412 elproto.insertBefore = function (element) {
2416 if (element.constructor == Set) {
2417 element = element[0];
2419 element.Group.parentNode.insertBefore(this.Group, element.Group);
2420 insertbefore(this, element, this.paper);
2423 elproto.blur = function (size) {
2424 var s = this.node.runtimeStyle,
2426 f = f.replace(blurregexp, E);
2428 this.attrs.blur = size;
2429 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2430 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2434 delete this.attrs.blur;
2438 theCircle = function (vml, x, y, r) {
2439 var g = createNode("group"),
2440 o = createNode("oval"),
2442 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2443 g.coordsize = coordsize;
2444 g.coordorigin = vml.coordorigin;
2446 var res = new Element(o, g, vml);
2447 res.type = "circle";
2448 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2452 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2453 vml.canvas[appendChild](g);
2456 function rectPath(x, y, w, h, r) {
2458 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);
2460 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2463 theRect = function (vml, x, y, w, h, r) {
2464 var path = rectPath(x, y, w, h, r),
2465 res = vml.path(path),
2469 res.W = a.width = w;
2470 res.H = a.height = h;
2476 theEllipse = function (vml, x, y, rx, ry) {
2477 var g = createNode("group"),
2478 o = createNode("oval"),
2480 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2481 g.coordsize = coordsize;
2482 g.coordorigin = vml.coordorigin;
2484 var res = new Element(o, g, vml);
2485 res.type = "ellipse";
2486 setFillAndStroke(res, {stroke: "#000"});
2491 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2492 vml.canvas[appendChild](g);
2495 theImage = function (vml, src, x, y, w, h) {
2496 var g = createNode("group"),
2497 o = createNode("image");
2498 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2499 g.coordsize = coordsize;
2500 g.coordorigin = vml.coordorigin;
2503 var res = new Element(o, g, vml);
2505 res.attrs.src = src;
2510 res.setBox({x: x, y: y, width: w, height: h});
2511 vml.canvas[appendChild](g);
2514 theText = function (vml, x, y, text) {
2515 var g = createNode("group"),
2516 el = createNode("shape"),
2518 path = createNode("path"),
2520 o = createNode("textpath");
2521 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2522 g.coordsize = coordsize;
2523 g.coordorigin = vml.coordorigin;
2524 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2525 path.textpathok = true;
2526 ol.width = vml.width;
2527 ol.height = vml.height;
2528 o.string = Str(text);
2531 el[appendChild](path);
2533 var res = new Element(o, g, vml);
2535 res.textpath = path;
2537 res.attrs.text = text;
2542 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2544 vml.canvas[appendChild](g);
2547 setSize = function (width, height) {
2548 var cs = this.canvas.style;
2549 width == +width && (width += "px");
2550 height == +height && (height += "px");
2553 cs.clip = "rect(0 " + width + " " + height + " 0)";
2557 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2559 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2560 createNode = function (tagName) {
2561 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2564 createNode = function (tagName) {
2565 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2568 create = function () {
2569 var con = getContainer[apply](0, arguments),
2570 container = con.container,
2571 height = con.height,
2577 throw new Error("VML container not found.");
2579 var res = new Paper,
2580 c = res.canvas = doc.createElement("div"),
2584 width = width || 512;
2585 height = height || 342;
2586 width == +width && (width += "px");
2587 height == +height && (height += "px");
2590 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2591 res.coordorigin = "0 0";
2592 res.span = doc.createElement("span");
2593 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2594 c[appendChild](res.span);
2595 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);
2596 if (container == 1) {
2597 doc.body[appendChild](c);
2600 cs.position = "absolute";
2602 if (container.firstChild) {
2603 container.insertBefore(c, container.firstChild);
2605 container[appendChild](c);
2608 plugins.call(res, res, R.fn);
2611 paperproto.clear = function () {
2612 this.canvas.innerHTML = E;
2613 this.span = doc.createElement("span");
2614 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2615 this.canvas[appendChild](this.span);
2616 this.bottom = this.top = null;
2618 paperproto.remove = function () {
2619 this.canvas.parentNode.removeChild(this.canvas);
2620 for (var i in this) {
2621 this[i] = removed(i);
2628 // WebKit rendering bug workaround method
2629 var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2630 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2631 paperproto.safari = function () {
2632 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2633 win.setTimeout(function () {rect.remove();});
2636 paperproto.safari = function () {};
2640 var preventDefault = function () {
2641 this.returnValue = false;
2643 preventTouch = function () {
2644 return this.originalEvent.preventDefault();
2646 stopPropagation = function () {
2647 this.cancelBubble = true;
2649 stopTouch = function () {
2650 return this.originalEvent.stopPropagation();
2652 addEvent = (function () {
2653 if (doc.addEventListener) {
2654 return function (obj, type, fn, element) {
2655 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2656 var f = function (e) {
2657 if (supportsTouch && touchMap[has](type)) {
2658 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2659 if (e.targetTouches[i].target == obj) {
2661 e = e.targetTouches[i];
2662 e.originalEvent = olde;
2663 e.preventDefault = preventTouch;
2664 e.stopPropagation = stopTouch;
2669 return fn.call(element, e);
2671 obj.addEventListener(realName, f, false);
2672 return function () {
2673 obj.removeEventListener(realName, f, false);
2677 } else if (doc.attachEvent) {
2678 return function (obj, type, fn, element) {
2679 var f = function (e) {
2681 e.preventDefault = e.preventDefault || preventDefault;
2682 e.stopPropagation = e.stopPropagation || stopPropagation;
2683 return fn.call(element, e);
2685 obj.attachEvent("on" + type, f);
2686 var detacher = function () {
2687 obj.detachEvent("on" + type, f);
2695 dragMove = function (e) {
2698 scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2699 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2704 if (supportsTouch) {
2705 var i = e.touches.length,
2708 touch = e.touches[i];
2709 if (touch.identifier == dragi.el._drag.id) {
2712 (e.originalEvent ? e.originalEvent : e).preventDefault();
2721 dragi.move && dragi.move.call(dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
2724 dragUp = function (e) {
2725 R.unmousemove(dragMove).unmouseup(dragUp);
2726 var i = drag.length,
2730 dragi.el._drag = {};
2731 dragi.end && dragi.end.call(dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
2735 for (var i = events[length]; i--;) {
2736 (function (eventName) {
2737 R[eventName] = Element[proto][eventName] = function (fn, scope) {
2738 if (R.is(fn, "function")) {
2739 this.events = this.events || [];
2740 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, scope || this)});
2744 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2745 var events = this.events,
2747 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2749 events.splice(l, 1);
2750 !events.length && delete this.events;
2757 elproto.hover = function (f_in, f_out, scope_in, scope_out) {
2758 return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
2760 elproto.unhover = function (f_in, f_out) {
2761 return this.unmouseover(f_in).unmouseout(f_out);
2763 elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
2765 this.mousedown(function (e) {
2766 (e.originalEvent || e).preventDefault();
2767 var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2768 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2769 this._drag.x = e.clientX + scrollX;
2770 this._drag.y = e.clientY + scrollY;
2771 this._drag.id = e.identifier;
2772 onstart && onstart.call(start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e);
2773 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2774 drag.push({el: this, move: onmove, end: onend, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
2778 elproto.undrag = function (onmove, onstart, onend) {
2779 var i = drag.length;
2781 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i++, 1);
2783 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2785 paperproto.circle = function (x, y, r) {
2786 return theCircle(this, x || 0, y || 0, r || 0);
2788 paperproto.rect = function (x, y, w, h, r) {
2789 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2791 paperproto.ellipse = function (x, y, rx, ry) {
2792 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2794 paperproto.path = function (pathString) {
2795 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2796 return thePath(R.format[apply](R, arguments), this);
2798 paperproto.image = function (src, x, y, w, h) {
2799 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2801 paperproto.text = function (x, y, text) {
2802 return theText(this, x || 0, y || 0, Str(text));
2804 paperproto.set = function (itemsArray) {
2805 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2806 return new Set(itemsArray);
2808 paperproto.setSize = setSize;
2809 paperproto.top = paperproto.bottom = null;
2810 paperproto.raphael = R;
2812 return this.x + S + this.y;
2814 elproto.resetScale = function () {
2820 this.attrs.scale = "1 1";
2822 elproto.scale = function (x, y, cx, cy) {
2826 if (x == null && y == null) {
2841 var bb = this.getBBox(),
2842 rcx = bb.x + bb.width / 2,
2843 rcy = bb.y + bb.height / 2,
2844 kx = abs(x / this._.sx),
2845 ky = abs(y / this._.sy);
2846 cx = (+cx || cx == 0) ? cx : rcx;
2847 cy = (+cy || cy == 0) ? cy : rcy;
2848 var posx = this._.sx > 0,
2849 posy = this._.sy > 0,
2850 dirx = ~~(x / abs(x)),
2851 diry = ~~(y / abs(y)),
2854 s = this.node.style,
2855 ncx = cx + abs(rcx - cx) * dkx * (rcx > cx == posx ? 1 : -1),
2856 ncy = cy + abs(rcy - cy) * dky * (rcy > cy == posy ? 1 : -1),
2857 fr = (x * dirx > y * diry ? ky : kx);
2858 switch (this.type) {
2861 var neww = a.width * kx,
2862 newh = a.height * ky;
2888 var path = pathToRelative(a.path),
2890 fx = posx ? dkx : kx,
2891 fy = posy ? dky : ky;
2892 for (var i = 0, ii = path[length]; i < ii; i++) {
2894 P0 = upperCase.call(p[0]);
2895 if (P0 == "M" && skip) {
2901 p[path[i][length] - 2] *= fx;
2902 p[path[i][length] - 1] *= fy;
2905 p[5] = +(dirx + diry ? !+p[4] : !!+p[4]);
2906 } else if (P0 == "H") {
2907 for (var j = 1, jj = p[length]; j < jj; j++) {
2910 } else if (P0 == "V") {
2911 for (j = 1, jj = p[length]; j < jj; j++) {
2915 for (j = 1, jj = p[length]; j < jj; j++) {
2916 p[j] *= (j % 2) ? fx : fy;
2920 var dim2 = pathDimensions(path);
2921 dx = ncx - dim2.x - dim2.width / 2;
2922 dy = ncy - dim2.y - dim2.height / 2;
2925 this.attr({path: path});
2928 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2929 if (this.transformations) {
2930 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2931 this.node[setAttribute]("transform", this.transformations[join](S));
2932 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2933 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2934 this.attr({x: dx, y: dy});
2938 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2939 ", M12=0, M21=0, M22=", diry,
2940 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2941 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2944 if (this.transformations) {
2945 this.transformations[2] = E;
2946 this.node[setAttribute]("transform", this.transformations[join](S));
2950 this.node.filterMatrix = E;
2951 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2954 a.scale = [x, y, cx, cy][join](S);
2960 elproto.clone = function () {
2964 var attr = this.attr();
2966 delete attr.translation;
2967 return this.paper[this.type]().attr(attr);
2969 var curveslengths = {},
2970 getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2973 name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
2974 cache = curveslengths[name],
2976 !cache && (curveslengths[name] = cache = {data: []});
2977 cache.timer && clearTimeout(cache.timer);
2978 cache.timer = setTimeout(function () {delete curveslengths[name];}, 2000);
2979 if (length != null) {
2980 var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
2981 precision = ~~total * 10;
2983 for (var i = 0; i < precision + 1; i++) {
2984 if (cache.data[length] > i) {
2985 dot = cache.data[i * precision];
2987 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
2988 cache.data[i] = dot;
2990 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2991 if (length != null && len >= length) {
2996 if (length == null) {
3000 getLengthFactory = function (istotal, subpath) {
3001 return function (path, length, onlystart) {
3002 path = path2curve(path);
3003 var x, y, p, l, sp = "", subpaths = {}, point,
3005 for (var i = 0, ii = path.length; i < ii; i++) {
3011 l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
3012 if (len + l > length) {
3013 if (subpath && !subpaths.start) {
3014 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3015 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
3016 if (onlystart) {return sp;}
3017 subpaths.start = sp;
3018 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
3024 if (!istotal && !subpath) {
3025 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3026 return {x: point.x, y: point.y, alpha: point.alpha};
3036 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
3037 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
3041 var getTotalLength = getLengthFactory(1),
3042 getPointAtLength = getLengthFactory(),
3043 getSubpathsAtLength = getLengthFactory(0, 1);
3044 elproto.getTotalLength = function () {
3045 if (this.type != "path") {return;}
3046 if (this.node.getTotalLength) {
3047 return this.node.getTotalLength();
3049 return getTotalLength(this.attrs.path);
3051 elproto.getPointAtLength = function (length) {
3052 if (this.type != "path") {return;}
3053 return getPointAtLength(this.attrs.path, length);
3055 elproto.getSubpath = function (from, to) {
3056 if (this.type != "path") {return;}
3057 if (abs(this.getTotalLength() - to) < "1e-6") {
3058 return getSubpathsAtLength(this.attrs.path, from).end;
3060 var a = getSubpathsAtLength(this.attrs.path, to, 1);
3061 return from ? getSubpathsAtLength(a, from).end : a;
3064 // animation easing formulas
3065 R.easing_formulas = {
3066 linear: function (n) {
3073 return pow(n - 1, 3) + 1;
3075 "<>": function (n) {
3078 return pow(n, 3) / 2;
3081 return (pow(n, 3) + 2) / 2;
3083 backIn: function (n) {
3085 return n * n * ((s + 1) * n - s);
3087 backOut: function (n) {
3090 return n * n * ((s + 1) * n + s) + 1;
3092 elastic: function (n) {
3093 if (n == 0 || n == 1) {
3098 return pow(2, -10 * n) * math.sin((n - s) * (2 * PI) / p) + 1;
3100 bounce: function (n) {
3109 l = s * n * n + .75;
3111 if (n < (2.5 / p)) {
3113 l = s * n * n + .9375;
3116 l = s * n * n + .984375;
3124 var animationElements = [],
3125 animation = function () {
3126 var Now = +new Date;
3127 for (var l = 0; l < animationElements[length]; l++) {
3128 var e = animationElements[l];
3129 if (e.stop || e.el.removed) {
3132 var time = Now - e.start,
3143 var pos = easing(time / ms);
3144 for (var attr in from) if (from[has](attr)) {
3145 switch (availableAnimAttrs[attr]) {
3147 now = pos * ms * diff[attr];
3148 to.back && (now = to.len - now);
3149 var point = getPointAtLength(to[attr], now);
3150 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3153 that.translate(point.x - diff.sx, point.y - diff.sy);
3154 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3157 now = +from[attr] + pos * ms * diff[attr];
3161 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3162 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3163 upto255(round(from[attr].b + pos * ms * diff[attr].b))
3168 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3169 now[i] = [from[attr][i][0]];
3170 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3171 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3173 now[i] = now[i][join](S);
3180 var x = pos * ms * diff[attr][0] - t.x,
3181 y = pos * ms * diff[attr][1] - t.y;
3187 now = +from[attr][0] + pos * ms * diff[attr][0];
3188 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3191 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);
3197 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3203 var from2 = [].concat(from[attr]);
3205 i = that.paper.customAttributes[attr].length;
3207 now[i] = +from2[i] + pos * ms * diff[attr][i];
3214 that._run && that._run.call(that);
3217 point = getPointAtLength(to.along, to.len * !to.back);
3218 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3219 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3221 (t.x || t.y) && that.translate(-t.x, -t.y);
3222 to.scale && (to.scale += E);
3224 animationElements.splice(l--, 1);
3227 R.svg && that && that.paper && that.paper.safari();
3228 animationElements[length] && setTimeout(animation);
3230 keyframesRun = function (attr, element, time, prev, prevcallback) {
3231 var dif = time - prev;
3232 element.timeouts.push(setTimeout(function () {
3233 R.is(prevcallback, "function") && prevcallback.call(element);
3234 element.animate(attr, dif, attr.easing);
3237 upto255 = function (color) {
3238 return mmax(mmin(color, 255), 0);
3240 translate = function (x, y) {
3242 return {x: this._.tx, y: this._.ty, toString: x_y};
3246 switch (this.type) {
3249 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3254 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3257 var path = pathToRelative(this.attrs.path);
3260 this.attr({path: path});
3265 elproto.animateWith = function (element, params, ms, easing, callback) {
3266 for (var i = 0, ii = animationElements.length; i < ii; i++) {
3267 if (animationElements[i].el.id == element.id) {
3268 params.start = animationElements[i].start;
3271 return this.animate(params, ms, easing, callback);
3273 elproto.animateAlong = along();
3274 elproto.animateAlongBack = along(1);
3275 function along(isBack) {
3276 return function (path, ms, rotate, callback) {
3277 var params = {back: isBack};
3278 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3279 path && path.constructor == Element && (path = path.attrs.path);
3280 path && (params.along = path);
3281 return this.animate(params, ms, callback);
3284 function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
3286 bx = 3 * (p2x - p1x) - cx,
3289 by = 3 * (p2y - p1y) - cy,
3291 function sampleCurveX(t) {
3292 return ((ax * t + bx) * t + cx) * t;
3294 function solve(x, epsilon) {
3295 var t = solveCurveX(x, epsilon);
3296 return ((ay * t + by) * t + cy) * t;
3298 function solveCurveX(x, epsilon) {
3299 var t0, t1, t2, x2, d2, i;
3300 for(t2 = x, i = 0; i < 8; i++) {
3301 x2 = sampleCurveX(t2) - x;
3302 if (abs(x2) < epsilon) {
3305 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
3306 if (abs(d2) < 1e-6) {
3321 x2 = sampleCurveX(t2);
3322 if (abs(x2 - x) < epsilon) {
3330 t2 = (t1 - t0) / 2 + t0;
3334 return solve(t, 1 / (200 * duration));
3336 elproto.onAnimation = function (f) {
3340 elproto.animate = function (params, ms, easing, callback) {
3342 element.timeouts = element.timeouts || [];
3343 if (R.is(easing, "function") || !easing) {
3344 callback = easing || null;
3346 if (element.removed) {
3347 callback && callback.call(element);
3352 animateable = false,
3354 for (var attr in params) if (params[has](attr)) {
3355 if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
3357 from[attr] = element.attr(attr);
3358 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3359 to[attr] = params[attr];
3360 switch (availableAnimAttrs[attr]) {
3362 var len = getTotalLength(params[attr]);
3363 var point = getPointAtLength(params[attr], len * !!params.back);
3364 var bb = element.getBBox();
3365 diff[attr] = len / ms;
3370 to.rot = params.rot;
3371 to.back = params.back;
3373 params.rot && (diff.r = toFloat(element.rotate()) || 0);
3376 diff[attr] = (to[attr] - from[attr]) / ms;
3379 from[attr] = R.getRGB(from[attr]);
3380 var toColour = R.getRGB(to[attr]);
3382 r: (toColour.r - from[attr].r) / ms,
3383 g: (toColour.g - from[attr].g) / ms,
3384 b: (toColour.b - from[attr].b) / ms
3388 var pathes = path2curve(from[attr], to[attr]);
3389 from[attr] = pathes[0];
3390 var toPath = pathes[1];
3392 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3393 diff[attr][i] = [0];
3394 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3395 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3400 var values = Str(params[attr])[split](separator),
3401 from2 = Str(from[attr])[split](separator);
3404 from[attr] = [0, 0];
3405 diff[attr] = [values[0] / ms, values[1] / ms];
3408 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3409 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3412 params[attr] = values;
3413 from[attr] = Str(from[attr])[split](separator);
3414 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3417 from[attr] = Str(from[attr])[split](separator);
3421 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3428 values = [].concat(params[attr]);
3429 from2 = [].concat(from[attr]);
3431 i = element.paper.customAttributes[attr][length];
3433 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3442 for (var key in params) if (params[has](key) && animKeyFrames.test(key)) {
3443 attr = {value: params[key]};
3444 key == "from" && (key = 0);
3445 key == "to" && (key = 100);
3446 attr.key = toInt(key, 10);
3449 attrs.sort(sortByKey);
3451 attrs.unshift({key: 0, value: element.attrs});
3453 for (i = 0, ii = attrs[length]; i < ii; i++) {
3454 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);
3456 lastcall = attrs[attrs[length] - 1].value.callback;
3458 element.timeouts.push(setTimeout(function () {lastcall.call(element);}, ms));
3461 var easyeasy = R.easing_formulas[easing];
3463 easyeasy = Str(easing).match(bezierrg);
3464 if (easyeasy && easyeasy[length] == 5) {
3465 var curve = easyeasy;
3466 easyeasy = function (t) {
3467 return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
3470 easyeasy = function (t) {
3475 animationElements.push({
3476 start: params.start || +new Date,
3485 R.is(callback, "function") && (element._ac = setTimeout(function () {
3486 callback.call(element);
3488 animationElements[length] == 1 && setTimeout(animation);
3492 elproto.stop = function () {
3493 for (var i = 0; i < animationElements.length; i++) {
3494 animationElements[i].el.id == this.id && animationElements.splice(i--, 1);
3496 for (i = 0, ii = this.timeouts && this.timeouts.length; i < ii; i++) {
3497 clearTimeout(this.timeouts[i]);
3500 clearTimeout(this._ac);
3504 elproto.translate = function (x, y) {
3505 return this.attr({translation: x + " " + y});
3507 elproto[toString] = function () {
3508 return "Rapha\xebl\u2019s object";
3510 R.ae = animationElements;
3513 var Set = function (items) {
3518 for (var i = 0, ii = items[length]; i < ii; i++) {
3519 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3520 this[this.items[length]] = this.items[this.items[length]] = items[i];
3526 Set[proto][push] = function () {
3529 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3530 item = arguments[i];
3531 if (item && (item.constructor == Element || item.constructor == Set)) {
3532 len = this.items[length];
3533 this[len] = this.items[len] = item;
3539 Set[proto].pop = function () {
3540 delete this[this[length]--];
3541 return this.items.pop();
3543 for (var method in elproto) if (elproto[has](method)) {
3544 Set[proto][method] = (function (methodname) {
3545 return function () {
3546 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3547 this.items[i][methodname][apply](this.items[i], arguments);
3553 Set[proto].attr = function (name, value) {
3554 if (name && R.is(name, array) && R.is(name[0], "object")) {
3555 for (var j = 0, jj = name[length]; j < jj; j++) {
3556 this.items[j].attr(name[j]);
3559 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3560 this.items[i].attr(name, value);
3565 Set[proto].animate = function (params, ms, easing, callback) {
3566 (R.is(easing, "function") || !easing) && (callback = easing || null);
3567 var len = this.items[length],
3572 callback && (collector = function () {
3573 !--len && callback.call(set);
3575 easing = R.is(easing, string) ? easing : collector;
3576 item = this.items[--i].animate(params, ms, easing, collector);
3578 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3582 Set[proto].insertAfter = function (el) {
3583 var i = this.items[length];
3585 this.items[i].insertAfter(el);
3589 Set[proto].getBBox = function () {
3594 for (var i = this.items[length]; i--;) {
3595 var box = this.items[i].getBBox();
3598 w[push](box.x + box.width);
3599 h[push](box.y + box.height);
3601 x = mmin[apply](0, x);
3602 y = mmin[apply](0, y);
3606 width: mmax[apply](0, w) - x,
3607 height: mmax[apply](0, h) - y
3610 Set[proto].clone = function (s) {
3612 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3613 s[push](this.items[i].clone());
3618 R.registerFont = function (font) {
3622 this.fonts = this.fonts || {};
3628 family = font.face["font-family"];
3629 for (var prop in font.face) if (font.face[has](prop)) {
3630 fontcopy.face[prop] = font.face[prop];
3632 if (this.fonts[family]) {
3633 this.fonts[family][push](fontcopy);
3635 this.fonts[family] = [fontcopy];
3638 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3639 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3640 var path = font.glyphs[glyph];
3641 fontcopy.glyphs[glyph] = {
3644 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3645 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3649 for (var k in path.k) if (path[has](k)) {
3650 fontcopy.glyphs[glyph].k[k] = path.k[k];
3657 paperproto.getFont = function (family, weight, style, stretch) {
3658 stretch = stretch || "normal";
3659 style = style || "normal";
3660 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3664 var font = R.fonts[family];
3666 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3667 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3668 if (name.test(fontName)) {
3669 font = R.fonts[fontName];
3676 for (var i = 0, ii = font[length]; i < ii; i++) {
3678 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3685 paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
3686 origin = origin || "middle"; // baseline|middle
3687 letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
3688 var out = this.set(),
3689 letters = Str(string)[split](E),
3693 R.is(font, string) && (font = this.getFont(font));
3695 scale = (size || 16) / font.face["units-per-em"];
3696 var bb = font.face.bbox.split(separator),
3698 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3699 for (var i = 0, ii = letters[length]; i < ii; i++) {
3700 var prev = i && font.glyphs[letters[i - 1]] || {},
3701 curr = font.glyphs[letters[i]];
3702 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
3703 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3705 out.scale(scale, scale, top, height).translate(x - top, y - height);
3710 R.format = function (token, params) {
3711 var args = R.is(params, array) ? [0][concat](params) : arguments;
3712 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3713 return args[++i] == null ? E : args[i];
3717 R.ninja = function () {
3718 oldRaphael.was ? (win.Raphael = oldRaphael.is) : delete Raphael;
3724 oldRaphael.was ? (win.Raphael = R) : (Raphael = R);