2 * Raphael 0.7.dev - JavaScript Vector Library
4 * Copyright (c) 2008 – 2009 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
9 var Raphael = (function () {
12 return create.apply(R, arguments);
14 R.version = "0.7.dev";
15 R.type = (window.SVGAngle ? "SVG" : "VML");
16 R.svg = !(R.vml = R.type == "VML");
20 var availableAttrs = {cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '16px "Arial"', "font-family": '"Arial"', "font-size": "16", gradient: 0, height: 0, opacity: 1, path: "M0,0", r: 0, rotation: 0, rx: 0, ry: 0, scale: "1 1", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, translation: "0 0", width: 0, x: 0, y: 0},
21 availableAnimAttrs = {cx: "number", cy: "number", fill: "colour", "fill-opacity": "number", "font-size": "number", height: "number", opacity: "number", path: "path", r: "number", rotation: "number", rx: "number", ry: "number", scale: "csv", stroke: "colour", "stroke-opacity": "number", "stroke-width": "number", translation: "csv", width: "number", x: "number", y: "number"};
23 R.toString = function () {
24 return "Your browser " + (this.vml ? "doesn't ": "") + "support" + (this.svg ? "s": "") +
25 " SVG.\nYou are running " + unescape("Rapha%EBl%20") + this.version;
28 var hsb2rgb = function (hue, saturation, brightness) {
29 if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
37 if (brightness == 0) {
38 return {r: 0, g: 0, b: 0, hex: "#000"};
40 if (hue > 1 || saturation > 1 || brightness > 1) {
45 var i = Math.floor(hue * 6),
47 p = brightness * (1 - saturation),
48 q = brightness * (1 - (saturation * f)),
49 t = brightness * (1 - (saturation * (1 - f)));
50 red = [brightness, q, p, p, t, brightness, brightness][i];
51 green = [t, brightness, brightness, q, p, p, t][i];
52 blue = [p, p, t, brightness, brightness, q, p][i];
53 var rgb = {r: red, g: green, b: blue};
57 var r = Math.round(red).toString(16);
61 var g = Math.round(green).toString(16);
65 var b = Math.round(blue).toString(16);
69 rgb.hex = "#" + r + g + b;
72 var rgb2hsb = function (red, green, blue) {
73 if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
78 if (typeof red == "string" && red.charAt(0) == "#") {
79 if (red.length == 4) {
80 blue = parseInt(red.substring(3), 16);
81 green = parseInt(red.substring(2, 3), 16);
82 red = parseInt(red.substring(1, 2), 16);
84 blue = parseInt(red.substring(5), 16);
85 green = parseInt(red.substring(3, 5), 16);
86 red = parseInt(red.substring(1, 3), 16);
89 if (red > 1 || green > 1 || blue > 1) {
94 var max = Math.max(red, green, blue),
95 min = Math.min(red, green, blue),
100 return {h: 0, s: 0, b: max};
102 var delta = (max - min);
103 saturation = delta / max;
105 hue = (green - blue) / delta;
106 } else if (green == max) {
107 hue = 2 + ((blue - red) / delta);
109 hue = 4 + ((red - green) / delta);
119 return {h: hue, s: saturation, b: brightness};
121 var getRGB = function (colour) {
123 return {r: 0, g: 0, b: 0, hex: "#000"};
125 if (colour == "none") {
126 return {r: -1, g: -1, b: -1, hex: "none"};
128 var red, green, blue,
129 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);
132 blue = parseInt(rgb[2].substring(5), 16);
133 green = parseInt(rgb[2].substring(3, 5), 16);
134 red = parseInt(rgb[2].substring(1, 3), 16);
137 blue = parseInt(rgb[3].substring(3) + rgb[3].substring(3), 16);
138 green = parseInt(rgb[3].substring(2, 3) + rgb[3].substring(2, 3), 16);
139 red = parseInt(rgb[3].substring(1, 2) + rgb[3].substring(1, 2), 16);
142 rgb = rgb[4].split(/\s*,\s*/);
143 red = parseFloat(rgb[0], 10);
144 green = parseFloat(rgb[1], 10);
145 blue = parseFloat(rgb[2], 10);
148 rgb = rgb[5].split(/\s*,\s*/);
149 red = parseFloat(rgb[0], 10) * 2.55;
150 green = parseFloat(rgb[1], 10) * 2.55;
151 blue = parseFloat(rgb[2], 10) * 2.55;
154 rgb = rgb[6].split(/\s*,\s*/);
155 red = parseFloat(rgb[0], 10);
156 green = parseFloat(rgb[1], 10);
157 blue = parseFloat(rgb[2], 10);
158 return hsb2rgb(red, green, blue);
161 rgb = rgb[7].split(/\s*,\s*/);
162 red = parseFloat(rgb[0], 10) * 2.55;
163 green = parseFloat(rgb[1], 10) * 2.55;
164 blue = parseFloat(rgb[2], 10) * 2.55;
165 return hsb2rgb(red, green, blue);
167 var rgb = {r: red, g: green, b: blue};
168 var r = Math.round(red).toString(16);
169 (r.length == 1) && (r = "0" + r);
170 var g = Math.round(green).toString(16);
171 (g.length == 1) && (g = "0" + g);
172 var b = Math.round(blue).toString(16);
173 (b.length == 1) && (b = "0" + b);
174 rgb.hex = "#" + r + g + b;
177 return {r: -1, g: -1, b: -1, hex: "none"};
180 R.getColor = function (value) {
181 var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
182 var rgb = hsb2rgb(start.h, start.s, start.b);
188 arguments.callee.start = {h: 0, s: 1, b: start.b};
193 R.getColor.reset = function () {
194 this.start = undefined;
197 R.parsePathString = function (pathString) {
198 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
200 toString = function () {
202 for (var i = 0, ii = this.length; i < ii; i++) {
203 res += this[i][0] + this[i].join(",").substring(2);
207 if (pathString.toString.toString() == toString.toString()) {
210 pathString.replace(/([achlmqstvz])[\s,]*((-?\d*\.?\d*\s*,?\s*)+)/ig, function (a, b, c) {
211 var params = [], name = b.toLowerCase();
212 c.replace(/(-?\d*\.?\d*)\s*,?\s*/ig, function (a, b) {
213 b && params.push(+b);
215 while (params.length >= paramCounts[name]) {
216 data.push([b].concat(params.splice(0, paramCounts[name])));
217 if (!paramCounts[name]) {
222 data.toString = toString;
225 var pathDimensions = function (path) {
226 var pathArray = path;
227 if (typeof path == "string") {
228 pathArray = Raphael.parsePathString(path);
230 pathArray = pathToAbsolute(pathArray);
231 var x = [], y = [], length = 0;
232 for (var i = 0, ii = pathArray.length; i < ii; i++) {
233 switch (pathArray[i][0]) {
237 x.push(pathArray[i][pathArray[i].length - 2]);
238 y.push(pathArray[i][pathArray[i].length - 1]);
241 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
243 x.push(pathArray[i][j]);
245 y.push(pathArray[i][j]);
250 var minx = Math.min.apply(Math, x),
251 miny = Math.min.apply(Math, y);
255 width: Math.max.apply(Math, x) - minx,
256 height: Math.max.apply(Math, y) - miny,
261 var pathToRelative = function (pathArray) {
263 if (typeof pathArray == "string") {
264 pathArray = this.parsePathString(pathArray);
266 var x = 0, y = 0, start = 0;
267 if (pathArray[0][0] == "M") {
271 res.push(pathArray[0]);
273 for (var i = start, ii = pathArray.length; i < ii; i++) {
275 if (pathArray[i][0] != pathArray[i][0].toLowerCase()) {
276 res[i][0] = pathArray[i][0].toLowerCase();
279 res[i][1] = pathArray[i][1];
280 res[i][2] = pathArray[i][2];
282 res[i][4] = pathArray[i][4];
283 res[i][5] = pathArray[i][5];
284 res[i][6] = +(pathArray[i][6] - x).toFixed(3);
285 res[i][7] = +(pathArray[i][7] - y).toFixed(3);
288 res[i][1] = +(pathArray[i][1] - y).toFixed(3);
291 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
292 res[i][j] = +(pathArray[i][j] - ((j % 2) ? x : y)).toFixed(3);
296 res[i] = pathArray[i];
302 x += res[i][res[i].length - 1];
305 y += res[i][res[i].length - 1];
308 x += res[i][res[i].length - 2];
309 y += res[i][res[i].length - 1];
312 res.toString = pathArray.toString;
315 var pathToAbsolute = function (pathArray) {
317 if (typeof pathArray == "string") {
318 pathArray = R.parsePathString(pathArray);
320 var x = 0, y = 0, start = 0;
321 if (pathArray[0][0] == "M") {
322 x = +pathArray[0][1];
323 y = +pathArray[0][2];
325 res[0] = pathArray[0];
327 for (var i = start, ii = pathArray.length; i < ii; i++) {
329 if (pathArray[i][0] != pathArray[i][0].toUpperCase()) {
330 res[i][0] = pathArray[i][0].toUpperCase();
333 res[i][1] = pathArray[i][1];
334 res[i][2] = pathArray[i][2];
336 res[i][4] = pathArray[i][4];
337 res[i][5] = pathArray[i][5];
338 res[i][6] = +(pathArray[i][6] + x).toFixed(3);
339 res[i][7] = +(pathArray[i][7] + y).toFixed(3);
342 res[i][1] = +pathArray[i][1] + y;
345 for (var j = 1, jj = pathArray[i].length; j < jj; j++) {
346 res[i][j] = +pathArray[i][j] + ((j % 2) ? x : y);
350 res[i] = pathArray[i];
362 x = res[i][res[i].length - 2];
363 y = res[i][res[i].length - 1];
366 res.toString = pathArray.toString;
369 var pathEqualiser = function (path1, path2) {
370 var data = [pathToAbsolute(this.parsePathString(path1)), pathToAbsolute(this.parsePathString(path2))],
371 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}],
372 processPath = function (path, d) {
382 var nx = d.x + (d.x - (d.bx || d.x));
383 var ny = d.y + (d.y - (d.by || d.y));
384 path = ["C", nx, ny, path[1], path[2], path[3], path[4]];
387 var nx = d.x + (d.x - (d.bx || d.x));
388 var ny = d.y + (d.y - (d.by || d.y));
389 path = ["Q", nx, ny, path[1], path[2]];
392 path = ["L", path[1], d.y];
395 path = ["L", d.x, path[1]];
398 path = ["L", d.X, d.Y];
403 edgeCases = function (a, b, i) {
404 if (data[a][i][0] == "M" && data[b][i][0] != "M") {
405 data[b].splice(i, 0, ["M", attrs[b].x, attrs[b].y]);
406 attrs[a].bx = data[a][i][data[a][i].length - 4] || 0;
407 attrs[a].by = data[a][i][data[a][i].length - 3] || 0;
408 attrs[a].x = data[a][i][data[a][i].length - 2];
409 attrs[a].y = data[a][i][data[a][i].length - 1];
411 } else if (data[a][i][0] == "L" && data[b][i][0] == "C") {
412 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]];
413 } else if (data[a][i][0] == "L" && data[b][i][0] == "Q") {
414 data[a][i] = ["Q", data[a][i][1], data[a][i][2], data[a][i][1], data[a][i][2]];
415 } else if (data[a][i][0] == "Q" && data[b][i][0] == "C") {
416 var x = data[b][i][data[b][i].length - 2];
417 var y = data[b][i][data[b][i].length - 1];
418 data[b].splice(i + 1, 0, ["Q", x, y, x, y]);
419 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
421 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
422 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
423 attrs[b].x = data[b][i][data[b][i].length - 2];
424 attrs[b].y = data[b][i][data[b][i].length - 1];
426 } else if (data[a][i][0] == "A" && data[b][i][0] == "C") {
427 var x = data[b][i][data[b][i].length - 2];
428 var y = data[b][i][data[b][i].length - 1];
429 data[b].splice(i + 1, 0, ["A", 0, 0, data[a][i][3], data[a][i][4], data[a][i][5], x, y]);
430 data[a].splice(i, 0, ["C", attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y, attrs[a].x, attrs[a].y]);
432 attrs[b].bx = data[b][i][data[b][i].length - 4] || 0;
433 attrs[b].by = data[b][i][data[b][i].length - 3] || 0;
434 attrs[b].x = data[b][i][data[b][i].length - 2];
435 attrs[b].y = data[b][i][data[b][i].length - 1];
437 } else if (data[a][i][0] == "U") {
438 data[a][i][0] = data[b][i][0];
439 for (var j = 1, jj = data[b][i].length; j < jj; j++) {
440 data[a][i][j] = (j % 2) ? attrs[a].x : attrs[a].y;
445 for (var i = 0; i < Math.max(data[0].length, data[1].length); i++) {
446 data[0][i] = processPath(data[0][i], attrs[0]);
447 data[1][i] = processPath(data[1][i], attrs[1]);
448 if (data[0][i][0] != data[1][i][0] && (edgeCases(0, 1, i) || edgeCases(1, 0, i))) {
451 attrs[0].bx = data[0][i][data[0][i].length - 4] || 0;
452 attrs[0].by = data[0][i][data[0][i].length - 3] || 0;
453 attrs[0].x = data[0][i][data[0][i].length - 2];
454 attrs[0].y = data[0][i][data[0][i].length - 1];
455 attrs[1].bx = data[1][i][data[1][i].length - 4] || 0;
456 attrs[1].by = data[1][i][data[1][i].length - 3] || 0;
457 attrs[1].x = data[1][i][data[1][i].length - 2];
458 attrs[1].y = data[1][i][data[1][i].length - 1];
465 var thePath = function (params, pathString, SVG) {
466 var el = document.createElementNS(SVG.svgns, "path");
467 el.setAttribute("fill", "none");
469 SVG.canvas.appendChild(el);
471 var p = new Element(el, SVG);
474 p.last = {x: 0, y: 0, bx: 0, by: 0};
475 p.absolutely = function () {
476 this.isAbsolute = true;
479 p.relatively = function () {
480 this.isAbsolute = false;
483 p.moveTo = function (x, y) {
484 var d = this.isAbsolute?"M":"m";
485 d += parseFloat(x, 10).toFixed(3) + " " + parseFloat(y, 10).toFixed(3) + " ";
486 var oldD = this[0].getAttribute("d") || "";
487 (oldD == "M0,0") && (oldD = "");
488 this[0].setAttribute("d", oldD + d);
489 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
490 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
491 this.attrs.path = oldD + d;
494 p.lineTo = function (x, y) {
495 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
496 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
497 var d = this.isAbsolute?"L":"l";
498 d += parseFloat(x, 10).toFixed(3) + " " + parseFloat(y, 10).toFixed(3) + " ";
499 var oldD = this[0].getAttribute("d") || "";
500 this[0].setAttribute("d", oldD + d);
501 this.attrs.path = oldD + d;
504 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
505 var d = this.isAbsolute ? "A" : "a";
506 d += [parseFloat(rx, 10).toFixed(3), parseFloat(ry, 10).toFixed(3), 0, large_arc_flag, sweep_flag, parseFloat(x, 10).toFixed(3), parseFloat(y, 10).toFixed(3)].join(" ");
507 var oldD = this[0].getAttribute("d") || "";
508 this[0].setAttribute("d", oldD + d);
509 this.last.x = parseFloat(x, 10);
510 this.last.y = parseFloat(y, 10);
511 this.attrs.path = oldD + d;
514 p.cplineTo = function (x1, y1, w1) {
516 return this.lineTo(x1, y1);
519 var x = parseFloat(x1, 10);
520 var y = parseFloat(y1, 10);
521 var w = parseFloat(w1, 10);
522 var d = this.isAbsolute?"C":"c";
523 var attr = [+this.last.x + w, +this.last.y, x - w, y, x, y];
524 for (var i = 0, ii = attr.length; i < ii; i++) {
525 d += attr[i].toFixed(3) + " ";
527 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
528 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
529 this.last.bx = attr[2];
530 this.last.by = attr[3];
531 var oldD = this[0].getAttribute("d") || "";
532 this[0].setAttribute("d", oldD + d);
533 this.attrs.path = oldD + d;
537 p.curveTo = function () {
539 command = [0, 1, 2, 3, "s", 5, "c"];
541 var d = command[arguments.length];
542 if (this.isAbsolute) {
545 for (var i = 0, ii = arguments.length; i < ii; i++) {
546 d += parseFloat(arguments[i], 10).toFixed(3) + " ";
548 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2], 10);
549 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1], 10);
550 this.last.bx = parseFloat(arguments[arguments.length - 4], 10);
551 this.last.by = parseFloat(arguments[arguments.length - 3], 10);
552 var oldD = this.node.getAttribute("d") || "";
553 this.node.setAttribute("d", oldD + d);
554 this.attrs.path = oldD + d;
557 p.qcurveTo = function () {
559 command = [0, 1, "t", 3, "q"];
561 var d = command[arguments.length];
562 if (this.isAbsolute) {
565 for (var i = 0, ii = arguments.length; i < ii; i++) {
566 d += parseFloat(arguments[i], 10).toFixed(3) + " ";
568 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[arguments.length - 2], 10);
569 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[arguments.length - 1], 10);
570 if (arguments.length != 2) {
571 this.last.qx = parseFloat(arguments[arguments.length - 4], 10);
572 this.last.qy = parseFloat(arguments[arguments.length - 3], 10);
574 var oldD = this.node.getAttribute("d") || "";
575 this.node.setAttribute("d", oldD + d);
576 this.attrs.path = oldD + d;
579 p.addRoundedCorner = function (r, dir) {
580 var R = .5522 * r, rollback = this.isAbsolute, o = this;
583 rollback = function () {
587 rollback = function () {};
593 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
596 o.curveTo(-R, 0, -r, r - R, -r, r);
603 o.curveTo(R, 0, r, -(r - R), r, -r);
606 o.curveTo(R, 0, r, r - R, r, r);
613 o.curveTo(0, -R, -(R - r), -r, r, -r);
616 o.curveTo(0, -R, R - r, -r, -r, -r);
623 o.curveTo(0, R, -(R - r), r, r, r);
626 o.curveTo(0, R, R - r, r, -r, r);
631 actions[dir[0]]()[dir[1]]();
635 p.andClose = function () {
636 var oldD = this[0].getAttribute("d") || "";
637 this[0].setAttribute("d", oldD + "Z ");
638 this.attrs.path = oldD + "Z ";
642 p.attrs.path = "" + pathString;
644 paper.pathfinder(p, p.attrs.path);
647 setFillAndStroke(p, params);
651 var addGrdientFill = function (o, gradient, SVG) {
652 var el = document.createElementNS(SVG.svgns, gradient.type + "Gradient");
653 el.id = "raphael-gradient-" + Raphael.idGenerator++;
654 if (gradient.vector && gradient.vector.length) {
655 el.setAttribute("x1", gradient.vector[0]);
656 el.setAttribute("y1", gradient.vector[1]);
657 el.setAttribute("x2", gradient.vector[2]);
658 el.setAttribute("y2", gradient.vector[3]);
660 SVG.defs.appendChild(el);
661 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
662 var stop = document.createElementNS(SVG.svgns, "stop");
663 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
664 stop.setAttribute("stop-color", getRGB(gradient.dots[i].color).hex || "#fff");
665 if (typeof gradient.dots[i].opacity != "undefined") {
666 stop.setAttribute("stop-opacity", gradient.dots[i].opacity);
668 el.appendChild(stop);
670 o.setAttribute("fill", "url(#" + el.id + ")");
672 var updatePosition = function (o) {
674 var bbox = o.node.getBBox();
675 o.pattern.setAttribute("patternTransform", "translate(" + [bbox.x, bbox.y].join(",") + ")");
678 var setFillAndStroke = function (o, params) {
683 "-..": [3, 1, 1, 1, 1, 1],
689 "--..": [8, 3, 1, 3, 1, 3]
691 addDashes = function (o, value) {
692 value = dasharray[value.toString().toLowerCase()];
694 var width = o.attrs["stroke-width"] || "1",
695 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
697 for (var i = 0, ii = value.length; i < ii; i++) {
698 dashes.push(value[i] * width + ((i % 2) ? 1 : -1) * butt);
700 value = dashes.join(",");
701 o.node.setAttribute("stroke-dasharray", value);
704 for (var att in params) {
705 var value = params[att];
706 o.attrs[att] = value;
709 if (o.type == "path") {
710 o.node.setAttribute("d", "M0,0");
711 paper.pathfinder(o, value);
716 o.node.setAttribute(att, value);
722 o.node.setAttribute(att, value);
726 o.node.setAttribute(att, value);
729 o.node.setAttribute(att, value);
732 addGrdientFill(o.node, value, o.svg);
735 o.node.style.strokeWidth = value;
736 // Need following line for Firefox
737 o.node.setAttribute(att, value);
738 if (o.attrs["stroke-dasharray"]) {
739 addDashes(o, o.attrs["stroke-dasharray"]);
742 case "stroke-dasharray":
746 if (o.type == "text") {
747 o.node.childNodes.length && o.node.removeChild(o.node.firstChild);
748 o.node.appendChild(document.createTextNode(value));
752 o.rotate(value, true);
755 var xy = value.split(/[, ]+/);
756 o.translate(xy[0], xy[1]);
759 var xy = value.split(/[, ]+/);
760 o.scale(xy[0], xy[1]);
763 var isURL = value.match(/^url\(([^\)]+)\)$/i);
765 var el = document.createElementNS(o.svg.svgns, "pattern");
766 var ig = document.createElementNS(o.svg.svgns, "image");
767 el.id = "raphael-pattern-" + Raphael.idGenerator++;
768 el.setAttribute("x", 0);
769 el.setAttribute("y", 0);
770 el.setAttribute("patternUnits", "userSpaceOnUse");
771 ig.setAttribute("x", 0);
772 ig.setAttribute("y", 0);
773 ig.setAttributeNS(o.svg.xlink, "href", isURL[1]);
776 var img = document.createElement("img");
777 img.style.position = "absolute";
778 img.style.top = "-9999em";
779 img.style.left = "-9999em";
780 img.onload = function () {
781 el.setAttribute("width", this.offsetWidth);
782 el.setAttribute("height", this.offsetHeight);
783 ig.setAttribute("width", this.offsetWidth);
784 ig.setAttribute("height", this.offsetHeight);
785 document.body.removeChild(this);
788 document.body.appendChild(img);
790 o.svg.defs.appendChild(el);
791 o.node.style.fill = "url(#" + el.id + ")";
792 o.node.setAttribute("fill", "url(#" + el.id + ")");
798 o.node.style[att] = getRGB(value).hex;
799 // Need following line for Firefox
800 o.node.setAttribute(att, getRGB(value).hex);
803 var cssrule = att.replace(/(\-.)/g, function (w) {
804 return w.substring(1).toUpperCase();
806 o.node.style[cssrule] = value;
807 // Need following line for Firefox
808 o.node.setAttribute(att, value);
813 var Element = function (node, svg) {
819 this.attrs = this.attrs || {};
820 this.transformations = []; // rotate, translate, scale
824 rt: {deg: 0, x: 0, y: 0},
829 Element.prototype.translate = function (x, y) {
830 if (x == undefined && y == undefined) {
831 return {x: this._.tx, y: this._.ty};
838 this.attr({cx: this.attrs.cx + x, cy: this.attrs.cy + y});
843 this.attr({x: this.attrs.x + x, y: this.attrs.y + y});
846 var path = pathToRelative(this.attrs.path);
849 this.attr({path: path.join(" ")});
854 Element.prototype.rotate = function (deg, isAbsolute, cx, cy) {
855 if (deg == undefined) {
856 return this._.rt.deg;
858 var bbox = this.getBBox();
862 this._.rt.deg += deg;
865 cx = cx || bbox.x + bbox.width / 2;
866 cy = cy || bbox.y + bbox.height / 2;
868 this.transformations[0] = ("rotate(" + this._.rt.deg + " " + cx + " " + cy + ")");
870 this.transformations[0] = "";
872 this.node.setAttribute("transform", this.transformations.join(" "));
875 Element.prototype.hide = function () {
876 this.node.style.display = "none";
879 Element.prototype.show = function () {
880 this.node.style.display = "block";
883 Element.prototype.remove = function () {
884 this.node.parentNode.removeChild(this.node);
886 Element.prototype.getBBox = function () {
887 return this.node.getBBox();
889 Element.prototype.attr = function () {
890 if (arguments.length == 1 && typeof arguments[0] == "string") {
891 if (arguments[0] == "translation") {
892 return this.translate();
894 return this.attrs[arguments[0]];
896 if (arguments.length == 1 && arguments[0] instanceof Array) {
898 for (var j in arguments[0]) {
899 values[arguments[0][j]] = this.attrs[arguments[0][j]];
903 if (arguments.length == 2) {
905 params[arguments[0]] = arguments[1];
906 setFillAndStroke(this, params);
907 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
908 setFillAndStroke(this, arguments[0]);
912 Element.prototype.toFront = function () {
913 this.node.parentNode.appendChild(this.node);
916 Element.prototype.toBack = function () {
917 if (this.node.parentNode.firstChild != this.node) {
918 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
922 Element.prototype.insertAfter = function (element) {
923 if (element.node.nextSibling) {
924 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
926 element.node.parentNode.appendChild(this.node);
930 Element.prototype.insertBefore = function (element) {
931 element.node.parentNode.insertBefore(this.node, element.node);
934 var theCircle = function (svg, x, y, r) {
935 var el = document.createElementNS(svg.svgns, "circle");
936 el.setAttribute("cx", x);
937 el.setAttribute("cy", y);
938 el.setAttribute("r", r);
939 el.setAttribute("fill", "none");
940 el.setAttribute("stroke", "#000");
942 svg.canvas.appendChild(el);
944 var res = new Element(el, svg);
945 res.attrs = res.attrs || {};
949 res.attrs.stroke = "#000";
953 var theRect = function (svg, x, y, w, h, r) {
954 var el = document.createElementNS(svg.svgns, "rect");
955 el.setAttribute("x", x);
956 el.setAttribute("y", y);
957 el.setAttribute("width", w);
958 el.setAttribute("height", h);
960 el.setAttribute("rx", r);
961 el.setAttribute("ry", r);
963 el.setAttribute("fill", "none");
964 el.setAttribute("stroke", "#000");
966 svg.canvas.appendChild(el);
968 var res = new Element(el, svg);
969 res.attrs = res.attrs || {};
973 res.attrs.height = h;
974 res.attrs.stroke = "#000";
976 res.attrs.rx = res.attrs.ry = r;
981 var theEllipse = function (svg, x, y, rx, ry) {
982 var el = document.createElementNS(svg.svgns, "ellipse");
983 el.setAttribute("cx", x);
984 el.setAttribute("cy", y);
985 el.setAttribute("rx", rx);
986 el.setAttribute("ry", ry);
987 el.setAttribute("fill", "none");
988 el.setAttribute("stroke", "#000");
990 svg.canvas.appendChild(el);
992 var res = new Element(el, svg);
993 res.attrs = res.attrs || {};
998 res.attrs.stroke = "#000";
999 res.type = "ellipse";
1002 var theImage = function (svg, src, x, y, w, h) {
1003 var el = document.createElementNS(svg.svgns, "image");
1004 el.setAttribute("x", x);
1005 el.setAttribute("y", y);
1006 el.setAttribute("width", w);
1007 el.setAttribute("height", h);
1008 el.setAttribute("preserveAspectRatio", "none");
1009 el.setAttributeNS(svg.xlink, "href", src);
1011 svg.canvas.appendChild(el);
1013 var res = new Element(el, svg);
1014 res.attrs = res.attrs || {};
1017 res.attrs.width = w;
1018 res.attrs.height = h;
1022 var theText = function (svg, x, y, text) {
1023 var el = document.createElementNS(svg.svgns, "text");
1024 el.setAttribute("x", x);
1025 el.setAttribute("y", y);
1026 el.setAttribute("text-anchor", "middle");
1028 el.appendChild(document.createTextNode(text));
1031 svg.canvas.appendChild(el);
1033 var res = new Element(el, svg);
1034 res.attrs = res.attrs || {};
1038 setFillAndStroke(res, {font: '10px "Arial"', stroke: "none", fill: "#000"});
1041 var theGroup = function (svg) {
1042 var el = document.createElementNS(svg.svgns, "g");
1044 svg.canvas.appendChild(el);
1046 var i = new Element(el, svg);
1047 for (var f in svg) {
1048 if (f[0] != "_" && typeof svg[f] == "function") {
1049 i[f] = (function (f) {
1050 return function () {
1051 var e = svg[f].apply(svg, arguments);
1052 el.appendChild(e[0]);
1061 create = function () {
1062 // container, width, height
1063 // x, y, width, height
1064 if (typeof arguments[0] == "string") {
1065 var container = document.getElementById(arguments[0]);
1066 var width = arguments[1];
1067 var height = arguments[2];
1069 if (typeof arguments[0] == "object") {
1070 var container = arguments[0];
1071 var width = arguments[1];
1072 var height = arguments[2];
1074 if (typeof arguments[0] == "number") {
1078 width = arguments[2],
1079 height = arguments[3];
1082 throw new Error("SVG container not found.");
1084 paper.canvas = document.createElementNS(paper.svgns, "svg");
1085 paper.canvas.setAttribute("width", width || 320);
1086 paper.width = width || 320;
1087 paper.canvas.setAttribute("height", height || 200);
1088 paper.height = height || 200;
1089 if (container == 1) {
1090 document.body.appendChild(paper.canvas);
1091 paper.canvas.style.position = "absolute";
1092 paper.canvas.style.left = x + "px";
1093 paper.canvas.style.top = y + "px";
1095 if (container.firstChild) {
1096 container.insertBefore(paper.canvas, container.firstChild);
1098 container.appendChild(paper.canvas);
1102 canvas: paper.canvas,
1103 clear: function () {
1104 while (this.canvas.firstChild) {
1105 this.canvas.removeChild(this.canvas.firstChild);
1107 this.defs = document.createElementNS(paper.svgns, "defs");
1108 this.canvas.appendChild(this.defs);
1111 for (var prop in paper) {
1112 if (prop != "create") {
1113 container[prop] = paper[prop];
1116 for (var prop in R.fn) {
1117 if (!container[prop]) {
1118 container[prop] = R.fn[prop];
1124 paper.remove = function () {
1125 this.canvas.parentNode.removeChild(this.canvas);
1127 paper.svgns = "http://www.w3.org/2000/svg";
1128 paper.xlink = "http://www.w3.org/1999/xlink";
1129 paper.safari = function () {
1130 if (navigator.vendor == "Apple Computer, Inc.") {
1131 var rect = this.rect(-this.width, -this.height, this.width * 3, this.height * 3).attr({stroke: "none"});
1132 setTimeout(function () {rect.remove();}, 0);
1139 thePath = function (params, pathString, VML) {
1140 var g = document.createElement("rvml:group"), gl = g.style;
1141 gl.position = "absolute";
1144 gl.width = VML.width + "px";
1145 gl.height = VML.height + "px";
1146 var el = document.createElement("rvml:shape"), ol = el.style;
1147 ol.width = VML.width + "px";
1148 ol.height = VML.height + "px";
1150 if (params["class"]) {
1151 el.className = params["class"];
1153 el.coordsize = this.coordsize;
1154 el.coordorigin = this.coordorigin;
1156 VML.canvas.appendChild(g);
1157 var p = new Element(el, g, VML);
1158 p.isAbsolute = true;
1161 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
1163 p.absolutely = function () {
1164 this.isAbsolute = true;
1167 p.relatively = function () {
1168 this.isAbsolute = false;
1171 p.moveTo = function (x, y) {
1172 var d = this.isAbsolute?"m":"t";
1173 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1174 this.node.path = this.Path += d;
1175 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1176 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1177 this.last.isAbsolute = this.isAbsolute;
1178 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
1181 p.lineTo = function (x, y) {
1182 var d = this.isAbsolute?"l":"r";
1183 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
1184 this[0].path = this.Path += d;
1185 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
1186 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
1187 this.last.isAbsolute = this.isAbsolute;
1188 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
1191 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
1192 // for more information of where this math came from visit:
1193 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1194 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
1195 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
1196 var x1 = this.last.x,
1200 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1201 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
1202 cx = k * rx * y / ry + (x1 + x2) / 2,
1203 cy = k * -ry * x / rx + (y1 + y2) / 2,
1204 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
1205 left = Math.round(cx - rx),
1206 top = Math.round(cy - ry);
1207 d += [left, top, Math.round(left + rx * 2), Math.round(top + ry * 2), Math.round(x1), Math.round(y1), Math.round(parseFloat(x2, 10)), Math.round(parseFloat(y2, 10))].join(", ");
1208 this.node.path = this.Path += d;
1209 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2, 10);
1210 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2, 10);
1211 this.last.isAbsolute = this.isAbsolute;
1212 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
1215 p.cplineTo = function (x1, y1, w1) {
1217 return this.lineTo(x1, y1);
1219 var x = Math.round(Math.round(parseFloat(x1, 10) * 100) / 100),
1220 y = Math.round(Math.round(parseFloat(y1, 10) * 100) / 100),
1221 w = Math.round(Math.round(parseFloat(w1, 10) * 100) / 100),
1222 d = this.isAbsolute ? "c" : "v",
1223 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
1224 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
1225 d += attr.join(" ") + " ";
1226 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
1227 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
1228 this.last.bx = attr[2];
1229 this.last.by = attr[3];
1230 this.node.path = this.Path += d;
1231 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
1235 p.curveTo = function () {
1236 var d = this.isAbsolute ? "c" : "v";
1237 if (arguments.length == 6) {
1238 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1239 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1240 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4], 10);
1241 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5], 10);
1242 d += [Math.round(parseFloat(arguments[0], 10)),
1243 Math.round(parseFloat(arguments[1], 10)),
1244 Math.round(parseFloat(arguments[2], 10)),
1245 Math.round(parseFloat(arguments[3], 10)),
1246 Math.round(parseFloat(arguments[4], 10)),
1247 Math.round(parseFloat(arguments[5], 10))].join(" ") + " ";
1248 this.last.isAbsolute = this.isAbsolute;
1249 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
1251 if (arguments.length == 4) {
1252 var bx = this.last.x * 2 - this.last.bx;
1253 var by = this.last.y * 2 - this.last.by;
1254 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1255 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1256 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1257 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1258 d += [Math.round(bx), Math.round(by),
1259 Math.round(parseFloat(arguments[0], 10)),
1260 Math.round(parseFloat(arguments[1], 10)),
1261 Math.round(parseFloat(arguments[2], 10)),
1262 Math.round(parseFloat(arguments[3], 10))].join(" ") + " ";
1263 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
1265 this.node.path = this.Path += d;
1268 p.qcurveTo = function () {
1270 if (arguments.length == 4) {
1271 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
1272 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
1273 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1274 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1275 d += [Math.round(this.last.qx),
1276 Math.round(this.last.qy),
1277 Math.round(this.last.x),
1278 Math.round(this.last.y)].join(" ") + " ";
1279 this.last.isAbsolute = this.isAbsolute;
1280 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
1282 if (arguments.length == 2) {
1283 this.last.qx = this.last.x * 2 - this.last.qx;
1284 this.last.qy = this.last.y * 2 - this.last.qy;
1285 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
1286 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
1287 d += [Math.round(this.last.qx),
1288 Math.round(this.last.qy),
1289 Math.round(this.last.x),
1290 Math.round(this.last.y)].join(" ") + " ";
1291 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
1293 this.node.path = this.Path += d;
1294 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
1297 p.addRoundedCorner = function (r, dir) {
1298 var R = .5522 * r, rollback = this.isAbsolute, o = this;
1301 rollback = function () {
1305 rollback = function () {};
1311 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
1314 o.curveTo(-R, 0, -r, r - R, -r, r);
1321 o.curveTo(R, 0, r, -(r - R), r, -r);
1324 o.curveTo(R, 0, r, r - R, r, r);
1331 o.curveTo(0, -R, -(R - r), -r, r, -r);
1334 o.curveTo(0, -R, R - r, -r, -r, -r);
1341 o.curveTo(0, R, -(R - r), r, r, r);
1344 o.curveTo(0, R, R - r, r, -r, r);
1349 actions[dir.charAt(0)]()[dir.charAt(1)]();
1353 p.andClose = function () {
1354 this.node.path = (this.Path += "x e");
1355 this.attrs.path += "z";
1361 paper.pathfinder(p, "" + pathString);
1364 setFillAndStroke(p, params);
1365 if (params.gradient) {
1366 addGrdientFill(p, params.gradient);
1370 var setFillAndStroke = function (o, params) {
1371 var s = o.node.style,
1373 o.attrs = o.attrs || {};
1374 for (var par in params) {
1375 o.attrs[par] = params[par];
1377 if (params.path && o.type == "path") {
1380 paper.pathfinder(o, params.path);
1382 if (params.rotation != null) {
1383 o.Group.style.rotation = params.rotation;
1385 if (params.translation) {
1386 var xy = params.translation.split(/[, ]+/);
1387 o.translate(xy[0], xy[1]);
1390 var xy = params.scale.split(/[, ]+/);
1391 o.scale(xy[0], xy[1]);
1393 if (o.type == "image" && params.opacity) {
1394 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
1395 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
1397 params.font && (s.font = params.font);
1398 params["font-family"] && (s.fontFamily = params["font-family"]);
1399 params["font-size"] && (s.fontSize = params["font-size"]);
1400 params["font-weight"] && (s.fontWeight = params["font-weight"]);
1401 params["font-style"] && (s.fontStyle = params["font-style"]);
1402 if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined") {
1403 o = o.shape || o.node;
1404 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || document.createElement("rvml:fill");
1405 if ("fill-opacity" in params || "opacity" in params) {
1406 fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
1411 if (fill.on == undefined || params.fill == "none") {
1414 if (fill.on && params.fill) {
1415 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
1417 fill.src = isURL[1];
1420 fill.color = getRGB(params.fill).hex;
1422 fill.type = "solid";
1425 o.appendChild(fill);
1426 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || document.createElement("rvml:stroke");
1427 if ((params.stroke && params.stroke != "none") || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"]) {
1430 if (params.stroke == "none" || typeof stroke.on == "undefined") {
1433 if (stroke.on && params.stroke) {
1434 stroke.color = getRGB(params.stroke).hex;
1436 stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
1437 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
1438 stroke.miterlimit = params["stroke-miterlimit"] || 8;
1439 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
1440 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12 / 16);
1441 if (params["stroke-dasharray"]) {
1445 "-.": "shortdashdot",
1446 "-..": "shortdashdotdot",
1451 "--.": "longdashdot",
1452 "--..": "longdashdotdot"
1454 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
1456 o.appendChild(stroke);
1458 if (res.type == "text") {
1459 var span = document.createElement("span"),
1461 res.attrs.font && (s.font = res.attrs.font);
1462 res.attrs["font-family"] && (s.fontFamily = res.attrs["font-family"]);
1463 res.attrs["font-size"] && (s.fontSize = res.attrs["font-size"]);
1464 res.attrs["font-weight"] && (s.fontWeight = res.attrs["font-weight"]);
1465 res.attrs["font-style"] && (s.fontStyle = res.attrs["font-style"]);
1466 res.node.parentNode.appendChild(span);
1467 span.innerText = res.node.string;
1468 res.W = res.attrs.w = span.offsetWidth;
1469 res.H = res.attrs.h = span.offsetHeight;
1470 res.X = res.attrs.x - Math.round(res.W / 2);
1471 res.Y = res.attrs.y - Math.round(res.H / 2);
1472 res.node.parentNode.removeChild(span);
1475 var getAngle = function (a, b, c, d) {
1476 var angle = Math.round(Math.atan((parseFloat(c, 10) - parseFloat(a, 10)) / (parseFloat(d, 10) - parseFloat(b, 10))) * 57.29) || 0;
1477 if (!angle && parseFloat(a, 10) < parseFloat(b, 10)) {
1486 var addGrdientFill = function (o, gradient) {
1487 o.attrs = o.attrs || {};
1488 o.attrs.gradient = gradient;
1489 o = o.shape || o[0];
1490 var fill = o.getElementsByTagName("fill");
1494 fill = document.createElement("rvml:fill");
1496 if (gradient.dots.length) {
1498 fill.method = "none";
1499 fill.type = (gradient.type.toLowerCase() == "linear") ? "gradient" : "gradientTitle";
1500 if (typeof gradient.dots[0].color != "undefined") {
1501 fill.color = getRGB(gradient.dots[0].color).hex;
1503 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
1504 fill.color2 = getRGB(gradient.dots[gradient.dots.length - 1].color).hex;
1507 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1508 if (gradient.dots[i].offset) {
1509 colors.push(gradient.dots[i].offset + " " + getRGB(gradient.dots[i].color).hex);
1512 var fillOpacity = typeof gradient.dots[0].opacity == "undefined" ? 1 : gradient.dots[0].opacity;
1513 var fillOpacity2 = typeof gradient.dots[gradient.dots.length - 1].opacity == "undefined" ? 1 : gradient.dots[gradient.dots.length - 1].opacity;
1515 fill.colors.value = colors.join(",");
1516 fillOpacity2 += fillOpacity;
1517 fillOpacity = fillOpacity2 - fillOpacity;
1518 fillOpacity2 -= fillOpacity;
1520 fill.setAttribute("opacity", fillOpacity);
1521 fill.setAttribute("opacity2", fillOpacity2);
1522 if (gradient.vector) {
1523 fill.angle = getAngle.apply(null, gradient.vector);
1525 if (gradient.type.toLowerCase() == "radial") {
1526 fill.focus = "100%";
1527 fill.focusposition = "0.5 0.5";
1531 var Element = function (node, group, vml) {
1551 Element.prototype.rotate = function (deg, isAbsolute, cx, cy) {
1552 if (deg == undefined) {
1560 this.setBox(null, cx, cy);
1561 this.Group.style.rotation = this._.rt;
1564 Element.prototype.setBox = function (params, cx, cy) {
1565 var gs = this.Group.style,
1566 os = (this.shape && this.shape.style) || this.node.style;
1567 for (var i in params) {
1568 this.attrs[i] = params[i];
1570 var attr = this.attrs, x, y, w, h;
1571 switch (this.type) {
1573 x = attr.cx - attr.r;
1574 y = attr.cy - attr.r;
1578 x = attr.cx - attr.rx;
1579 y = attr.cy - attr.ry;
1587 w = attr.width || 0;
1588 h = attr.height || 0;
1591 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("");
1592 x = attr.x - Math.round(this.W / 2);
1593 y = attr.y - this.H / 2;
1598 if (!this.attrs.path) {
1602 h = this.vml.height;
1604 var dim = pathDimensions(this.attrs.path),
1615 h = this.vml.height;
1618 cx = cx || x + w / 2;
1619 cy = cy || y + h / 2;
1620 var left = cx - this.vml.width / 2,
1621 top = cy - this.vml.height / 2;
1622 if (this.type == "path" || this.type == "text") {
1623 gs.left = left + "px";
1624 gs.top = top + "px";
1625 this.X = this.type == "text" ? x : -left;
1626 this.Y = this.type == "text" ? y : -top;
1629 os.left = -left + "px";
1630 os.top = -top + "px";
1631 // var left = Math.round(this.vml.width / 2 - w / 2 - x),
1632 // top = Math.round(this.vml.height / 2 - h / 2 - y);
1633 // gs.left = - left + "px";
1634 // gs.top = - top + "px";
1635 // this.X = this.type == "text" ? x : left;
1636 // this.Y = this.type == "text" ? y : top;
1639 // os.top = top + "px";
1640 // os.left = left + "px";
1642 gs.left = left + "px";
1643 gs.top = top + "px";
1648 gs.width = this.vml.width + "px";
1649 gs.height = this.vml.height + "px";
1650 os.left = x - left + "px";
1651 os.top = y - top + "px";
1652 os.width = w + "px";
1653 os.height = h + "px";
1656 Element.prototype.hide = function () {
1657 this.Group.style.display = "none";
1660 Element.prototype.show = function () {
1661 this.Group.style.display = "block";
1664 Element.prototype.translate = function (x, y) {
1665 if (x == undefined && y == undefined) {
1666 return {x: this._.tx, y: this._.ty};
1670 if (this.type == "path") {
1671 var path = this.attrs.path;
1672 path = pathToRelative(path);
1675 this.attr({path: path.join(" ")});
1677 this.setBox({x: this._.tx, y: this._.ty});
1680 Element.prototype.getBBox = function () {
1688 Element.prototype.remove = function () {
1689 this[0].parentNode.removeChild(this[0]);
1690 this.Group.parentNode.removeChild(this.Group);
1691 this.shape && this.shape.parentNode.removeChild(this.shape);
1693 Element.prototype.attr = function () {
1694 if (arguments.length == 1 && typeof arguments[0] == "string") {
1695 if (arguments[0] == "translation") {
1696 return this.translate();
1698 return this.attrs[arguments[0]];
1700 if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
1702 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
1703 values[arguments[0][i]] = this.attrs[arguments[0][i]];
1707 if (this.node.tagName.toLowerCase() == "group") {
1708 var children = this[0].childNodes;
1709 this.attrs = this.attrs || {};
1710 if (arguments.length == 2) {
1711 this.attrs[arguments[0]] = arguments[1];
1712 } else if (arguments.length == 1 || typeof arguments[0] == "object") {
1713 for (var j in arguments[0]) {
1714 this.attrs[j] = arguments[0][j];
1717 for (var i = 0, ii = children.length; i < ii; i++) {
1718 this.attr.apply(new item(children[i], this[0], this.vml), arguments);
1722 if (arguments.length == 2) {
1724 params[arguments[0]] = arguments[1];
1726 if (arguments.length == 1 && typeof arguments[0] == "object") {
1727 params = arguments[0];
1730 if (params.gradient) {
1731 addGrdientFill(this, params.gradient);
1733 if (params.text && this.type == "text") {
1734 this.node.string = params.text;
1737 this.node.id = params.id;
1739 setFillAndStroke(this, params);
1740 // this.setBox(params);
1745 Element.prototype.toFront = function () {
1746 this.Group.parentNode.appendChild(this.Group);
1749 Element.prototype.toBack = function () {
1750 if (this.Group.parentNode.firstChild != this.Group) {
1751 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
1755 Element.prototype.insertAfter = function (element) {
1756 if (element.Group.nextSibling) {
1757 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
1759 element.Group.parentNode.appendChild(this.Group);
1763 Element.prototype.insertBefore = function (element) {
1764 element.Group.parentNode.insertBefore(this.Group, element.Group);
1767 var theCircle = function (vml, x, y, r) {
1768 var g = document.createElement("rvml:group");
1769 var o = document.createElement("rvml:oval");
1771 vml.canvas.appendChild(g);
1772 var res = new Element(o, g, vml);
1773 res.type = "circle";
1774 setFillAndStroke(res, {stroke: "#000", fill: "none"});
1778 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
1781 var theRect = function (vml, x, y, w, h, r) {
1782 var g = document.createElement("rvml:group");
1783 var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
1785 o.arcsize = r / (Math.min(w, h));
1788 vml.canvas.appendChild(g);
1789 var res = new Element(o, g, vml);
1791 setFillAndStroke(res, {stroke: "#000"});
1797 res.setBox({x: x, y: y, width: w, height: h});
1800 var theEllipse = function (vml, x, y, rx, ry) {
1801 var g = document.createElement("rvml:group");
1802 var o = document.createElement("rvml:oval");
1804 vml.canvas.appendChild(g);
1805 var res = new Element(o, g, vml);
1806 res.type = "ellipse";
1807 setFillAndStroke(res, {stroke: "#000"});
1812 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
1815 var theImage = function (vml, src, x, y, w, h) {
1816 var g = document.createElement("rvml:group");
1817 var o = document.createElement("rvml:image");
1820 vml.canvas.appendChild(g);
1821 var res = new Element(o, g, vml);
1827 res.setBox({x: x, y: y, width: w, height: h});
1830 var theText = function (vml, x, y, text) {
1831 var g = document.createElement("rvml:group"), gs = g.style;
1832 var el = document.createElement("rvml:shape"), ol = el.style;
1833 var path = document.createElement("rvml:path"), ps = path.style;
1834 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
1835 path.textpathok = true;
1836 ol.width = vml.width;
1837 ol.height = vml.height;
1838 gs.position = "absolute";
1841 gs.width = vml.width;
1842 gs.height = vml.height;
1843 var o = document.createElement("rvml:textpath");
1846 o.coordsize = vml.coordsize;
1847 o.coordorigin = vml.coordorigin;
1849 el.appendChild(path);
1851 vml.canvas.appendChild(g);
1852 var res = new Element(o, g, vml);
1854 res.textpath = path;
1860 setFillAndStroke(res, {font: '10px "Arial"', stroke: "none", fill: "#000"});
1863 create = function () {
1864 // container, width, height
1865 // x, y, width, height
1866 var container, width, height;
1867 if (typeof arguments[0] == "string") {
1868 container = document.getElementById(arguments[0]);
1869 width = arguments[1];
1870 height = arguments[2];
1872 if (typeof arguments[0] == "object") {
1873 container = arguments[0];
1874 width = arguments[1];
1875 height = arguments[2];
1877 if (typeof arguments[0] == "number") {
1881 width = arguments[2];
1882 height = arguments[3];
1885 throw new Error("VML container not found.");
1887 if (!document.namespaces["rvml"]) {
1888 document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
1889 document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
1891 var c = document.createElement("div"),
1892 d = document.createElement("div"),
1893 r = paper.canvas = document.createElement("rvml:group"),
1894 cs = c.style, rs = r.style;
1895 paper.width = width;
1896 paper.height = height;
1897 width = width || "320px";
1898 height = height || "200px";
1899 cs.clip = "rect(0 " + width + " " + height + " 0)";
1902 cs.position = "absolute";
1903 rs.position = "absolute";
1904 d.style.position = "relative";
1907 r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
1908 r.coordorigin = "0 0";
1910 var b = document.createElement("rvml:rect"), bs = b.style;
1911 bs.left = bs.top = 0;
1912 bs.width = rs.width;
1913 bs.height = rs.height;
1914 b.filled = b.stroked = "f";
1919 if (container == 1) {
1920 document.body.appendChild(d);
1921 cs.position = "absolute";
1933 cs.width = container.style.width = width;
1934 cs.height = container.style.height = height;
1935 if (container.firstChild) {
1936 container.insertBefore(d, container.firstChild);
1938 container.appendChild(d);
1941 for (var prop in paper) {
1942 container[prop] = paper[prop];
1944 for (var prop in R.fn) {
1945 if (!container[prop]) {
1946 container[prop] = R.fn[prop];
1949 container.clear = function () {
1951 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
1952 if (r.childNodes[i] != b) {
1953 todel.push(r.childNodes[i]);
1956 for (i = 0, ii = todel.length; i < ii; i++) {
1957 r.removeChild(todel[i]);
1962 paper.remove = function () {
1963 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
1965 paper.safari = function () {};
1971 var addEvent = (function () {
1972 if (document.addEventListener) {
1973 return function (obj, type, fn) {
1974 obj.addEventListener(type, fn, false);
1976 } else if (document.attachEvent) {
1977 return function (obj, type, fn) {
1978 var f = function (e) {
1979 fn.call(this, e || window.event);
1981 obj.attachEvent("on" + type, f);
1985 var events = ["click", "dblclick", "mousedown", "mousemove", "mouseout", "mouseover", "mouseup"];
1986 for (var i = events.length; i--;) {
1987 (function (eventName) {
1988 Element.prototype[eventName] = function (fn) {
1989 addEvent(this.node, eventName, fn);
1996 var Set = function (itemsArray) {
1998 if (itemsArray && itemsArray.constructor == Array) {
1999 for (var i = itemsArray.length; i--;) {
2000 if (itemsArray[i].constructor == Element) {
2001 this.items[this.items.length] = itemsArray[i];
2006 Set.prototype.add = function (item) {
2007 if (item && item.constructor == Element) {
2008 this.items[this.items.length] = item;
2012 Set.prototype.remove = function (item) {
2013 if (item && item.constructor == Element) {
2014 for (var i = this.items.length; i--;) {
2015 if (this.items[i] == item) {
2016 this.items.splice(i, 1);
2023 for (var method in Element.prototype) {
2024 Set.prototype[method] = (function (methodname) {
2025 return function () {
2026 for (var i = this.items.length; i--;) {
2027 this.items[i][methodname].apply(this.items[i], arguments);
2033 Set.prototype.getBBox = function () {
2034 var x = [], y = [], w = [], h = [];
2035 for (var i = this.items.length; i--;) {
2036 var box = this.items[i].getBBox();
2039 w.push(box.x + box.width);
2040 h.push(box.y + box.height);
2052 paper.circle = function (x, y, r) {
2053 return theCircle(this, x, y, r);
2055 paper.rect = function (x, y, w, h, r) {
2056 return theRect(this, x, y, w, h, r);
2058 paper.ellipse = function (x, y, rx, ry) {
2059 return theEllipse(this, x, y, rx, ry);
2061 paper.path = function (params, pathString) {
2062 return thePath(params, pathString, this);
2064 paper.image = function (src, x, y, w, h) {
2065 return theImage(this, src, x, y, w, h);
2067 paper.text = function (x, y, text) {
2068 return theText(this, x, y, text);
2070 paper.group = function () {
2073 paper.drawGrid = function (x, y, w, h, wv, hv, color) {
2074 color = color || "#000";
2075 var p = this.path({stroke: color, "stroke-width": 1})
2076 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y),
2078 columnWidth = w / wv;
2079 for (var i = 1; i < hv; i++) {
2080 p.moveTo(x, y + i * rowHeight).lineTo(x + w, y + i * rowHeight);
2082 for (var i = 1; i < wv; i++) {
2083 p.moveTo(x + i * columnWidth, y).lineTo(x + i * columnWidth, y + h);
2087 paper.pathfinder = function (p, path) {
2089 M: function (x, y) {
2092 C: function (x1, y1, x2, y2, x3, y3) {
2093 this.curveTo(x1, y1, x2, y2, x3, y3);
2095 Q: function (x1, y1, x2, y2) {
2096 this.qcurveTo(x1, y1, x2, y2);
2098 T: function (x, y) {
2099 this.qcurveTo(x, y);
2101 S: function (x1, y1, x2, y2) {
2102 p.curveTo(x1, y1, x2, y2);
2104 L: function (x, y) {
2108 this.lineTo(x, this.last.y);
2111 this.lineTo(this.last.x, y);
2113 A: function (rx, ry, xaxisrotation, largearcflag, sweepflag, x, y) {
2114 this.arcTo(rx, ry, largearcflag, sweepflag, x, y);
2121 path = pathToAbsolute(path);
2122 for (var i = 0, ii = path.length; i < ii; i++) {
2123 var b = path[i].shift();
2124 commands[b].apply(p, path[i]);
2127 paper.set = function (itemsArray) {
2128 return new Set(itemsArray);
2130 Element.prototype.stop = function () {
2131 clearTimeout(this.animation_in_progress);
2133 Element.prototype.scale = function (x, y) {
2134 if (x == undefined && y == undefined) {
2135 return {x: this._.sx, y: this._.sy};
2139 if (x != 0 && !(x == 1 && y == 1)) {
2140 var dirx = Math.round(x / Math.abs(x)),
2141 diry = Math.round(y / Math.abs(y)),
2142 s = this.node.style;
2143 dx = this.attr("x");
2144 dy = this.attr("y");
2145 cx = this.attr("cx");
2146 cy = this.attr("cy");
2147 if (dirx != 1 || diry != 1) {
2148 if (this.transformations) {
2149 this.transformations[2] = "scale(" + [dirx, diry] + ")";
2150 this.node.setAttribute("transform", this.transformations.join(" "));
2151 dx = (dirx < 0) ? -this.attr("x") - this.attrs.width * x * dirx / this._.sx : this.attr("x");
2152 dy = (diry < 0) ? -this.attr("y") - this.attrs.height * y * diry / this._.sy : this.attr("y");
2153 cx = this.attr("cx") * dirx;
2154 cy = this.attr("cy") * diry;
2156 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11=" + dirx +
2157 ", M12=0, M21=0, M22=" + diry +
2158 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')";
2159 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2162 if (this.transformations) {
2163 this.transformations[2] = "";
2164 this.node.setAttribute("transform", this.transformations.join(" "));
2166 this.node.filterMatrix = "";
2167 s.filter = (this.node.filterMatrix || "") + (this.node.filterOpacity || "");
2170 switch (this.type) {
2174 width: this.attrs.width * x * dirx / this._.sx,
2175 height: this.attrs.height * y * diry / this._.sy,
2183 rx: this.attrs.rx * x * dirx / this._.sx,
2184 ry: this.attrs.ry * y * diry / this._.sy,
2185 r: this.attrs.r * x * diry / this._.sx,
2191 var path = pathToRelative(Raphael.parsePathString(this.attr("path"))),
2193 dim = pathDimensions(this.attrs.path),
2194 dx = -dim.width * (x - 1) / 2,
2195 dy = -dim.height * (y - 1) / 2;
2196 for (var i = 0, ii = path.length; i < ii; i++) {
2197 if (path[i][0].toUpperCase() == "M" && skip) {
2202 if (path[i][0].toUpperCase() == "A") {
2203 path[i][path[i].length - 2] *= x * dirx;
2204 path[i][path[i].length - 1] *= y * diry;
2206 for (var j = 1, jj = path[i].length; j < jj; j++) {
2207 path[i][j] *= (j % 2) ? x * dirx / this._.sx : y * diry / this._.sy;
2211 var dim2 = pathDimensions(path),
2212 dx = dim.x + dim.width / 2 - dim2.x - dim2.width / 2,
2213 dy = dim.y + dim.height / 2 - dim2.y - dim2.height / 2;
2214 path = pathToRelative(path);
2218 this.attr({path: path.join(" ")});
2225 Element.prototype.animate = function (params, ms, callback) {
2226 clearTimeout(this.animation_in_progress);
2227 var from = {}, to = {}, diff = {}, t = {x: 0, y: 0};
2228 for (var attr in params) {
2229 if (attr in availableAnimAttrs) {
2230 from[attr] = this.attr(attr);
2231 if (typeof from[attr] == "undefined") {
2232 from[attr] = availableAttrs[attr];
2234 to[attr] = params[attr];
2235 switch (availableAnimAttrs[attr]) {
2237 diff[attr] = (to[attr] - from[attr]) / ms;
2240 from[attr] = getRGB(from[attr]);
2241 var toColour = getRGB(to[attr]);
2243 r: (toColour.r - from[attr].r) / ms,
2244 g: (toColour.g - from[attr].g) / ms,
2245 b: (toColour.b - from[attr].b) / ms
2249 var pathes = pathEqualiser(from[attr], to[attr]);
2250 from[attr] = pathes[0];
2251 to[attr] = pathes[1];
2253 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2254 diff[attr][i] = [0];
2255 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2256 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
2261 var values = params[attr].split(/[, ]+/);
2262 if (attr == "translation") {
2263 from[attr] = [0, 0];
2264 diff[attr] = [values[0] / ms, values[1] / ms];
2266 from[attr] = from[attr].split(/[, ]+/);
2267 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][0]) / ms];
2273 var start = new Date(),
2277 var time = (new Date()).getTime() - start.getTime(),
2281 for (var attr in from) {
2282 switch (availableAnimAttrs[attr]) {
2284 now = +from[attr] + time * diff[attr];
2288 Math.round(from[attr].r + time * diff[attr].r),
2289 Math.round(from[attr].g + time * diff[attr].g),
2290 Math.round(from[attr].b + time * diff[attr].b)
2295 for (var i = 0, ii = from[attr].length; i < ii; i++) {
2296 now[i] = [from[attr][i][0]];
2297 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
2298 now[i][j] = from[attr][i][j] + time * diff[attr][i][j];
2300 now[i] = now[i].join(" ");
2302 now = now.join(" ");
2305 if (attr == "translation") {
2306 var x = diff[attr][0] * (time - prev),
2307 y = diff[attr][1] * (time - prev);
2310 now = [x, y].join(" ");
2312 now = [+from[attr][0] + time * diff[attr][0], +from[attr][1] + time * diff[attr][1]].join(" ");
2316 if (attr == "font-size") {
2317 set[attr] = now + "px";
2323 that.animation_in_progress = setTimeout(arguments.callee, 0);
2327 that.translate(-t.x, -t.y);
2330 clearTimeout(that.animation_in_progress);
2332 (typeof callback == "function") && callback.call(that);