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 return pathcache[pathString];
218 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
220 toString = function () {
222 for (var i = 0, ii = this.length; i < ii; i++) {
223 res += this[i][0] + this[i].join(",").substring(2);
227 if (pathString.toString.toString() == toString.toString()) {
231 pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
232 var params = [], name = b.toLowerCase();
233 c.replace(/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
234 b && params.push(+b);
236 while (params.length >= paramCounts[name]) {
237 data.push([b].concat(params.splice(0, paramCounts[name])));
238 if (!paramCounts[name]) {
243 data.toString = toString;
245 if (pathcount.length > 20) {
246 delete pathcache[pathcount.unshift()];
248 pathcount.push(pathString);
249 pathcache[pathString] = data;
252 var pathDimensions = function (path) {
253 var pathArray = path;
254 if (typeof path == "string") {
255 pathArray = R.parsePathString(path);
257 pathArray = pathToAbsolute(pathArray);
258 var x = [], y = [], length = 0;
259 for (var i = 0, ii = pathArray.length; i < ii; i++) {
260 var pa = pathArray[i];
265 x.push(pa[pa.length - 2]);
266 y.push(pa[pa.length - 1]);
269 for (var j = 1, jj = pa.length; j < jj; j++) {
270 (j % 2 ? x : y).push(pa[j]);
274 var minx = Math.min.apply(Math, x),
275 miny = Math.min.apply(Math, y);
279 width: Math.max.apply(Math, x) - minx,
280 height: Math.max.apply(Math, y) - miny,
285 pathToRelative = function (pathArray) {
287 if (typeof pathArray == "string") {
288 pathArray = R.parsePathString(pathArray);
290 var x = 0, y = 0, start = 0;
291 if (pathArray[0][0] == "M") {
295 res.push(["M", x, y]);
297 for (var i = start, ii = pathArray.length; i < ii; i++) {
300 if (pa[0] != pa[0].toLowerCase()) {
301 r[0] = pa[0].toLowerCase();
309 r[6] = +(pa[6] - x).toFixed(3);
310 r[7] = +(pa[7] - y).toFixed(3);
313 r[1] = (pa[1] - y).toFixed(3);
316 for (var j = 1, jj = pa.length; j < jj; j++) {
317 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
322 for (var k = 0, kk = pa.length; k < kk; k++) {
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);
379 for (var j = 1, jj = pa.length; j < jj; j++) {
380 r[j] = +pa[j] + ((j % 2) ? x : y);
385 for (var k = 0, kk = pa.length; k < kk; k++) {
399 x = res[i][res[i].length - 2];
400 y = res[i][res[i].length - 1];
403 res.toString = pathArray.toString;
406 pecache = {}, pecount = [],
407 pathEqualiser = function (path1, path2) {
408 if ((path1 + path2) in pecache) {
409 return pecache[path1 + path2];
411 var data = [pathToAbsolute(R.parsePathString(path1)), pathToAbsolute(R.parsePathString(path2))],
412 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}],
413 processPath = function (path, d) {
423 var nx = d.x + (d.x - (d.bx || d.x));
424 var ny = d.y + (d.y - (d.by || d.y));
425 path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
428 var nx = d.x + (d.x - (d.bx || d.x));
429 var ny = d.y + (d.y - (d.by || d.y));
430 path = ["Q", nx, ny, path[1], path[2]];
433 path = ["L", path[1], d.y];
436 path = ["L", d.x, path[1]];
439 path = ["L", d.X, d.Y];
444 edgeCases = function (a, b, i) {
445 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
446 data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
447 attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
448 attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
449 attrs[a].x = data[a][i][data[a][i].length - 2];
450 attrs[a].y = data[a][i][data[a][i].length - 1];
452 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
453 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]];
454 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
455 data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
456 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
457 var x = data[b][i][data[b][i].length - 2];
458 var y = data[b][i][data[b][i].length - 1];
459 data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
460 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
462 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
463 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
464 attrs[b].x = data[b][i][data[b][i].length - 2];
465 attrs[b].y = data[b][i][data[b][i].length - 1];
467 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
468 var x = data[b][i][data[b][i].length - 2];
469 var y = data[b][i][data[b][i].length - 1];
470 data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
471 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
473 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
474 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
475 attrs[b].x = data[b][i][data[b][i].length - 2];
476 attrs[b].y = data[b][i][data[b][i].length - 1];
478 } else if (data[a][i][0] == "U") {
479 data[a][i][0] = data[b][i][0];
480 for (var j = 1, jj = data[b][i].length; j < jj; j++) {
481 data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
486 for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
487 data[0][i] = processPath(data[0][i], attrs[0]);
488 data[1][i] = processPath(data[1][i], attrs[1]);
489 if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
492 attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
493 attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
494 attrs[0].x = data[0][i][data[0][i].length - 2];
495 attrs[0].y = data[0][i][data[0][i].length - 1];
496 attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
497 attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
498 attrs[1].x = data[1][i][data[1][i].length - 2];
499 attrs[1].y = data[1][i][data[1][i].length - 1];
501 if (pecount.length > 20) {
502 delete pecache[pecount.unshift()];
504 pecount.push(path1 + path2);
505 pecache[path1 + path2] = data;
508 toGradient = function (gradient) {
509 if (typeof gradient == "string") {
510 gradient = gradient.split(/\s*\-\s*/);
511 var angle = gradient.shift();
512 if (angle.toLowerCase() == "v") {
514 } else if (angle.toLowerCase() == "h") {
517 angle = parseFloat(angle);
520 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)]};
521 var max = 1 / (Math.max(Math.abs(grobj.vector[2]), Math.abs(grobj.vector[3])) || 1);
522 grobj.vector[2] *= max;
523 grobj.vector[3] *= max;
524 if (grobj.vector[2] < 0) {
525 grobj.vector[0] = -grobj.vector[2];
528 if (grobj.vector[3] < 0) {
529 grobj.vector[1] = -grobj.vector[3];
532 grobj.vector[0] = grobj.vector[0].toFixed(3);
533 grobj.vector[1] = grobj.vector[1].toFixed(3);
534 grobj.vector[2] = grobj.vector[2].toFixed(3);
535 grobj.vector[3] = grobj.vector[3].toFixed(3);
536 for (var i = 0, ii = gradient.length; i < ii; i++) {
538 var par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
539 dot.color = R.getRGB(par[1]).hex;
540 par[2] && (dot.offset = par[2] + "%");
541 grobj.dots.push(dot);
543 for (var i = 1, ii = grobj.dots.length - 1; i < ii; i++) {
544 if (!grobj.dots[i].offset) {
545 var start = parseFloat(grobj.dots[i - 1].offset || 0),
547 for (var j = i + 1; j < ii; j++) {
548 if (grobj.dots[j].offset) {
549 end = grobj.dots[j].offset;
557 end = parseFloat(end);
558 var d = (end - start) / (j - i + 1);
561 grobj.dots[i].offset = start + "%";
570 getContainer = function () {
571 var container, x, y, width, height;
572 if (typeof arguments[0] == "string" || typeof arguments[0] == "object") {
573 if (typeof arguments[0] == "string") {
574 container = doc.getElementById(arguments[0]);
576 container = arguments[0];
578 if (container.tagName) {
579 if (arguments[1] == null) {
581 container: container,
582 width: container.style.pixelWidth || container.offsetWidth,
583 height: container.style.pixelHeight || container.offsetHeight
586 return {container: container, width: arguments[1], height: arguments[2]};
589 } else if (typeof arguments[0] == "number" && arguments.length > 3) {
590 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
593 plugins = function (con, add) {
594 for (var prop in add) if (add.hasOwnProperty(prop) && !(prop in con)) {
595 switch (typeof add[prop]) {
597 con[prop] = con === this ? add[prop] : function () { return add[prop].apply(this, arguments); };
601 plugins.call(this, con[prop], add[prop]);
604 con[prop] = add[prop];
612 R.toString = function () {
613 return "Your browser supports SVG.\nYou are running Rapha\u00ebl " + this.version;
615 var thePath = function (params, pathString, SVG) {
616 var el = doc.createElementNS(SVG.svgns, "path");
618 SVG.canvas.appendChild(el);
620 var p = new Element(el, SVG);
623 p.last = {x: 0, y: 0, bx: 0, by: 0};
624 p.absolutely = function () {
625 this.isAbsolute = true;
628 p.relatively = function () {
629 this.isAbsolute = false;
632 p.moveTo = function (x, y) {
633 var d = this.isAbsolute?"M":"m";
634 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
635 var oldD = this[0].getAttribute("d") || "";
636 (oldD == "M0,0") && (oldD = "");
637 this[0].setAttribute("d", oldD + d);
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 this.attrs.path = oldD + d;
643 p.lineTo = function (x, y) {
644 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
645 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
646 var d = this.isAbsolute?"L":"l";
647 d += parseFloat(x).toFixed(3) + " " + parseFloat(y).toFixed(3) + " ";
648 var oldD = this[0].getAttribute("d") || "";
649 this[0].setAttribute("d", oldD + d);
650 this.attrs.path = oldD + d;
653 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
654 var d = this.isAbsolute ? "A" : "a";
655 d += [parseFloat(rx).toFixed(3), parseFloat(ry).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x).toFixed(3), parseFloat(y).toFixed(3)].join(" ");
656 var oldD = this[0].getAttribute("d") || "";
657 this[0].setAttribute("d", oldD + d);
658 this.last.x = parseFloat(x);
659 this.last.y = parseFloat(y);
660 this.attrs.path = oldD + d;
663 p.cplineTo = function (x1, y1, w1) {
665 return this.lineTo(x1, y1);
668 var x = parseFloat(x1);
669 var y = parseFloat(y1);
670 var w = parseFloat(w1);
671 var d = this.isAbsolute?"C":"c";
672 var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
673 for (var i = 0, ii = attr.length; i < ii; i++) {
674 d += attr[i].toFixed(3) + " ";
676 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
677 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
678 this.last.bx = attr[2];
679 this.last.by = attr[3];
680 var oldD = this[0].getAttribute("d") || "";
681 this[0].setAttribute("d", oldD + d);
682 this.attrs.path = oldD + d;
686 p.curveTo = function () {
688 command = [0, 1, 2, 3, "s", 5, "c"];
690 var d = command[arguments.length];
691 if (this.isAbsolute) {
694 for (var i = 0, ii = arguments.length; i < ii; i++) {
695 d += parseFloat(arguments[i]).toFixed(3) + " ";
697 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
698 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
699 this.last.bx = parseFloat(arguments[arguments.length - 4]);
700 this.last.by = parseFloat(arguments[arguments.length - 3]);
701 var oldD = this.node.getAttribute("d") || "";
702 this.node.setAttribute("d", oldD + d);
703 this.attrs.path = oldD + d;
706 p.qcurveTo = function () {
708 command = [0, 1, "t", 3, "q"];
710 var d = command[arguments.length];
711 if (this.isAbsolute) {
714 for (var i = 0, ii = arguments.length; i < ii; i++) {
715 d += parseFloat(arguments[i]).toFixed(3) + " ";
717 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2]);
718 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1]);
719 if (arguments.length != 2) {
720 this.last.qx = parseFloat(arguments[arguments.length - 4]);
721 this.last.qy = parseFloat(arguments[arguments.length - 3]);
723 var oldD = this.node.getAttribute("d") || "";
724 this.node.setAttribute("d", oldD + d);
725 this.attrs.path = oldD + d;
728 p.addRoundedCorner = function (r, dir) {
729 var R = .5522 * r, rollback = this.isAbsolute, o = this;
732 rollback = function () {
736 rollback = function () {};
742 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
745 o.curveTo(-R, 0, -r, r - R, -r, r);
752 o.curveTo(R, 0, r, -(r - R), r, -r);
755 o.curveTo(R, 0, r, r - R, r, r);
762 o.curveTo(0, -R, -(R - r), -r, r, -r);
765 o.curveTo(0, -R, R - r, -r, -r, -r);
772 o.curveTo(0, R, -(R - r), r, r, r);
775 o.curveTo(0, R, R - r, r, -r, r);
780 actions[dir[0]]()[dir[1]]();
784 p.andClose = function () {
785 var oldD = this[0].getAttribute("d") || "";
786 this[0].setAttribute("d", oldD + "Z ");
787 this.attrs.path = oldD + "Z ";
791 p.attrs.path = "" + pathString;
793 paper.pathfinder(p, p.attrs.path);
796 !params.gradient && (params.fill = params.fill || "none");
797 params.stroke = params.stroke || "#000";
799 params = {fill: "none", stroke: "#000"};
801 setFillAndStroke(p, params);
804 var addGradientFill = function (o, gradient, SVG) {
805 gradient = toGradient(gradient);
806 var el = doc.createElementNS(SVG.svgns, (gradient.type || "linear") + "Gradient");
807 el.id = "raphael-gradient-" + R.idGenerator++;
808 if (gradient.vector && gradient.vector.length) {
809 el.setAttribute("x1", gradient.vector[0]);
810 el.setAttribute("y1", gradient.vector[1]);
811 el.setAttribute("x2", gradient.vector[2]);
812 el.setAttribute("y2", gradient.vector[3]);
814 SVG.defs.appendChild(el);
815 var isopacity = true;
816 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
817 var stop = doc.createElementNS(SVG.svgns, "stop");
818 if (gradient.dots[i].offset) {
821 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
822 stop.setAttribute("stop-color", R.getRGB(gradient.dots[i].color).hex || "#fff");
823 // ignoring opacity for internal points, because VML doesn't support it
824 el.appendChild(stop);
826 if (isopacity && typeof gradient.dots[ii - 1].opacity != "undefined") {
827 stop.setAttribute("stop-opacity", gradient.dots[ii - 1].opacity);
829 o.setAttribute("fill", "url(#" + el.id + ")");
832 o.style.fillOpacity = 1;
833 o.setAttribute("opacity", 1);
834 o.setAttribute("fill-opacity", 1);
836 var updatePosition = function (o) {
838 var bbox = o.getBBox();
839 o.pattern.setAttribute("patternTransform", "translate(".concat(bbox.x, ",", bbox.y, ")"));
842 var setFillAndStroke = function (o, params) {
847 "-..": [3, 1, 1, 1, 1, 1],
853 "--..": [8, 3, 1, 3, 1, 3]
857 addDashes = function (o, value) {
858 value = dasharray[value.toString().toLowerCase()];
860 var width = o.attrs["stroke-width"] || "1",
861 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
863 for (var i = 0, ii = value.length; i < ii; i++) {
864 dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
866 value = dashes.join(",");
867 node.setAttribute("stroke-dasharray", value);
870 for (var att in params) {
871 if (!(att in availableAttrs)) {
874 var value = params[att];
881 var pn = node.parentNode;
882 if (pn.tagName.toLowerCase() != "a") {
883 var hl = doc.createElementNS(o.paper.svgns, "a");
884 pn.insertBefore(hl, node);
885 hl.appendChild(node);
888 pn.setAttributeNS(o.paper.xlink, att, value);
891 if (o.type == "path") {
892 node.setAttribute("d", "M0,0");
893 paper.pathfinder(o, value);
896 node.setAttribute(att, value);
905 value = -attrs.x - (attrs.width || 0);
909 node.setAttribute(att, value);
913 node.setAttribute(att, value);
922 value = -attrs.y - (attrs.height || 0);
926 node.setAttribute(att, value);
930 if (o.type == "rect") {
931 node.setAttribute("rx", value);
932 node.setAttribute("ry", value);
934 node.setAttribute(att, value);
938 if (o.type == "image") {
939 node.setAttributeNS(o.paper.xlink, "href", value);
943 node.style.strokeWidth = value;
944 // Need following line for Firefox
945 node.setAttribute(att, value);
946 if (attrs["stroke-dasharray"]) {
947 addDashes(o, attrs["stroke-dasharray"]);
950 case "stroke-dasharray":
954 o.rotate(value, true);
957 var xy = (value + "").split(separator);
958 o.translate((+xy[0] + 1 || 2) - 1, (+xy[1] + 1 || 2) - 1);
961 var xy = (value + "").split(separator);
962 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
965 var isURL = value.match(/^url\(([^\)]+)\)$/i);
967 var el = doc.createElementNS(o.paper.svgns, "pattern");
968 var ig = doc.createElementNS(o.paper.svgns, "image");
969 el.id = "raphael-pattern-" + R.idGenerator++;
970 el.setAttribute("x", 0);
971 el.setAttribute("y", 0);
972 el.setAttribute("patternUnits", "userSpaceOnUse");
973 ig.setAttribute("x", 0);
974 ig.setAttribute("y", 0);
975 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
978 var img = doc.createElement("img");
979 img.style.position = "absolute";
980 img.style.top = "-9999em";
981 img.style.left = "-9999em";
982 img.onload = function () {
983 el.setAttribute("width", this.offsetWidth);
984 el.setAttribute("height", this.offsetHeight);
985 ig.setAttribute("width", this.offsetWidth);
986 ig.setAttribute("height", this.offsetHeight);
987 doc.body.removeChild(this);
990 doc.body.appendChild(img);
992 o.paper.defs.appendChild(el);
993 node.style.fill = "url(#" + el.id + ")";
994 node.setAttribute("fill", "url(#" + el.id + ")");
999 delete params.gradient;
1000 delete attrs.gradient;
1001 if (typeof attrs.opacity != "undefined" && typeof params.opacity == "undefined" ) {
1002 node.style.opacity = attrs.opacity;
1003 // Need following line for Firefox
1004 node.setAttribute("opacity", attrs.opacity);
1006 if (typeof attrs["fill-opacity"] != "undefined" && typeof params["fill-opacity"] == "undefined" ) {
1007 node.style.fillOpacity = o.attrs["fill-opacity"];
1008 // Need following line for Firefox
1009 node.setAttribute("fill-opacity", attrs["fill-opacity"]);
1012 node.style[att] = R.getRGB(value).hex;
1013 // Need following line for Firefox
1014 node.setAttribute(att, R.getRGB(value).hex);
1017 addGradientFill(node, value, o.paper);
1020 case "fill-opacity":
1021 if (attrs.gradient) {
1022 var gradient = doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, ""));
1024 var stops = gradient.getElementsByTagName("stop");
1025 stops[stops.length - 1].setAttribute("stop-opacity", value);
1030 var cssrule = att.replace(/(\-.)/g, function (w) {
1031 return w.substring(1).toUpperCase();
1033 node.style[cssrule] = value;
1034 // Need following line for Firefox
1035 node.setAttribute(att, value);
1040 tuneText(o, params);
1043 var tuneText = function (el, params) {
1044 if (el.type != "text" || !("text" in params || "font" in params || "font-size" in params || "x" in params || "y" in params)) {
1049 fontSize = node.firstChild ? parseInt(doc.defaultView.getComputedStyle(node.firstChild, "").getPropertyValue("font-size"), 10) : 10;
1051 if ("text" in params) {
1052 while (node.firstChild) {
1053 node.removeChild(node.firstChild);
1055 var texts = (params.text + "").split("\n");
1056 for (var i = 0, ii = texts.length; i < ii; i++) {
1057 var tspan = doc.createElementNS(el.paper.svgns, "tspan");
1058 i && tspan.setAttribute("dy", fontSize * leading);
1059 i && tspan.setAttribute("x", a.x);
1060 tspan.appendChild(doc.createTextNode(texts[i]));
1061 node.appendChild(tspan);
1064 var texts = node.getElementsByTagName("tspan");
1065 for (var i = 0, ii = texts.length; i < ii; i++) {
1066 i && texts[i].setAttribute("dy", fontSize * leading);
1067 i && texts[i].setAttribute("x", a.x);
1070 node.setAttribute("y", a.y);
1071 var bb = el.getBBox(),
1072 dif = a.y - (bb.y + bb.height / 2);
1073 dif && node.setAttribute("y", a.y + dif);
1075 var Element = function (node, svg) {
1081 this.attrs = this.attrs || {};
1082 this.transformations = []; // rotate, translate, scale
1086 rt: {deg: 0, x: 0, y: 0},
1091 Element.prototype.rotate = function (deg, cx, cy) {
1093 return this._.rt.deg;
1095 var bbox = this.getBBox();
1096 deg = deg.toString().split(separator);
1097 if (deg.length - 1) {
1098 cx = parseFloat(deg[1]);
1099 cy = parseFloat(deg[2]);
1101 deg = parseFloat(deg[0]);
1103 this._.rt.deg = deg;
1105 this._.rt.deg += deg;
1110 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
1111 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
1112 if (this._.rt.deg) {
1113 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
1115 this.transformations[0] = "";
1117 this.node.setAttribute("transform", this.transformations.join(" "));
1120 Element.prototype.hide = function () {
1121 this.node.style.display = "none";
1124 Element.prototype.show = function () {
1125 this.node.style.display = "block";
1128 Element.prototype.remove = function () {
1129 this.node.parentNode.removeChild(this.node);
1131 Element.prototype.getBBox = function () {
1132 var bbox = this.node.getBBox();
1133 if (this.type == "text") {
1134 bbox = {x: bbox.x, y: Infinity, width: bbox.width, height: 0};
1135 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
1136 var bb = this.node.getExtentOfChar(i);
1137 (bb.y < bbox.y) && (bbox.y = bb.y);
1138 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
1143 Element.prototype.attr = function () {
1144 if (arguments.length == 1 && typeof arguments[0] == "string") {
1145 if (arguments[0] == "translation") {
1146 return this.translate();
1148 return this.attrs[arguments[0]];
1150 if (arguments.length == 1 && arguments[0] instanceof win.Array) {
1152 for (var j in arguments[0]) {
1153 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1157 if (arguments.length == 2) {
1159 params[arguments[0]] = arguments[1];
1160 setFillAndStroke(this, params);
1161 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1162 setFillAndStroke(this, arguments[0]);
1166 Element.prototype.toFront = function () {
1167 this.node.parentNode.appendChild(this.node);
1170 Element.prototype.toBack = function () {
1171 if (this.node.parentNode.firstChild != this.node) {
1172 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1176 Element.prototype.insertAfter = function (element) {
1177 if (element.node.nextSibling) {
1178 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
1180 element.node.parentNode.appendChild(this.node);
1184 Element.prototype.insertBefore = function (element) {
1185 element.node.parentNode.insertBefore(this.node, element.node);
1188 var theCircle = function (svg, x, y, r) {
1189 var el = doc.createElementNS(svg.svgns, "circle");
1190 el.setAttribute("cx", x);
1191 el.setAttribute("cy", y);
1192 el.setAttribute("r", r);
1193 el.setAttribute("fill", "none");
1194 el.setAttribute("stroke", "#000");
1196 svg.canvas.appendChild(el);
1198 var res = new Element(el, svg);
1199 res.attrs = res.attrs || {};
1203 res.attrs.stroke = "#000";
1204 res.type = "circle";
1207 var theRect = function (svg, x, y, w, h, r) {
1208 var el = doc.createElementNS(svg.svgns, "rect");
1209 el.setAttribute("x", x);
1210 el.setAttribute("y", y);
1211 el.setAttribute("width", w);
1212 el.setAttribute("height", h);
1214 el.setAttribute("rx", r);
1215 el.setAttribute("ry", r);
1217 el.setAttribute("fill", "none");
1218 el.setAttribute("stroke", "#000");
1220 svg.canvas.appendChild(el);
1222 var res = new Element(el, svg);
1223 res.attrs = res.attrs || {};
1226 res.attrs.width = w;
1227 res.attrs.height = h;
1228 res.attrs.stroke = "#000";
1230 res.attrs.rx = res.attrs.ry = r;
1235 var theEllipse = function (svg, x, y, rx, ry) {
1236 var el = doc.createElementNS(svg.svgns, "ellipse");
1237 el.setAttribute("cx", x);
1238 el.setAttribute("cy", y);
1239 el.setAttribute("rx", rx);
1240 el.setAttribute("ry", ry);
1241 el.setAttribute("fill", "none");
1242 el.setAttribute("stroke", "#000");
1244 svg.canvas.appendChild(el);
1246 var res = new Element(el, svg);
1247 res.attrs = res.attrs || {};
1252 res.attrs.stroke = "#000";
1253 res.type = "ellipse";
1256 var theImage = function (svg, src, x, y, w, h) {
1257 var el = doc.createElementNS(svg.svgns, "image");
1258 el.setAttribute("x", x);
1259 el.setAttribute("y", y);
1260 el.setAttribute("width", w);
1261 el.setAttribute("height", h);
1262 el.setAttribute("preserveAspectRatio", "none");
1263 el.setAttributeNS(svg.xlink, "href", src);
1265 svg.canvas.appendChild(el);
1267 var res = new Element(el, svg);
1268 res.attrs = res.attrs || {};
1271 res.attrs.width = w;
1272 res.attrs.height = h;
1276 var theText = function (svg, x, y, text) {
1277 var el = doc.createElementNS(svg.svgns, "text");
1278 el.setAttribute("x", x);
1279 el.setAttribute("y", y);
1280 el.setAttribute("text-anchor", "middle");
1282 svg.canvas.appendChild(el);
1284 var res = new Element(el, svg);
1285 res.attrs = res.attrs || {};
1289 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000", text: text});
1292 var setSize = function (width, height) {
1293 this.width = width || this.width;
1294 this.height = height || this.height;
1295 this.canvas.setAttribute("width", this.width);
1296 this.canvas.setAttribute("height", this.height);
1299 var create = function () {
1300 var con = getContainer.apply(null, arguments);
1301 var container = con.container,
1305 height = con.height;
1307 throw new Error("SVG container not found.");
1309 paper.canvas = doc.createElementNS(paper.svgns, "svg");
1310 paper.canvas.setAttribute("width", width || 512);
1311 paper.width = width || 512;
1312 paper.canvas.setAttribute("height", height || 342);
1313 paper.height = height || 342;
1314 if (container == 1) {
1315 doc.body.appendChild(paper.canvas);
1316 paper.canvas.style.position = "absolute";
1317 paper.canvas.style.left = x + "px";
1318 paper.canvas.style.top = y + "px";
1320 if (container.firstChild) {
1321 container.insertBefore(paper.canvas, container.firstChild);
1323 container.appendChild(paper.canvas);
1327 canvas: paper.canvas,
1328 clear: function () {
1329 while (this.canvas.firstChild) {
1330 this.canvas.removeChild(this.canvas.firstChild);
1332 this.defs = doc.createElementNS(paper.svgns, "defs");
1333 this.canvas.appendChild(this.defs);
1336 for (var prop in paper) {
1337 if (prop != "create") {
1338 container[prop] = paper[prop];
1341 plugins.call(container, container, R.fn);
1343 container.raphael = R;
1346 paper.remove = function () {
1347 this.canvas.parentNode.removeChild(this.canvas);
1349 paper.svgns = "http://www.w3.org/2000/svg";
1350 paper.xlink = "http://www.w3.org/1999/xlink";
1351 paper.safari = function () {
1352 if ({"Apple Computer, Inc.": 1, "Google Inc.": 1}[navigator.vendor]) {
1353 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1354 setTimeout(function () {rect.remove();});
1361 R.toString = function () {
1362 return "Your browser doesn\u2019t support SVG.\nYou are running Rapha\u00ebl " + this.version;
1364 thePath = function (params, pathString, VML) {
1365 var g = createNode("group"), gl = g.style;
1366 gl.position = "absolute";
1369 gl.width = VML.width + "px";
1370 gl.height = VML.height + "px";
1371 var el = createNode("shape"), ol = el.style;
1372 ol.width = VML.width + "px";
1373 ol.height = VML.height + "px";
1375 if (params["class"]) {
1376 el.className = "rvml " + params["class"];
1378 el.coordsize = this.coordsize;
1379 el.coordorigin = this.coordorigin;
1381 var p = new Element(el, g, VML);
1382 p.isAbsolute = true;
1385 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1387 p.absolutely = function () {
1388 this.isAbsolute = true;
1391 p.relatively = function () {
1392 this.isAbsolute = false;
1395 p.moveTo = function (x, y) {
1396 var d = this.isAbsolute?"m":"t";
1397 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1398 this.node.path = this.Path += d;
1399 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1400 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1401 this.last.isAbsolute = this.isAbsolute;
1402 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1405 p.lineTo = function (x, y) {
1406 var d = this.isAbsolute?"l":"r";
1407 d += Math.round(parseFloat(x)) + " " + Math.round(parseFloat(y));
1408 this[0].path = this.Path += d;
1409 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x);
1410 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y);
1411 this.last.isAbsolute = this.isAbsolute;
1412 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1415 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1416 // for more information of where this math came from visit:
1417 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1418 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1419 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1420 var x1 = this.last.x,
1424 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1425 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1426 cx = k * rx * y / ry + (x1 + x2) / 2,
1427 cy = k * -ry * x / rx + (y1 + y2) / 2,
1428 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1429 left = Math.round(cx - rx),
1430 top = Math.round(cy - ry);
1431 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(", ");
1432 this.node.path = this.Path += d;
1433 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2);
1434 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2);
1435 this.last.isAbsolute = this.isAbsolute;
1436 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1439 p.cplineTo = function (x1, y1, w1) {
1441 return this.lineTo(x1, y1);
1443 var x = Math.round(Math.round(parseFloat(x1) * 100) / 100),
1444 y = Math.round(Math.round(parseFloat(y1) * 100) / 100),
1445 w = Math.round(Math.round(parseFloat(w1) * 100) / 100),
1446 d = this.isAbsolute ? "c" : "v",
1447 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1448 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1449 d += attr.join(" ") + " ";
1450 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1451 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1452 this.last.bx = attr[2];
1453 this.last.by = attr[3];
1454 this.node.path = this.Path += d;
1455 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1459 p.curveTo = function () {
1460 var d = this.isAbsolute ? "c" : "v";
1461 if (arguments.length == 6) {
1462 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1463 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1464 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4]);
1465 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5]);
1466 d += [Math.round(parseFloat(arguments[0])),
1467 Math.round(parseFloat(arguments[1])),
1468 Math.round(parseFloat(arguments[2])),
1469 Math.round(parseFloat(arguments[3])),
1470 Math.round(parseFloat(arguments[4])),
1471 Math.round(parseFloat(arguments[5]))].join(" ") + " ";
1472 this.last.isAbsolute = this.isAbsolute;
1473 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1475 if (arguments.length == 4) {
1476 var bx = this.last.x * 2 - this.last.bx;
1477 var by = this.last.y * 2 - this.last.by;
1478 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1479 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1480 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1481 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1482 d += [Math.round(bx), Math.round(by),
1483 Math.round(parseFloat(arguments[0])),
1484 Math.round(parseFloat(arguments[1])),
1485 Math.round(parseFloat(arguments[2])),
1486 Math.round(parseFloat(arguments[3]))].join(" ") + " ";
1487 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1489 this.node.path = this.Path += d;
1492 p.qcurveTo = function () {
1494 if (arguments.length == 4) {
1495 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0]);
1496 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1]);
1497 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1498 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1499 d += [Math.round(this.last.qx),
1500 Math.round(this.last.qy),
1501 Math.round(this.last.x),
1502 Math.round(this.last.y)].join(" ") + " ";
1503 this.last.isAbsolute = this.isAbsolute;
1504 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1506 if (arguments.length == 2) {
1507 this.last.qx = this.last.x * 2 - this.last.qx;
1508 this.last.qy = this.last.y * 2 - this.last.qy;
1509 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2]);
1510 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3]);
1511 d += [Math.round(this.last.qx),
1512 Math.round(this.last.qy),
1513 Math.round(this.last.x),
1514 Math.round(this.last.y)].join(" ") + " ";
1515 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1517 this.node.path = this.Path += d;
1518 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1521 p.addRoundedCorner = function (r, dir) {
1522 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1525 rollback = function () {
1529 rollback = function () {};
1535 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1538 o.curveTo(-R, 0, -r, r - R, -r, r);
1545 o.curveTo(R, 0, r, -(r - R), r, -r);
1548 o.curveTo(R, 0, r, r - R, r, r);
1555 o.curveTo(0, -R, -(R - r), -r, r, -r);
1558 o.curveTo(0, -R, R - r, -r, -r, -r);
1565 o.curveTo(0, R, -(R - r), r, r, r);
1568 o.curveTo(0, R, R - r, r, -r, r);
1573 actions[dir.charAt(0)]()[dir.charAt(1)]();
1577 p.andClose = function () {
1578 this.node.path = (this.Path += "x");
1579 this.attrs.path += "z";
1585 paper.pathfinder(p, "" + pathString);
1588 params.fill = params.fill || "none";
1589 params.stroke = params.stroke || "#000";
1591 params = {fill: "none", stroke: "#000"};
1593 setFillAndStroke(p, params);
1594 if (params.gradient) {
1595 addGradientFill(p, params.gradient);
1597 VML.canvas.appendChild(g);
1600 var setFillAndStroke = function (o, params) {
1604 o.attrs = o.attrs || {};
1605 for (var par in params) {
1606 o.attrs[par] = params[par];
1608 params.href && (node.href = params.href);
1609 params.title && (node.title = params.title);
1610 params.target && (node.target = params.target);
1611 if (params.path && o.type == "path") {
1614 paper.pathfinder(o, params.path);
1616 if (params.rotation != null) {
1617 o.rotate(params.rotation, true);
1619 if (params.translation) {
1620 var xy = (params.translation + "").split(separator);
1621 o.translate(xy[0], xy[1]);
1624 var xy = (params.scale + "").split(separator);
1625 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
1627 if (o.type == "image" && params.src) {
1628 node.src = params.src;
1630 if (o.type == "image" && params.opacity) {
1631 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1632 node.style.filter = (node.filterMatrix || "") + (node.filterOpacity || "");
1634 params.font && (s.font = params.font);
1635 params["font-family"] && (s.fontFamily = params["font-family"]);
1636 params["font-size"] && (s.fontSize = params["font-size"]);
1637 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1638 params["font-style"] && (s.fontStyle = params["font-style"]);
1639 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"]) {
1640 o = o.shape || node;
1641 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || createNode("fill");
1642 if ("fill-opacity" in params || "opacity" in params) {
1643 fill.opacity = ((+params["fill-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1645 params.fill && (fill.on = true);
1646 if (typeof fill.on == "undefined" || params.fill == "none") {
1649 if (fill.on && params.fill) {
1650 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1652 fill.src = isURL[1];
1655 fill.color = R.getRGB(params.fill).hex;
1657 fill.type = "solid";
1660 o.appendChild(fill);
1661 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || createNode("stroke");
1662 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"]) {
1665 if (params.stroke == "none" || typeof stroke.on == "undefined" || params.stroke == 0) {
1668 if (stroke.on && params.stroke) {
1669 stroke.color = R.getRGB(params.stroke).hex;
1671 stroke.opacity = ((+params["stroke-opacity"] + 1 || 2) - 1) * ((+params.opacity + 1 || 2) - 1);
1672 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1673 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1674 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1675 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"]) || 1) * 12 / 16);
1676 if (params["stroke-dasharray"]) {
1680 "-.": "shortdashdot",
1681 "-..": "shortdashdotdot",
1686 "--.": "longdashdot",
1687 "--..": "longdashdotdot"
1689 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1691 o.appendChild(stroke);
1693 if (res.type == "text") {
1694 var span = doc.createElement("span"),
1700 s.display = "inline";
1701 a.font && (s.font = a.font);
1702 a["font-family"] && (s.fontFamily = a["font-family"]);
1703 a["font-size"] && (s.fontSize = a["font-size"]);
1704 a["font-weight"] && (s.fontWeight = a["font-weight"]);
1705 a["font-style"] && (s.fontStyle = a["font-style"]);
1706 res.node.parentNode.appendChild(span);
1707 span.innerText = res.node.string;
1708 res.W = a.w = span.offsetWidth;
1709 res.H = a.h = span.offsetHeight;
1711 res.Y = a.y + Math.round(res.H / 2);
1712 res.node.parentNode.removeChild(span);
1714 // text-anchor emulation
1715 switch (a["text-anchor"]) {
1717 res.node.style["v-text-align"] = "left";
1718 res.bbx = Math.round(res.W / 2);
1721 res.node.style["v-text-align"] = "right";
1722 res.bbx = -Math.round(res.W / 2);
1725 res.node.style["v-text-align"] = "center";
1730 var getAngle = function (a, b, c, d) {
1731 var angle = Math.round(Math.atan((parseFloat(c) - parseFloat(a)) / (parseFloat(d) - parseFloat(b))) * 57.29) || 0;
1732 if (!angle && parseFloat(a) < parseFloat(b)) {
1741 var addGradientFill = function (o, gradient) {
1742 gradient = toGradient(gradient);
1743 o.attrs = o.attrs || {};
1744 var attrs = o.attrs;
1745 o.attrs.gradient = gradient;
1746 o = o.shape || o[0];
1747 var fill = o.getElementsByTagName("fill");
1751 fill = createNode("fill");
1753 if (gradient.dots.length) {
1755 fill.method = "none";
1756 fill.type = ((gradient.type + "").toLowerCase() == "radial") ? "gradientTitle" : "gradient";
1757 if (typeof gradient.dots[0].color != "undefined") {
1758 fill.color = R.getRGB(gradient.dots[0].color).hex;
1760 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1761 fill.color2 = R.getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1764 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1765 if (gradient.dots[i].offset) {
1766 clrs.push(gradient.dots[i].offset + " " + R.getRGB(gradient.dots[i].color).hex);
1769 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;
1771 fill.colors.value = clrs.join(",");
1772 fillOpacity = typeof attrs.opacity == "undefined" ? 1 : attrs.opacity;
1774 fill.colors && (fill.colors.value = "0% " + fill.color);
1776 fill.opacity = fillOpacity;
1777 if (typeof gradient.angle != "undefined") {
1778 fill.angle = (-gradient.angle + 270) % 360;
1779 } else if (gradient.vector) {
1780 fill.angle = getAngle.apply(null, gradient.vector);
1782 if ((gradient.type + "").toLowerCase() == "radial") {
1783 fill.focus = "100%";
1784 fill.focusposition = "0.5 0.5";
1788 var Element = function (node, group, vml) {
1808 Element.prototype.rotate = function (deg, cx, cy) {
1810 return this._.rt.deg;
1812 deg = deg.toString().split(separator);
1813 if (deg.length - 1) {
1814 cx = parseFloat(deg[1]);
1815 cy = parseFloat(deg[2]);
1817 deg = parseFloat(deg[0]);
1819 this._.rt.deg = deg;
1821 this._.rt.deg += deg;
1828 this.setBox(null, cx, cy);
1829 this.Group.style.rotation = this._.rt.deg;
1830 // gradient fix for rotation. TODO
1831 // var fill = (this.shape || this.node).getElementsByTagName("fill");
1832 // fill = fill[0] || {};
1833 // var b = ((360 - this._.rt.deg) - 270) % 360;
1834 // typeof fill.angle != "undefined" && (fill.angle = b);
1837 Element.prototype.setBox = function (params, cx, cy) {
1838 var gs = this.Group.style,
1839 os = (this.shape && this.shape.style) || this.node.style;
1840 params = params || {};
1841 for (var i in params) {
1842 this.attrs[i] = params[i];
1844 cx = cx || this._.rt.cx;
1845 cy = cy || this._.rt.cy;
1846 var attr = this.attrs, x, y, w, h;
1847 switch (this.type) {
1849 x = attr.cx - attr.r;
1850 y = attr.cy - attr.r;
1854 x = attr.cx - attr.rx;
1855 y = attr.cy - attr.ry;
1863 w = attr.width || 0;
1864 h = attr.height || 0;
1867 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("");
1868 x = attr.x - Math.round(this.W / 2);
1869 y = attr.y - this.H / 2;
1874 if (!this.attrs.path) {
1877 w = this.paper.width;
1878 h = this.paper.height;
1880 var dim = pathDimensions(this.attrs.path),
1890 w = this.paper.width;
1891 h = this.paper.height;
1894 cx = (cx == null) ? x + w / 2 : cx;
1895 cy = (cy == null) ? y + h / 2 : cy;
1896 var left = cx - this.paper.width / 2,
1897 top = cy - this.paper.height / 2;
1898 if (this.type == "path" || this.type == "text") {
1899 (gs.left != left + "px") && (gs.left = left + "px");
1900 (gs.top != top + "px") && (gs.top = top + "px");
1901 this.X = this.type == "text" ? x : -left;
1902 this.Y = this.type == "text" ? y : -top;
1905 (os.left != -left + "px") && (os.left = -left + "px");
1906 (os.top != -top + "px") && (os.top = -top + "px");
1908 (gs.left != left + "px") && (gs.left = left + "px");
1909 (gs.top != top + "px") && (gs.top = top + "px");
1914 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
1915 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
1916 (os.left != x - left + "px") && (os.left = x - left + "px");
1917 (os.top != y - top + "px") && (os.top = y - top + "px");
1918 (os.width != w + "px") && (os.width = w + "px");
1919 (os.height != h + "px") && (os.height = h + "px");
1920 var arcsize = (+params.r || 0) / (Math.min(w, h));
1921 if (this.type == "rect" && this.arcsize != arcsize && (arcsize || this.arcsize)) {
1922 // We should replace element with the new one
1923 var o = createNode(arcsize ? "roundrect" : "rect");
1924 o.arcsize = arcsize;
1925 this.Group.appendChild(o);
1926 this.node.parentNode.removeChild(this.node);
1928 this.arcsize = arcsize;
1929 setFillAndStroke(this, this.attrs);
1930 this.setBox(this.attrs);
1934 Element.prototype.hide = function () {
1935 this.Group.style.display = "none";
1938 Element.prototype.show = function () {
1939 this.Group.style.display = "block";
1942 Element.prototype.getBBox = function () {
1943 if (this.type == "path") {
1944 return pathDimensions(this.attr("path"));
1947 x: this.X + (this.bbx || 0),
1953 Element.prototype.remove = function () {
1954 this[0].parentNode.removeChild(this[0]);
1955 this.Group.parentNode.removeChild(this.Group);
1956 this.shape && this.shape.parentNode.removeChild(this.shape);
1958 Element.prototype.attr = function () {
1959 if (arguments.length == 1 && typeof arguments[0] == "string") {
1960 if (arguments[0] == "translation") {
1961 return this.translate();
1963 return this.attrs[arguments[0]];
1965 if (this.attrs && arguments.length == 1 && arguments[0] instanceof win.Array) {
1967 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1968 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1973 if (arguments.length == 2) {
1975 params[arguments[0]] = arguments[1];
1977 if (arguments.length == 1 && typeof arguments[0] == "object") {
1978 params = arguments[0];
1981 if (params.gradient) {
1982 addGradientFill(this, params.gradient);
1984 if (params.text && this.type == "text") {
1985 this.node.string = params.text;
1988 this.node.id = params.id;
1990 setFillAndStroke(this, params);
1991 this.setBox(params);
1995 Element.prototype.toFront = function () {
1996 this.Group.parentNode.appendChild(this.Group);
1999 Element.prototype.toBack = function () {
2000 if (this.Group.parentNode.firstChild != this.Group) {
2001 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
2005 Element.prototype.insertAfter = function (element) {
2006 if (element.Group.nextSibling) {
2007 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
2009 element.Group.parentNode.appendChild(this.Group);
2013 Element.prototype.insertBefore = function (element) {
2014 element.Group.parentNode.insertBefore(this.Group, element.Group);
2017 var theCircle = function (vml, x, y, r) {
2018 var g = createNode("group");
2019 var o = createNode("oval");
2021 var res = new Element(o, g, vml);
2022 res.type = "circle";
2023 setFillAndStroke(res, {stroke: "#000", fill: "none"});
2027 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
2028 vml.canvas.appendChild(g);
2031 var theRect = function (vml, x, y, w, h, r) {
2032 var g = createNode("group"),
2033 o = createNode(r ? "roundrect" : "rect"),
2034 arcsize = (+r || 0) / (Math.min(w, h));
2035 o.arcsize = arcsize;
2037 var res = new Element(o, g, vml);
2039 setFillAndStroke(res, {stroke: "#000"});
2045 res.arcsize = arcsize;
2046 res.setBox({x: x, y: y, width: w, height: h});
2047 vml.canvas.appendChild(g);
2050 var theEllipse = function (vml, x, y, rx, ry) {
2051 var g = createNode("group");
2052 var o = createNode("oval");
2054 var res = new Element(o, g, vml);
2055 res.type = "ellipse";
2056 setFillAndStroke(res, {stroke: "#000"});
2061 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
2062 vml.canvas.appendChild(g);
2065 var theImage = function (vml, src, x, y, w, h) {
2066 var g = createNode("group");
2067 var o = createNode("image");
2070 var res = new Element(o, g, vml);
2076 res.setBox({x: x, y: y, width: w, height: h});
2077 vml.canvas.appendChild(g);
2080 var theText = function (vml, x, y, text) {
2081 var g = createNode("group"), gs = g.style;
2082 var el = createNode("shape"), ol = el.style;
2083 var path = createNode("path"), ps = path.style;
2084 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
2085 path.textpathok = true;
2086 ol.width = vml.width;
2087 ol.height = vml.height;
2088 gs.position = "absolute";
2091 gs.width = vml.width;
2092 gs.height = vml.height;
2093 var o = createNode("textpath");
2096 o.coordsize = vml.coordsize;
2097 o.coordorigin = vml.coordorigin;
2099 el.appendChild(path);
2101 var res = new Element(o, g, vml);
2103 res.textpath = path;
2109 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
2111 vml.canvas.appendChild(g);
2114 var setSize = function (width, height) {
2115 this.width = width || this.width;
2116 this.height = height || this.height;
2117 this.canvas.style.width = this.width + "px";
2118 this.canvas.style.height = this.height + "px";
2119 this.canvas.parentNode.style.clip = "rect(0 " + this.width + "px " + this.height + "px 0)";
2120 this.canvas.coordsize = this.width + " " + this.height;
2123 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
2125 if (!doc.namespaces.rvml) {
2126 doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
2128 var createNode = function (tagName) {
2129 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
2132 var createNode = function (tagName) {
2133 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
2136 var create = function () {
2137 var con = getContainer.apply(null, arguments);
2138 var container = con.container,
2142 height = con.height;
2144 throw new Error("VML container not found.");
2146 var c = doc.createElement("div"),
2147 d = doc.createElement("div"),
2148 r = paper.canvas = createNode("group"),
2149 cs = c.style, rs = r.style;
2150 paper.width = width;
2151 paper.height = height;
2152 width = width || "512px";
2153 height = height || "342px";
2154 cs.clip = "rect(0 " + width + "px " + height + "px 0)";
2157 cs.position = "absolute";
2158 rs.position = "absolute";
2159 d.style.position = "relative";
2162 r.coordsize = (/%$/.test(width) ? width : parseFloat(width)) + " " + (/%$/.test(height) ? height : parseFloat(height));
2163 r.coordorigin = "0 0";
2165 var b = createNode("rect"), bs = b.style;
2166 bs.left = bs.top = 0;
2167 bs.width = rs.width;
2168 bs.height = rs.height;
2169 b.filled = b.stroked = "f";
2174 if (container == 1) {
2175 doc.body.appendChild(d);
2176 cs.position = "absolute";
2188 cs.width = container.style.width = width;
2189 cs.height = container.style.height = height;
2190 if (container.firstChild) {
2191 container.insertBefore(d, container.firstChild);
2193 container.appendChild(d);
2196 for (var prop in paper) {
2197 container[prop] = paper[prop];
2199 plugins.call(container, container, R.fn);
2200 container.clear = function () {
2202 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
2203 if (r.childNodes[i] != b) {
2204 todel.push(r.childNodes[i]);
2207 for (i = 0, ii = todel.length; i < ii; i++) {
2208 r.removeChild(todel[i]);
2211 container.raphael = R;
2214 paper.remove = function () {
2215 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
2217 paper.safari = function () {};
2223 var addEvent = (function () {
2224 if (doc.addEventListener) {
2225 return function (obj, type, fn, element) {
2226 var f = function (e) {
2227 return fn.call(element, e);
2229 obj.addEventListener(type, f, false);
2230 return function () {
2231 obj.removeEventListener(type, f, false);
2235 } else if (doc.attachEvent) {
2236 return function (obj, type, fn, element) {
2237 var f = function (e) {
2238 return fn.call(element, e || win.event);
2240 obj.attachEvent("on" + type, f);
2241 var detacher = function () {
2242 obj.detachEvent("on" + type, f);
2245 if (type == "mouseover") {
2246 obj.attachEvent("onmouseenter", f);
2247 return function () {
2248 obj.detachEvent("onmouseenter", f);
2251 } else if (type == "mouseout") {
2252 obj.attachEvent("onmouseleave", f);
2253 return function () {
2254 obj.detachEvent("onmouseleave", f);
2262 for (var i = events.length; i--;) {
2263 (function (eventName) {
2264 Element.prototype[eventName] = function (fn) {
2265 if (typeof fn == "function") {
2266 this.events = this.events || {};
2267 this.events[eventName] = this.events[eventName] || {};
2268 this.events[eventName][fn] = this.events[eventName][fn] || [];
2269 this.events[eventName][fn].push(addEvent(this.shape || this.node, eventName, fn, this));
2273 Element.prototype["un" + eventName] = function (fn) {
2275 this.events[eventName] &&
2276 this.events[eventName][fn] &&
2277 this.events[eventName][fn].length &&
2278 this.events[eventName][fn].shift()() &&
2279 !this.events[eventName][fn].length &&
2280 delete this.events[eventName][fn];
2285 paper.circle = function (x, y, r) {
2286 return theCircle(this, x, y, r);
2288 paper.rect = function (x, y, w, h, r) {
2289 return theRect(this, x, y, w, h, r);
2291 paper.ellipse = function (x, y, rx, ry) {
2292 return theEllipse(this, x, y, rx, ry);
2294 paper.path = function (params, pathString) {
2295 return thePath(params, pathString, this);
2297 paper.image = function (src, x, y, w, h) {
2298 return theImage(this, src, x, y, w, h);
2300 paper.text = function (x, y, text) {
2301 return theText(this, x, y, text);
2303 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2304 color = color || "#000";
2305 var path = ["M", x, y, "L", x + w, y, x + w, y + h, x, y + h, x, y],
2307 columnWidth = w / wv;
2308 for (var i = 1; i < hv; i++) {
2309 path = path.concat(["M", x, y + i * rowHeight, "L", x + w, y + i * rowHeight]);
2311 for (var i = 1; i < wv; i++) {
2312 path = path.concat(["M", x + i * columnWidth, y, "L", x + i * columnWidth, y + h]);
2314 return this.path({stroke: color, "stroke-width": 1}, path.join(","));
2316 paper.pathfinder = function (p, path) {
2318 M: function (x, y) {
2321 C: function (x1, y1, x2, y2, x3, y3) {
2322 this.curveTo(x1, y1, x2, y2, x3, y3);
2324 Q: function (x1, y1, x2, y2) {
2325 this.qcurveTo(x1, y1, x2, y2);
2327 T: function (x, y) {
2328 this.qcurveTo(x, y);
2330 S: function (x1, y1, x2, y2) {
2331 p.curveTo(x1, y1, x2, y2);
2333 L: function (x, y) {
2337 this.lineTo(x, this.last.y);
2340 this.lineTo(this.last.x, y);
2342 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2343 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2350 path = pathToAbsolute(path);
2351 for (var i = 0, ii = path.length; i < ii; i++) {
2352 var b = path[i].shift();
2353 commands[b].apply(p, path[i]);
2357 paper.set = function (itemsArray) {
2358 return new Set(itemsArray);
2360 paper.setSize = setSize;
2361 Element.prototype.stop = function () {
2362 clearTimeout(this.animation_in_progress);
2364 Element.prototype.scale = function (x, y, cx, cy) {
2365 if (x == null && y == null) {
2366 return {x: this._.sx, y: this._.sy, toString: function () { return this.x.toFixed(3) + " " + this.y.toFixed(3); }};
2370 var dx, dy, dcx, dcy, a = this.attrs;
2372 var bb = this.type == "path" ? pathDimensions(a.path) : this.getBBox(),
2373 rcx = bb.x + bb.width / 2,
2374 rcy = bb.y + bb.height / 2;
2375 cx = (+cx || cx == 0) ? cx : rcx;
2376 cy = (+cy || cy == 0) ? cy : rcy;
2377 var dirx = Math.round(x / Math.abs(x)),
2378 diry = Math.round(y / Math.abs(y)),
2379 s = this.node.style,
2380 ncx = cx + (rcx - cx) * x * dirx / this._.sx,
2381 ncy = cy + (rcy - cy) * y * diry / this._.sy;
2382 switch (this.type) {
2385 var neww = a.width * x * dirx / this._.sx,
2386 newh = a.height * y * diry / this._.sy,
2387 newx = ncx - neww / 2,
2388 newy = ncy - newh / 2;
2399 rx: a.rx * x / this._.sx,
2400 ry: a.ry * y / this._.sy,
2401 r: a.r * x / this._.sx,
2407 var path = pathToRelative(a.path),
2409 for (var i = 0, ii = path.length; i < ii; i++) {
2411 if (p[0].toUpperCase() == "M" && skip) {
2416 if (R.svg && p[0].toUpperCase() == "A") {
2417 p[path[i].length - 2] *= x / this._.sx;
2418 p[path[i].length - 1] *= y / this._.sy;
2419 p[1] *= x / this._.sx;
2420 p[2] *= y / this._.sy;
2421 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
2423 for (var j = 1, jj = p.length; j < jj; j++) {
2424 p[j] *= (j % 2) ? x / this._.sx : y / this._.sy;
2428 var dim2 = pathDimensions(path),
2429 dx = ncx - dim2.x - dim2.width / 2,
2430 dy = ncy - dim2.y - dim2.height / 2;
2431 path = pathToRelative(path);
2435 this.attr({path: path.join(" ")});
2438 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
2439 if (this.transformations) {
2440 this.transformations[2] = "scale(".concat(dirx, ",", diry, ")");
2441 this.node.setAttribute("transform", this.transformations.join(" "));
2442 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
2443 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
2444 this.attr({x: dx, y: dy});
2448 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=".concat(dirx,
2449 ", M12=0, M21=0, M22=", diry,
2450 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
2451 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2454 if (this.transformations) {
2455 this.transformations[2] = "";
2456 this.node.setAttribute("transform", this.transformations.join(" "));
2460 this.node.filterMatrix = "";
2461 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2464 a.scale = [x, y, cx, cy].join(" ");
2470 Element.prototype.animate = function (params, ms, callback) {
2471 clearTimeout(this.animation_in_progress);
2476 for (var attr in params) {
2477 if (attr in availableAnimAttrs) {
2478 from[attr] = this.attr(attr);
2479 (typeof from[attr] == "undefined") && (from[attr] = availableAttrs[attr]);
2480 to[attr] = params[attr];
2481 switch (availableAnimAttrs[attr]) {
2483 diff[attr] = (to[attr] - from[attr]) / ms;
2486 from[attr] = R.getRGB(from[attr]);
2487 var toColour = R.getRGB(to[attr]);
2489 r: (toColour.r - from[attr].r) / ms,
2490 g: (toColour.g - from[attr].g) / ms,
2491 b: (toColour.b - from[attr].b) / ms
2495 var pathes = pathEqualiser(from[attr], to[attr]);
2496 from[attr] = pathes[0];
2497 to[attr] = pathes[1];
2499 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2500 diff[attr][i] = [0];
2501 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2502 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2507 var values = (params[attr] + "").split(separator),
2508 from2 = (from[attr] + "").split(separator);
2511 from[attr] = [0, 0];
2512 diff[attr] = [values[0] / ms, values[1] / ms];
2515 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
2516 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
2519 params[attr] = values;
2520 from[attr] = (from[attr] + "").split(separator);
2521 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
2527 var start = +new Date,
2531 var time = new Date - start,
2535 for (var attr in from) {
2536 switch (availableAnimAttrs[attr]) {
2538 now = +from[attr] + time * diff[attr];
2542 Math.round(from[attr].r + time * diff[attr].r),
2543 Math.round(from[attr].g + time * diff[attr].g),
2544 Math.round(from[attr].b + time * diff[attr].b)
2549 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2550 now[i] = [from[attr][i][0]];
2551 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2552 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2554 now[i] = now[i].join(" ");
2556 now = now.join(" ");
2561 var x = diff[attr][0] * (time - prev),
2562 y = diff[attr][1] * (time - prev);
2565 now = [x, y].join(" ");
2568 now = +from[attr][0] + time * diff[attr][0];
2569 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
2572 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1], (2 in params[attr] ? params[attr][2] : ""), (3 in params[attr] ? params[attr][3] : "")].join(" ");
2576 if (attr == "font-size") {
2577 set[attr] = now + "px";
2583 that.animation_in_progress = setTimeout(tick);
2586 (t.x || t.y) && that.translate(-t.x, -t.y);
2588 clearTimeout(that.animation_in_progress);
2590 (typeof callback == "function") && callback.call(that);
2596 Element.prototype.translate = function (x, y) {
2598 return {x: this._.tx, y: this._.ty};
2602 switch (this.type) {
2605 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
2610 this.attr({x: this.attrs.x + (+x), y: this.attrs.y + (+y)});
2613 var path = pathToRelative(this.attrs.path);
2616 this.attr({path: path.join(" ")});
2623 var Set = function (itemsArray) {
2625 this.length = (itemsArray && itemsArray.length) || 0;
2626 if (itemsArray && itemsArray.constructor == Array) {
2627 for (var i = itemsArray.length; i--;) {
2628 if (itemsArray[i].constructor == Element) {
2629 this.items[this.items.length] = itemsArray[i];
2634 Set.prototype.push = function (item) {
2635 if (item && item.constructor == Element) {
2636 var len = this.items.length;
2637 this.items[len] = item;
2643 Set.prototype.pull = function (id) {
2644 var res = this.items.splice(id, 1)[0];
2645 for (var j = id, jj = this.items.length; j < jj; j++) {
2646 this[j] = this[j + 1];
2648 delete this[jj + 1];
2652 for (var method in Element.prototype) {
2653 Set.prototype[method] = (function (methodname) {
2654 return function () {
2655 for (var i = this.items.length; i--;) {
2656 this.items[i][methodname].apply(this.items[i], arguments);
2662 Set.prototype.getBBox = function () {
2663 var x = [], y = [], w = [], h = [];
2664 for (var i = this.items.length; i--;) {
2665 var box = this.items[i].getBBox();
2668 w.push(box.x + box.width);
2669 h.push(box.y + box.height);
2671 x = Math.min.apply(Math, x);
2672 y = Math.min.apply(Math, y);
2676 width: Math.max.apply(Math, w) - x,
2677 height: Math.max.apply(Math, h) - y
2681 R.registerFont = function (font) {
2685 this.fonts = this.fonts || {};
2686 if (this.fonts[font.face["font-family"]]) {
2687 this.fonts[font.face["font-family"]].push(font);
2689 this.fonts[font.face["font-family"]] = [font];
2692 font.face["units-per-em"] = parseInt(font.face["units-per-em"], 10);
2694 for (var glyph in font.glyphs) {
2695 var path = font.glyphs[glyph];
2697 path.d = "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
2698 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
2704 paper.getFont = function (family, weight, style, stretch) {
2705 stretch = stretch || "normal";
2706 style = style || "normal";
2707 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
2708 var font = R.fonts[family];
2710 var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, "") + "(\\s|$)", "i");
2711 for (var fontName in R.fonts) {
2712 if (name.test(fontName)) {
2713 font = R.fonts[fontName];
2720 for (var i = 0, ii = font.length; i < ii; i++) {
2722 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
2729 paper.print = function (x, y, string, font, size) {
2730 var out = this.set(),
2731 letters = (string + "").split(""),
2736 scale = (size || 16) / font.face["units-per-em"];
2737 for (var i = 0, ii = letters.length; i < ii; i++) {
2738 var prev = i && font.glyphs[letters[i - 1]] || {},
2739 curr = font.glyphs[letters[i]];
2740 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
2741 curr && curr.d && out.push(this.path({fill: "#000", stroke: "none"}, curr.d).translate(shift, 0));
2743 out.scale(scale, scale, 0, y).translate(x, (size || 16) / 2);
2748 R.ninja = function () {
2749 var r = window.Raphael;
2750 if (oldRaphael.was) {
2751 window.Raphael = oldRaphael.is;
2754 delete window.Raphael;
2756 window.Raphael = void(0);
2761 R.el = Element.prototype;