2 * Raphael 0.8 - JavaScript Vector Library
4 * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 window.Raphael = (function () {
10 var separator = /[, ]+/,
14 was: "Raphael" in window,
18 return create.apply(R, arguments);
21 availableAttrs = {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},
22 availableAnimAttrs = {cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "csv", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"},
23 events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
25 R.type = (window.SVGAngle || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
26 R.svg = !(R.vml = R.type == "VML");
29 R.toString = function () {
30 return "Your browser " + (this.vml ? "doesn't ": "") + "support" + (this.svg ? "s": "") +
31 " SVG.\nYou are running " + unescape("Rapha%EBl%20") + this.version;
33 R.setWindow = function (newwin) {
38 R.hsb2rgb = function (hue, saturation, brightness) {
39 if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
47 if (brightness == 0) {
48 return {r: 0, g: 0, b: 0, hex: "#000"};
50 if (hue > 1 || saturation > 1 || brightness > 1) {
55 var i = Math.floor(hue * 6),
57 p = brightness * (1 - saturation),
58 q = brightness * (1 - (saturation * f)),
59 t = brightness * (1 - (saturation * (1 - f)));
60 red = [brightness, q, p, p, t, brightness, brightness][i];
61 green = [t, brightness, brightness, q, p, p, t][i];
62 blue = [p, p, t, brightness, brightness, q, p][i];
66 var rgb = {r: red, g: green, b: blue};
67 var r = Math.round(red).toString(16);
71 var g = Math.round(green).toString(16);
75 var b = Math.round(blue).toString(16);
79 rgb.hex = "#" + r + g + b;
82 R.rgb2hsb = function (red, green, blue) {
83 if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
88 if (typeof red == "string") {
89 var clr = getRGB(red);
94 if (red > 1 || green > 1 || blue > 1) {
99 var max = Math.max(red, green, blue),
100 min = Math.min(red, green, blue),
105 return {h: 0, s: 0, b: max};
107 var delta = (max - min);
108 saturation = delta / max;
110 hue = (green - blue) / delta;
111 } else if (green == max) {
112 hue = 2 + ((blue - red) / delta);
114 hue = 4 + ((red - green) / delta);
124 return {h: hue, s: saturation, b: brightness};
126 var getRGB = function (colour) {
127 var htmlcolors = {aliceblue: "#f0f8ff", amethyst: "#96c", antiquewhite: "#faebd7", aqua: "#0ff", aquamarine: "#7fffd4", azure: "#f0ffff", beige: "#f5f5dc", bisque: "#ffe4c4", black: "#000", blanchedalmond: "#ffebcd", blue: "#00f", blueviolet: "#8a2be2", brown: "#a52a2a", burlywood: "#deb887", cadetblue: "#5f9ea0", chartreuse: "#7fff00", chocolate: "#d2691e", coral: "#ff7f50", cornflowerblue: "#6495ed", cornsilk: "#fff8dc", crimson: "#dc143c", cyan: "#0ff", darkblue: "#00008b", darkcyan: "#008b8b", darkgoldenrod: "#b8860b", darkgray: "#a9a9a9", darkgreen: "#006400", darkkhaki: "#bdb76b", darkmagenta: "#8b008b", darkolivegreen: "#556b2f", darkorange: "#ff8c00", darkorchid: "#9932cc", darkred: "#8b0000", darksalmon: "#e9967a", darkseagreen: "#8fbc8f", darkslateblue: "#483d8b", darkslategray: "#2f4f4f", darkturquoise: "#00ced1", darkviolet: "#9400d3", deeppink: "#ff1493", deepskyblue: "#00bfff", dimgray: "#696969", dodgerblue: "#1e90ff", firebrick: "#b22222", floralwhite: "#fffaf0", forestgreen: "#228b22", fuchsia: "#f0f", gainsboro: "#dcdcdc", ghostwhite: "#f8f8ff", gold: "#ffd700", goldenrod: "#daa520", gray: "#808080", green: "#008000", greenyellow: "#adff2f", honeydew: "#f0fff0", hotpink: "#ff69b4", indianred: "#cd5c5c", indigo: "#4b0082", ivory: "#fffff0", khaki: "#f0e68c", lavender: "#e6e6fa", lavenderblush: "#fff0f5", lawngreen: "#7cfc00", lemonchiffon: "#fffacd", lightblue: "#add8e6", lightcoral: "#f08080", lightcyan: "#e0ffff", lightgoldenrodyellow: "#fafad2", lightgreen: "#90ee90", lightgrey: "#d3d3d3", lightpink: "#ffb6c1", lightsalmon: "#ffa07a", lightsalmon: "#ffa07a", lightseagreen: "#20b2aa", lightskyblue: "#87cefa", lightslategray: "#789", lightsteelblue: "#b0c4de", lightyellow: "#ffffe0", lime: "#0f0", limegreen: "#32cd32", linen: "#faf0e6", magenta: "#f0f", maroon: "#800000", mediumaquamarine: "#66cdaa", mediumblue: "#0000cd", mediumorchid: "#ba55d3", mediumpurple: "#9370db", mediumseagreen: "#3cb371", mediumslateblue: "#7b68ee", mediumslateblue: "#7b68ee", mediumspringgreen: "#00fa9a", mediumturquoise: "#48d1cc", mediumvioletred: "#c71585", midnightblue: "#191970", mintcream: "#f5fffa", mistyrose: "#ffe4e1", moccasin: "#ffe4b5", navajowhite: "#ffdead", navy: "#000080", oldlace: "#fdf5e6", olive: "#808000", olivedrab: "#6b8e23", orange: "#ffa500", orangered: "#ff4500", orchid: "#da70d6", palegoldenrod: "#eee8aa", palegreen: "#98fb98", paleturquoise: "#afeeee", palevioletred: "#db7093", papayawhip: "#ffefd5", peachpuff: "#ffdab9", peru: "#cd853f", pink: "#ffc0cb", plum: "#dda0dd", powderblue: "#b0e0e6", purple: "#800080", red: "#f00", rosybrown: "#bc8f8f", royalblue: "#4169e1", saddlebrown: "#8b4513", salmon: "#fa8072", sandybrown: "#f4a460", seagreen: "#2e8b57", seashell: "#fff5ee", sienna: "#a0522d", silver: "#c0c0c0", skyblue: "#87ceeb", slateblue: "#6a5acd", slategray: "#708090", snow: "#fffafa", springgreen: "#00ff7f", steelblue: "#4682b4", tan: "#d2b48c", teal: "#008080", thistle: "#d8bfd8", tomato: "#ff6347", turquoise: "#40e0d0", violet: "#ee82ee", wheat: "#f5deb3", white: "#fff", whitesmoke: "#f5f5f5", yellow: "#ff0", yellowgreen: "#9acd32"};
128 if (colour.toString().toLowerCase() in htmlcolors) {
129 colour = htmlcolors[colour.toString().toLowerCase()];
132 return {r: 0, g: 0, b: 0, hex: "#000"};
134 if (colour == "none") {
135 return {r: -1, g: -1, b: -1, hex: "none"};
137 var red, green, blue,
138 rgb = colour.match(/^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hsb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i);
141 blue = parseInt(rgb[2].substring(5), 16);
142 green = parseInt(rgb[2].substring(3, 5), 16);
143 red = parseInt(rgb[2].substring(1, 3), 16);
146 blue = parseInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
147 green = parseInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
148 red = parseInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
151 rgb = rgb[4].split(/\s*,\s*/);
152 red = parseFloat(rgb[0]);
153 green = parseFloat(rgb[1]);
154 blue = parseFloat(rgb[2]);
157 rgb = rgb[5].split(/\s*,\s*/);
158 red = parseFloat(rgb[0]) * 2.55;
159 green = parseFloat(rgb[1]) * 2.55;
160 blue = parseFloat(rgb[2]) * 2.55;
163 rgb = rgb[6].split(/\s*,\s*/);
164 red = parseFloat(rgb[0]);
165 green = parseFloat(rgb[1]);
166 blue = parseFloat(rgb[2]);
167 return R.hsb2rgb(red, green, blue);
170 rgb = rgb[7].split(/\s*,\s*/);
171 red = parseFloat(rgb[0]) * 2.55;
172 green = parseFloat(rgb[1]) * 2.55;
173 blue = parseFloat(rgb[2]) * 2.55;
174 return R.hsb2rgb(red, green, blue);
176 var rgb = {r: red, g: green, b: blue};
177 var r = Math.round(red).toString(16);
178 (r.length == 1) && (r = "0" + r);
179 var g = Math.round(green).toString(16);
180 (g.length == 1) && (g = "0" + g);
181 var b = Math.round(blue).toString(16);
182 (b.length == 1) && (b = "0" + b);
183 rgb.hex = "#" + r + g + b;
186 return {r: -1, g: -1, b: -1, hex: "none"};
189 R.getColor = function (value) {
190 var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
191 var rgb = this.hsb2rgb(start.h, start.s, start.b);
197 arguments.callee.start = {h: 0, s: 1, b: start.b};
202 R.getColor.reset = function () {
206 R.parsePathString = function (pathString) {
207 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
209 toString = function () {
211 for (var i = 0, ii = this.length; i < ii; i++) {
212 res += this[i][0] + this[i].join(",").substring(2);
216 if (pathString.toString.toString() == toString.toString()) {
219 pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
220 var params = [], name = b.toLowerCase();
221 c.replace(/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
222 b && params.push(+b);
224 while (params.length >= paramCounts[name]) {
225 data.push([b].concat(params.splice(0, paramCounts[name])));
226 if (!paramCounts[name]) {
231 data.toString = toString;
234 var pathDimensions = function (path) {
235 var pathArray = path;
236 if (typeof path == "string") {
237 pathArray = R.parsePathString(path);
239 pathArray = pathToAbsolute(pathArray);
240 var x = [], y = [], length = 0;
241 for (var i = 0, ii = pathArray.length; i < ii; i++) {
242 switch (pathArray[i][0]) {
246 x.push(pathArray[i][pathArray[i].length - 2]);
247 y.push(pathArray[i][pathArray[i].length - 1]);
250 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
252 x.push(pathArray[i][j]);
254 y.push(pathArray[i][j]);
259 var minx = Math.min.apply(Math, x),
260 miny = Math.min.apply(Math, y);
264 width: Math.max.apply(Math, x) - minx,
265 height: Math.max.apply(Math, y) - miny,
270 var pathToRelative = function (pathArray) {
272 if (typeof pathArray == "string") {
273 pathArray = R.parsePathString(pathArray);
275 var x = 0, y = 0, start = 0;
276 if (pathArray[0][0] == "M") {
280 res.push(pathArray[0]);
282 for (var i = start, ii = pathArray.length; i < ii; i++) {
284 if (pathArray[i][0] != pathArray[i][0].toLowerCase()) {
285 res[i][0] = pathArray[i][0].toLowerCase();
288 res[i][1] = pathArray[i][1];
289 res[i][2] = pathArray[i][2];
291 res[i][4] = pathArray[i][4];
292 res[i][5] = pathArray[i][5];
293 res[i][6] = +(pathArray[i][6] - x).toFixed(3);
294 res[i][7] = +(pathArray[i][7] - y).toFixed(3);
297 res[i][1] = +(pathArray[i][1] - y).toFixed(3);
300 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
301 res[i][j] = +(pathArray[i][j] - ((j % 2) ? x : y)).toFixed(3);
305 res[i] = pathArray[i];
311 x += res[i][res[i].length - 1];
314 y += res[i][res[i].length - 1];
317 x += res[i][res[i].length - 2];
318 y += res[i][res[i].length - 1];
321 res.toString = pathArray.toString;
324 var pathToAbsolute = function (pathArray) {
326 if (typeof pathArray == "string") {
327 pathArray = R.parsePathString(pathArray);
332 if (pathArray[0][0] == "M") {
333 x = +pathArray[0][1];
334 y = +pathArray[0][2];
336 res[0] = pathArray[0];
338 for (var i = start, ii = pathArray.length; i < ii; i++) {
340 if (pathArray[i][0] != (pathArray[i][0] + "").toUpperCase()) {
341 res[i][0] = (pathArray[i][0] + "").toUpperCase();
344 res[i][1] = pathArray[i][1];
345 res[i][2] = pathArray[i][2];
347 res[i][4] = pathArray[i][4];
348 res[i][5] = pathArray[i][5];
349 res[i][6] = +(pathArray[i][6] + x).toFixed(3);
350 res[i][7] = +(pathArray[i][7] + y).toFixed(3);
353 res[i][1] = +pathArray[i][1] + y;
356 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
357 res[i][j] = +pathArray[i][j] + ((j % 2) ? x : y);
361 res[i] = pathArray[i];
373 x = res[i][res[i].length - 2];
374 y = res[i][res[i].length - 1];
377 res.toString = pathArray.toString;
380 var pathEqualiser = function (path1, path2) {
381 var data = [pathToAbsolute(R.parsePathString(path1)), pathToAbsolute(R.parsePathString(path2))],
382 attrs = [{x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0}, {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0}],
383 processPath = function (path, d) {
393 var nx = d.x + (d.x - (d.bx || d.x));
394 var ny = d.y + (d.y - (d.by || d.y));
395 path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
398 var nx = d.x + (d.x - (d.bx || d.x));
399 var ny = d.y + (d.y - (d.by || d.y));
400 path = ["Q", nx, ny, path[1], path[2]];
403 path = ["L", path[1], d.y];
406 path = ["L", d.x, path[1]];
409 path = ["L", d.X, d.Y];
414 edgeCases = function (a, b, i) {
415 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
416 data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
417 attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
418 attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
419 attrs[a].x = data[a][i][data[a][i].length - 2];
420 attrs[a].y = data[a][i][data[a][i].length - 1];
422 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
423 data[a][i] = ["C", attrs[a].x, attrs[a].y, data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
424 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
425 data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
426 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
427 var x = data[b][i][data[b][i].length - 2];
428 var y = data[b][i][data[b][i].length - 1];
429 data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
430 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
432 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
433 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
434 attrs[b].x = data[b][i][data[b][i].length - 2];
435 attrs[b].y = data[b][i][data[b][i].length - 1];
437 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
438 var x = data[b][i][data[b][i].length - 2];
439 var y = data[b][i][data[b][i].length - 1];
440 data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
441 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
443 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
444 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
445 attrs[b].x = data[b][i][data[b][i].length - 2];
446 attrs[b].y = data[b][i][data[b][i].length - 1];
448 } else if (data[a][i][0] == "U") {
449 data[a][i][0] = data[b][i][0];
450 for (var j = 1, jj = data[b][i].length; j < jj; j++) {
451 data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
456 for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
457 data[0][i] = processPath(data[0][i], attrs[0]);
458 data[1][i] = processPath(data[1][i], attrs[1]);
459 if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
462 attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
463 attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
464 attrs[0].x = data[0][i][data[0][i].length - 2];
465 attrs[0].y = data[0][i][data[0][i].length - 1];
466 attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
467 attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
468 attrs[1].x = data[1][i][data[1][i].length - 2];
469 attrs[1].y = data[1][i][data[1][i].length - 1];
473 var toGradient = function (gradient) {
474 if (typeof gradient == "string") {
475 gradient = gradient.split(/\s*\-\s*/);
476 var angle = gradient.shift();
477 if (angle.toLowerCase() == "v") {
479 } else if (angle.toLowerCase() == "h") {
482 angle = parseFloat(angle);
485 var grobj = {angle: angle, type: "linear", dots: [], vector: [0, 0, Math.cos(angle * Math.PI / 180).toFixed(3), Math.sin(angle * Math.PI / 180).toFixed(3)]};
486 var max = 1 / (Math.max(Math.abs(grobj.vector[2]), Math.abs(grobj.vector[3])) || 1);
487 grobj.vector[2] *= max;
488 grobj.vector[3] *= max;
489 if (grobj.vector[2] < 0) {
490 grobj.vector[0] = -grobj.vector[2];
493 if (grobj.vector[3] < 0) {
494 grobj.vector[1] = -grobj.vector[3];
497 grobj.vector[0] = grobj.vector[0].toFixed(3);
498 grobj.vector[1] = grobj.vector[1].toFixed(3);
499 grobj.vector[2] = grobj.vector[2].toFixed(3);
500 grobj.vector[3] = grobj.vector[3].toFixed(3);
501 for (var i = 0, ii = gradient.length; i < ii; i++) {
503 var par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
504 dot.color = getRGB(par[1]).hex;
505 par[2] && (dot.offset = par[2] + "%");
506 grobj.dots.push(dot);
508 for (var i = 1, ii = grobj.dots.length - 1; i < ii; i++) {
509 if (!grobj.dots[i].offset) {
510 var start = parseFloat(grobj.dots[i - 1].offset || 0),
512 for (var j = i + 1; j < ii; j++) {
513 if (grobj.dots[j].offset) {
514 end = grobj.dots[j].offset;
522 end = parseFloat(end);
523 var d = (end - start) / (j - i + 1);
526 grobj.dots[i].offset = start + "%";
535 var getContainer = function () {
536 var container, x, y, width, height;
537 if (typeof arguments[0] == "string" || typeof arguments[0] == "object") {
538 if (typeof arguments[0] == "string") {
539 container = doc.getElementById(arguments[0]);
541 container = arguments[0];
543 if (container.tagName) {
544 if (arguments[1] == null) {
546 container: container,
547 width: container.style.pixelWidth || container.offsetWidth,
548 height: container.style.pixelHeight || container.offsetHeight
551 return {container: container, width: arguments[1], height: arguments[2]};
554 } else if (typeof arguments[0] == "number" && arguments.length > 3) {
555 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
558 var plugins = function (con, scope, add) {
559 for (var prop in add) if (add.hasOwnProperty(prop) && !(prop in con)) {
560 switch (typeof add[prop]) {
562 con[prop] = con === scope ? add[prop] : function () { add[prop].apply(scope, arguments); };
566 plugins(con[prop], con, add[prop]);
569 con[prop] = add[prop];
577 var thePath = function (params, pathString, SVG) {
578 var el = doc.createElementNS(SVG.svgns, "path");
579 el.setAttribute("fill", "none");
581 SVG.canvas.appendChild(el);
583 var p = new Element(el, SVG);
586 p.last = {x: 0, y: 0, bx: 0, by: 0};
587 p.absolutely = function () {
588 this.isAbsolute = true;
591 p.relatively = function () {
592 this.isAbsolute = false;
595 p.moveTo = function (x, y) {
596 var d = this.isAbsolute?"M":"m";
597 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
598 var oldD = this[0].getAttribute("d") || "";
599 (oldD == "M0,0") && (oldD = "");
600 this[0].setAttribute("d", oldD + d);
601 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
602 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
603 this.attrs.path = oldD + d;
606 p.lineTo = function (x, y) {
607 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
608 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
609 var d = this.isAbsolute?"L":"l";
610 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
611 var oldD = this[0].getAttribute("d") || "";
612 this[0].setAttribute("d", oldD + d);
613 this.attrs.path = oldD + d;
616 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
617 var d = this.isAbsolute ? "A" : "a";
618 d += [parseFloat(rx).toFixed(3), parseFloat(ry).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x).toFixed(3), parseFloat(y).toFixed(3)].join(" ");
619 var oldD = this[0].getAttribute("d") || "";
620 this[0].setAttribute("d", oldD + d);
621 this.last.x = parseFloat(x);
622 this.last.y = parseFloat(y);
623 this.attrs.path = oldD + d;
626 p.cplineTo = function (x1, y1, w1) {
628 return this.lineTo(x1, y1);
631 var x = parseFloat(x1);
632 var y = parseFloat(y1);
633 var w = parseFloat(w1);
634 var d = this.isAbsolute?"C":"c";
635 var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
636 for (var i = 0, ii = attr.length; i < ii; i++) {
637 d += attr[i].toFixed(3) + " ";
639 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
640 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
641 this.last.bx = attr[2];
642 this.last.by = attr[3];
643 var oldD = this[0].getAttribute("d") || "";
644 this[0].setAttribute("d", oldD + d);
645 this.attrs.path = oldD + d;
649 p.curveTo = function () {
651 command = [0, 1, 2, 3, "s", 5, "c"];
653 var d = command[arguments.length];
654 if (this.isAbsolute) {
657 for (var i = 0, ii = arguments.length; i < ii; i++) {
658 d += parseFloat(arguments[i]).toFixed(3) + " ";
660 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
661 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
662 this.last.bx = parseFloat(arguments[arguments.length - 4]);
663 this.last.by = parseFloat(arguments[arguments.length - 3]);
664 var oldD = this.node.getAttribute("d") || "";
665 this.node.setAttribute("d", oldD + d);
666 this.attrs.path = oldD + d;
669 p.qcurveTo = function () {
671 command = [0, 1, "t", 3, "q"];
673 var d = command[arguments.length];
674 if (this.isAbsolute) {
677 for (var i = 0, ii = arguments.length; i < ii; i++) {
678 d += parseFloat(arguments[i]).toFixed(3) + " ";
680 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
681 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
682 if (arguments.length != 2) {
683 this.last.qx = parseFloat(arguments[arguments.length - 4]);
684 this.last.qy = parseFloat(arguments[arguments.length - 3]);
686 var oldD = this.node.getAttribute("d") || "";
687 this.node.setAttribute("d", oldD + d);
688 this.attrs.path = oldD + d;
691 p.addRoundedCorner = function (r, dir) {
692 var R = .5522 * r, rollback = this.isAbsolute, o = this;
695 rollback = function () {
699 rollback = function () {};
705 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
708 o.curveTo(-R, 0, -r, r - R, -r, r);
715 o.curveTo(R, 0, r, -(r - R), r, -r);
718 o.curveTo(R, 0, r, r - R, r, r);
725 o.curveTo(0, -R, -(R - r), -r, r, -r);
728 o.curveTo(0, -R, R - r, -r, -r, -r);
735 o.curveTo(0, R, -(R - r), r, r, r);
738 o.curveTo(0, R, R - r, r, -r, r);
743 actions[dir[0]]()[dir[1]]();
747 p.andClose = function () {
748 var oldD = this[0].getAttribute("d") || "";
749 this[0].setAttribute("d", oldD + "Z ");
750 this.attrs.path = oldD + "Z ";
754 p.attrs.path = "" + pathString;
756 paper.pathfinder(p, p.attrs.path);
759 setFillAndStroke(p, params);
763 var addGrdientFill = function (o, gradient, SVG) {
764 gradient = toGradient(gradient);
765 var el = doc.createElementNS(SVG.svgns, (gradient.type || "linear") + "Gradient");
766 el.id = "raphael-gradient-" + R.idGenerator++;
767 if (gradient.vector && gradient.vector.length) {
768 el.setAttribute("x1", gradient.vector[0]);
769 el.setAttribute("y1", gradient.vector[1]);
770 el.setAttribute("x2", gradient.vector[2]);
771 el.setAttribute("y2", gradient.vector[3]);
773 SVG.defs.appendChild(el);
774 var isopacity = true;
775 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
776 var stop = doc.createElementNS(SVG.svgns, "stop");
777 if (gradient.dots[i].offset) {
780 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
781 stop.setAttribute("stop-color", getRGB(gradient.dots[i].color).hex || "#fff");
782 // ignoring opacity for internal points, because VML doesn't support it
783 el.appendChild(stop);
785 if (isopacity && typeof gradient.dots[ii - 1].opacity != "undefined") {
786 stop.setAttribute("stop-opacity", gradient.dots[ii - 1].opacity);
788 o.setAttribute("fill", "url(#" + el.id + ")");
790 o.style.fillOpacity = 1;
791 o.setAttribute("opacity", 1);
792 o.setAttribute("fill-opacity", 1);
794 var updatePosition = function (o) {
796 var bbox = o.getBBox();
797 o.pattern.setAttribute("patternTransform", "translate(" + [bbox.x, bbox.y].join(",") + ")");
800 var setFillAndStroke = function (o, params) {
805 "-..": [3, 1, 1, 1, 1, 1],
811 "--..": [8, 3, 1, 3, 1, 3]
813 addDashes = function (o, value) {
814 value = dasharray[value.toString().toLowerCase()];
816 var width = o.attrs["stroke-width"] || "1",
817 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
819 for (var i = 0, ii = value.length; i < ii; i++) {
820 dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
822 value = dashes.join(",");
823 o.node.setAttribute("stroke-dasharray", value);
826 for (var att in params) {
827 if (!(att in availableAttrs)) {
830 var value = params[att];
831 o.attrs[att] = value;
837 var pn = o.node.parentNode;
838 if (pn.tagName.toLowerCase() != "a") {
839 var hl = doc.createElementNS(o.svg.svgns, "a");
840 pn.insertBefore(hl, o.node);
841 hl.appendChild(o.node);
844 pn.setAttributeNS(o.svg.xlink, att, value);
847 if (o.type == "path") {
848 o.node.setAttribute("d", "M0,0");
849 paper.pathfinder(o, value);
854 o.node.setAttribute(att, value);
860 o.node.setAttribute(att, value);
864 o.node.setAttribute(att, value);
867 o.node.setAttribute(att, value);
870 if (o.type == "rect") {
871 o.node.setAttribute("rx", value);
872 o.node.setAttribute("ry", value);
874 o.node.setAttribute(att, value);
878 if (o.type == "image") {
879 o.node.setAttributeNS(o.svg.xlink, "href", value);
883 o.node.style.strokeWidth = value;
884 // Need following line for Firefox
885 o.node.setAttribute(att, value);
886 if (o.attrs["stroke-dasharray"]) {
887 addDashes(o, o.attrs["stroke-dasharray"]);
890 case "stroke-dasharray":
894 o.rotate(value, true);
897 var xy = (value + "").split(separator);
898 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
901 var xy = (value + "").split(separator);
902 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1);
905 var isURL = value.match(/^url\(([^\)]+)\)$/i);
907 var el = doc.createElementNS(o.svg.svgns, "pattern");
908 var ig = doc.createElementNS(o.svg.svgns, "image");
909 el.id = "raphael-pattern-" + R.idGenerator++;
910 el.setAttribute("x", 0);
911 el.setAttribute("y", 0);
912 el.setAttribute("patternUnits", "userSpaceOnUse");
913 ig.setAttribute("x", 0);
914 ig.setAttribute("y", 0);
915 ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
918 var img = doc.createElement("img");
919 img.style.position = "absolute";
920 img.style.top = "-9999em";
921 img.style.left = "-9999em";
922 img.onload = function () {
923 el.setAttribute("width", this.offsetWidth);
924 el.setAttribute("height", this.offsetHeight);
925 ig.setAttribute("width", this.offsetWidth);
926 ig.setAttribute("height", this.offsetHeight);
927 doc.body.removeChild(this);
930 doc.body.appendChild(img);
932 o.svg.defs.appendChild(el);
933 o.node.style.fill = "url(#" + el.id + ")";
934 o.node.setAttribute("fill", "url(#" + el.id + ")");
939 delete params.gradient;
940 delete o.attrs.gradient;
941 if (typeof o.attrs.opacity != "undefined" && typeof params.opacity == "undefined" ) {
942 o.node.style.opacity = o.attrs.opacity;
943 // Need following line for Firefox
944 o.node.setAttribute("opacity", o.attrs.opacity);
946 if (typeof o.attrs["fill-opacity"] != "undefined" && typeof params["fill-opacity"] == "undefined" ) {
947 o.node.style.fillOpacity = o.attrs["fill-opacity"];
948 // Need following line for Firefox
949 o.node.setAttribute("fill-opacity", o.attrs["fill-opacity"]);
952 o.node.style[att] = getRGB(value).hex;
953 // Need following line for Firefox
954 o.node.setAttribute(att, getRGB(value).hex);
957 addGrdientFill(o.node, value, o.svg);
961 if (o.attrs.gradient) {
962 var gradient = doc.getElementById(o.node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
964 var stops = gradient.getElementsByTagName("stop");
965 stops[stops.length - 1].setAttribute("stop-opacity", value);
970 var cssrule = att.replace(/(\-.)/g, function (w) {
971 return w.substring(1).toUpperCase();
973 o.node.style[cssrule] = value;
974 // Need following line for Firefox
975 o.node.setAttribute(att, value);
982 var tuneText = function (el, params) {
983 if (el.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params)) {
986 var fontSize = el.node.firstChild ? parseInt(doc.defaultView.getComputedStyle(el.node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
988 if ("text" in params) {
989 while (el.node.firstChild) {
990 el.node.removeChild(el.node.firstChild);
992 var texts = (params.text + "").split("\n");
993 for (var i = 0, ii = texts.length; i < ii; i++) {
994 var tspan = doc.createElementNS(el.svg.svgns, "tspan");
995 i && tspan.setAttribute("dy", fontSize * leading);
996 i && tspan.setAttribute("x", el.attrs.x);
997 tspan.appendChild(doc.createTextNode(texts[i]));
998 el.node.appendChild(tspan);
1001 var texts = el.node.getElementsByTagName("tspan");
1002 for (var i = 0, ii = texts.length; i < ii; i++) {
1003 i && texts[i].setAttribute("dy", fontSize * leading);
1004 i && texts[i].setAttribute("x", el.attrs.x);
1007 el.node.setAttribute("y", el.attrs.y);
1008 var bb = el.getBBox();
1009 var dif = el.attrs.y - (bb.y + bb.height / 2);
1011 el.node.setAttribute("y", el.attrs.y + dif);
1014 var Element = function (node, svg) {
1020 this.attrs = this.attrs || {};
1021 this.transformations = []; // rotate, translate, scale
1025 rt: {deg: 0, x: 0, y: 0},
1030 Element.prototype.rotate = function (deg, cx, cy) {
1032 return this._.rt.deg;
1034 var bbox = this.getBBox();
1035 deg = deg.toString().split(separator);
1036 if (deg.length - 1) {
1037 cx = parseFloat(deg[1]);
1038 cy = parseFloat(deg[2]);
1040 deg = parseFloat(deg[0]);
1042 this._.rt.deg = deg;
1044 this._.rt.deg += deg;
1049 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1050 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1051 if (this._.rt.deg) {
1052 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
1054 this.transformations[0] = "";
1056 this.node.setAttribute("transform", this.transformations.join(" "));
1059 Element.prototype.hide = function () {
1060 this.node.style.display = "none";
1063 Element.prototype.show = function () {
1064 this.node.style.display = "block";
1067 Element.prototype.remove = function () {
1068 this.node.parentNode.removeChild(this.node);
1070 Element.prototype.getBBox = function () {
1071 var bbox = this.node.getBBox();
1072 if (this.type == "text") {
1073 bbox = {x: bbox.x, y: Infinity, width: bbox.width, height: 0};
1074 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1075 var bb = this.node.getExtentOfChar(i);
1076 (bb.y < bbox.y) && (bbox.y = bb.y);
1077 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1082 Element.prototype.attr = function () {
1083 if (arguments.length == 1 && typeof arguments[0] == "string") {
1084 if (arguments[0] == "translation") {
1085 return this.translate();
1087 return this.attrs[arguments[0]];
1089 if (arguments.length == 1 && arguments[0] instanceof win.Array) {
1091 for (var j in arguments[0]) {
1092 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1096 if (arguments.length == 2) {
1098 params[arguments[0]] = arguments[1];
1099 setFillAndStroke(this, params);
1100 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1101 setFillAndStroke(this, arguments[0]);
1105 Element.prototype.toFront = function () {
1106 this.node.parentNode.appendChild(this.node);
1109 Element.prototype.toBack = function () {
1110 if (this.node.parentNode.firstChild != this.node) {
1111 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1115 Element.prototype.insertAfter = function (element) {
1116 if (element.node.nextSibling) {
1117 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1119 element.node.parentNode.appendChild(this.node);
1123 Element.prototype.insertBefore = function (element) {
1124 element.node.parentNode.insertBefore(this.node, element.node);
1127 var theCircle = function (svg, x, y, r) {
1128 var el = doc.createElementNS(svg.svgns, "circle");
1129 el.setAttribute("cx", x);
1130 el.setAttribute("cy", y);
1131 el.setAttribute("r", r);
1132 el.setAttribute("fill", "none");
1133 el.setAttribute("stroke", "#000");
1135 svg.canvas.appendChild(el);
1137 var res = new Element(el, svg);
1138 res.attrs = res.attrs || {};
1142 res.attrs.stroke = "#000";
1143 res.type = "circle";
1146 var theRect = function (svg, x, y, w, h, r) {
1147 var el = doc.createElementNS(svg.svgns, "rect");
1148 el.setAttribute("x", x);
1149 el.setAttribute("y", y);
1150 el.setAttribute("width", w);
1151 el.setAttribute("height", h);
1153 el.setAttribute("rx", r);
1154 el.setAttribute("ry", r);
1156 el.setAttribute("fill", "none");
1157 el.setAttribute("stroke", "#000");
1159 svg.canvas.appendChild(el);
1161 var res = new Element(el, svg);
1162 res.attrs = res.attrs || {};
1165 res.attrs.width = w;
1166 res.attrs.height = h;
1167 res.attrs.stroke = "#000";
1169 res.attrs.rx = res.attrs.ry = r;
1174 var theEllipse = function (svg, x, y, rx, ry) {
1175 var el = doc.createElementNS(svg.svgns, "ellipse");
1176 el.setAttribute("cx", x);
1177 el.setAttribute("cy", y);
1178 el.setAttribute("rx", rx);
1179 el.setAttribute("ry", ry);
1180 el.setAttribute("fill", "none");
1181 el.setAttribute("stroke", "#000");
1183 svg.canvas.appendChild(el);
1185 var res = new Element(el, svg);
1186 res.attrs = res.attrs || {};
1191 res.attrs.stroke = "#000";
1192 res.type = "ellipse";
1195 var theImage = function (svg, src, x, y, w, h) {
1196 var el = doc.createElementNS(svg.svgns, "image");
1197 el.setAttribute("x", x);
1198 el.setAttribute("y", y);
1199 el.setAttribute("width", w);
1200 el.setAttribute("height", h);
1201 el.setAttribute("preserveAspectRatio", "none");
1202 el.setAttributeNS(svg.xlink, "href", src);
1204 svg.canvas.appendChild(el);
1206 var res = new Element(el, svg);
1207 res.attrs = res.attrs || {};
1210 res.attrs.width = w;
1211 res.attrs.height = h;
1215 var theText = function (svg, x, y, text) {
1216 var el = doc.createElementNS(svg.svgns, "text");
1217 el.setAttribute("x", x);
1218 el.setAttribute("y", y);
1219 el.setAttribute("text-anchor", "middle");
1221 svg.canvas.appendChild(el);
1223 var res = new Element(el, svg);
1224 res.attrs = res.attrs || {};
1228 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000", text: text});
1231 var theGroup = function (svg) {
1232 var el = doc.createElementNS(svg.svgns, "g");
1234 svg.canvas.appendChild(el);
1236 var i = new Element(el, svg);
1237 for (var f in svg) {
1238 if (f[0] != "_" && typeof svg[f] == "function") {
1239 i[f] = (function (f) {
1240 return function () {
1241 var e = svg[f].apply(svg, arguments);
1242 el.appendChild(e[0]);
1251 var setSize = function (width, height) {
1252 this.width = width || this.width;
1253 this.height = height || this.height;
1254 this.canvas.setAttribute("width", this.width);
1255 this.canvas.setAttribute("height", this.height);
1258 var create = function () {
1259 var con = getContainer.apply(null, arguments);
1260 var container = con.container,
1264 height = con.height;
1266 throw new Error("SVG container not found.");
1268 paper.canvas = doc.createElementNS(paper.svgns, "svg");
1269 paper.canvas.setAttribute("width", width || 320);
1270 paper.width = width || 320;
1271 paper.canvas.setAttribute("height", height || 200);
1272 paper.height = height || 200;
1273 if (container == 1) {
1274 doc.body.appendChild(paper.canvas);
1275 paper.canvas.style.position = "absolute";
1276 paper.canvas.style.left = x + "px";
1277 paper.canvas.style.top = y + "px";
1279 if (container.firstChild) {
1280 container.insertBefore(paper.canvas, container.firstChild);
1282 container.appendChild(paper.canvas);
1286 canvas: paper.canvas,
1287 clear: function () {
1288 while (this.canvas.firstChild) {
1289 this.canvas.removeChild(this.canvas.firstChild);
1291 this.defs = doc.createElementNS(paper.svgns, "defs");
1292 this.canvas.appendChild(this.defs);
1295 for (var prop in paper) {
1296 if (prop != "create") {
1297 container[prop] = paper[prop];
1300 plugins(container, container, R.fn);
1304 paper.remove = function () {
1305 this.canvas.parentNode.removeChild(this.canvas);
1307 paper.svgns = "http://www.w3.org/2000/svg";
1308 paper.xlink = "http://www.w3.org/1999/xlink";
1309 paper.safari = function () {
1310 if (navigator.vendor == "Apple Computer, Inc.") {
1311 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1312 setTimeout(function () {rect.remove();}, 0);
1319 thePath = function (params, pathString, VML) {
1320 var g = createNode("group"), gl = g.style;
1321 gl.position = "absolute";
1324 gl.width = VML.width + "px";
1325 gl.height = VML.height + "px";
1326 var el = createNode("shape"), ol = el.style;
1327 ol.width = VML.width + "px";
1328 ol.height = VML.height + "px";
1330 if (params["class"]) {
1331 el.className = "rvml " + params["class"];
1333 el.coordsize = this.coordsize;
1334 el.coordorigin = this.coordorigin;
1336 VML.canvas.appendChild(g);
1337 var p = new Element(el, g, VML);
1338 p.isAbsolute = true;
1341 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1343 p.absolutely = function () {
1344 this.isAbsolute = true;
1347 p.relatively = function () {
1348 this.isAbsolute = false;
1351 p.moveTo = function (x, y) {
1352 var d = this.isAbsolute?"m":"t";
1353 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1354 this.node.path = this.Path += d;
1355 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1356 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1357 this.last.isAbsolute = this.isAbsolute;
1358 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1361 p.lineTo = function (x, y) {
1362 var d = this.isAbsolute?"l":"r";
1363 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1364 this[0].path = this.Path += d;
1365 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1366 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1367 this.last.isAbsolute = this.isAbsolute;
1368 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1371 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1372 // for more information of where this math came from visit:
1373 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1374 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1375 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1376 var x1 = this.last.x,
1380 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1381 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1382 cx = k * rx * y / ry + (x1 + x2) / 2,
1383 cy = k * -ry * x / rx + (y1 + y2) / 2,
1384 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1385 left = Math.round(cx - rx),
1386 top = Math.round(cy - ry);
1387 d += [left, top, Math.round(left + rx * 2), Math.round(top + ry * 2), Math.round(x1), Math.round(y1), Math.round(parseFloat(x2)), Math.round(parseFloat(y2))].join(", ");
1388 this.node.path = this.Path += d;
1389 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2);
1390 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2);
1391 this.last.isAbsolute = this.isAbsolute;
1392 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1395 p.cplineTo = function (x1, y1, w1) {
1397 return this.lineTo(x1, y1);
1399 var x = Math.round(Math.round(parseFloat(x1) * 100) / 100),
1400 y = Math.round(Math.round(parseFloat(y1) * 100) / 100),
1401 w = Math.round(Math.round(parseFloat(w1) * 100) / 100),
1402 d = this.isAbsolute ? "c" : "v",
1403 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1404 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1405 d += attr.join(" ") + " ";
1406 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1407 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1408 this.last.bx = attr[2];
1409 this.last.by = attr[3];
1410 this.node.path = this.Path += d;
1411 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1415 p.curveTo = function () {
1416 var d = this.isAbsolute ? "c" : "v";
1417 if (arguments.length == 6) {
1418 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1419 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1420 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4]);
1421 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5]);
1422 d += [Math.round(parseFloat(arguments[0])),
1423 Math.round(parseFloat(arguments[1])),
1424 Math.round(parseFloat(arguments[2])),
1425 Math.round(parseFloat(arguments[3])),
1426 Math.round(parseFloat(arguments[4])),
1427 Math.round(parseFloat(arguments[5]))].join(" ") + " ";
1428 this.last.isAbsolute = this.isAbsolute;
1429 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1431 if (arguments.length == 4) {
1432 var bx = this.last.x * 2 - this.last.bx;
1433 var by = this.last.y * 2 - this.last.by;
1434 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1435 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1436 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1437 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1438 d += [Math.round(bx), Math.round(by),
1439 Math.round(parseFloat(arguments[0])),
1440 Math.round(parseFloat(arguments[1])),
1441 Math.round(parseFloat(arguments[2])),
1442 Math.round(parseFloat(arguments[3]))].join(" ") + " ";
1443 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1445 this.node.path = this.Path += d;
1448 p.qcurveTo = function () {
1450 if (arguments.length == 4) {
1451 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1452 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1453 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1454 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1455 d += [Math.round(this.last.qx),
1456 Math.round(this.last.qy),
1457 Math.round(this.last.x),
1458 Math.round(this.last.y)].join(" ") + " ";
1459 this.last.isAbsolute = this.isAbsolute;
1460 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1462 if (arguments.length == 2) {
1463 this.last.qx = this.last.x * 2 - this.last.qx;
1464 this.last.qy = this.last.y * 2 - this.last.qy;
1465 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1466 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1467 d += [Math.round(this.last.qx),
1468 Math.round(this.last.qy),
1469 Math.round(this.last.x),
1470 Math.round(this.last.y)].join(" ") + " ";
1471 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1473 this.node.path = this.Path += d;
1474 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1477 p.addRoundedCorner = function (r, dir) {
1478 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1481 rollback = function () {
1485 rollback = function () {};
1491 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1494 o.curveTo(-R, 0, -r, r - R, -r, r);
1501 o.curveTo(R, 0, r, -(r - R), r, -r);
1504 o.curveTo(R, 0, r, r - R, r, r);
1511 o.curveTo(0, -R, -(R - r), -r, r, -r);
1514 o.curveTo(0, -R, R - r, -r, -r, -r);
1521 o.curveTo(0, R, -(R - r), r, r, r);
1524 o.curveTo(0, R, R - r, r, -r, r);
1529 actions[dir.charAt(0)]()[dir.charAt(1)]();
1533 p.andClose = function () {
1534 this.node.path = (this.Path += "x");
1535 this.attrs.path += "z";
1541 paper.pathfinder(p, "" + pathString);
1544 setFillAndStroke(p, params);
1545 if (params.gradient) {
1546 addGrdientFill(p, params.gradient);
1550 var setFillAndStroke = function (o, params) {
1551 var s = o.node.style,
1553 o.attrs = o.attrs || {};
1554 for (var par in params) {
1555 o.attrs[par] = params[par];
1557 params.href && (o.node.href = params.href);
1558 params.title && (o.node.title = params.title);
1559 params.target && (o.node.target = params.target);
1560 if (params.path && o.type == "path") {
1563 paper.pathfinder(o, params.path);
1565 if (params.rotation != null) {
1566 o.rotate(params.rotation, true);
1568 if (params.translation) {
1569 var xy = (params.translation + "").split(separator);
1570 o.translate(xy[0], xy[1]);
1573 var xy = (params.scale + "").split(separator);
1574 o.scale(xy[0], xy[1]);
1576 if (o.type == "image" && params.src) {
1577 o.node.src = params.src;
1579 if (o.type == "image" && params.opacity) {
1580 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1581 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1583 params.font && (s.font = params.font);
1584 params["font-family"] && (s.fontFamily = params["font-family"]);
1585 params["font-size"] && (s.fontSize = params["font-size"]);
1586 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1587 params["font-style"] && (s.fontStyle = params["font-style"]);
1588 if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined" || params["stroke-width"] || params["stroke-opacity"] || params["fill-opacity"] || params["stroke-dasharray"] || params["stroke-miterlimit"] || params["stroke-linejoin"] || params["stroke-linecap"]) {
1589 o = o.shape || o.node;
1590 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || createNode("fill");
1591 if ("fill-opacity" in params || "opacity" in params) {
1592 fill.opacity = ((+params["fill-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1597 if (typeof fill.on == "undefined" || params.fill == "none") {
1600 if (fill.on && params.fill) {
1601 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1603 fill.src = isURL[1];
1606 fill.color = getRGB(params.fill).hex;
1608 fill.type = "solid";
1611 o.appendChild(fill);
1612 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || createNode("stroke");
1613 if ((params.stroke && params.stroke != "none") || params["stroke-width"] || typeof params["stroke-opacity"] != "undefined" || params["stroke-dasharray"] || params["stroke-miterlimit"] || params["stroke-linejoin"] || params["stroke-linecap"]) {
1616 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1619 if (stroke.on && params.stroke) {
1620 stroke.color = getRGB(params.stroke).hex;
1622 stroke.opacity = ((+params["stroke-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1623 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1624 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1625 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1626 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"]) || 1) * 12 / 16);
1627 if (params["stroke-dasharray"]) {
1631 "-.": "shortdashdot",
1632 "-..": "shortdashdotdot",
1637 "--.": "longdashdot",
1638 "--..": "longdashdotdot"
1640 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1642 o.appendChild(stroke);
1644 if (res.type == "text") {
1645 var span = doc.createElement("span"),
1650 s.display = "inline";
1651 res.attrs.font && (s.font = res.attrs.font);
1652 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1653 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1654 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1655 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1656 res.node.parentNode.appendChild(span);
1657 span.innerText = res.node.string;
1658 res.W = res.attrs.w = span.offsetWidth;
1659 res.H = res.attrs.h = span.offsetHeight;
1660 res.X = res.attrs.x;
1661 res.Y = res.attrs.y + Math.round(res.H / 2);
1662 res.node.parentNode.removeChild(span);
1664 // text-anchor emulation
1665 switch (res.attrs["text-anchor"]) {
1667 res.node.style["v-text-align"] = "left";
1668 res.bbx = Math.round(res.W / 2);
1671 res.node.style["v-text-align"] = "right";
1672 res.bbx = -Math.round(res.W / 2);
1675 res.node.style["v-text-align"] = "center";
1680 var getAngle = function (a, b, c, d) {
1681 var angle = Math.round(Math.atan((parseFloat(c) - parseFloat(a)) / (parseFloat(d) - parseFloat(b))) * 57.29) || 0;
1682 if (!angle && parseFloat(a) < parseFloat(b)) {
1691 var addGrdientFill = function (o, gradient) {
1692 gradient = toGradient(gradient);
1693 o.attrs = o.attrs || {};
1694 var attrs = o.attrs;
1695 o.attrs.gradient = gradient;
1696 o = o.shape || o[0];
1697 var fill = o.getElementsByTagName("fill");
1701 fill = createNode("fill");
1703 if (gradient.dots.length) {
1705 fill.method = "none";
1706 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1707 if (typeof gradient.dots[0].color != "undefined") {
1708 fill.color = getRGB(gradient.dots[0].color).hex;
1710 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1711 fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1714 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1715 if (gradient.dots[i].offset) {
1716 colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1719 var fillOpacity = typeof gradient.dots[gradient.dots.length - 1].opacity == "undefined" ? (typeof attrs.opacity == "undefined" ? 1 : attrs.opacity) : gradient.dots[gradient.dots.length - 1].opacity;
1720 if (colors.length) {
1721 fill.colors.value = colors.join(",");
1722 fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1724 fill.colors.value = "0% " + fill.color;
1726 fill.opacity = fillOpacity;
1727 if (typeof gradient.angle != "undefined") {
1728 fill.angle = (-gradient.angle + 270) % 360;
1729 } else if (gradient.vector) {
1730 fill.angle = getAngle.apply(null, gradient.vector);
1732 if ((gradient.type + "").toLowerCase() == "radial") {
1733 fill.focus = "100%";
1734 fill.focusposition = "0.5 0.5";
1738 var Element = function (node, group, vml) {
1758 Element.prototype.rotate = function (deg, cx, cy) {
1760 return this._.rt.deg;
1762 deg = deg.toString().split(separator);
1763 if (deg.length - 1) {
1764 cx = parseFloat(deg[1]);
1765 cy = parseFloat(deg[2]);
1767 deg = parseFloat(deg[0]);
1769 this._.rt.deg = deg;
1771 this._.rt.deg += deg;
1778 this.setBox(null, cx, cy);
1779 this.Group.style.rotation = this._.rt.deg;
1782 Element.prototype.setBox = function (params, cx, cy) {
1783 var gs = this.Group.style,
1784 os = (this.shape && this.shape.style) || this.node.style;
1785 for (var i in params) {
1786 this.attrs[i] = params[i];
1788 cx = cx || this._.rt.cx;
1789 cy = cy || this._.rt.cy;
1790 var attr = this.attrs, x, y, w, h;
1791 switch (this.type) {
1793 x = attr.cx - attr.r;
1794 y = attr.cy - attr.r;
1798 x = attr.cx - attr.rx;
1799 y = attr.cy - attr.ry;
1807 w = attr.width || 0;
1808 h = attr.height || 0;
1811 this.textpath.v = ["m", Math.round(attr.x), ", ", Math.round(attr.y - 2), "l", Math.round(attr.x) + 1, ", ", Math.round(attr.y - 2)].join("");
1812 x = attr.x - Math.round(this.W / 2);
1813 y = attr.y - this.H / 2;
1818 if (!this.attrs.path) {
1822 h = this.vml.height;
1824 var dim = pathDimensions(this.attrs.path),
1835 h = this.vml.height;
1838 cx = (cx == null) ? x + w / 2 : cx;
1839 cy = (cy == null) ? y + h / 2 : cy;
1840 var left = cx - this.vml.width / 2,
1841 top = cy - this.vml.height / 2;
1842 if (this.type == "path" || this.type == "text") {
1843 gs.left = left + "px";
1844 gs.top = top + "px";
1845 this.X = this.type == "text" ? x : -left;
1846 this.Y = this.type == "text" ? y : -top;
1849 os.left = -left + "px";
1850 os.top = -top + "px";
1852 gs.left = left + "px";
1853 gs.top = top + "px";
1858 gs.width = this.vml.width + "px";
1859 gs.height = this.vml.height + "px";
1860 os.left = x - left + "px";
1861 os.top = y - top + "px";
1862 os.width = w + "px";
1863 os.height = h + "px";
1864 var arcsize = (+params.r || 0) / (Math.min(w, h));
1865 if (this.type == "rect" && this.arcsize != arcsize) {
1866 // We should recplace element with the new one
1867 var o = createNode("roundrect");
1868 o.arcsize = arcsize;
1869 this.Group.appendChild(o);
1870 this.node.parentNode.removeChild(this.node);
1872 this.arcsize = arcsize;
1873 setFillAndStroke(this, this.attrs);
1874 this.setBox(this.attrs);
1878 Element.prototype.hide = function () {
1879 this.Group.style.display = "none";
1882 Element.prototype.show = function () {
1883 this.Group.style.display = "block";
1886 Element.prototype.getBBox = function () {
1887 if (this.type == "path") {
1888 return pathDimensions(this.attr("path"));
1891 x: this.X + (this.bbx || 0),
1897 Element.prototype.remove = function () {
1898 this[0].parentNode.removeChild(this[0]);
1899 this.Group.parentNode.removeChild(this.Group);
1900 this.shape && this.shape.parentNode.removeChild(this.shape);
1902 Element.prototype.attr = function () {
1903 if (arguments.length == 1 && typeof arguments[0] == "string") {
1904 if (arguments[0] == "translation") {
1905 return this.translate();
1907 return this.attrs[arguments[0]];
1909 if (this.attrs && arguments.length == 1 && arguments[0] instanceof win.Array) {
1911 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1912 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1917 if (arguments.length == 2) {
1919 params[arguments[0]] = arguments[1];
1921 if (arguments.length == 1 && typeof arguments[0] == "object") {
1922 params = arguments[0];
1925 if (params.gradient) {
1926 addGrdientFill(this, params.gradient);
1928 if (params.text && this.type == "text") {
1929 this.node.string = params.text;
1932 this.node.id = params.id;
1934 setFillAndStroke(this, params);
1935 this.setBox(params);
1939 Element.prototype.toFront = function () {
1940 this.Group.parentNode.appendChild(this.Group);
1943 Element.prototype.toBack = function () {
1944 if (this.Group.parentNode.firstChild != this.Group) {
1945 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1949 Element.prototype.insertAfter = function (element) {
1950 if (element.Group.nextSibling) {
1951 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1953 element.Group.parentNode.appendChild(this.Group);
1957 Element.prototype.insertBefore = function (element) {
1958 element.Group.parentNode.insertBefore(this.Group, element.Group);
1961 var theCircle = function (vml, x, y, r) {
1962 var g = createNode("group");
1963 var o = createNode("oval");
1965 vml.canvas.appendChild(g);
1966 var res = new Element(o, g, vml);
1967 res.type = "circle";
1968 setFillAndStroke(res, {stroke: "#000", fill: "none"});
1972 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1975 var theRect = function (vml, x, y, w, h, r) {
1976 var g = createNode("group"),
1977 o = createNode("roundrect"),
1978 arcsize = (+r || 0) / (Math.min(w, h));
1979 o.arcsize = arcsize;
1981 vml.canvas.appendChild(g);
1982 var res = new Element(o, g, vml);
1984 setFillAndStroke(res, {stroke: "#000"});
1990 res.arcsize = arcsize;
1991 res.setBox({x: x, y: y, width: w, height: h});
1994 var theEllipse = function (vml, x, y, rx, ry) {
1995 var g = createNode("group");
1996 var o = createNode("oval");
1998 vml.canvas.appendChild(g);
1999 var res = new Element(o, g, vml);
2000 res.type = "ellipse";
2001 setFillAndStroke(res, {stroke: "#000"});
2006 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2009 var theImage = function (vml, src, x, y, w, h) {
2010 var g = createNode("group");
2011 var o = createNode("image");
2014 vml.canvas.appendChild(g);
2015 var res = new Element(o, g, vml);
2021 res.setBox({x: x, y: y, width: w, height: h});
2024 var theText = function (vml, x, y, text) {
2025 var g = createNode("group"), gs = g.style;
2026 var el = createNode("shape"), ol = el.style;
2027 var path = createNode("path"), ps = path.style;
2028 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
2029 path.textpathok = true;
2030 ol.width = vml.width;
2031 ol.height = vml.height;
2032 gs.position = "absolute";
2035 gs.width = vml.width;
2036 gs.height = vml.height;
2037 var o = createNode("textpath");
2040 o.coordsize = vml.coordsize;
2041 o.coordorigin = vml.coordorigin;
2043 el.appendChild(path);
2045 vml.canvas.appendChild(g);
2046 var res = new Element(o, g, vml);
2048 res.textpath = path;
2054 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2058 var setSize = function (width, height) {
2059 this.width = width || this.width;
2060 this.height = height || this.height;
2061 this.canvas.style.width = this.width + "px";
2062 this.canvas.style.height = this.height + "px";
2063 this.canvas.parentNode.style.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2064 this.canvas.coordsize = this.width + " " + this.height;
2067 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2069 if (!doc.namespaces.rvml) {
2070 doc.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
2072 var createNode = function (tagName) {
2073 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2076 var createNode = function (tagName) {
2077 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2080 var create = function () {
2081 var con = getContainer.apply(null, arguments);
2082 var container = con.container,
2086 height = con.height;
2088 throw new Error("VML container not found.");
2090 var c = doc.createElement("div"),
2091 d = doc.createElement("div"),
2092 r = paper.canvas = createNode("group"),
2093 cs = c.style, rs = r.style;
2094 paper.width = width;
2095 paper.height = height;
2096 width = width || "320px";
2097 height = height || "200px";
2098 cs.clip = "rect(0 " + width + "px " + height + "px 0)";
2101 cs.position = "absolute";
2102 rs.position = "absolute";
2103 d.style.position = "relative";
2106 r.coordsize = (/%$/.test(width) ? width : parseFloat(width)) + " " + (/%$/.test(height) ? height : parseFloat(height));
2107 r.coordorigin = "0 0";
2109 var b = createNode("rect"), bs = b.style;
2110 bs.left = bs.top = 0;
2111 bs.width = rs.width;
2112 bs.height = rs.height;
2113 b.filled = b.stroked = "f";
2118 if (container == 1) {
2119 doc.body.appendChild(d);
2120 cs.position = "absolute";
2132 cs.width = container.style.width = width;
2133 cs.height = container.style.height = height;
2134 if (container.firstChild) {
2135 container.insertBefore(d, container.firstChild);
2137 container.appendChild(d);
2140 for (var prop in paper) {
2141 container[prop] = paper[prop];
2143 plugins(container, container, R.fn);
2144 container.clear = function () {
2146 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2147 if (r.childNodes[i] != b) {
2148 todel.push(r.childNodes[i]);
2151 for (i = 0, ii = todel.length; i < ii; i++) {
2152 r.removeChild(todel[i]);
2157 paper.remove = function () {
2158 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2160 paper.safari = function () {};
2166 var addEvent = (function () {
2167 if (doc.addEventListener) {
2168 return function (obj, type, fn, element) {
2169 var f = function (e) {
2170 return fn.call(element, e);
2172 obj.addEventListener(type, f, false);
2173 return function () {
2174 obj.removeEventListener(type, f, false);
2178 } else if (doc.attachEvent) {
2179 return function (obj, type, fn, element) {
2180 var f = function (e) {
2181 return fn.call(element, e || win.event);
2183 obj.attachEvent("on" + type, f);
2184 var detacher = function () {
2185 obj.detachEvent("on" + type, f);
2188 if (type == "mouseover") {
2189 obj.attachEvent("onmouseenter", f);
2190 return function () {
2191 obj.detachEvent("onmouseenter", f);
2194 } else if (type == "mouseout") {
2195 obj.attachEvent("onmouseleave", f);
2196 return function () {
2197 obj.detachEvent("onmouseleave", f);
2205 for (var i = events.length; i--;) {
2206 (function (eventName) {
2207 Element.prototype[eventName] = function (fn) {
2208 if (typeof fn == "function") {
2209 this.events = this.events || {};
2210 this.events[eventName] = this.events[eventName] || {};
2211 this.events[eventName][fn] = this.events[eventName][fn] || [];
2212 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2216 Element.prototype["un" + eventName] = function (fn) {
2218 this.events[eventName] &&
2219 this.events[eventName][fn] &&
2220 this.events[eventName][fn].length &&
2221 this.events[eventName][fn].shift()() &&
2222 !this.events[eventName][fn].length &&
2223 delete this.events[eventName][fn];
2228 paper.circle = function (x, y, r) {
2229 return theCircle(this, x, y, r);
2231 paper.rect = function (x, y, w, h, r) {
2232 return theRect(this, x, y, w, h, r);
2234 paper.ellipse = function (x, y, rx, ry) {
2235 return theEllipse(this, x, y, rx, ry);
2237 paper.path = function (params, pathString) {
2238 return thePath(params, pathString, this);
2240 paper.image = function (src, x, y, w, h) {
2241 return theImage(this, src, x, y, w, h);
2243 paper.text = function (x, y, text) {
2244 return theText(this, x, y, text);
2246 paper.group = function () {
2249 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2250 color = color || "#000";
2251 var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
2253 columnWidth = w / wv;
2254 for (var i = 1; i < hv; i++) {
2255 path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
2257 for (var i = 1; i < wv; i++) {
2258 path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
2260 return this.path({stroke: color, "stroke-width": 1}, path.join(","));
2262 paper.pathfinder = function (p, path) {
2264 M: function (x, y) {
2267 C: function (x1, y1, x2, y2, x3, y3) {
2268 this.curveTo(x1, y1, x2, y2, x3, y3);
2270 Q: function (x1, y1, x2, y2) {
2271 this.qcurveTo(x1, y1, x2, y2);
2273 T: function (x, y) {
2274 this.qcurveTo(x, y);
2276 S: function (x1, y1, x2, y2) {
2277 p.curveTo(x1, y1, x2, y2);
2279 L: function (x, y) {
2283 this.lineTo(x, this.last.y);
2286 this.lineTo(this.last.x, y);
2288 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2289 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2296 path = pathToAbsolute(path);
2297 for (var i = 0, ii = path.length; i < ii; i++) {
2298 var b = path[i].shift();
2299 commands[b].apply(p, path[i]);
2302 paper.set = function (itemsArray) {
2303 return new Set(itemsArray);
2305 paper.setSize = setSize;
2306 Element.prototype.stop = function () {
2307 clearTimeout(this.animation_in_progress);
2309 Element.prototype.scale = function (x, y, cx, cy) {
2310 if (x == null && y == null) {
2311 return {x: this._.sx, y: this._.sy};
2315 var dx, dy, dcx, dcy;
2317 var bb = this.type == "path" ? pathDimensions(this.attrs.path) : this.getBBox(),
2318 rcx = bb.x + bb.width / 2,
2319 rcy = bb.y + bb.height / 2;
2320 cx = isNaN(cx) ? rcx : cx;
2321 cy = isNaN(cy) ? rcy : cy;
2322 var dirx = Math.round(x / Math.abs(x)),
2323 diry = Math.round(y / Math.abs(y)),
2324 s = this.node.style,
2325 ncx = cx + (rcx - cx) * x * dirx / this._.sx,
2326 ncy = cy + (rcy - cy) * y * diry / this._.sy;
2327 dx = this.attr("x");
2328 dy = this.attr("y");
2329 dcx = this.attr("cx");
2330 dcy = this.attr("cy");
2331 if (!(this.type in {rect: 1, circle: 1, ellipse: 1}) && (dirx != 1 || diry != 1)) {
2332 if (this.transformations) {
2333 this.transformations[2] = "scale(" + [dirx, diry] + ")";
2334 this.node.setAttribute("transform", this.transformations.join(" "));
2335 dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
2336 dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
2337 dcx = this.attr("cx") * dirx;
2338 dcy = this.attr("cy") * diry;
2340 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2341 ", M12=0, M21=0, M22=" + diry +
2342 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2343 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2347 if (this.transformations) {
2348 this.transformations[2] = "";
2349 this.node.setAttribute("transform", this.transformations.join(" "));
2351 this.node.filterMatrix = "";
2352 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2355 switch (this.type) {
2358 var neww = this.attrs.width * x * dirx / this._.sx,
2359 newh = this.attrs.height * y * diry / this._.sy,
2360 newx = ncx - neww / 2,
2361 newy = ncy - newh / 2;
2372 rx: this.attrs.rx * x * dirx / this._.sx,
2373 ry: this.attrs.ry * y * diry / this._.sy,
2374 r: this.attrs.r * x * dirx / this._.sx,
2380 var path = pathToRelative(R.parsePathString(this.attrs.path)),
2382 for (var i = 0, ii = path.length; i < ii; i++) {
2383 if (path[i][0].toUpperCase() == "M" && skip) {
2388 if (this.svg && path[i][0].toUpperCase() == "A") {
2389 path[i][path[i].length - 2] *= x * dirx;
2390 path[i][path[i].length - 1] *= y * diry;
2394 for (var j = 1, jj = path[i].length; j < jj; j++) {
2395 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2399 var dim2 = pathDimensions(path),
2400 dx = ncx - dim2.x - dim2.width / 2,
2401 dy = ncy - dim2.y - dim2.height / 2;
2402 path = pathToRelative(path);
2406 this.attr({path: path.join(" ")});
2413 Element.prototype.animate = function (params, ms, callback) {
2414 clearTimeout(this.animation_in_progress);
2419 for (var attr in params) {
2420 if (attr in availableAnimAttrs) {
2421 from[attr] = this.attr(attr);
2422 if (typeof from[attr] == "undefined") {
2423 from[attr] = availableAttrs[attr];
2425 to[attr] = params[attr];
2426 switch (availableAnimAttrs[attr]) {
2428 diff[attr] = (to[attr] - from[attr]) / ms;
2431 from[attr] = getRGB(from[attr]);
2432 var toColour = getRGB(to[attr]);
2434 r: (toColour.r - from[attr].r) / ms,
2435 g: (toColour.g - from[attr].g) / ms,
2436 b: (toColour.b - from[attr].b) / ms
2440 var pathes = pathEqualiser(from[attr], to[attr]);
2441 from[attr] = pathes[0];
2442 to[attr] = pathes[1];
2444 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2445 diff[attr][i] = [0];
2446 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2447 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2452 var values = params[attr].toString().split(separator),
2453 from2 = from[attr].toString().split(separator);
2454 if (attr == "translation") {
2455 from[attr] = [0, 0];
2456 diff[attr] = [values[0] / ms, values[1] / ms];
2457 } else if (attr == "rotation") {
2458 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2459 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2461 from[attr] = (from[attr] + "").split(separator);
2462 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2468 var start = +new Date,
2472 var time = +new Date - start,
2476 for (var attr in from) {
2477 switch (availableAnimAttrs[attr]) {
2479 now = +from[attr] + time * diff[attr];
2483 Math.round(from[attr].r + time * diff[attr].r),
2484 Math.round(from[attr].g + time * diff[attr].g),
2485 Math.round(from[attr].b + time * diff[attr].b)
2490 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2491 now[i] = [from[attr][i][0]];
2492 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2493 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2495 now[i] = now[i].join(" ");
2497 now = now.join(" ");
2500 if (attr == "translation") {
2501 var x = diff[attr][0] * (time - prev),
2502 y = diff[attr][1] * (time - prev);
2505 now = [x, y].join(" ");
2506 } else if (attr == "rotation") {
2507 now = +from[attr][0] + time * diff[attr][0];
2508 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2510 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2514 if (attr == "font-size") {
2515 set[attr] = now + "px";
2521 that.animation_in_progress = setTimeout(arguments.callee, 0);
2524 (t.x || t.y) && that.translate(-t.x, -t.y);
2526 clearTimeout(that.animation_in_progress);
2528 (typeof callback == "function") && callback.call(that);
2534 Element.prototype.translate = function (x, y) {
2536 return {x: this._.tx, y: this._.ty};
2540 switch (this.type) {
2543 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
2548 this.attr({x: this.attrs.x + (+x), y: this.attrs.y + (+y)});
2551 var path = pathToRelative(this.attrs.path);
2554 this.attr({path: path.join(" ")});
2561 var Set = function (itemsArray) {
2563 this.length = (itemsArray && itemsArray.length) || 0;
2564 if (itemsArray && itemsArray.constructor == Array) {
2565 for (var i = itemsArray.length; i--;) {
2566 if (itemsArray[i].constructor == Element) {
2567 this.items[this.items.length] = itemsArray[i];
2572 Set.prototype.push = function (item) {
2573 if (item && item.constructor == Element) {
2574 var len = this.items.length;
2575 this.items[len] = item;
2581 Set.prototype.pull = function (id) {
2582 var res = this.items.splice(id, 1)[0];
2583 for (var j = id, jj = this.items.length; j < jj; j++) {
2584 this[j] = this[j + 1];
2586 delete this[jj + 1];
2590 for (var method in Element.prototype) {
2591 Set.prototype[method] = (function (methodname) {
2592 return function () {
2593 for (var i = this.items.length; i--;) {
2594 this.items[i][methodname].apply(this.items[i], arguments);
2600 Set.prototype.getBBox = function () {
2601 var x = [], y = [], w = [], h = [];
2602 for (var i = this.items.length; i--;) {
2603 var box = this.items[i].getBBox();
2606 w.push(box.x + box.width);
2607 h.push(box.y + box.height);
2609 x = Math.min.apply(Math, x);
2610 y = Math.min.apply(Math, y);
2614 width: Math.max.apply(Math, w) - x,
2615 height: Math.max.apply(Math, h) - y
2619 R.registerFont = function (font) {
2623 this.fonts = this.fonts || {};
2624 if (this.fonts[font.face["font-family"]]) {
2625 this.fonts[font.face["font-family"]].push(font);
2627 this.fonts[font.face["font-family"]] = [font];
2629 font.face["units-per-em"] = parseInt(font.face["units-per-em"], 10);
2631 for (var glyph in font.glyphs) {
2632 var path = font.glyphs[glyph];
2634 path.d = "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
2635 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2640 paper.getFont = function (family, weight, style, stretch) {
2641 stretch = stretch || "normal";
2642 style = style || "normal";
2643 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2644 var font = R.fonts[family];
2646 var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, "") + "(\\s|$)", "i");
2647 for (var fontName in R.fonts) {
2648 if (name.test(fontName)) {
2649 font = R.fonts[fontName];
2656 for (var i = 0, ii = font.length; i < ii; i++) {
2658 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2666 paper.print = function (x, y, string, font, size) {
2667 var out = this.set(),
2668 letters = (string + "").split(""),
2673 scale = (size || 16) / font.face["units-per-em"];
2674 for (var i = 0, ii = letters.length; i < ii; i++) {
2675 var prev = i && font.glyphs[letters[i - 1]] || {},
2676 curr = font.glyphs[letters[i]];
2677 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
2678 curr && curr.d && out.push(this.path({fill: "#000", stroke: "none"}, curr.d).translate(shift, 0));
2680 out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
2686 R.noConflict = function () {
2687 var r = window.Raphael;
2688 delete window.Raphael;
2689 if (oldRaphael.was) {
2690 window.Raphael = oldRaphael.is;