1 // ┌─────────────────────────────────────────────────────────────────────┐ \\
2 // │ Raphaël - 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",
23 var xlink = "http://www.w3.org/1999/xlink",
25 block: "M5,0 0,2.5 5,5z",
26 classic: "M5,0 0,2.5 5,5 3.5,3 3.5,2z",
27 diamond: "M2.5,0 5,2.5 2.5,5 0,2.5z",
28 open: "M6,1 1,3.5 6,6",
29 oval: "M2.5,0A2.5,2.5,0,0,1,2.5,5 2.5,2.5,0,0,1,2.5,0z"
32 R.toString = function () {
33 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
35 var $ = function (el, attr) {
37 if (typeof el == "string") {
40 for (var key in attr) if (attr[has](key)) {
41 if (key.substring(0, 6) == "xlink:") {
42 el.setAttributeNS(xlink, key.substring(6), Str(attr[key]));
44 el.setAttribute(key, Str(attr[key]));
48 el = R._g.doc.createElementNS("http://www.w3.org/2000/svg", el);
49 el.style && (el.style.webkitTapHighlightColor = "rgba(0,0,0,0)");
53 addGradientFill = function (element, gradient) {
55 id = element.id + gradient,
60 el = R._g.doc.getElementById(id);
62 gradient = Str(gradient).replace(R._radial_gradient, function (all, _fx, _fy) {
67 var dir = ((fy > .5) * 2 - 1);
68 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
69 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
71 (fy = fy.toFixed(5) - 1e-5 * dir);
75 gradient = gradient.split(/\s*\-\s*/);
76 if (type == "linear") {
77 var angle = gradient.shift();
78 angle = -toFloat(angle);
82 var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
83 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
87 vector[0] = -vector[2];
91 vector[1] = -vector[3];
95 var dots = R._parseDots(gradient);
99 id = id.replace(/[\(\)\s,\xb0#]/g, "_");
101 if (element.gradient && id != element.gradient.id) {
102 SVG.defs.removeChild(element.gradient);
103 delete element.gradient;
106 if (!element.gradient) {
107 el = $(type + "Gradient", {id: id});
108 element.gradient = el;
109 $(el, type == "radial" ? {
117 gradientTransform: element.matrix.invert()
119 SVG.defs.appendChild(el);
120 for (var i = 0, ii = dots.length; i < ii; i++) {
121 el.appendChild($("stop", {
122 offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
123 "stop-color": dots[i].color || "#fff"
129 fill: "url(#" + id + ")",
138 updatePosition = function (o) {
139 var bbox = o.getBBox(1);
140 $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
142 addArrow = function (o, value, isEnd) {
143 if (o.type == "path") {
144 var values = Str(value).toLowerCase().split("-"),
146 se = isEnd ? "end" : "start",
149 stroke = attrs["stroke-width"],
170 case "wide": h = 5; break;
171 case "narrow": h = 2; break;
172 case "long": w = 5; break;
173 case "short": w = 2; break;
176 if (type == "open") {
181 refX = isEnd ? 4 : 1;
195 o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
196 o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
198 o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
199 o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
204 if (type != "none") {
205 var pathId = "raphael-marker-" + type,
206 markerId = "raphael-marker-" + se + type + w + h;
207 if (!R._g.doc.getElementById(pathId)) {
208 p.defs.appendChild($($("path"), {
209 "stroke-linecap": "round",
213 markerCounter[pathId] = 1;
215 markerCounter[pathId]++;
217 var marker = R._g.doc.getElementById(markerId),
220 marker = $($("marker"), {
229 "xlink:href": "#" + pathId,
230 transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
231 "stroke-width": 1 / ((w / t + h / t) / 2)
233 marker.appendChild(use);
234 p.defs.appendChild(marker);
235 markerCounter[markerId] = 1;
237 markerCounter[markerId]++;
238 use = marker.getElementsByTagName("use")[0];
241 var delta = dx * (type != "diamond" && type != "oval");
243 from = o._.arrows.startdx * stroke || 0;
244 to = R.getTotalLength(attrs.path) - delta * stroke;
246 from = delta * stroke;
247 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
250 attr["marker-" + se] = "url(#" + markerId + ")";
252 attr.d = Raphael.getSubpath(attrs.path, from, to);
255 o._.arrows[se + "Path"] = pathId;
256 o._.arrows[se + "Marker"] = markerId;
257 o._.arrows[se + "dx"] = delta;
258 o._.arrows[se + "Type"] = type;
259 o._.arrows[se + "String"] = value;
262 from = o._.arrows.startdx * stroke || 0;
263 to = R.getTotalLength(attrs.path) - from;
266 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
268 o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
269 delete o._.arrows[se + "Path"];
270 delete o._.arrows[se + "Marker"];
271 delete o._.arrows[se + "dx"];
272 delete o._.arrows[se + "Type"];
273 delete o._.arrows[se + "String"];
275 for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
276 var item = R._g.doc.getElementById(attr);
277 item && item.parentNode.removeChild(item);
287 "-..": [3, 1, 1, 1, 1, 1],
293 "--..": [8, 3, 1, 3, 1, 3]
295 addDashes = function (o, value, params) {
296 value = dasharray[Str(value).toLowerCase()];
298 var width = o.attrs["stroke-width"] || "1",
299 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
303 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
305 $(o.node, {"stroke-dasharray": dashes.join(",")});
308 setFillAndStroke = function (o, params) {
311 vis = node.style.visibility;
312 node.style.visibility = "hidden";
313 for (var att in params) {
314 if (params[has](att)) {
315 if (!R._availableAttrs[has](att)) {
318 var value = params[att];
327 var pn = node.parentNode;
328 if (pn.tagName.toLowerCase() != "a") {
330 pn.insertBefore(hl, node);
331 hl.appendChild(node);
334 if (att == "target" && value == "blank") {
335 pn.setAttributeNS(xlink, "show", "new");
337 pn.setAttributeNS(xlink, att, value);
341 node.style.cursor = value;
350 addArrow(o, value, 1);
353 var rect = Str(value).split(separator);
354 if (rect.length == 4) {
355 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
356 var el = $("clipPath"),
358 el.id = R.createUUID();
366 o.paper.defs.appendChild(el);
367 $(node, {"clip-path": "url(#" + el.id + ")"});
371 var path = node.getAttribute("clip-path");
373 var clip = R._g.doc.getElementById(path.replace(/(^url\(#|\)$)/g, E));
374 clip && clip.parentNode.removeChild(clip);
375 $(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"], params);
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":
464 addDashes(o, value, params);
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 = R._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 = R._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);
555 node.style.visibility = vis;
558 tuneText = function (el, params) {
559 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
564 fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
566 if (params[has]("text")) {
567 a.text = params.text;
568 while (node.firstChild) {
569 node.removeChild(node.firstChild);
571 var texts = Str(params.text).split("\n"),
574 for (var i = 0, ii = texts.length; i < ii; i++) {
576 i && $(tspan, {dy: fontSize * leading, x: a.x});
577 tspan.appendChild(R._g.doc.createTextNode(texts[i]));
578 node.appendChild(tspan);
582 tspans = node.getElementsByTagName("tspan");
583 for (i = 0, ii = tspans.length; i < ii; i++) if (i) {
584 $(tspans[i], {dy: fontSize * leading, x: a.x});
586 $(tspans[0], {dy: 0});
589 $(node, {x: a.x, y: a.y});
591 var bb = el._getBBox(),
592 dif = a.y - (bb.y + bb.height / 2);
593 dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
595 Element = function (node, svg) {
600 [ property (object) ]
602 * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
604 * Note: Don’t mess with it.
606 | // draw a circle at coordinate 10,10 with radius of 10
607 | var c = paper.circle(10, 10, 10);
608 | c.node.onclick = function () {
609 | c.attr("fill", "red");
612 this[0] = this.node = node;
615 [ property (object) ]
617 * Internal reference to @Raphael object. In case it is not available.
619 | Raphael.el.red = function () {
620 | var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
622 | this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
628 [ property (number) ]
630 * Unique id of the element. Especially usesful when you want to listen to events of the element,
631 * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
634 node.raphaelid = this.id;
635 this.matrix = R.matrix();
636 this.realPath = null;
639 [ property (object) ]
641 * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
643 | Raphael.el.cross = function () {
644 | this.attr({fill: "red"});
645 | this.paper.path("M10,10L50,50M50,10L10,50")
646 | .attr({stroke: "red"});
650 this.attrs = this.attrs || {};
660 !svg.bottom && (svg.bottom = this);
663 [ property (object) ]
665 * Reference to the previous element in the hierarchy.
668 svg.top && (svg.top.next = this);
672 [ property (object) ]
674 * Reference to the next element in the hierarchy.
680 Element.prototype = elproto;
681 elproto.constructor = Element;
683 R._engine.path = function (pathString, SVG) {
685 SVG.canvas && SVG.canvas.appendChild(el);
686 var p = new Element(el, SVG);
688 setFillAndStroke(p, {
699 * Adds rotation by given angle around given point to the list of
700 * transformations of the element.
702 - deg (number) angle in degrees
703 - cx (number) #optional x coordinate of the centre of rotation
704 - cy (number) #optional y coordinate of the centre of rotation
705 * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
708 elproto.rotate = function (deg, cx, cy) {
712 deg = Str(deg).split(separator);
713 if (deg.length - 1) {
714 cx = toFloat(deg[1]);
715 cy = toFloat(deg[2]);
717 deg = toFloat(deg[0]);
718 (cy == null) && (cx = cy);
719 if (cx == null || cy == null) {
720 var bbox = this.getBBox(1);
721 cx = bbox.x + bbox.width / 2;
722 cy = bbox.y + bbox.height / 2;
724 this.transform(this._.transform.concat([["r", deg, cx, cy]]));
731 * Adds scale by given amount relative to given point to the list of
732 * transformations of the element.
734 - sx (number) horisontal scale amount
735 - sy (number) vertical scale amount
736 - cx (number) #optional x coordinate of the centre of scale
737 - cy (number) #optional y coordinate of the centre of scale
738 * If cx & cy aren’t specified centre of the shape is used instead.
741 elproto.scale = function (sx, sy, cx, cy) {
745 sx = Str(sx).split(separator);
752 (sy == null) && (sy = sx);
753 (cy == null) && (cx = cy);
754 if (cx == null || cy == null) {
755 var bbox = this.getBBox(1);
757 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
758 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
759 this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
766 * Adds translation by given amount to the list of transformations of the element.
768 - dx (number) horisontal shift
769 - dy (number) vertical shift
772 elproto.translate = function (dx, dy) {
776 dx = Str(dx).split(separator);
780 dx = toFloat(dx[0]) || 0;
782 this.transform(this._.transform.concat([["t", dx, dy]]));
789 * Adds transformation to the element which is separate to other attributes,
790 * i.e. translation doesn’t change `x` or `y` of the rectange. The format
791 * of transformation string is similar to the path string syntax:
792 | "t100,100r30,100,100s2,2,100,100r45s1.5"
793 * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
794 * scale and `m` is for matrix.
796 * There are also alternative “absolute” translation, rotation and scale: `T`, `R` and `S`. They will not take previous transformation into account. For example, `...T100,0` will always move element 100 px horisontally, while `...t100,0` could move it vertically if there is `r90` before. Just compare results of `r90t100,0` and `r90T100,0`.
798 * So, the example line above could be read like “translate by 100, 100; rotate 30° around 100, 100; scale twice around 100, 100;
799 * rotate 45° around centre; scale 1.5 times relative to centre”. As you can see rotate and scale commands have origin
800 * coordinates as optional parameters, the default is the centre point of the element.
801 * Matrix accepts six parameters.
803 | var el = paper.rect(10, 20, 300, 200);
804 | // translate 100, 100, rotate 45°, translate -100, 0
805 | el.transform("t100,100r45t-100,0");
806 | // if you want you can append or prepend transformations
807 | el.transform("...t50,50");
808 | el.transform("s2...");
810 | el.transform("t50,50...t-50-50");
811 | // to reset transformation call method with empty string
813 | // to get current value call it without parameters
814 | console.log(el.transform());
816 - tstr (string) #optional transformation string
817 * If tstr isn’t specified
818 = (string) current transformation string
822 elproto.transform = function (tstr) {
827 R._extractTransform(this, tstr);
829 this.clip && $(this.clip, {transform: this.matrix.invert()});
830 this.pattern && updatePosition(this);
831 this.node && $(this.node, {transform: this.matrix});
833 if (_.sx != 1 || _.sy != 1) {
834 var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
835 this.attr({"stroke-width": sw});
844 * Makes element invisible. See @Element.show.
847 elproto.hide = function () {
848 !this.removed && this.paper.safari(this.node.style.display = "none");
855 * Makes element visible. See @Element.hide.
858 elproto.show = function () {
859 !this.removed && this.paper.safari(this.node.style.display = "");
866 * Removes element form the paper.
868 elproto.remove = function () {
872 var paper = this.paper;
873 paper.__set__ && paper.__set__.exclude(this);
874 eve.unbind("*.*." + this.id);
876 paper.defs.removeChild(this.gradient);
878 R._tear(this, paper);
879 this.node.parentNode.removeChild(this.node);
880 for (var i in this) {
881 this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null;
885 elproto._getBBox = function () {
886 if (this.node.style.display == "none") {
892 bbox = this.node.getBBox();
894 // Firefox 3.0.x plays badly here
905 * Sets the attributes of the element.
907 - attrName (string) attribute’s name
908 - value (string) value
910 - params (object) object of name/value pairs
912 - attrName (string) attribute’s name
914 - attrNames (array) in this case method returns array of current values for given attribute names
915 = (object) @Element if attrsName & value or params are passed in.
916 = (...) value of the attribute if only attrsName is passed in.
917 = (array) array of values of the attribute if attrsNames is passed in.
918 = (object) object of attributes if nothing is passed in.
919 > Possible parameters
920 # <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>
921 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`.
922 o clip-rect (string) comma or space separated values: x, y, width and height
923 o cursor (string) CSS type of the cursor
924 o cx (number) the x-axis coordinate of the center of the circle, or ellipse
925 o cy (number) the y-axis coordinate of the center of the circle, or ellipse
926 o fill (string) colour, gradient or image
927 o fill-opacity (number)
929 o font-family (string)
930 o font-size (number) font size in pixels
931 o font-weight (string)
933 o href (string) URL, if specified element behaves as hyperlink
935 o path (string) SVG path string format
936 o r (number) radius of the circle, ellipse or rounded corner on the rect
937 o rx (number) horisontal radius of the ellipse
938 o ry (number) vertical radius of the ellipse
939 o src (string) image URL, only works for @Element.image element
940 o stroke (string) stroke colour
941 o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
942 o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
943 o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
944 o stroke-miterlimit (number)
945 o stroke-opacity (number)
946 o stroke-width (number) stroke width in pixels, default is '1'
947 o target (string) used with href
948 o text (string) contents of the text element. Use `\n` for multiline text
949 o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
950 o title (string) will create tooltip with a given text
951 o transform (string) see @Element.transform
956 * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
957 * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
959 * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
960 * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
961 * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
963 # <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>
966 # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
967 # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
968 # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
969 # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200, 100, 0)</code>”)</li>
970 # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%, 175%, 0%)</code>”)</li>
971 # <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200, 100, 0, .5)</code>”)</li>
972 # <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%, 175%, 0%, 50%)</code>”)</li>
973 # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5, 0.25, 1)</code>”)</li>
974 # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
975 # <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
976 # <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>
977 # <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
978 # <li>hsla(•••, •••, •••, •••) — same as above, but with opacity</li>
979 # <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>
982 elproto.attr = function (name, value) {
988 for (var a in this.attrs) if (this.attrs[has](a)) {
989 res[a] = this.attrs[a];
991 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
992 res.transform = this._.transform;
995 if (value == null && R.is(name, "string")) {
996 if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
997 return this.attrs.gradient;
999 if (name == "transform") {
1000 return this._.transform;
1002 var names = name.split(separator),
1004 for (var i = 0, ii = names.length; i < ii; i++) {
1006 if (name in this.attrs) {
1007 out[name] = this.attrs[name];
1008 } else if (R.is(this.paper.customAttributes[name], "function")) {
1009 out[name] = this.paper.customAttributes[name].def;
1011 out[name] = R._availableAttrs[name];
1014 return ii - 1 ? out : out[names[0]];
1016 if (value == null && R.is(name, "array")) {
1018 for (i = 0, ii = name.length; i < ii; i++) {
1019 out[name[i]] = this.attr(name[i]);
1023 if (value != null) {
1025 params[name] = value;
1026 } else if (name != null && R.is(name, "object")) {
1029 for (var key in params) {
1030 eve("attr." + key + "." + this.id, this, params[key]);
1032 for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1033 var par = this.paper.customAttributes[key].apply(this, [].concat(params[key]));
1034 this.attrs[key] = params[key];
1035 for (var subkey in par) if (par[has](subkey)) {
1036 params[subkey] = par[subkey];
1039 setFillAndStroke(this, params);
1046 * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
1049 elproto.toFront = function () {
1053 if (this.node.parentNode.tagName.toLowerCase() == "a") {
1054 this.node.parentNode.parentNode.appendChild(this.node.parentNode);
1056 this.node.parentNode.appendChild(this.node);
1058 var svg = this.paper;
1059 svg.top != this && R._tofront(this, svg);
1066 * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
1069 elproto.toBack = function () {
1073 var parent = this.node.parentNode;
1074 if (parent.tagName.toLowerCase() == "a") {
1075 parent.parentNode.insertBefore(this.node.parentNode, this.node.parentNode.parentNode.firstChild);
1076 } else if (parent.firstChild != this.node) {
1077 parent.insertBefore(this.node, this.node.parentNode.firstChild);
1079 R._toback(this, this.paper);
1080 var svg = this.paper;
1084 * Element.insertAfter
1087 * Inserts current object after the given one.
1090 elproto.insertAfter = function (element) {
1094 var node = element.node || element[element.length - 1].node;
1095 if (node.nextSibling) {
1096 node.parentNode.insertBefore(this.node, node.nextSibling);
1098 node.parentNode.appendChild(this.node);
1100 R._insertafter(this, element, this.paper);
1104 * Element.insertBefore
1107 * Inserts current object before the given one.
1110 elproto.insertBefore = function (element) {
1114 var node = element.node || element[0].node;
1115 node.parentNode.insertBefore(this.node, node);
1116 R._insertbefore(this, element, this.paper);
1119 elproto.blur = function (size) {
1120 // Experimental. No Safari support. Use it on your own risk.
1123 var fltr = $("filter"),
1124 blur = $("feGaussianBlur");
1125 t.attrs.blur = size;
1126 fltr.id = R.createUUID();
1127 $(blur, {stdDeviation: +size || 1.5});
1128 fltr.appendChild(blur);
1129 t.paper.defs.appendChild(fltr);
1131 $(t.node, {filter: "url(#" + fltr.id + ")"});
1134 t._blur.parentNode.removeChild(t._blur);
1136 delete t.attrs.blur;
1138 t.node.removeAttribute("filter");
1141 R._engine.circle = function (svg, x, y, r) {
1142 var el = $("circle");
1143 svg.canvas && svg.canvas.appendChild(el);
1144 var res = new Element(el, svg);
1145 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1146 res.type = "circle";
1150 R._engine.rect = function (svg, x, y, w, h, r) {
1152 svg.canvas && svg.canvas.appendChild(el);
1153 var res = new Element(el, svg);
1154 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1159 R._engine.ellipse = function (svg, x, y, rx, ry) {
1160 var el = $("ellipse");
1161 svg.canvas && svg.canvas.appendChild(el);
1162 var res = new Element(el, svg);
1163 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1164 res.type = "ellipse";
1168 R._engine.image = function (svg, src, x, y, w, h) {
1169 var el = $("image");
1170 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1171 el.setAttributeNS(xlink, "href", src);
1172 svg.canvas && svg.canvas.appendChild(el);
1173 var res = new Element(el, svg);
1174 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1178 R._engine.text = function (svg, x, y, text) {
1180 // $(el, {x: x, y: y, "text-anchor": "middle"});
1181 svg.canvas && svg.canvas.appendChild(el);
1182 var res = new Element(el, svg);
1186 "text-anchor": "middle",
1188 font: R._availableAttrs.font,
1193 setFillAndStroke(res, res.attrs);
1196 R._engine.setSize = function (width, height) {
1197 this.width = width || this.width;
1198 this.height = height || this.height;
1199 this.canvas.setAttribute("width", this.width);
1200 this.canvas.setAttribute("height", this.height);
1201 if (this._viewBox) {
1202 this.setViewBox.apply(this, this._viewBox);
1206 R._engine.create = function () {
1207 var con = R._getContainer.apply(0, arguments),
1208 container = con && con.container,
1212 height = con.height;
1214 throw new Error("SVG container not found.");
1216 var cnvs = $("svg"),
1217 css = "overflow:hidden;",
1221 width = width || 512;
1222 height = height || 342;
1227 xmlns: "http://www.w3.org/2000/svg"
1229 if (container == 1) {
1230 cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
1231 R._g.doc.body.appendChild(cnvs);
1234 cnvs.style.cssText = css + "position:relative";
1235 if (container.firstChild) {
1236 container.insertBefore(cnvs, container.firstChild);
1238 container.appendChild(cnvs);
1241 container = new R._Paper;
1242 container.width = width;
1243 container.height = height;
1244 container.canvas = cnvs;
1245 // plugins.call(container, container, R.fn);
1247 container._left = container._top = 0;
1248 isFloating && (container.renderfix = function () {});
1249 container.renderfix();
1252 R._engine.setViewBox = function (x, y, w, h, fit) {
1253 eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
1254 var size = mmax(w / this.width, h / this.height),
1256 aspectRatio = fit ? "meet" : "xMinYMin",
1263 delete this._vbSize;
1264 vb = "0 0 " + this.width + S + this.height;
1266 this._vbSize = size;
1267 vb = x + S + y + S + w + S + h;
1271 preserveAspectRatio: aspectRatio
1273 while (size && top) {
1274 sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
1275 top.attr({"stroke-width": sw});
1280 this._viewBox = [x, y, w, h, !!fit];
1287 * Fixes the issue of Firefox and IE9 regarding subpixel rendering. If paper is dependant
1288 * on other elements after reflow it could shift half pixel which cause for lines to lost their crispness.
1289 * This method fixes the issue.
1291 Special thanks to Mariusz Nowak (http://www.medikoo.com/) for this method.
1293 R.prototype.renderfix = function () {
1294 var cnvs = this.canvas,
1296 pos = cnvs.getScreenCTM() || cnvs.createSVGMatrix(),
1301 this._left = (this._left + left) % 1;
1302 s.left = this._left + "px";
1305 this._top = (this._top + top) % 1;
1306 s.top = this._top + "px";
1314 * Clears the paper, i.e. removes all the elements.
1316 R.prototype.clear = function () {
1317 R.eve("clear", this);
1318 var c = this.canvas;
1319 while (c.firstChild) {
1320 c.removeChild(c.firstChild);
1322 this.bottom = this.top = null;
1323 (this.desc = $("desc")).appendChild(R._g.doc.createTextNode("Created with Rapha\xebl " + R.version));
1324 c.appendChild(this.desc);
1325 c.appendChild(this.defs = $("defs"));
1331 * Removes the paper from the DOM.
1333 R.prototype.remove = function () {
1334 eve("remove", this);
1335 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1336 for (var i in this) {
1337 this[i] = typeof this[i] == "function" ? R._removedFactory(i) : null;
1340 var setproto = R.st;
1341 for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
1342 setproto[method] = (function (methodname) {
1343 return function () {
1344 var arg = arguments;
1345 return this.forEach(function (el) {
1346 el[methodname].apply(el, arg);