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");
580 SVG.canvas.appendChild(el);
582 var p = new Element(el, SVG);
585 p.last = {x: 0, y: 0, bx: 0, by: 0};
586 p.absolutely = function () {
587 this.isAbsolute = true;
590 p.relatively = function () {
591 this.isAbsolute = false;
594 p.moveTo = function (x, y) {
595 var d = this.isAbsolute?"M":"m";
596 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
597 var oldD = this[0].getAttribute("d") || "";
598 (oldD == "M0,0") && (oldD = "");
599 this[0].setAttribute("d", oldD + d);
600 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
601 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
602 this.attrs.path = oldD + d;
605 p.lineTo = function (x, y) {
606 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
607 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
608 var d = this.isAbsolute?"L":"l";
609 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
610 var oldD = this[0].getAttribute("d") || "";
611 this[0].setAttribute("d", oldD + d);
612 this.attrs.path = oldD + d;
615 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
616 var d = this.isAbsolute ? "A" : "a";
617 d += [parseFloat(rx).toFixed(3), parseFloat(ry).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x).toFixed(3), parseFloat(y).toFixed(3)].join(" ");
618 var oldD = this[0].getAttribute("d") || "";
619 this[0].setAttribute("d", oldD + d);
620 this.last.x = parseFloat(x);
621 this.last.y = parseFloat(y);
622 this.attrs.path = oldD + d;
625 p.cplineTo = function (x1, y1, w1) {
627 return this.lineTo(x1, y1);
630 var x = parseFloat(x1);
631 var y = parseFloat(y1);
632 var w = parseFloat(w1);
633 var d = this.isAbsolute?"C":"c";
634 var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
635 for (var i = 0, ii = attr.length; i < ii; i++) {
636 d += attr[i].toFixed(3) + " ";
638 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
639 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
640 this.last.bx = attr[2];
641 this.last.by = attr[3];
642 var oldD = this[0].getAttribute("d") || "";
643 this[0].setAttribute("d", oldD + d);
644 this.attrs.path = oldD + d;
648 p.curveTo = function () {
650 command = [0, 1, 2, 3, "s", 5, "c"];
652 var d = command[arguments.length];
653 if (this.isAbsolute) {
656 for (var i = 0, ii = arguments.length; i < ii; i++) {
657 d += parseFloat(arguments[i]).toFixed(3) + " ";
659 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
660 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
661 this.last.bx = parseFloat(arguments[arguments.length - 4]);
662 this.last.by = parseFloat(arguments[arguments.length - 3]);
663 var oldD = this.node.getAttribute("d") || "";
664 this.node.setAttribute("d", oldD + d);
665 this.attrs.path = oldD + d;
668 p.qcurveTo = function () {
670 command = [0, 1, "t", 3, "q"];
672 var d = command[arguments.length];
673 if (this.isAbsolute) {
676 for (var i = 0, ii = arguments.length; i < ii; i++) {
677 d += parseFloat(arguments[i]).toFixed(3) + " ";
679 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
680 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
681 if (arguments.length != 2) {
682 this.last.qx = parseFloat(arguments[arguments.length - 4]);
683 this.last.qy = parseFloat(arguments[arguments.length - 3]);
685 var oldD = this.node.getAttribute("d") || "";
686 this.node.setAttribute("d", oldD + d);
687 this.attrs.path = oldD + d;
690 p.addRoundedCorner = function (r, dir) {
691 var R = .5522 * r, rollback = this.isAbsolute, o = this;
694 rollback = function () {
698 rollback = function () {};
704 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
707 o.curveTo(-R, 0, -r, r - R, -r, r);
714 o.curveTo(R, 0, r, -(r - R), r, -r);
717 o.curveTo(R, 0, r, r - R, r, r);
724 o.curveTo(0, -R, -(R - r), -r, r, -r);
727 o.curveTo(0, -R, R - r, -r, -r, -r);
734 o.curveTo(0, R, -(R - r), r, r, r);
737 o.curveTo(0, R, R - r, r, -r, r);
742 actions[dir[0]]()[dir[1]]();
746 p.andClose = function () {
747 var oldD = this[0].getAttribute("d") || "";
748 this[0].setAttribute("d", oldD + "Z ");
749 this.attrs.path = oldD + "Z ";
753 p.attrs.path = "" + pathString;
755 paper.pathfinder(p, p.attrs.path);
758 params.fill = params.fill || "none";
759 params.stroke = params.stroke || "#000";
761 params = {fill: "none", stroke: "#000"};
763 setFillAndStroke(p, params);
766 var addGrdientFill = function (o, gradient, SVG) {
767 gradient = toGradient(gradient);
768 var el = doc.createElementNS(SVG.svgns, (gradient.type || "linear") + "Gradient");
769 el.id = "raphael-gradient-" + R.idGenerator++;
770 if (gradient.vector && gradient.vector.length) {
771 el.setAttribute("x1", gradient.vector[0]);
772 el.setAttribute("y1", gradient.vector[1]);
773 el.setAttribute("x2", gradient.vector[2]);
774 el.setAttribute("y2", gradient.vector[3]);
776 SVG.defs.appendChild(el);
777 var isopacity = true;
778 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
779 var stop = doc.createElementNS(SVG.svgns, "stop");
780 if (gradient.dots[i].offset) {
783 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
784 stop.setAttribute("stop-color", getRGB(gradient.dots[i].color).hex || "#fff");
785 // ignoring opacity for internal points, because VML doesn't support it
786 el.appendChild(stop);
788 if (isopacity && typeof gradient.dots[ii - 1].opacity != "undefined") {
789 stop.setAttribute("stop-opacity", gradient.dots[ii - 1].opacity);
791 o.setAttribute("fill", "url(#" + el.id + ")");
793 o.style.fillOpacity = 1;
794 o.setAttribute("opacity", 1);
795 o.setAttribute("fill-opacity", 1);
797 var updatePosition = function (o) {
799 var bbox = o.getBBox();
800 o.pattern.setAttribute("patternTransform", "translate(" + [bbox.x, bbox.y].join(",") + ")");
803 var setFillAndStroke = function (o, params) {
808 "-..": [3, 1, 1, 1, 1, 1],
814 "--..": [8, 3, 1, 3, 1, 3]
816 addDashes = function (o, value) {
817 value = dasharray[value.toString().toLowerCase()];
819 var width = o.attrs["stroke-width"] || "1",
820 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
822 for (var i = 0, ii = value.length; i < ii; i++) {
823 dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
825 value = dashes.join(",");
826 o.node.setAttribute("stroke-dasharray", value);
829 for (var att in params) {
830 if (!(att in availableAttrs)) {
833 var value = params[att];
834 o.attrs[att] = value;
835 if (att == "x" && o.attrs.dx) {
838 if (att == "y" && o.attrs.dy) {
846 var pn = o.node.parentNode;
847 if (pn.tagName.toLowerCase() != "a") {
848 var hl = doc.createElementNS(o.svg.svgns, "a");
849 pn.insertBefore(hl, o.node);
850 hl.appendChild(o.node);
853 pn.setAttributeNS(o.svg.xlink, att, value);
856 if (o.type == "path") {
857 o.node.setAttribute("d", "M0,0");
858 paper.pathfinder(o, value);
863 o.node.setAttribute(att, value);
869 o.node.setAttribute(att, value);
873 o.node.setAttribute(att, value);
876 o.node.setAttribute(att, value);
879 if (o.type == "rect") {
880 o.node.setAttribute("rx", value);
881 o.node.setAttribute("ry", value);
883 o.node.setAttribute(att, value);
887 if (o.type == "image") {
888 o.node.setAttributeNS(o.svg.xlink, "href", value);
892 o.node.style.strokeWidth = value;
893 // Need following line for Firefox
894 o.node.setAttribute(att, value);
895 if (o.attrs["stroke-dasharray"]) {
896 addDashes(o, o.attrs["stroke-dasharray"]);
899 case "stroke-dasharray":
903 o.rotate(value, true);
906 var xy = (value + "").split(separator);
907 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
910 var xy = (value + "").split(separator);
911 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
914 var isURL = value.match(/^url\(([^\)]+)\)$/i);
916 var el = doc.createElementNS(o.svg.svgns, "pattern");
917 var ig = doc.createElementNS(o.svg.svgns, "image");
918 el.id = "raphael-pattern-" + R.idGenerator++;
919 el.setAttribute("x", 0);
920 el.setAttribute("y", 0);
921 el.setAttribute("patternUnits", "userSpaceOnUse");
922 ig.setAttribute("x", 0);
923 ig.setAttribute("y", 0);
924 ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
927 var img = doc.createElement("img");
928 img.style.position = "absolute";
929 img.style.top = "-9999em";
930 img.style.left = "-9999em";
931 img.onload = function () {
932 el.setAttribute("width", this.offsetWidth);
933 el.setAttribute("height", this.offsetHeight);
934 ig.setAttribute("width", this.offsetWidth);
935 ig.setAttribute("height", this.offsetHeight);
936 doc.body.removeChild(this);
939 doc.body.appendChild(img);
941 o.svg.defs.appendChild(el);
942 o.node.style.fill = "url(#" + el.id + ")";
943 o.node.setAttribute("fill", "url(#" + el.id + ")");
948 delete params.gradient;
949 delete o.attrs.gradient;
950 if (typeof o.attrs.opacity != "undefined" && typeof params.opacity == "undefined" ) {
951 o.node.style.opacity = o.attrs.opacity;
952 // Need following line for Firefox
953 o.node.setAttribute("opacity", o.attrs.opacity);
955 if (typeof o.attrs["fill-opacity"] != "undefined" && typeof params["fill-opacity"] == "undefined" ) {
956 o.node.style.fillOpacity = o.attrs["fill-opacity"];
957 // Need following line for Firefox
958 o.node.setAttribute("fill-opacity", o.attrs["fill-opacity"]);
961 o.node.style[att] = getRGB(value).hex;
962 // Need following line for Firefox
963 o.node.setAttribute(att, getRGB(value).hex);
966 addGrdientFill(o.node, value, o.svg);
970 if (o.attrs.gradient) {
971 var gradient = doc.getElementById(o.node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
973 var stops = gradient.getElementsByTagName("stop");
974 stops[stops.length - 1].setAttribute("stop-opacity", value);
979 var cssrule = att.replace(/(\-.)/g, function (w) {
980 return w.substring(1).toUpperCase();
982 o.node.style[cssrule] = value;
983 // Need following line for Firefox
984 o.node.setAttribute(att, value);
991 var tuneText = function (el, params) {
992 if (el.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params)) {
995 var fontSize = el.node.firstChild ? parseInt(doc.defaultView.getComputedStyle(el.node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
997 if ("text" in params) {
998 while (el.node.firstChild) {
999 el.node.removeChild(el.node.firstChild);
1001 var texts = (params.text + "").split("\n");
1002 for (var i = 0, ii = texts.length; i < ii; i++) {
1003 var tspan = doc.createElementNS(el.svg.svgns, "tspan");
1004 i && tspan.setAttribute("dy", fontSize * leading);
1005 i && tspan.setAttribute("x", el.attrs.x);
1006 tspan.appendChild(doc.createTextNode(texts[i]));
1007 el.node.appendChild(tspan);
1010 var texts = el.node.getElementsByTagName("tspan");
1011 for (var i = 0, ii = texts.length; i < ii; i++) {
1012 i && texts[i].setAttribute("dy", fontSize * leading);
1013 i && texts[i].setAttribute("x", el.attrs.x);
1016 el.node.setAttribute("y", el.attrs.y);
1017 var bb = el.getBBox();
1018 var dif = el.attrs.y - (bb.y + bb.height / 2);
1020 el.node.setAttribute("y", el.attrs.y + dif);
1023 var Element = function (node, svg) {
1029 this.attrs = this.attrs || {};
1030 this.transformations = []; // rotate, translate, scale
1034 rt: {deg: 0, x: 0, y: 0},
1039 Element.prototype.rotate = function (deg, cx, cy) {
1041 return this._.rt.deg;
1043 var bbox = this.getBBox();
1044 deg = deg.toString().split(separator);
1045 if (deg.length - 1) {
1046 cx = parseFloat(deg[1]);
1047 cy = parseFloat(deg[2]);
1049 deg = parseFloat(deg[0]);
1051 this._.rt.deg = deg;
1053 this._.rt.deg += deg;
1058 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1059 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1060 if (this._.rt.deg) {
1061 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
1063 this.transformations[0] = "";
1065 this.node.setAttribute("transform", this.transformations.join(" "));
1068 Element.prototype.hide = function () {
1069 this.node.style.display = "none";
1072 Element.prototype.show = function () {
1073 this.node.style.display = "block";
1076 Element.prototype.remove = function () {
1077 this.node.parentNode.removeChild(this.node);
1079 Element.prototype.getBBox = function () {
1080 var bbox = this.node.getBBox();
1081 if (this.type == "text") {
1082 bbox = {x: bbox.x, y: Infinity, width: bbox.width, height: 0};
1083 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1084 var bb = this.node.getExtentOfChar(i);
1085 (bb.y < bbox.y) && (bbox.y = bb.y);
1086 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1091 Element.prototype.attr = function () {
1092 if (arguments.length == 1 && typeof arguments[0] == "string") {
1093 if (arguments[0] == "translation") {
1094 return this.translate();
1096 return this.attrs[arguments[0]];
1098 if (arguments.length == 1 && arguments[0] instanceof win.Array) {
1100 for (var j in arguments[0]) {
1101 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1105 if (arguments.length == 2) {
1107 params[arguments[0]] = arguments[1];
1108 setFillAndStroke(this, params);
1109 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1110 setFillAndStroke(this, arguments[0]);
1114 Element.prototype.toFront = function () {
1115 this.node.parentNode.appendChild(this.node);
1118 Element.prototype.toBack = function () {
1119 if (this.node.parentNode.firstChild != this.node) {
1120 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1124 Element.prototype.insertAfter = function (element) {
1125 if (element.node.nextSibling) {
1126 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1128 element.node.parentNode.appendChild(this.node);
1132 Element.prototype.insertBefore = function (element) {
1133 element.node.parentNode.insertBefore(this.node, element.node);
1136 var theCircle = function (svg, x, y, r) {
1137 var el = doc.createElementNS(svg.svgns, "circle");
1138 el.setAttribute("cx", x);
1139 el.setAttribute("cy", y);
1140 el.setAttribute("r", r);
1141 el.setAttribute("fill", "none");
1142 el.setAttribute("stroke", "#000");
1144 svg.canvas.appendChild(el);
1146 var res = new Element(el, svg);
1147 res.attrs = res.attrs || {};
1151 res.attrs.stroke = "#000";
1152 res.type = "circle";
1155 var theRect = function (svg, x, y, w, h, r) {
1156 var el = doc.createElementNS(svg.svgns, "rect");
1157 el.setAttribute("x", x);
1158 el.setAttribute("y", y);
1159 el.setAttribute("width", w);
1160 el.setAttribute("height", h);
1162 el.setAttribute("rx", r);
1163 el.setAttribute("ry", r);
1165 el.setAttribute("fill", "none");
1166 el.setAttribute("stroke", "#000");
1168 svg.canvas.appendChild(el);
1170 var res = new Element(el, svg);
1171 res.attrs = res.attrs || {};
1174 res.attrs.width = w;
1175 res.attrs.height = h;
1176 res.attrs.stroke = "#000";
1178 res.attrs.rx = res.attrs.ry = r;
1183 var theEllipse = function (svg, x, y, rx, ry) {
1184 var el = doc.createElementNS(svg.svgns, "ellipse");
1185 el.setAttribute("cx", x);
1186 el.setAttribute("cy", y);
1187 el.setAttribute("rx", rx);
1188 el.setAttribute("ry", ry);
1189 el.setAttribute("fill", "none");
1190 el.setAttribute("stroke", "#000");
1192 svg.canvas.appendChild(el);
1194 var res = new Element(el, svg);
1195 res.attrs = res.attrs || {};
1200 res.attrs.stroke = "#000";
1201 res.type = "ellipse";
1204 var theImage = function (svg, src, x, y, w, h) {
1205 var el = doc.createElementNS(svg.svgns, "image");
1206 el.setAttribute("x", x);
1207 el.setAttribute("y", y);
1208 el.setAttribute("width", w);
1209 el.setAttribute("height", h);
1210 el.setAttribute("preserveAspectRatio", "none");
1211 el.setAttributeNS(svg.xlink, "href", src);
1213 svg.canvas.appendChild(el);
1215 var res = new Element(el, svg);
1216 res.attrs = res.attrs || {};
1219 res.attrs.width = w;
1220 res.attrs.height = h;
1224 var theText = function (svg, x, y, text) {
1225 var el = doc.createElementNS(svg.svgns, "text");
1226 el.setAttribute("x", x);
1227 el.setAttribute("y", y);
1228 el.setAttribute("text-anchor", "middle");
1230 svg.canvas.appendChild(el);
1232 var res = new Element(el, svg);
1233 res.attrs = res.attrs || {};
1237 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000", text: text});
1240 var theGroup = function (svg) {
1241 var el = doc.createElementNS(svg.svgns, "g");
1243 svg.canvas.appendChild(el);
1245 var i = new Element(el, svg);
1246 for (var f in svg) {
1247 if (f[0] != "_" && typeof svg[f] == "function") {
1248 i[f] = (function (f) {
1249 return function () {
1250 var e = svg[f].apply(svg, arguments);
1251 el.appendChild(e[0]);
1260 var setSize = function (width, height) {
1261 this.width = width || this.width;
1262 this.height = height || this.height;
1263 this.canvas.setAttribute("width", this.width);
1264 this.canvas.setAttribute("height", this.height);
1267 var create = function () {
1268 var con = getContainer.apply(null, arguments);
1269 var container = con.container,
1273 height = con.height;
1275 throw new Error("SVG container not found.");
1277 paper.canvas = doc.createElementNS(paper.svgns, "svg");
1278 paper.canvas.setAttribute("width", width || 320);
1279 paper.width = width || 320;
1280 paper.canvas.setAttribute("height", height || 200);
1281 paper.height = height || 200;
1282 if (container == 1) {
1283 doc.body.appendChild(paper.canvas);
1284 paper.canvas.style.position = "absolute";
1285 paper.canvas.style.left = x + "px";
1286 paper.canvas.style.top = y + "px";
1288 if (container.firstChild) {
1289 container.insertBefore(paper.canvas, container.firstChild);
1291 container.appendChild(paper.canvas);
1295 canvas: paper.canvas,
1296 clear: function () {
1297 while (this.canvas.firstChild) {
1298 this.canvas.removeChild(this.canvas.firstChild);
1300 this.defs = doc.createElementNS(paper.svgns, "defs");
1301 this.canvas.appendChild(this.defs);
1304 for (var prop in paper) {
1305 if (prop != "create") {
1306 container[prop] = paper[prop];
1309 plugins(container, container, R.fn);
1313 paper.remove = function () {
1314 this.canvas.parentNode.removeChild(this.canvas);
1316 paper.svgns = "http://www.w3.org/2000/svg";
1317 paper.xlink = "http://www.w3.org/1999/xlink";
1318 paper.safari = function () {
1319 if (navigator.vendor == "Apple Computer, Inc.") {
1320 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1321 setTimeout(function () {rect.remove();}, 0);
1328 thePath = function (params, pathString, VML) {
1329 var g = createNode("group"), gl = g.style;
1330 gl.position = "absolute";
1333 gl.width = VML.width + "px";
1334 gl.height = VML.height + "px";
1335 var el = createNode("shape"), ol = el.style;
1336 ol.width = VML.width + "px";
1337 ol.height = VML.height + "px";
1339 if (params["class"]) {
1340 el.className = "rvml " + params["class"];
1342 el.coordsize = this.coordsize;
1343 el.coordorigin = this.coordorigin;
1345 VML.canvas.appendChild(g);
1346 var p = new Element(el, g, VML);
1347 p.isAbsolute = true;
1350 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1352 p.absolutely = function () {
1353 this.isAbsolute = true;
1356 p.relatively = function () {
1357 this.isAbsolute = false;
1360 p.moveTo = function (x, y) {
1361 var d = this.isAbsolute?"m":"t";
1362 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1363 this.node.path = this.Path += d;
1364 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1365 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1366 this.last.isAbsolute = this.isAbsolute;
1367 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1370 p.lineTo = function (x, y) {
1371 var d = this.isAbsolute?"l":"r";
1372 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1373 this[0].path = this.Path += d;
1374 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1375 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1376 this.last.isAbsolute = this.isAbsolute;
1377 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1380 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1381 // for more information of where this math came from visit:
1382 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1383 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1384 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1385 var x1 = this.last.x,
1389 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1390 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1391 cx = k * rx * y / ry + (x1 + x2) / 2,
1392 cy = k * -ry * x / rx + (y1 + y2) / 2,
1393 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1394 left = Math.round(cx - rx),
1395 top = Math.round(cy - ry);
1396 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(", ");
1397 this.node.path = this.Path += d;
1398 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2);
1399 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2);
1400 this.last.isAbsolute = this.isAbsolute;
1401 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1404 p.cplineTo = function (x1, y1, w1) {
1406 return this.lineTo(x1, y1);
1408 var x = Math.round(Math.round(parseFloat(x1) * 100) / 100),
1409 y = Math.round(Math.round(parseFloat(y1) * 100) / 100),
1410 w = Math.round(Math.round(parseFloat(w1) * 100) / 100),
1411 d = this.isAbsolute ? "c" : "v",
1412 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1413 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1414 d += attr.join(" ") + " ";
1415 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1416 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1417 this.last.bx = attr[2];
1418 this.last.by = attr[3];
1419 this.node.path = this.Path += d;
1420 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1424 p.curveTo = function () {
1425 var d = this.isAbsolute ? "c" : "v";
1426 if (arguments.length == 6) {
1427 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1428 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1429 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4]);
1430 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5]);
1431 d += [Math.round(parseFloat(arguments[0])),
1432 Math.round(parseFloat(arguments[1])),
1433 Math.round(parseFloat(arguments[2])),
1434 Math.round(parseFloat(arguments[3])),
1435 Math.round(parseFloat(arguments[4])),
1436 Math.round(parseFloat(arguments[5]))].join(" ") + " ";
1437 this.last.isAbsolute = this.isAbsolute;
1438 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1440 if (arguments.length == 4) {
1441 var bx = this.last.x * 2 - this.last.bx;
1442 var by = this.last.y * 2 - this.last.by;
1443 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1444 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1445 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1446 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1447 d += [Math.round(bx), Math.round(by),
1448 Math.round(parseFloat(arguments[0])),
1449 Math.round(parseFloat(arguments[1])),
1450 Math.round(parseFloat(arguments[2])),
1451 Math.round(parseFloat(arguments[3]))].join(" ") + " ";
1452 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1454 this.node.path = this.Path += d;
1457 p.qcurveTo = function () {
1459 if (arguments.length == 4) {
1460 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1461 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1462 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1463 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1464 d += [Math.round(this.last.qx),
1465 Math.round(this.last.qy),
1466 Math.round(this.last.x),
1467 Math.round(this.last.y)].join(" ") + " ";
1468 this.last.isAbsolute = this.isAbsolute;
1469 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1471 if (arguments.length == 2) {
1472 this.last.qx = this.last.x * 2 - this.last.qx;
1473 this.last.qy = this.last.y * 2 - this.last.qy;
1474 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1475 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1476 d += [Math.round(this.last.qx),
1477 Math.round(this.last.qy),
1478 Math.round(this.last.x),
1479 Math.round(this.last.y)].join(" ") + " ";
1480 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1482 this.node.path = this.Path += d;
1483 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1486 p.addRoundedCorner = function (r, dir) {
1487 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1490 rollback = function () {
1494 rollback = function () {};
1500 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1503 o.curveTo(-R, 0, -r, r - R, -r, r);
1510 o.curveTo(R, 0, r, -(r - R), r, -r);
1513 o.curveTo(R, 0, r, r - R, r, r);
1520 o.curveTo(0, -R, -(R - r), -r, r, -r);
1523 o.curveTo(0, -R, R - r, -r, -r, -r);
1530 o.curveTo(0, R, -(R - r), r, r, r);
1533 o.curveTo(0, R, R - r, r, -r, r);
1538 actions[dir.charAt(0)]()[dir.charAt(1)]();
1542 p.andClose = function () {
1543 this.node.path = (this.Path += "x");
1544 this.attrs.path += "z";
1550 paper.pathfinder(p, "" + pathString);
1554 params.fill = params.fill || "none";
1555 params.stroke = params.stroke || "#000";
1557 params = {fill: "none", stroke: "#000"};
1559 setFillAndStroke(p, params);
1560 if (params.gradient) {
1561 addGrdientFill(p, params.gradient);
1565 var setFillAndStroke = function (o, params) {
1566 var s = o.node.style,
1568 o.attrs = o.attrs || {};
1569 for (var par in params) {
1570 o.attrs[par] = params[par];
1572 params.href && (o.node.href = params.href);
1573 params.title && (o.node.title = params.title);
1574 params.target && (o.node.target = params.target);
1575 if (params.path && o.type == "path") {
1578 paper.pathfinder(o, params.path);
1580 if (params.rotation != null) {
1581 o.rotate(params.rotation, true);
1583 if (params.translation) {
1584 var xy = (params.translation + "").split(separator);
1585 o.translate(xy[0], xy[1]);
1588 var xy = (params.scale + "").split(separator);
1589 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1591 if (o.type == "image" && params.src) {
1592 o.node.src = params.src;
1594 if (o.type == "image" && params.opacity) {
1595 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1596 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1598 params.font && (s.font = params.font);
1599 params["font-family"] && (s.fontFamily = params["font-family"]);
1600 params["font-size"] && (s.fontSize = params["font-size"]);
1601 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1602 params["font-style"] && (s.fontStyle = params["font-style"]);
1603 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"]) {
1604 o = o.shape || o.node;
1605 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || createNode("fill");
1606 if ("fill-opacity" in params || "opacity" in params) {
1607 fill.opacity = ((+params["fill-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1612 if (typeof fill.on == "undefined" || params.fill == "none") {
1615 if (fill.on && params.fill) {
1616 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1618 fill.src = isURL[1];
1621 fill.color = getRGB(params.fill).hex;
1623 fill.type = "solid";
1626 o.appendChild(fill);
1627 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || createNode("stroke");
1628 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"]) {
1631 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1634 if (stroke.on && params.stroke) {
1635 stroke.color = getRGB(params.stroke).hex;
1637 stroke.opacity = ((+params["stroke-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1638 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1639 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1640 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1641 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"]) || 1) * 12 / 16);
1642 if (params["stroke-dasharray"]) {
1646 "-.": "shortdashdot",
1647 "-..": "shortdashdotdot",
1652 "--.": "longdashdot",
1653 "--..": "longdashdotdot"
1655 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1657 o.appendChild(stroke);
1659 if (res.type == "text") {
1660 var span = doc.createElement("span"),
1665 s.display = "inline";
1666 res.attrs.font && (s.font = res.attrs.font);
1667 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1668 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1669 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1670 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1671 res.node.parentNode.appendChild(span);
1672 span.innerText = res.node.string;
1673 res.W = res.attrs.w = span.offsetWidth;
1674 res.H = res.attrs.h = span.offsetHeight;
1675 res.X = res.attrs.x;
1676 res.Y = res.attrs.y + Math.round(res.H / 2);
1677 res.node.parentNode.removeChild(span);
1679 // text-anchor emulation
1680 switch (res.attrs["text-anchor"]) {
1682 res.node.style["v-text-align"] = "left";
1683 res.bbx = Math.round(res.W / 2);
1686 res.node.style["v-text-align"] = "right";
1687 res.bbx = -Math.round(res.W / 2);
1690 res.node.style["v-text-align"] = "center";
1695 var getAngle = function (a, b, c, d) {
1696 var angle = Math.round(Math.atan((parseFloat(c) - parseFloat(a)) / (parseFloat(d) - parseFloat(b))) * 57.29) || 0;
1697 if (!angle && parseFloat(a) < parseFloat(b)) {
1706 var addGrdientFill = function (o, gradient) {
1707 gradient = toGradient(gradient);
1708 o.attrs = o.attrs || {};
1709 var attrs = o.attrs;
1710 o.attrs.gradient = gradient;
1711 o = o.shape || o[0];
1712 var fill = o.getElementsByTagName("fill");
1716 fill = createNode("fill");
1718 if (gradient.dots.length) {
1720 fill.method = "none";
1721 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1722 if (typeof gradient.dots[0].color != "undefined") {
1723 fill.color = getRGB(gradient.dots[0].color).hex;
1725 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1726 fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1729 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1730 if (gradient.dots[i].offset) {
1731 colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1734 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;
1735 if (colors.length) {
1736 fill.colors.value = colors.join(",");
1737 fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1739 fill.colors.value = "0% " + fill.color;
1741 fill.opacity = fillOpacity;
1742 if (typeof gradient.angle != "undefined") {
1743 fill.angle = (-gradient.angle + 270) % 360;
1744 } else if (gradient.vector) {
1745 fill.angle = getAngle.apply(null, gradient.vector);
1747 if ((gradient.type + "").toLowerCase() == "radial") {
1748 fill.focus = "100%";
1749 fill.focusposition = "0.5 0.5";
1753 var Element = function (node, group, vml) {
1773 Element.prototype.rotate = function (deg, cx, cy) {
1775 return this._.rt.deg;
1777 deg = deg.toString().split(separator);
1778 if (deg.length - 1) {
1779 cx = parseFloat(deg[1]);
1780 cy = parseFloat(deg[2]);
1782 deg = parseFloat(deg[0]);
1784 this._.rt.deg = deg;
1786 this._.rt.deg += deg;
1793 this.setBox(null, cx, cy);
1794 this.Group.style.rotation = this._.rt.deg;
1797 Element.prototype.setBox = function (params, cx, cy) {
1798 var gs = this.Group.style,
1799 os = (this.shape && this.shape.style) || this.node.style;
1800 for (var i in params) {
1801 this.attrs[i] = params[i];
1803 cx = cx || this._.rt.cx;
1804 cy = cy || this._.rt.cy;
1805 var attr = this.attrs, x, y, w, h;
1806 switch (this.type) {
1808 x = attr.cx - attr.r;
1809 y = attr.cy - attr.r;
1813 x = attr.cx - attr.rx;
1814 y = attr.cy - attr.ry;
1822 w = attr.width || 0;
1823 h = attr.height || 0;
1826 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("");
1827 x = attr.x - Math.round(this.W / 2);
1828 y = attr.y - this.H / 2;
1833 if (!this.attrs.path) {
1837 h = this.vml.height;
1839 var dim = pathDimensions(this.attrs.path),
1850 h = this.vml.height;
1853 cx = (cx == null) ? x + w / 2 : cx;
1854 cy = (cy == null) ? y + h / 2 : cy;
1855 var left = cx - this.vml.width / 2,
1856 top = cy - this.vml.height / 2;
1857 if (this.type == "path" || this.type == "text") {
1858 (gs.left != left + "px") && (gs.left = left + "px");
1859 (gs.top != top + "px") && (gs.top = top + "px");
1860 this.X = this.type == "text" ? x : -left;
1861 this.Y = this.type == "text" ? y : -top;
1864 (os.left != -left + "px") && (os.left = -left + "px");
1865 (os.top != -top + "px") && (os.top = -top + "px");
1867 (gs.left != left + "px") && (gs.left = left + "px");
1868 (gs.top != top + "px") && (gs.top = top + "px");
1873 (gs.width != this.vml.width + "px") && (gs.width = this.vml.width + "px");
1874 (gs.height != this.vml.height + "px") && (gs.height = this.vml.height + "px");
1875 (os.left != x - left + "px") && (os.left = x - left + "px");
1876 (os.top != y - top + "px") && (os.top = y - top + "px");
1877 (os.width != w + "px") && (os.width = w + "px");
1878 (os.height != h + "px") && (os.height = h + "px");
1879 var arcsize = (+params.r || 0) / (Math.min(w, h));
1880 if (this.type == "rect" && this.arcsize != arcsize) {
1881 // We should replace element with the new one
1882 var o = createNode("roundrect");
1883 o.arcsize = arcsize;
1884 this.Group.appendChild(o);
1885 this.node.parentNode.removeChild(this.node);
1887 this.arcsize = arcsize;
1888 setFillAndStroke(this, this.attrs);
1889 this.setBox(this.attrs);
1893 Element.prototype.hide = function () {
1894 this.Group.style.display = "none";
1897 Element.prototype.show = function () {
1898 this.Group.style.display = "block";
1901 Element.prototype.getBBox = function () {
1902 if (this.type == "path") {
1903 return pathDimensions(this.attr("path"));
1906 x: this.X + (this.bbx || 0),
1912 Element.prototype.remove = function () {
1913 this[0].parentNode.removeChild(this[0]);
1914 this.Group.parentNode.removeChild(this.Group);
1915 this.shape && this.shape.parentNode.removeChild(this.shape);
1917 Element.prototype.attr = function () {
1918 if (arguments.length == 1 && typeof arguments[0] == "string") {
1919 if (arguments[0] == "translation") {
1920 return this.translate();
1922 return this.attrs[arguments[0]];
1924 if (this.attrs && arguments.length == 1 && arguments[0] instanceof win.Array) {
1926 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1927 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1932 if (arguments.length == 2) {
1934 params[arguments[0]] = arguments[1];
1936 if (arguments.length == 1 && typeof arguments[0] == "object") {
1937 params = arguments[0];
1940 if (params.gradient) {
1941 addGrdientFill(this, params.gradient);
1943 if (params.text && this.type == "text") {
1944 this.node.string = params.text;
1947 this.node.id = params.id;
1949 setFillAndStroke(this, params);
1950 this.setBox(params);
1954 Element.prototype.toFront = function () {
1955 this.Group.parentNode.appendChild(this.Group);
1958 Element.prototype.toBack = function () {
1959 if (this.Group.parentNode.firstChild != this.Group) {
1960 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1964 Element.prototype.insertAfter = function (element) {
1965 if (element.Group.nextSibling) {
1966 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1968 element.Group.parentNode.appendChild(this.Group);
1972 Element.prototype.insertBefore = function (element) {
1973 element.Group.parentNode.insertBefore(this.Group, element.Group);
1976 var theCircle = function (vml, x, y, r) {
1977 var g = createNode("group");
1978 var o = createNode("oval");
1980 var res = new Element(o, g, vml);
1981 res.type = "circle";
1982 setFillAndStroke(res, {stroke: "#000", fill: "none"});
1986 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1987 vml.canvas.appendChild(g);
1990 var theRect = function (vml, x, y, w, h, r) {
1991 var g = createNode("group"),
1992 o = createNode("roundrect"),
1993 arcsize = (+r || 0) / (Math.min(w, h));
1994 o.arcsize = arcsize;
1996 var res = new Element(o, g, vml);
1998 setFillAndStroke(res, {stroke: "#000"});
2004 res.arcsize = arcsize;
2005 res.setBox({x: x, y: y, width: w, height: h});
2006 vml.canvas.appendChild(g);
2009 var theEllipse = function (vml, x, y, rx, ry) {
2010 var g = createNode("group");
2011 var o = createNode("oval");
2013 var res = new Element(o, g, vml);
2014 res.type = "ellipse";
2015 setFillAndStroke(res, {stroke: "#000"});
2020 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2021 vml.canvas.appendChild(g);
2024 var theImage = function (vml, src, x, y, w, h) {
2025 var g = createNode("group");
2026 var o = createNode("image");
2029 var res = new Element(o, g, vml);
2035 res.setBox({x: x, y: y, width: w, height: h});
2036 vml.canvas.appendChild(g);
2039 var theText = function (vml, x, y, text) {
2040 var g = createNode("group"), gs = g.style;
2041 var el = createNode("shape"), ol = el.style;
2042 var path = createNode("path"), ps = path.style;
2043 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
2044 path.textpathok = true;
2045 ol.width = vml.width;
2046 ol.height = vml.height;
2047 gs.position = "absolute";
2050 gs.width = vml.width;
2051 gs.height = vml.height;
2052 var o = createNode("textpath");
2055 o.coordsize = vml.coordsize;
2056 o.coordorigin = vml.coordorigin;
2058 el.appendChild(path);
2060 var res = new Element(o, g, vml);
2062 res.textpath = path;
2068 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2070 vml.canvas.appendChild(g);
2073 var setSize = function (width, height) {
2074 this.width = width || this.width;
2075 this.height = height || this.height;
2076 this.canvas.style.width = this.width + "px";
2077 this.canvas.style.height = this.height + "px";
2078 this.canvas.parentNode.style.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2079 this.canvas.coordsize = this.width + " " + this.height;
2082 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2084 if (!doc.namespaces.rvml) {
2085 doc.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
2087 var createNode = function (tagName) {
2088 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2091 var createNode = function (tagName) {
2092 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2095 var create = function () {
2096 var con = getContainer.apply(null, arguments);
2097 var container = con.container,
2101 height = con.height;
2103 throw new Error("VML container not found.");
2105 var c = doc.createElement("div"),
2106 d = doc.createElement("div"),
2107 r = paper.canvas = createNode("group"),
2108 cs = c.style, rs = r.style;
2109 paper.width = width;
2110 paper.height = height;
2111 width = width || "320px";
2112 height = height || "200px";
2113 cs.clip = "rect(0 " + width + "px " + height + "px 0)";
2116 cs.position = "absolute";
2117 rs.position = "absolute";
2118 d.style.position = "relative";
2121 r.coordsize = (/%$/.test(width) ? width : parseFloat(width)) + " " + (/%$/.test(height) ? height : parseFloat(height));
2122 r.coordorigin = "0 0";
2124 var b = createNode("rect"), bs = b.style;
2125 bs.left = bs.top = 0;
2126 bs.width = rs.width;
2127 bs.height = rs.height;
2128 b.filled = b.stroked = "f";
2133 if (container == 1) {
2134 doc.body.appendChild(d);
2135 cs.position = "absolute";
2147 cs.width = container.style.width = width;
2148 cs.height = container.style.height = height;
2149 if (container.firstChild) {
2150 container.insertBefore(d, container.firstChild);
2152 container.appendChild(d);
2155 for (var prop in paper) {
2156 container[prop] = paper[prop];
2158 plugins(container, container, R.fn);
2159 container.clear = function () {
2161 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2162 if (r.childNodes[i] != b) {
2163 todel.push(r.childNodes[i]);
2166 for (i = 0, ii = todel.length; i < ii; i++) {
2167 r.removeChild(todel[i]);
2172 paper.remove = function () {
2173 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2175 paper.safari = function () {};
2181 var addEvent = (function () {
2182 if (doc.addEventListener) {
2183 return function (obj, type, fn, element) {
2184 var f = function (e) {
2185 return fn.call(element, e);
2187 obj.addEventListener(type, f, false);
2188 return function () {
2189 obj.removeEventListener(type, f, false);
2193 } else if (doc.attachEvent) {
2194 return function (obj, type, fn, element) {
2195 var f = function (e) {
2196 return fn.call(element, e || win.event);
2198 obj.attachEvent("on" + type, f);
2199 var detacher = function () {
2200 obj.detachEvent("on" + type, f);
2203 if (type == "mouseover") {
2204 obj.attachEvent("onmouseenter", f);
2205 return function () {
2206 obj.detachEvent("onmouseenter", f);
2209 } else if (type == "mouseout") {
2210 obj.attachEvent("onmouseleave", f);
2211 return function () {
2212 obj.detachEvent("onmouseleave", f);
2220 for (var i = events.length; i--;) {
2221 (function (eventName) {
2222 Element.prototype[eventName] = function (fn) {
2223 if (typeof fn == "function") {
2224 this.events = this.events || {};
2225 this.events[eventName] = this.events[eventName] || {};
2226 this.events[eventName][fn] = this.events[eventName][fn] || [];
2227 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2231 Element.prototype["un" + eventName] = function (fn) {
2233 this.events[eventName] &&
2234 this.events[eventName][fn] &&
2235 this.events[eventName][fn].length &&
2236 this.events[eventName][fn].shift()() &&
2237 !this.events[eventName][fn].length &&
2238 delete this.events[eventName][fn];
2243 paper.circle = function (x, y, r) {
2244 return theCircle(this, x, y, r);
2246 paper.rect = function (x, y, w, h, r) {
2247 return theRect(this, x, y, w, h, r);
2249 paper.ellipse = function (x, y, rx, ry) {
2250 return theEllipse(this, x, y, rx, ry);
2252 paper.path = function (params, pathString) {
2253 return thePath(params, pathString, this);
2255 paper.image = function (src, x, y, w, h) {
2256 return theImage(this, src, x, y, w, h);
2258 paper.text = function (x, y, text) {
2259 return theText(this, x, y, text);
2261 paper.group = function () {
2264 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2265 color = color || "#000";
2266 var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
2268 columnWidth = w / wv;
2269 for (var i = 1; i < hv; i++) {
2270 path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
2272 for (var i = 1; i < wv; i++) {
2273 path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
2275 return this.path({stroke: color, "stroke-width": 1}, path.join(","));
2277 paper.pathfinder = function (p, path) {
2279 M: function (x, y) {
2282 C: function (x1, y1, x2, y2, x3, y3) {
2283 this.curveTo(x1, y1, x2, y2, x3, y3);
2285 Q: function (x1, y1, x2, y2) {
2286 this.qcurveTo(x1, y1, x2, y2);
2288 T: function (x, y) {
2289 this.qcurveTo(x, y);
2291 S: function (x1, y1, x2, y2) {
2292 p.curveTo(x1, y1, x2, y2);
2294 L: function (x, y) {
2298 this.lineTo(x, this.last.y);
2301 this.lineTo(this.last.x, y);
2303 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2304 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2311 path = pathToAbsolute(path);
2312 for (var i = 0, ii = path.length; i < ii; i++) {
2313 var b = path[i].shift();
2314 commands[b].apply(p, path[i]);
2317 paper.set = function (itemsArray) {
2318 return new Set(itemsArray);
2320 paper.setSize = setSize;
2321 Element.prototype.stop = function () {
2322 clearTimeout(this.animation_in_progress);
2324 Element.prototype.scale = function (x, y, cx, cy) {
2325 if (x == null && y == null) {
2326 return {x: this._.sx, y: this._.sy, toString: function () { return this.x.toFixed(3) + " " + this.y.toFixed(3); }};
2330 var dx, dy, dcx, dcy;
2332 var bb = this.type == "path" ? pathDimensions(this.attrs.path) : this.getBBox(),
2333 rcx = bb.x + bb.width / 2,
2334 rcy = bb.y + bb.height / 2;
2335 cx = isNaN(cx) ? rcx : cx;
2336 cy = isNaN(cy) ? rcy : cy;
2337 var dirx = Math.round(x / Math.abs(x)),
2338 diry = Math.round(y / Math.abs(y)),
2339 s = this.node.style,
2340 ncx = cx + (rcx - cx) * x * dirx / this._.sx,
2341 ncy = cy + (rcy - cy) * y * diry / this._.sy;
2342 switch (this.type) {
2345 var neww = this.attrs.width * x * dirx / this._.sx,
2346 newh = this.attrs.height * y * diry / this._.sy,
2347 newx = ncx - neww / 2,
2348 newy = ncy - newh / 2;
2359 rx: this.attrs.rx * x / this._.sx,
2360 ry: this.attrs.ry * y / this._.sy,
2361 r: this.attrs.r * x / this._.sx,
2367 var path = pathToRelative(R.parsePathString(this.attrs.path)),
2369 for (var i = 0, ii = path.length; i < ii; i++) {
2370 if (path[i][0].toUpperCase() == "M" && skip) {
2375 if (this.svg && path[i][0].toUpperCase() == "A") {
2376 path[i][path[i].length - 2] *= x;
2377 path[i][path[i].length - 1] *= y;
2381 for (var j = 1, jj = path[i].length; j < jj; j++) {
2382 // path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2383 path[i][j] *= (j % 2) ? x / this._.sx : y / this._.sy;
2387 var dim2 = pathDimensions(path),
2388 dx = ncx - dim2.x - dim2.width / 2,
2389 dy = ncy - dim2.y - dim2.height / 2;
2390 path = pathToRelative(path);
2394 this.attr({path: path.join(" ")});
2397 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2398 if (this.transformations) {
2399 var bb = this.getBBox();
2400 this.transformations[2] = "scale(" + [dirx, diry] + ")";
2401 this.node.setAttribute("transform", this.transformations.join(" "));
2402 dx = (dirx == -1) ? -this.attrs.x * 2 - neww || bb.width : 0;
2403 dy = (diry == -1) ? -this.attrs.y * 2 - newh || bb.height : 0;
2404 this.attr({x: this.attrs.x + dx, y: this.attrs.y + dy});
2408 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2409 ", M12=0, M21=0, M22=" + diry +
2410 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2411 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2414 if (this.transformations) {
2415 this.transformations[2] = "";
2416 this.node.setAttribute("transform", this.transformations.join(" "));
2420 this.node.filterMatrix = "";
2421 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2429 Element.prototype.animate = function (params, ms, callback) {
2430 clearTimeout(this.animation_in_progress);
2435 for (var attr in params) {
2436 if (attr in availableAnimAttrs) {
2437 from[attr] = this.attr(attr);
2438 if (typeof from[attr] == "undefined") {
2439 from[attr] = availableAttrs[attr];
2441 to[attr] = params[attr];
2442 switch (availableAnimAttrs[attr]) {
2444 diff[attr] = (to[attr] - from[attr]) / ms;
2447 from[attr] = getRGB(from[attr]);
2448 var toColour = getRGB(to[attr]);
2450 r: (toColour.r - from[attr].r) / ms,
2451 g: (toColour.g - from[attr].g) / ms,
2452 b: (toColour.b - from[attr].b) / ms
2456 var pathes = pathEqualiser(from[attr], to[attr]);
2457 from[attr] = pathes[0];
2458 to[attr] = pathes[1];
2460 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2461 diff[attr][i] = [0];
2462 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2463 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2468 var values = params[attr].toString().split(separator),
2469 from2 = from[attr].toString().split(separator);
2470 if (attr == "translation") {
2471 from[attr] = [0, 0];
2472 diff[attr] = [values[0] / ms, values[1] / ms];
2473 } else if (attr == "rotation") {
2474 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2475 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2477 from[attr] = (from[attr] + "").split(separator);
2478 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2484 var start = +new Date,
2488 var time = +new Date - start,
2492 for (var attr in from) {
2493 switch (availableAnimAttrs[attr]) {
2495 now = +from[attr] + time * diff[attr];
2499 Math.round(from[attr].r + time * diff[attr].r),
2500 Math.round(from[attr].g + time * diff[attr].g),
2501 Math.round(from[attr].b + time * diff[attr].b)
2506 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2507 now[i] = [from[attr][i][0]];
2508 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2509 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2511 now[i] = now[i].join(" ");
2513 now = now.join(" ");
2516 if (attr == "translation") {
2517 var x = diff[attr][0] * (time - prev),
2518 y = diff[attr][1] * (time - prev);
2521 now = [x, y].join(" ");
2522 } else if (attr == "rotation") {
2523 now = +from[attr][0] + time * diff[attr][0];
2524 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2526 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2530 if (attr == "font-size") {
2531 set[attr] = now + "px";
2537 that.animation_in_progress = setTimeout(arguments.callee, 0);
2540 (t.x || t.y) && that.translate(-t.x, -t.y);
2542 clearTimeout(that.animation_in_progress);
2544 (typeof callback == "function") && callback.call(that);
2550 Element.prototype.translate = function (x, y) {
2552 return {x: this._.tx, y: this._.ty};
2556 switch (this.type) {
2559 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
2564 this.attr({x: this.attrs.x + (+x), y: this.attrs.y + (+y)});
2567 var path = pathToRelative(this.attrs.path);
2570 this.attr({path: path.join(" ")});
2577 var Set = function (itemsArray) {
2579 this.length = (itemsArray && itemsArray.length) || 0;
2580 if (itemsArray && itemsArray.constructor == Array) {
2581 for (var i = itemsArray.length; i--;) {
2582 if (itemsArray[i].constructor == Element) {
2583 this.items[this.items.length] = itemsArray[i];
2588 Set.prototype.push = function (item) {
2589 if (item && item.constructor == Element) {
2590 var len = this.items.length;
2591 this.items[len] = item;
2597 Set.prototype.pull = function (id) {
2598 var res = this.items.splice(id, 1)[0];
2599 for (var j = id, jj = this.items.length; j < jj; j++) {
2600 this[j] = this[j + 1];
2602 delete this[jj + 1];
2606 for (var method in Element.prototype) {
2607 Set.prototype[method] = (function (methodname) {
2608 return function () {
2609 for (var i = this.items.length; i--;) {
2610 this.items[i][methodname].apply(this.items[i], arguments);
2616 Set.prototype.getBBox = function () {
2617 var x = [], y = [], w = [], h = [];
2618 for (var i = this.items.length; i--;) {
2619 var box = this.items[i].getBBox();
2622 w.push(box.x + box.width);
2623 h.push(box.y + box.height);
2625 x = Math.min.apply(Math, x);
2626 y = Math.min.apply(Math, y);
2630 width: Math.max.apply(Math, w) - x,
2631 height: Math.max.apply(Math, h) - y
2635 R.registerFont = function (font) {
2639 this.fonts = this.fonts || {};
2640 if (this.fonts[font.face["font-family"]]) {
2641 this.fonts[font.face["font-family"]].push(font);
2643 this.fonts[font.face["font-family"]] = [font];
2646 font.face["units-per-em"] = parseInt(font.face["units-per-em"], 10);
2648 for (var glyph in font.glyphs) {
2649 var path = font.glyphs[glyph];
2651 path.d = "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
2652 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2658 paper.getFont = function (family, weight, style, stretch) {
2659 stretch = stretch || "normal";
2660 style = style || "normal";
2661 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2662 var font = R.fonts[family];
2664 var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, "") + "(\\s|$)", "i");
2665 for (var fontName in R.fonts) {
2666 if (name.test(fontName)) {
2667 font = R.fonts[fontName];
2674 for (var i = 0, ii = font.length; i < ii; i++) {
2676 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2683 paper.print = function (x, y, string, font, size) {
2684 var out = this.set(),
2685 letters = (string + "").split(""),
2690 scale = (size || 16) / font.face["units-per-em"];
2691 for (var i = 0, ii = letters.length; i < ii; i++) {
2692 var prev = i && font.glyphs[letters[i - 1]] || {},
2693 curr = font.glyphs[letters[i]];
2694 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
2695 curr && curr.d && out.push(this.path({fill: "#000", stroke: "none"}, curr.d).translate(shift, 0));
2697 out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
2702 R.noConflict = function () {
2703 var r = window.Raphael;
2704 delete window.Raphael;
2705 if (oldRaphael.was) {
2706 window.Raphael = oldRaphael.is;