2 * Raphael 0.7.1 - 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 var Raphael = (function () {
10 var separator = /[, ]+/,
15 return create.apply(R, arguments);
18 R.type = (win.SVGAngle ? "SVG" : "VML");
19 R.svg = !(R.vml = R.type == "VML");
23 var availableAttrs = {cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", gradient: 0, height: 0, 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, translation: "0 0", width: 0, x: 0, y: 0},
24 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"},
25 events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
26 R.toString = function () {
27 return "Your browser " + (this.vml ? "doesn't ": "") + "support" + (this.svg ? "s": "") +
28 " SVG.\nYou are running " + unescape("Rapha%EBl%20") + this.version;
30 R.setWindow = function (newwin) {
35 R.hsb2rgb = function (hue, saturation, brightness) {
36 if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
44 if (brightness == 0) {
45 return {r: 0, g: 0, b: 0, hex: "#000"};
47 if (hue > 1 || saturation > 1 || brightness > 1) {
52 var i = Math.floor(hue * 6),
54 p = brightness * (1 - saturation),
55 q = brightness * (1 - (saturation * f)),
56 t = brightness * (1 - (saturation * (1 - f)));
57 red = [brightness, q, p, p, t, brightness, brightness][i];
58 green = [t, brightness, brightness, q, p, p, t][i];
59 blue = [p, p, t, brightness, brightness, q, p][i];
63 var rgb = {r: red, g: green, b: blue};
64 var r = Math.round(red).toString(16);
68 var g = Math.round(green).toString(16);
72 var b = Math.round(blue).toString(16);
76 rgb.hex = "#" + r + g + b;
79 R.rgb2hsb = function (red, green, blue) {
80 if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
85 if (typeof red == "string" && red.charAt(0) == "#") {
86 if (red.length == 4) {
87 blue = parseInt(red.substring(3), 16);
88 green = parseInt(red.substring(2, 3), 16);
89 red = parseInt(red.substring(1, 2), 16);
91 blue = parseInt(red.substring(5), 16);
92 green = parseInt(red.substring(3, 5), 16);
93 red = parseInt(red.substring(1, 3), 16);
96 if (red > 1 || green > 1 || blue > 1) {
101 var max = Math.max(red, green, blue),
102 min = Math.min(red, green, blue),
107 return {h: 0, s: 0, b: max};
109 var delta = (max - min);
110 saturation = delta / max;
112 hue = (green - blue) / delta;
113 } else if (green == max) {
114 hue = 2 + ((blue - red) / delta);
116 hue = 4 + ((red - green) / delta);
126 return {h: hue, s: saturation, b: brightness};
128 var getRGB = function (colour) {
129 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"};
130 if (colour.toString().toLowerCase() in htmlcolors) {
131 colour = htmlcolors[colour.toString().toLowerCase()];
134 return {r: 0, g: 0, b: 0, hex: "#000"};
136 if (colour == "none") {
137 return {r: -1, g: -1, b: -1, hex: "none"};
139 var red, green, blue,
140 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);
143 blue = parseInt(rgb[2].substring(5), 16);
144 green = parseInt(rgb[2].substring(3, 5), 16);
145 red = parseInt(rgb[2].substring(1, 3), 16);
148 blue = parseInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
149 green = parseInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
150 red = parseInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
153 rgb = rgb[4].split(/\s*,\s*/);
154 red = parseFloat(rgb[0], 10);
155 green = parseFloat(rgb[1], 10);
156 blue = parseFloat(rgb[2], 10);
159 rgb = rgb[5].split(/\s*,\s*/);
160 red = parseFloat(rgb[0], 10) * 2.55;
161 green = parseFloat(rgb[1], 10) * 2.55;
162 blue = parseFloat(rgb[2], 10) * 2.55;
165 rgb = rgb[6].split(/\s*,\s*/);
166 red = parseFloat(rgb[0], 10);
167 green = parseFloat(rgb[1], 10);
168 blue = parseFloat(rgb[2], 10);
169 return Raphael.hsb2rgb(red, green, blue);
172 rgb = rgb[7].split(/\s*,\s*/);
173 red = parseFloat(rgb[0], 10) * 2.55;
174 green = parseFloat(rgb[1], 10) * 2.55;
175 blue = parseFloat(rgb[2], 10) * 2.55;
176 return Raphael.hsb2rgb(red, green, blue);
178 var rgb = {r: red, g: green, b: blue};
179 var r = Math.round(red).toString(16);
180 (r.length == 1) && (r = "0" + r);
181 var g = Math.round(green).toString(16);
182 (g.length == 1) && (g = "0" + g);
183 var b = Math.round(blue).toString(16);
184 (b.length == 1) && (b = "0" + b);
185 rgb.hex = "#" + r + g + b;
188 return {r: -1, g: -1, b: -1, hex: "none"};
191 R.getColor = function (value) {
192 var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
193 var rgb = Raphael.hsb2rgb(start.h, start.s, start.b);
199 arguments.callee.start = {h: 0, s: 1, b: start.b};
204 R.getColor.reset = function () {
205 this.start = undefined;
208 R.parsePathString = function (pathString) {
209 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
211 toString = function () {
213 for (var i = 0, ii = this.length; i < ii; i++) {
214 res += this[i][0] + this[i].join(",").substring(2);
218 if (pathString.toString.toString() == toString.toString()) {
221 pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*\s*,?\s*)+)/ig, function (a, b, c) {
222 var params = [], name = b.toLowerCase();
223 c.replace(/(-?\d*\.?\d*)\s*,?\s*/ig, function (a, b) {
224 b && params.push(+b);
226 while (params.length >= paramCounts[name]) {
227 data.push([b].concat(params.splice(0, paramCounts[name])));
228 if (!paramCounts[name]) {
233 data.toString = toString;
236 var pathDimensions = function (path) {
237 var pathArray = path;
238 if (typeof path == "string") {
239 pathArray = Raphael.parsePathString(path);
241 pathArray = pathToAbsolute(pathArray);
242 var x = [], y = [], length = 0;
243 for (var i = 0, ii = pathArray.length; i < ii; i++) {
244 switch (pathArray[i][0]) {
248 x.push(pathArray[i][pathArray[i].length - 2]);
249 y.push(pathArray[i][pathArray[i].length - 1]);
252 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
254 x.push(pathArray[i][j]);
256 y.push(pathArray[i][j]);
261 var minx = Math.min.apply(Math, x),
262 miny = Math.min.apply(Math, y);
266 width: Math.max.apply(Math, x) - minx,
267 height: Math.max.apply(Math, y) - miny,
272 var pathToRelative = function (pathArray) {
274 if (typeof pathArray == "string") {
275 pathArray = R.parsePathString(pathArray);
277 var x = 0, y = 0, start = 0;
278 if (pathArray[0][0] == "M") {
282 res.push(pathArray[0]);
284 for (var i = start, ii = pathArray.length; i < ii; i++) {
286 if (pathArray[i][0] != pathArray[i][0].toLowerCase()) {
287 res[i][0] = pathArray[i][0].toLowerCase();
290 res[i][1] = pathArray[i][1];
291 res[i][2] = pathArray[i][2];
293 res[i][4] = pathArray[i][4];
294 res[i][5] = pathArray[i][5];
295 res[i][6] = +(pathArray[i][6] - x).toFixed(3);
296 res[i][7] = +(pathArray[i][7] - y).toFixed(3);
299 res[i][1] = +(pathArray[i][1] - y).toFixed(3);
302 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
303 res[i][j] = +(pathArray[i][j] - ((j % 2) ? x : y)).toFixed(3);
307 res[i] = pathArray[i];
313 x += res[i][res[i].length - 1];
316 y += res[i][res[i].length - 1];
319 x += res[i][res[i].length - 2];
320 y += res[i][res[i].length - 1];
323 res.toString = pathArray.toString;
326 var pathToAbsolute = function (pathArray) {
328 if (typeof pathArray == "string") {
329 pathArray = R.parsePathString(pathArray);
334 if (pathArray[0][0] == "M") {
335 x = +pathArray[0][1];
336 y = +pathArray[0][2];
338 res[0] = pathArray[0];
340 for (var i = start, ii = pathArray.length; i < ii; i++) {
342 if (pathArray[i][0] != (pathArray[i][0] + "").toUpperCase()) {
343 res[i][0] = (pathArray[i][0] + "").toUpperCase();
346 res[i][1] = pathArray[i][1];
347 res[i][2] = pathArray[i][2];
349 res[i][4] = pathArray[i][4];
350 res[i][5] = pathArray[i][5];
351 res[i][6] = +(pathArray[i][6] + x).toFixed(3);
352 res[i][7] = +(pathArray[i][7] + y).toFixed(3);
355 res[i][1] = +pathArray[i][1] + y;
358 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
359 res[i][j] = +pathArray[i][j] + ((j % 2) ? x : y);
363 res[i] = pathArray[i];
375 x = res[i][res[i].length - 2];
376 y = res[i][res[i].length - 1];
379 res.toString = pathArray.toString;
382 var pathEqualiser = function (path1, path2) {
383 var data = [pathToAbsolute(Raphael.parsePathString(path1)), pathToAbsolute(Raphael.parsePathString(path2))],
384 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}],
385 processPath = function (path, d) {
395 var nx = d.x + (d.x - (d.bx || d.x));
396 var ny = d.y + (d.y - (d.by || d.y));
397 path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
400 var nx = d.x + (d.x - (d.bx || d.x));
401 var ny = d.y + (d.y - (d.by || d.y));
402 path = ["Q", nx, ny, path[1], path[2]];
405 path = ["L", path[1], d.y];
408 path = ["L", d.x, path[1]];
411 path = ["L", d.X, d.Y];
416 edgeCases = function (a, b, i) {
417 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
418 data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
419 attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
420 attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
421 attrs[a].x = data[a][i][data[a][i].length - 2];
422 attrs[a].y = data[a][i][data[a][i].length - 1];
424 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
425 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]];
426 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
427 data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
428 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
429 var x = data[b][i][data[b][i].length - 2];
430 var y = data[b][i][data[b][i].length - 1];
431 data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
432 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
434 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
435 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
436 attrs[b].x = data[b][i][data[b][i].length - 2];
437 attrs[b].y = data[b][i][data[b][i].length - 1];
439 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
440 var x = data[b][i][data[b][i].length - 2];
441 var y = data[b][i][data[b][i].length - 1];
442 data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
443 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
445 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
446 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
447 attrs[b].x = data[b][i][data[b][i].length - 2];
448 attrs[b].y = data[b][i][data[b][i].length - 1];
450 } else if (data[a][i][0] == "U") {
451 data[a][i][0] = data[b][i][0];
452 for (var j = 1, jj = data[b][i].length; j < jj; j++) {
453 data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
458 for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
459 data[0][i] = processPath(data[0][i], attrs[0]);
460 data[1][i] = processPath(data[1][i], attrs[1]);
461 if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
464 attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
465 attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
466 attrs[0].x = data[0][i][data[0][i].length - 2];
467 attrs[0].y = data[0][i][data[0][i].length - 1];
468 attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
469 attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
470 attrs[1].x = data[1][i][data[1][i].length - 2];
471 attrs[1].y = data[1][i][data[1][i].length - 1];
475 var toGradient = function (gradient) {
476 if (typeof gradient == "string") {
477 gradient = gradient.split(/\s*\-\s*/);
478 var angle = gradient.shift();
479 if (angle.toLowerCase() == "v") {
481 } else if (angle.toLowerCase() == "h") {
484 angle = parseFloat(angle, 10);
487 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)]};
488 var max = 1 / (Math.max(Math.abs(grobj.vector[2]), Math.abs(grobj.vector[3])) || 1);
489 grobj.vector[2] *= max;
490 grobj.vector[3] *= max;
491 if (grobj.vector[2] < 0) {
492 grobj.vector[0] = -grobj.vector[2];
495 if (grobj.vector[3] < 0) {
496 grobj.vector[1] = -grobj.vector[3];
499 grobj.vector[0] = grobj.vector[0].toFixed(3);
500 grobj.vector[1] = grobj.vector[1].toFixed(3);
501 grobj.vector[2] = grobj.vector[2].toFixed(3);
502 grobj.vector[3] = grobj.vector[3].toFixed(3);
503 for (var i = 0, ii = gradient.length; i < ii; i++) {
505 var par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
506 dot.color = getRGB(par[1]).hex;
507 par[2] && (dot.offset = par[2] + "%");
508 grobj.dots.push(dot);
510 for (var i = 1, ii = grobj.dots.length - 1; i < ii; i++) {
511 if (!grobj.dots[i].offset) {
512 var start = parseFloat(grobj.dots[i - 1].offset || 0, 10),
514 for (var j = i + 1; j < ii; j++) {
515 if (grobj.dots[j].offset) {
516 end = grobj.dots[j].offset;
524 end = parseFloat(end, 10);
525 var d = (end - start) / (j - i + 1);
528 grobj.dots[i].offset = start + "%";
540 var thePath = function (params, pathString, SVG) {
541 var el = doc.createElementNS(SVG.svgns, "path");
542 el.setAttribute("fill", "none");
544 SVG.canvas.appendChild(el);
546 var p = new Element(el, SVG);
549 p.last = {x: 0, y: 0, bx: 0, by: 0};
550 p.absolutely = function () {
551 this.isAbsolute = true;
554 p.relatively = function () {
555 this.isAbsolute = false;
558 p.moveTo = function (x, y) {
559 var d = this.isAbsolute?"M":"m";
560 d += parseFloat(x, 10).toFixed(3) + " " + parseFloat(y, 10).toFixed(3) + " ";
561 var oldD = this[0].getAttribute("d") || "";
562 (oldD == "M0,0") && (oldD = "");
563 this[0].setAttribute("d", oldD + d);
564 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
565 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
566 this.attrs.path = oldD + d;
569 p.lineTo = function (x, y) {
570 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
571 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
572 var d = this.isAbsolute?"L":"l";
573 d += parseFloat(x, 10).toFixed(3) + " " + parseFloat(y, 10).toFixed(3) + " ";
574 var oldD = this[0].getAttribute("d") || "";
575 this[0].setAttribute("d", oldD + d);
576 this.attrs.path = oldD + d;
579 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
580 var d = this.isAbsolute ? "A" : "a";
581 d += [parseFloat(rx, 10).toFixed(3), parseFloat(ry, 10).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x, 10).toFixed(3), parseFloat(y, 10).toFixed(3)].join(" ");
582 var oldD = this[0].getAttribute("d") || "";
583 this[0].setAttribute("d", oldD + d);
584 this.last.x = parseFloat(x, 10);
585 this.last.y = parseFloat(y, 10);
586 this.attrs.path = oldD + d;
589 p.cplineTo = function (x1, y1, w1) {
591 return this.lineTo(x1, y1);
594 var x = parseFloat(x1, 10);
595 var y = parseFloat(y1, 10);
596 var w = parseFloat(w1, 10);
597 var d = this.isAbsolute?"C":"c";
598 var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
599 for (var i = 0, ii = attr.length; i < ii; i++) {
600 d += attr[i].toFixed(3) + " ";
602 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
603 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
604 this.last.bx = attr[2];
605 this.last.by = attr[3];
606 var oldD = this[0].getAttribute("d") || "";
607 this[0].setAttribute("d", oldD + d);
608 this.attrs.path = oldD + d;
612 p.curveTo = function () {
614 command = [0, 1, 2, 3, "s", 5, "c"];
616 var d = command[arguments.length];
617 if (this.isAbsolute) {
620 for (var i = 0, ii = arguments.length; i < ii; i++) {
621 d += parseFloat(arguments[i], 10).toFixed(3) + " ";
623 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2], 10);
624 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1], 10);
625 this.last.bx = parseFloat(arguments[arguments.length - 4], 10);
626 this.last.by = parseFloat(arguments[arguments.length - 3], 10);
627 var oldD = this.node.getAttribute("d") || "";
628 this.node.setAttribute("d", oldD + d);
629 this.attrs.path = oldD + d;
632 p.qcurveTo = function () {
634 command = [0, 1, "t", 3, "q"];
636 var d = command[arguments.length];
637 if (this.isAbsolute) {
640 for (var i = 0, ii = arguments.length; i < ii; i++) {
641 d += parseFloat(arguments[i], 10).toFixed(3) + " ";
643 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2], 10);
644 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1], 10);
645 if (arguments.length != 2) {
646 this.last.qx = parseFloat(arguments[arguments.length - 4], 10);
647 this.last.qy = parseFloat(arguments[arguments.length - 3], 10);
649 var oldD = this.node.getAttribute("d") || "";
650 this.node.setAttribute("d", oldD + d);
651 this.attrs.path = oldD + d;
654 p.addRoundedCorner = function (r, dir) {
655 var R = .5522 * r, rollback = this.isAbsolute, o = this;
658 rollback = function () {
662 rollback = function () {};
668 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
671 o.curveTo(-R, 0, -r, r - R, -r, r);
678 o.curveTo(R, 0, r, -(r - R), r, -r);
681 o.curveTo(R, 0, r, r - R, r, r);
688 o.curveTo(0, -R, -(R - r), -r, r, -r);
691 o.curveTo(0, -R, R - r, -r, -r, -r);
698 o.curveTo(0, R, -(R - r), r, r, r);
701 o.curveTo(0, R, R - r, r, -r, r);
706 actions[dir[0]]()[dir[1]]();
710 p.andClose = function () {
711 var oldD = this[0].getAttribute("d") || "";
712 this[0].setAttribute("d", oldD + "Z ");
713 this.attrs.path = oldD + "Z ";
717 p.attrs.path = "" + pathString;
719 paper.pathfinder(p, p.attrs.path);
722 setFillAndStroke(p, params);
726 var addGrdientFill = function (o, gradient, SVG) {
727 gradient = toGradient(gradient);
728 var el = doc.createElementNS(SVG.svgns, (gradient.type || "linear") + "Gradient");
729 el.id = "raphael-gradient-" + Raphael.idGenerator++;
730 if (gradient.vector && gradient.vector.length) {
731 el.setAttribute("x1", gradient.vector[0]);
732 el.setAttribute("y1", gradient.vector[1]);
733 el.setAttribute("x2", gradient.vector[2]);
734 el.setAttribute("y2", gradient.vector[3]);
736 SVG.defs.appendChild(el);
737 var isopacity = true;
738 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
739 var stop = doc.createElementNS(SVG.svgns, "stop");
740 if (gradient.dots[i].offset) {
743 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
744 stop.setAttribute("stop-color", getRGB(gradient.dots[i].color).hex || "#fff");
745 // ignoring opacity for internal points, because VML doesn't support it
746 el.appendChild(stop);
748 if (isopacity && typeof gradient.dots[ii - 1].opacity != "undefined") {
749 stop.setAttribute("stop-opacity", gradient.dots[ii - 1].opacity);
751 o.setAttribute("fill", "url(#" + el.id + ")");
753 o.style.fillOpacity = 1;
754 o.setAttribute("opacity", 1);
755 o.setAttribute("fill-opacity", 1);
757 var updatePosition = function (o) {
759 var bbox = o.node.getBBox();
760 o.pattern.setAttribute("patternTransform", "translate(" + [bbox.x, bbox.y].join(",") + ")");
763 var setFillAndStroke = function (o, params) {
768 "-..": [3, 1, 1, 1, 1, 1],
774 "--..": [8, 3, 1, 3, 1, 3]
776 addDashes = function (o, value) {
777 value = dasharray[value.toString().toLowerCase()];
779 var width = o.attrs["stroke-width"] || "1",
780 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
782 for (var i = 0, ii = value.length; i < ii; i++) {
783 dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
785 value = dashes.join(",");
786 o.node.setAttribute("stroke-dasharray", value);
789 for (var att in params) {
790 var value = params[att];
791 o.attrs[att] = value;
794 if (o.type == "path") {
795 o.node.setAttribute("d", "M0,0");
796 paper.pathfinder(o, value);
801 o.node.setAttribute(att, value);
807 o.node.setAttribute(att, value);
811 o.node.setAttribute(att, value);
814 o.node.setAttribute(att, value);
817 if (o.type == "image") {
818 o.node.setAttributeNS(svg.xlink, "href", value);
822 o.node.style.strokeWidth = value;
823 // Need following line for Firefox
824 o.node.setAttribute(att, value);
825 if (o.attrs["stroke-dasharray"]) {
826 addDashes(o, o.attrs["stroke-dasharray"]);
829 case "stroke-dasharray":
833 o.rotate(value, true);
836 var xy = (value + "").split(separator);
837 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
840 var xy = (value + "").split(separator);
841 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1);
844 var isURL = value.match(/^url\(([^\)]+)\)$/i);
846 var el = doc.createElementNS(o.svg.svgns, "pattern");
847 var ig = doc.createElementNS(o.svg.svgns, "image");
848 el.id = "raphael-pattern-" + Raphael.idGenerator++;
849 el.setAttribute("x", 0);
850 el.setAttribute("y", 0);
851 el.setAttribute("patternUnits", "userSpaceOnUse");
852 ig.setAttribute("x", 0);
853 ig.setAttribute("y", 0);
854 ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
857 var img = doc.createElement("img");
858 img.style.position = "absolute";
859 img.style.top = "-9999em";
860 img.style.left = "-9999em";
861 img.onload = function () {
862 el.setAttribute("width", this.offsetWidth);
863 el.setAttribute("height", this.offsetHeight);
864 ig.setAttribute("width", this.offsetWidth);
865 ig.setAttribute("height", this.offsetHeight);
866 doc.body.removeChild(this);
869 doc.body.appendChild(img);
871 o.svg.defs.appendChild(el);
872 o.node.style.fill = "url(#" + el.id + ")";
873 o.node.setAttribute("fill", "url(#" + el.id + ")");
878 delete params.gradient;
879 delete o.attrs.gradient;
880 if (typeof o.attrs.opacity != "undefined" && typeof params.opacity == "undefined" ) {
881 o.node.style.opacity = o.attrs.opacity;
882 // Need following line for Firefox
883 o.node.setAttribute("opacity", o.attrs.opacity);
885 if (typeof o.attrs["fill-opacity"] != "undefined" && typeof params["fill-opacity"] == "undefined" ) {
886 o.node.style.fillOpacity = o.attrs["fill-opacity"];
887 // Need following line for Firefox
888 o.node.setAttribute("fill-opacity", o.attrs["fill-opacity"]);
891 o.node.style[att] = getRGB(value).hex;
892 // Need following line for Firefox
893 o.node.setAttribute(att, getRGB(value).hex);
896 addGrdientFill(o.node, value, o.svg);
900 if (o.attrs.gradient) {
901 var gradient = doc.getElementById(o.node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
903 var stops = gradient.getElementsByTagName("stop");
904 stops[stops.length - 1].setAttribute("stop-opacity", value);
909 var cssrule = att.replace(/(\-.)/g, function (w) {
910 return w.substring(1).toUpperCase();
912 o.node.style[cssrule] = value;
913 // Need following line for Firefox
914 o.node.setAttribute(att, value);
921 var tuneText = function (element, params) {
922 if (element.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params)) {
925 var fontSize = element.node.firstChild ? parseInt(doc.defaultView.getComputedStyle(element.node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
928 if ("text" in params) {
929 while (element.node.firstChild) {
930 element.node.removeChild(element.node.firstChild);
932 var texts = (params.text + "").split("\n");
933 for (var i = 0, ii = texts.length; i < ii; i++) {
934 var tspan = doc.createElementNS(element.svg.svgns, "tspan");
935 i && tspan.setAttribute("dy", fontSize * leading);
936 i && tspan.setAttribute("x", element.attrs.x);
937 tspan.appendChild(doc.createTextNode(texts[i]));
938 element.node.appendChild(tspan);
939 height += fontSize * leading;
942 var texts = element.node.getElementsByTagName("tspan");
943 for (var i = 0, ii = texts.length; i < ii; i++) {
944 i && texts[i].setAttribute("dy", fontSize * leading);
945 i && texts[i].setAttribute("x", element.attrs.x);
946 height += fontSize * leading;
949 height -= fontSize * (leading - 1);
950 var dif = height / 2 - fontSize;
952 element.node.setAttribute("y", element.attrs.y - dif);
954 setTimeout(function () {
957 var Element = function (node, svg) {
963 this.attrs = this.attrs || {};
964 this.transformations = []; // rotate, translate, scale
968 rt: {deg: 0, x: 0, y: 0},
973 Element.prototype.translate = function (x, y) {
974 if (x == undefined && y == undefined) {
975 return {x: this._.tx, y: this._.ty};
982 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
987 this.attr({x: this.attrs.x + +x, y: this.attrs.y + +y});
990 var path = pathToRelative(this.attrs.path);
993 this.attr({path: path.join(" ")});
998 Element.prototype.rotate = function (deg, cx, cy) {
1000 return this._.rt.deg;
1002 var bbox = this.getBBox();
1003 deg = deg.toString().split(separator);
1004 if (deg.length - 1) {
1005 cx = parseFloat(deg[1], 10);
1006 cy = parseFloat(deg[2], 10);
1008 deg = parseFloat(deg[0], 10);
1010 this._.rt.deg = deg;
1012 this._.rt.deg += deg;
1017 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1018 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1019 if (this._.rt.deg) {
1020 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
1022 this.transformations[0] = "";
1024 this.node.setAttribute("transform", this.transformations.join(" "));
1027 Element.prototype.hide = function () {
1028 this.node.style.display = "none";
1031 Element.prototype.show = function () {
1032 this.node.style.display = "block";
1035 Element.prototype.remove = function () {
1036 this.node.parentNode.removeChild(this.node);
1038 Element.prototype.getBBox = function () {
1039 return this.node.getBBox();
1041 Element.prototype.attr = function () {
1042 if (arguments.length == 1 && typeof arguments[0] == "string") {
1043 if (arguments[0] == "translation") {
1044 return this.translate();
1046 return this.attrs[arguments[0]];
1048 if (arguments.length == 1 && arguments[0] instanceof Array) {
1050 for (var j in arguments[0]) {
1051 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1055 if (arguments.length == 2) {
1057 params[arguments[0]] = arguments[1];
1058 setFillAndStroke(this, params);
1059 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1060 setFillAndStroke(this, arguments[0]);
1064 Element.prototype.toFront = function () {
1065 this.node.parentNode.appendChild(this.node);
1068 Element.prototype.toBack = function () {
1069 if (this.node.parentNode.firstChild != this.node) {
1070 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1074 Element.prototype.insertAfter = function (element) {
1075 if (element.node.nextSibling) {
1076 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1078 element.node.parentNode.appendChild(this.node);
1082 Element.prototype.insertBefore = function (element) {
1083 element.node.parentNode.insertBefore(this.node, element.node);
1086 var theCircle = function (svg, x, y, r) {
1087 var el = doc.createElementNS(svg.svgns, "circle");
1088 el.setAttribute("cx", x);
1089 el.setAttribute("cy", y);
1090 el.setAttribute("r", r);
1091 el.setAttribute("fill", "none");
1092 el.setAttribute("stroke", "#000");
1094 svg.canvas.appendChild(el);
1096 var res = new Element(el, svg);
1097 res.attrs = res.attrs || {};
1101 res.attrs.stroke = "#000";
1102 res.type = "circle";
1105 var theRect = function (svg, x, y, w, h, r) {
1106 var el = doc.createElementNS(svg.svgns, "rect");
1107 el.setAttribute("x", x);
1108 el.setAttribute("y", y);
1109 el.setAttribute("width", w);
1110 el.setAttribute("height", h);
1112 el.setAttribute("rx", r);
1113 el.setAttribute("ry", r);
1115 el.setAttribute("fill", "none");
1116 el.setAttribute("stroke", "#000");
1118 svg.canvas.appendChild(el);
1120 var res = new Element(el, svg);
1121 res.attrs = res.attrs || {};
1124 res.attrs.width = w;
1125 res.attrs.height = h;
1126 res.attrs.stroke = "#000";
1128 res.attrs.rx = res.attrs.ry = r;
1133 var theEllipse = function (svg, x, y, rx, ry) {
1134 var el = doc.createElementNS(svg.svgns, "ellipse");
1135 el.setAttribute("cx", x);
1136 el.setAttribute("cy", y);
1137 el.setAttribute("rx", rx);
1138 el.setAttribute("ry", ry);
1139 el.setAttribute("fill", "none");
1140 el.setAttribute("stroke", "#000");
1142 svg.canvas.appendChild(el);
1144 var res = new Element(el, svg);
1145 res.attrs = res.attrs || {};
1150 res.attrs.stroke = "#000";
1151 res.type = "ellipse";
1154 var theImage = function (svg, src, x, y, w, h) {
1155 var el = doc.createElementNS(svg.svgns, "image");
1156 el.setAttribute("x", x);
1157 el.setAttribute("y", y);
1158 el.setAttribute("width", w);
1159 el.setAttribute("height", h);
1160 el.setAttribute("preserveAspectRatio", "none");
1161 el.setAttributeNS(svg.xlink, "href", src);
1163 svg.canvas.appendChild(el);
1165 var res = new Element(el, svg);
1166 res.attrs = res.attrs || {};
1169 res.attrs.width = w;
1170 res.attrs.height = h;
1174 var theText = function (svg, x, y, text) {
1175 var el = doc.createElementNS(svg.svgns, "text");
1176 el.setAttribute("x", x);
1177 el.setAttribute("y", y);
1178 el.setAttribute("text-anchor", "middle");
1180 svg.canvas.appendChild(el);
1182 var res = new Element(el, svg);
1183 res.attrs = res.attrs || {};
1187 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000", text: text});
1190 var theGroup = function (svg) {
1191 var el = doc.createElementNS(svg.svgns, "g");
1193 svg.canvas.appendChild(el);
1195 var i = new Element(el, svg);
1196 for (var f in svg) {
1197 if (f[0] != "_" && typeof svg[f] == "function") {
1198 i[f] = (function (f) {
1199 return function () {
1200 var e = svg[f].apply(svg, arguments);
1201 el.appendChild(e[0]);
1210 var setSize = function (width, height) {
1211 this.width = width || this.width;
1212 this.height = height || this.height;
1213 this.canvas.setAttribute("width", this.width);
1214 this.canvas.setAttribute("height", this.height);
1217 create = function () {
1218 // container, width, height
1219 // x, y, width, height
1220 if (typeof arguments[0] == "string") {
1221 var container = doc.getElementById(arguments[0]);
1222 var width = arguments[1];
1223 var height = arguments[2];
1225 if (typeof arguments[0] == "object") {
1226 var container = arguments[0];
1227 var width = arguments[1];
1228 var height = arguments[2];
1230 if (typeof arguments[0] == "number") {
1234 width = arguments[2],
1235 height = arguments[3];
1238 throw new Error("SVG container not found.");
1240 paper.canvas = doc.createElementNS(paper.svgns, "svg");
1241 paper.canvas.setAttribute("width", width || 320);
1242 paper.width = width || 320;
1243 paper.canvas.setAttribute("height", height || 200);
1244 paper.height = height || 200;
1245 if (container == 1) {
1246 doc.body.appendChild(paper.canvas);
1247 paper.canvas.style.position = "absolute";
1248 paper.canvas.style.left = x + "px";
1249 paper.canvas.style.top = y + "px";
1251 if (container.firstChild) {
1252 container.insertBefore(paper.canvas, container.firstChild);
1254 container.appendChild(paper.canvas);
1258 canvas: paper.canvas,
1259 clear: function () {
1260 while (this.canvas.firstChild) {
1261 this.canvas.removeChild(this.canvas.firstChild);
1263 this.defs = doc.createElementNS(paper.svgns, "defs");
1264 this.canvas.appendChild(this.defs);
1267 for (var prop in paper) {
1268 if (prop != "create") {
1269 container[prop] = paper[prop];
1272 for (var prop in R.fn) {
1273 if (!container[prop]) {
1274 container[prop] = R.fn[prop];
1280 paper.remove = function () {
1281 this.canvas.parentNode.removeChild(this.canvas);
1283 paper.svgns = "http://www.w3.org/2000/svg";
1284 paper.xlink = "http://www.w3.org/1999/xlink";
1285 paper.safari = function () {
1286 if (navigator.vendor == "Apple Computer, Inc.") {
1287 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1288 setTimeout(function () {rect.remove();}, 0);
1295 thePath = function (params, pathString, VML) {
1296 var g = doc.createElement("rvml:group"), gl = g.style;
1297 gl.position = "absolute";
1300 gl.width = VML.width + "px";
1301 gl.height = VML.height + "px";
1302 var el = doc.createElement("rvml:shape"), ol = el.style;
1303 ol.width = VML.width + "px";
1304 ol.height = VML.height + "px";
1306 if (params["class"]) {
1307 el.className = params["class"];
1309 el.coordsize = this.coordsize;
1310 el.coordorigin = this.coordorigin;
1312 VML.canvas.appendChild(g);
1313 var p = new Element(el, g, VML);
1314 p.isAbsolute = true;
1317 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1319 p.absolutely = function () {
1320 this.isAbsolute = true;
1323 p.relatively = function () {
1324 this.isAbsolute = false;
1327 p.moveTo = function (x, y) {
1328 var d = this.isAbsolute?"m":"t";
1329 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1330 this.node.path = this.Path += d;
1331 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1332 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1333 this.last.isAbsolute = this.isAbsolute;
1334 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1337 p.lineTo = function (x, y) {
1338 var d = this.isAbsolute?"l":"r";
1339 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1340 this[0].path = this.Path += d;
1341 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1342 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1343 this.last.isAbsolute = this.isAbsolute;
1344 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1347 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1348 // for more information of where this math came from visit:
1349 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1350 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1351 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1352 var x1 = this.last.x,
1356 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1357 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1358 cx = k * rx * y / ry + (x1 + x2) / 2,
1359 cy = k * -ry * x / rx + (y1 + y2) / 2,
1360 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1361 left = Math.round(cx - rx),
1362 top = Math.round(cy - ry);
1363 d += [left, top, Math.round(left + rx * 2), Math.round(top + ry * 2), Math.round(x1), Math.round(y1), Math.round(parseFloat(x2, 10)), Math.round(parseFloat(y2, 10))].join(", ");
1364 this.node.path = this.Path += d;
1365 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2, 10);
1366 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2, 10);
1367 this.last.isAbsolute = this.isAbsolute;
1368 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1371 p.cplineTo = function (x1, y1, w1) {
1373 return this.lineTo(x1, y1);
1375 var x = Math.round(Math.round(parseFloat(x1, 10) * 100) / 100),
1376 y = Math.round(Math.round(parseFloat(y1, 10) * 100) / 100),
1377 w = Math.round(Math.round(parseFloat(w1, 10) * 100) / 100),
1378 d = this.isAbsolute ? "c" : "v",
1379 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1380 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1381 d += attr.join(" ") + " ";
1382 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1383 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1384 this.last.bx = attr[2];
1385 this.last.by = attr[3];
1386 this.node.path = this.Path += d;
1387 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1391 p.curveTo = function () {
1392 var d = this.isAbsolute ? "c" : "v";
1393 if (arguments.length == 6) {
1394 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1395 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1396 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4], 10);
1397 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5], 10);
1398 d += [Math.round(parseFloat(arguments[0], 10)),
1399 Math.round(parseFloat(arguments[1], 10)),
1400 Math.round(parseFloat(arguments[2], 10)),
1401 Math.round(parseFloat(arguments[3], 10)),
1402 Math.round(parseFloat(arguments[4], 10)),
1403 Math.round(parseFloat(arguments[5], 10))].join(" ") + " ";
1404 this.last.isAbsolute = this.isAbsolute;
1405 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1407 if (arguments.length == 4) {
1408 var bx = this.last.x * 2 - this.last.bx;
1409 var by = this.last.y * 2 - this.last.by;
1410 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1411 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1412 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1413 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1414 d += [Math.round(bx), Math.round(by),
1415 Math.round(parseFloat(arguments[0], 10)),
1416 Math.round(parseFloat(arguments[1], 10)),
1417 Math.round(parseFloat(arguments[2], 10)),
1418 Math.round(parseFloat(arguments[3], 10))].join(" ") + " ";
1419 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1421 this.node.path = this.Path += d;
1424 p.qcurveTo = function () {
1426 if (arguments.length == 4) {
1427 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1428 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1429 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1430 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1431 d += [Math.round(this.last.qx),
1432 Math.round(this.last.qy),
1433 Math.round(this.last.x),
1434 Math.round(this.last.y)].join(" ") + " ";
1435 this.last.isAbsolute = this.isAbsolute;
1436 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1438 if (arguments.length == 2) {
1439 this.last.qx = this.last.x * 2 - this.last.qx;
1440 this.last.qy = this.last.y * 2 - this.last.qy;
1441 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1442 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1443 d += [Math.round(this.last.qx),
1444 Math.round(this.last.qy),
1445 Math.round(this.last.x),
1446 Math.round(this.last.y)].join(" ") + " ";
1447 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1449 this.node.path = this.Path += d;
1450 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1453 p.addRoundedCorner = function (r, dir) {
1454 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1457 rollback = function () {
1461 rollback = function () {};
1467 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1470 o.curveTo(-R, 0, -r, r - R, -r, r);
1477 o.curveTo(R, 0, r, -(r - R), r, -r);
1480 o.curveTo(R, 0, r, r - R, r, r);
1487 o.curveTo(0, -R, -(R - r), -r, r, -r);
1490 o.curveTo(0, -R, R - r, -r, -r, -r);
1497 o.curveTo(0, R, -(R - r), r, r, r);
1500 o.curveTo(0, R, R - r, r, -r, r);
1505 actions[dir.charAt(0)]()[dir.charAt(1)]();
1509 p.andClose = function () {
1510 this.node.path = (this.Path += "x e");
1511 this.attrs.path += "z";
1517 paper.pathfinder(p, "" + pathString);
1520 setFillAndStroke(p, params);
1521 if (params.gradient) {
1522 addGrdientFill(p, params.gradient);
1526 var setFillAndStroke = function (o, params) {
1527 var s = o.node.style,
1529 o.attrs = o.attrs || {};
1530 for (var par in params) {
1531 o.attrs[par] = params[par];
1533 if (params.path && o.type == "path") {
1536 paper.pathfinder(o, params.path);
1538 if (params.rotation != null) {
1539 o.rotate(params.rotation, true);
1541 if (params.translation) {
1542 var xy = (params.translation + "").split(separator);
1543 o.translate(xy[0], xy[1]);
1546 var xy = (params.scale + "").split(separator);
1547 o.scale(xy[0], xy[1]);
1549 if (o.type == "image" && params.src) {
1550 o.node.src = params.src;
1552 if (o.type == "image" && params.opacity) {
1553 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1554 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1556 params.font && (s.font = params.font);
1557 params["font-family"] && (s.fontFamily = params["font-family"]);
1558 params["font-size"] && (s.fontSize = params["font-size"]);
1559 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1560 params["font-style"] && (s.fontStyle = params["font-style"]);
1561 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["stroke-dasharray"] || params["stroke-miterlimit"] || params["stroke-linejoin"] || params["stroke-linecap"]) {
1562 o = o.shape || o.node;
1563 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || doc.createElement("rvml:fill");
1564 if ("fill-opacity" in params || "opacity" in params) {
1565 fill.opacity = ((+params["fill-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1570 if (typeof fill.on == "undefined" || params.fill == "none") {
1573 if (fill.on && params.fill) {
1574 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1576 fill.src = isURL[1];
1579 fill.color = getRGB(params.fill).hex;
1581 fill.type = "solid";
1584 o.appendChild(fill);
1585 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || doc.createElement("rvml:stroke");
1586 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"]) {
1589 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1592 if (stroke.on && params.stroke) {
1593 stroke.color = getRGB(params.stroke).hex;
1595 stroke.opacity = ((+params["stroke-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1596 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1597 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1598 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1599 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12 / 16);
1600 if (params["stroke-dasharray"]) {
1604 "-.": "shortdashdot",
1605 "-..": "shortdashdotdot",
1610 "--.": "longdashdot",
1611 "--..": "longdashdotdot"
1613 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1615 o.appendChild(stroke);
1617 if (res.type == "text") {
1618 var span = doc.createElement("span"),
1620 res.attrs.font && (s.font = res.attrs.font);
1621 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1622 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1623 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1624 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1625 res.node.parentNode.appendChild(span);
1626 span.innerText = res.node.string;
1627 res.W = res.attrs.w = span.offsetWidth;
1628 res.H = res.attrs.h = span.offsetHeight;
1629 res.X = res.attrs.x - Math.round(res.W / 2);
1630 res.Y = res.attrs.y - Math.round(res.H / 2);
1631 res.node.parentNode.removeChild(span);
1634 var getAngle = function (a, b, c, d) {
1635 var angle = Math.round(Math.atan((parseFloat(c, 10) - parseFloat(a, 10)) / (parseFloat(d, 10) - parseFloat(b, 10))) * 57.29) || 0;
1636 if (!angle && parseFloat(a, 10) < parseFloat(b, 10)) {
1645 var addGrdientFill = function (o, gradient) {
1646 gradient = toGradient(gradient);
1647 o.attrs = o.attrs || {};
1648 var attrs = o.attrs;
1649 o.attrs.gradient = gradient;
1650 o = o.shape || o[0];
1651 var fill = o.getElementsByTagName("fill");
1655 fill = doc.createElement("rvml:fill");
1657 if (gradient.dots.length) {
1659 fill.method = "none";
1660 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1661 if (typeof gradient.dots[0].color != "undefined") {
1662 fill.color = getRGB(gradient.dots[0].color).hex;
1664 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1665 fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1668 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1669 if (gradient.dots[i].offset) {
1670 colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1673 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;
1674 if (colors.length) {
1675 fill.colors.value = colors.join(",");
1676 fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1678 fill.colors.value = "0% " + fill.color;
1680 fill.opacity = fillOpacity;
1681 if (typeof gradient.angle != "undefined") {
1682 fill.angle = (-gradient.angle + 270) % 360;
1683 } else if (gradient.vector) {
1684 fill.angle = getAngle.apply(null, gradient.vector);
1686 if ((gradient.type + "").toLowerCase() == "radial") {
1687 fill.focus = "100%";
1688 fill.focusposition = "0.5 0.5";
1692 var Element = function (node, group, vml) {
1712 Element.prototype.rotate = function (deg, cx, cy) {
1714 return this._.rt.deg;
1716 deg = deg.toString().split(separator);
1717 if (deg.length - 1) {
1718 cx = parseFloat(deg[1], 10);
1719 cy = parseFloat(deg[2], 10);
1721 deg = parseFloat(deg[0], 10);
1726 this._.rt.deg = deg;
1728 this._.rt.deg += deg;
1732 this.setBox(null, cx, cy);
1733 this.Group.style.rotation = this._.rt.deg;
1736 Element.prototype.setBox = function (params, cx, cy) {
1737 var gs = this.Group.style,
1738 os = (this.shape && this.shape.style) || this.node.style;
1739 for (var i in params) {
1740 this.attrs[i] = params[i];
1742 cx = cx || this._.rt.cx;
1743 cy = cy || this._.rt.cy;
1744 var attr = this.attrs, x, y, w, h;
1745 switch (this.type) {
1747 x = attr.cx - attr.r;
1748 y = attr.cy - attr.r;
1752 x = attr.cx - attr.rx;
1753 y = attr.cy - attr.ry;
1761 w = attr.width || 0;
1762 h = attr.height || 0;
1765 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("");
1766 x = attr.x - Math.round(this.W / 2);
1767 y = attr.y - this.H / 2;
1772 if (!this.attrs.path) {
1776 h = this.vml.height;
1778 var dim = pathDimensions(this.attrs.path),
1789 h = this.vml.height;
1792 cx = (cx == null) ? x + w / 2 : cx;
1793 cy = (cy == null) ? y + h / 2 : cy;
1794 var left = cx - this.vml.width / 2,
1795 top = cy - this.vml.height / 2;
1796 if (this.type == "path" || this.type == "text") {
1797 gs.left = left + "px";
1798 gs.top = top + "px";
1799 this.X = this.type == "text" ? x : -left;
1800 this.Y = this.type == "text" ? y : -top;
1803 os.left = -left + "px";
1804 os.top = -top + "px";
1806 gs.left = left + "px";
1807 gs.top = top + "px";
1812 gs.width = this.vml.width + "px";
1813 gs.height = this.vml.height + "px";
1814 os.left = x - left + "px";
1815 os.top = y - top + "px";
1816 os.width = w + "px";
1817 os.height = h + "px";
1820 Element.prototype.hide = function () {
1821 this.Group.style.display = "none";
1824 Element.prototype.show = function () {
1825 this.Group.style.display = "block";
1828 Element.prototype.translate = function (x, y) {
1829 if (x == undefined && y == undefined) {
1830 return {x: this._.tx, y: this._.ty};
1834 if (this.type == "path") {
1835 var path = this.attrs.path;
1836 path = pathToRelative(path);
1839 this.attr({path: path.join(" ")});
1841 this.setBox({x: this._.tx, y: this._.ty});
1844 Element.prototype.getBBox = function () {
1852 Element.prototype.remove = function () {
1853 this[0].parentNode.removeChild(this[0]);
1854 this.Group.parentNode.removeChild(this.Group);
1855 this.shape && this.shape.parentNode.removeChild(this.shape);
1857 Element.prototype.attr = function () {
1858 if (arguments.length == 1 && typeof arguments[0] == "string") {
1859 if (arguments[0] == "translation") {
1860 return this.translate();
1862 return this.attrs[arguments[0]];
1864 if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
1866 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1867 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1872 if (arguments.length == 2) {
1874 params[arguments[0]] = arguments[1];
1876 if (arguments.length == 1 && typeof arguments[0] == "object") {
1877 params = arguments[0];
1880 if (params.gradient) {
1881 addGrdientFill(this, params.gradient);
1883 if (params.text && this.type == "text") {
1884 this.node.string = params.text;
1887 this.node.id = params.id;
1889 setFillAndStroke(this, params);
1890 this.setBox(params);
1894 Element.prototype.toFront = function () {
1895 this.Group.parentNode.appendChild(this.Group);
1898 Element.prototype.toBack = function () {
1899 if (this.Group.parentNode.firstChild != this.Group) {
1900 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1904 Element.prototype.insertAfter = function (element) {
1905 if (element.Group.nextSibling) {
1906 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1908 element.Group.parentNode.appendChild(this.Group);
1912 Element.prototype.insertBefore = function (element) {
1913 element.Group.parentNode.insertBefore(this.Group, element.Group);
1916 var theCircle = function (vml, x, y, r) {
1917 var g = doc.createElement("rvml:group");
1918 var o = doc.createElement("rvml:oval");
1920 vml.canvas.appendChild(g);
1921 var res = new Element(o, g, vml);
1922 res.type = "circle";
1923 setFillAndStroke(res, {stroke: "#000", fill: "none"});
1927 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1930 var theRect = function (vml, x, y, w, h, r) {
1931 var g = doc.createElement("rvml:group");
1932 var o = doc.createElement(r ? "rvml:roundrect" : "rvml:rect");
1934 o.arcsize = r / (Math.min(w, h));
1937 vml.canvas.appendChild(g);
1938 var res = new Element(o, g, vml);
1940 setFillAndStroke(res, {stroke: "#000"});
1946 res.setBox({x: x, y: y, width: w, height: h});
1949 var theEllipse = function (vml, x, y, rx, ry) {
1950 var g = doc.createElement("rvml:group");
1951 var o = doc.createElement("rvml:oval");
1953 vml.canvas.appendChild(g);
1954 var res = new Element(o, g, vml);
1955 res.type = "ellipse";
1956 setFillAndStroke(res, {stroke: "#000"});
1961 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
1964 var theImage = function (vml, src, x, y, w, h) {
1965 var g = doc.createElement("rvml:group");
1966 var o = doc.createElement("rvml:image");
1969 vml.canvas.appendChild(g);
1970 var res = new Element(o, g, vml);
1976 res.setBox({x: x, y: y, width: w, height: h});
1979 var theText = function (vml, x, y, text) {
1980 var g = doc.createElement("rvml:group"), gs = g.style;
1981 var el = doc.createElement("rvml:shape"), ol = el.style;
1982 var path = doc.createElement("rvml:path"), ps = path.style;
1983 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
1984 path.textpathok = true;
1985 ol.width = vml.width;
1986 ol.height = vml.height;
1987 gs.position = "absolute";
1990 gs.width = vml.width;
1991 gs.height = vml.height;
1992 var o = doc.createElement("rvml:textpath");
1995 o.coordsize = vml.coordsize;
1996 o.coordorigin = vml.coordorigin;
1998 el.appendChild(path);
2000 vml.canvas.appendChild(g);
2001 var res = new Element(o, g, vml);
2003 res.textpath = path;
2009 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2012 var setSize = function (width, height) {
2013 this.width = width || this.width;
2014 this.height = height || this.height;
2015 this.canvas.style.width = this.width + "px";
2016 this.canvas.style.height = this.height + "px";
2017 this.canvas.parentNode.style.clip = "rect(0 " + this.width + " " + this.height + " 0)";
2018 this.canvas.coordsize = this.width + " " + this.height;
2021 create = function () {
2022 // container, width, height
2023 // x, y, width, height
2024 var container, width, height;
2025 if (typeof arguments[0] == "string") {
2026 container = doc.getElementById(arguments[0]);
2027 width = arguments[1];
2028 height = arguments[2];
2030 if (typeof arguments[0] == "object") {
2031 container = arguments[0];
2032 width = arguments[1];
2033 height = arguments[2];
2035 if (typeof arguments[0] == "number") {
2039 width = arguments[2];
2040 height = arguments[3];
2043 throw new Error("VML container not found.");
2045 if (!doc.namespaces["rvml"]) {
2046 doc.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
2047 doc.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
2049 var c = doc.createElement("div"),
2050 d = doc.createElement("div"),
2051 r = paper.canvas = doc.createElement("rvml:group"),
2052 cs = c.style, rs = r.style;
2053 paper.width = width;
2054 paper.height = height;
2055 width = width || "320px";
2056 height = height || "200px";
2057 cs.clip = "rect(0 " + width + " " + height + " 0)";
2060 cs.position = "absolute";
2061 rs.position = "absolute";
2062 d.style.position = "relative";
2065 r.coordsize = (/%$/.test(width) ? width : parseFloat(width, 10)) + " " + (/%$/.test(height) ? height : parseFloat(height, 10));
2066 r.coordorigin = "0 0";
2068 var b = doc.createElement("rvml:rect"), bs = b.style;
2069 bs.left = bs.top = 0;
2070 bs.width = rs.width;
2071 bs.height = rs.height;
2072 b.filled = b.stroked = "f";
2077 if (container == 1) {
2078 doc.body.appendChild(d);
2079 cs.position = "absolute";
2091 cs.width = container.style.width = width;
2092 cs.height = container.style.height = height;
2093 if (container.firstChild) {
2094 container.insertBefore(d, container.firstChild);
2096 container.appendChild(d);
2099 for (var prop in paper) {
2100 container[prop] = paper[prop];
2102 for (var prop in R.fn) {
2103 if (!container[prop]) {
2104 container[prop] = R.fn[prop];
2107 container.clear = function () {
2109 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2110 if (r.childNodes[i] != b) {
2111 todel.push(r.childNodes[i]);
2114 for (i = 0, ii = todel.length; i < ii; i++) {
2115 r.removeChild(todel[i]);
2120 paper.remove = function () {
2121 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2123 paper.safari = function () {};
2129 var addEvent = (function () {
2130 if (doc.addEventListener) {
2131 return function (obj, type, fn, element) {
2132 var f = function (e) {
2133 return fn.call(element, e);
2135 obj.addEventListener(type, f, false);
2136 return function () {
2137 obj.removeEventListener(type, f, false);
2141 } else if (doc.attachEvent) {
2142 return function (obj, type, fn, element) {
2143 var f = function (e) {
2144 return fn.call(element, e || win.event);
2146 obj.attachEvent("on" + type, f);
2147 var detacher = function () {
2148 obj.detachEvent("on" + type, f);
2151 if (type == "mouseover") {
2152 obj.attachEvent("onmouseenter", f);
2153 return function () {
2154 obj.detachEvent("onmouseenter", f);
2157 } else if (type == "mouseout") {
2158 obj.attachEvent("onmouseleave", f);
2159 return function () {
2160 obj.detachEvent("onmouseleave", f);
2168 for (var i = events.length; i--;) {
2169 (function (eventName) {
2170 Element.prototype[eventName] = function (fn) {
2171 if (typeof fn == "function") {
2172 this.events = this.events || {};
2173 this.events[eventName] = this.events[eventName] || {};
2174 this.events[eventName][fn] = this.events[eventName][fn] || [];
2175 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2179 Element.prototype["un" + eventName] = function (fn) {
2181 this.events[eventName] &&
2182 this.events[eventName][fn] &&
2183 this.events[eventName][fn].length &&
2184 this.events[eventName][fn].shift()() &&
2185 !this.events[eventName][fn].length &&
2186 delete this.events[eventName][fn];
2191 paper.circle = function (x, y, r) {
2192 return theCircle(this, x, y, r);
2194 paper.rect = function (x, y, w, h, r) {
2195 return theRect(this, x, y, w, h, r);
2197 paper.ellipse = function (x, y, rx, ry) {
2198 return theEllipse(this, x, y, rx, ry);
2200 paper.path = function (params, pathString) {
2201 return thePath(params, pathString, this);
2203 paper.image = function (src, x, y, w, h) {
2204 return theImage(this, src, x, y, w, h);
2206 paper.text = function (x, y, text) {
2207 return theText(this, x, y, text);
2209 paper.group = function () {
2212 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2213 color = color || "#000";
2214 var p = this.path({stroke: color, "stroke-width": 1})
2215 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y),
2217 columnWidth = w / wv;
2218 for (var i = 1; i < hv; i++) {
2219 p.moveTo(x, y + i * rowHeight).lineTo(x + w, y + i * rowHeight);
2221 for (var i = 1; i < wv; i++) {
2222 p.moveTo(x + i * columnWidth, y).lineTo(x + i * columnWidth, y + h);
2226 paper.pathfinder = function (p, path) {
2228 M: function (x, y) {
2231 C: function (x1, y1, x2, y2, x3, y3) {
2232 this.curveTo(x1, y1, x2, y2, x3, y3);
2234 Q: function (x1, y1, x2, y2) {
2235 this.qcurveTo(x1, y1, x2, y2);
2237 T: function (x, y) {
2238 this.qcurveTo(x, y);
2240 S: function (x1, y1, x2, y2) {
2241 p.curveTo(x1, y1, x2, y2);
2243 L: function (x, y) {
2247 this.lineTo(x, this.last.y);
2250 this.lineTo(this.last.x, y);
2252 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2253 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2260 path = pathToAbsolute(path);
2261 for (var i = 0, ii = path.length; i < ii; i++) {
2262 var b = path[i].shift();
2263 commands[b].apply(p, path[i]);
2266 paper.set = function (itemsArray) {
2267 return new Set(itemsArray);
2269 paper.setSize = setSize;
2270 Element.prototype.stop = function () {
2271 clearTimeout(this.animation_in_progress);
2273 Element.prototype.scale = function (x, y) {
2274 if (x == undefined && y == undefined) {
2275 return {x: this._.sx, y: this._.sy};
2278 // following line is for IE, apparently NaN is not always falsy
2279 isNaN(y) && (y = x);
2282 var dirx = Math.round(x / Math.abs(x)),
2283 diry = Math.round(y / Math.abs(y)),
2284 s = this.node.style;
2285 dx = this.attr("x");
2286 dy = this.attr("y");
2287 cx = this.attr("cx");
2288 cy = this.attr("cy");
2289 if (dirx != 1 || diry != 1) {
2290 if (this.transformations) {
2291 this.transformations[2] = "scale(" + [dirx, diry] + ")";
2292 this.node.setAttribute("transform", this.transformations.join(" "));
2293 dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
2294 dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
2295 cx = this.attr("cx") * dirx;
2296 cy = this.attr("cy") * diry;
2298 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2299 ", M12=0, M21=0, M22=" + diry +
2300 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2301 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2304 if (this.transformations) {
2305 this.transformations[2] = "";
2306 this.node.setAttribute("transform", this.transformations.join(" "));
2308 this.node.filterMatrix = "";
2309 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2312 switch (this.type) {
2316 width: this.attrs.width * x * dirx / this._.sx,
2317 height: this.attrs.height * y * diry / this._.sy,
2325 rx: this.attrs.rx * x * dirx / this._.sx,
2326 ry: this.attrs.ry * y * diry / this._.sy,
2327 r: this.attrs.r * x * diry / this._.sx,
2333 var path = pathToRelative(Raphael.parsePathString(this.attr("path"))),
2335 dim = pathDimensions(this.attrs.path),
2336 dx = -dim.width * (x - 1) / 2,
2337 dy = -dim.height * (y - 1) / 2;
2338 for (var i = 0, ii = path.length; i < ii; i++) {
2339 if (path[i][0].toUpperCase() == "M" && skip) {
2344 if (path[i][0].toUpperCase() == "A") {
2345 path[i][path[i].length - 2] *= x * dirx;
2346 path[i][path[i].length - 1] *= y * diry;
2348 for (var j = 1, jj = path[i].length; j < jj; j++) {
2349 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2353 var dim2 = pathDimensions(path),
2354 dx = dim.x + dim.width / 2 - dim2.x - dim2.width / 2,
2355 dy = dim.y + dim.height / 2 - dim2.y - dim2.height / 2;
2356 path = pathToRelative(path);
2360 this.attr({path: path.join(" ")});
2367 Element.prototype.animate = function (params, ms, callback) {
2368 clearTimeout(this.animation_in_progress);
2373 for (var attr in params) {
2374 if (attr in availableAnimAttrs) {
2375 from[attr] = this.attr(attr);
2376 if (typeof from[attr] == "undefined") {
2377 from[attr] = availableAttrs[attr];
2379 to[attr] = params[attr];
2380 switch (availableAnimAttrs[attr]) {
2382 diff[attr] = (to[attr] - from[attr]) / ms;
2385 from[attr] = getRGB(from[attr]);
2386 var toColour = getRGB(to[attr]);
2388 r: (toColour.r - from[attr].r) / ms,
2389 g: (toColour.g - from[attr].g) / ms,
2390 b: (toColour.b - from[attr].b) / ms
2394 var pathes = pathEqualiser(from[attr], to[attr]);
2395 from[attr] = pathes[0];
2396 to[attr] = pathes[1];
2398 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2399 diff[attr][i] = [0];
2400 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2401 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2406 var values = params[attr].toString().split(separator),
2407 from2 = from[attr].toString().split(separator);
2408 if (attr == "translation") {
2409 from[attr] = [0, 0];
2410 diff[attr] = [values[0] / ms, values[1] / ms];
2411 } else if (attr == "rotation") {
2412 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2413 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2415 from[attr] = (from[attr] + "").split(separator);
2416 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2422 var start = new Date(),
2426 var time = (new Date()).getTime() - start.getTime(),
2430 for (var attr in from) {
2431 switch (availableAnimAttrs[attr]) {
2433 now = +from[attr] + time * diff[attr];
2437 Math.round(from[attr].r + time * diff[attr].r),
2438 Math.round(from[attr].g + time * diff[attr].g),
2439 Math.round(from[attr].b + time * diff[attr].b)
2444 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2445 now[i] = [from[attr][i][0]];
2446 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2447 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2449 now[i] = now[i].join(" ");
2451 now = now.join(" ");
2454 if (attr == "translation") {
2455 var x = diff[attr][0] * (time - prev),
2456 y = diff[attr][1] * (time - prev);
2459 now = [x, y].join(" ");
2460 } else if (attr == "rotation") {
2461 now = +from[attr][0] + time * diff[attr][0];
2462 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2464 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2468 if (attr == "font-size") {
2469 set[attr] = now + "px";
2475 that.animation_in_progress = setTimeout(arguments.callee, 0);
2478 (t.x || t.y) && that.translate(-t.x, -t.y);
2480 clearTimeout(that.animation_in_progress);
2482 (typeof callback == "function") && callback.call(that);
2490 var Set = function (itemsArray) {
2492 if (itemsArray && itemsArray.constructor == Array) {
2493 for (var i = itemsArray.length; i--;) {
2494 if (itemsArray[i].constructor == Element) {
2495 this.items[this.items.length] = itemsArray[i];
2500 Set.prototype.push = function (item) {
2501 if (item && item.constructor == Element) {
2502 var len = this.items.length;
2503 this.items[len] = item;
2508 Set.prototype.pull = function (id) {
2509 var res = this.items.splice(id, 1)[0];
2510 for (var j = id, jj = this.items.length; j < jj; j++) {
2511 this[j] = this[j + 1];
2513 delete this[jj + 1];
2516 for (var method in Element.prototype) {
2517 Set.prototype[method] = (function (methodname) {
2518 return function () {
2519 for (var i = this.items.length; i--;) {
2520 this.items[i][methodname].apply(this.items[i], arguments);
2526 Set.prototype.getBBox = function () {
2527 var x = [], y = [], w = [], h = [];
2528 for (var i = this.items.length; i--;) {
2529 var box = this.items[i].getBBox();
2532 w.push(box.x + box.width);
2533 h.push(box.y + box.height);
2535 x = Math.min.apply(Math, x);
2536 y = Math.min.apply(Math, y);
2540 width: Math.max.apply(Math, w) - x,
2541 height: Math.max.apply(Math, h) - y