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,
62 toString = "toString",
64 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*\)|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,
70 isnan = /^(NaN|-?Infinity)$/,
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 p2s = /,?([achlmqrstvxz]),?/gi,
82 commaSpaces = /\s*,\s*/,
83 hsrg = {hs: 1, rg: 1},
84 animKeyFrames= /^(from|to|\d+%)$/,
85 pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
86 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
87 sortByKey = function (a, b) {
91 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
92 if (R.type == "VML") {
93 var d = doc.createElement("div"),
95 d.innerHTML = '<v:shape adj="1"/>';
97 b.style.behavior = "url(#default#VML)";
98 if (!(b && typeof b.adj == "object")) {
103 R.svg = !(R.vml = R.type == "VML");
104 Paper[proto] = R[proto];
105 paperproto = Paper[proto];
109 R.is = function (o, type) {
110 type = lowerCase.call(type);
111 if (type == "finite") {
112 return !isnan.test(+o);
114 return (type == "null" && o === null) ||
115 (type == typeof o) ||
116 (type == "object" && o === Object(o)) ||
117 (type == "array" && Array.isArray && Array.isArray(o)) ||
118 objectToString.call(o).slice(8, -1).toLowerCase() == type;
120 R.angle = function (x1, y1, x2, y2, x3, y3) {
127 return ((x < 0) * 180 + math.atan(-y / -x) * 180 / math.PI + 360) % 360;
129 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
132 R.snapTo = function (values, value, tolerance) {
133 tolerance = tolerance || 10;
134 values = [][concat](values);
135 var i = values.length;
136 while (i--) if (abs(values[i] - value) <= tolerance) {
141 function createUUID() {
142 // http://www.ietf.org/rfc/rfc4122.txt
145 for (; i < 32; i++) {
146 s[i] = (~~(math.random() * 16))[toString](16);
148 s[12] = 4; // bits 12-15 of the time_hi_and_version field to 0010
149 s[16] = ((s[16] & 3) | 8)[toString](16); // bits 6-7 of the clock_seq_hi_and_reserved to 01
150 return "r-" + s[join]("");
153 R.setWindow = function (newwin) {
158 var toHex = function (color) {
160 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
161 var trim = /^\s+|\s+$/g;
164 var docum = new ActiveXObject("htmlfile");
165 docum.write("<body>");
169 bod = createPopup().document.body;
171 var range = bod.createTextRange();
172 toHex = cacher(function (color) {
174 bod.style.color = Str(color)[rp](trim, E);
175 var value = range.queryCommandValue("ForeColor");
176 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
177 return "#" + ("000000" + value[toString](16)).slice(-6);
183 var i = doc.createElement("i");
184 i.title = "Rapha\xebl Colour Picker";
185 i.style.display = "none";
186 doc.body[appendChild](i);
187 toHex = cacher(function (color) {
188 i.style.color = color;
189 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
194 hsbtoString = function () {
195 return "hsb(" + [this.h, this.s, this.b] + ")";
197 hsltoString = function () {
198 return "hsl(" + [this.h, this.s, this.l] + ")";
200 rgbtoString = function () {
203 R.hsb2rgb = function (h, s, b) {
204 if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
209 return R.hsl2rgb(h, s, b / 2);
211 R.hsl2rgb = function (h, s, l) {
212 if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
217 if (h > 1 || s > 1 || l > 1) {
223 channels = ["r", "g", "b"],
238 for (var i = 0, ii = channels.length; i < ii; i++) {
239 t3 = h + 1 / 3 * -(i - 1);
243 rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
244 } else if (t3 * 2 < 1) {
245 rgb[channels[i]] = t2;
246 } else if (t3 * 3 < 2) {
247 rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
249 rgb[channels[i]] = t1;
256 r = (~~rgb.r)[toString](16);
257 g = (~~rgb.g)[toString](16);
258 b = (~~rgb.b)[toString](16);
262 rgb.hex = "#" + r + g + b;
263 rgb.toString = rgbtoString;
266 R.rgb2hsb = function (red, green, blue) {
267 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
272 if (green == null && R.is(red, string)) {
273 var clr = R.getRGB(red);
278 if (red > 1 || green > 1 || blue > 1) {
283 var max = mmax(red, green, blue),
284 min = mmin(red, green, blue),
289 return {h: 0, s: 0, b: max, toString: hsbtoString};
291 var delta = (max - min);
292 saturation = delta / max;
294 hue = (green - blue) / delta;
295 } else if (green == max) {
296 hue = 2 + ((blue - red) / delta);
298 hue = 4 + ((red - green) / delta);
304 return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
306 R.rgb2hsl = function (red, green, blue) {
307 if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
312 if (green == null && R.is(red, string)) {
313 var clr = R.getRGB(red);
318 if (red > 1 || green > 1 || blue > 1) {
323 var max = mmax(red, green, blue),
324 min = mmin(red, green, blue),
330 hsl = {h: 0, s: 0, l: l};
332 var delta = max - min;
333 s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
335 h = (green - blue) / delta;
336 } else if (green == max) {
337 h = 2 + (blue - red) / delta;
339 h = 4 + (red - green) / delta;
344 hsl = {h: h, s: s, l: l};
346 hsl.toString = hsltoString;
349 R._path2string = function () {
350 return this.join(",")[rp](p2s, "$1");
352 function cacher(f, scope, postprocessor) {
354 var arg = Array[proto].slice.call(arguments, 0),
355 args = arg[join]("\u25ba"),
356 cache = newf.cache = newf.cache || {},
357 count = newf.count = newf.count || [];
358 if (cache[has](args)) {
359 return postprocessor ? postprocessor(cache[args]) : cache[args];
361 count[length] >= 1e3 && delete cache[count.shift()];
363 cache[args] = f[apply](scope, arg);
364 return postprocessor ? postprocessor(cache[args]) : cache[args];
369 R.getRGB = cacher(function (colour) {
370 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
371 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
373 if (colour == "none") {
374 return {r: -1, g: -1, b: -1, hex: "none"};
376 !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
383 rgb = colour.match(colourRegExp);
386 blue = toInt(rgb[2].substring(5), 16);
387 green = toInt(rgb[2].substring(3, 5), 16);
388 red = toInt(rgb[2].substring(1, 3), 16);
391 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
392 green = toInt((t = rgb[3].charAt(2)) + t, 16);
393 red = toInt((t = rgb[3].charAt(1)) + t, 16);
396 rgb = rgb[4][split](commaSpaces);
397 red = toFloat(rgb[0]);
398 green = toFloat(rgb[1]);
399 blue = toFloat(rgb[2]);
400 opacity = toFloat(rgb[3]);
403 rgb = rgb[5][split](commaSpaces);
404 red = toFloat(rgb[0]) * 2.55;
405 green = toFloat(rgb[1]) * 2.55;
406 blue = toFloat(rgb[2]) * 2.55;
407 opacity = toFloat(rgb[3]);
410 rgb = rgb[6][split](commaSpaces);
411 red = toFloat(rgb[0]);
412 green = toFloat(rgb[1]);
413 blue = toFloat(rgb[2]);
414 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
415 return R.hsb2rgb(red, green, blue);
418 rgb = rgb[7][split](commaSpaces);
419 red = toFloat(rgb[0]) * 2.55;
420 green = toFloat(rgb[1]) * 2.55;
421 blue = toFloat(rgb[2]) * 2.55;
422 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
423 return R.hsb2rgb(red, green, blue);
426 rgb = rgb[8][split](commaSpaces);
427 red = toFloat(rgb[0]);
428 green = toFloat(rgb[1]);
429 blue = toFloat(rgb[2]);
430 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
431 return R.hsl2rgb(red, green, blue);
434 rgb = rgb[9][split](commaSpaces);
435 red = toFloat(rgb[0]) * 2.55;
436 green = toFloat(rgb[1]) * 2.55;
437 blue = toFloat(rgb[2]) * 2.55;
438 (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
439 return R.hsl2rgb(red, green, blue);
441 rgb = {r: red, g: green, b: blue};
442 var r = (~~red)[toString](16),
443 g = (~~green)[toString](16),
444 b = (~~blue)[toString](16);
448 rgb.hex = "#" + r + g + b;
449 isFinite(toFloat(opacity)) && (rgb.o = opacity);
452 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
454 R.getColor = function (value) {
455 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
456 rgb = this.hsb2rgb(start.h, start.s, start.b);
461 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
465 R.getColor.reset = function () {
469 R.parsePathString = cacher(function (pathString) {
473 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
475 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
476 data = pathClone(pathString);
479 Str(pathString)[rp](pathCommand, function (a, b, c) {
481 name = lowerCase.call(b);
482 c[rp](pathValues, function (a, b) {
483 b && params[push](+b);
485 if (name == "m" && params[length] > 2) {
486 data[push]([b][concat](params.splice(0, 2)));
488 b = b == "m" ? "l" : "L";
490 while (params[length] >= paramCounts[name]) {
491 data[push]([b][concat](params.splice(0, paramCounts[name])));
492 if (!paramCounts[name]) {
498 data[toString] = R._path2string;
501 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
503 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
504 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
505 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
506 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
507 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
508 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
509 ax = (1 - t) * p1x + t * c1x,
510 ay = (1 - t) * p1y + t * c1y,
511 cx = (1 - t) * c2x + t * p2x,
512 cy = (1 - t) * c2y + t * p2y,
513 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
514 (mx > nx || my < ny) && (alpha += 180);
515 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};
517 var pathDimensions = cacher(function (path) {
519 return {x: 0, y: 0, width: 0, height: 0};
521 path = path2curve(path);
527 for (var i = 0, ii = path[length]; i < ii; i++) {
535 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
536 X = X[concat](dim.min.x, dim.max.x);
537 Y = Y[concat](dim.min.y, dim.max.y);
542 var xmin = mmin[apply](0, X),
543 ymin = mmin[apply](0, Y);
547 width: mmax[apply](0, X) - xmin,
548 height: mmax[apply](0, Y) - ymin
551 pathClone = function (pathArray) {
553 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
554 pathArray = R.parsePathString(pathArray);
556 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
558 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
559 res[i][j] = pathArray[i][j];
562 res[toString] = R._path2string;
565 pathToRelative = cacher(function (pathArray) {
566 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
567 pathArray = R.parsePathString(pathArray);
575 if (pathArray[0][0] == "M") {
581 res[push](["M", x, y]);
583 for (var i = start, ii = pathArray[length]; i < ii; i++) {
586 if (pa[0] != lowerCase.call(pa[0])) {
587 r[0] = lowerCase.call(pa[0]);
595 r[6] = +(pa[6] - x).toFixed(3);
596 r[7] = +(pa[7] - y).toFixed(3);
599 r[1] = +(pa[1] - y).toFixed(3);
605 for (var j = 1, jj = pa[length]; j < jj; j++) {
606 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
615 for (var k = 0, kk = pa[length]; k < kk; k++) {
619 var len = res[i][length];
626 x += +res[i][len - 1];
629 y += +res[i][len - 1];
632 x += +res[i][len - 2];
633 y += +res[i][len - 1];
636 res[toString] = R._path2string;
639 pathToAbsolute = cacher(function (pathArray) {
640 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
641 pathArray = R.parsePathString(pathArray);
649 if (pathArray[0][0] == "M") {
650 x = +pathArray[0][1];
651 y = +pathArray[0][2];
655 res[0] = ["M", x, y];
657 for (var i = start, ii = pathArray[length]; i < ii; i++) {
660 if (pa[0] != upperCase.call(pa[0])) {
661 r[0] = upperCase.call(pa[0]);
682 for (var j = 1, jj = pa[length]; j < jj; j++) {
683 r[j] = +pa[j] + ((j % 2) ? x : y);
687 for (var k = 0, kk = pa[length]; k < kk; k++) {
703 mx = res[i][res[i][length] - 2];
704 my = res[i][res[i][length] - 1];
706 x = res[i][res[i][length] - 2];
707 y = res[i][res[i][length] - 1];
710 res[toString] = R._path2string;
713 l2c = function (x1, y1, x2, y2) {
714 return [x1, y1, x2, y2, x2, y2];
716 q2c = function (x1, y1, ax, ay, x2, y2) {
728 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
729 // for more information of where this math came from visit:
730 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
732 _120 = PI * 120 / 180,
733 rad = PI / 180 * (+angle || 0),
736 rotate = cacher(function (x, y, rad) {
737 var X = x * math.cos(rad) - y * math.sin(rad),
738 Y = x * math.sin(rad) + y * math.cos(rad);
742 xy = rotate(x1, y1, -rad);
745 xy = rotate(x2, y2, -rad);
748 var cos = math.cos(PI / 180 * angle),
749 sin = math.sin(PI / 180 * angle),
752 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
760 k = (large_arc_flag == sweep_flag ? -1 : 1) *
761 math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
762 cx = k * rx * y / ry + (x1 + x2) / 2,
763 cy = k * -ry * x / rx + (y1 + y2) / 2,
764 f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
765 f2 = math.asin(((y2 - cy) / ry).toFixed(9));
767 f1 = x1 < cx ? PI - f1 : f1;
768 f2 = x2 < cx ? PI - f2 : f2;
769 f1 < 0 && (f1 = PI * 2 + f1);
770 f2 < 0 && (f2 = PI * 2 + f2);
771 if (sweep_flag && f1 > f2) {
774 if (!sweep_flag && f2 > f1) {
784 if (abs(df) > _120) {
788 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
789 x2 = cx + rx * math.cos(f2);
790 y2 = cy + ry * math.sin(f2);
791 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
794 var c1 = math.cos(f1),
798 t = math.tan(df / 4),
802 m2 = [x1 + hx * s1, y1 - hy * c1],
803 m3 = [x2 + hx * s2, y2 - hy * c2],
805 m2[0] = 2 * m1[0] - m2[0];
806 m2[1] = 2 * m1[1] - m2[1];
808 return [m2, m3, m4][concat](res);
810 res = [m2, m3, m4][concat](res)[join]()[split](",");
812 for (var i = 0, ii = res[length]; i < ii; i++) {
813 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
818 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
821 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
822 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
825 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
826 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
827 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
829 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
830 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
834 abs(t1) > "1e12" && (t1 = .5);
835 abs(t2) > "1e12" && (t2 = .5);
836 if (t1 > 0 && t1 < 1) {
837 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
841 if (t2 > 0 && t2 < 1) {
842 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
846 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
847 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
849 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
850 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
851 abs(t1) > "1e12" && (t1 = .5);
852 abs(t2) > "1e12" && (t2 = .5);
853 if (t1 > 0 && t1 < 1) {
854 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
858 if (t2 > 0 && t2 < 1) {
859 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
864 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
865 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
868 path2curve = cacher(function (path, path2) {
869 var p = pathToAbsolute(path),
870 p2 = path2 && pathToAbsolute(path2),
871 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
872 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
873 processPath = function (path, d) {
876 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
878 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
885 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
888 nx = d.x + (d.x - (d.bx || d.x));
889 ny = d.y + (d.y - (d.by || d.y));
890 path = ["C", nx, ny][concat](path.slice(1));
893 d.qx = d.x + (d.x - (d.qx || d.x));
894 d.qy = d.y + (d.y - (d.qy || d.y));
895 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
900 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
903 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
906 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
909 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
912 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
917 fixArc = function (pp, i) {
918 if (pp[i][length] > 7) {
922 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
925 ii = mmax(p[length], p2 && p2[length] || 0);
928 fixM = function (path1, path2, a1, a2, i) {
929 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
930 path2.splice(i, 0, ["M", a2.x, a2.y]);
935 ii = mmax(p[length], p2 && p2[length] || 0);
938 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
939 p[i] = processPath(p[i], attrs);
941 p2 && (p2[i] = processPath(p2[i], attrs2));
943 fixM(p, p2, attrs, attrs2, i);
944 fixM(p2, p, attrs2, attrs, i);
947 seglen = seg[length],
948 seg2len = p2 && seg2[length];
949 attrs.x = seg[seglen - 2];
950 attrs.y = seg[seglen - 1];
951 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
952 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
953 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
954 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
955 attrs2.x = p2 && seg2[seg2len - 2];
956 attrs2.y = p2 && seg2[seg2len - 1];
958 return p2 ? [p, p2] : p;
960 parseDots = cacher(function (gradient) {
962 for (var i = 0, ii = gradient[length]; i < ii; i++) {
964 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
965 dot.color = R.getRGB(par[1]);
966 if (dot.color.error) {
969 dot.color = dot.color.hex;
970 par[2] && (dot.offset = par[2] + "%");
973 for (i = 1, ii = dots[length] - 1; i < ii; i++) {
974 if (!dots[i].offset) {
975 var start = toFloat(dots[i - 1].offset || 0),
977 for (var j = i + 1; j < ii; j++) {
978 if (dots[j].offset) {
979 end = dots[j].offset;
988 var d = (end - start) / (j - i + 1);
991 dots[i].offset = start + "%";
997 getContainer = function (x, y, w, h) {
999 if (R.is(x, string) || R.is(x, "object")) {
1000 container = R.is(x, string) ? doc.getElementById(x) : x;
1001 if (container.tagName) {
1004 container: container,
1005 width: container.style.pixelWidth || container.offsetWidth,
1006 height: container.style.pixelHeight || container.offsetHeight
1009 return {container: container, width: y, height: w};
1013 return {container: 1, x: x, y: y, width: w, height: h};
1016 plugins = function (con, add) {
1018 for (var prop in add) {
1019 if (add[has](prop) && !(prop in con)) {
1020 switch (typeof add[prop]) {
1023 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1027 con[prop] = con[prop] || {};
1028 plugins.call(this, con[prop], add[prop]);
1031 con[prop] = add[prop];
1037 tear = function (el, paper) {
1038 el == paper.top && (paper.top = el.prev);
1039 el == paper.bottom && (paper.bottom = el.next);
1040 el.next && (el.next.prev = el.prev);
1041 el.prev && (el.prev.next = el.next);
1043 tofront = function (el, paper) {
1044 if (paper.top === el) {
1049 el.prev = paper.top;
1050 paper.top.next = el;
1053 toback = function (el, paper) {
1054 if (paper.bottom === el) {
1058 el.next = paper.bottom;
1060 paper.bottom.prev = el;
1063 insertafter = function (el, el2, paper) {
1065 el2 == paper.top && (paper.top = el);
1066 el2.next && (el2.next.prev = el);
1071 insertbefore = function (el, el2, paper) {
1073 el2 == paper.bottom && (paper.bottom = el);
1074 el2.prev && (el2.prev.next = el);
1079 removed = function (methodname) {
1080 return function () {
1081 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1084 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
1085 R.pathToRelative = pathToRelative;
1088 paperproto.svgns = "http://www.w3.org/2000/svg";
1089 paperproto.xlink = "http://www.w3.org/1999/xlink";
1090 round = function (num) {
1091 return +num + (~~num === num) * .5;
1093 var $ = function (el, attr) {
1095 for (var key in attr) {
1096 if (attr[has](key)) {
1097 el[setAttribute](key, Str(attr[key]));
1101 el = doc.createElementNS(paperproto.svgns, el);
1102 el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
1106 R[toString] = function () {
1107 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1109 var thePath = function (pathString, SVG) {
1111 SVG.canvas && SVG.canvas[appendChild](el);
1112 var p = new Element(el, SVG);
1114 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1117 var addGradientFill = function (o, gradient, SVG) {
1118 var type = "linear",
1121 gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
1126 var dir = ((fy > .5) * 2 - 1);
1127 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1128 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1130 (fy = fy.toFixed(5) - 1e-5 * dir);
1134 gradient = gradient[split](/\s*\-\s*/);
1135 if (type == "linear") {
1136 var angle = gradient.shift();
1137 angle = -toFloat(angle);
1141 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
1142 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
1145 if (vector[2] < 0) {
1146 vector[0] = -vector[2];
1149 if (vector[3] < 0) {
1150 vector[1] = -vector[3];
1154 var dots = parseDots(gradient);
1158 var id = o.getAttribute(fillString);
1159 id = id.match(/^url\(#(.*)\)$/);
1160 id && SVG.defs.removeChild(doc.getElementById(id[1]));
1162 var el = $(type + "Gradient");
1163 el.id = createUUID();
1164 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
1165 SVG.defs[appendChild](el);
1166 for (var i = 0, ii = dots[length]; i < ii; i++) {
1167 var stop = $("stop");
1169 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
1170 "stop-color": dots[i].color || "#fff"
1172 el[appendChild](stop);
1175 fill: "url(#" + el.id + ")",
1184 var updatePosition = function (o) {
1185 var bbox = o.getBBox();
1186 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
1188 var setFillAndStroke = function (o, params) {
1195 "-..": [3, 1, 1, 1, 1, 1],
1199 "- .": [4, 3, 1, 3],
1200 "--.": [8, 3, 1, 3],
1201 "--..": [8, 3, 1, 3, 1, 3]
1206 addDashes = function (o, value) {
1207 value = dasharray[lowerCase.call(value)];
1209 var width = o.attrs["stroke-width"] || "1",
1210 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
1212 var i = value[length];
1214 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
1216 $(node, {"stroke-dasharray": dashes[join](",")});
1219 params[has]("rotation") && (rot = params.rotation);
1220 var rotxy = Str(rot)[split](separator);
1221 if (!(rotxy.length - 1)) {
1224 rotxy[1] = +rotxy[1];
1225 rotxy[2] = +rotxy[2];
1227 toFloat(rot) && o.rotate(0, true);
1228 for (var att in params) {
1229 if (params[has](att)) {
1230 if (!availableAttrs[has](att)) {
1233 var value = params[att];
1240 o.rotate(value, true);
1245 var pn = node.parentNode;
1246 if (lowerCase.call(pn.tagName) != "a") {
1248 pn.insertBefore(hl, node);
1249 hl[appendChild](node);
1252 if (att == "target" && value == "blank") {
1253 pn.setAttributeNS(o.paper.xlink, "show", "new");
1255 pn.setAttributeNS(o.paper.xlink, att, value);
1259 node.style.cursor = value;
1262 var rect = Str(value)[split](separator);
1263 if (rect[length] == 4) {
1264 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
1265 var el = $("clipPath"),
1267 el.id = createUUID();
1274 el[appendChild](rc);
1275 o.paper.defs[appendChild](el);
1276 $(node, {"clip-path": "url(#" + el.id + ")"});
1280 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
1281 clip && clip.parentNode.removeChild(clip);
1282 $(node, {"clip-path": E});
1287 if (o.type == "path") {
1288 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
1292 node[setAttribute](att, value);
1301 value = -attrs.x - (attrs.width || 0);
1304 if (att == "rx" && o.type == "rect") {
1308 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
1309 node[setAttribute](att, value);
1310 o.pattern && updatePosition(o);
1313 node[setAttribute](att, value);
1322 value = -attrs.y - (attrs.height || 0);
1325 if (att == "ry" && o.type == "rect") {
1329 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
1330 node[setAttribute](att, value);
1331 o.pattern && updatePosition(o);
1334 if (o.type == "rect") {
1335 $(node, {rx: value, ry: value});
1337 node[setAttribute](att, value);
1341 if (o.type == "image") {
1342 node.setAttributeNS(o.paper.xlink, "href", value);
1345 case "stroke-width":
1346 node.style.strokeWidth = value;
1347 // Need following line for Firefox
1348 node[setAttribute](att, value);
1349 if (attrs["stroke-dasharray"]) {
1350 addDashes(o, attrs["stroke-dasharray"]);
1353 case "stroke-dasharray":
1354 addDashes(o, value);
1357 var xy = Str(value)[split](separator);
1358 xy[0] = +xy[0] || 0;
1359 xy[1] = +xy[1] || 0;
1364 translate.call(o, xy[0], xy[1]);
1367 xy = Str(value)[split](separator);
1368 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
1371 var isURL = Str(value).match(ISURL);
1374 var ig = $("image");
1375 el.id = createUUID();
1376 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
1377 $(ig, {x: 0, y: 0});
1378 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
1379 el[appendChild](ig);
1381 var img = doc.createElement("img");
1382 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
1383 img.onload = function () {
1384 $(el, {width: this.offsetWidth, height: this.offsetHeight});
1385 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
1386 doc.body.removeChild(this);
1389 doc.body[appendChild](img);
1391 o.paper.defs[appendChild](el);
1392 node.style.fill = "url(#" + el.id + ")";
1393 $(node, {fill: "url(#" + el.id + ")"});
1395 o.pattern && updatePosition(o);
1398 var clr = R.getRGB(value);
1400 delete params.gradient;
1401 delete attrs.gradient;
1402 !R.is(attrs.opacity, "undefined") &&
1403 R.is(params.opacity, "undefined") &&
1404 $(node, {opacity: attrs.opacity});
1405 !R.is(attrs["fill-opacity"], "undefined") &&
1406 R.is(params["fill-opacity"], "undefined") &&
1407 $(node, {"fill-opacity": attrs["fill-opacity"]});
1408 } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
1409 attrs.gradient = value;
1410 attrs.fill = "none";
1413 clr[has]("o") && $(node, {"fill-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
1415 clr = R.getRGB(value);
1416 node[setAttribute](att, clr.hex);
1417 att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
1420 (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
1423 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
1424 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
1427 case "fill-opacity":
1428 if (attrs.gradient) {
1429 var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
1431 var stops = gradient.getElementsByTagName("stop");
1432 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
1437 att == "font-size" && (value = toInt(value, 10) + "px");
1438 var cssrule = att[rp](/(\-.)/g, function (w) {
1439 return upperCase.call(w.substring(1));
1441 node.style[cssrule] = value;
1442 // Need following line for Firefox
1443 node[setAttribute](att, value);
1449 tuneText(o, params);
1451 o.rotate(rotxy.join(S));
1453 toFloat(rot) && o.rotate(rot, true);
1457 tuneText = function (el, params) {
1458 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
1463 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
1465 if (params[has]("text")) {
1466 a.text = params.text;
1467 while (node.firstChild) {
1468 node.removeChild(node.firstChild);
1470 var texts = Str(params.text)[split]("\n");
1471 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
1472 var tspan = $("tspan");
1473 i && $(tspan, {dy: fontSize * leading, x: a.x});
1474 tspan[appendChild](doc.createTextNode(texts[i]));
1475 node[appendChild](tspan);
1478 texts = node.getElementsByTagName("tspan");
1479 for (i = 0, ii = texts[length]; i < ii; i++) {
1480 i && $(texts[i], {dy: fontSize * leading, x: a.x});
1484 var bb = el.getBBox(),
1485 dif = a.y - (bb.y + bb.height / 2);
1486 dif && isFinite(dif) && $(node, {y: a.y + dif});
1488 Element = function (node, svg) {
1494 node.raphael = this;
1496 this.attrs = this.attrs || {};
1497 this.transformations = []; // rotate, translate, scale
1501 rt: {deg: 0, cx: 0, cy: 0},
1505 !svg.bottom && (svg.bottom = this);
1506 this.prev = svg.top;
1507 svg.top && (svg.top.next = this);
1511 var elproto = Element[proto];
1512 Element[proto].rotate = function (deg, cx, cy) {
1518 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
1520 return this._.rt.deg;
1522 var bbox = this.getBBox();
1523 deg = Str(deg)[split](separator);
1524 if (deg[length] - 1) {
1525 cx = toFloat(deg[1]);
1526 cy = toFloat(deg[2]);
1528 deg = toFloat(deg[0]);
1529 if (cx != null && cx !== false) {
1530 this._.rt.deg = deg;
1532 this._.rt.deg += deg;
1534 (cy == null) && (cx = null);
1537 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1538 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1539 if (this._.rt.deg) {
1540 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
1541 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
1543 this.transformations[0] = E;
1544 this.clip && $(this.clip, {transform: E});
1546 $(this.node, {transform: this.transformations[join](S)});
1549 Element[proto].hide = function () {
1550 !this.removed && (this.node.style.display = "none");
1553 Element[proto].show = function () {
1554 !this.removed && (this.node.style.display = "");
1557 Element[proto].remove = function () {
1561 tear(this, this.paper);
1562 this.node.parentNode.removeChild(this.node);
1563 for (var i in this) {
1566 this.removed = true;
1568 Element[proto].getBBox = function () {
1572 if (this.type == "path") {
1573 return pathDimensions(this.attrs.path);
1575 if (this.node.style.display == "none") {
1581 bbox = this.node.getBBox();
1583 // Firefox 3.0.x plays badly here
1587 if (this.type == "text") {
1588 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
1589 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1590 var bb = this.node.getExtentOfChar(i);
1591 (bb.y < bbox.y) && (bbox.y = bb.y);
1592 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1593 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
1596 hide && this.hide();
1599 Element[proto].attr = function (name, value) {
1605 for (var i in this.attrs) if (this.attrs[has](i)) {
1606 res[i] = this.attrs[i];
1608 this._.rt.deg && (res.rotation = this.rotate());
1609 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
1610 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1613 if (value == null && R.is(name, string)) {
1614 if (name == "translation") {
1615 return translate.call(this);
1617 if (name == "rotation") {
1618 return this.rotate();
1620 if (name == "scale") {
1621 return this.scale();
1623 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
1624 return this.attrs.gradient;
1626 return this.attrs[name];
1628 if (value == null && R.is(name, array)) {
1630 for (var j = 0, jj = name.length; j < jj; j++) {
1631 values[name[j]] = this.attr(name[j]);
1635 if (value != null) {
1637 params[name] = value;
1638 } else if (name != null && R.is(name, "object")) {
1641 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1642 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
1643 this.attrs[key] = params[key];
1644 for (var subkey in par) if (par[has](subkey)) {
1645 params[subkey] = par[subkey];
1648 setFillAndStroke(this, params);
1651 Element[proto].toFront = function () {
1655 this.node.parentNode[appendChild](this.node);
1656 var svg = this.paper;
1657 svg.top != this && tofront(this, svg);
1660 Element[proto].toBack = function () {
1664 if (this.node.parentNode.firstChild != this.node) {
1665 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1666 toback(this, this.paper);
1667 var svg = this.paper;
1671 Element[proto].insertAfter = function (element) {
1675 var node = element.node || element[element.length - 1].node;
1676 if (node.nextSibling) {
1677 node.parentNode.insertBefore(this.node, node.nextSibling);
1679 node.parentNode[appendChild](this.node);
1681 insertafter(this, element, this.paper);
1684 Element[proto].insertBefore = function (element) {
1688 var node = element.node || element[0].node;
1689 node.parentNode.insertBefore(this.node, node);
1690 insertbefore(this, element, this.paper);
1693 Element[proto].blur = function (size) {
1694 // Experimental. No Safari support. Use it on your own risk.
1697 var fltr = $("filter"),
1698 blur = $("feGaussianBlur");
1699 t.attrs.blur = size;
1700 fltr.id = createUUID();
1701 $(blur, {stdDeviation: +size || 1.5});
1702 fltr.appendChild(blur);
1703 t.paper.defs.appendChild(fltr);
1705 $(t.node, {filter: "url(#" + fltr.id + ")"});
1708 t._blur.parentNode.removeChild(t._blur);
1710 delete t.attrs.blur;
1712 t.node.removeAttribute("filter");
1715 var theCircle = function (svg, x, y, r) {
1716 var el = $("circle");
1717 svg.canvas && svg.canvas[appendChild](el);
1718 var res = new Element(el, svg);
1719 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1720 res.type = "circle";
1724 theRect = function (svg, x, y, w, h, r) {
1726 svg.canvas && svg.canvas[appendChild](el);
1727 var res = new Element(el, svg);
1728 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1733 theEllipse = function (svg, x, y, rx, ry) {
1734 var el = $("ellipse");
1735 svg.canvas && svg.canvas[appendChild](el);
1736 var res = new Element(el, svg);
1737 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1738 res.type = "ellipse";
1742 theImage = function (svg, src, x, y, w, h) {
1743 var el = $("image");
1744 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1745 el.setAttributeNS(svg.xlink, "href", src);
1746 svg.canvas && svg.canvas[appendChild](el);
1747 var res = new Element(el, svg);
1748 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1752 theText = function (svg, x, y, text) {
1754 $(el, {x: x, y: y, "text-anchor": "middle"});
1755 svg.canvas && svg.canvas[appendChild](el);
1756 var res = new Element(el, svg);
1757 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
1759 setFillAndStroke(res, res.attrs);
1762 setSize = function (width, height) {
1763 this.width = width || this.width;
1764 this.height = height || this.height;
1765 this.canvas[setAttribute]("width", this.width);
1766 this.canvas[setAttribute]("height", this.height);
1769 create = function () {
1770 var con = getContainer[apply](0, arguments),
1771 container = con && con.container,
1775 height = con.height;
1777 throw new Error("SVG container not found.");
1779 var cnvs = $("svg");
1782 width = width || 512;
1783 height = height || 342;
1785 xmlns: "http://www.w3.org/2000/svg",
1790 if (container == 1) {
1791 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
1792 doc.body[appendChild](cnvs);
1794 if (container.firstChild) {
1795 container.insertBefore(cnvs, container.firstChild);
1797 container[appendChild](cnvs);
1800 container = new Paper;
1801 container.width = width;
1802 container.height = height;
1803 container.canvas = cnvs;
1804 plugins.call(container, container, R.fn);
1808 paperproto.clear = function () {
1809 var c = this.canvas;
1810 while (c.firstChild) {
1811 c.removeChild(c.firstChild);
1813 this.bottom = this.top = null;
1814 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
1815 c[appendChild](this.desc);
1816 c[appendChild](this.defs = $("defs"));
1818 paperproto.remove = function () {
1819 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1820 for (var i in this) {
1821 this[i] = removed(i);
1828 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
1829 bites = /([clmz]),?([^clmz]*)/gi,
1830 blurregexp = / progid:\S+Blur\([^\)]+\)/g,
1831 val = /-?[^,\s-]+/g,
1832 coordsize = 1e3 + S + 1e3,
1834 pathlike = {path: 1, rect: 1},
1835 path2vml = function (path) {
1836 var total = /[ahqstv]/ig,
1837 command = pathToAbsolute;
1838 Str(path).match(total) && (command = path2curve);
1840 if (command == pathToAbsolute && !Str(path).match(total)) {
1841 var res = Str(path)[rp](bites, function (all, command, args) {
1843 isMove = lowerCase.call(command) == "m",
1845 args[rp](val, function (value) {
1846 if (isMove && vals[length] == 2) {
1847 res += vals + map[command == "m" ? "l" : "L"];
1850 vals[push](round(value * zoom));
1856 var pa = command(path), p, r;
1858 for (var i = 0, ii = pa[length]; i < ii; i++) {
1860 r = lowerCase.call(pa[i][0]);
1861 r == "z" && (r = "x");
1862 for (var j = 1, jj = p[length]; j < jj; j++) {
1863 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
1867 return res[join](S);
1870 R[toString] = function () {
1871 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
1873 thePath = function (pathString, vml) {
1874 var g = createNode("group");
1875 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
1876 g.coordsize = vml.coordsize;
1877 g.coordorigin = vml.coordorigin;
1878 var el = createNode("shape"), ol = el.style;
1879 ol.width = vml.width + "px";
1880 ol.height = vml.height + "px";
1881 el.coordsize = coordsize;
1882 el.coordorigin = vml.coordorigin;
1884 var p = new Element(el, g, vml),
1885 attr = {fill: "none", stroke: "#000"};
1886 pathString && (attr.path = pathString);
1890 setFillAndStroke(p, attr);
1891 vml.canvas[appendChild](g);
1894 setFillAndStroke = function (o, params) {
1895 o.attrs = o.attrs || {};
1900 newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
1903 for (var par in params) if (params[has](par)) {
1904 a[par] = params[par];
1907 a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
1913 params.href && (node.href = params.href);
1914 params.title && (node.title = params.title);
1915 params.target && (node.target = params.target);
1916 params.cursor && (s.cursor = params.cursor);
1917 "blur" in params && o.blur(params.blur);
1918 if (params.path && o.type == "path" || newpath) {
1919 node.path = path2vml(a.path);
1921 if (params.rotation != null) {
1922 o.rotate(params.rotation, true);
1924 if (params.translation) {
1925 xy = Str(params.translation)[split](separator);
1926 translate.call(o, xy[0], xy[1]);
1927 if (o._.rt.cx != null) {
1928 o._.rt.cx +=+ xy[0];
1929 o._.rt.cy +=+ xy[1];
1930 o.setBox(o.attrs, xy[0], xy[1]);
1934 xy = Str(params.scale)[split](separator);
1935 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1937 if ("clip-rect" in params) {
1938 var rect = Str(params["clip-rect"])[split](separator);
1939 if (rect[length] == 4) {
1940 rect[2] = +rect[2] + (+rect[0]);
1941 rect[3] = +rect[3] + (+rect[1]);
1942 var div = node.clipRect || doc.createElement("div"),
1944 group = node.parentNode;
1945 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
1946 if (!node.clipRect) {
1947 dstyle.position = "absolute";
1950 dstyle.width = o.paper.width + "px";
1951 dstyle.height = o.paper.height + "px";
1952 group.parentNode.insertBefore(div, group);
1953 div[appendChild](group);
1954 node.clipRect = div;
1957 if (!params["clip-rect"]) {
1958 node.clipRect && (node.clipRect.style.clip = E);
1961 if (o.type == "image" && params.src) {
1962 node.src = params.src;
1964 if (o.type == "image" && params.opacity) {
1965 node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
1966 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
1968 params.font && (s.font = params.font);
1969 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
1970 params["font-size"] && (s.fontSize = params["font-size"]);
1971 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1972 params["font-style"] && (s.fontStyle = params["font-style"]);
1973 if (params.opacity != null ||
1974 params["stroke-width"] != null ||
1975 params.fill != null ||
1976 params.stroke != null ||
1977 params["stroke-width"] != null ||
1978 params["stroke-opacity"] != null ||
1979 params["fill-opacity"] != null ||
1980 params["stroke-dasharray"] != null ||
1981 params["stroke-miterlimit"] != null ||
1982 params["stroke-linejoin"] != null ||
1983 params["stroke-linecap"] != null) {
1984 node = o.shape || node;
1985 var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
1987 !fill && (newfill = fill = createNode(fillString));
1988 if ("fill-opacity" in params || "opacity" in params) {
1989 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
1990 opacity = mmin(mmax(opacity, 0), 1);
1991 fill.opacity = opacity;
1993 params.fill && (fill.on = true);
1994 if (fill.on == null || params.fill == "none") {
1997 if (fill.on && params.fill) {
1998 var isURL = params.fill.match(ISURL);
2000 fill.src = isURL[1];
2003 fill.color = R.getRGB(params.fill).hex;
2005 fill.type = "solid";
2006 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
2008 a.gradient = params.fill;
2012 newfill && node[appendChild](fill);
2013 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
2015 !stroke && (newstroke = stroke = createNode("stroke"));
2016 if ((params.stroke && params.stroke != "none") ||
2017 params["stroke-width"] ||
2018 params["stroke-opacity"] != null ||
2019 params["stroke-dasharray"] ||
2020 params["stroke-miterlimit"] ||
2021 params["stroke-linejoin"] ||
2022 params["stroke-linecap"]) {
2025 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
2026 var strokeColor = R.getRGB(params.stroke);
2027 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
2028 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
2029 var width = (toFloat(params["stroke-width"]) || 1) * .75;
2030 opacity = mmin(mmax(opacity, 0), 1);
2031 params["stroke-width"] == null && (width = a["stroke-width"]);
2032 params["stroke-width"] && (stroke.weight = width);
2033 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
2034 stroke.opacity = opacity;
2036 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
2037 stroke.miterlimit = params["stroke-miterlimit"] || 8;
2038 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
2039 if (params["stroke-dasharray"]) {
2043 "-.": "shortdashdot",
2044 "-..": "shortdashdotdot",
2049 "--.": "longdashdot",
2050 "--..": "longdashdotdot"
2052 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
2054 newstroke && node[appendChild](stroke);
2056 if (res.type == "text") {
2057 s = res.paper.span.style;
2058 a.font && (s.font = a.font);
2059 a["font-family"] && (s.fontFamily = a["font-family"]);
2060 a["font-size"] && (s.fontSize = a["font-size"]);
2061 a["font-weight"] && (s.fontWeight = a["font-weight"]);
2062 a["font-style"] && (s.fontStyle = a["font-style"]);
2063 res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/</g, "<")[rp](/&/g, "&")[rp](/\n/g, "<br>"));
2064 res.W = a.w = res.paper.span.offsetWidth;
2065 res.H = a.h = res.paper.span.offsetHeight;
2067 res.Y = a.y + round(res.H / 2);
2069 // text-anchor emulationm
2070 switch (a["text-anchor"]) {
2072 res.node.style["v-text-align"] = "left";
2073 res.bbx = round(res.W / 2);
2076 res.node.style["v-text-align"] = "right";
2077 res.bbx = -round(res.W / 2);
2080 res.node.style["v-text-align"] = "center";
2085 addGradientFill = function (o, gradient) {
2086 o.attrs = o.attrs || {};
2087 var attrs = o.attrs,
2091 o.attrs.gradient = gradient;
2092 gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
2097 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
2102 gradient = gradient[split](/\s*\-\s*/);
2103 if (type == "linear") {
2104 var angle = gradient.shift();
2105 angle = -toFloat(angle);
2110 var dots = parseDots(gradient);
2114 o = o.shape || o.node;
2115 fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
2116 !fill.parentNode && o.appendChild(fill);
2119 fill.method = "none";
2120 fill.color = dots[0].color;
2121 fill.color2 = dots[dots[length] - 1].color;
2123 for (var i = 0, ii = dots[length]; i < ii; i++) {
2124 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
2126 fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
2127 if (type == "radial") {
2128 fill.type = "gradientradial";
2129 fill.focus = "100%";
2130 fill.focussize = fxfy;
2131 fill.focusposition = fxfy;
2133 fill.type = "gradient";
2134 fill.angle = (270 - angle) % 360;
2139 Element = function (node, group, vml) {
2147 node.raphael = this;
2160 !vml.bottom && (vml.bottom = this);
2161 this.prev = vml.top;
2162 vml.top && (vml.top.next = this);
2166 elproto = Element[proto];
2167 elproto.rotate = function (deg, cx, cy) {
2173 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
2175 return this._.rt.deg;
2177 deg = Str(deg)[split](separator);
2178 if (deg[length] - 1) {
2179 cx = toFloat(deg[1]);
2180 cy = toFloat(deg[2]);
2182 deg = toFloat(deg[0]);
2184 this._.rt.deg = deg;
2186 this._.rt.deg += deg;
2188 cy == null && (cx = null);
2191 this.setBox(this.attrs, cx, cy);
2192 this.Group.style.rotation = this._.rt.deg;
2193 // gradient fix for rotation. TODO
2194 // var fill = (this.shape || this.node).getElementsByTagName(fillString);
2195 // fill = fill[0] || {};
2196 // var b = ((360 - this._.rt.deg) - 270) % 360;
2197 // !R.is(fill.angle, "undefined") && (fill.angle = b);
2200 elproto.setBox = function (params, cx, cy) {
2204 var gs = this.Group.style,
2205 os = (this.shape && this.shape.style) || this.node.style;
2206 params = params || {};
2207 for (var i in params) if (params[has](i)) {
2208 this.attrs[i] = params[i];
2210 cx = cx || this._.rt.cx;
2211 cy = cy || this._.rt.cy;
2212 var attr = this.attrs,
2217 switch (this.type) {
2219 x = attr.cx - attr.r;
2220 y = attr.cy - attr.r;
2224 x = attr.cx - attr.rx;
2225 y = attr.cy - attr.ry;
2232 w = attr.width || 0;
2233 h = attr.height || 0;
2236 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
2237 x = attr.x - round(this.W / 2);
2238 y = attr.y - this.H / 2;
2244 if (!this.attrs.path) {
2247 w = this.paper.width;
2248 h = this.paper.height;
2250 var dim = pathDimensions(this.attrs.path);
2260 w = this.paper.width;
2261 h = this.paper.height;
2264 cx = (cx == null) ? x + w / 2 : cx;
2265 cy = (cy == null) ? y + h / 2 : cy;
2266 var left = cx - this.paper.width / 2,
2267 top = cy - this.paper.height / 2, t;
2268 gs.left != (t = left + "px") && (gs.left = t);
2269 gs.top != (t = top + "px") && (gs.top = t);
2270 this.X = pathlike[has](this.type) ? -left : x;
2271 this.Y = pathlike[has](this.type) ? -top : y;
2274 if (pathlike[has](this.type)) {
2275 os.left != (t = -left * zoom + "px") && (os.left = t);
2276 os.top != (t = -top * zoom + "px") && (os.top = t);
2277 } else if (this.type == "text") {
2278 os.left != (t = -left + "px") && (os.left = t);
2279 os.top != (t = -top + "px") && (os.top = t);
2281 gs.width != (t = this.paper.width + "px") && (gs.width = t);
2282 gs.height != (t = this.paper.height + "px") && (gs.height = t);
2283 os.left != (t = x - left + "px") && (os.left = t);
2284 os.top != (t = y - top + "px") && (os.top = t);
2285 os.width != (t = w + "px") && (os.width = t);
2286 os.height != (t = h + "px") && (os.height = t);
2289 elproto.hide = function () {
2290 !this.removed && (this.Group.style.display = "none");
2293 elproto.show = function () {
2294 !this.removed && (this.Group.style.display = "block");
2297 elproto.getBBox = function () {
2301 if (pathlike[has](this.type)) {
2302 return pathDimensions(this.attrs.path);
2305 x: this.X + (this.bbx || 0),
2311 elproto.remove = function () {
2315 tear(this, this.paper);
2316 this.node.parentNode.removeChild(this.node);
2317 this.Group.parentNode.removeChild(this.Group);
2318 this.shape && this.shape.parentNode.removeChild(this.shape);
2319 for (var i in this) {
2322 this.removed = true;
2324 elproto.attr = function (name, value) {
2330 for (var i in this.attrs) if (this.attrs[has](i)) {
2331 res[i] = this.attrs[i];
2333 this._.rt.deg && (res.rotation = this.rotate());
2334 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
2335 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2338 if (value == null && R.is(name, "string")) {
2339 if (name == "translation") {
2340 return translate.call(this);
2342 if (name == "rotation") {
2343 return this.rotate();
2345 if (name == "scale") {
2346 return this.scale();
2348 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2349 return this.attrs.gradient;
2351 return this.attrs[name];
2353 if (this.attrs && value == null && R.is(name, array)) {
2354 var ii, values = {};
2355 for (i = 0, ii = name[length]; i < ii; i++) {
2356 values[name[i]] = this.attr(name[i]);
2361 if (value != null) {
2363 params[name] = value;
2365 value == null && R.is(name, "object") && (params = name);
2367 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2368 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2369 this.attrs[key] = params[key];
2370 for (var subkey in par) if (par[has](subkey)) {
2371 params[subkey] = par[subkey];
2374 if (params.text && this.type == "text") {
2375 this.node.string = params.text;
2377 setFillAndStroke(this, params);
2378 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
2379 addGradientFill(this, params.gradient);
2381 (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
2385 elproto.toFront = function () {
2386 !this.removed && this.Group.parentNode[appendChild](this.Group);
2387 this.paper.top != this && tofront(this, this.paper);
2390 elproto.toBack = function () {
2394 if (this.Group.parentNode.firstChild != this.Group) {
2395 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2396 toback(this, this.paper);
2400 elproto.insertAfter = function (element) {
2404 if (element.constructor == Set) {
2405 element = element[element.length - 1];
2407 if (element.Group.nextSibling) {
2408 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2410 element.Group.parentNode[appendChild](this.Group);
2412 insertafter(this, element, this.paper);
2415 elproto.insertBefore = function (element) {
2419 if (element.constructor == Set) {
2420 element = element[0];
2422 element.Group.parentNode.insertBefore(this.Group, element.Group);
2423 insertbefore(this, element, this.paper);
2426 elproto.blur = function (size) {
2427 var s = this.node.runtimeStyle,
2429 f = f.replace(blurregexp, E);
2431 this.attrs.blur = size;
2432 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
2433 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
2437 delete this.attrs.blur;
2441 theCircle = function (vml, x, y, r) {
2442 var g = createNode("group"),
2443 o = createNode("oval"),
2445 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2446 g.coordsize = coordsize;
2447 g.coordorigin = vml.coordorigin;
2449 var res = new Element(o, g, vml);
2450 res.type = "circle";
2451 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2455 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2456 vml.canvas[appendChild](g);
2459 function rectPath(x, y, w, h, r) {
2461 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);
2463 return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
2466 theRect = function (vml, x, y, w, h, r) {
2467 var path = rectPath(x, y, w, h, r),
2468 res = vml.path(path),
2472 res.W = a.width = w;
2473 res.H = a.height = h;
2479 theEllipse = function (vml, x, y, rx, ry) {
2480 var g = createNode("group"),
2481 o = createNode("oval"),
2483 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2484 g.coordsize = coordsize;
2485 g.coordorigin = vml.coordorigin;
2487 var res = new Element(o, g, vml);
2488 res.type = "ellipse";
2489 setFillAndStroke(res, {stroke: "#000"});
2494 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2495 vml.canvas[appendChild](g);
2498 theImage = function (vml, src, x, y, w, h) {
2499 var g = createNode("group"),
2500 o = createNode("image");
2501 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2502 g.coordsize = coordsize;
2503 g.coordorigin = vml.coordorigin;
2506 var res = new Element(o, g, vml);
2508 res.attrs.src = src;
2513 res.setBox({x: x, y: y, width: w, height: h});
2514 vml.canvas[appendChild](g);
2517 theText = function (vml, x, y, text) {
2518 var g = createNode("group"),
2519 el = createNode("shape"),
2521 path = createNode("path"),
2523 o = createNode("textpath");
2524 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
2525 g.coordsize = coordsize;
2526 g.coordorigin = vml.coordorigin;
2527 path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
2528 path.textpathok = true;
2529 ol.width = vml.width;
2530 ol.height = vml.height;
2531 o.string = Str(text);
2534 el[appendChild](path);
2536 var res = new Element(o, g, vml);
2538 res.textpath = path;
2540 res.attrs.text = text;
2545 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2547 vml.canvas[appendChild](g);
2550 setSize = function (width, height) {
2551 var cs = this.canvas.style;
2552 width == +width && (width += "px");
2553 height == +height && (height += "px");
2556 cs.clip = "rect(0 " + width + " " + height + " 0)";
2560 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2562 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2563 createNode = function (tagName) {
2564 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2567 createNode = function (tagName) {
2568 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2571 create = function () {
2572 var con = getContainer[apply](0, arguments),
2573 container = con.container,
2574 height = con.height,
2580 throw new Error("VML container not found.");
2582 var res = new Paper,
2583 c = res.canvas = doc.createElement("div"),
2587 width = width || 512;
2588 height = height || 342;
2589 width == +width && (width += "px");
2590 height == +height && (height += "px");
2593 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
2594 res.coordorigin = "0 0";
2595 res.span = doc.createElement("span");
2596 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2597 c[appendChild](res.span);
2598 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);
2599 if (container == 1) {
2600 doc.body[appendChild](c);
2603 cs.position = "absolute";
2605 if (container.firstChild) {
2606 container.insertBefore(c, container.firstChild);
2608 container[appendChild](c);
2611 plugins.call(res, res, R.fn);
2614 paperproto.clear = function () {
2615 this.canvas.innerHTML = E;
2616 this.span = doc.createElement("span");
2617 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
2618 this.canvas[appendChild](this.span);
2619 this.bottom = this.top = null;
2621 paperproto.remove = function () {
2622 this.canvas.parentNode.removeChild(this.canvas);
2623 for (var i in this) {
2624 this[i] = removed(i);
2631 // WebKit rendering bug workaround method
2632 var version = navigator.userAgent.match(/Version\/(.*?)\s/);
2633 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP")) {
2634 paperproto.safari = function () {
2635 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
2636 win.setTimeout(function () {rect.remove();});
2639 paperproto.safari = function () {};
2643 var preventDefault = function () {
2644 this.returnValue = false;
2646 preventTouch = function () {
2647 return this.originalEvent.preventDefault();
2649 stopPropagation = function () {
2650 this.cancelBubble = true;
2652 stopTouch = function () {
2653 return this.originalEvent.stopPropagation();
2655 addEvent = (function () {
2656 if (doc.addEventListener) {
2657 return function (obj, type, fn, element) {
2658 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
2659 var f = function (e) {
2660 if (supportsTouch && touchMap[has](type)) {
2661 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
2662 if (e.targetTouches[i].target == obj) {
2664 e = e.targetTouches[i];
2665 e.originalEvent = olde;
2666 e.preventDefault = preventTouch;
2667 e.stopPropagation = stopTouch;
2672 return fn.call(element, e);
2674 obj.addEventListener(realName, f, false);
2675 return function () {
2676 obj.removeEventListener(realName, f, false);
2680 } else if (doc.attachEvent) {
2681 return function (obj, type, fn, element) {
2682 var f = function (e) {
2684 e.preventDefault = e.preventDefault || preventDefault;
2685 e.stopPropagation = e.stopPropagation || stopPropagation;
2686 return fn.call(element, e);
2688 obj.attachEvent("on" + type, f);
2689 var detacher = function () {
2690 obj.detachEvent("on" + type, f);
2698 dragMove = function (e) {
2701 scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2702 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft,
2707 if (supportsTouch) {
2708 var i = e.touches.length,
2711 touch = e.touches[i];
2712 if (touch.identifier == dragi.el._drag.id) {
2715 (e.originalEvent ? e.originalEvent : e).preventDefault();
2724 dragi.move && dragi.move.call(dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y);
2727 dragUp = function () {
2728 R.unmousemove(dragMove).unmouseup(dragUp);
2729 var i = drag.length,
2733 dragi.el._drag = {};
2734 dragi.end && dragi.end.call(dragi.el);
2738 for (var i = events[length]; i--;) {
2739 (function (eventName) {
2740 R[eventName] = Element[proto][eventName] = function (fn) {
2741 if (R.is(fn, "function")) {
2742 this.events = this.events || [];
2743 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
2747 R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
2748 var events = this.events,
2750 while (l--) if (events[l].name == eventName && events[l].f == fn) {
2752 events.splice(l, 1);
2753 !events.length && delete this.events;
2760 elproto.hover = function (f_in, f_out) {
2761 return this.mouseover(f_in).mouseout(f_out);
2763 elproto.unhover = function (f_in, f_out) {
2764 return this.unmouseover(f_in).unmouseout(f_out);
2766 elproto.drag = function (onmove, onstart, onend) {
2768 this.mousedown(function (e) {
2769 (e.originalEvent || e).preventDefault();
2770 var scrollY = doc.documentElement.scrollTop || doc.body.scrollTop,
2771 scrollX = doc.documentElement.scrollLeft || doc.body.scrollLeft;
2772 this._drag.x = e.clientX + scrollX;
2773 this._drag.y = e.clientY + scrollY;
2774 this._drag.id = e.identifier;
2775 onstart && onstart.call(this, e.clientX + scrollX, e.clientY + scrollY);
2776 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
2777 drag.push({el: this, move: onmove, end: onend});
2781 elproto.undrag = function (onmove, onstart, onend) {
2782 var i = drag.length;
2784 drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i++, 1);
2786 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
2788 paperproto.circle = function (x, y, r) {
2789 return theCircle(this, x || 0, y || 0, r || 0);
2791 paperproto.rect = function (x, y, w, h, r) {
2792 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
2794 paperproto.ellipse = function (x, y, rx, ry) {
2795 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
2797 paperproto.path = function (pathString) {
2798 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
2799 return thePath(R.format[apply](R, arguments), this);
2801 paperproto.image = function (src, x, y, w, h) {
2802 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
2804 paperproto.text = function (x, y, text) {
2805 return theText(this, x || 0, y || 0, Str(text));
2807 paperproto.set = function (itemsArray) {
2808 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
2809 return new Set(itemsArray);
2811 paperproto.setSize = setSize;
2812 paperproto.top = paperproto.bottom = null;
2813 paperproto.raphael = R;
2815 return this.x + S + this.y;
2817 elproto.resetScale = function () {
2823 this.attrs.scale = "1 1";
2825 elproto.scale = function (x, y, cx, cy) {
2829 if (x == null && y == null) {
2844 var bb = this.getBBox(),
2845 rcx = bb.x + bb.width / 2,
2846 rcy = bb.y + bb.height / 2,
2847 kx = abs(x / this._.sx),
2848 ky = abs(y / this._.sy);
2849 cx = (+cx || cx == 0) ? cx : rcx;
2850 cy = (+cy || cy == 0) ? cy : rcy;
2851 var posx = this._.sx > 0,
2852 posy = this._.sy > 0,
2853 dirx = ~~(x / abs(x)),
2854 diry = ~~(y / abs(y)),
2857 s = this.node.style,
2858 ncx = cx + abs(rcx - cx) * dkx * (rcx > cx == posx ? 1 : -1),
2859 ncy = cy + abs(rcy - cy) * dky * (rcy > cy == posy ? 1 : -1),
2860 fr = (x * dirx > y * diry ? ky : kx);
2861 switch (this.type) {
2864 var neww = a.width * kx,
2865 newh = a.height * ky;
2891 var path = pathToRelative(a.path),
2893 fx = posx ? dkx : kx,
2894 fy = posy ? dky : ky;
2895 for (var i = 0, ii = path[length]; i < ii; i++) {
2897 P0 = upperCase.call(p[0]);
2898 if (P0 == "M" && skip) {
2904 p[path[i][length] - 2] *= fx;
2905 p[path[i][length] - 1] *= fy;
2908 p[5] = +(dirx + diry ? !+p[4] : !!+p[4]);
2909 } else if (P0 == "H") {
2910 for (var j = 1, jj = p[length]; j < jj; j++) {
2913 } else if (P0 == "V") {
2914 for (j = 1, jj = p[length]; j < jj; j++) {
2918 for (j = 1, jj = p[length]; j < jj; j++) {
2919 p[j] *= (j % 2) ? fx : fy;
2923 var dim2 = pathDimensions(path);
2924 dx = ncx - dim2.x - dim2.width / 2;
2925 dy = ncy - dim2.y - dim2.height / 2;
2928 this.attr({path: path});
2931 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2932 if (this.transformations) {
2933 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
2934 this.node[setAttribute]("transform", this.transformations[join](S));
2935 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2936 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2937 this.attr({x: dx, y: dy});
2941 this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
2942 ", M12=0, M21=0, M22=", diry,
2943 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2944 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2947 if (this.transformations) {
2948 this.transformations[2] = E;
2949 this.node[setAttribute]("transform", this.transformations[join](S));
2953 this.node.filterMatrix = E;
2954 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
2957 a.scale = [x, y, cx, cy][join](S);
2963 elproto.clone = function () {
2967 var attr = this.attr();
2969 delete attr.translation;
2970 return this.paper[this.type]().attr(attr);
2972 var curveslengths = {},
2973 getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
2976 name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
2977 cache = curveslengths[name],
2979 !cache && (curveslengths[name] = cache = {data: []});
2980 cache.timer && clearTimeout(cache.timer);
2981 cache.timer = setTimeout(function () {delete curveslengths[name];}, 2000);
2982 if (length != null) {
2983 var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
2984 precision = ~~total * 10;
2986 for (var i = 0; i < precision + 1; i++) {
2987 if (cache.data[length] > i) {
2988 dot = cache.data[i * precision];
2990 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
2991 cache.data[i] = dot;
2993 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
2994 if (length != null && len >= length) {
2999 if (length == null) {
3003 getLengthFactory = function (istotal, subpath) {
3004 return function (path, length, onlystart) {
3005 path = path2curve(path);
3006 var x, y, p, l, sp = "", subpaths = {}, point,
3008 for (var i = 0, ii = path.length; i < ii; i++) {
3014 l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
3015 if (len + l > length) {
3016 if (subpath && !subpaths.start) {
3017 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3018 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
3019 if (onlystart) {return sp;}
3020 subpaths.start = sp;
3021 sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
3027 if (!istotal && !subpath) {
3028 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
3029 return {x: point.x, y: point.y, alpha: point.alpha};
3039 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
3040 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
3044 var getTotalLength = getLengthFactory(1),
3045 getPointAtLength = getLengthFactory(),
3046 getSubpathsAtLength = getLengthFactory(0, 1);
3047 elproto.getTotalLength = function () {
3048 if (this.type != "path") {return;}
3049 if (this.node.getTotalLength) {
3050 return this.node.getTotalLength();
3052 return getTotalLength(this.attrs.path);
3054 elproto.getPointAtLength = function (length) {
3055 if (this.type != "path") {return;}
3056 return getPointAtLength(this.attrs.path, length);
3058 elproto.getSubpath = function (from, to) {
3059 if (this.type != "path") {return;}
3060 if (abs(this.getTotalLength() - to) < "1e-6") {
3061 return getSubpathsAtLength(this.attrs.path, from).end;
3063 var a = getSubpathsAtLength(this.attrs.path, to, 1);
3064 return from ? getSubpathsAtLength(a, from).end : a;
3067 // animation easing formulas
3068 R.easing_formulas = {
3069 linear: function (n) {
3076 return pow(n - 1, 3) + 1;
3078 "<>": function (n) {
3081 return pow(n, 3) / 2;
3084 return (pow(n, 3) + 2) / 2;
3086 backIn: function (n) {
3088 return n * n * ((s + 1) * n - s);
3090 backOut: function (n) {
3093 return n * n * ((s + 1) * n + s) + 1;
3095 elastic: function (n) {
3096 if (n == 0 || n == 1) {
3101 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
3103 bounce: function (n) {
3112 l = s * n * n + .75;
3114 if (n < (2.5 / p)) {
3116 l = s * n * n + .9375;
3119 l = s * n * n + .984375;
3127 var animationElements = [],
3128 animation = function () {
3129 var Now = +new Date;
3130 for (var l = 0; l < animationElements[length]; l++) {
3131 var e = animationElements[l];
3132 if (e.stop || e.el.removed) {
3135 var time = Now - e.start,
3146 var pos = easing(time / ms);
3147 for (var attr in from) if (from[has](attr)) {
3148 switch (availableAnimAttrs[attr]) {
3150 now = pos * ms * diff[attr];
3151 to.back && (now = to.len - now);
3152 var point = getPointAtLength(to[attr], now);
3153 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
3156 that.translate(point.x - diff.sx, point.y - diff.sy);
3157 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3160 now = +from[attr] + pos * ms * diff[attr];
3164 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
3165 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
3166 upto255(round(from[attr].b + pos * ms * diff[attr].b))
3171 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3172 now[i] = [from[attr][i][0]];
3173 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3174 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
3176 now[i] = now[i][join](S);
3183 var x = pos * ms * diff[attr][0] - t.x,
3184 y = pos * ms * diff[attr][1] - t.y;
3190 now = +from[attr][0] + pos * ms * diff[attr][0];
3191 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
3194 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);
3200 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
3206 var from2 = [].concat(from[attr]);
3208 i = that.paper.customAttributes[attr].length;
3210 now[i] = +from2[i] + pos * ms * diff[attr][i];
3217 that._run && that._run.call(that);
3220 point = getPointAtLength(to.along, to.len * !to.back);
3221 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
3222 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
3224 (t.x || t.y) && that.translate(-t.x, -t.y);
3225 to.scale && (to.scale += E);
3227 animationElements.splice(l--, 1);
3230 R.svg && that && that.paper && that.paper.safari();
3231 animationElements[length] && setTimeout(animation);
3233 keyframesRun = function (attr, element, time, prev, prevcallback) {
3234 var dif = time - prev;
3235 element.timeouts.push(setTimeout(function () {
3236 R.is(prevcallback, "function") && prevcallback.call(element);
3237 element.animate(attr, dif, attr.easing);
3240 upto255 = function (color) {
3241 return mmax(mmin(color, 255), 0);
3243 translate = function (x, y) {
3245 return {x: this._.tx, y: this._.ty, toString: x_y};
3249 switch (this.type) {
3252 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
3257 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
3260 var path = pathToRelative(this.attrs.path);
3263 this.attr({path: path});
3268 elproto.animateWith = function (element, params, ms, easing, callback) {
3269 for (var i = 0, ii = animationElements.length; i < ii; i++) {
3270 if (animationElements[i].el.id == element.id) {
3271 params.start = animationElements[i].start;
3274 return this.animate(params, ms, easing, callback);
3276 elproto.animateAlong = along();
3277 elproto.animateAlongBack = along(1);
3278 function along(isBack) {
3279 return function (path, ms, rotate, callback) {
3280 var params = {back: isBack};
3281 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
3282 path && path.constructor == Element && (path = path.attrs.path);
3283 path && (params.along = path);
3284 return this.animate(params, ms, callback);
3287 function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
3289 bx = 3 * (p2x - p1x) - cx,
3292 by = 3 * (p2y - p1y) - cy,
3294 function sampleCurveX(t) {
3295 return ((ax * t + bx) * t + cx) * t;
3297 function solve(x, epsilon) {
3298 var t = solveCurveX(x, epsilon);
3299 return ((ay * t + by) * t + cy) * t;
3301 function solveCurveX(x, epsilon) {
3302 var t0, t1, t2, x2, d2, i;
3303 for(t2 = x, i = 0; i < 8; i++) {
3304 x2 = sampleCurveX(t2) - x;
3305 if (abs(x2) < epsilon) {
3308 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
3309 if (abs(d2) < 1e-6) {
3324 x2 = sampleCurveX(t2);
3325 if (abs(x2 - x) < epsilon) {
3333 t2 = (t1 - t0) / 2 + t0;
3337 return solve(t, 1 / (200 * duration));
3339 elproto.onAnimation = function (f) {
3343 elproto.animate = function (params, ms, easing, callback) {
3345 element.timeouts = element.timeouts || [];
3346 if (R.is(easing, "function") || !easing) {
3347 callback = easing || null;
3349 if (element.removed) {
3350 callback && callback.call(element);
3355 animateable = false,
3357 for (var attr in params) if (params[has](attr)) {
3358 if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
3360 from[attr] = element.attr(attr);
3361 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
3362 to[attr] = params[attr];
3363 switch (availableAnimAttrs[attr]) {
3365 var len = getTotalLength(params[attr]);
3366 var point = getPointAtLength(params[attr], len * !!params.back);
3367 var bb = element.getBBox();
3368 diff[attr] = len / ms;
3373 to.rot = params.rot;
3374 to.back = params.back;
3376 params.rot && (diff.r = toFloat(element.rotate()) || 0);
3379 diff[attr] = (to[attr] - from[attr]) / ms;
3382 from[attr] = R.getRGB(from[attr]);
3383 var toColour = R.getRGB(to[attr]);
3385 r: (toColour.r - from[attr].r) / ms,
3386 g: (toColour.g - from[attr].g) / ms,
3387 b: (toColour.b - from[attr].b) / ms
3391 var pathes = path2curve(from[attr], to[attr]);
3392 from[attr] = pathes[0];
3393 var toPath = pathes[1];
3395 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
3396 diff[attr][i] = [0];
3397 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
3398 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
3403 var values = Str(params[attr])[split](separator),
3404 from2 = Str(from[attr])[split](separator);
3407 from[attr] = [0, 0];
3408 diff[attr] = [values[0] / ms, values[1] / ms];
3411 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
3412 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
3415 params[attr] = values;
3416 from[attr] = Str(from[attr])[split](separator);
3417 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
3420 from[attr] = Str(from[attr])[split](separator);
3424 diff[attr][i] = (values[i] - from[attr][i]) / ms;
3431 values = [].concat(params[attr]);
3432 from2 = [].concat(from[attr]);
3434 i = element.paper.customAttributes[attr][length];
3436 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
3445 for (var key in params) if (params[has](key) && animKeyFrames.test(key)) {
3446 attr = {value: params[key]};
3447 key == "from" && (key = 0);
3448 key == "to" && (key = 100);
3449 attr.key = toInt(key, 10);
3452 attrs.sort(sortByKey);
3454 attrs.unshift({key: 0, value: element.attrs});
3456 for (i = 0, ii = attrs[length]; i < ii; i++) {
3457 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);
3459 lastcall = attrs[attrs[length] - 1].value.callback;
3461 element.timeouts.push(setTimeout(function () {lastcall.call(element);}, ms));
3464 var easyeasy = R.easing_formulas[easing];
3466 easyeasy = Str(easing).match(bezierrg);
3467 if (easyeasy && easyeasy[length] == 5) {
3468 var curve = easyeasy;
3469 easyeasy = function (t) {
3470 return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
3473 easyeasy = function (t) {
3478 animationElements.push({
3479 start: params.start || +new Date,
3488 R.is(callback, "function") && (element._ac = setTimeout(function () {
3489 callback.call(element);
3491 animationElements[length] == 1 && setTimeout(animation);
3495 elproto.stop = function () {
3496 for (var i = 0; i < animationElements.length; i++) {
3497 animationElements[i].el.id == this.id && animationElements.splice(i--, 1);
3499 for (i = 0, ii = this.timeouts && this.timeouts.length; i < ii; i++) {
3500 clearTimeout(this.timeouts[i]);
3503 clearTimeout(this._ac);
3507 elproto.translate = function (x, y) {
3508 return this.attr({translation: x + " " + y});
3510 elproto[toString] = function () {
3511 return "Rapha\xebl\u2019s object";
3513 R.ae = animationElements;
3516 var Set = function (items) {
3521 for (var i = 0, ii = items[length]; i < ii; i++) {
3522 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
3523 this[this.items[length]] = this.items[this.items[length]] = items[i];
3529 Set[proto][push] = function () {
3532 for (var i = 0, ii = arguments[length]; i < ii; i++) {
3533 item = arguments[i];
3534 if (item && (item.constructor == Element || item.constructor == Set)) {
3535 len = this.items[length];
3536 this[len] = this.items[len] = item;
3542 Set[proto].pop = function () {
3543 delete this[this[length]--];
3544 return this.items.pop();
3546 for (var method in elproto) if (elproto[has](method)) {
3547 Set[proto][method] = (function (methodname) {
3548 return function () {
3549 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3550 this.items[i][methodname][apply](this.items[i], arguments);
3556 Set[proto].attr = function (name, value) {
3557 if (name && R.is(name, array) && R.is(name[0], "object")) {
3558 for (var j = 0, jj = name[length]; j < jj; j++) {
3559 this.items[j].attr(name[j]);
3562 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3563 this.items[i].attr(name, value);
3568 Set[proto].animate = function (params, ms, easing, callback) {
3569 (R.is(easing, "function") || !easing) && (callback = easing || null);
3570 var len = this.items[length],
3575 callback && (collector = function () {
3576 !--len && callback.call(set);
3578 easing = R.is(easing, string) ? easing : collector;
3579 item = this.items[--i].animate(params, ms, easing, collector);
3581 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, params, ms, easing, collector);
3585 Set[proto].insertAfter = function (el) {
3586 var i = this.items[length];
3588 this.items[i].insertAfter(el);
3592 Set[proto].getBBox = function () {
3597 for (var i = this.items[length]; i--;) {
3598 var box = this.items[i].getBBox();
3601 w[push](box.x + box.width);
3602 h[push](box.y + box.height);
3604 x = mmin[apply](0, x);
3605 y = mmin[apply](0, y);
3609 width: mmax[apply](0, w) - x,
3610 height: mmax[apply](0, h) - y
3613 Set[proto].clone = function (s) {
3615 for (var i = 0, ii = this.items[length]; i < ii; i++) {
3616 s[push](this.items[i].clone());
3621 R.registerFont = function (font) {
3625 this.fonts = this.fonts || {};
3631 family = font.face["font-family"];
3632 for (var prop in font.face) if (font.face[has](prop)) {
3633 fontcopy.face[prop] = font.face[prop];
3635 if (this.fonts[family]) {
3636 this.fonts[family][push](fontcopy);
3638 this.fonts[family] = [fontcopy];
3641 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
3642 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
3643 var path = font.glyphs[glyph];
3644 fontcopy.glyphs[glyph] = {
3647 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
3648 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
3652 for (var k in path.k) if (path[has](k)) {
3653 fontcopy.glyphs[glyph].k[k] = path.k[k];
3660 paperproto.getFont = function (family, weight, style, stretch) {
3661 stretch = stretch || "normal";
3662 style = style || "normal";
3663 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
3667 var font = R.fonts[family];
3669 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
3670 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
3671 if (name.test(fontName)) {
3672 font = R.fonts[fontName];
3679 for (var i = 0, ii = font[length]; i < ii; i++) {
3681 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
3688 paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
3689 origin = origin || "middle"; // baseline|middle
3690 letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
3691 var out = this.set(),
3692 letters = Str(string)[split](E),
3696 R.is(font, string) && (font = this.getFont(font));
3698 scale = (size || 16) / font.face["units-per-em"];
3699 var bb = font.face.bbox.split(separator),
3701 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
3702 for (var i = 0, ii = letters[length]; i < ii; i++) {
3703 var prev = i && font.glyphs[letters[i - 1]] || {},
3704 curr = font.glyphs[letters[i]];
3705 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
3706 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
3708 out.scale(scale, scale, top, height).translate(x - top, y - height);
3713 R.format = function (token, params) {
3714 var args = R.is(params, array) ? [0][concat](params) : arguments;
3715 token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
3716 return args[++i] == null ? E : args[i];
3720 R.ninja = function () {
3721 oldRaphael.was ? (win.Raphael = oldRaphael.is) : delete Raphael;
3727 oldRaphael.was ? (win.Raphael = R) : (Raphael = R);