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.setWindow = function (newwin) {
34 R.hsb2rgb = function (hue, saturation, brightness) {
35 if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
43 if (brightness == 0) {
44 return {r: 0, g: 0, b: 0, hex: "#000"};
46 if (hue > 1 || saturation > 1 || brightness > 1) {
51 var i = Math.floor(hue * 6),
53 p = brightness * (1 - saturation),
54 q = brightness * (1 - (saturation * f)),
55 t = brightness * (1 - (saturation * (1 - f)));
56 red = [brightness, q, p, p, t, brightness, brightness][i];
57 green = [t, brightness, brightness, q, p, p, t][i];
58 blue = [p, p, t, brightness, brightness, q, p][i];
62 var rgb = {r: red, g: green, b: blue};
63 var r = Math.round(red).toString(16);
67 var g = Math.round(green).toString(16);
71 var b = Math.round(blue).toString(16);
75 rgb.hex = "#" + r + g + b;
78 R.rgb2hsb = function (red, green, blue) {
79 if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
84 if (typeof red == "string") {
85 var clr = R.getRGB(red);
90 if (red > 1 || green > 1 || blue > 1) {
95 var max = Math.max(red, green, blue),
96 min = Math.min(red, green, blue),
101 return {h: 0, s: 0, b: max};
103 var delta = (max - min);
104 saturation = delta / max;
106 hue = (green - blue) / delta;
107 } else if (green == max) {
108 hue = 2 + ((blue - red) / delta);
110 hue = 4 + ((red - green) / delta);
120 return {h: hue, s: saturation, b: brightness};
122 var getRGBcache = {}, getRGBcount = [];
123 R.getRGB = function (colour) {
124 if (colour in getRGBcache) {
125 return getRGBcache[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"},
129 if (colour.toString().toLowerCase() in htmlcolors) {
130 colour = htmlcolors[colour.toString().toLowerCase()];
133 return {r: 0, g: 0, b: 0, hex: "#000"};
135 if (colour == "none") {
136 return {r: -1, g: -1, b: -1, hex: "none"};
138 var red, green, blue,
139 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);
142 blue = parseInt(rgb[2].substring(5), 16);
143 green = parseInt(rgb[2].substring(3, 5), 16);
144 red = parseInt(rgb[2].substring(1, 3), 16);
147 blue = parseInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
148 green = parseInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
149 red = parseInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
152 rgb = rgb[4].split(/\s*,\s*/);
153 red = parseFloat(rgb[0]);
154 green = parseFloat(rgb[1]);
155 blue = parseFloat(rgb[2]);
158 rgb = rgb[5].split(/\s*,\s*/);
159 red = parseFloat(rgb[0]) * 2.55;
160 green = parseFloat(rgb[1]) * 2.55;
161 blue = parseFloat(rgb[2]) * 2.55;
164 rgb = rgb[6].split(/\s*,\s*/);
165 red = parseFloat(rgb[0]);
166 green = parseFloat(rgb[1]);
167 blue = parseFloat(rgb[2]);
168 return R.hsb2rgb(red, green, blue);
171 rgb = rgb[7].split(/\s*,\s*/);
172 red = parseFloat(rgb[0]) * 2.55;
173 green = parseFloat(rgb[1]) * 2.55;
174 blue = parseFloat(rgb[2]) * 2.55;
175 return R.hsb2rgb(red, green, blue);
177 var rgb = {r: red, g: green, b: blue};
178 var r = Math.round(red).toString(16);
179 (r.length == 1) && (r = "0" + r);
180 var g = Math.round(green).toString(16);
181 (g.length == 1) && (g = "0" + g);
182 var b = Math.round(blue).toString(16);
183 (b.length == 1) && (b = "0" + b);
184 rgb.hex = "#" + r + g + b;
187 res = {r: -1, g: -1, b: -1, hex: "none"};
189 if (getRGBcount.length > 20) {
190 delete getRGBcache[getRGBcount.unshift()];
192 getRGBcount.push(colour);
193 getRGBcache[colour] = res;
196 R.getColor = function (value) {
197 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
198 rgb = this.hsb2rgb(start.h, start.s, start.b);
204 this.getColor.start = {h: 0, s: 1, b: start.b};
209 R.getColor.reset = function () {
213 var pathcache = {}, pathcount = [];
214 R.parsePathString = function (pathString) {
215 if (pathString in pathcache) {
216 console.log("cache1:" + pathString);
217 console.log("cache2:" + pathcache[pathString]);
218 return pathcache[pathString];
220 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
222 toString = function () {
224 for (var i = 0, ii = this.length; i < ii; i++) {
225 res += this[i][0] + this[i].join(",").substring(2);
229 if (pathString.toString.toString() == toString.toString()) {
233 pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
234 var params = [], name = b.toLowerCase();
235 c.replace(/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
236 b && params.push(+b);
238 while (params.length >= paramCounts[name]) {
239 data.push([b].concat(params.splice(0, paramCounts[name])));
240 if (!paramCounts[name]) {
245 data.toString = toString;
247 if (pathcount.length > 20) {
248 delete pathcache[pathcount.unshift()];
250 pathcount.push(pathString);
251 // console.log("data:" + pathString + ":" + data);
252 pathcache[pathString] = data;
255 var pathDimensions = function (path) {
256 var pathArray = path;
257 if (typeof path == "string") {
258 pathArray = R.parsePathString(path);
260 pathArray = pathToAbsolute(pathArray);
261 var x = [], y = [], length = 0;
262 for (var i = 0, ii = pathArray.length; i < ii; i++) {
263 var pa = pathArray[i];
268 x.push(pa[pa.length - 2]);
269 y.push(pa[pa.length - 1]);
272 for (var j = 1, jj = pa.length; j < jj; j++) {
273 (j % 2 ? x : y).push(pa[j]);
277 var minx = Math.min.apply(Math, x),
278 miny = Math.min.apply(Math, y);
282 width: Math.max.apply(Math, x) - minx,
283 height: Math.max.apply(Math, y) - miny,
288 pathToRelative = function (pathArray) {
290 if (typeof pathArray == "string") {
291 pathArray = R.parsePathString(pathArray);
293 var x = 0, y = 0, start = 0;
294 if (pathArray[0][0] == "M") {
298 res.push(["M", x, y]);
300 for (var i = start, ii = pathArray.length; i < ii; i++) {
303 if (pa[0] != pa[0].toLowerCase()) {
304 r[0] = pa[0].toLowerCase();
312 r[6] = +(pa[6] - x).toFixed(3);
313 r[7] = +(pa[7] - y).toFixed(3);
316 r[1] = +(pa[1] - y).toFixed(3);
319 for (var j = 1, jj = pa.length; j < jj; j++) {
320 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
330 x += res[i][res[i].length - 1];
333 y += res[i][res[i].length - 1];
336 x += res[i][res[i].length - 2];
337 y += res[i][res[i].length - 1];
340 res.toString = pathArray.toString;
343 pathToAbsolute = function (pathArray) {
345 if (typeof pathArray == "string") {
346 pathArray = R.parsePathString(pathArray);
351 if (pathArray[0][0] == "M") {
352 x = +pathArray[0][1];
353 y = +pathArray[0][2];
355 res[0] = ["M", x, y];
357 for (var i = start, ii = pathArray.length; i < ii; i++) {
360 if (pa[0] != (pa[0] + "").toUpperCase()) {
361 r[0] = (pa[0] + "").toUpperCase();
369 r[6] = +(pa[6] + x).toFixed(3);
370 r[7] = +(pa[7] + y).toFixed(3);
376 for (var j = 1, jj = pa.length; j < jj; j++) {
377 r[j] = +pa[j] + ((j % 2) ? x : y);
393 x = res[i][res[i].length - 2];
394 y = res[i][res[i].length - 1];
397 res.toString = pathArray.toString;
400 pecache = {}, pecount = [],
401 pathEqualiser = function (path1, path2) {
402 if ((path1 + path2) in pecache) {
403 return pecache[path1 + path2];
405 var data = [pathToAbsolute(R.parsePathString(path1)), pathToAbsolute(R.parsePathString(path2))],
406 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}],
407 processPath = function (path, d) {
417 var nx = d.x + (d.x - (d.bx || d.x));
418 var ny = d.y + (d.y - (d.by || d.y));
419 path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
422 var nx = d.x + (d.x - (d.bx || d.x));
423 var ny = d.y + (d.y - (d.by || d.y));
424 path = ["Q", nx, ny, path[1], path[2]];
427 path = ["L", path[1], d.y];
430 path = ["L", d.x, path[1]];
433 path = ["L", d.X, d.Y];
438 edgeCases = function (a, b, i) {
439 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
440 data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
441 attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
442 attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
443 attrs[a].x = data[a][i][data[a][i].length - 2];
444 attrs[a].y = data[a][i][data[a][i].length - 1];
446 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
447 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]];
448 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
449 data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
450 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
451 var x = data[b][i][data[b][i].length - 2];
452 var y = data[b][i][data[b][i].length - 1];
453 data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
454 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
456 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
457 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
458 attrs[b].x = data[b][i][data[b][i].length - 2];
459 attrs[b].y = data[b][i][data[b][i].length - 1];
461 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
462 var x = data[b][i][data[b][i].length - 2];
463 var y = data[b][i][data[b][i].length - 1];
464 data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
465 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
467 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
468 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
469 attrs[b].x = data[b][i][data[b][i].length - 2];
470 attrs[b].y = data[b][i][data[b][i].length - 1];
472 } else if (data[a][i][0] == "U") {
473 data[a][i][0] = data[b][i][0];
474 for (var j = 1, jj = data[b][i].length; j < jj; j++) {
475 data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
480 for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
481 data[0][i] = processPath(data[0][i], attrs[0]);
482 data[1][i] = processPath(data[1][i], attrs[1]);
483 if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
486 attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
487 attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
488 attrs[0].x = data[0][i][data[0][i].length - 2];
489 attrs[0].y = data[0][i][data[0][i].length - 1];
490 attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
491 attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
492 attrs[1].x = data[1][i][data[1][i].length - 2];
493 attrs[1].y = data[1][i][data[1][i].length - 1];
495 if (pecount.length > 20) {
496 delete pecache[pecount.unshift()];
498 pecount.push(path1 + path2);
499 pecache[path1 + path2] = data;
502 toGradient = function (gradient) {
503 if (typeof gradient == "string") {
504 gradient = gradient.split(/\s*\-\s*/);
505 var angle = gradient.shift();
506 if (angle.toLowerCase() == "v") {
508 } else if (angle.toLowerCase() == "h") {
511 angle = parseFloat(angle);
514 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)]};
515 var max = 1 / (Math.max(Math.abs(grobj.vector[2]), Math.abs(grobj.vector[3])) || 1);
516 grobj.vector[2] *= max;
517 grobj.vector[3] *= max;
518 if (grobj.vector[2] < 0) {
519 grobj.vector[0] = -grobj.vector[2];
522 if (grobj.vector[3] < 0) {
523 grobj.vector[1] = -grobj.vector[3];
526 grobj.vector[0] = grobj.vector[0].toFixed(3);
527 grobj.vector[1] = grobj.vector[1].toFixed(3);
528 grobj.vector[2] = grobj.vector[2].toFixed(3);
529 grobj.vector[3] = grobj.vector[3].toFixed(3);
530 for (var i = 0, ii = gradient.length; i < ii; i++) {
532 var par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
533 dot.color = R.getRGB(par[1]).hex;
534 par[2] && (dot.offset = par[2] + "%");
535 grobj.dots.push(dot);
537 for (var i = 1, ii = grobj.dots.length - 1; i < ii; i++) {
538 if (!grobj.dots[i].offset) {
539 var start = parseFloat(grobj.dots[i - 1].offset || 0),
541 for (var j = i + 1; j < ii; j++) {
542 if (grobj.dots[j].offset) {
543 end = grobj.dots[j].offset;
551 end = parseFloat(end);
552 var d = (end - start) / (j - i + 1);
555 grobj.dots[i].offset = start + "%";
564 getContainer = function () {
565 var container, x, y, width, height;
566 if (typeof arguments[0] == "string" || typeof arguments[0] == "object") {
567 if (typeof arguments[0] == "string") {
568 container = doc.getElementById(arguments[0]);
570 container = arguments[0];
572 if (container.tagName) {
573 if (arguments[1] == null) {
575 container: container,
576 width: container.style.pixelWidth || container.offsetWidth,
577 height: container.style.pixelHeight || container.offsetHeight
580 return {container: container, width: arguments[1], height: arguments[2]};
583 } else if (typeof arguments[0] == "number" && arguments.length > 3) {
584 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
587 plugins = function (con, add) {
588 for (var prop in add) if (add.hasOwnProperty(prop) && !(prop in con)) {
589 switch (typeof add[prop]) {
591 con[prop] = con === paper ? add[prop] : function () { return add[prop].apply(paper, arguments); };
595 plugins(con[prop], add[prop]);
598 con[prop] = add[prop];
606 R.toString = function () {
607 return "Your browser supports SVG.\nYou are running Rapha\u00ebl " + this.version;
609 var thePath = function (params, pathString, SVG) {
610 var el = doc.createElementNS(SVG.svgns, "path");
612 SVG.canvas.appendChild(el);
614 var p = new Element(el, SVG);
617 p.last = {x: 0, y: 0, bx: 0, by: 0};
618 p.absolutely = function () {
619 this.isAbsolute = true;
622 p.relatively = function () {
623 this.isAbsolute = false;
626 p.moveTo = function (x, y) {
627 var d = this.isAbsolute?"M":"m";
628 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
629 var oldD = this[0].getAttribute("d") || "";
630 (oldD == "M0,0") && (oldD = "");
631 this[0].setAttribute("d", oldD + d);
632 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
633 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
634 this.attrs.path = oldD + d;
637 p.lineTo = function (x, y) {
638 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
639 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
640 var d = this.isAbsolute?"L":"l";
641 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
642 var oldD = this[0].getAttribute("d") || "";
643 this[0].setAttribute("d", oldD + d);
644 this.attrs.path = oldD + d;
647 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
648 var d = this.isAbsolute ? "A" : "a";
649 d += [parseFloat(rx).toFixed(3), parseFloat(ry).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x).toFixed(3), parseFloat(y).toFixed(3)].join(" ");
650 var oldD = this[0].getAttribute("d") || "";
651 this[0].setAttribute("d", oldD + d);
652 this.last.x = parseFloat(x);
653 this.last.y = parseFloat(y);
654 this.attrs.path = oldD + d;
657 p.cplineTo = function (x1, y1, w1) {
659 return this.lineTo(x1, y1);
662 var x = parseFloat(x1);
663 var y = parseFloat(y1);
664 var w = parseFloat(w1);
665 var d = this.isAbsolute?"C":"c";
666 var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
667 for (var i = 0, ii = attr.length; i < ii; i++) {
668 d += attr[i].toFixed(3) + " ";
670 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
671 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
672 this.last.bx = attr[2];
673 this.last.by = attr[3];
674 var oldD = this[0].getAttribute("d") || "";
675 this[0].setAttribute("d", oldD + d);
676 this.attrs.path = oldD + d;
680 p.curveTo = function () {
682 command = [0, 1, 2, 3, "s", 5, "c"];
684 var d = command[arguments.length];
685 if (this.isAbsolute) {
688 for (var i = 0, ii = arguments.length; i < ii; i++) {
689 d += parseFloat(arguments[i]).toFixed(3) + " ";
691 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
692 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
693 this.last.bx = parseFloat(arguments[arguments.length - 4]);
694 this.last.by = parseFloat(arguments[arguments.length - 3]);
695 var oldD = this.node.getAttribute("d") || "";
696 this.node.setAttribute("d", oldD + d);
697 this.attrs.path = oldD + d;
700 p.qcurveTo = function () {
702 command = [0, 1, "t", 3, "q"];
704 var d = command[arguments.length];
705 if (this.isAbsolute) {
708 for (var i = 0, ii = arguments.length; i < ii; i++) {
709 d += parseFloat(arguments[i]).toFixed(3) + " ";
711 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
712 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
713 if (arguments.length != 2) {
714 this.last.qx = parseFloat(arguments[arguments.length - 4]);
715 this.last.qy = parseFloat(arguments[arguments.length - 3]);
717 var oldD = this.node.getAttribute("d") || "";
718 this.node.setAttribute("d", oldD + d);
719 this.attrs.path = oldD + d;
722 p.addRoundedCorner = function (r, dir) {
723 var R = .5522 * r, rollback = this.isAbsolute, o = this;
726 rollback = function () {
730 rollback = function () {};
736 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
739 o.curveTo(-R, 0, -r, r - R, -r, r);
746 o.curveTo(R, 0, r, -(r - R), r, -r);
749 o.curveTo(R, 0, r, r - R, r, r);
756 o.curveTo(0, -R, -(R - r), -r, r, -r);
759 o.curveTo(0, -R, R - r, -r, -r, -r);
766 o.curveTo(0, R, -(R - r), r, r, r);
769 o.curveTo(0, R, R - r, r, -r, r);
774 actions[dir[0]]()[dir[1]]();
778 p.andClose = function () {
779 var oldD = this[0].getAttribute("d") || "";
780 this[0].setAttribute("d", oldD + "Z ");
781 this.attrs.path = oldD + "Z ";
785 p.attrs.path = "" + pathString;
787 paper.pathfinder(p, p.attrs.path);
790 params.fill = params.fill || "none";
791 params.stroke = params.stroke || "#000";
793 params = {fill: "none", stroke: "#000"};
795 setFillAndStroke(p, params);
798 var addGrdientFill = function (o, gradient, SVG) {
799 gradient = toGradient(gradient);
800 var el = doc.createElementNS(SVG.svgns, (gradient.type || "linear") + "Gradient");
801 el.id = "raphael-gradient-" + R.idGenerator++;
802 if (gradient.vector && gradient.vector.length) {
803 el.setAttribute("x1", gradient.vector[0]);
804 el.setAttribute("y1", gradient.vector[1]);
805 el.setAttribute("x2", gradient.vector[2]);
806 el.setAttribute("y2", gradient.vector[3]);
808 SVG.defs.appendChild(el);
809 var isopacity = true;
810 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
811 var stop = doc.createElementNS(SVG.svgns, "stop");
812 if (gradient.dots[i].offset) {
815 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
816 stop.setAttribute("stop-color", R.getRGB(gradient.dots[i].color).hex || "#fff");
817 // ignoring opacity for internal points, because VML doesn't support it
818 el.appendChild(stop);
820 if (isopacity && typeof gradient.dots[ii - 1].opacity != "undefined") {
821 stop.setAttribute("stop-opacity", gradient.dots[ii - 1].opacity);
823 o.setAttribute("fill", "url(#" + el.id + ")");
825 o.style.fillOpacity = 1;
826 o.setAttribute("opacity", 1);
827 o.setAttribute("fill-opacity", 1);
829 var updatePosition = function (o) {
831 var bbox = o.getBBox();
832 o.pattern.setAttribute("patternTransform", "translate(".concat(bbox.x, ",", bbox.y, ")"));
835 var setFillAndStroke = function (o, params) {
840 "-..": [3, 1, 1, 1, 1, 1],
846 "--..": [8, 3, 1, 3, 1, 3]
850 addDashes = function (o, value) {
851 value = dasharray[value.toString().toLowerCase()];
853 var width = o.attrs["stroke-width"] || "1",
854 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
856 for (var i = 0, ii = value.length; i < ii; i++) {
857 dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
859 value = dashes.join(",");
860 node.setAttribute("stroke-dasharray", value);
863 for (var att in params) {
864 if (!(att in availableAttrs)) {
867 var value = params[att];
874 var pn = node.parentNode;
875 if (pn.tagName.toLowerCase() != "a") {
876 var hl = doc.createElementNS(o.svg.svgns, "a");
877 pn.insertBefore(hl, node);
878 hl.appendChild(node);
881 pn.setAttributeNS(o.svg.xlink, att, value);
884 if (o.type == "path") {
885 node.setAttribute("d", "M0,0");
886 paper.pathfinder(o, value);
889 node.setAttribute(att, value);
898 value = -attrs.x - (attrs.width || 0);
902 node.setAttribute(att, value);
906 node.setAttribute(att, value);
915 value = -attrs.y - (attrs.height || 0);
919 node.setAttribute(att, value);
923 if (o.type == "rect") {
924 node.setAttribute("rx", value);
925 node.setAttribute("ry", value);
927 node.setAttribute(att, value);
931 if (o.type == "image") {
932 node.setAttributeNS(o.svg.xlink, "href", value);
936 node.style.strokeWidth = value;
937 // Need following line for Firefox
938 node.setAttribute(att, value);
939 if (attrs["stroke-dasharray"]) {
940 addDashes(o, attrs["stroke-dasharray"]);
943 case "stroke-dasharray":
947 o.rotate(value, true);
950 var xy = (value + "").split(separator);
951 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
954 var xy = (value + "").split(separator);
955 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
958 var isURL = value.match(/^url\(([^\)]+)\)$/i);
960 var el = doc.createElementNS(o.svg.svgns, "pattern");
961 var ig = doc.createElementNS(o.svg.svgns, "image");
962 el.id = "raphael-pattern-" + R.idGenerator++;
963 el.setAttribute("x", 0);
964 el.setAttribute("y", 0);
965 el.setAttribute("patternUnits", "userSpaceOnUse");
966 ig.setAttribute("x", 0);
967 ig.setAttribute("y", 0);
968 ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
971 var img = doc.createElement("img");
972 img.style.position = "absolute";
973 img.style.top = "-9999em";
974 img.style.left = "-9999em";
975 img.onload = function () {
976 el.setAttribute("width", this.offsetWidth);
977 el.setAttribute("height", this.offsetHeight);
978 ig.setAttribute("width", this.offsetWidth);
979 ig.setAttribute("height", this.offsetHeight);
980 doc.body.removeChild(this);
983 doc.body.appendChild(img);
985 o.svg.defs.appendChild(el);
986 node.style.fill = "url(#" + el.id + ")";
987 node.setAttribute("fill", "url(#" + el.id + ")");
992 delete params.gradient;
993 delete attrs.gradient;
994 if (typeof attrs.opacity != "undefined" && typeof params.opacity == "undefined" ) {
995 node.style.opacity = attrs.opacity;
996 // Need following line for Firefox
997 node.setAttribute("opacity", attrs.opacity);
999 if (typeof attrs["fill-opacity"] != "undefined" && typeof params["fill-opacity"] == "undefined" ) {
1000 node.style.fillOpacity = o.attrs["fill-opacity"];
1001 // Need following line for Firefox
1002 node.setAttribute("fill-opacity", attrs["fill-opacity"]);
1005 node.style[att] = R.getRGB(value).hex;
1006 // Need following line for Firefox
1007 node.setAttribute(att, R.getRGB(value).hex);
1010 addGrdientFill(node, value, o.svg);
1013 case "fill-opacity":
1014 if (attrs.gradient) {
1015 var gradient = doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
1017 var stops = gradient.getElementsByTagName("stop");
1018 stops[stops.length - 1].setAttribute("stop-opacity", value);
1023 var cssrule = att.replace(/(\-.)/g, function (w) {
1024 return w.substring(1).toUpperCase();
1026 node.style[cssrule] = value;
1027 // Need following line for Firefox
1028 node.setAttribute(att, value);
1033 tuneText(o, params);
1036 var tuneText = function (el, params) {
1037 if (el.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params || "y" in params)) {
1042 fontSize = node.firstChild ? parseInt(doc.defaultView.getComputedStyle(node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
1044 if ("text" in params) {
1045 while (node.firstChild) {
1046 node.removeChild(node.firstChild);
1048 var texts = (params.text + "").split("\n");
1049 for (var i = 0, ii = texts.length; i < ii; i++) {
1050 var tspan = doc.createElementNS(el.svg.svgns, "tspan");
1051 i && tspan.setAttribute("dy", fontSize * leading);
1052 i && tspan.setAttribute("x", a.x);
1053 tspan.appendChild(doc.createTextNode(texts[i]));
1054 node.appendChild(tspan);
1057 var texts = node.getElementsByTagName("tspan");
1058 for (var i = 0, ii = texts.length; i < ii; i++) {
1059 i && texts[i].setAttribute("dy", fontSize * leading);
1060 i && texts[i].setAttribute("x", a.x);
1063 node.setAttribute("y", a.y);
1064 var bb = el.getBBox(),
1065 dif = a.y - (bb.y + bb.height / 2);
1066 dif && node.setAttribute("y", a.y + dif);
1068 var Element = function (node, svg) {
1074 this.attrs = this.attrs || {};
1075 this.transformations = []; // rotate, translate, scale
1079 rt: {deg: 0, x: 0, y: 0},
1084 Element.prototype.rotate = function (deg, cx, cy) {
1086 return this._.rt.deg;
1088 var bbox = this.getBBox();
1089 deg = deg.toString().split(separator);
1090 if (deg.length - 1) {
1091 cx = parseFloat(deg[1]);
1092 cy = parseFloat(deg[2]);
1094 deg = parseFloat(deg[0]);
1096 this._.rt.deg = deg;
1098 this._.rt.deg += deg;
1103 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1104 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1105 if (this._.rt.deg) {
1106 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
1108 this.transformations[0] = "";
1110 this.node.setAttribute("transform", this.transformations.join(" "));
1113 Element.prototype.hide = function () {
1114 this.node.style.display = "none";
1117 Element.prototype.show = function () {
1118 this.node.style.display = "block";
1121 Element.prototype.remove = function () {
1122 this.node.parentNode.removeChild(this.node);
1124 Element.prototype.getBBox = function () {
1125 var bbox = this.node.getBBox();
1126 if (this.type == "text") {
1127 bbox = {x: bbox.x, y: Infinity, width: bbox.width, height: 0};
1128 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1129 var bb = this.node.getExtentOfChar(i);
1130 (bb.y < bbox.y) && (bbox.y = bb.y);
1131 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1136 Element.prototype.attr = function () {
1137 if (arguments.length == 1 && typeof arguments[0] == "string") {
1138 if (arguments[0] == "translation") {
1139 return this.translate();
1141 return this.attrs[arguments[0]];
1143 if (arguments.length == 1 && arguments[0] instanceof win.Array) {
1145 for (var j in arguments[0]) {
1146 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1150 if (arguments.length == 2) {
1152 params[arguments[0]] = arguments[1];
1153 setFillAndStroke(this, params);
1154 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1155 setFillAndStroke(this, arguments[0]);
1159 Element.prototype.toFront = function () {
1160 this.node.parentNode.appendChild(this.node);
1163 Element.prototype.toBack = function () {
1164 if (this.node.parentNode.firstChild != this.node) {
1165 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1169 Element.prototype.insertAfter = function (element) {
1170 if (element.node.nextSibling) {
1171 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1173 element.node.parentNode.appendChild(this.node);
1177 Element.prototype.insertBefore = function (element) {
1178 element.node.parentNode.insertBefore(this.node, element.node);
1181 var theCircle = function (svg, x, y, r) {
1182 var el = doc.createElementNS(svg.svgns, "circle");
1183 el.setAttribute("cx", x);
1184 el.setAttribute("cy", y);
1185 el.setAttribute("r", r);
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 || {};
1196 res.attrs.stroke = "#000";
1197 res.type = "circle";
1200 var theRect = function (svg, x, y, w, h, r) {
1201 var el = doc.createElementNS(svg.svgns, "rect");
1202 el.setAttribute("x", x);
1203 el.setAttribute("y", y);
1204 el.setAttribute("width", w);
1205 el.setAttribute("height", h);
1207 el.setAttribute("rx", r);
1208 el.setAttribute("ry", r);
1210 el.setAttribute("fill", "none");
1211 el.setAttribute("stroke", "#000");
1213 svg.canvas.appendChild(el);
1215 var res = new Element(el, svg);
1216 res.attrs = res.attrs || {};
1219 res.attrs.width = w;
1220 res.attrs.height = h;
1221 res.attrs.stroke = "#000";
1223 res.attrs.rx = res.attrs.ry = r;
1228 var theEllipse = function (svg, x, y, rx, ry) {
1229 var el = doc.createElementNS(svg.svgns, "ellipse");
1230 el.setAttribute("cx", x);
1231 el.setAttribute("cy", y);
1232 el.setAttribute("rx", rx);
1233 el.setAttribute("ry", ry);
1234 el.setAttribute("fill", "none");
1235 el.setAttribute("stroke", "#000");
1237 svg.canvas.appendChild(el);
1239 var res = new Element(el, svg);
1240 res.attrs = res.attrs || {};
1245 res.attrs.stroke = "#000";
1246 res.type = "ellipse";
1249 var theImage = function (svg, src, x, y, w, h) {
1250 var el = doc.createElementNS(svg.svgns, "image");
1251 el.setAttribute("x", x);
1252 el.setAttribute("y", y);
1253 el.setAttribute("width", w);
1254 el.setAttribute("height", h);
1255 el.setAttribute("preserveAspectRatio", "none");
1256 el.setAttributeNS(svg.xlink, "href", src);
1258 svg.canvas.appendChild(el);
1260 var res = new Element(el, svg);
1261 res.attrs = res.attrs || {};
1264 res.attrs.width = w;
1265 res.attrs.height = h;
1269 var theText = function (svg, x, y, text) {
1270 var el = doc.createElementNS(svg.svgns, "text");
1271 el.setAttribute("x", x);
1272 el.setAttribute("y", y);
1273 el.setAttribute("text-anchor", "middle");
1275 svg.canvas.appendChild(el);
1277 var res = new Element(el, svg);
1278 res.attrs = res.attrs || {};
1282 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000", text: text});
1285 var setSize = function (width, height) {
1286 this.width = width || this.width;
1287 this.height = height || this.height;
1288 this.canvas.setAttribute("width", this.width);
1289 this.canvas.setAttribute("height", this.height);
1292 var create = function () {
1293 var con = getContainer.apply(null, arguments);
1294 var container = con.container,
1298 height = con.height;
1300 throw new Error("SVG container not found.");
1302 paper.canvas = doc.createElementNS(paper.svgns, "svg");
1303 paper.canvas.setAttribute("width", width || 320);
1304 paper.width = width || 320;
1305 paper.canvas.setAttribute("height", height || 200);
1306 paper.height = height || 200;
1307 if (container == 1) {
1308 doc.body.appendChild(paper.canvas);
1309 paper.canvas.style.position = "absolute";
1310 paper.canvas.style.left = x + "px";
1311 paper.canvas.style.top = y + "px";
1313 if (container.firstChild) {
1314 container.insertBefore(paper.canvas, container.firstChild);
1316 container.appendChild(paper.canvas);
1320 canvas: paper.canvas,
1321 clear: function () {
1322 while (this.canvas.firstChild) {
1323 this.canvas.removeChild(this.canvas.firstChild);
1325 this.defs = doc.createElementNS(paper.svgns, "defs");
1326 this.canvas.appendChild(this.defs);
1329 for (var prop in paper) {
1330 if (prop != "create") {
1331 container[prop] = paper[prop];
1334 plugins(container, R.fn);
1338 paper.remove = function () {
1339 this.canvas.parentNode.removeChild(this.canvas);
1341 paper.svgns = "http://www.w3.org/2000/svg";
1342 paper.xlink = "http://www.w3.org/1999/xlink";
1343 paper.safari = function () {
1344 if (navigator.vendor == "Apple Computer, Inc.") {
1345 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1346 setTimeout(function () {rect.remove();}, 0);
1353 R.toString = function () {
1354 return "Your browser doesn\u2019t support SVG.\nYou are running Rapha\u00ebl " + this.version;
1356 thePath = function (params, pathString, VML) {
1357 var g = createNode("group"), gl = g.style;
1358 gl.position = "absolute";
1361 gl.width = VML.width + "px";
1362 gl.height = VML.height + "px";
1363 var el = createNode("shape"), ol = el.style;
1364 ol.width = VML.width + "px";
1365 ol.height = VML.height + "px";
1367 if (params["class"]) {
1368 el.className = "rvml " + params["class"];
1370 el.coordsize = this.coordsize;
1371 el.coordorigin = this.coordorigin;
1373 var p = new Element(el, g, VML);
1374 p.isAbsolute = true;
1377 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1379 p.absolutely = function () {
1380 this.isAbsolute = true;
1383 p.relatively = function () {
1384 this.isAbsolute = false;
1387 p.moveTo = function (x, y) {
1388 var d = this.isAbsolute?"m":"t";
1389 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1390 this.node.path = this.Path += d;
1391 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1392 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1393 this.last.isAbsolute = this.isAbsolute;
1394 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1397 p.lineTo = function (x, y) {
1398 var d = this.isAbsolute?"l":"r";
1399 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1400 this[0].path = this.Path += d;
1401 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1402 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1403 this.last.isAbsolute = this.isAbsolute;
1404 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1407 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1408 // for more information of where this math came from visit:
1409 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1410 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1411 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1412 var x1 = this.last.x,
1416 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1417 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1418 cx = k * rx * y / ry + (x1 + x2) / 2,
1419 cy = k * -ry * x / rx + (y1 + y2) / 2,
1420 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1421 left = Math.round(cx - rx),
1422 top = Math.round(cy - ry);
1423 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(", ");
1424 this.node.path = this.Path += d;
1425 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2);
1426 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2);
1427 this.last.isAbsolute = this.isAbsolute;
1428 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1431 p.cplineTo = function (x1, y1, w1) {
1433 return this.lineTo(x1, y1);
1435 var x = Math.round(Math.round(parseFloat(x1) * 100) / 100),
1436 y = Math.round(Math.round(parseFloat(y1) * 100) / 100),
1437 w = Math.round(Math.round(parseFloat(w1) * 100) / 100),
1438 d = this.isAbsolute ? "c" : "v",
1439 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1440 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1441 d += attr.join(" ") + " ";
1442 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1443 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1444 this.last.bx = attr[2];
1445 this.last.by = attr[3];
1446 this.node.path = this.Path += d;
1447 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1451 p.curveTo = function () {
1452 var d = this.isAbsolute ? "c" : "v";
1453 if (arguments.length == 6) {
1454 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1455 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1456 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4]);
1457 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5]);
1458 d += [Math.round(parseFloat(arguments[0])),
1459 Math.round(parseFloat(arguments[1])),
1460 Math.round(parseFloat(arguments[2])),
1461 Math.round(parseFloat(arguments[3])),
1462 Math.round(parseFloat(arguments[4])),
1463 Math.round(parseFloat(arguments[5]))].join(" ") + " ";
1464 this.last.isAbsolute = this.isAbsolute;
1465 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1467 if (arguments.length == 4) {
1468 var bx = this.last.x * 2 - this.last.bx;
1469 var by = this.last.y * 2 - this.last.by;
1470 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1471 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1472 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1473 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1474 d += [Math.round(bx), Math.round(by),
1475 Math.round(parseFloat(arguments[0])),
1476 Math.round(parseFloat(arguments[1])),
1477 Math.round(parseFloat(arguments[2])),
1478 Math.round(parseFloat(arguments[3]))].join(" ") + " ";
1479 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1481 this.node.path = this.Path += d;
1484 p.qcurveTo = function () {
1486 if (arguments.length == 4) {
1487 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1488 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1489 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1490 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1491 d += [Math.round(this.last.qx),
1492 Math.round(this.last.qy),
1493 Math.round(this.last.x),
1494 Math.round(this.last.y)].join(" ") + " ";
1495 this.last.isAbsolute = this.isAbsolute;
1496 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1498 if (arguments.length == 2) {
1499 this.last.qx = this.last.x * 2 - this.last.qx;
1500 this.last.qy = this.last.y * 2 - this.last.qy;
1501 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1502 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1503 d += [Math.round(this.last.qx),
1504 Math.round(this.last.qy),
1505 Math.round(this.last.x),
1506 Math.round(this.last.y)].join(" ") + " ";
1507 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1509 this.node.path = this.Path += d;
1510 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1513 p.addRoundedCorner = function (r, dir) {
1514 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1517 rollback = function () {
1521 rollback = function () {};
1527 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1530 o.curveTo(-R, 0, -r, r - R, -r, r);
1537 o.curveTo(R, 0, r, -(r - R), r, -r);
1540 o.curveTo(R, 0, r, r - R, r, r);
1547 o.curveTo(0, -R, -(R - r), -r, r, -r);
1550 o.curveTo(0, -R, R - r, -r, -r, -r);
1557 o.curveTo(0, R, -(R - r), r, r, r);
1560 o.curveTo(0, R, R - r, r, -r, r);
1565 actions[dir.charAt(0)]()[dir.charAt(1)]();
1569 p.andClose = function () {
1570 this.node.path = (this.Path += "x");
1571 this.attrs.path += "z";
1577 paper.pathfinder(p, "" + pathString);
1580 params.fill = params.fill || "none";
1581 params.stroke = params.stroke || "#000";
1583 params = {fill: "none", stroke: "#000"};
1585 setFillAndStroke(p, params);
1586 if (params.gradient) {
1587 addGrdientFill(p, params.gradient);
1589 VML.canvas.appendChild(g);
1592 var setFillAndStroke = function (o, params) {
1596 o.attrs = o.attrs || {};
1597 for (var par in params) {
1598 o.attrs[par] = params[par];
1600 params.href && (node.href = params.href);
1601 params.title && (node.title = params.title);
1602 params.target && (node.target = params.target);
1603 if (params.path && o.type == "path") {
1606 paper.pathfinder(o, params.path);
1608 if (params.rotation != null) {
1609 o.rotate(params.rotation, true);
1611 if (params.translation) {
1612 var xy = (params.translation + "").split(separator);
1613 o.translate(xy[0], xy[1]);
1616 var xy = (params.scale + "").split(separator);
1617 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1619 if (o.type == "image" && params.src) {
1620 node.src = params.src;
1622 if (o.type == "image" && params.opacity) {
1623 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1624 node.style.filter = (node.filterMatrix || "") + (node.filterOpacity || "");
1626 params.font && (s.font = params.font);
1627 params["font-family"] && (s.fontFamily = params["font-family"]);
1628 params["font-size"] && (s.fontSize = params["font-size"]);
1629 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1630 params["font-style"] && (s.fontStyle = params["font-style"]);
1631 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"]) {
1632 o = o.shape || node;
1633 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || createNode("fill");
1634 if ("fill-opacity" in params || "opacity" in params) {
1635 fill.opacity = ((+params["fill-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1637 params.fill && (fill.on = true);
1638 if (typeof fill.on == "undefined" || params.fill == "none") {
1641 if (fill.on && params.fill) {
1642 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1644 fill.src = isURL[1];
1647 fill.color = R.getRGB(params.fill).hex;
1649 fill.type = "solid";
1652 o.appendChild(fill);
1653 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || createNode("stroke");
1654 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"]) {
1657 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1660 if (stroke.on && params.stroke) {
1661 stroke.color = R.getRGB(params.stroke).hex;
1663 stroke.opacity = ((+params["stroke-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1664 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1665 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1666 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1667 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"]) || 1) * 12 / 16);
1668 if (params["stroke-dasharray"]) {
1672 "-.": "shortdashdot",
1673 "-..": "shortdashdotdot",
1678 "--.": "longdashdot",
1679 "--..": "longdashdotdot"
1681 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1683 o.appendChild(stroke);
1685 if (res.type == "text") {
1686 var span = doc.createElement("span"),
1692 s.display = "inline";
1693 a.font && (s.font = a.font);
1694 a["font-family"] && (s.fontFamily = a["font-family"]);
1695 a["font-size"] && (s.fontSize = a["font-size"]);
1696 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1697 a["font-style"] && (s.fontStyle = a["font-style"]);
1698 res.node.parentNode.appendChild(span);
1699 span.innerText = res.node.string;
1700 res.W = a.w = span.offsetWidth;
1701 res.H = a.h = span.offsetHeight;
1703 res.Y = a.y + Math.round(res.H / 2);
1704 res.node.parentNode.removeChild(span);
1706 // text-anchor emulation
1707 switch (a["text-anchor"]) {
1709 res.node.style["v-text-align"] = "left";
1710 res.bbx = Math.round(res.W / 2);
1713 res.node.style["v-text-align"] = "right";
1714 res.bbx = -Math.round(res.W / 2);
1717 res.node.style["v-text-align"] = "center";
1722 var getAngle = function (a, b, c, d) {
1723 var angle = Math.round(Math.atan((parseFloat(c) - parseFloat(a)) / (parseFloat(d) - parseFloat(b))) * 57.29) || 0;
1724 if (!angle && parseFloat(a) < parseFloat(b)) {
1733 var addGrdientFill = function (o, gradient) {
1734 gradient = toGradient(gradient);
1735 o.attrs = o.attrs || {};
1736 var attrs = o.attrs;
1737 o.attrs.gradient = gradient;
1738 o = o.shape || o[0];
1739 var fill = o.getElementsByTagName("fill");
1743 fill = createNode("fill");
1745 if (gradient.dots.length) {
1747 fill.method = "none";
1748 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1749 if (typeof gradient.dots[0].color != "undefined") {
1750 fill.color = R.getRGB(gradient.dots[0].color).hex;
1752 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1753 fill.color2 = R.getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1756 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1757 if (gradient.dots[i].offset) {
1758 colors.push(gradient.dots[i].offset + " " + R.getRGB(gradient.dots[i].color).hex);
1761 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;
1762 if (colors.length) {
1763 fill.colors.value = colors.join(",");
1764 fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1766 fill.colors.value = "0% " + fill.color;
1768 fill.opacity = fillOpacity;
1769 if (typeof gradient.angle != "undefined") {
1770 fill.angle = (-gradient.angle + 270) % 360;
1771 } else if (gradient.vector) {
1772 fill.angle = getAngle.apply(null, gradient.vector);
1774 if ((gradient.type + "").toLowerCase() == "radial") {
1775 fill.focus = "100%";
1776 fill.focusposition = "0.5 0.5";
1780 var Element = function (node, group, vml) {
1800 Element.prototype.rotate = function (deg, cx, cy) {
1802 return this._.rt.deg;
1804 deg = deg.toString().split(separator);
1805 if (deg.length - 1) {
1806 cx = parseFloat(deg[1]);
1807 cy = parseFloat(deg[2]);
1809 deg = parseFloat(deg[0]);
1811 this._.rt.deg = deg;
1813 this._.rt.deg += deg;
1820 this.setBox(null, cx, cy);
1821 this.Group.style.rotation = this._.rt.deg;
1824 Element.prototype.setBox = function (params, cx, cy) {
1825 var gs = this.Group.style,
1826 os = (this.shape && this.shape.style) || this.node.style;
1827 params = params || {};
1828 for (var i in params) {
1829 this.attrs[i] = params[i];
1831 cx = cx || this._.rt.cx;
1832 cy = cy || this._.rt.cy;
1833 var attr = this.attrs, x, y, w, h;
1834 switch (this.type) {
1836 x = attr.cx - attr.r;
1837 y = attr.cy - attr.r;
1841 x = attr.cx - attr.rx;
1842 y = attr.cy - attr.ry;
1850 w = attr.width || 0;
1851 h = attr.height || 0;
1854 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("");
1855 x = attr.x - Math.round(this.W / 2);
1856 y = attr.y - this.H / 2;
1861 if (!this.attrs.path) {
1865 h = this.vml.height;
1867 var dim = pathDimensions(this.attrs.path),
1878 h = this.vml.height;
1881 cx = (cx == null) ? x + w / 2 : cx;
1882 cy = (cy == null) ? y + h / 2 : cy;
1883 var left = cx - this.vml.width / 2,
1884 top = cy - this.vml.height / 2;
1885 if (this.type == "path" || this.type == "text") {
1886 (gs.left != left + "px") && (gs.left = left + "px");
1887 (gs.top != top + "px") && (gs.top = top + "px");
1888 this.X = this.type == "text" ? x : -left;
1889 this.Y = this.type == "text" ? y : -top;
1892 (os.left != -left + "px") && (os.left = -left + "px");
1893 (os.top != -top + "px") && (os.top = -top + "px");
1895 (gs.left != left + "px") && (gs.left = left + "px");
1896 (gs.top != top + "px") && (gs.top = top + "px");
1901 (gs.width != this.vml.width + "px") && (gs.width = this.vml.width + "px");
1902 (gs.height != this.vml.height + "px") && (gs.height = this.vml.height + "px");
1903 (os.left != x - left + "px") && (os.left = x - left + "px");
1904 (os.top != y - top + "px") && (os.top = y - top + "px");
1905 (os.width != w + "px") && (os.width = w + "px");
1906 (os.height != h + "px") && (os.height = h + "px");
1907 var arcsize = (+params.r || 0) / (Math.min(w, h));
1908 if (this.type == "rect" && this.arcsize != arcsize && (arcsize || this.arcsize)) {
1909 // We should replace element with the new one
1910 var o = createNode("roundrect");
1911 o.arcsize = arcsize;
1912 this.Group.appendChild(o);
1913 this.node.parentNode.removeChild(this.node);
1915 this.arcsize = arcsize;
1916 setFillAndStroke(this, this.attrs);
1917 this.setBox(this.attrs);
1921 Element.prototype.hide = function () {
1922 this.Group.style.display = "none";
1925 Element.prototype.show = function () {
1926 this.Group.style.display = "block";
1929 Element.prototype.getBBox = function () {
1930 if (this.type == "path") {
1931 return pathDimensions(this.attr("path"));
1934 x: this.X + (this.bbx || 0),
1940 Element.prototype.remove = function () {
1941 this[0].parentNode.removeChild(this[0]);
1942 this.Group.parentNode.removeChild(this.Group);
1943 this.shape && this.shape.parentNode.removeChild(this.shape);
1945 Element.prototype.attr = function () {
1946 if (arguments.length == 1 && typeof arguments[0] == "string") {
1947 if (arguments[0] == "translation") {
1948 return this.translate();
1950 return this.attrs[arguments[0]];
1952 if (this.attrs && arguments.length == 1 && arguments[0] instanceof win.Array) {
1954 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1955 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1960 if (arguments.length == 2) {
1962 params[arguments[0]] = arguments[1];
1964 if (arguments.length == 1 && typeof arguments[0] == "object") {
1965 params = arguments[0];
1968 if (params.gradient) {
1969 addGrdientFill(this, params.gradient);
1971 if (params.text && this.type == "text") {
1972 this.node.string = params.text;
1975 this.node.id = params.id;
1977 setFillAndStroke(this, params);
1978 this.setBox(params);
1982 Element.prototype.toFront = function () {
1983 this.Group.parentNode.appendChild(this.Group);
1986 Element.prototype.toBack = function () {
1987 if (this.Group.parentNode.firstChild != this.Group) {
1988 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1992 Element.prototype.insertAfter = function (element) {
1993 if (element.Group.nextSibling) {
1994 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1996 element.Group.parentNode.appendChild(this.Group);
2000 Element.prototype.insertBefore = function (element) {
2001 element.Group.parentNode.insertBefore(this.Group, element.Group);
2004 var theCircle = function (vml, x, y, r) {
2005 var g = createNode("group");
2006 var o = createNode("oval");
2008 var res = new Element(o, g, vml);
2009 res.type = "circle";
2010 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2014 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2015 vml.canvas.appendChild(g);
2018 var theRect = function (vml, x, y, w, h, r) {
2019 var g = createNode("group"),
2020 o = createNode("roundrect"),
2021 arcsize = (+r || 0) / (Math.min(w, h));
2022 o.arcsize = arcsize;
2024 var res = new Element(o, g, vml);
2026 setFillAndStroke(res, {stroke: "#000"});
2032 res.arcsize = arcsize;
2033 res.setBox({x: x, y: y, width: w, height: h});
2034 vml.canvas.appendChild(g);
2037 var theEllipse = function (vml, x, y, rx, ry) {
2038 var g = createNode("group");
2039 var o = createNode("oval");
2041 var res = new Element(o, g, vml);
2042 res.type = "ellipse";
2043 setFillAndStroke(res, {stroke: "#000"});
2048 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2049 vml.canvas.appendChild(g);
2052 var theImage = function (vml, src, x, y, w, h) {
2053 var g = createNode("group");
2054 var o = createNode("image");
2057 var res = new Element(o, g, vml);
2063 res.setBox({x: x, y: y, width: w, height: h});
2064 vml.canvas.appendChild(g);
2067 var theText = function (vml, x, y, text) {
2068 var g = createNode("group"), gs = g.style;
2069 var el = createNode("shape"), ol = el.style;
2070 var path = createNode("path"), ps = path.style;
2071 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
2072 path.textpathok = true;
2073 ol.width = vml.width;
2074 ol.height = vml.height;
2075 gs.position = "absolute";
2078 gs.width = vml.width;
2079 gs.height = vml.height;
2080 var o = createNode("textpath");
2083 o.coordsize = vml.coordsize;
2084 o.coordorigin = vml.coordorigin;
2086 el.appendChild(path);
2088 var res = new Element(o, g, vml);
2090 res.textpath = path;
2096 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2098 vml.canvas.appendChild(g);
2101 var setSize = function (width, height) {
2102 this.width = width || this.width;
2103 this.height = height || this.height;
2104 this.canvas.style.width = this.width + "px";
2105 this.canvas.style.height = this.height + "px";
2106 this.canvas.parentNode.style.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2107 this.canvas.coordsize = this.width + " " + this.height;
2110 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2112 if (!doc.namespaces.rvml) {
2113 doc.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
2115 var createNode = function (tagName) {
2116 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2119 var createNode = function (tagName) {
2120 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2123 var create = function () {
2124 var con = getContainer.apply(null, arguments);
2125 var container = con.container,
2129 height = con.height;
2131 throw new Error("VML container not found.");
2133 var c = doc.createElement("div"),
2134 d = doc.createElement("div"),
2135 r = paper.canvas = createNode("group"),
2136 cs = c.style, rs = r.style;
2137 paper.width = width;
2138 paper.height = height;
2139 width = width || "320px";
2140 height = height || "200px";
2141 cs.clip = "rect(0 " + width + "px " + height + "px 0)";
2144 cs.position = "absolute";
2145 rs.position = "absolute";
2146 d.style.position = "relative";
2149 r.coordsize = (/%$/.test(width) ? width : parseFloat(width)) + " " + (/%$/.test(height) ? height : parseFloat(height));
2150 r.coordorigin = "0 0";
2152 var b = createNode("rect"), bs = b.style;
2153 bs.left = bs.top = 0;
2154 bs.width = rs.width;
2155 bs.height = rs.height;
2156 b.filled = b.stroked = "f";
2161 if (container == 1) {
2162 doc.body.appendChild(d);
2163 cs.position = "absolute";
2175 cs.width = container.style.width = width;
2176 cs.height = container.style.height = height;
2177 if (container.firstChild) {
2178 container.insertBefore(d, container.firstChild);
2180 container.appendChild(d);
2183 for (var prop in paper) {
2184 container[prop] = paper[prop];
2186 plugins(container, R.fn);
2187 container.clear = function () {
2189 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2190 if (r.childNodes[i] != b) {
2191 todel.push(r.childNodes[i]);
2194 for (i = 0, ii = todel.length; i < ii; i++) {
2195 r.removeChild(todel[i]);
2200 paper.remove = function () {
2201 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2203 paper.safari = function () {};
2209 var addEvent = (function () {
2210 if (doc.addEventListener) {
2211 return function (obj, type, fn, element) {
2212 var f = function (e) {
2213 return fn.call(element, e);
2215 obj.addEventListener(type, f, false);
2216 return function () {
2217 obj.removeEventListener(type, f, false);
2221 } else if (doc.attachEvent) {
2222 return function (obj, type, fn, element) {
2223 var f = function (e) {
2224 return fn.call(element, e || win.event);
2226 obj.attachEvent("on" + type, f);
2227 var detacher = function () {
2228 obj.detachEvent("on" + type, f);
2231 if (type == "mouseover") {
2232 obj.attachEvent("onmouseenter", f);
2233 return function () {
2234 obj.detachEvent("onmouseenter", f);
2237 } else if (type == "mouseout") {
2238 obj.attachEvent("onmouseleave", f);
2239 return function () {
2240 obj.detachEvent("onmouseleave", f);
2248 for (var i = events.length; i--;) {
2249 (function (eventName) {
2250 Element.prototype[eventName] = function (fn) {
2251 if (typeof fn == "function") {
2252 this.events = this.events || {};
2253 this.events[eventName] = this.events[eventName] || {};
2254 this.events[eventName][fn] = this.events[eventName][fn] || [];
2255 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2259 Element.prototype["un" + eventName] = function (fn) {
2261 this.events[eventName] &&
2262 this.events[eventName][fn] &&
2263 this.events[eventName][fn].length &&
2264 this.events[eventName][fn].shift()() &&
2265 !this.events[eventName][fn].length &&
2266 delete this.events[eventName][fn];
2271 paper.circle = function (x, y, r) {
2272 return theCircle(this, x, y, r);
2274 paper.rect = function (x, y, w, h, r) {
2275 return theRect(this, x, y, w, h, r);
2277 paper.ellipse = function (x, y, rx, ry) {
2278 return theEllipse(this, x, y, rx, ry);
2280 paper.path = function (params, pathString) {
2281 return thePath(params, pathString, this);
2283 paper.image = function (src, x, y, w, h) {
2284 return theImage(this, src, x, y, w, h);
2286 paper.text = function (x, y, text) {
2287 return theText(this, x, y, text);
2289 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2290 color = color || "#000";
2291 var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
2293 columnWidth = w / wv;
2294 for (var i = 1; i < hv; i++) {
2295 path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
2297 for (var i = 1; i < wv; i++) {
2298 path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
2300 return this.path({stroke: color, "stroke-width": 1}, path.join(","));
2302 paper.pathfinder = function (p, path) {
2304 M: function (x, y) {
2307 C: function (x1, y1, x2, y2, x3, y3) {
2308 this.curveTo(x1, y1, x2, y2, x3, y3);
2310 Q: function (x1, y1, x2, y2) {
2311 this.qcurveTo(x1, y1, x2, y2);
2313 T: function (x, y) {
2314 this.qcurveTo(x, y);
2316 S: function (x1, y1, x2, y2) {
2317 p.curveTo(x1, y1, x2, y2);
2319 L: function (x, y) {
2323 this.lineTo(x, this.last.y);
2326 this.lineTo(this.last.x, y);
2328 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2329 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2336 path = pathToAbsolute(path);
2337 for (var i = 0, ii = path.length; i < ii; i++) {
2338 var b = path[i].shift();
2339 commands[b].apply(p, path[i]);
2343 paper.set = function (itemsArray) {
2344 return new Set(itemsArray);
2346 paper.setSize = setSize;
2347 Element.prototype.stop = function () {
2348 clearTimeout(this.animation_in_progress);
2350 Element.prototype.scale = function (x, y, cx, cy) {
2351 if (x == null && y == null) {
2352 return {x: this._.sx, y: this._.sy, toString: function () { return this.x.toFixed(3) + " " + this.y.toFixed(3); }};
2356 var dx, dy, dcx, dcy, a = this.attrs;
2358 var bb = this.type == "path" ? pathDimensions(a.path) : this.getBBox(),
2359 rcx = bb.x + bb.width / 2,
2360 rcy = bb.y + bb.height / 2;
2361 cx = (+cx || cx == 0) ? cx : rcx;
2362 cy = (+cy || cy == 0) ? cy : rcy;
2363 var dirx = Math.round(x / Math.abs(x)),
2364 diry = Math.round(y / Math.abs(y)),
2365 s = this.node.style,
2366 ncx = cx + (rcx - cx) * x * dirx / this._.sx,
2367 ncy = cy + (rcy - cy) * y * diry / this._.sy;
2368 switch (this.type) {
2371 var neww = a.width * x * dirx / this._.sx,
2372 newh = a.height * y * diry / this._.sy,
2373 newx = ncx - neww / 2,
2374 newy = ncy - newh / 2;
2385 rx: a.rx * x / this._.sx,
2386 ry: a.ry * y / this._.sy,
2387 r: a.r * x / this._.sx,
2393 var path = pathToRelative(a.path),
2395 for (var i = 0, ii = path.length; i < ii; i++) {
2397 if (p[0].toUpperCase() == "M" && skip) {
2402 if (this.svg && p[0].toUpperCase() == "A") {
2403 p[path[i].length - 2] *= x;
2404 p[path[i].length - 1] *= y;
2408 for (var j = 1, jj = p.length; j < jj; j++) {
2409 p[j] *= (j % 2) ? x / this._.sx : y / this._.sy;
2413 var dim2 = pathDimensions(path),
2414 dx = ncx - dim2.x - dim2.width / 2,
2415 dy = ncy - dim2.y - dim2.height / 2;
2416 path = pathToRelative(path);
2420 this.attr({path: path.join(" ")});
2423 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2424 if (this.transformations) {
2425 this.transformations[2] = "scale(".concat(dirx, ",", diry, ")");
2426 this.node.setAttribute("transform", this.transformations.join(" "));
2427 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2428 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2429 this.attr({x: dx, y: dy});
2433 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=".concat(dirx,
2434 ", M12=0, M21=0, M22=", diry,
2435 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2436 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2439 if (this.transformations) {
2440 this.transformations[2] = "";
2441 this.node.setAttribute("transform", this.transformations.join(" "));
2445 this.node.filterMatrix = "";
2446 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2454 Element.prototype.animate = function (params, ms, callback) {
2455 clearTimeout(this.animation_in_progress);
2460 for (var attr in params) {
2461 if (attr in availableAnimAttrs) {
2462 from[attr] = this.attr(attr);
2463 if (typeof from[attr] == "undefined") {
2464 from[attr] = availableAttrs[attr];
2466 to[attr] = params[attr];
2467 switch (availableAnimAttrs[attr]) {
2469 diff[attr] = (to[attr] - from[attr]) / ms;
2472 from[attr] = R.getRGB(from[attr]);
2473 var toColour = R.getRGB(to[attr]);
2475 r: (toColour.r - from[attr].r) / ms,
2476 g: (toColour.g - from[attr].g) / ms,
2477 b: (toColour.b - from[attr].b) / ms
2481 var pathes = pathEqualiser(from[attr], to[attr]);
2482 from[attr] = pathes[0];
2483 to[attr] = pathes[1];
2485 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2486 diff[attr][i] = [0];
2487 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2488 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2493 var values = params[attr].toString().split(separator),
2494 from2 = from[attr].toString().split(separator);
2495 if (attr == "translation") {
2496 from[attr] = [0, 0];
2497 diff[attr] = [values[0] / ms, values[1] / ms];
2498 } else if (attr == "rotation") {
2499 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2500 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2502 from[attr] = (from[attr] + "").split(separator);
2503 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2509 var start = +new Date,
2513 var time = +new Date - start,
2517 for (var attr in from) {
2518 switch (availableAnimAttrs[attr]) {
2520 now = +from[attr] + time * diff[attr];
2524 Math.round(from[attr].r + time * diff[attr].r),
2525 Math.round(from[attr].g + time * diff[attr].g),
2526 Math.round(from[attr].b + time * diff[attr].b)
2531 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2532 now[i] = [from[attr][i][0]];
2533 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2534 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2536 now[i] = now[i].join(" ");
2538 now = now.join(" ");
2541 if (attr == "translation") {
2542 var x = diff[attr][0] * (time - prev),
2543 y = diff[attr][1] * (time - prev);
2546 now = [x, y].join(" ");
2547 } else if (attr == "rotation") {
2548 now = +from[attr][0] + time * diff[attr][0];
2549 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2551 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2555 if (attr == "font-size") {
2556 set[attr] = now + "px";
2562 that.animation_in_progress = setTimeout(arguments.callee, 0);
2565 (t.x || t.y) && that.translate(-t.x, -t.y);
2567 clearTimeout(that.animation_in_progress);
2569 (typeof callback == "function") && callback.call(that);
2575 Element.prototype.translate = function (x, y) {
2577 return {x: this._.tx, y: this._.ty};
2581 switch (this.type) {
2584 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
2589 this.attr({x: this.attrs.x + (+x), y: this.attrs.y + (+y)});
2592 var path = pathToRelative(this.attrs.path);
2595 this.attr({path: path.join(" ")});
2602 var Set = function (itemsArray) {
2604 this.length = (itemsArray && itemsArray.length) || 0;
2605 if (itemsArray && itemsArray.constructor == Array) {
2606 for (var i = itemsArray.length; i--;) {
2607 if (itemsArray[i].constructor == Element) {
2608 this.items[this.items.length] = itemsArray[i];
2613 Set.prototype.push = function (item) {
2614 if (item && item.constructor == Element) {
2615 var len = this.items.length;
2616 this.items[len] = item;
2622 Set.prototype.pull = function (id) {
2623 var res = this.items.splice(id, 1)[0];
2624 for (var j = id, jj = this.items.length; j < jj; j++) {
2625 this[j] = this[j + 1];
2627 delete this[jj + 1];
2631 for (var method in Element.prototype) {
2632 Set.prototype[method] = (function (methodname) {
2633 return function () {
2634 for (var i = this.items.length; i--;) {
2635 this.items[i][methodname].apply(this.items[i], arguments);
2641 Set.prototype.getBBox = function () {
2642 var x = [], y = [], w = [], h = [];
2643 for (var i = this.items.length; i--;) {
2644 var box = this.items[i].getBBox();
2647 w.push(box.x + box.width);
2648 h.push(box.y + box.height);
2650 x = Math.min.apply(Math, x);
2651 y = Math.min.apply(Math, y);
2655 width: Math.max.apply(Math, w) - x,
2656 height: Math.max.apply(Math, h) - y
2660 R.registerFont = function (font) {
2664 this.fonts = this.fonts || {};
2665 if (this.fonts[font.face["font-family"]]) {
2666 this.fonts[font.face["font-family"]].push(font);
2668 this.fonts[font.face["font-family"]] = [font];
2671 font.face["units-per-em"] = parseInt(font.face["units-per-em"], 10);
2673 for (var glyph in font.glyphs) {
2674 var path = font.glyphs[glyph];
2676 path.d = "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
2677 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2683 paper.getFont = function (family, weight, style, stretch) {
2684 stretch = stretch || "normal";
2685 style = style || "normal";
2686 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2687 var font = R.fonts[family];
2689 var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, "") + "(\\s|$)", "i");
2690 for (var fontName in R.fonts) {
2691 if (name.test(fontName)) {
2692 font = R.fonts[fontName];
2699 for (var i = 0, ii = font.length; i < ii; i++) {
2701 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2708 paper.print = function (x, y, string, font, size) {
2709 var out = this.set(),
2710 letters = (string + "").split(""),
2715 scale = (size || 16) / font.face["units-per-em"];
2716 for (var i = 0, ii = letters.length; i < ii; i++) {
2717 var prev = i && font.glyphs[letters[i - 1]] || {},
2718 curr = font.glyphs[letters[i]];
2719 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
2720 curr && curr.d && out.push(this.path({fill: "#000", stroke: "none"}, curr.d).translate(shift, 0));
2722 out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
2727 R.ninja = function () {
2728 var r = window.Raphael;
2729 delete window.Raphael;
2730 if (oldRaphael.was) {
2731 window.Raphael = oldRaphael.is;
2735 R.el = Element.prototype;