1 // ┌─────────────────────────────────────────────────────────────────────┐ \\
2 // │ Raphaël 2 - JavaScript Vector Library │ \\
3 // ├─────────────────────────────────────────────────────────────────────┤ \\
5 // ├─────────────────────────────────────────────────────────────────────┤ \\
6 // │ Copyright (c) 2008-2011 Dmitry Baranovskiy (http://raphaeljs.com) │ \\
7 // │ Copyright (c) 2008-2011 Sencha Labs (http://sencha.com) │ \\
8 // │ Licensed under the MIT (http://raphaeljs.com/license.html) license. │ \\
9 // └─────────────────────────────────────────────────────────────────────┘ \\
10 window.Raphael.svg && function (R) {
11 var has = "hasOwnProperty",
21 var xlink = "http://www.w3.org/1999/xlink",
23 block: "M5,0 0,2.5 5,5z",
24 classic: "M5,0 0,2.5 5,5 3.5,3 3.5,2z",
25 diamond: "M2.5,0 5,2.5 2.5,5 0,2.5z",
26 open: "M6,1 1,3.5 6,6",
27 oval: "M2.5,0A2.5,2.5,0,0,1,2.5,5 2.5,2.5,0,0,1,2.5,0z"
30 R.toString = function () {
31 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
33 var $ = function (el, attr) {
35 if (typeof el == "string") {
38 for (var key in attr) if (attr[has](key)) {
39 if (key.substring(0, 6) == "xlink:") {
40 el.setAttributeNS(xlink, key.substring(6), Str(attr[key]));
42 el.setAttribute(key, Str(attr[key]));
46 el = R._g.doc.createElementNS("http://www.w3.org/2000/svg", el);
47 el.style && (el.style.webkitTapHighlightColor = "rgba(0,0,0,0)");
52 rgGrad = /^url\(#(.*)\)$/,
53 removeGradientFill = function (node, paper) {
54 var oid = node.getAttribute("fill");
55 oid = oid && oid.match(rgGrad);
56 if (oid && !--gradients[oid[1]]) {
57 delete gradients[oid[1]];
58 paper.defs.removeChild(g.doc.getElementById(oid[1]));
61 addGradientFill = function (element, gradient) {
63 id = element.id + gradient,
68 el = R._g.doc.getElementById(id);
70 gradient = Str(gradient).replace(R._radial_gradient, function (all, _fx, _fy) {
75 var dir = ((fy > .5) * 2 - 1);
76 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
77 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
79 (fy = fy.toFixed(5) - 1e-5 * dir);
83 gradient = gradient.split(/\s*\-\s*/);
84 if (type == "linear") {
85 var angle = gradient.shift();
86 angle = -toFloat(angle);
90 var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
91 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
95 vector[0] = -vector[2];
99 vector[1] = -vector[3];
103 var dots = R._parseDots(gradient);
107 if (element.gradient) {
108 SVG.defs.removeChild(element.gradient);
109 delete element.gradient;
112 id = id.replace(/[\(\)\s,\xb0#]/g, "-");
113 el = $(type + "Gradient", {id: id});
114 element.gradient = el;
115 $(el, type == "radial" ? {
123 gradientTransform: element.matrix.invert()
125 SVG.defs.appendChild(el);
126 for (var i = 0, ii = dots.length; i < ii; i++) {
127 el.appendChild($("stop", {
128 offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
129 "stop-color": dots[i].color || "#fff"
134 fill: "url(#" + id + ")",
143 updatePosition = function (o) {
144 var bbox = o.getBBox(1);
145 $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
147 addArrow = function (o, value, isEnd) {
148 if (o.type == "path") {
149 var values = Str(value).toLowerCase().split("-"),
151 se = isEnd ? "end" : "start",
154 stroke = attrs["stroke-width"],
175 case "wide": h = 5; break;
176 case "narrow": h = 2; break;
177 case "long": w = 5; break;
178 case "short": w = 2; break;
181 if (type == "open") {
186 refX = isEnd ? 4 : 1;
200 o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
201 o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
203 o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
204 o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
209 if (type != "none") {
210 var pathId = "raphael-marker-" + type,
211 markerId = "raphael-marker-" + se + type + w + h;
212 if (!g.doc.getElementById(pathId)) {
213 p.defs.appendChild($($("path"), {
214 "stroke-linecap": "round",
218 markerCounter[pathId] = 1;
220 markerCounter[pathId]++;
222 var marker = g.doc.getElementById(markerId),
225 marker = $($("marker"), {
234 "xlink:href": "#" + pathId,
235 transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
236 "stroke-width": 1 / ((w / t + h / t) / 2)
238 marker.appendChild(use);
239 p.defs.appendChild(marker);
240 markerCounter[markerId] = 1;
242 markerCounter[markerId]++;
243 use = marker.getElementsByTagName("use")[0];
246 var delta = dx * (type != "diamond" && type != "oval");
248 from = o._.arrows.startdx * stroke || 0;
249 to = R.getTotalLength(attrs.path) - delta * stroke;
251 from = delta * stroke;
252 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
255 attr["marker-" + se] = "url(#" + markerId + ")";
257 attr.d = Raphael.getSubpath(attrs.path, from, to);
260 o._.arrows[se + "Path"] = pathId;
261 o._.arrows[se + "Marker"] = markerId;
262 o._.arrows[se + "dx"] = delta;
263 o._.arrows[se + "Type"] = type;
264 o._.arrows[se + "String"] = value;
267 from = o._.arrows.startdx * stroke || 0;
268 to = R.getTotalLength(attrs.path) - from;
271 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
273 o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
274 delete o._.arrows[se + "Path"];
275 delete o._.arrows[se + "Marker"];
276 delete o._.arrows[se + "dx"];
277 delete o._.arrows[se + "Type"];
278 delete o._.arrows[se + "String"];
280 for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
281 var item = g.doc.getElementById(attr);
282 item && item.parentNode.removeChild(item);
286 setFillAndStroke = function (o, params) {
293 "-..": [3, 1, 1, 1, 1, 1],
299 "--..": [8, 3, 1, 3, 1, 3]
303 addDashes = function (o, value) {
304 value = dasharray[lowerCase.call(value)];
306 var width = o.attrs["stroke-width"] || "1",
307 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
311 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
313 $(node, {"stroke-dasharray": dashes.join(",")});
316 for (var att in params) {
317 if (params[has](att)) {
318 if (!R._availableAttrs[has](att)) {
321 var value = params[att];
330 var pn = node.parentNode;
331 if (lowerCase.call(pn.tagName) != "a") {
333 pn.insertBefore(hl, node);
334 hl.appendChild(node);
337 if (att == "target" && value == "blank") {
338 pn.setAttributeNS(xlink, "show", "new");
340 pn.setAttributeNS(xlink, att, value);
344 node.style.cursor = value;
353 addArrow(o, value, 1);
356 var rect = Str(value).split(separator);
357 if (rect.length == 4) {
358 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
359 var el = $("clipPath"),
361 el.id = R._createUUID();
369 o.paper.defs.appendChild(el);
370 $(node, {"clip-path": "url(#" + el.id + ")"});
374 var clip = g.doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, E));
375 clip && clip.parentNode.removeChild(clip);
376 $(node, {"clip-path": E});
381 if (o.type == "path") {
382 $(node, {d: value ? attrs.path = R._pathToAbsolute(value) : "M0,0"});
385 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
386 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
391 node.setAttribute(att, value);
401 value = -attrs.x - (attrs.width || 0);
404 if (att == "rx" && o.type == "rect") {
408 node.setAttribute(att, value);
409 o.pattern && updatePosition(o);
413 node.setAttribute(att, value);
423 value = -attrs.y - (attrs.height || 0);
426 if (att == "ry" && o.type == "rect") {
430 node.setAttribute(att, value);
431 o.pattern && updatePosition(o);
435 if (o.type == "rect") {
436 $(node, {rx: value, ry: value});
438 node.setAttribute(att, value);
443 if (o.type == "image") {
444 node.setAttributeNS(xlink, "href", value);
448 if (o._.sx != 1 || o._.sy != 1) {
449 value /= mmax(abs(o._.sx), abs(o._.sy)) || 1;
451 if (o.paper._vbSize) {
452 value *= o.paper._vbSize;
454 node.setAttribute(att, value);
455 if (attrs["stroke-dasharray"]) {
456 addDashes(o, attrs["stroke-dasharray"]);
459 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
460 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
463 case "stroke-dasharray":
467 var isURL = Str(value).match(R._ISURL);
471 el.id = R._createUUID();
472 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
473 $(ig, {x: 0, y: 0, "xlink:href": isURL[1]});
477 R._preload(isURL[1], function () {
478 var w = this.offsetWidth,
479 h = this.offsetHeight;
480 $(el, {width: w, height: h});
481 $(ig, {width: w, height: h});
485 o.paper.defs.appendChild(el);
486 node.style.fill = "url(#" + el.id + ")";
487 $(node, {fill: "url(#" + el.id + ")"});
489 o.pattern && updatePosition(o);
492 var clr = R.getRGB(value);
494 delete params.gradient;
495 delete attrs.gradient;
496 !R.is(attrs.opacity, "undefined") &&
497 R.is(params.opacity, "undefined") &&
498 $(node, {opacity: attrs.opacity});
499 !R.is(attrs["fill-opacity"], "undefined") &&
500 R.is(params["fill-opacity"], "undefined") &&
501 $(node, {"fill-opacity": attrs["fill-opacity"]});
502 } else if ((o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value)) {
503 if ("opacity" in attrs || "fill-opacity" in attrs) {
504 var gradient = g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
506 var stops = gradient.getElementsByTagName("stop");
507 $(stops[stops.length - 1], {"stop-opacity": ("opacity" in attrs ? attrs.opacity : 1) * ("fill-opacity" in attrs ? attrs["fill-opacity"] : 1)});
510 attrs.gradient = value;
514 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
516 clr = R.getRGB(value);
517 node.setAttribute(att, clr.hex);
518 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
519 if (att == "stroke" && o._.arrows) {
520 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
521 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
525 (o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value);
528 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
529 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
533 if (attrs.gradient) {
534 gradient = g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
536 stops = gradient.getElementsByTagName("stop");
537 $(stops[stops.length - 1], {"stop-opacity": value});
542 att == "font-size" && (value = toInt(value, 10) + "px");
543 var cssrule = att.replace(/(\-.)/g, function (w) {
544 return w.substring(1).toUpperCase();
546 node.style[cssrule] = value;
548 node.setAttribute(att, value);
557 tuneText = function (el, params) {
558 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
563 fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
565 if (params[has]("text")) {
566 a.text = params.text;
567 while (node.firstChild) {
568 node.removeChild(node.firstChild);
570 var texts = Str(params.text).split("\n"),
573 for (var i = 0, ii = texts.length; i < ii; i++) {
575 i && $(tspan, {dy: fontSize * leading, x: a.x});
576 tspan.appendChild(R._g.doc.createTextNode(texts[i]));
577 node.appendChild(tspan);
581 tspans = node.getElementsByTagName("tspan");
582 for (i = 0, ii = tspans.length; i < ii; i++) if (i) {
583 $(tspans[i], {dy: fontSize * leading, x: a.x});
585 $(tspans[0], {dy: 0});
588 $(node, {x: a.x, y: a.y});
590 var bb = el._getBBox(),
591 dif = a.y - (bb.y + bb.height / 2);
592 dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
594 Element = function (node, svg) {
599 [ property (object) ]
601 * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
603 | // draw a circle at coordinate 10,10 with radius of 10
604 | var c = paper.circle(10, 10, 10);
605 | c.node.onclick = function () {
606 | c.attr("fill", "red");
609 this[0] = this.node = node;
612 [ property (object) ]
614 * Internal reference to @Raphael object. In case it is not available.
616 | Raphael.el.red = function () {
617 | var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
619 | this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
625 [ property (number) ]
627 * Unique id of the element. Especially usesful when you want to listen to events of the element,
628 * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
631 node.raphaelid = this.id;
632 this.matrix = R.matrix();
633 this.realPath = null;
636 [ property (object) ]
638 * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
640 | Raphael.el.cross = function () {
641 | this.attr({fill: "red"});
642 | this.paper.path("M10,10L50,50M50,10L10,50")
643 | .attr({stroke: "red"});
647 this.attrs = this.attrs || {};
657 !svg.bottom && (svg.bottom = this);
660 [ property (object) ]
662 * Reference to the previous element in the hierarchy.
665 svg.top && (svg.top.next = this);
669 [ property (object) ]
671 * Reference to the next element in the hierarchy.
677 Element.prototype = elproto;
678 elproto.constructor = Element;
680 R._engine.path = function (pathString, SVG) {
682 SVG.canvas && SVG.canvas.appendChild(el);
683 var p = new Element(el, SVG);
685 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
692 * Adds rotation by given angle around given point to the list of
693 * transformations of the element.
695 - deg (number) angle in degrees
696 - cx (number) #optional x coordinate of the centre of rotation
697 - cy (number) #optional y coordinate of the centre of rotation
698 * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
701 elproto.rotate = function (deg, cx, cy) {
705 deg = Str(deg).split(separator);
706 if (deg.length - 1) {
707 cx = toFloat(deg[1]);
708 cy = toFloat(deg[2]);
710 deg = toFloat(deg[0]);
711 (cy == null) && (cx = cy);
712 if (cx == null || cy == null) {
713 var bbox = this.getBBox(1);
714 cx = bbox.x + bbox.width / 2;
715 cy = bbox.y + bbox.height / 2;
717 this.transform(this._.transform.concat([["r", deg, cx, cy]]));
724 * Adds scale by given amount relative to given point to the list of
725 * transformations of the element.
727 - sx (number) horisontal scale amount
728 - sy (number) vertical scale amount
729 - cx (number) #optional x coordinate of the centre of scale
730 - cy (number) #optional y coordinate of the centre of scale
731 * If cx & cy aren’t specified centre of the shape is used instead.
734 elproto.scale = function (sx, sy, cx, cy) {
738 sx = Str(sx).split(separator);
745 (sy == null) && (sy = sx);
746 (cy == null) && (cx = cy);
747 if (cx == null || cy == null) {
748 var bbox = this.getBBox(1);
750 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
751 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
752 this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
759 * Adds translation by given amount to the list of transformations of the element.
761 - dx (number) horisontal shift
762 - dy (number) vertical shift
765 elproto.translate = function (dx, dy) {
769 dx = Str(dx).split(separator);
773 dx = toFloat(dx[0]) || 0;
775 this.transform(this._.transform.concat([["t", dx, dy]]));
782 * Adds transformation to the element which is separate to other attributes,
783 * i.e. translation doesn’t change `x` or `y` of the rectange. The format
784 * of transformation string is similar to the path string syntax:
785 | "t100,100r30,100,100s2,2,100,100r45s1.5"
786 * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
787 * scale and `m` is for matrix.
789 * So, the example line above could be read like “translate by 100, 100; rotate 30° around 100, 100; scale twice around 100, 100;
790 * rotate 45° around centre; scale 1.5 times relative to centre”. As you can see rotate and scale commands have origin
791 * coordinates as optional parameters, the default is the centre point of the element.
792 * Matrix accepts six parameters.
794 | var el = paper.rect(10, 20, 300, 200);
795 | // translate 100, 100, rotate 45°, translate -100, 0
796 | el.transform("t100,100r45t-100,0");
797 | // if you want you can append or prepend transformations
798 | el.transform("...t50,50");
799 | el.transform("s2...");
801 | el.transform("t50,50...t-50-50");
802 | // to reset transformation call method with empty string
804 | // to get current value call it without parameters
805 | console.log(el.transform());
807 - tstr (string) #optional transformation string
808 * If tstr isn’t specified
809 = (string) current transformation string
813 elproto.transform = function (tstr) {
818 R._extractTransform(this, tstr);
820 this.clip && $(this.clip, {transform: this.matrix.invert()});
821 this.pattern && updatePosition(this);
822 this.node && $(this.node, {transform: this.matrix});
824 if (_.sx != 1 || _.sy != 1) {
825 var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
826 this.attr({"stroke-width": sw});
835 * Makes element invisible. See @Element.show.
838 elproto.hide = function () {
839 !this.removed && this.paper.safari(this.node.style.display = "none");
846 * Makes element visible. See @Element.hide.
849 elproto.show = function () {
850 !this.removed && this.paper.safari(this.node.style.display = "");
857 * Removes element form the paper.
859 elproto.remove = function () {
863 eve.unbind("*.*." + this.id);
864 R._tear(this, this.paper);
865 this.node.parentNode.removeChild(this.node);
866 for (var i in this) {
871 elproto._getBBox = function () {
872 if (this.node.style.display == "none") {
878 bbox = this.node.getBBox();
880 // Firefox 3.0.x plays badly here
891 * Sets the attributes of the element.
893 - attrName (string) attribute’s name
894 - value (string) value
896 - params (object) object of name/value pairs
898 - attrName (string) attribute’s name
900 - attrNames (array) in this case method returns array of current values for given attribute names
901 = (object) @Element if attrsName & value or params are passed in.
902 = (...) value of the attribute if only attrsName is passed in.
903 = (array) array of values of the attribute if attrsNames is passed in.
904 = (object) object of attributes if nothing is passed in.
905 > Possible parameters
906 # <p>Please refer to the <a href="http://www.w3.org/TR/SVG/" title="The W3C Recommendation for the SVG language describes these properties in detail.">SVG specification</a> for an explanation of these parameters.</p>
907 o arrow-end (string) arrowhead on the end of the path. The format for string is `<type>[-<width>[-<length>]]`. Possible types: `classic`, `block`, `open`, `oval`, `diamond`, `none`, width: `wide`, `narrow`, `midium`, length: `long`, `short`, `midium`.
908 o clip-rect (string) comma or space separated values: x, y, width and height
909 o cursor (string) CSS type of the cursor
912 o fill (string) colour, gradient or image
913 o fill-opacity (number)
915 o font-family (string)
916 o font-size (number) font size in pixels
917 o font-weight (string)
919 o href (string) URL, if specified element behaves as hyperlink
921 o path (string) SVG path string format
925 o src (string) image URL, only works for @Element.image element
926 o stroke (string) stroke colour
927 o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
928 o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
929 o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
930 o stroke-miterlimit (number)
931 o stroke-opacity (number)
932 o stroke-width (number) stroke width in pixels, default is '1'
933 o target (string) used with href
934 o text (string) contents of the text element. Use `\n` for multiline text
935 o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
936 o title (string) will create tooltip with a given text
937 o transform (string) see @Element.transform
942 * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
943 * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
945 * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
946 * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
947 * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
949 # <p>Please refer to <a href="http://www.w3.org/TR/SVG/paths.html#PathData" title="Details of a path’s data attribute’s format are described in the SVG specification.">SVG documentation regarding path string</a>. Raphaël fully supports it.</p>
952 # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
953 # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
954 # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
955 # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200, 100, 0)</code>”)</li>
956 # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%, 175%, 0%)</code>”)</li>
957 # <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200, 100, 0, .5)</code>”)</li>
958 # <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%, 175%, 0%, 50%)</code>”)</li>
959 # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5, 0.25, 1)</code>”)</li>
960 # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
961 # <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
962 # <li>hsl(•••, •••, •••) — almost the same as hsb, see <a href="http://en.wikipedia.org/wiki/HSL_and_HSV" title="HSL and HSV - Wikipedia, the free encyclopedia">Wikipedia page</a></li>
963 # <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
964 # <li>hsla(•••, •••, •••) — same as above, but with opacity</li>
965 # <li>Optionally for hsb and hsl you could specify hue as a degree: “<code>hsl(240deg, 1, .5)</code>” or, if you want to go fancy, “<code>hsl(240°, 1, .5)</code>”</li>
968 elproto.attr = function (name, value) {
974 for (var a in this.attrs) if (this.attrs[has](a)) {
975 res[a] = this.attrs[a];
977 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
978 res.transform = this._.transform;
981 if (value == null && R.is(name, "string")) {
982 if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
983 return this.attrs.gradient;
985 if (name == "transform") {
986 return this._.transform;
988 var names = name.split(separator),
990 for (var i = 0, ii = names.length; i < ii; i++) {
992 if (name in this.attrs) {
993 out[name] = this.attrs[name];
994 } else if (R.is(this.paper.customAttributes[name], "function")) {
995 out[name] = this.paper.customAttributes[name].def;
997 out[name] = R._availableAttrs[name];
1000 return ii - 1 ? out : out[names[0]];
1002 if (value == null && R.is(name, "array")) {
1004 for (i = 0, ii = name.length; i < ii; i++) {
1005 out[name[i]] = this.attr(name[i]);
1009 if (value != null) {
1011 params[name] = value;
1012 } else if (name != null && R.is(name, "object")) {
1015 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1016 var par = this.paper.customAttributes[key].apply(this, [].concat(params[key]));
1017 this.attrs[key] = params[key];
1018 for (var subkey in par) if (par[has](subkey)) {
1019 params[subkey] = par[subkey];
1022 setFillAndStroke(this, params);
1029 * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
1032 elproto.toFront = function () {
1036 this.node.parentNode.appendChild(this.node);
1037 var svg = this.paper;
1038 svg.top != this && R._tofront(this, svg);
1045 * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
1048 elproto.toBack = function () {
1052 if (this.node.parentNode.firstChild != this.node) {
1053 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1054 R._toback(this, this.paper);
1055 var svg = this.paper;
1060 * Element.insertAfter
1063 * Inserts current object after the given one.
1066 elproto.insertAfter = function (element) {
1070 var node = element.node || element[element.length - 1].node;
1071 if (node.nextSibling) {
1072 node.parentNode.insertBefore(this.node, node.nextSibling);
1074 node.parentNode.appendChild(this.node);
1076 R._insertafter(this, element, this.paper);
1080 * Element.insertBefore
1083 * Inserts current object before the given one.
1086 elproto.insertBefore = function (element) {
1090 var node = element.node || element[0].node;
1091 node.parentNode.insertBefore(this.node, node);
1092 R._insertbefore(this, element, this.paper);
1095 elproto.blur = function (size) {
1096 // Experimental. No Safari support. Use it on your own risk.
1099 var fltr = $("filter"),
1100 blur = $("feGaussianBlur");
1101 t.attrs.blur = size;
1102 fltr.id = R._createUUID();
1103 $(blur, {stdDeviation: +size || 1.5});
1104 fltr.appendChild(blur);
1105 t.paper.defs.appendChild(fltr);
1107 $(t.node, {filter: "url(#" + fltr.id + ")"});
1110 t._blur.parentNode.removeChild(t._blur);
1112 delete t.attrs.blur;
1114 t.node.removeAttribute("filter");
1117 R._engine.circle = function (svg, x, y, r) {
1118 var el = $("circle");
1119 svg.canvas && svg.canvas.appendChild(el);
1120 var res = new Element(el, svg);
1121 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1122 res.type = "circle";
1126 R._engine.rect = function (svg, x, y, w, h, r) {
1128 svg.canvas && svg.canvas.appendChild(el);
1129 var res = new Element(el, svg);
1130 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1135 R._engine.ellipse = function (svg, x, y, rx, ry) {
1136 var el = $("ellipse");
1137 svg.canvas && svg.canvas.appendChild(el);
1138 var res = new Element(el, svg);
1139 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1140 res.type = "ellipse";
1144 R._engine.image = function (svg, src, x, y, w, h) {
1145 var el = $("image");
1146 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1147 el.setAttributeNS(xlink, "href", src);
1148 svg.canvas && svg.canvas.appendChild(el);
1149 var res = new Element(el, svg);
1150 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1154 R._engine.text = function (svg, x, y, text) {
1156 // $(el, {x: x, y: y, "text-anchor": "middle"});
1157 svg.canvas && svg.canvas.appendChild(el);
1158 var res = new Element(el, svg);
1162 "text-anchor": "middle",
1164 font: R._availableAttrs.font,
1169 setFillAndStroke(res, res.attrs);
1172 R._engine.setSize = function (width, height) {
1173 this.width = width || this.width;
1174 this.height = height || this.height;
1175 this.canvas.setAttribute("width", this.width);
1176 this.canvas.setAttribute("height", this.height);
1177 if (this._viewBox) {
1178 this.setViewBox.apply(this, this._viewBox);
1182 R._engine.create = function () {
1183 var con = R._getContainer.apply(0, arguments),
1184 container = con && con.container,
1188 height = con.height;
1190 throw new Error("SVG container not found.");
1192 var cnvs = $("svg"),
1193 css = "overflow:hidden;",
1197 width = width || 512;
1198 height = height || 342;
1203 xmlns: "http://www.w3.org/2000/svg"
1205 if (container == 1) {
1206 cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
1207 R._g.doc.body.appendChild(cnvs);
1210 cnvs.style.cssText = css + "position:relative";
1211 if (container.firstChild) {
1212 container.insertBefore(cnvs, container.firstChild);
1214 container.appendChild(cnvs);
1217 container = new R._Paper;
1218 container.width = width;
1219 container.height = height;
1220 container.canvas = cnvs;
1221 // plugins.call(container, container, R.fn);
1223 container._left = container._top = 0;
1224 isFloating && (container.renderfix = function () {});
1225 container.renderfix();
1228 R._engine.setViewBox = function (x, y, w, h, fit) {
1229 eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
1230 var size = mmax(w / this.width, h / this.height),
1232 aspectRatio = fit ? "meet" : "xMinYMin",
1239 delete this._vbSize;
1240 vb = "0 0 " + this.width + S + this.height;
1242 this._vbSize = size;
1243 vb = x + S + y + S + w + S + h;
1247 preserveAspectRatio: aspectRatio
1249 while (size && top) {
1250 sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
1251 top.attr({"stroke-width": sw});
1256 this._viewBox = [x, y, w, h, !!fit];
1263 * Fixes the issue of Firefox and IE9 regarding subpixel rendering. If paper is dependant
1264 * on other elements after reflow it could shift half pixel which cause for lines to lost their crispness.
1265 * This method fixes the issue.
1267 Special thanks to Mariusz Nowak (http://www.medikoo.com/) for this method.
1269 R.prototype.renderfix = function () {
1270 var cnvs = this.canvas,
1272 pos = cnvs.getScreenCTM(),
1277 this._left = (this._left + left) % 1;
1278 s.left = this._left + "px";
1281 this._top = (this._top + top) % 1;
1282 s.top = this._top + "px";
1290 * Clears the paper, i.e. removes all the elements.
1292 R.prototype.clear = function () {
1293 R.eve("clear", this);
1294 var c = this.canvas;
1295 while (c.firstChild) {
1296 c.removeChild(c.firstChild);
1298 this.bottom = this.top = null;
1299 (this.desc = $("desc")).appendChild(R._g.doc.createTextNode("Created with Rapha\xebl " + R.version));
1300 c.appendChild(this.desc);
1301 c.appendChild(this.defs = $("defs"));
1307 * Removes the paper from the DOM.
1309 R.prototype.remove = function () {
1310 eve("remove", this);
1311 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1312 for (var i in this) {
1313 this[i] = removed(i);
1316 var setproto = R.st;
1317 for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
1318 setproto[method] = (function (methodname) {
1319 return function () {
1320 var arg = arguments;
1321 return this.forEach(function (el) {
1322 el[methodname].apply(el, arg);