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 (element, params) {
983 if (element.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params)) {
986 var fontSize = element.node.firstChild ? parseInt(doc.defaultView.getComputedStyle(element.node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
989 if ("text" in params) {
990 while (element.node.firstChild) {
991 element.node.removeChild(element.node.firstChild);
993 var texts = (params.text + "").split("\n");
994 for (var i = 0, ii = texts.length; i < ii; i++) {
995 var tspan = doc.createElementNS(element.svg.svgns, "tspan");
996 i && tspan.setAttribute("dy", fontSize * leading);
997 i && tspan.setAttribute("x", element.attrs.x);
998 tspan.appendChild(doc.createTextNode(texts[i]));
999 element.node.appendChild(tspan);
1000 height += fontSize * leading;
1003 var texts = element.node.getElementsByTagName("tspan");
1004 for (var i = 0, ii = texts.length; i < ii; i++) {
1005 i && texts[i].setAttribute("dy", fontSize * leading);
1006 i && texts[i].setAttribute("x", element.attrs.x);
1007 height += fontSize * leading;
1010 height -= fontSize * (leading - 1);
1011 var dif = height / 2 - fontSize;
1013 element.node.setAttribute("y", element.attrs.y - dif);
1016 var Element = function (node, svg) {
1022 this.attrs = this.attrs || {};
1023 this.transformations = []; // rotate, translate, scale
1027 rt: {deg: 0, x: 0, y: 0},
1032 Element.prototype.rotate = function (deg, cx, cy) {
1034 return this._.rt.deg;
1036 var bbox = this.getBBox();
1037 deg = deg.toString().split(separator);
1038 if (deg.length - 1) {
1039 cx = parseFloat(deg[1]);
1040 cy = parseFloat(deg[2]);
1042 deg = parseFloat(deg[0]);
1044 this._.rt.deg = deg;
1046 this._.rt.deg += deg;
1051 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1052 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1053 if (this._.rt.deg) {
1054 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
1056 this.transformations[0] = "";
1058 this.node.setAttribute("transform", this.transformations.join(" "));
1061 Element.prototype.hide = function () {
1062 this.node.style.display = "none";
1065 Element.prototype.show = function () {
1066 this.node.style.display = "block";
1069 Element.prototype.remove = function () {
1070 this.node.parentNode.removeChild(this.node);
1072 Element.prototype.getBBox = function () {
1073 var bbox = this.node.getBBox();
1074 if (this.type == "text") {
1075 var chr0 = this.node.getExtentOfChar(0);
1076 if (chr0.height > bbox.height) {
1077 var chrl = this.node.getExtentOfChar(this.node.getNumberOfChars() - 1);
1081 width: chrl.x - chr0.x + chrl.width,
1088 Element.prototype.attr = function () {
1089 if (arguments.length == 1 && typeof arguments[0] == "string") {
1090 if (arguments[0] == "translation") {
1091 return this.translate();
1093 return this.attrs[arguments[0]];
1095 if (arguments.length == 1 && arguments[0] instanceof win.Array) {
1097 for (var j in arguments[0]) {
1098 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1102 if (arguments.length == 2) {
1104 params[arguments[0]] = arguments[1];
1105 setFillAndStroke(this, params);
1106 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1107 setFillAndStroke(this, arguments[0]);
1111 Element.prototype.toFront = function () {
1112 this.node.parentNode.appendChild(this.node);
1115 Element.prototype.toBack = function () {
1116 if (this.node.parentNode.firstChild != this.node) {
1117 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1121 Element.prototype.insertAfter = function (element) {
1122 if (element.node.nextSibling) {
1123 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1125 element.node.parentNode.appendChild(this.node);
1129 Element.prototype.insertBefore = function (element) {
1130 element.node.parentNode.insertBefore(this.node, element.node);
1133 var theCircle = function (svg, x, y, r) {
1134 var el = doc.createElementNS(svg.svgns, "circle");
1135 el.setAttribute("cx", x);
1136 el.setAttribute("cy", y);
1137 el.setAttribute("r", r);
1138 el.setAttribute("fill", "none");
1139 el.setAttribute("stroke", "#000");
1141 svg.canvas.appendChild(el);
1143 var res = new Element(el, svg);
1144 res.attrs = res.attrs || {};
1148 res.attrs.stroke = "#000";
1149 res.type = "circle";
1152 var theRect = function (svg, x, y, w, h, r) {
1153 var el = doc.createElementNS(svg.svgns, "rect");
1154 el.setAttribute("x", x);
1155 el.setAttribute("y", y);
1156 el.setAttribute("width", w);
1157 el.setAttribute("height", h);
1159 el.setAttribute("rx", r);
1160 el.setAttribute("ry", r);
1162 el.setAttribute("fill", "none");
1163 el.setAttribute("stroke", "#000");
1165 svg.canvas.appendChild(el);
1167 var res = new Element(el, svg);
1168 res.attrs = res.attrs || {};
1171 res.attrs.width = w;
1172 res.attrs.height = h;
1173 res.attrs.stroke = "#000";
1175 res.attrs.rx = res.attrs.ry = r;
1180 var theEllipse = function (svg, x, y, rx, ry) {
1181 var el = doc.createElementNS(svg.svgns, "ellipse");
1182 el.setAttribute("cx", x);
1183 el.setAttribute("cy", y);
1184 el.setAttribute("rx", rx);
1185 el.setAttribute("ry", ry);
1186 el.setAttribute("fill", "none");
1187 el.setAttribute("stroke", "#000");
1189 svg.canvas.appendChild(el);
1191 var res = new Element(el, svg);
1192 res.attrs = res.attrs || {};
1197 res.attrs.stroke = "#000";
1198 res.type = "ellipse";
1201 var theImage = function (svg, src, x, y, w, h) {
1202 var el = doc.createElementNS(svg.svgns, "image");
1203 el.setAttribute("x", x);
1204 el.setAttribute("y", y);
1205 el.setAttribute("width", w);
1206 el.setAttribute("height", h);
1207 el.setAttribute("preserveAspectRatio", "none");
1208 el.setAttributeNS(svg.xlink, "href", src);
1210 svg.canvas.appendChild(el);
1212 var res = new Element(el, svg);
1213 res.attrs = res.attrs || {};
1216 res.attrs.width = w;
1217 res.attrs.height = h;
1221 var theText = function (svg, x, y, text) {
1222 var el = doc.createElementNS(svg.svgns, "text");
1223 el.setAttribute("x", x);
1224 el.setAttribute("y", y);
1225 el.setAttribute("text-anchor", "middle");
1227 svg.canvas.appendChild(el);
1229 var res = new Element(el, svg);
1230 res.attrs = res.attrs || {};
1234 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000", text: text});
1237 var theGroup = function (svg) {
1238 var el = doc.createElementNS(svg.svgns, "g");
1240 svg.canvas.appendChild(el);
1242 var i = new Element(el, svg);
1243 for (var f in svg) {
1244 if (f[0] != "_" && typeof svg[f] == "function") {
1245 i[f] = (function (f) {
1246 return function () {
1247 var e = svg[f].apply(svg, arguments);
1248 el.appendChild(e[0]);
1257 var setSize = function (width, height) {
1258 this.width = width || this.width;
1259 this.height = height || this.height;
1260 this.canvas.setAttribute("width", this.width);
1261 this.canvas.setAttribute("height", this.height);
1264 var create = function () {
1265 var con = getContainer.apply(null, arguments);
1266 var container = con.container,
1270 height = con.height;
1272 throw new Error("SVG container not found.");
1274 paper.canvas = doc.createElementNS(paper.svgns, "svg");
1275 paper.canvas.setAttribute("width", width || 320);
1276 paper.width = width || 320;
1277 paper.canvas.setAttribute("height", height || 200);
1278 paper.height = height || 200;
1279 if (container == 1) {
1280 doc.body.appendChild(paper.canvas);
1281 paper.canvas.style.position = "absolute";
1282 paper.canvas.style.left = x + "px";
1283 paper.canvas.style.top = y + "px";
1285 if (container.firstChild) {
1286 container.insertBefore(paper.canvas, container.firstChild);
1288 container.appendChild(paper.canvas);
1292 canvas: paper.canvas,
1293 clear: function () {
1294 while (this.canvas.firstChild) {
1295 this.canvas.removeChild(this.canvas.firstChild);
1297 this.defs = doc.createElementNS(paper.svgns, "defs");
1298 this.canvas.appendChild(this.defs);
1301 for (var prop in paper) {
1302 if (prop != "create") {
1303 container[prop] = paper[prop];
1306 plugins(container, container, R.fn);
1310 paper.remove = function () {
1311 this.canvas.parentNode.removeChild(this.canvas);
1313 paper.svgns = "http://www.w3.org/2000/svg";
1314 paper.xlink = "http://www.w3.org/1999/xlink";
1315 paper.safari = function () {
1316 if (navigator.vendor == "Apple Computer, Inc.") {
1317 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1318 setTimeout(function () {rect.remove();}, 0);
1325 thePath = function (params, pathString, VML) {
1326 var g = createNode("group"), gl = g.style;
1327 gl.position = "absolute";
1330 gl.width = VML.width + "px";
1331 gl.height = VML.height + "px";
1332 var el = createNode("shape"), ol = el.style;
1333 ol.width = VML.width + "px";
1334 ol.height = VML.height + "px";
1336 if (params["class"]) {
1337 el.className = "rvml " + params["class"];
1339 el.coordsize = this.coordsize;
1340 el.coordorigin = this.coordorigin;
1342 VML.canvas.appendChild(g);
1343 var p = new Element(el, g, VML);
1344 p.isAbsolute = true;
1347 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1349 p.absolutely = function () {
1350 this.isAbsolute = true;
1353 p.relatively = function () {
1354 this.isAbsolute = false;
1357 p.moveTo = function (x, y) {
1358 var d = this.isAbsolute?"m":"t";
1359 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1360 this.node.path = this.Path += d;
1361 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1362 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1363 this.last.isAbsolute = this.isAbsolute;
1364 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1367 p.lineTo = function (x, y) {
1368 var d = this.isAbsolute?"l":"r";
1369 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1370 this[0].path = this.Path += d;
1371 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1372 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1373 this.last.isAbsolute = this.isAbsolute;
1374 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1377 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1378 // for more information of where this math came from visit:
1379 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1380 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1381 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1382 var x1 = this.last.x,
1386 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1387 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1388 cx = k * rx * y / ry + (x1 + x2) / 2,
1389 cy = k * -ry * x / rx + (y1 + y2) / 2,
1390 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1391 left = Math.round(cx - rx),
1392 top = Math.round(cy - ry);
1393 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(", ");
1394 this.node.path = this.Path += d;
1395 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2);
1396 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2);
1397 this.last.isAbsolute = this.isAbsolute;
1398 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1401 p.cplineTo = function (x1, y1, w1) {
1403 return this.lineTo(x1, y1);
1405 var x = Math.round(Math.round(parseFloat(x1) * 100) / 100),
1406 y = Math.round(Math.round(parseFloat(y1) * 100) / 100),
1407 w = Math.round(Math.round(parseFloat(w1) * 100) / 100),
1408 d = this.isAbsolute ? "c" : "v",
1409 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1410 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1411 d += attr.join(" ") + " ";
1412 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1413 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1414 this.last.bx = attr[2];
1415 this.last.by = attr[3];
1416 this.node.path = this.Path += d;
1417 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1421 p.curveTo = function () {
1422 var d = this.isAbsolute ? "c" : "v";
1423 if (arguments.length == 6) {
1424 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1425 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1426 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4]);
1427 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5]);
1428 d += [Math.round(parseFloat(arguments[0])),
1429 Math.round(parseFloat(arguments[1])),
1430 Math.round(parseFloat(arguments[2])),
1431 Math.round(parseFloat(arguments[3])),
1432 Math.round(parseFloat(arguments[4])),
1433 Math.round(parseFloat(arguments[5]))].join(" ") + " ";
1434 this.last.isAbsolute = this.isAbsolute;
1435 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1437 if (arguments.length == 4) {
1438 var bx = this.last.x * 2 - this.last.bx;
1439 var by = this.last.y * 2 - this.last.by;
1440 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1441 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1442 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1443 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1444 d += [Math.round(bx), Math.round(by),
1445 Math.round(parseFloat(arguments[0])),
1446 Math.round(parseFloat(arguments[1])),
1447 Math.round(parseFloat(arguments[2])),
1448 Math.round(parseFloat(arguments[3]))].join(" ") + " ";
1449 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1451 this.node.path = this.Path += d;
1454 p.qcurveTo = function () {
1456 if (arguments.length == 4) {
1457 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1458 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1459 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1460 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1461 d += [Math.round(this.last.qx),
1462 Math.round(this.last.qy),
1463 Math.round(this.last.x),
1464 Math.round(this.last.y)].join(" ") + " ";
1465 this.last.isAbsolute = this.isAbsolute;
1466 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1468 if (arguments.length == 2) {
1469 this.last.qx = this.last.x * 2 - this.last.qx;
1470 this.last.qy = this.last.y * 2 - this.last.qy;
1471 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1472 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1473 d += [Math.round(this.last.qx),
1474 Math.round(this.last.qy),
1475 Math.round(this.last.x),
1476 Math.round(this.last.y)].join(" ") + " ";
1477 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1479 this.node.path = this.Path += d;
1480 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1483 p.addRoundedCorner = function (r, dir) {
1484 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1487 rollback = function () {
1491 rollback = function () {};
1497 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1500 o.curveTo(-R, 0, -r, r - R, -r, r);
1507 o.curveTo(R, 0, r, -(r - R), r, -r);
1510 o.curveTo(R, 0, r, r - R, r, r);
1517 o.curveTo(0, -R, -(R - r), -r, r, -r);
1520 o.curveTo(0, -R, R - r, -r, -r, -r);
1527 o.curveTo(0, R, -(R - r), r, r, r);
1530 o.curveTo(0, R, R - r, r, -r, r);
1535 actions[dir.charAt(0)]()[dir.charAt(1)]();
1539 p.andClose = function () {
1540 this.node.path = (this.Path += "x");
1541 this.attrs.path += "z";
1547 paper.pathfinder(p, "" + pathString);
1550 setFillAndStroke(p, params);
1551 if (params.gradient) {
1552 addGrdientFill(p, params.gradient);
1556 var setFillAndStroke = function (o, params) {
1557 var s = o.node.style,
1559 o.attrs = o.attrs || {};
1560 for (var par in params) {
1561 o.attrs[par] = params[par];
1563 params.href && (o.node.href = params.href);
1564 params.title && (o.node.title = params.title);
1565 params.target && (o.node.target = params.target);
1566 if (params.path && o.type == "path") {
1569 paper.pathfinder(o, params.path);
1571 if (params.rotation != null) {
1572 o.rotate(params.rotation, true);
1574 if (params.translation) {
1575 var xy = (params.translation + "").split(separator);
1576 o.translate(xy[0], xy[1]);
1579 var xy = (params.scale + "").split(separator);
1580 o.scale(xy[0], xy[1]);
1582 if (o.type == "image" && params.src) {
1583 o.node.src = params.src;
1585 if (o.type == "image" && params.opacity) {
1586 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1587 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1589 params.font && (s.font = params.font);
1590 params["font-family"] && (s.fontFamily = params["font-family"]);
1591 params["font-size"] && (s.fontSize = params["font-size"]);
1592 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1593 params["font-style"] && (s.fontStyle = params["font-style"]);
1594 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"]) {
1595 o = o.shape || o.node;
1596 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || createNode("fill");
1597 if ("fill-opacity" in params || "opacity" in params) {
1598 fill.opacity = ((+params["fill-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1603 if (typeof fill.on == "undefined" || params.fill == "none") {
1606 if (fill.on && params.fill) {
1607 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1609 fill.src = isURL[1];
1612 fill.color = getRGB(params.fill).hex;
1614 fill.type = "solid";
1617 o.appendChild(fill);
1618 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || createNode("stroke");
1619 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"]) {
1622 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1625 if (stroke.on && params.stroke) {
1626 stroke.color = getRGB(params.stroke).hex;
1628 stroke.opacity = ((+params["stroke-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1629 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1630 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1631 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1632 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"]) || 1) * 12 / 16);
1633 if (params["stroke-dasharray"]) {
1637 "-.": "shortdashdot",
1638 "-..": "shortdashdotdot",
1643 "--.": "longdashdot",
1644 "--..": "longdashdotdot"
1646 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1648 o.appendChild(stroke);
1650 if (res.type == "text") {
1651 var span = doc.createElement("span"),
1656 s.display = "inline";
1657 res.attrs.font && (s.font = res.attrs.font);
1658 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1659 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1660 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1661 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1662 res.node.parentNode.appendChild(span);
1663 span.innerText = res.node.string;
1664 res.W = res.attrs.w = span.offsetWidth;
1665 res.H = res.attrs.h = span.offsetHeight;
1666 res.X = res.attrs.x;
1667 res.Y = res.attrs.y + Math.round(res.H / 2);
1668 res.node.parentNode.removeChild(span);
1670 // text-anchor emulation
1671 switch (res.attrs["text-anchor"]) {
1673 res.node.style["v-text-align"] = "left";
1674 res.bbx = Math.round(res.W / 2);
1677 res.node.style["v-text-align"] = "right";
1678 res.bbx = -Math.round(res.W / 2);
1681 res.node.style["v-text-align"] = "center";
1686 var getAngle = function (a, b, c, d) {
1687 var angle = Math.round(Math.atan((parseFloat(c) - parseFloat(a)) / (parseFloat(d) - parseFloat(b))) * 57.29) || 0;
1688 if (!angle && parseFloat(a) < parseFloat(b)) {
1697 var addGrdientFill = function (o, gradient) {
1698 gradient = toGradient(gradient);
1699 o.attrs = o.attrs || {};
1700 var attrs = o.attrs;
1701 o.attrs.gradient = gradient;
1702 o = o.shape || o[0];
1703 var fill = o.getElementsByTagName("fill");
1707 fill = createNode("fill");
1709 if (gradient.dots.length) {
1711 fill.method = "none";
1712 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1713 if (typeof gradient.dots[0].color != "undefined") {
1714 fill.color = getRGB(gradient.dots[0].color).hex;
1716 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1717 fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1720 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1721 if (gradient.dots[i].offset) {
1722 colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1725 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;
1726 if (colors.length) {
1727 fill.colors.value = colors.join(",");
1728 fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1730 fill.colors.value = "0% " + fill.color;
1732 fill.opacity = fillOpacity;
1733 if (typeof gradient.angle != "undefined") {
1734 fill.angle = (-gradient.angle + 270) % 360;
1735 } else if (gradient.vector) {
1736 fill.angle = getAngle.apply(null, gradient.vector);
1738 if ((gradient.type + "").toLowerCase() == "radial") {
1739 fill.focus = "100%";
1740 fill.focusposition = "0.5 0.5";
1744 var Element = function (node, group, vml) {
1764 Element.prototype.rotate = function (deg, cx, cy) {
1766 return this._.rt.deg;
1768 deg = deg.toString().split(separator);
1769 if (deg.length - 1) {
1770 cx = parseFloat(deg[1]);
1771 cy = parseFloat(deg[2]);
1773 deg = parseFloat(deg[0]);
1775 this._.rt.deg = deg;
1777 this._.rt.deg += deg;
1784 this.setBox(null, cx, cy);
1785 this.Group.style.rotation = this._.rt.deg;
1788 Element.prototype.setBox = function (params, cx, cy) {
1789 var gs = this.Group.style,
1790 os = (this.shape && this.shape.style) || this.node.style;
1791 for (var i in params) {
1792 this.attrs[i] = params[i];
1794 cx = cx || this._.rt.cx;
1795 cy = cy || this._.rt.cy;
1796 var attr = this.attrs, x, y, w, h;
1797 switch (this.type) {
1799 x = attr.cx - attr.r;
1800 y = attr.cy - attr.r;
1804 x = attr.cx - attr.rx;
1805 y = attr.cy - attr.ry;
1813 w = attr.width || 0;
1814 h = attr.height || 0;
1817 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("");
1818 x = attr.x - Math.round(this.W / 2);
1819 y = attr.y - this.H / 2;
1824 if (!this.attrs.path) {
1828 h = this.vml.height;
1830 var dim = pathDimensions(this.attrs.path),
1841 h = this.vml.height;
1844 cx = (cx == null) ? x + w / 2 : cx;
1845 cy = (cy == null) ? y + h / 2 : cy;
1846 var left = cx - this.vml.width / 2,
1847 top = cy - this.vml.height / 2;
1848 if (this.type == "path" || this.type == "text") {
1849 gs.left = left + "px";
1850 gs.top = top + "px";
1851 this.X = this.type == "text" ? x : -left;
1852 this.Y = this.type == "text" ? y : -top;
1855 os.left = -left + "px";
1856 os.top = -top + "px";
1858 gs.left = left + "px";
1859 gs.top = top + "px";
1864 gs.width = this.vml.width + "px";
1865 gs.height = this.vml.height + "px";
1866 os.left = x - left + "px";
1867 os.top = y - top + "px";
1868 os.width = w + "px";
1869 os.height = h + "px";
1870 if (this.type == "rect" && this.attrs.r != params.r) {
1871 // We should recplace element with the new one
1872 // this.node.arcsize = (params.r || 0) / (Math.min(w, h));
1876 Element.prototype.hide = function () {
1877 this.Group.style.display = "none";
1880 Element.prototype.show = function () {
1881 this.Group.style.display = "block";
1884 Element.prototype.getBBox = function () {
1885 if (this.type == "path") {
1886 return pathDimensions(this.attr("path"));
1889 x: this.X + (this.bbx || 0),
1895 Element.prototype.remove = function () {
1896 this[0].parentNode.removeChild(this[0]);
1897 this.Group.parentNode.removeChild(this.Group);
1898 this.shape && this.shape.parentNode.removeChild(this.shape);
1900 Element.prototype.attr = function () {
1901 if (arguments.length == 1 && typeof arguments[0] == "string") {
1902 if (arguments[0] == "translation") {
1903 return this.translate();
1905 return this.attrs[arguments[0]];
1907 if (this.attrs && arguments.length == 1 && arguments[0] instanceof win.Array) {
1909 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1910 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1915 if (arguments.length == 2) {
1917 params[arguments[0]] = arguments[1];
1919 if (arguments.length == 1 && typeof arguments[0] == "object") {
1920 params = arguments[0];
1923 if (params.gradient) {
1924 addGrdientFill(this, params.gradient);
1926 if (params.text && this.type == "text") {
1927 this.node.string = params.text;
1930 this.node.id = params.id;
1932 setFillAndStroke(this, params);
1933 this.setBox(params);
1937 Element.prototype.toFront = function () {
1938 this.Group.parentNode.appendChild(this.Group);
1941 Element.prototype.toBack = function () {
1942 if (this.Group.parentNode.firstChild != this.Group) {
1943 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1947 Element.prototype.insertAfter = function (element) {
1948 if (element.Group.nextSibling) {
1949 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1951 element.Group.parentNode.appendChild(this.Group);
1955 Element.prototype.insertBefore = function (element) {
1956 element.Group.parentNode.insertBefore(this.Group, element.Group);
1959 var theCircle = function (vml, x, y, r) {
1960 var g = createNode("group");
1961 var o = createNode("oval");
1963 vml.canvas.appendChild(g);
1964 var res = new Element(o, g, vml);
1965 res.type = "circle";
1966 setFillAndStroke(res, {stroke: "#000", fill: "none"});
1970 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1973 var theRect = function (vml, x, y, w, h, r) {
1974 var g = createNode("group");
1975 var o = createNode("roundrect");
1976 o.arcsize = (+r || 0) / (Math.min(w, h));
1978 vml.canvas.appendChild(g);
1979 var res = new Element(o, g, vml);
1981 setFillAndStroke(res, {stroke: "#000"});
1987 res.setBox({x: x, y: y, width: w, height: h});
1990 var theEllipse = function (vml, x, y, rx, ry) {
1991 var g = createNode("group");
1992 var o = createNode("oval");
1994 vml.canvas.appendChild(g);
1995 var res = new Element(o, g, vml);
1996 res.type = "ellipse";
1997 setFillAndStroke(res, {stroke: "#000"});
2002 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2005 var theImage = function (vml, src, x, y, w, h) {
2006 var g = createNode("group");
2007 var o = createNode("image");
2010 vml.canvas.appendChild(g);
2011 var res = new Element(o, g, vml);
2017 res.setBox({x: x, y: y, width: w, height: h});
2020 var theText = function (vml, x, y, text) {
2021 var g = createNode("group"), gs = g.style;
2022 var el = createNode("shape"), ol = el.style;
2023 var path = createNode("path"), ps = path.style;
2024 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
2025 path.textpathok = true;
2026 ol.width = vml.width;
2027 ol.height = vml.height;
2028 gs.position = "absolute";
2031 gs.width = vml.width;
2032 gs.height = vml.height;
2033 var o = createNode("textpath");
2036 o.coordsize = vml.coordsize;
2037 o.coordorigin = vml.coordorigin;
2039 el.appendChild(path);
2041 vml.canvas.appendChild(g);
2042 var res = new Element(o, g, vml);
2044 res.textpath = path;
2050 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2054 var setSize = function (width, height) {
2055 this.width = width || this.width;
2056 this.height = height || this.height;
2057 this.canvas.style.width = this.width + "px";
2058 this.canvas.style.height = this.height + "px";
2059 this.canvas.parentNode.style.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2060 this.canvas.coordsize = this.width + " " + this.height;
2063 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2065 if (!doc.namespaces.rvml) {
2066 doc.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
2068 var createNode = function (tagName) {
2069 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2072 var createNode = function (tagName) {
2073 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2076 var create = function () {
2077 var con = getContainer.apply(null, arguments);
2078 var container = con.container,
2082 height = con.height;
2084 throw new Error("VML container not found.");
2086 var c = doc.createElement("div"),
2087 d = doc.createElement("div"),
2088 r = paper.canvas = createNode("group"),
2089 cs = c.style, rs = r.style;
2090 paper.width = width;
2091 paper.height = height;
2092 width = width || "320px";
2093 height = height || "200px";
2094 cs.clip = "rect(0 " + width + "px " + height + "px 0)";
2097 cs.position = "absolute";
2098 rs.position = "absolute";
2099 d.style.position = "relative";
2102 r.coordsize = (/%$/.test(width) ? width : parseFloat(width)) + " " + (/%$/.test(height) ? height : parseFloat(height));
2103 r.coordorigin = "0 0";
2105 var b = createNode("rect"), bs = b.style;
2106 bs.left = bs.top = 0;
2107 bs.width = rs.width;
2108 bs.height = rs.height;
2109 b.filled = b.stroked = "f";
2114 if (container == 1) {
2115 doc.body.appendChild(d);
2116 cs.position = "absolute";
2128 cs.width = container.style.width = width;
2129 cs.height = container.style.height = height;
2130 if (container.firstChild) {
2131 container.insertBefore(d, container.firstChild);
2133 container.appendChild(d);
2136 for (var prop in paper) {
2137 container[prop] = paper[prop];
2139 plugins(container, container, R.fn);
2140 container.clear = function () {
2142 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2143 if (r.childNodes[i] != b) {
2144 todel.push(r.childNodes[i]);
2147 for (i = 0, ii = todel.length; i < ii; i++) {
2148 r.removeChild(todel[i]);
2153 paper.remove = function () {
2154 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2156 paper.safari = function () {};
2162 var addEvent = (function () {
2163 if (doc.addEventListener) {
2164 return function (obj, type, fn, element) {
2165 var f = function (e) {
2166 return fn.call(element, e);
2168 obj.addEventListener(type, f, false);
2169 return function () {
2170 obj.removeEventListener(type, f, false);
2174 } else if (doc.attachEvent) {
2175 return function (obj, type, fn, element) {
2176 var f = function (e) {
2177 return fn.call(element, e || win.event);
2179 obj.attachEvent("on" + type, f);
2180 var detacher = function () {
2181 obj.detachEvent("on" + type, f);
2184 if (type == "mouseover") {
2185 obj.attachEvent("onmouseenter", f);
2186 return function () {
2187 obj.detachEvent("onmouseenter", f);
2190 } else if (type == "mouseout") {
2191 obj.attachEvent("onmouseleave", f);
2192 return function () {
2193 obj.detachEvent("onmouseleave", f);
2201 for (var i = events.length; i--;) {
2202 (function (eventName) {
2203 Element.prototype[eventName] = function (fn) {
2204 if (typeof fn == "function") {
2205 this.events = this.events || {};
2206 this.events[eventName] = this.events[eventName] || {};
2207 this.events[eventName][fn] = this.events[eventName][fn] || [];
2208 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2212 Element.prototype["un" + eventName] = function (fn) {
2214 this.events[eventName] &&
2215 this.events[eventName][fn] &&
2216 this.events[eventName][fn].length &&
2217 this.events[eventName][fn].shift()() &&
2218 !this.events[eventName][fn].length &&
2219 delete this.events[eventName][fn];
2224 paper.circle = function (x, y, r) {
2225 return theCircle(this, x, y, r);
2227 paper.rect = function (x, y, w, h, r) {
2228 return theRect(this, x, y, w, h, r);
2230 paper.ellipse = function (x, y, rx, ry) {
2231 return theEllipse(this, x, y, rx, ry);
2233 paper.path = function (params, pathString) {
2234 return thePath(params, pathString, this);
2236 paper.image = function (src, x, y, w, h) {
2237 return theImage(this, src, x, y, w, h);
2239 paper.text = function (x, y, text) {
2240 return theText(this, x, y, text);
2242 paper.group = function () {
2245 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2246 color = color || "#000";
2247 var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
2249 columnWidth = w / wv;
2250 for (var i = 1; i < hv; i++) {
2251 path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
2253 for (var i = 1; i < wv; i++) {
2254 path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
2256 return this.path({stroke: color, "stroke-width": 1}, path.join(","));
2258 paper.pathfinder = function (p, path) {
2260 M: function (x, y) {
2263 C: function (x1, y1, x2, y2, x3, y3) {
2264 this.curveTo(x1, y1, x2, y2, x3, y3);
2266 Q: function (x1, y1, x2, y2) {
2267 this.qcurveTo(x1, y1, x2, y2);
2269 T: function (x, y) {
2270 this.qcurveTo(x, y);
2272 S: function (x1, y1, x2, y2) {
2273 p.curveTo(x1, y1, x2, y2);
2275 L: function (x, y) {
2279 this.lineTo(x, this.last.y);
2282 this.lineTo(this.last.x, y);
2284 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2285 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2292 path = pathToAbsolute(path);
2293 for (var i = 0, ii = path.length; i < ii; i++) {
2294 var b = path[i].shift();
2295 commands[b].apply(p, path[i]);
2298 paper.set = function (itemsArray) {
2299 return new Set(itemsArray);
2301 paper.setSize = setSize;
2302 Element.prototype.stop = function () {
2303 clearTimeout(this.animation_in_progress);
2305 Element.prototype.scale = function (x, y, cx, cy) {
2306 if (x == null && y == null) {
2307 return {x: this._.sx, y: this._.sy};
2311 var dx, dy, dcx, dcy;
2313 var bb = this.getBBox(),
2314 rcx = bb.x + bb.width / 2,
2315 rcy = bb.y + bb.height / 2;
2318 var dirx = Math.round(x / Math.abs(x)),
2319 diry = Math.round(y / Math.abs(y)),
2320 s = this.node.style,
2321 ncx = cx + (rcx - cx) * x * dirx / this._.sx,
2322 ncy = cy + (rcy - cy) * y * diry / this._.sy;
2323 dx = this.attr("x");
2324 dy = this.attr("y");
2325 dcx = this.attr("cx");
2326 dcy = this.attr("cy");
2327 if (dirx != 1 || diry != 1) {
2328 if (this.transformations) {
2329 this.transformations[2] = "scale(" + [dirx, diry] + ")";
2330 this.node.setAttribute("transform", this.transformations.join(" "));
2331 dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
2332 dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
2333 dcx = this.attr("cx") * dirx;
2334 dcy = this.attr("cy") * diry;
2336 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2337 ", M12=0, M21=0, M22=" + diry +
2338 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2339 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2342 if (this.transformations) {
2343 this.transformations[2] = "";
2344 this.node.setAttribute("transform", this.transformations.join(" "));
2346 this.node.filterMatrix = "";
2347 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2350 switch (this.type) {
2353 var neww = this.attrs.width * x * dirx / this._.sx,
2354 newh = this.attrs.height * y * diry / this._.sy,
2355 newx = ncx - neww / 2,
2356 newy = ncy - newh / 2;
2367 rx: this.attrs.rx * x * dirx / this._.sx,
2368 ry: this.attrs.ry * y * diry / this._.sy,
2369 r: this.attrs.r * x * dirx / this._.sx,
2375 var path = pathToRelative(R.parsePathString(this.attr("path"))),
2377 for (var i = 0, ii = path.length; i < ii; i++) {
2378 if (path[i][0].toUpperCase() == "M" && skip) {
2383 if (this.svg && path[i][0].toUpperCase() == "A") {
2384 path[i][path[i].length - 2] *= x * dirx;
2385 path[i][path[i].length - 1] *= y * diry;
2389 for (var j = 1, jj = path[i].length; j < jj; j++) {
2390 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2394 var dim2 = pathDimensions(path),
2395 dx = ncx - dim2.x - dim2.width / 2,
2396 dy = ncy - dim2.y - dim2.height / 2;
2397 path = pathToRelative(path);
2401 this.attr({path: path.join(" ")});
2408 Element.prototype.animate = function (params, ms, callback) {
2409 clearTimeout(this.animation_in_progress);
2414 for (var attr in params) {
2415 if (attr in availableAnimAttrs) {
2416 from[attr] = this.attr(attr);
2417 if (typeof from[attr] == "undefined") {
2418 from[attr] = availableAttrs[attr];
2420 to[attr] = params[attr];
2421 switch (availableAnimAttrs[attr]) {
2423 diff[attr] = (to[attr] - from[attr]) / ms;
2426 from[attr] = getRGB(from[attr]);
2427 var toColour = getRGB(to[attr]);
2429 r: (toColour.r - from[attr].r) / ms,
2430 g: (toColour.g - from[attr].g) / ms,
2431 b: (toColour.b - from[attr].b) / ms
2435 var pathes = pathEqualiser(from[attr], to[attr]);
2436 from[attr] = pathes[0];
2437 to[attr] = pathes[1];
2439 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2440 diff[attr][i] = [0];
2441 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2442 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2447 var values = params[attr].toString().split(separator),
2448 from2 = from[attr].toString().split(separator);
2449 if (attr == "translation") {
2450 from[attr] = [0, 0];
2451 diff[attr] = [values[0] / ms, values[1] / ms];
2452 } else if (attr == "rotation") {
2453 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2454 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2456 from[attr] = (from[attr] + "").split(separator);
2457 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2463 var start = +new Date,
2467 var time = +new Date - start,
2471 for (var attr in from) {
2472 switch (availableAnimAttrs[attr]) {
2474 now = +from[attr] + time * diff[attr];
2478 Math.round(from[attr].r + time * diff[attr].r),
2479 Math.round(from[attr].g + time * diff[attr].g),
2480 Math.round(from[attr].b + time * diff[attr].b)
2485 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2486 now[i] = [from[attr][i][0]];
2487 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2488 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2490 now[i] = now[i].join(" ");
2492 now = now.join(" ");
2495 if (attr == "translation") {
2496 var x = diff[attr][0] * (time - prev),
2497 y = diff[attr][1] * (time - prev);
2500 now = [x, y].join(" ");
2501 } else if (attr == "rotation") {
2502 now = +from[attr][0] + time * diff[attr][0];
2503 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2505 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2509 if (attr == "font-size") {
2510 set[attr] = now + "px";
2516 that.animation_in_progress = setTimeout(arguments.callee, 0);
2519 (t.x || t.y) && that.translate(-t.x, -t.y);
2521 clearTimeout(that.animation_in_progress);
2523 (typeof callback == "function") && callback.call(that);
2529 Element.prototype.translate = function (x, y) {
2531 return {x: this._.tx, y: this._.ty};
2535 switch (this.type) {
2538 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
2543 this.attr({x: this.attrs.x + (+x), y: this.attrs.y + (+y)});
2546 var path = pathToRelative(this.attrs.path);
2549 this.attr({path: path.join(" ")});
2556 var Set = function (itemsArray) {
2558 this.length = (itemsArray && itemsArray.length) || 0;
2559 if (itemsArray && itemsArray.constructor == Array) {
2560 for (var i = itemsArray.length; i--;) {
2561 if (itemsArray[i].constructor == Element) {
2562 this.items[this.items.length] = itemsArray[i];
2567 Set.prototype.push = function (item) {
2568 if (item && item.constructor == Element) {
2569 var len = this.items.length;
2570 this.items[len] = item;
2576 Set.prototype.pull = function (id) {
2577 var res = this.items.splice(id, 1)[0];
2578 for (var j = id, jj = this.items.length; j < jj; j++) {
2579 this[j] = this[j + 1];
2581 delete this[jj + 1];
2585 for (var method in Element.prototype) {
2586 Set.prototype[method] = (function (methodname) {
2587 return function () {
2588 for (var i = this.items.length; i--;) {
2589 this.items[i][methodname].apply(this.items[i], arguments);
2595 Set.prototype.getBBox = function () {
2596 var x = [], y = [], w = [], h = [];
2597 for (var i = this.items.length; i--;) {
2598 var box = this.items[i].getBBox();
2601 w.push(box.x + box.width);
2602 h.push(box.y + box.height);
2604 x = Math.min.apply(Math, x);
2605 y = Math.min.apply(Math, y);
2609 width: Math.max.apply(Math, w) - x,
2610 height: Math.max.apply(Math, h) - y
2614 R.noConflict = function () {
2615 var r = window.Raphael;
2616 delete window.Raphael;
2617 if (oldRaphael.was) {
2618 window.Raphael = oldRaphael.is;