2 * Raphael 0.7 - 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 throw new Error("raphael-vml.js should not be included into HTML. It has to be loaded with raphael.js");
12 R.vml = !(R.svg = false);
13 R._.thePath = function (params, pathString, VML) {
14 var g = document.createElement("rvml:group"), gl = g.style;
15 gl.position = "absolute";
18 gl.width = VML.width + "px";
19 gl.height = VML.height + "px";
20 var el = document.createElement("rvml:shape"), ol = el.style;
21 ol.width = VML.width + "px";
22 ol.height = VML.height + "px";
24 if (params["class"]) {
25 el.className = params["class"];
27 el.coordsize = this.coordsize;
28 el.coordorigin = this.coordorigin;
30 VML.canvas.appendChild(g);
31 var p = new Element(el, g, VML);
35 p.last = {x: 0, y: 0, bx: 0, by: 0, isAbsolute: true};
37 p.absolutely = function () {
38 this.isAbsolute = true;
41 p.relatively = function () {
42 this.isAbsolute = false;
45 p.moveTo = function (x, y) {
46 var d = this.isAbsolute?"m":"t";
47 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
48 this.node.path = this.Path += d;
49 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
50 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
51 this.last.isAbsolute = this.isAbsolute;
52 this.attrs.path += (this.isAbsolute ? "M" : "m") + [x, y];
55 p.lineTo = function (x, y) {
56 var d = this.isAbsolute?"l":"r";
57 d += Math.round(parseFloat(x, 10)) + " " + Math.round(parseFloat(y, 10));
58 this[0].path = this.Path += d;
59 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x, 10);
60 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y, 10);
61 this.last.isAbsolute = this.isAbsolute;
62 this.attrs.path += (this.isAbsolute ? "L" : "l") + [x, y];
65 p.arcTo = function (rx, ry, large_arc_flag, sweep_flag, x2, y2) {
66 // for more information of where this math came from visit:
67 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
68 x2 = (this.isAbsolute ? 0 : this.last.x) + x2;
69 y2 = (this.isAbsolute ? 0 : this.last.y) + y2;
74 k = (large_arc_flag == sweep_flag ? -1 : 1) *
75 Math.sqrt(Math.abs(rx * rx * ry * ry - rx * rx * y * y - ry * ry * x * x) / (rx * rx * y * y + ry * ry * x * x)),
76 cx = k * rx * y / ry + (x1 + x2) / 2,
77 cy = k * -ry * x / rx + (y1 + y2) / 2,
78 d = sweep_flag ? (this.isAbsolute ? "wa" : "wr") : (this.isAbsolute ? "at" : "ar"),
79 left = Math.round(cx - rx),
80 top = Math.round(cy - ry);
81 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(", ");
82 this.node.path = this.Path += d;
83 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(x2, 10);
84 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(y2, 10);
85 this.last.isAbsolute = this.isAbsolute;
86 this.attrs.path += (this.isAbsolute ? "A" : "a") + [rx, ry, 0, large_arc_flag, sweep_flag, x2, y2];
89 p.cplineTo = function (x1, y1, w1) {
91 return this.lineTo(x1, y1);
93 var x = Math.round(Math.round(parseFloat(x1, 10) * 100) / 100),
94 y = Math.round(Math.round(parseFloat(y1, 10) * 100) / 100),
95 w = Math.round(Math.round(parseFloat(w1, 10) * 100) / 100),
96 d = this.isAbsolute ? "c" : "v",
97 attr = [Math.round(this.last.x) + w, Math.round(this.last.y), x - w, y, x, y],
98 svgattr = [this.last.x + w1, this.last.y, x1 - w1, y1, x1, y1];
99 d += attr.join(" ") + " ";
100 this.last.x = (this.isAbsolute ? 0 : this.last.x) + attr[4];
101 this.last.y = (this.isAbsolute ? 0 : this.last.y) + attr[5];
102 this.last.bx = attr[2];
103 this.last.by = attr[3];
104 this.node.path = this.Path += d;
105 this.attrs.path += (this.isAbsolute ? "C" : "c") + svgattr;
109 p.curveTo = function () {
110 var d = this.isAbsolute ? "c" : "v";
111 if (arguments.length == 6) {
112 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
113 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
114 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[4], 10);
115 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[5], 10);
116 d += [Math.round(parseFloat(arguments[0], 10)),
117 Math.round(parseFloat(arguments[1], 10)),
118 Math.round(parseFloat(arguments[2], 10)),
119 Math.round(parseFloat(arguments[3], 10)),
120 Math.round(parseFloat(arguments[4], 10)),
121 Math.round(parseFloat(arguments[5], 10))].join(" ") + " ";
122 this.last.isAbsolute = this.isAbsolute;
123 this.attrs.path += (this.isAbsolute ? "C" : "c") + Array.prototype.splice.call(arguments, 0, arguments.length);
125 if (arguments.length == 4) {
126 var bx = this.last.x * 2 - this.last.bx;
127 var by = this.last.y * 2 - this.last.by;
128 this.last.bx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
129 this.last.by = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
130 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
131 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
132 d += [Math.round(bx), Math.round(by),
133 Math.round(parseFloat(arguments[0], 10)),
134 Math.round(parseFloat(arguments[1], 10)),
135 Math.round(parseFloat(arguments[2], 10)),
136 Math.round(parseFloat(arguments[3], 10))].join(" ") + " ";
137 this.attrs.path += (this.isAbsolute ? "S" : "s") + Array.prototype.splice.call(arguments, 0, arguments.length);
139 this.node.path = this.Path += d;
142 p.qcurveTo = function () {
144 if (arguments.length == 4) {
145 this.last.qx = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[0], 10);
146 this.last.qy = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[1], 10);
147 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
148 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
149 d += [Math.round(this.last.qx),
150 Math.round(this.last.qy),
151 Math.round(this.last.x),
152 Math.round(this.last.y)].join(" ") + " ";
153 this.last.isAbsolute = this.isAbsolute;
154 this.attrs.path += (this.isAbsolute ? "Q" : "q") + Array.prototype.splice.call(arguments, 0, arguments.length);
156 if (arguments.length == 2) {
157 this.last.qx = this.last.x * 2 - this.last.qx;
158 this.last.qy = this.last.y * 2 - this.last.qy;
159 this.last.x = (this.isAbsolute ? 0 : this.last.x) + parseFloat(arguments[2], 10);
160 this.last.y = (this.isAbsolute ? 0 : this.last.y) + parseFloat(arguments[3], 10);
161 d += [Math.round(this.last.qx),
162 Math.round(this.last.qy),
163 Math.round(this.last.x),
164 Math.round(this.last.y)].join(" ") + " ";
165 this.attrs.path += (this.isAbsolute ? "T" : "t") + Array.prototype.splice.call(arguments, 0, arguments.length);
167 this.node.path = this.Path += d;
168 this.path.push({type: "qcurve", arg: [].slice.call(arguments, 0), pos: this.isAbsolute});
171 p.addRoundedCorner = function (r, dir) {
172 var R = .5522 * r, rollback = this.isAbsolute, o = this;
175 rollback = function () {
179 rollback = function () {};
185 o.curveTo(-R, 0, -r, -(r - R), -r, -r);
188 o.curveTo(-R, 0, -r, r - R, -r, r);
195 o.curveTo(R, 0, r, -(r - R), r, -r);
198 o.curveTo(R, 0, r, r - R, r, r);
205 o.curveTo(0, -R, -(R - r), -r, r, -r);
208 o.curveTo(0, -R, R - r, -r, -r, -r);
215 o.curveTo(0, R, -(R - r), r, r, r);
218 o.curveTo(0, R, R - r, r, -r, r);
223 actions[dir.charAt(0)]()[dir.charAt(1)]();
227 p.andClose = function () {
228 this.node.path = (this.Path += "x e");
229 this.attrs.path += "z";
235 R._.paper.pathfinder(p, "" + pathString);
238 R._.setFillAndStroke(p, params);
239 if (params.gradient) {
240 R._.addGrdientFill(p, params.gradient);
244 R._.setFillAndStroke = function (o, params) {
246 o.attrs = o.attrs || {};
247 for (var par in params) {
248 o.attrs[par] = params[par];
250 if (params.path && o.type == "path") {
253 R._.paper.pathfinder(o, params.path);
255 if (params.rotation != null) {
256 o.Group.style.rotation = params.rotation;
258 if (params.translation) {
259 var xy = params.translation.split(/[, ]+/);
260 o.translate(xy[0], xy[1]);
263 var xy = params.scale.split(/[, ]+/);
264 o.scale(xy[0], xy[1]);
266 if (o.type == "image" && params.opacity) {
267 o.node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
268 o.node.style.filter = (o.node.filterMatrix || "") + (o.node.filterOpacity || "");
270 params["font-family"] && (s.fontFamily = params["font-family"]);
271 params["font-size"] && (s.fontSize = params["font-size"]);
272 params["font"] && (s.font = params["font"]);
273 params["font-weight"] && (s.fontWeight = params["font-weight"]);
274 if (typeof params.opacity != "undefined" || typeof params["stroke-width"] != "undefined" || typeof params.fill != "undefined" || typeof params.stroke != "undefined") {
275 o = o.shape || o.node;
276 var fill = (o.getElementsByTagName("fill") && o.getElementsByTagName("fill")[0]) || document.createElement("rvml:fill");
277 if ("fill-opacity" in params || "opacity" in params) {
278 fill.opacity = ((params["fill-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
283 if (fill.on == undefined || params.fill == "none") {
286 if (fill.on && params.fill) {
287 var isURL = params.fill.match(/^url\(([^\)]+)\)$/i);
292 fill.color = params.fill;
298 var stroke = (o.getElementsByTagName("stroke") && o.getElementsByTagName("stroke")[0]) || document.createElement("rvml:stroke");
299 if ((params.stroke && params.stroke != "none") || params["stroke-width"] || params["stroke-opacity"] || params["stroke-dasharray"]) {
302 if (params.stroke == "none" || typeof stroke.on == "undefined") {
305 if (stroke.on && params.stroke) {
306 stroke.color = params.stroke;
308 stroke.opacity = ((params["stroke-opacity"] + 1 || 2) - 1) * ((params.opacity + 1 || 2) - 1);
309 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
310 stroke.miterlimit = params["stroke-miterlimit"] || 8;
311 params["stroke-linecap"] && (stroke.endcap = {butt: "flat", square: "square", round: "round"}[params["stroke-linecap"]] || "miter");
312 params["stroke-width"] && (stroke.weight = (parseFloat(params["stroke-width"], 10) || 1) * 12 / 16);
313 if (params["stroke-dasharray"]) {
317 "-.": "shortdashdot",
318 "-..": "shortdashdotdot",
323 "--.": "longdashdot",
324 "--..": "longdashdotdot"
326 stroke.dashstyle = dasharray[params["stroke-dasharray"]] || "";
328 o.appendChild(stroke);
331 R._.addGrdientFill = function (o, gradient) {
332 o.attrs = o.attrs || {};
333 o.attrs.gradient = gradient;
335 var fill = o.getElementsByTagName("fill");
339 fill = document.createElement("rvml:fill");
341 if (gradient.dots.length) {
343 fill.method = "none";
344 fill.type = (gradient.type.toLowerCase() == "linear") ? "gradient" : "gradientTitle";
345 if (typeof gradient.dots[0].color != "undefined") {
346 fill.color = gradient.dots[0].color || "#000";
348 if (typeof gradient.dots[gradient.dots.length - 1].color != "undefined") {
349 fill.color2 = gradient.dots[gradient.dots.length - 1].color || "#000";
352 for (var i = 0, ii = gradient.dots.length; i < ii; i++) {
353 if (gradient.dots[i].offset) {
354 colors.push(gradient.dots[i].offset + " " + gradient.dots[i].color);
357 var fillOpacity = gradient.dots[0].opacity || 1;
358 var fillOpacity2 = gradient.dots[gradient.dots.length - 1].opacity || 1;
360 fill.colors.value = colors.join(",");
361 fillOpacity2 += fillOpacity;
362 fillOpacity = fillOpacity2 - fillOpacity;
363 fillOpacity2 -= fillOpacity;
365 fill.setAttribute("opacity", fillOpacity);
366 fill.setAttribute("opacity2", fillOpacity2);
367 if (gradient.vector) {
368 var angle = Math.round(Math.atan((parseFloat(gradient.vector[3], 10) - parseFloat(gradient.vector[1], 10)) / (parseFloat(gradient.vector[2], 10) - parseFloat(gradient.vector[0], 10))) * 57.29) || 0;
369 fill.angle = 270 - angle;
371 if (gradient.type.toLowerCase() == "radial") {
373 fill.focusposition = "0.5 0.5";
377 var Element = function (node, group, vml) {
397 Element.prototype.rotate = function (deg, isAbsolute) {
398 if (deg == undefined) {
406 this.Group.style.rotation = this._.rt;
409 Element.prototype.setBox = function (params) {
410 var gs = this.Group.style,
412 for (var i in params) {
413 this.attrs[i] = params[i];
415 var attr = this.attrs, x, y, w, h;
418 x = attr.cx - attr.r;
419 y = attr.cy - attr.r;
423 x = attr.cx - attr.rx;
424 y = attr.cy - attr.ry;
433 h = attr.height || 0;
436 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("");
439 if (!this.attrs.path) {
445 var dim = Raphael.pathDimensions(this.attrs.path),
459 if (this.type == "path") {
460 var left = Math.round(this.vml.width / 2 - w / 2 - x),
461 top = Math.round(this.vml.height / 2 - h / 2 - y);
462 gs.left = - left + "px";
463 gs.top = - top + "px";
469 os.left = left + "px";
471 var left = this.vml.width / 2 - w / 2,
472 top = this.vml.height / 2 - h / 2;
473 gs.position = "absolute";
474 gs.left = x - left + "px";
475 gs.top = y - top + "px";
480 gs.width = this.vml.width + "px";
481 gs.height = this.vml.height + "px";
482 os.position = "absolute";
484 os.left = left + "px";
486 os.height = h + "px";
489 Element.prototype.hide = function () {
490 this.Group.style.display = "none";
493 Element.prototype.show = function () {
494 this.Group.style.display = "block";
497 Element.prototype.translate = function (x, y) {
498 if (x == undefined && y == undefined) {
499 return {x: this._.tx, y: this._.ty};
503 if (this.type == "path") {
504 var path = this.attrs.path;
505 path = Raphael.pathToRelative(path);
508 this.attr({path: path.join(" ")});
510 this.setBox({x: this._.tx, y: this._.ty});
513 Element.prototype.getBBox = function () {
521 Element.prototype.remove = function () {
522 this[0].parentNode.removeChild(this[0]);
523 this.Group.parentNode.removeChild(this.Group);
524 this.shape && this.shape.parentNode.removeChild(this.shape);
526 Element.prototype.attr = function () {
527 if (arguments.length == 1 && typeof arguments[0] == "string") {
528 if (arguments[0] == "translation") {
529 return this.translate();
531 return this.attrs[arguments[0]];
533 if (this.attrs && arguments.length == 1 && arguments[0] instanceof Array) {
535 for (var i = 0, ii = arguments[0].length; i < ii; i++) {
536 values[arguments[0][i]] = this.attrs[arguments[0][i]];
540 if (this[0].tagName.toLowerCase() == "group") {
541 var children = this[0].childNodes;
542 this.attrs = this.attrs || {};
543 if (arguments.length == 2) {
544 this.attrs[arguments[0]] = arguments[1];
545 } else if (arguments.length == 1 || typeof arguments[0] == "object") {
546 for (var j in arguments[0]) {
547 this.attrs[j] = arguments[0][j];
550 for (var i = 0, ii = children.length; i < ii; i++) {
551 this.attr.apply(new item(children[i], this[0], this.vml), arguments);
555 if (arguments.length == 2) {
557 params[arguments[0]] = arguments[1];
559 if (arguments.length == 1 && typeof arguments[0] == "object") {
560 params = arguments[0];
563 R._.setFillAndStroke(this, params);
565 if (params.gradient) {
566 R._.addGrdientFill(this, params.gradient);
568 if (params.text && this.type == "text") {
569 this[0].string = params.text;
572 this[0].id = params.id;
578 Element.prototype.toFront = function () {
579 this.Group.parentNode.appendChild(this.Group);
582 Element.prototype.toBack = function () {
583 if (this.Group.parentNode.firstChild != this.Group) {
584 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
588 Element.prototype.insertAfter = function (element) {
589 if (element.Group.nextSibling) {
590 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
592 element.Group.parentNode.appendChild(this.Group);
596 Element.prototype.insertBefore = function (element) {
597 element.Group.parentNode.insertBefore(this.Group, element.Group);
600 for (var method in R._.element) {
601 Element.prototype[method] = R._.element[method];
603 R._.theCircle = function (vml, x, y, r) {
604 var g = document.createElement("rvml:group");
605 var o = document.createElement("rvml:oval");
607 vml.canvas.appendChild(g);
608 var res = new Element(o, g, vml);
609 R._.setFillAndStroke(res, {stroke: "#000", fill: "none"});
610 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
617 R._.theRect = function (vml, x, y, w, h, r) {
618 var g = document.createElement("rvml:group");
619 var o = document.createElement(r ? "rvml:roundrect" : "rvml:rect");
621 o.arcsize = r / (Math.min(w, h));
624 vml.canvas.appendChild(g);
625 var res = new Element(o, g, vml);
626 R._.setFillAndStroke(res, {stroke: "#000"});
627 res.setBox({x: x, y: y, width: w, height: h});
636 R._.theEllipse = function (vml, x, y, rx, ry) {
637 var g = document.createElement("rvml:group");
638 var o = document.createElement("rvml:oval");
640 vml.canvas.appendChild(g);
641 var res = new Element(o, g, vml);
642 R._.setFillAndStroke(res, {stroke: "#000"});
643 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
648 res.type = "ellipse";
651 R._.theImage = function (vml, src, x, y, w, h) {
652 var g = document.createElement("rvml:group");
653 var o = document.createElement("rvml:image");
656 vml.canvas.appendChild(g);
657 var res = new Element(o, g, vml);
659 res.setBox({x: x, y: y, width: w, height: h});
666 R._.theText = function (vml, x, y, text) {
668 var g = document.createElement("rvml:group"), gs = g.style;
669 var el = document.createElement("rvml:shape"), ol = el.style;
670 var path = document.createElement("rvml:path"), ps = path.style;
671 path.v = ["m", Math.round(x), ", ", Math.round(y - 2), "l", Math.round(x) + 1, ", ", Math.round(y - 2)].join("");
672 path.textpathok = true;
673 ol.width = vml.width;
674 ol.height = vml.height;
675 gs.position = "absolute";
678 gs.width = vml.width;
679 gs.height = vml.height;
680 var o = document.createElement("rvml:textpath");
683 o.coordsize = vml.coordsize;
684 o.coordorigin = vml.coordorigin;
686 el.appendChild(path);
688 vml.canvas.appendChild(g);
689 var res = new Element(o, g, vml);
697 R._.setFillAndStroke(res, {stroke: "none", fill: "#000"});
700 R._.theGroup = function (vml) {
701 var el = document.createElement("rvml:group"), els = el.style;
702 els.position = "absolute";
705 els.width = vml.width;
706 els.height = vml.height;
708 vml.canvas.appendChild(el);
710 var res = new Element(el, el, vml);
712 if (f.charAt(0) != "_" && typeof vml[f] == "function") {
713 res[f] = (function (f) {
715 var e = vml[f].apply(vml, arguments);
716 el.appendChild(e[0].parentNode);
725 R._create = function () {
726 // container, width, height
727 // x, y, width, height
728 var container, width, height;
729 if (typeof arguments[0] == "string") {
730 container = document.getElementById(arguments[0]);
731 width = arguments[1];
732 height = arguments[2];
734 if (typeof arguments[0] == "object") {
735 container = arguments[0];
736 width = arguments[1];
737 height = arguments[2];
739 if (typeof arguments[0] == "number") {
743 width = arguments[2];
744 height = arguments[3];
747 throw new Error("VML container not found.");
749 if (!document.namespaces["rvml"]) {
750 document.namespaces.add("rvml","urn:schemas-microsoft-com:vml");
751 document.createStyleSheet().addRule("rvml\\:*", "behavior:url(#default#VML)");
753 var c = document.createElement("div"),
754 d = document.createElement("div"),
755 r = R._.paper.canvas = document.createElement("rvml:group"),
756 cs = c.style, rs = r.style;
757 R._.paper.width = width;
758 R._.paper.height = height;
759 width = width || "320px";
760 height = height || "200px";
761 cs.clip = "rect(0 " + width + " " + height + " 0)";
764 cs.position = "absolute";
765 rs.position = "absolute";
766 d.style.position = "relative";
769 r.coordsize = (width == "100%" ? width : parseFloat(width)) + " " + (height == "100%" ? height : parseFloat(height));
770 r.coordorigin = "0 0";
772 var b = document.createElement("rvml:rect"), bs = b.style;
773 bs.left = bs.top = 0;
775 bs.height = rs.height;
776 b.filled = b.stroked = "f";
781 if (container == 1) {
782 document.body.appendChild(d);
783 cs.position = "absolute";
795 cs.width = container.style.width = width;
796 cs.height = container.style.height = height;
797 if (container.firstChild) {
798 container.insertBefore(d, container.firstChild);
800 container.appendChild(d);
803 for (var prop in R._.paper) {
804 container[prop] = R._.paper[prop];
806 container.clear = function () {
808 for (var i = 0, ii = r.childNodes.length; i < ii; i++) {
809 if (r.childNodes[i] != b) {
810 todel.push(r.childNodes[i]);
813 for (i = 0, ii = todel.length; i < ii; i++) {
814 r.removeChild(todel[i]);
819 R._.paper.remove = function () {
820 this.canvas.parentNode.parentNode.parentNode.removeChild(this.canvas.parentNode.parentNode);
822 R._.paper.safari = function () {};