2 * Raphael 0.5.5b - JavaScript Vector Library
4 * Copyright (c) 2008 Dmitry Baranovskiy (raphaeljs.com)
5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
7 var Raphael = (function (type) {
9 return r._create.apply(r, arguments);
14 function Matrix(m11, m12, m21, m22, dx, dy) {
16 [m11 || 1, m12 || 0, 0],
17 [m21 || 0, m22 || 1, 0],
18 [dx || 0, dy || 0, 1],
22 C._getX = C._getY = C._getW = C._getH = function (x) { return x; };
25 Matrix.prototype.toString = function () {
26 return "progid:DXImageTransform.Microsoft.Matrix(M11=" + this.m[0][0] +
27 ", M12=" + this.m[1][0] + ", M21=" + this.m[0][1] + ", M22=" + this.m[1][1] +
28 ", Dx=" + this.m[2][0] + ", Dy=" + this.m[2][1] + ", sizingmethod='auto expand', filtertype='bilinear')";
30 var thePath = function (params, pathString, VML) {
31 var g = document.createElement("rvml:group"), gl = g.style;
32 gl.position = "absolute";
35 gl.width = VML.width + "px";
36 gl.height = VML.height + "px";
37 var el = document.createElement("rvml:shape"), ol = el.style;
38 ol.width = VML.width + "px";
39 ol.height = VML.height + "px";
41 if (params["class"]) {
42 el.className = params["class"];
44 el.coordsize = this.coordsize;
45 el.coordorigin = this.coordorigin;
47 VML.canvas.appendChild(g);
48 var p = new Element(el, g, VML);
49 setFillAndStroke(p, params);
50 if (params.gradient) {
51 addGrdientFill(p, params.gradient);
56 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
58 p.absolutely = function () {
59 this.isAbsolute = true;
62 p.relatively = function () {
63 this.isAbsolute = false;
66 p.redraw = function () {
68 var oldPath = this.path;
70 for (var i = 0, ii = oldPath.length; i < ii; i++) {
71 if (oldPath[i].type != "end") {
72 this[oldPath[i].type + "To"].apply(this, oldPath[i].arg);
79 p.moveTo = function (x, y) {
80 var d = this.isAbsolute?"m":"t";
81 var _getX = this.isAbsolute ? VML._getX : VML._getW;
82 var _getY = this.isAbsolute ? VML._getY : VML._getH;
83 d += Math.round(_getX(parseFloat(x, 10))) + " " + Math.round(_getY(parseFloat(y, 10)));
84 this[0].path = this.Path += d;
85 this.last.x = (this.isAbsolute ? 0 : this.last.x) + Math.round(_getX(parseFloat(x, 10)));
86 this.last.y = (this.isAbsolute ? 0 : this.last.y) + Math.round(_getY(parseFloat(y, 10)));
87 this.last.isAbsolute = this.isAbsolute;
88 this.path.push({type: "move", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
91 p.lineTo = function (x, y) {
92 var d = this.isAbsolute?"l":"r";
93 var _getX = this.isAbsolute ? VML._getX : VML._getW;
94 var _getY = this.isAbsolute ? VML._getY : VML._getH;
95 d += Math.round(_getX(parseFloat(x, 10))) + " " + Math.round(_getY(parseFloat(y, 10)));
96 this[0].path = this.Path += d;
97 this.last.x = (this.isAbsolute ? 0 : this.last.x) + Math.round(_getX(parseFloat(x, 10)));
98 this.last.y = (this.isAbsolute ? 0 : this.last.y) + Math.round(_getY(parseFloat(y, 10)));
99 this.last.isAbsolute = this.isAbsolute;
100 this.path.push({type: "line", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
103 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
104 // for more information of where this math came from visit:
105 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
106 var x1 = this.last.x,
110 k = (large_arc_flag == sweep_flag ? -1 : 1) *
111 Math.sqrt((rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
112 cx = k * rx * y / ry + (x1 + x2) / 2,
113 cy = k * -ry * x / rx + (y1 + y2) / 2,
114 d = sweep_flag ? (this.isAbsolute?"wa":"wr") : (this.isAbsolute?"at":"ar"),
115 _getX = this.isAbsolute ? VML._getX : VML._getW,
116 _getY = this.isAbsolute ? VML._getY : VML._getH,
117 left = Math.round(cx - rx),
118 top = Math.round(cy - ry);
119 d += [left, top, left + rx * 2, top + ry * 2, x1, y1, Math.round(_getX(parseFloat(x2, 10))), Math.round(_getX(parseFloat(y2, 10)))].join(", ");
120 this[0].path = this.Path += d;
121 this.last.x = (this.isAbsolute ? 0 : this.last.x) + Math.round(_getX(parseFloat(x2, 10)));
122 this.last.y = (this.isAbsolute ? 0 : this.last.y) + Math.round(_getY(parseFloat(y2, 10)));
123 this.last.isAbsolute = this.isAbsolute;
124 this.path.push({type: "arc", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
127 p.cplineTo = function (x1, y1, w1) {
129 return this.lineTo(x1, y1);
132 p._getX = this.isAbsolute ? VML._getX : VML._getW;
133 p._getY = this.isAbsolute ? VML._getY : VML._getH;
134 var x = Math.round(p._getX(Math.round(parseFloat(x1, 10) * 100) / 100));
135 var y = Math.round(p._getY(Math.round(parseFloat(y1, 10) * 100) / 100));
136 var w = Math.round(VML._getW(Math.round(parseFloat(w1, 10) * 100) / 100));
137 var d = this.isAbsolute?"c":"v";
138 var attr = [this.last.x + w, this.last.y, x - w, y, x, y];
139 d += attr.join(" ") + " ";
140 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
141 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
142 this.last.bx = attr[2];
143 this.last.by = attr[3];
144 this[0].path = this.Path += d;
145 this.path.push({type: "cpline", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
149 p.curveTo = function () {
150 var d = this.isAbsolute?"c":"v";
151 var _getX = this.isAbsolute ? VML._getX : VML._getW;
152 var _getY = this.isAbsolute ? VML._getY : VML._getH;
153 if (arguments.length == 6) {
154 this.last.x = (this.isAbsolute ? 0 : this.last.x) + Math.round(_getX(parseFloat(arguments[4], 10)));
155 this.last.y = (this.isAbsolute ? 0 : this.last.y) + Math.round(_getY(parseFloat(arguments[5], 10)));
156 this.last.bx = Math.round(_getX(parseFloat(arguments[2], 10)));
157 this.last.by = Math.round(_getY(parseFloat(arguments[3], 10)));
158 d += Math.round(_getX(parseFloat(arguments[0], 10))) + " " + Math.round(_getY(parseFloat(arguments[1], 10))) + " " +
159 Math.round(_getX(parseFloat(arguments[2], 10))) + " " + Math.round(_getY(parseFloat(arguments[3], 10))) + " " +
160 Math.round(_getX(parseFloat(arguments[4], 10))) + " " + Math.round(_getY(parseFloat(arguments[5], 10))) + " ";
161 this.last.isAbsolute = this.isAbsolute;
163 this[0].path = this.Path += d;
164 this.path.push({type: "curve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
167 p.addRoundedCorner = function (r, dir) {
168 var R = .5522 * r, rollback = this.isAbsolute, o = this;
171 rollback = function () {
175 rollback = function () {};
181 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
184 o.curveTo(-R, 0, -r, r - R, -r, r);
191 o.curveTo(R, 0, r, -(r - R), r, -r);
194 o.curveTo(R, 0, r, r - R, r, r);
201 o.curveTo(0, -R, -(R - r), -r, r, -r);
204 o.curveTo(0, -R, R - r, -r, -r, -r);
211 o.curveTo(0, R, -(R - r), r, r, r);
214 o.curveTo(0, R, R - r, r, -r, r);
219 actions[dir.charAt(0)]()[dir.charAt(1)]();
223 p.andClose = function () {
224 this[0].path = (this.Path += "x e");
227 if (typeof pathString == "string") {
228 pathString = pathString.replace(/([mzlhvcsqta])/ig, ",$1,").replace(/([^,])\-/ig, "$1,-");
229 path = pathString.split(",");
230 var i = 1, ii = path.length;
234 p.absolutely().moveTo(path[++i], path[++i]);
237 p.relatively().moveTo(path[++i], path[++i]);
240 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
243 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
246 p.absolutely().lineTo(path[++i], path[++i]);
249 p.relatively().lineTo(path[++i], path[++i]);
252 p.absolutely().lineTo(path[++i], 0);
255 p.relatively().lineTo(path[++i], 0);
258 p.absolutely().lineTo(0, path[++i]);
261 p.relatively().lineTo(0, path[++i]);
273 var setFillAndStroke = function (o, params) {
274 o[0].attrs = o[0].attrs || {};
275 for (var par in params) {
276 o[0].attrs[par] = params[par];
278 params["font-family"] && (o[0].style.fontFamily = params["font-family"]);
279 params["font-size"] && (o[0].style.fontSize = params["font-size"]);
280 params["font"] && (o[0].style.font = params["font"]);
281 params["font-weight"] && (o[0].style.fontWeight = params["font-weight"]);
282 if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined") {
284 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || document.createElement("rvml:fill");
285 if ("fill-opacity" in params || "opacity" in params) {
286 fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
288 fill.on = (params.fill && params.fill != "none");
289 if (fill.on && params.fill) {
290 fill.color = params.fill;
292 if (params.fill == "none") {
296 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || document.createElement("rvml:stroke");
297 if ((params.stroke && params.stroke != "none") || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"]) {
300 if (params.stroke == "none" || typeof stroke.on == "undefined") {
303 if (stroke.on && params.stroke) {
304 stroke.color = params.stroke;
306 stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
307 stroke.joinstyle = params["stroke-linejoin"] || "miter";
308 stroke.miterlimit = params["stroke-miterlimit"] || 8;
309 stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"] || "miter"];
310 if (params["stroke-width"]) {
311 stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12/16;
313 if (params["stroke-dasharray"]) {
314 var dashes = params["stroke-dasharray"].replace(" ", ",").split(","),
317 for (var i = 0, ii = dashes.length; i < ii; i++) {
318 var res = dashes[i] / str;
323 stroke.dashstyle = dashesn.join(" ");
325 o.appendChild(stroke);
328 var addGrdientFill = function (o, gradient) {
329 o[0].attrs = o[0].attrs || {};
330 o[0].attrs.gradient = gradient;
332 var fill = o.getElementsByTagName("fill");
336 fill = document.createElement("rvml:fill");
338 if (gradient.dots.length) {
340 fill.type = (gradient.type.toLowerCase() == "linear") ? "gradient" : "gradientradial";
341 if (typeof gradient.dots[0].color != "undefined") {
342 fill.color = gradient.dots[0].color || "#000";
344 if (typeof gradient.dots[0].opacity != "undefined") {
345 fill.opacity = gradient.dots[0].opacity;
347 if (typeof gradient.dots[gradient.dots.length - 1].opacity != "undefined") {
348 fill.opacity2 = gradient.dots[gradient.dots.length - 1].opacity;
350 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
351 fill.color2 = gradient.dots[gradient.dots.length - 1].color || "#000";
354 for (var i = 1, ii = gradient.dots.length - 1; i < ii; i++) {
355 colors += gradient.dots[i].offset + " " + gradient.dots[i].color;
361 fill.colors = colors;
363 if (gradient.vector) {
364 var angle = Math.round(Math.atan((parseInt(gradient.vector[3], 10) - parseInt(gradient.vector[1], 10)) / (parseInt(gradient.vector[2], 10) - parseInt(gradient.vector[0], 10))) * 57.29) + 180;
365 fill.angle = angle + 90;
367 if (gradient.type.toLowerCase() == "radial") {
368 fill.focusposition = "0.5, 0.5";
369 fill.focussize = "0, 0";
370 fill.method = "none";
374 var Element = function (node, group, vml) {
382 arguments.callee.name = "Element";
385 this.setBox = function (params) {
386 var gs = this.Group.style,
388 for (var i in params) {
389 this[0].attrs[i] = params[i];
391 var attr = this[0].attrs, x, y, w, h;
394 x = attr.cx - attr.r;
395 y = attr.cy - attr.r;
399 x = attr.cx - attr.rx;
400 y = attr.cy - attr.ry;
412 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("");
417 var left = vml.width / 2 - w / 2,
418 top = vml.height / 2 - h / 2;
419 gs.position = "absolute";
420 gs.left = x - left + "px";
421 gs.top = y - top + "px";
426 gs.width = vml.width + "px";
427 gs.height = vml.height + "px";
428 os.position = "absolute";
430 os.left = left + "px";
432 os.height = h + "px";
434 this.hide = function () {
435 this.Group.style.display = "none";
438 this.show = function () {
439 this.Group.style.display = "block";
442 this.rotate = function (deg) {
444 this.Group.style.rotation = Rotation;
447 this.translate = function (x, y) {
450 this.Group.style.left = this.X + "px";
451 this.Group.style.top = this.Y + "px";
455 this.matrix = function (xx, xy, yx, yy, dx, dy) {
456 tMatrix = new Matrix(xx, xy, yx, yy, dx, dy);
457 this.Group.style.filter = tMatrix;
460 this.scale = function (x, y) {
462 if (x != 0 && !(x == 1 && y == 1)) {
463 var dirx = Math.round(x / Math.abs(x)),
464 diry = Math.round(y / Math.abs(y));
465 if (dirx != 1 || diry != 1) {
466 this[0].style.filter = new Matrix(dirx, 0, 0, diry, 0, 0);
468 var width = parseInt(this[0].style.width, 10) * x * dirx;
469 var height = parseInt(this[0].style.height, 10) * y * diry;
470 var left = parseInt(this[0].style.left, 10);
471 var top = parseInt(this[0].style.top, 10);
472 this[0].style.left = this.X = left + this.W / 2 - width / 2;
473 this[0].style.top = this.Y = top + this.H / 2 - height / 2;
474 this[0].style.width = this.W = width;
475 this[0].style.height = this.H = height;
479 this.getBBox = function () {
481 x: this.Group.offsetLeft,
482 y: this.Group.offsetTop,
483 width: this.Group.offsetWidth,
484 height: this.Group.offsetHeight
487 this.remove = function () {
488 this[0].parentNode.removeChild(this[0]);
489 this.Group.parentNode.removeChild(this.Group);
490 this.shape && this.shape.parentNode.removeChild(this.shape);
492 this.attr = function () {
493 if (arguments.length == 1 && typeof arguments[0] == "string") {
494 return this[0].attrs[arguments[0]];
496 if (this[0].attrs && arguments.length == 1 && arguments[0] instanceof Array) {
498 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
499 values[arguments[0][i]] = this[0].attrs[arguments[0][i]];
503 if (this[0].tagName.toLowerCase() == "group") {
504 var children = this[0].childNodes;
505 this[0].attrs = this[0].attrs || {};
506 if (arguments.length == 2) {
507 this[0].attrs[arguments[0]] = arguments[1];
508 } else if (arguments.length == 1 || typeof arguments[0] == "object") {
509 for (var j in arguments[0]) {
510 this[0].attrs[j] = arguments[0][j];
513 for (var i = 0, ii = children.length; i < ii; i++) {
514 this.attr.apply(new item(children[i], this[0], vml), arguments);
518 if (arguments.length == 2) {
520 params[arguments[0]] = arguments[1];
522 if (arguments.length == 1 && typeof arguments[0] == "object") {
523 params = arguments[0];
526 setFillAndStroke(this, params);
528 if (params.gradient) {
529 addGrdientFill(this, params.gradient);
531 if (params.text && this.type == "text") {
532 this[0].string = params.text;
535 this[0].id = params.id;
541 this.toFront = function () {
542 this.Group.parentNode.appendChild(this.Group);
545 this.toBack = function () {
546 if (this.Group.parentNode.firstChild != this.Group) {
547 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
552 var theCircle = function (vml, x, y, r) {
553 var g = document.createElement("rvml:group");
554 var o = document.createElement("rvml:oval");
556 vml.canvas.appendChild(g);
557 var res = new Element(o, g, vml);
558 setFillAndStroke(res, {stroke: "#000", fill: "none"});
559 res.setBox({x: x - r, y: y - r, w: r * 2, h: r * 2});
566 var theRect = function (vml, x, y, w, h, r) {
567 var g = document.createElement("rvml:group");
568 var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
570 o.arcsize = r / (Math.min(w, h));
573 vml.canvas.appendChild(g);
574 var res = new Element(o, g, vml);
575 setFillAndStroke(res, {stroke: "#000"});
576 res.setBox({x: x, y: y, w: w, h: h});
585 var theEllipse = function (vml, x, y, rx, ry) {
586 var g = document.createElement("rvml:group");
587 var o = document.createElement("rvml:oval");
589 vml.canvas.appendChild(g);
590 var res = new Element(o, g, vml);
591 setFillAndStroke(res, {stroke: "#000"});
592 res.setBox({x: x - rx, y: y - ry, w: rx * 2, h: ry * 2});
597 res.type = "ellipse";
600 var theImage = function (vml, src, x, y, w, h) {
601 var g = document.createElement("rvml:group");
602 var o = document.createElement("rvml:image");
605 vml.canvas.appendChild(g);
606 var res = new Element(o, g, vml);
608 res.setBox({x: x, y: y, w: w, h: h});
615 var theText = function (vml, x, y, text) {
617 var g = document.createElement("rvml:group"), gs = g.style;
618 var el = document.createElement("rvml:shape"), ol = el.style;
619 var path = document.createElement("rvml:path"), ps = path.style;
620 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
621 path.textpathok = true;
622 ol.width = vml.width;
623 ol.height = vml.height;
624 gs.position = "absolute";
627 gs.width = vml.width;
628 gs.height = vml.height;
629 var o = document.createElement("rvml:textpath");
632 o.coordsize = vml.coordsize;
633 o.coordorigin = vml.coordorigin;
635 el.appendChild(path);
637 vml.canvas.appendChild(g);
638 var res = new Element(o, g, vml);
648 var theGroup = function (vml) {
649 var el = document.createElement("rvml:group"), els = el.style;
650 els.position = "absolute";
653 els.width = vml.width;
654 els.height = vml.height;
656 vml.canvas.appendChild(el);
658 var res = new Element(el, el, vml);
660 if (f.charAt(0) != "_" && typeof vml[f] == "function") {
661 res[f] = (function (f) {
663 var e = vml[f].apply(vml, arguments);
664 el.appendChild(e[0].parentNode);
673 r._create = function () {
674 // container, width, height
675 // x, y, width, height
676 var container, width, height;
677 if (typeof arguments[0] == "string") {
678 container = document.getElementById(arguments[0]);
679 width = arguments[1];
680 height = arguments[2];
682 if (typeof arguments[0] == "object") {
683 container = arguments[0];
684 width = arguments[1];
685 height = arguments[2];
687 if (typeof arguments[0] == "number") {
691 width = arguments[2];
692 height = arguments[3];
695 throw new Error("VML container not found.");
697 if (!document.namespaces["rvml"]) {
698 document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
699 document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
701 var c = document.createElement("div"),
702 r = C.canvas = document.createElement("rvml:group"),
703 cs = c.style, rs = r.style;
706 width = width || "320px";
707 height = height || "200px";
708 cs.clip = "rect(0 " + width + " " + height + " 0)";
709 cs.position = "absolute";
712 r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
713 r.coordorigin = "0 0";
715 var b = document.createElement("rvml:rect"), bs = b.style;
716 bs.left = bs.top = 0;
718 bs.height = rs.height;
719 b.filled = b.stroked = "f";
723 if (container == 1) {
724 document.body.appendChild(c);
725 cs.position = "absolute";
737 cs.width = container.style.width = width;
738 cs.height = container.style.height = height;
739 if (container.firstChild) {
740 container.insertBefore(c, container.firstChild);
742 container.appendChild(c);
745 for (var prop in C) {
746 container[prop] = C[prop];
748 container.clear = function () {
750 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
751 if (r.childNodes[i] != b) {
752 todel.push(r.childNodes[i]);
755 for (i = 0, ii = todel.length; i < ii; i++) {
756 r.removeChild(todel[i]);
763 Matrix.prototype.toString = function () {
764 return "matrix(" + this.m[0][0] +
765 ", " + this.m[1][0] + ", " + this.m[0][1] + ", " + this.m[1][1] +
766 ", " + this.m[2][0] + ", " + this.m[2][1] + ")";
768 var thePath = function (params, pathString, SVG) {
769 var el = document.createElementNS(SVG.svgns, "path");
770 el.setAttribute("fill", "none");
772 for (var attr in params) {
773 if (params.gradient) {
774 addGrdientFill(el, params.gradient, SVG);
776 el.setAttribute(attr, params[attr]);
781 SVG.canvas.appendChild(el);
783 var p = new Element(el, SVG);
784 for (var attr in params) {
785 p.attrs[attr] = params[attr];
789 p.last = {x: 0, y: 0, bx: 0, by: 0};
790 p.absolutely = function () {
791 this.isAbsolute = true;
794 p.relatively = function () {
795 this.isAbsolute = false;
798 p.redraw = function () {
799 this[0].setAttribute("d", "M0 0");
800 var oldPath = this.path;
802 for (var i = 0, ii = oldPath.length; i < ii; i++) {
803 if (oldPath[i].type != "end") {
804 this[oldPath[i].type + "To"].apply(this, oldPath[i].arg);
811 p.moveTo = function (x, y) {
812 var d = this.isAbsolute?"M":"m";
813 var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
814 var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
815 d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
816 var oldD = this[0].getAttribute("d") || "";
817 this[0].setAttribute("d", oldD + d);
818 this.last.x = SVG._getX(parseFloat(x, 10));
819 this.last.y = SVG._getY(parseFloat(y, 10));
820 this.path.push({type: "move", arg: arguments, pos: this.isAbsolute});
823 p.lineTo = function (x, y) {
824 var d = this.isAbsolute?"L":"l";
825 var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
826 var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
827 d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
828 var oldD = this[0].getAttribute("d") || "";
829 this[0].setAttribute("d", oldD + d);
830 this.last.x = SVG._getX(parseFloat(x, 10));
831 this.last.y = SVG._getY(parseFloat(y, 10));
832 this.path.push({type: "line", arg: arguments, pos: this.isAbsolute});
835 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
836 var d = this.isAbsolute ? "A" : "a";
837 var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
838 var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
839 d += [SVG._getW(parseFloat(rx, 10)), SVG._getH(parseFloat(ry, 10)), 0, large_arc_flag, sweep_flag, _getX(parseFloat(x, 10)), _getY(parseFloat(y, 10))].join(" ");
840 var oldD = this[0].getAttribute("d") || "";
841 this[0].setAttribute("d", oldD + d);
842 this.last.x = SVG._getX(parseFloat(x, 10));
843 this.last.y = SVG._getY(parseFloat(y, 10));
844 this.path.push({type: "arc", arg: arguments, pos: this.isAbsolute});
847 p.cplineTo = function (x1, y1, w1) {
849 return this.lineTo(x1, y1);
852 p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
853 p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
854 var x = p._getX(Math.round(parseFloat(x1, 10) * 100) / 100);
855 var y = p._getY(Math.round(parseFloat(y1, 10) * 100) / 100);
856 var w = SVG._getW(Math.round(parseFloat(w1, 10) * 100) / 100);
857 var d = this.isAbsolute?"C":"c";
858 var attr = [this.last.x + w, this.last.y, x - w, y, x, y];
859 for (var i = 0, ii = attr.length; i < ii; i++) {
862 this.last.x = attr[4];
863 this.last.y = attr[5];
864 this.last.bx = attr[2];
865 this.last.by = attr[3];
866 var oldD = this[0].getAttribute("d") || "";
867 this[0].setAttribute("d", oldD + d);
868 this.path.push({type: "cpline", arg: arguments, pos: this.isAbsolute});
872 p.curveTo = function () {
874 p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
875 p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
876 if (arguments.length == 6) {
877 var d = this.isAbsolute?"C":"c";
878 for (var i = 0, ii = arguments.length; i < ii; i++) {
879 d += p[(i % 2 == 0) ? "_getX" : "_getY"](Math.round(parseFloat(arguments[i], 10) * 100) / 100) + " ";
881 this.last.x = p._getX((parseFloat(arguments[4], 10) * 100) / 100);
882 this.last.y = p._getY((parseFloat(arguments[5], 10) * 100) / 100);
883 this.last.bx = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
884 this.last.by = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
886 if (arguments.length == 4) {
887 var d = this.isAbsolute?"S":"s";
888 for (var i = 0, ii = arguments.length; i < ii; i++) {
889 d += p[i % 2 == 0 ? "_getX" : "_getY"]((parseFloat(arguments[i], 10) * 100) / 100) + " ";
892 this.last.x = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
893 this.last.y = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
894 this.last.bx = p._getX((parseFloat(arguments[0], 10) * 100) / 100);
895 this.last.by = p._getY((parseFloat(arguments[1], 10) * 100) / 100);
897 var oldD = this[0].getAttribute("d") || "";
898 this[0].setAttribute("d", oldD + d);
899 this.path.push({type: "curve", arg: arguments, pos: this.isAbsolute});
902 p.addRoundedCorner = function (r, dir) {
903 var R = .5522 * r, rollback = this.isAbsolute, o = this;
906 rollback = function () {
910 rollback = function () {};
916 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
919 o.curveTo(-R, 0, -r, r - R, -r, r);
926 o.curveTo(R, 0, r, -(r - R), r, -r);
929 o.curveTo(R, 0, r, r - R, r, r);
936 o.curveTo(0, -R, -(R - r), -r, r, -r);
939 o.curveTo(0, -R, R - r, -r, -r, -r);
946 o.curveTo(0, R, -(R - r), r, r, r);
949 o.curveTo(0, R, R - r, r, -r, r);
954 actions[dir[0]]()[dir[1]]();
958 p.andClose = function () {
959 var oldD = this[0].getAttribute("d") || "";
960 this[0].setAttribute("d", oldD + "Z ");
961 this.path.push({type: "end"});
964 if (typeof pathString == "string") {
965 pathString = pathString.replace(/([mzlhvcsqta])/ig, ",$1,").replace(/([^,])\-/ig, "$1,-");
966 path = pathString.split(",");
967 var i = 1, ii = path.length;
971 p.absolutely().moveTo(path[++i], path[++i]);
974 p.relatively().moveTo(path[++i], path[++i]);
977 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
980 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
983 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i]);
986 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i]);
989 p.absolutely().lineTo(path[++i], path[++i]);
992 p.relatively().lineTo(path[++i], path[++i]);
995 p.absolutely().lineTo(path[++i], 0);
998 p.relatively().lineTo(path[++i], 0);
1001 p.absolutely().lineTo(0, path[++i]);
1004 p.relatively().lineTo(0, path[++i]);
1015 var addGrdientFill = function (o, gradient, SVG) {
1016 var el = document.createElementNS(SVG.svgns, gradient.type + "Gradient");
1017 el.id = "raphael-gradient-" + SVG.gradients++;
1018 if (gradient.vector && gradient.vector.length) {
1019 el.setAttribute("x1", gradient.vector[0]);
1020 el.setAttribute("y1", gradient.vector[1]);
1021 el.setAttribute("x2", gradient.vector[2]);
1022 el.setAttribute("y2", gradient.vector[3]);
1024 SVG.defs.appendChild(el);
1025 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1026 var stop = document.createElementNS(SVG.svgns, "stop");
1027 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
1028 stop.setAttribute("stop-color", gradient.dots[i].color || "#fff");
1029 if (typeof gradient.dots[i].opacity != "undefined") {
1030 stop.setAttribute("stop-opacity", gradient.dots[i].opacity);
1032 el.appendChild(stop);
1034 o.setAttribute("fill", "url(#" + el.id + ")");
1036 var Element = function (node, svg) {
1039 Rotation = {deg: 0, x: 0, y: 0},
1044 this.attrs = this.attrs || {};
1045 this.transformations = []; // rotate, translate, scale, matrix
1046 this.hide = function () {
1047 this[0].style.display = "none";
1050 this.show = function () {
1051 this[0].style.display = "block";
1054 this.rotate = function (deg) {
1055 var bbox = this.getBBox();
1056 Rotation.deg += deg;
1058 this.transformations[0] = ("rotate(" + Rotation.deg + " " + (bbox.x + bbox.width / 2) + " " + (bbox.y + bbox.height / 2) + ")");
1060 this.transformations[0] = "";
1062 this[0].setAttribute("transform", this.transformations.join(" "));
1065 this.translate = function (x, y) {
1069 this.transformations[1] = "translate(" + X + "," + Y + ")";
1071 this.transformations[1] = "";
1073 this[0].setAttribute("transform", this.transformations.join(" "));
1076 this.scale = function (x, y) {
1078 if (x != 0 && !(x == 1 && y == 1)) {
1081 if (!(ScaleX == 1 && ScaleY == 1)) {
1082 var bbox = this.getBBox(),
1083 dx = bbox.x * (1 - ScaleX) + (bbox.width / 2 - bbox.width * ScaleX / 2),
1084 dy = bbox.y * (1 - ScaleY) + (bbox.height / 2 - bbox.height * ScaleY / 2);
1085 this.transformations[2] = new Matrix(ScaleX, 0, 0, ScaleY, dx, dy);
1087 this.transformations[2] = "";
1089 this[0].setAttribute("transform", this.transformations.join(" "));
1094 this.matrix = function (xx, xy, yx, yy, dx, dy) {
1095 this.transformations[3] = new Matrix(xx, xy, yx, yy, dx, dy);
1096 this[0].setAttribute("transform", this.transformations.join(" "));
1099 this.remove = function () {
1100 this[0].parentNode.removeChild(this[0]);
1102 this.getBBox = function () {
1103 return this[0].getBBox();
1105 this.attr = function () {
1106 if (arguments.length == 1 && typeof arguments[0] == "string") {
1107 return this[0].getAttribute(arguments[0]);
1109 if (arguments.length == 1 && arguments[0] instanceof Array) {
1111 for (var j in arguments[0]) {
1112 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1116 if (arguments.length == 2) {
1117 var att = arguments[0],
1118 value = arguments[1];
1120 this.attrs[att] = value;
1125 this[0].setAttribute(att, svg._getX(value));
1130 this[0].setAttribute(att, svg._getY(value));
1133 this[0].setAttribute(att, svg._getW(value));
1136 this[0].setAttribute(att, svg._getH(value));
1139 addGrdientFill(this[0], params.gradient, svg);
1141 case "stroke-dasharray":
1142 this[0].setAttribute(att, value.replace(" ", ","));
1145 if (this.type == "text") {
1146 this[0].removeChild(this[0].firstChild);
1147 this[0].appendChild(document.createTextNode(value));
1151 var cssrule = att.replace(/(\-.)/g, function (w) {
1152 return w.substring(1).toUpperCase();
1154 this[0].style[cssrule] = value;
1155 // Need following line for Firefox
1156 this[0].setAttribute(att, value);
1159 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1160 var params = arguments[0];
1161 for (var attr in params) {
1162 this.attrs[attr] = params[attr];
1163 if (attr == "stroke-dasharray") {
1164 this[0].setAttribute(attr, params[attr].replace(" ", ","));
1165 } else if (attr == "text" && this.type == "text") {
1166 this[0].removeChild(this[0].firstChild);
1167 this[0].appendChild(document.createTextNode(params[attr]));
1169 var cssrule = attr.replace(/(\-.)/g, function (w) {
1170 return w.substring(1).toUpperCase();
1172 this[0].style[cssrule] = params[attr];
1173 // Need following line for Firefox
1174 this[0].setAttribute(attr, params[attr]);
1177 if (params.gradient) {
1178 this[0].attrs.gradient = params.gradient;
1179 addGrdientFill(this[0], params.gradient, svg);
1184 this.toFront = function () {
1185 this[0].parentNode.appendChild(this[0]);
1188 this.toBack = function () {
1189 if (this[0].parentNode.firstChild != this[0]) {
1190 this[0].parentNode.insertBefore(this[0], this[0].parentNode.firstChild);
1194 // this.animateTo = function (x, y, ms, callback) {
1195 // if ("cx" in node.attrs || "x" in node.attrs) {
1196 // var X = node.attrs.cx || node.attrs.x;
1197 // var Y = node.attrs.cy || node.attrs.y;
1200 // var coeff = dy / dx;
1201 // var plus = Y - coeff * X;
1202 // var alpha = Math.atan(this.coeff);
1203 // this.xs = this.step * Math.cos(alpha);
1205 // this.xs = -this.xs;
1210 var theCircle = function (svg, x, y, r) {
1211 var el = document.createElementNS(svg.svgns, "circle");
1212 el.setAttribute("cx", svg._getX(x));
1213 el.setAttribute("cy", svg._getY(y));
1214 el.setAttribute("r", r);
1215 el.setAttribute("fill", "none");
1216 el.setAttribute("stroke", "#000");
1217 el.attrs = el.attrs || {};
1221 el.attrs.stroke = "#000";
1223 svg.canvas.appendChild(el);
1225 var res = new Element(el, svg);
1226 res.type = "circle";
1229 var theRect = function (svg, x, y, w, h, r) {
1230 var el = document.createElementNS(svg.svgns, "rect");
1231 el.setAttribute("x", svg._getX(x));
1232 el.setAttribute("y", svg._getY(y));
1233 el.setAttribute("width", svg._getW(w));
1234 el.setAttribute("height", svg._getH(h));
1235 el.attrs = el.attrs || {};
1239 el.attrs.height = h;
1241 el.setAttribute("rx", r);
1242 el.setAttribute("ry", r);
1243 el.attrs.rx = el.attrs.ry = r;
1245 el.setAttribute("fill", "none");
1246 el.setAttribute("stroke", "#000");
1247 el.attrs.stroke = "#000";
1249 svg.canvas.appendChild(el);
1251 var res = new Element(el, svg);
1255 var theEllipse = function (svg, x, y, rx, ry) {
1256 var el = document.createElementNS(svg.svgns, "ellipse");
1257 el.setAttribute("cx", svg._getX(x));
1258 el.setAttribute("cy", svg._getY(y));
1259 el.setAttribute("rx", svg._getW(rx));
1260 el.setAttribute("ry", svg._getH(ry));
1261 el.setAttribute("fill", "none");
1262 el.setAttribute("stroke", "#000");
1263 el.attrs = el.attrs || {};
1268 el.attrs.stroke = "#000";
1270 svg.canvas.appendChild(el);
1272 var res = new Element(el, svg);
1273 res.type = "ellipse";
1276 var theImage = function (svg, src, x, y, w, h) {
1277 var el = document.createElementNS(svg.svgns, "image");
1278 el.setAttribute("x", svg._getX(x));
1279 el.setAttribute("y", svg._getY(y));
1280 el.setAttribute("width", svg._getW(w));
1281 el.setAttribute("height", svg._getH(h));
1282 el.setAttributeNS(svg.xlink, "href", src);
1284 svg.canvas.appendChild(el);
1286 var res = new Element(el, svg);
1290 var theText = function (svg, x, y, text) {
1291 var el = document.createElementNS(svg.svgns, "text");
1292 el.setAttribute("x", x);
1293 el.setAttribute("y", y);
1294 el.setAttribute("text-anchor", "middle");
1295 el.setAttribute("fill", "#000");
1296 el.attrs = el.attrs || {};
1299 el.attrs.fill = "#000";
1301 el.appendChild(document.createTextNode(text));
1304 svg.canvas.appendChild(el);
1306 var res = new Element(el, svg);
1310 var theGroup = function (svg) {
1311 var el = document.createElementNS(svg.svgns, "g");
1313 svg.canvas.appendChild(el);
1315 var i = new Element(el, svg);
1316 for (var f in svg) {
1317 if (f[0] != "_" && typeof svg[f] == "function") {
1318 i[f] = (function (f) {
1319 return function () {
1320 var e = svg[f].apply(svg, arguments);
1321 el.appendChild(e[0]);
1330 r._create = function () {
1331 // container, width, height
1332 // x, y, width, height
1333 if (typeof arguments[0] == "string") {
1334 var container = document.getElementById(arguments[0]);
1335 var width = arguments[1];
1336 var height = arguments[2];
1338 if (typeof arguments[0] == "object") {
1339 var container = arguments[0];
1340 var width = arguments[1];
1341 var height = arguments[2];
1343 if (typeof arguments[0] == "number") {
1347 width = arguments[2],
1348 height = arguments[3];
1351 throw new Error("SVG container not found.");
1353 C.canvas = document.createElementNS(C.svgns, "svg");
1354 C.canvas.setAttribute("width", width || 320);
1355 C.width = width || 320;
1356 C.canvas.setAttribute("height", height || 200);
1357 C.height = height || 200;
1358 if (container == 1) {
1359 document.body.appendChild(C.canvas);
1360 C.canvas.style.position = "absolute";
1361 C.canvas.style.left = x + "px";
1362 C.canvas.style.top = y + "px";
1364 if (container.firstChild) {
1365 container.insertBefore(C.canvas, container.firstChild);
1367 container.appendChild(C.canvas);
1372 clear: function () {
1373 while (this.canvas.firstChild) {
1374 this.canvas.removeChild(this.canvas.firstChild);
1376 this.defs = document.createElementNS(C.svgns, "defs");
1378 this.canvas.appendChild(this.defs);
1381 for (var prop in C) {
1382 if (prop != "create") {
1383 container[prop] = C[prop];
1389 C.svgns = "http://www.w3.org/2000/svg";
1390 C.xlink = "http://www.w3.org/1999/xlink";
1392 if (type == "VML" || type == "SVG") {
1393 C.circle = function (x, y, r) {
1394 return theCircle(this, x, y, r);
1396 C.rect = function (x, y, w, h, r) {
1397 return theRect(this, x, y, w, h, r);
1399 C.ellipse = function (x, y, rx, ry) {
1400 return theEllipse(this, x, y, rx, ry);
1402 C.path = function (params, pathString) {
1403 return thePath(params, pathString, this);
1405 C.image = function (src, x, y, w, h) {
1406 return theImage(this, src, x, y, w, h);
1408 C.text = function (x, y, text) {
1409 return theText(this, x, y, text);
1411 C.group = function () {
1412 return theGroup(this);
1414 C.linerect = function (x, y, w, h, r) {
1415 if (r && parseInt(r, 10)) {
1416 return this.path({stroke: "#000"}).moveTo(x + r, y).lineTo(x + w - r, y).addRoundedCorner(r, "rd").lineTo(x + w, y + h - r).addRoundedCorner(r, "dl").lineTo(x + r, y + h).addRoundedCorner(r, "lu").lineTo(x, y + r).addRoundedCorner(r, "ur").andClose();
1418 return this.path({stroke: "#000"}).moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).andClose();
1420 C.drawGrid = function (x, y, w, h, wv, hv, color) {
1421 color = color || "#000";
1422 var p = this.path({stroke: color, "stroke-width": 1})
1423 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y);
1424 for (var i = 1; i < hv; i++) {
1425 p.moveTo(x, y + i * Math.round(h / hv)).lineTo(x + w, y + i * Math.round(h / hv));
1427 for (var i = 1; i < wv; i++) {
1428 p.moveTo(x + i * Math.round(w / wv), y).lineTo(x + i * Math.round(w / wv), y + h);
1432 C.setGrid = function (xmin, ymin, xmax, ymax, w, h) {
1433 var xc = (xmax - xmin) / w;
1434 var yc = (ymax - ymin) / h;
1435 this._getX = function (x) {
1436 return xmin + x * xc;
1438 this._getY = function (y) {
1439 return ymin + y * yc;
1441 this._getW = function (w) {
1444 this._getH = function (h) {
1448 C.clearGrid = function () {
1449 this._getX = this._getY = this._getW = this._getH = function (x) { return x; };
1451 C.safari = function () {
1452 if (r.type == "SVG") {
1453 var rect = C.rect(-C.width, -C.height, C.width * 3, C.height * 3).attr({stroke: "none"});
1454 setTimeout(function () {rect.remove();}, 0);
1460 return function () {};
1462 })((!(window.SVGPreserveAspectRatio && window.SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN == 2)) ? "VML" : "SVG");
1465 Raphael.vml = !(Raphael.svg = (Raphael.type == "SVG"));
1466 if (Raphael.vml && window.CanvasRenderingContext2D) {
1467 Raphael.type = "Canvas only";
1468 Raphael.vml = Raphael.svg = false;
1470 Raphael.toString = function () {
1471 return "Your browser supports " + this.type;
1473 // generic utilities
1474 Raphael.hsb2rgb = function (hue, saturation, brightness) {
1475 if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
1483 if (brightness == 0) {
1484 return {r: 0, g: 0, b: 0, hex: "#000"};
1486 var i = Math.floor(hue * 6),
1488 p = brightness * (1 - saturation),
1489 q = brightness * (1 - (saturation * f)),
1490 t = brightness * (1 - (saturation * (1 - f)));
1492 function () {red = brightness; green = t; blue = p;},
1493 function () {red = q; green = brightness; blue = p;},
1494 function () {red = p; green = brightness; blue = t;},
1495 function () {red = p; green = q; blue = brightness;},
1496 function () {red = t; green = p; blue = brightness;},
1497 function () {red = brightness; green = p; blue = q;},
1498 function () {red = brightness; green = t; blue = p;},
1501 var rgb = {r: red, g: green, b: blue};
1505 var r = Math.round(red).toString(16);
1506 if (r.length == 1) {
1509 var g = Math.round(green).toString(16);
1510 if (g.length == 1) {
1513 var b = Math.round(blue).toString(16);
1514 if (b.length == 1) {
1517 rgb.hex = "#" + r + g + b;
1520 Raphael.rgb2hsb = function (red, green, blue) {
1521 if (typeof red == "object" && "r" in red && "h" in red && "b" in red) {
1526 if (red.charAt(0) == "#") {
1527 if (red.length == 4) {
1528 blue = parseInt(red.substring(3), 16);
1529 green = parseInt(red.substring(2, 3), 16);
1530 red = parseInt(red.substring(1, 2), 16);
1532 blue = parseInt(red.substring(5), 16);
1533 green = parseInt(red.substring(3, 5), 16);
1534 red = parseInt(red.substring(1, 3), 16);
1537 if (red > 1 || green > 1 || blue > 1) {
1542 var max = Math.max(red, green, blue),
1543 min = Math.min(red, green, blue),
1548 return {h: 0, s: 0, b: max};
1550 var delta = (max - min);
1551 saturation = delta / max;
1553 hue = (green - blue) / delta;
1554 } else if (green == max) {
1555 hue = 2 + ((blue - red) / delta);
1557 hue = 4 + ((red - green) / delta);
1567 return {h: hue, s: saturation, b: brightness};
1569 Raphael.getColor = function (value) {
1570 var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
1571 var rgb = this.hsb2rgb(start.h, start.s, start.b);
1577 start = {h: 0, s: 1, b: start.b};
1582 Raphael.getColor.reset = function () {
1583 this.start = undefined;