2 * Raphael 0.5.6b - 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 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
107 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
108 var x1 = this.last.x,
112 k = (large_arc_flag == sweep_flag ? -1 : 1) *
113 Math.sqrt((rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
114 cx = k * rx * y / ry + (x1 + x2) / 2,
115 cy = k * -ry * x / rx + (y1 + y2) / 2,
116 d = sweep_flag ? (this.isAbsolute?"wa":"wr") : (this.isAbsolute?"at":"ar"),
117 _getX = this.isAbsolute ? VML._getX : VML._getW,
118 _getY = this.isAbsolute ? VML._getY : VML._getH,
119 left = Math.round(cx - rx),
120 top = Math.round(cy - ry);
121 d += [left, top, left + rx * 2, top + ry * 2, x1, y1, Math.round(_getX(parseFloat(x2, 10))), Math.round(_getX(parseFloat(y2, 10)))].join(", ");
122 this[0].path = this.Path += d;
123 this.last.x = (this.isAbsolute ? 0 : this.last.x) + Math.round(_getX(parseFloat(x2, 10)));
124 this.last.y = (this.isAbsolute ? 0 : this.last.y) + Math.round(_getY(parseFloat(y2, 10)));
125 this.last.isAbsolute = this.isAbsolute;
126 this.path.push({type: "arc", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
129 p.cplineTo = function (x1, y1, w1) {
131 return this.lineTo(x1, y1);
134 p._getX = this.isAbsolute ? VML._getX : VML._getW;
135 p._getY = this.isAbsolute ? VML._getY : VML._getH;
136 var x = Math.round(p._getX(Math.round(parseFloat(x1, 10) * 100) / 100));
137 var y = Math.round(p._getY(Math.round(parseFloat(y1, 10) * 100) / 100));
138 var w = Math.round(VML._getW(Math.round(parseFloat(w1, 10) * 100) / 100));
139 var d = this.isAbsolute?"c":"v";
140 var attr = [this.last.x + w, this.last.y, x - w, y, x, y];
141 d += attr.join(" ") + " ";
142 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
143 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
144 this.last.bx = attr[2];
145 this.last.by = attr[3];
146 this[0].path = this.Path += d;
147 this.path.push({type: "cpline", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
151 p.curveTo = function () {
152 var d = this.isAbsolute?"c":"v";
153 var _getX = this.isAbsolute ? VML._getX : VML._getW;
154 var _getY = this.isAbsolute ? VML._getY : VML._getH;
155 if (arguments.length == 6) {
156 this.last.x = (this.isAbsolute ? 0 : this.last.x) + Math.round(_getX(parseFloat(arguments[4], 10)));
157 this.last.y = (this.isAbsolute ? 0 : this.last.y) + Math.round(_getY(parseFloat(arguments[5], 10)));
158 this.last.bx = Math.round(_getX(parseFloat(arguments[2], 10)));
159 this.last.by = Math.round(_getY(parseFloat(arguments[3], 10)));
160 d += Math.round(_getX(parseFloat(arguments[0], 10))) + " " + Math.round(_getY(parseFloat(arguments[1], 10))) + " " +
161 Math.round(_getX(parseFloat(arguments[2], 10))) + " " + Math.round(_getY(parseFloat(arguments[3], 10))) + " " +
162 Math.round(_getX(parseFloat(arguments[4], 10))) + " " + Math.round(_getY(parseFloat(arguments[5], 10))) + " ";
163 this.last.isAbsolute = this.isAbsolute;
165 this[0].path = this.Path += d;
166 this.path.push({type: "curve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
169 p.addRoundedCorner = function (r, dir) {
170 var R = .5522 * r, rollback = this.isAbsolute, o = this;
173 rollback = function () {
177 rollback = function () {};
183 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
186 o.curveTo(-R, 0, -r, r - R, -r, r);
193 o.curveTo(R, 0, r, -(r - R), r, -r);
196 o.curveTo(R, 0, r, r - R, r, r);
203 o.curveTo(0, -R, -(R - r), -r, r, -r);
206 o.curveTo(0, -R, R - r, -r, -r, -r);
213 o.curveTo(0, R, -(R - r), r, r, r);
216 o.curveTo(0, R, R - r, r, -r, r);
221 actions[dir.charAt(0)]()[dir.charAt(1)]();
225 p.andClose = function () {
226 this[0].path = (this.Path += "x e");
229 if (typeof pathString == "string") {
230 pathString = pathString.replace(/([mzlhvcsqta])/ig, ",$1,").replace(/([^,])\-/ig, "$1,-");
231 path = pathString.split(",");
232 var i = 1, ii = path.length;
236 p.absolutely().moveTo(path[++i], path[++i]);
239 p.relatively().moveTo(path[++i], path[++i]);
242 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
245 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
248 p.absolutely().lineTo(path[++i], path[++i]);
251 p.relatively().lineTo(path[++i], path[++i]);
254 p.absolutely().lineTo(path[++i], 0);
257 p.relatively().lineTo(path[++i], 0);
260 p.absolutely().lineTo(0, path[++i]);
263 p.relatively().lineTo(0, path[++i]);
266 p.absolutely().arcTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
269 p.relatively().arcTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
281 var setFillAndStroke = function (o, params) {
283 o.attrs = o.attrs || {};
284 for (var par in params) {
285 o.attrs[par] = params[par];
287 params["font-family"] && (s.fontFamily = params["font-family"]);
288 params["font-size"] && (s.fontSize = params["font-size"]);
289 params["font"] && (s.font = params["font"]);
290 params["font-weight"] && (s.fontWeight = params["font-weight"]);
291 if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined") {
293 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || document.createElement("rvml:fill");
294 if ("fill-opacity" in params || "opacity" in params) {
295 fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
297 fill.on = (params.fill && params.fill != "none");
298 if (fill.on && params.fill) {
299 fill.color = params.fill;
302 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || document.createElement("rvml:stroke");
303 if ((params.stroke && params.stroke != "none") || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"]) {
306 if (params.stroke == "none" || typeof stroke.on == "undefined") {
309 if (stroke.on && params.stroke) {
310 stroke.color = params.stroke;
312 stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
313 stroke.joinstyle = params["stroke-linejoin"] || "miter";
314 stroke.miterlimit = params["stroke-miterlimit"] || 8;
315 stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"] || "miter"];
316 if (params["stroke-width"]) {
317 stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12/16;
319 if (params["stroke-dasharray"]) {
320 var dashes = params["stroke-dasharray"].replace(" ", ",").split(","),
323 for (var i = 0, ii = dashes.length; i < ii; i++) {
324 var res = dashes[i] / str;
329 stroke.dashstyle = dashesn.join(" ");
331 o.appendChild(stroke);
334 var addGrdientFill = function (o, gradient) {
335 o.attrs = o.attrs || {};
336 o.attrs.gradient = gradient;
338 var fill = o.getElementsByTagName("fill");
342 fill = document.createElement("rvml:fill");
344 if (gradient.dots.length) {
346 fill.type = (gradient.type.toLowerCase() == "linear") ? "gradient" : "gradientradial";
347 if (typeof gradient.dots[0].color != "undefined") {
348 fill.color = gradient.dots[0].color || "#000";
350 if (typeof gradient.dots[0].opacity != "undefined") {
351 fill.opacity = gradient.dots[0].opacity;
353 if (typeof gradient.dots[gradient.dots.length - 1].opacity != "undefined") {
354 fill.opacity2 = gradient.dots[gradient.dots.length - 1].opacity;
356 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
357 fill.color2 = gradient.dots[gradient.dots.length - 1].color || "#000";
360 for (var i = 1, ii = gradient.dots.length - 1; i < ii; i++) {
361 colors += gradient.dots[i].offset + " " + gradient.dots[i].color;
367 fill.colors = colors;
369 if (gradient.vector) {
370 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;
371 fill.angle = angle + 90;
373 if (gradient.type.toLowerCase() == "radial") {
374 fill.focusposition = "0.5, 0.5";
375 fill.focussize = "0, 0";
376 fill.method = "none";
380 var Element = function (node, group, vml) {
392 Element.prototype.setBox = function (params) {
393 var gs = this.Group.style,
395 for (var i in params) {
396 this.attrs[i] = params[i];
398 var attr = this.attrs, x, y, w, h;
401 x = attr.cx - attr.r;
402 y = attr.cy - attr.r;
406 x = attr.cx - attr.rx;
407 y = attr.cy - attr.ry;
419 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("");
424 var left = this.vml.width / 2 - w / 2,
425 top = this.vml.height / 2 - h / 2;
426 gs.position = "absolute";
427 gs.left = x - left + "px";
428 gs.top = y - top + "px";
433 gs.width = this.vml.width + "px";
434 gs.height = this.vml.height + "px";
435 os.position = "absolute";
437 os.left = left + "px";
439 os.height = h + "px";
441 Element.prototype.hide = function () {
442 this.Group.style.display = "none";
445 Element.prototype.show = function () {
446 this.Group.style.display = "block";
449 Element.prototype.rotate = function (deg) {
451 this.Group.style.rotation = Rotation;
454 Element.prototype.translate = function (x, y) {
457 this.Group.style.left = this.X + "px";
458 this.Group.style.top = this.Y + "px";
462 Element.prototype.matrix = function (xx, xy, yx, yy, dx, dy) {
463 tMatrix = new Matrix(xx, xy, yx, yy, dx, dy);
464 this.Group.style.filter = tMatrix;
467 Element.prototype.scale = function (x, y) {
469 if (x != 0 && !(x == 1 && y == 1)) {
470 var dirx = Math.round(x / Math.abs(x)),
471 diry = Math.round(y / Math.abs(y)),
473 if (dirx != 1 || diry != 1) {
474 s.filter = new Matrix(dirx, 0, 0, diry, 0, 0);
476 var width = parseInt(s.width, 10) * x * dirx;
477 var height = parseInt(s.height, 10) * y * diry;
478 var left = parseInt(s.left, 10);
479 var top = parseInt(s.top, 10);
480 s.left = this.X = left + this.W / 2 - width / 2;
481 s.top = this.Y = top + this.H / 2 - height / 2;
482 s.width = this.W = width;
483 s.height = this.H = height;
487 Element.prototype.getBBox = function () {
489 x: this.Group.offsetLeft,
490 y: this.Group.offsetTop,
491 width: this.Group.offsetWidth,
492 height: this.Group.offsetHeight
495 Element.prototype.remove = function () {
496 this[0].parentNode.removeChild(this[0]);
497 this.Group.parentNode.removeChild(this.Group);
498 this.shape && this.shape.parentNode.removeChild(this.shape);
500 Element.prototype.attr = function () {
501 if (arguments.length == 1 && typeof arguments[0] == "string") {
502 return this.attrs[arguments[0]];
504 if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
506 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
507 values[arguments[0][i]] = this.attrs[arguments[0][i]];
511 if (this[0].tagName.toLowerCase() == "group") {
512 var children = this[0].childNodes;
513 this.attrs = this.attrs || {};
514 if (arguments.length == 2) {
515 this.attrs[arguments[0]] = arguments[1];
516 } else if (arguments.length == 1 || typeof arguments[0] == "object") {
517 for (var j in arguments[0]) {
518 this.attrs[j] = arguments[0][j];
521 for (var i = 0, ii = children.length; i < ii; i++) {
522 this.attr.apply(new item(children[i], this[0], this.vml), arguments);
526 if (arguments.length == 2) {
528 params[arguments[0]] = arguments[1];
530 if (arguments.length == 1 && typeof arguments[0] == "object") {
531 params = arguments[0];
534 setFillAndStroke(this, params);
536 if (params.gradient) {
537 addGrdientFill(this, params.gradient);
539 if (params.text && this.type == "text") {
540 this[0].string = params.text;
543 this[0].id = params.id;
549 Element.prototype.toFront = function () {
550 this.Group.parentNode.appendChild(this.Group);
553 Element.prototype.toBack = function () {
554 if (this.Group.parentNode.firstChild != this.Group) {
555 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
559 var theCircle = function (vml, x, y, r) {
560 var g = document.createElement("rvml:group");
561 var o = document.createElement("rvml:oval");
563 vml.canvas.appendChild(g);
564 var res = new Element(o, g, vml);
565 setFillAndStroke(res, {stroke: "#000", fill: "none"});
566 res.setBox({x: x - r, y: y - r, w: r * 2, h: r * 2});
573 var theRect = function (vml, x, y, w, h, r) {
574 var g = document.createElement("rvml:group");
575 var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
577 o.arcsize = r / (Math.min(w, h));
580 vml.canvas.appendChild(g);
581 var res = new Element(o, g, vml);
582 setFillAndStroke(res, {stroke: "#000"});
583 res.setBox({x: x, y: y, w: w, h: h});
592 var theEllipse = function (vml, x, y, rx, ry) {
593 var g = document.createElement("rvml:group");
594 var o = document.createElement("rvml:oval");
596 vml.canvas.appendChild(g);
597 var res = new Element(o, g, vml);
598 setFillAndStroke(res, {stroke: "#000"});
599 res.setBox({x: x - rx, y: y - ry, w: rx * 2, h: ry * 2});
604 res.type = "ellipse";
607 var theImage = function (vml, src, x, y, w, h) {
608 var g = document.createElement("rvml:group");
609 var o = document.createElement("rvml:image");
612 vml.canvas.appendChild(g);
613 var res = new Element(o, g, vml);
615 res.setBox({x: x, y: y, w: w, h: h});
622 var theText = function (vml, x, y, text) {
624 var g = document.createElement("rvml:group"), gs = g.style;
625 var el = document.createElement("rvml:shape"), ol = el.style;
626 var path = document.createElement("rvml:path"), ps = path.style;
627 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
628 path.textpathok = true;
629 ol.width = vml.width;
630 ol.height = vml.height;
631 gs.position = "absolute";
634 gs.width = vml.width;
635 gs.height = vml.height;
636 var o = document.createElement("rvml:textpath");
639 o.coordsize = vml.coordsize;
640 o.coordorigin = vml.coordorigin;
642 el.appendChild(path);
644 vml.canvas.appendChild(g);
645 var res = new Element(o, g, vml);
655 var theGroup = function (vml) {
656 var el = document.createElement("rvml:group"), els = el.style;
657 els.position = "absolute";
660 els.width = vml.width;
661 els.height = vml.height;
663 vml.canvas.appendChild(el);
665 var res = new Element(el, el, vml);
667 if (f.charAt(0) != "_" && typeof vml[f] == "function") {
668 res[f] = (function (f) {
670 var e = vml[f].apply(vml, arguments);
671 el.appendChild(e[0].parentNode);
680 r._create = function () {
681 // container, width, height
682 // x, y, width, height
683 var container, width, height;
684 if (typeof arguments[0] == "string") {
685 container = document.getElementById(arguments[0]);
686 width = arguments[1];
687 height = arguments[2];
689 if (typeof arguments[0] == "object") {
690 container = arguments[0];
691 width = arguments[1];
692 height = arguments[2];
694 if (typeof arguments[0] == "number") {
698 width = arguments[2];
699 height = arguments[3];
702 throw new Error("VML container not found.");
704 if (!document.namespaces["rvml"]) {
705 document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
706 document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
708 var c = document.createElement("div"),
709 r = C.canvas = document.createElement("rvml:group"),
710 cs = c.style, rs = r.style;
713 width = width || "320px";
714 height = height || "200px";
715 cs.clip = "rect(0 " + width + " " + height + " 0)";
716 cs.position = "absolute";
719 r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
720 r.coordorigin = "0 0";
722 var b = document.createElement("rvml:rect"), bs = b.style;
723 bs.left = bs.top = 0;
725 bs.height = rs.height;
726 b.filled = b.stroked = "f";
730 if (container == 1) {
731 document.body.appendChild(c);
732 cs.position = "absolute";
744 cs.width = container.style.width = width;
745 cs.height = container.style.height = height;
746 if (container.firstChild) {
747 container.insertBefore(c, container.firstChild);
749 container.appendChild(c);
752 for (var prop in C) {
753 container[prop] = C[prop];
755 container.clear = function () {
757 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
758 if (r.childNodes[i] != b) {
759 todel.push(r.childNodes[i]);
762 for (i = 0, ii = todel.length; i < ii; i++) {
763 r.removeChild(todel[i]);
770 Matrix.prototype.toString = function () {
771 return "matrix(" + this.m[0][0] +
772 ", " + this.m[1][0] + ", " + this.m[0][1] + ", " + this.m[1][1] +
773 ", " + this.m[2][0] + ", " + this.m[2][1] + ")";
775 var thePath = function (params, pathString, SVG) {
776 var el = document.createElementNS(SVG.svgns, "path");
777 el.setAttribute("fill", "none");
779 for (var attr in params) {
780 if (params.gradient) {
781 addGrdientFill(el, params.gradient, SVG);
783 el.setAttribute(attr, params[attr]);
788 SVG.canvas.appendChild(el);
790 var p = new Element(el, SVG);
791 for (var attr in params) {
792 p.attrs[attr] = params[attr];
796 p.last = {x: 0, y: 0, bx: 0, by: 0};
797 p.absolutely = function () {
798 this.isAbsolute = true;
801 p.relatively = function () {
802 this.isAbsolute = false;
805 p.redraw = function () {
806 this[0].setAttribute("d", "M0 0");
807 var oldPath = this.path;
809 for (var i = 0, ii = oldPath.length; i < ii; i++) {
810 if (oldPath[i].type != "end") {
811 this[oldPath[i].type + "To"].apply(this, oldPath[i].arg);
818 p.moveTo = function (x, y) {
819 var d = this.isAbsolute?"M":"m";
820 var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
821 var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
822 d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
823 var oldD = this[0].getAttribute("d") || "";
824 this[0].setAttribute("d", oldD + d);
825 this.last.x = SVG._getX(parseFloat(x, 10));
826 this.last.y = SVG._getY(parseFloat(y, 10));
827 this.path.push({type: "move", arg: arguments, pos: this.isAbsolute});
830 p.lineTo = function (x, y) {
831 var d = this.isAbsolute?"L":"l";
832 var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
833 var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
834 d += _getX(parseFloat(x, 10)) + " " + _getY(parseFloat(y, 10)) + " ";
835 var oldD = this[0].getAttribute("d") || "";
836 this[0].setAttribute("d", oldD + d);
837 this.last.x = SVG._getX(parseFloat(x, 10));
838 this.last.y = SVG._getY(parseFloat(y, 10));
839 this.path.push({type: "line", arg: arguments, pos: this.isAbsolute});
842 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x, y) {
843 var d = this.isAbsolute ? "A" : "a";
844 var _getX = this.isAbsolute ? SVG._getX : SVG._getW;
845 var _getY = this.isAbsolute ? SVG._getY : SVG._getH;
846 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(" ");
847 var oldD = this[0].getAttribute("d") || "";
848 this[0].setAttribute("d", oldD + d);
849 this.last.x = SVG._getX(parseFloat(x, 10));
850 this.last.y = SVG._getY(parseFloat(y, 10));
851 this.path.push({type: "arc", arg: arguments, pos: this.isAbsolute});
854 p.cplineTo = function (x1, y1, w1) {
856 return this.lineTo(x1, y1);
859 p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
860 p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
861 var x = p._getX(Math.round(parseFloat(x1, 10) * 100) / 100);
862 var y = p._getY(Math.round(parseFloat(y1, 10) * 100) / 100);
863 var w = SVG._getW(Math.round(parseFloat(w1, 10) * 100) / 100);
864 var d = this.isAbsolute?"C":"c";
865 var attr = [this.last.x + w, this.last.y, x - w, y, x, y];
866 for (var i = 0, ii = attr.length; i < ii; i++) {
869 this.last.x = attr[4];
870 this.last.y = attr[5];
871 this.last.bx = attr[2];
872 this.last.by = attr[3];
873 var oldD = this[0].getAttribute("d") || "";
874 this[0].setAttribute("d", oldD + d);
875 this.path.push({type: "cpline", arg: arguments, pos: this.isAbsolute});
879 p.curveTo = function () {
881 p._getX = this.isAbsolute ? SVG._getX : SVG._getW;
882 p._getY = this.isAbsolute ? SVG._getY : SVG._getH;
883 if (arguments.length == 6) {
884 var d = this.isAbsolute?"C":"c";
885 for (var i = 0, ii = arguments.length; i < ii; i++) {
886 d += p[(i % 2 == 0) ? "_getX" : "_getY"](Math.round(parseFloat(arguments[i], 10) * 100) / 100) + " ";
888 this.last.x = p._getX((parseFloat(arguments[4], 10) * 100) / 100);
889 this.last.y = p._getY((parseFloat(arguments[5], 10) * 100) / 100);
890 this.last.bx = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
891 this.last.by = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
893 if (arguments.length == 4) {
894 var d = this.isAbsolute?"S":"s";
895 for (var i = 0, ii = arguments.length; i < ii; i++) {
896 d += p[i % 2 == 0 ? "_getX" : "_getY"]((parseFloat(arguments[i], 10) * 100) / 100) + " ";
899 this.last.x = p._getX((parseFloat(arguments[2], 10) * 100) / 100);
900 this.last.y = p._getY((parseFloat(arguments[3], 10) * 100) / 100);
901 this.last.bx = p._getX((parseFloat(arguments[0], 10) * 100) / 100);
902 this.last.by = p._getY((parseFloat(arguments[1], 10) * 100) / 100);
904 var oldD = this[0].getAttribute("d") || "";
905 this[0].setAttribute("d", oldD + d);
906 this.path.push({type: "curve", arg: arguments, pos: this.isAbsolute});
909 p.addRoundedCorner = function (r, dir) {
910 var R = .5522 * r, rollback = this.isAbsolute, o = this;
913 rollback = function () {
917 rollback = function () {};
923 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
926 o.curveTo(-R, 0, -r, r - R, -r, r);
933 o.curveTo(R, 0, r, -(r - R), r, -r);
936 o.curveTo(R, 0, r, r - R, r, r);
943 o.curveTo(0, -R, -(R - r), -r, r, -r);
946 o.curveTo(0, -R, R - r, -r, -r, -r);
953 o.curveTo(0, R, -(R - r), r, r, r);
956 o.curveTo(0, R, R - r, r, -r, r);
961 actions[dir[0]]()[dir[1]]();
965 p.andClose = function () {
966 var oldD = this[0].getAttribute("d") || "";
967 this[0].setAttribute("d", oldD + "Z ");
968 this.path.push({type: "end"});
971 if (typeof pathString == "string") {
972 pathString = pathString.replace(/([mzlhvcsqta])/ig, ",$1,").replace(/([^,])\-/ig, "$1,-");
973 path = pathString.split(",");
974 var i = 1, ii = path.length;
978 p.absolutely().moveTo(path[++i], path[++i]);
981 p.relatively().moveTo(path[++i], path[++i]);
984 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
987 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
990 p.relatively().curveTo(path[++i], path[++i], path[++i], path[++i]);
993 p.absolutely().curveTo(path[++i], path[++i], path[++i], path[++i]);
996 p.absolutely().lineTo(path[++i], path[++i]);
999 p.relatively().lineTo(path[++i], path[++i]);
1002 p.absolutely().lineTo(path[++i], 0);
1005 p.relatively().lineTo(path[++i], 0);
1008 p.absolutely().lineTo(0, path[++i]);
1011 p.relatively().lineTo(0, path[++i]);
1014 p.absolutely().arcTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
1017 p.relatively().arcTo(path[++i], path[++i], path[++i], path[++i], path[++i], path[++i]);
1028 var addGrdientFill = function (o, gradient, SVG) {
1029 var el = document.createElementNS(SVG.svgns, gradient.type + "Gradient");
1030 el.id = "raphael-gradient-" + SVG.gradients++;
1031 if (gradient.vector && gradient.vector.length) {
1032 el.setAttribute("x1", gradient.vector[0]);
1033 el.setAttribute("y1", gradient.vector[1]);
1034 el.setAttribute("x2", gradient.vector[2]);
1035 el.setAttribute("y2", gradient.vector[3]);
1037 SVG.defs.appendChild(el);
1038 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
1039 var stop = document.createElementNS(SVG.svgns, "stop");
1040 stop.setAttribute("offset", gradient.dots[i].offset ? gradient.dots[i].offset : (i == 0) ? "0%" : "100%");
1041 stop.setAttribute("stop-color", gradient.dots[i].color || "#fff");
1042 if (typeof gradient.dots[i].opacity != "undefined") {
1043 stop.setAttribute("stop-opacity", gradient.dots[i].opacity);
1045 el.appendChild(stop);
1047 o.setAttribute("fill", "url(#" + el.id + ")");
1049 var Element = function (node, svg) {
1052 Rotation = {deg: 0, x: 0, y: 0},
1057 this.attrs = this.attrs || {};
1058 this.transformations = []; // rotate, translate, scale, matrix
1060 Element.prototype.hide = function () {
1061 this[0].style.display = "none";
1064 Element.prototype.show = function () {
1065 this[0].style.display = "block";
1068 Element.prototype.rotate = function (deg) {
1069 var bbox = this.getBBox();
1070 Rotation.deg += deg;
1072 this.transformations[0] = ("rotate(" + Rotation.deg + " " + (bbox.x + bbox.width / 2) + " " + (bbox.y + bbox.height / 2) + ")");
1074 this.transformations[0] = "";
1076 this[0].setAttribute("transform", this.transformations.join(" "));
1079 Element.prototype.translate = function (x, y) {
1083 this.transformations[1] = "translate(" + X + "," + Y + ")";
1085 this.transformations[1] = "";
1087 this[0].setAttribute("transform", this.transformations.join(" "));
1090 Element.prototype.scale = function (x, y) {
1092 if (x != 0 && !(x == 1 && y == 1)) {
1095 if (!(ScaleX == 1 && ScaleY == 1)) {
1096 var bbox = this.getBBox(),
1097 dx = bbox.x * (1 - ScaleX) + (bbox.width / 2 - bbox.width * ScaleX / 2),
1098 dy = bbox.y * (1 - ScaleY) + (bbox.height / 2 - bbox.height * ScaleY / 2);
1099 this.transformations[2] = new Matrix(ScaleX, 0, 0, ScaleY, dx, dy);
1101 this.transformations[2] = "";
1103 this[0].setAttribute("transform", this.transformations.join(" "));
1108 Element.prototype.matrix = function (xx, xy, yx, yy, dx, dy) {
1109 this.transformations[3] = new Matrix(xx, xy, yx, yy, dx, dy);
1110 this[0].setAttribute("transform", this.transformations.join(" "));
1113 Element.prototype.remove = function () {
1114 this[0].parentNode.removeChild(this[0]);
1116 Element.prototype.getBBox = function () {
1117 return this[0].getBBox();
1119 Element.prototype.attr = function () {
1120 if (arguments.length == 1 && typeof arguments[0] == "string") {
1121 return this[0].getAttribute(arguments[0]);
1123 if (arguments.length == 1 && arguments[0] instanceof Array) {
1125 for (var j in arguments[0]) {
1126 values[arguments[0][j]] = this.attrs[arguments[0][j]];
1130 if (arguments.length == 2) {
1131 var att = arguments[0],
1132 value = arguments[1];
1134 this.attrs[att] = value;
1139 this[0].setAttribute(att, svg._getX(value));
1144 this[0].setAttribute(att, svg._getY(value));
1147 this[0].setAttribute(att, svg._getW(value));
1150 this[0].setAttribute(att, svg._getH(value));
1153 addGrdientFill(this[0], params.gradient, svg);
1155 case "stroke-dasharray":
1156 this[0].setAttribute(att, value.replace(" ", ","));
1159 if (this.type == "text") {
1160 this[0].removeChild(this[0].firstChild);
1161 this[0].appendChild(document.createTextNode(value));
1165 var cssrule = att.replace(/(\-.)/g, function (w) {
1166 return w.substring(1).toUpperCase();
1168 this[0].style[cssrule] = value;
1169 // Need following line for Firefox
1170 this[0].setAttribute(att, value);
1173 } else if (arguments.length == 1 && typeof arguments[0] == "object") {
1174 var params = arguments[0];
1175 for (var attr in params) {
1176 this.attrs[attr] = params[attr];
1177 if (attr == "stroke-dasharray") {
1178 this[0].setAttribute(attr, params[attr].replace(" ", ","));
1179 } else if (attr == "text" && this.type == "text") {
1180 this[0].removeChild(this[0].firstChild);
1181 this[0].appendChild(document.createTextNode(params[attr]));
1183 var cssrule = attr.replace(/(\-.)/g, function (w) {
1184 return w.substring(1).toUpperCase();
1186 this[0].style[cssrule] = params[attr];
1187 // Need following line for Firefox
1188 this[0].setAttribute(attr, params[attr]);
1191 if (params.gradient) {
1192 this.attrs.gradient = params.gradient;
1193 addGrdientFill(this[0], params.gradient, svg);
1198 Element.prototype.toFront = function () {
1199 this[0].parentNode.appendChild(this[0]);
1202 Element.prototype.toBack = function () {
1203 if (this[0].parentNode.firstChild != this[0]) {
1204 this[0].parentNode.insertBefore(this[0], this[0].parentNode.firstChild);
1208 var theCircle = function (svg, x, y, r) {
1209 var el = document.createElementNS(svg.svgns, "circle");
1210 el.setAttribute("cx", svg._getX(x));
1211 el.setAttribute("cy", svg._getY(y));
1212 el.setAttribute("r", r);
1213 el.setAttribute("fill", "none");
1214 el.setAttribute("stroke", "#000");
1216 svg.canvas.appendChild(el);
1218 var res = new Element(el, svg);
1219 res.attrs = res.attrs || {};
1223 res.attrs.stroke = "#000";
1224 res.type = "circle";
1227 var theRect = function (svg, x, y, w, h, r) {
1228 var el = document.createElementNS(svg.svgns, "rect");
1229 el.setAttribute("x", svg._getX(x));
1230 el.setAttribute("y", svg._getY(y));
1231 el.setAttribute("width", svg._getW(w));
1232 el.setAttribute("height", svg._getH(h));
1234 el.setAttribute("rx", r);
1235 el.setAttribute("ry", r);
1237 el.setAttribute("fill", "none");
1238 el.setAttribute("stroke", "#000");
1240 svg.canvas.appendChild(el);
1242 var res = new Element(el, svg);
1243 res.attrs = res.attrs || {};
1246 res.attrs.width = w;
1247 res.attrs.height = h;
1248 res.attrs.stroke = "#000";
1250 res.attrs.rx = res.attrs.ry = r;
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");
1264 svg.canvas.appendChild(el);
1266 var res = new Element(el, svg);
1267 res.attrs = res.attrs || {};
1272 res.attrs.stroke = "#000";
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);
1287 res.attrs = res.attrs || {};
1290 res.attrs.width = w;
1291 res.attrs.height = h;
1295 var theText = function (svg, x, y, text) {
1296 var el = document.createElementNS(svg.svgns, "text");
1297 el.setAttribute("x", x);
1298 el.setAttribute("y", y);
1299 el.setAttribute("text-anchor", "middle");
1300 el.setAttribute("fill", "#000");
1302 el.appendChild(document.createTextNode(text));
1305 svg.canvas.appendChild(el);
1307 var res = new Element(el, svg);
1308 res.attrs = res.attrs || {};
1311 res.attrs.fill = "#000";
1315 var theGroup = function (svg) {
1316 var el = document.createElementNS(svg.svgns, "g");
1318 svg.canvas.appendChild(el);
1320 var i = new Element(el, svg);
1321 for (var f in svg) {
1322 if (f[0] != "_" && typeof svg[f] == "function") {
1323 i[f] = (function (f) {
1324 return function () {
1325 var e = svg[f].apply(svg, arguments);
1326 el.appendChild(e[0]);
1335 r._create = function () {
1336 // container, width, height
1337 // x, y, width, height
1338 if (typeof arguments[0] == "string") {
1339 var container = document.getElementById(arguments[0]);
1340 var width = arguments[1];
1341 var height = arguments[2];
1343 if (typeof arguments[0] == "object") {
1344 var container = arguments[0];
1345 var width = arguments[1];
1346 var height = arguments[2];
1348 if (typeof arguments[0] == "number") {
1352 width = arguments[2],
1353 height = arguments[3];
1356 throw new Error("SVG container not found.");
1358 C.canvas = document.createElementNS(C.svgns, "svg");
1359 C.canvas.setAttribute("width", width || 320);
1360 C.width = width || 320;
1361 C.canvas.setAttribute("height", height || 200);
1362 C.height = height || 200;
1363 if (container == 1) {
1364 document.body.appendChild(C.canvas);
1365 C.canvas.style.position = "absolute";
1366 C.canvas.style.left = x + "px";
1367 C.canvas.style.top = y + "px";
1369 if (container.firstChild) {
1370 container.insertBefore(C.canvas, container.firstChild);
1372 container.appendChild(C.canvas);
1377 clear: function () {
1378 while (this.canvas.firstChild) {
1379 this.canvas.removeChild(this.canvas.firstChild);
1381 this.defs = document.createElementNS(C.svgns, "defs");
1383 this.canvas.appendChild(this.defs);
1386 for (var prop in C) {
1387 if (prop != "create") {
1388 container[prop] = C[prop];
1394 C.svgns = "http://www.w3.org/2000/svg";
1395 C.xlink = "http://www.w3.org/1999/xlink";
1397 if (type == "VML" || type == "SVG") {
1398 C.circle = function (x, y, r) {
1399 return theCircle(this, x, y, r);
1401 C.rect = function (x, y, w, h, r) {
1402 return theRect(this, x, y, w, h, r);
1404 C.ellipse = function (x, y, rx, ry) {
1405 return theEllipse(this, x, y, rx, ry);
1407 C.path = function (params, pathString) {
1408 return thePath(params, pathString, this);
1410 C.image = function (src, x, y, w, h) {
1411 return theImage(this, src, x, y, w, h);
1413 C.text = function (x, y, text) {
1414 return theText(this, x, y, text);
1416 C.group = function () {
1417 return theGroup(this);
1419 C.linerect = function (x, y, w, h, r) {
1420 if (r && parseInt(r, 10)) {
1421 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();
1423 return this.path({stroke: "#000"}).moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).andClose();
1425 C.drawGrid = function (x, y, w, h, wv, hv, color) {
1426 color = color || "#000";
1427 var p = this.path({stroke: color, "stroke-width": 1})
1428 .moveTo(x, y).lineTo(x + w, y).lineTo(x + w, y + h).lineTo(x, y + h).lineTo(x, y);
1429 for (var i = 1; i < hv; i++) {
1430 p.moveTo(x, y + i * Math.round(h / hv)).lineTo(x + w, y + i * Math.round(h / hv));
1432 for (var i = 1; i < wv; i++) {
1433 p.moveTo(x + i * Math.round(w / wv), y).lineTo(x + i * Math.round(w / wv), y + h);
1437 C.setGrid = function (xmin, ymin, xmax, ymax, w, h) {
1438 var xc = (xmax - xmin) / w;
1439 var yc = (ymax - ymin) / h;
1440 this._getX = function (x) {
1441 return xmin + x * xc;
1443 this._getY = function (y) {
1444 return ymin + y * yc;
1446 this._getW = function (w) {
1449 this._getH = function (h) {
1453 C.clearGrid = function () {
1454 this._getX = this._getY = this._getW = this._getH = function (x) { return x; };
1456 C.safari = function () {
1457 if (r.type == "SVG") {
1458 var rect = C.rect(-C.width, -C.height, C.width * 3, C.height * 3).attr({stroke: "none"});
1459 setTimeout(function () {rect.remove();}, 0);
1462 Element.prototype.animateTo = function (x, y, ms, callback) {
1463 clearTimeout(this.animation_in_progress);
1464 if ("cx" in this.attrs || "x" in this.attrs) {
1465 var is_round = ("cx" in this.attrs),
1466 X = this.attrs.cx || this.attrs.x,
1467 Y = this.attrs.cy || this.attrs.y;
1468 if (x == X && y == Y) {
1474 plus = Y - coeff * X,
1475 alpha = Math.atan(this.coeff);
1476 this.xs = this.step * Math.cos(alpha);
1480 var start = new Date(),
1483 var time = (new Date()).getTime() - start.getTime();
1485 var x1 = X + time * dx / ms;
1486 var y1 = x1 * coeff + plus;
1487 that.attr(is_round ? {cx: x1, cy: y1} : {x: x1, y: y1});
1488 that.animation_in_progress = setTimeout(arguments.callee, 1);
1491 that.attr(is_round ? {cx: x, cy: y} : {x: x, y: y});
1493 callback && callback.call(that);
1502 return function () {};
1504 })((!(window.SVGPreserveAspectRatio && window.SVGPreserveAspectRatio.SVG_PRESERVEASPECTRATIO_XMINYMIN == 2)) ? "VML" : "SVG");
1507 Raphael.vml = !(Raphael.svg = (Raphael.type == "SVG"));
1508 if (Raphael.vml && window.CanvasRenderingContext2D) {
1509 Raphael.type = "Canvas only";
1510 Raphael.vml = Raphael.svg = false;
1512 Raphael.toString = function () {
1513 return "Your browser supports " + this.type;
1515 // generic utilities
1516 Raphael.hsb2rgb = function (hue, saturation, brightness) {
1517 if (typeof hue == "object" && "h" in hue && "s" in hue && "b" in hue) {
1525 if (brightness == 0) {
1526 return {r: 0, g: 0, b: 0, hex: "#000"};
1528 var i = Math.floor(hue * 6),
1530 p = brightness * (1 - saturation),
1531 q = brightness * (1 - (saturation * f)),
1532 t = brightness * (1 - (saturation * (1 - f)));
1534 function () {red = brightness; green = t; blue = p;},
1535 function () {red = q; green = brightness; blue = p;},
1536 function () {red = p; green = brightness; blue = t;},
1537 function () {red = p; green = q; blue = brightness;},
1538 function () {red = t; green = p; blue = brightness;},
1539 function () {red = brightness; green = p; blue = q;},
1540 function () {red = brightness; green = t; blue = p;},
1543 var rgb = {r: red, g: green, b: blue};
1547 var r = Math.round(red).toString(16);
1548 if (r.length == 1) {
1551 var g = Math.round(green).toString(16);
1552 if (g.length == 1) {
1555 var b = Math.round(blue).toString(16);
1556 if (b.length == 1) {
1559 rgb.hex = "#" + r + g + b;
1562 Raphael.rgb2hsb = function (red, green, blue) {
1563 if (typeof red == "object" && "r" in red && "g" in red && "b" in red) {
1568 if (red.charAt(0) == "#") {
1569 if (red.length == 4) {
1570 blue = parseInt(red.substring(3), 16);
1571 green = parseInt(red.substring(2, 3), 16);
1572 red = parseInt(red.substring(1, 2), 16);
1574 blue = parseInt(red.substring(5), 16);
1575 green = parseInt(red.substring(3, 5), 16);
1576 red = parseInt(red.substring(1, 3), 16);
1579 if (red > 1 || green > 1 || blue > 1) {
1584 var max = Math.max(red, green, blue),
1585 min = Math.min(red, green, blue),
1590 return {h: 0, s: 0, b: max};
1592 var delta = (max - min);
1593 saturation = delta / max;
1595 hue = (green - blue) / delta;
1596 } else if (green == max) {
1597 hue = 2 + ((blue - red) / delta);
1599 hue = 4 + ((red - green) / delta);
1609 return {h: hue, s: saturation, b: brightness};
1611 Raphael.getColor = function (value) {
1612 var start = arguments.callee.start = arguments.callee.start || {h: 0, s: 1, b: value || .75};
1613 var rgb = this.hsb2rgb(start.h, start.s, start.b);
1619 start = {h: 0, s: 1, b: start.b};
1624 Raphael.getColor.reset = function () {
1625 this.start = undefined;