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",
24 var xlink = "http://www.w3.org/1999/xlink",
26 block: "M5,0 0,2.5 5,5z",
27 classic: "M5,0 0,2.5 5,5 3.5,3 3.5,2z",
28 diamond: "M2.5,0 5,2.5 2.5,5 0,2.5z",
29 open: "M6,1 1,3.5 6,6",
30 oval: "M2.5,0A2.5,2.5,0,0,1,2.5,5 2.5,2.5,0,0,1,2.5,0z"
33 R.toString = function () {
34 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
36 var $ = function (el, attr) {
38 if (typeof el == "string") {
41 for (var key in attr) if (attr[has](key)) {
42 if (key.substring(0, 6) == "xlink:") {
43 el.setAttributeNS(xlink, key.substring(6), Str(attr[key]));
45 el.setAttribute(key, Str(attr[key]));
49 el = R._g.doc.createElementNS("http://www.w3.org/2000/svg", el);
50 el.style && (el.style.webkitTapHighlightColor = "rgba(0,0,0,0)");
55 rgGrad = /^url\(#(.*)\)$/,
56 removeGradientFill = function (node, paper) {
57 var oid = node.getAttribute("fill");
58 oid = oid && oid.match(rgGrad);
59 if (oid && !--gradients[oid[1]]) {
60 delete gradients[oid[1]];
61 paper.defs.removeChild(R._g.doc.getElementById(oid[1]));
64 addGradientFill = function (element, gradient) {
66 id = element.id + gradient,
71 el = R._g.doc.getElementById(id);
73 gradient = Str(gradient).replace(R._radial_gradient, function (all, _fx, _fy) {
78 var dir = ((fy > .5) * 2 - 1);
79 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
80 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
82 (fy = fy.toFixed(5) - 1e-5 * dir);
86 gradient = gradient.split(/\s*\-\s*/);
87 if (type == "linear") {
88 var angle = gradient.shift();
89 angle = -toFloat(angle);
93 var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
94 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
98 vector[0] = -vector[2];
102 vector[1] = -vector[3];
106 var dots = R._parseDots(gradient);
110 if (element.gradient) {
111 SVG.defs.removeChild(element.gradient);
112 delete element.gradient;
115 id = id.replace(/[\(\)\s,\xb0#]/g, "-");
116 el = $(type + "Gradient", {id: id});
117 element.gradient = el;
118 $(el, type == "radial" ? {
126 gradientTransform: element.matrix.invert()
128 SVG.defs.appendChild(el);
129 for (var i = 0, ii = dots.length; i < ii; i++) {
130 el.appendChild($("stop", {
131 offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
132 "stop-color": dots[i].color || "#fff"
137 fill: "url(#" + id + ")",
146 updatePosition = function (o) {
147 var bbox = o.getBBox(1);
148 $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
150 addArrow = function (o, value, isEnd) {
151 if (o.type == "path") {
152 var values = Str(value).toLowerCase().split("-"),
154 se = isEnd ? "end" : "start",
157 stroke = attrs["stroke-width"],
178 case "wide": h = 5; break;
179 case "narrow": h = 2; break;
180 case "long": w = 5; break;
181 case "short": w = 2; break;
184 if (type == "open") {
189 refX = isEnd ? 4 : 1;
203 o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
204 o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
206 o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
207 o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
212 if (type != "none") {
213 var pathId = "raphael-marker-" + type,
214 markerId = "raphael-marker-" + se + type + w + h;
215 if (!R._g.doc.getElementById(pathId)) {
216 p.defs.appendChild($($("path"), {
217 "stroke-linecap": "round",
221 markerCounter[pathId] = 1;
223 markerCounter[pathId]++;
225 var marker = R._g.doc.getElementById(markerId),
228 marker = $($("marker"), {
237 "xlink:href": "#" + pathId,
238 transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
239 "stroke-width": 1 / ((w / t + h / t) / 2)
241 marker.appendChild(use);
242 p.defs.appendChild(marker);
243 markerCounter[markerId] = 1;
245 markerCounter[markerId]++;
246 use = marker.getElementsByTagName("use")[0];
249 var delta = dx * (type != "diamond" && type != "oval");
251 from = o._.arrows.startdx * stroke || 0;
252 to = R.getTotalLength(attrs.path) - delta * stroke;
254 from = delta * stroke;
255 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
258 attr["marker-" + se] = "url(#" + markerId + ")";
260 attr.d = Raphael.getSubpath(attrs.path, from, to);
263 o._.arrows[se + "Path"] = pathId;
264 o._.arrows[se + "Marker"] = markerId;
265 o._.arrows[se + "dx"] = delta;
266 o._.arrows[se + "Type"] = type;
267 o._.arrows[se + "String"] = value;
270 from = o._.arrows.startdx * stroke || 0;
271 to = R.getTotalLength(attrs.path) - from;
274 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
276 o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
277 delete o._.arrows[se + "Path"];
278 delete o._.arrows[se + "Marker"];
279 delete o._.arrows[se + "dx"];
280 delete o._.arrows[se + "Type"];
281 delete o._.arrows[se + "String"];
283 for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
284 var item = R._g.doc.getElementById(attr);
285 item && item.parentNode.removeChild(item);
295 "-..": [3, 1, 1, 1, 1, 1],
301 "--..": [8, 3, 1, 3, 1, 3]
303 addDashes = function (o, value, params) {
304 value = dasharray[Str(value).toLowerCase()];
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 $(o.node, {"stroke-dasharray": dashes.join(",")});
316 setFillAndStroke = function (o, params) {
319 vis = node.style.visibility;
320 node.style.visibility = "hidden";
321 for (var att in params) {
322 if (params[has](att)) {
323 if (!R._availableAttrs[has](att)) {
326 var value = params[att];
335 var pn = node.parentNode;
336 if (pn.tagName.toLowerCase() != "a") {
338 pn.insertBefore(hl, node);
339 hl.appendChild(node);
342 if (att == "target" && value == "blank") {
343 pn.setAttributeNS(xlink, "show", "new");
345 pn.setAttributeNS(xlink, att, value);
349 node.style.cursor = value;
358 addArrow(o, value, 1);
361 var rect = Str(value).split(separator);
362 if (rect.length == 4) {
363 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
364 var el = $("clipPath"),
366 el.id = R._createUUID();
374 o.paper.defs.appendChild(el);
375 $(node, {"clip-path": "url(#" + el.id + ")"});
379 var clip = R._g.doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, E));
380 clip && clip.parentNode.removeChild(clip);
381 $(node, {"clip-path": E});
386 if (o.type == "path") {
387 $(node, {d: value ? attrs.path = R._pathToAbsolute(value) : "M0,0"});
390 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
391 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
396 node.setAttribute(att, value);
406 value = -attrs.x - (attrs.width || 0);
409 if (att == "rx" && o.type == "rect") {
413 node.setAttribute(att, value);
414 o.pattern && updatePosition(o);
418 node.setAttribute(att, value);
428 value = -attrs.y - (attrs.height || 0);
431 if (att == "ry" && o.type == "rect") {
435 node.setAttribute(att, value);
436 o.pattern && updatePosition(o);
440 if (o.type == "rect") {
441 $(node, {rx: value, ry: value});
443 node.setAttribute(att, value);
448 if (o.type == "image") {
449 node.setAttributeNS(xlink, "href", value);
453 if (o._.sx != 1 || o._.sy != 1) {
454 value /= mmax(abs(o._.sx), abs(o._.sy)) || 1;
456 if (o.paper._vbSize) {
457 value *= o.paper._vbSize;
459 node.setAttribute(att, value);
460 if (attrs["stroke-dasharray"]) {
461 addDashes(o, attrs["stroke-dasharray"], params);
464 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
465 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
468 case "stroke-dasharray":
469 addDashes(o, value, params);
472 var isURL = Str(value).match(R._ISURL);
476 el.id = R._createUUID();
477 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
478 $(ig, {x: 0, y: 0, "xlink:href": isURL[1]});
482 R._preload(isURL[1], function () {
483 var w = this.offsetWidth,
484 h = this.offsetHeight;
485 $(el, {width: w, height: h});
486 $(ig, {width: w, height: h});
490 o.paper.defs.appendChild(el);
491 node.style.fill = "url(#" + el.id + ")";
492 $(node, {fill: "url(#" + el.id + ")"});
494 o.pattern && updatePosition(o);
497 var clr = R.getRGB(value);
499 delete params.gradient;
500 delete attrs.gradient;
501 !R.is(attrs.opacity, "undefined") &&
502 R.is(params.opacity, "undefined") &&
503 $(node, {opacity: attrs.opacity});
504 !R.is(attrs["fill-opacity"], "undefined") &&
505 R.is(params["fill-opacity"], "undefined") &&
506 $(node, {"fill-opacity": attrs["fill-opacity"]});
507 } else if ((o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value)) {
508 if ("opacity" in attrs || "fill-opacity" in attrs) {
509 var gradient = R._g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
511 var stops = gradient.getElementsByTagName("stop");
512 $(stops[stops.length - 1], {"stop-opacity": ("opacity" in attrs ? attrs.opacity : 1) * ("fill-opacity" in attrs ? attrs["fill-opacity"] : 1)});
515 attrs.gradient = value;
519 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
521 clr = R.getRGB(value);
522 node.setAttribute(att, clr.hex);
523 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
524 if (att == "stroke" && o._.arrows) {
525 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
526 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
530 (o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value);
533 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
534 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
538 if (attrs.gradient) {
539 gradient = R._g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
541 stops = gradient.getElementsByTagName("stop");
542 $(stops[stops.length - 1], {"stop-opacity": value});
547 att == "font-size" && (value = toInt(value, 10) + "px");
548 var cssrule = att.replace(/(\-.)/g, function (w) {
549 return w.substring(1).toUpperCase();
551 node.style[cssrule] = value;
553 node.setAttribute(att, value);
560 node.style.visibility = vis;
563 tuneText = function (el, params) {
564 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
569 fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
571 if (params[has]("text")) {
572 a.text = params.text;
573 while (node.firstChild) {
574 node.removeChild(node.firstChild);
576 var texts = Str(params.text).split("\n"),
579 for (var i = 0, ii = texts.length; i < ii; i++) {
581 i && $(tspan, {dy: fontSize * leading, x: a.x});
582 tspan.appendChild(R._g.doc.createTextNode(texts[i]));
583 node.appendChild(tspan);
587 tspans = node.getElementsByTagName("tspan");
588 for (i = 0, ii = tspans.length; i < ii; i++) if (i) {
589 $(tspans[i], {dy: fontSize * leading, x: a.x});
591 $(tspans[0], {dy: 0});
594 $(node, {x: a.x, y: a.y});
596 var bb = el._getBBox(),
597 dif = a.y - (bb.y + bb.height / 2);
598 dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
600 Element = function (node, svg) {
605 [ property (object) ]
607 * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
609 * Note: Don’t mess with it.
611 | // draw a circle at coordinate 10,10 with radius of 10
612 | var c = paper.circle(10, 10, 10);
613 | c.node.onclick = function () {
614 | c.attr("fill", "red");
617 this[0] = this.node = node;
620 [ property (object) ]
622 * Internal reference to @Raphael object. In case it is not available.
624 | Raphael.el.red = function () {
625 | var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
627 | this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
633 [ property (number) ]
635 * Unique id of the element. Especially usesful when you want to listen to events of the element,
636 * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
639 node.raphaelid = this.id;
640 this.matrix = R.matrix();
641 this.realPath = null;
644 [ property (object) ]
646 * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
648 | Raphael.el.cross = function () {
649 | this.attr({fill: "red"});
650 | this.paper.path("M10,10L50,50M50,10L10,50")
651 | .attr({stroke: "red"});
655 this.attrs = this.attrs || {};
665 !svg.bottom && (svg.bottom = this);
668 [ property (object) ]
670 * Reference to the previous element in the hierarchy.
673 svg.top && (svg.top.next = this);
677 [ property (object) ]
679 * Reference to the next element in the hierarchy.
685 Element.prototype = elproto;
686 elproto.constructor = Element;
688 R._engine.path = function (pathString, SVG) {
690 SVG.canvas && SVG.canvas.appendChild(el);
691 var p = new Element(el, SVG);
693 setFillAndStroke(p, {
704 * Adds rotation by given angle around given point to the list of
705 * transformations of the element.
707 - deg (number) angle in degrees
708 - cx (number) #optional x coordinate of the centre of rotation
709 - cy (number) #optional y coordinate of the centre of rotation
710 * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
713 elproto.rotate = function (deg, cx, cy) {
717 deg = Str(deg).split(separator);
718 if (deg.length - 1) {
719 cx = toFloat(deg[1]);
720 cy = toFloat(deg[2]);
722 deg = toFloat(deg[0]);
723 (cy == null) && (cx = cy);
724 if (cx == null || cy == null) {
725 var bbox = this.getBBox(1);
726 cx = bbox.x + bbox.width / 2;
727 cy = bbox.y + bbox.height / 2;
729 this.transform(this._.transform.concat([["r", deg, cx, cy]]));
736 * Adds scale by given amount relative to given point to the list of
737 * transformations of the element.
739 - sx (number) horisontal scale amount
740 - sy (number) vertical scale amount
741 - cx (number) #optional x coordinate of the centre of scale
742 - cy (number) #optional y coordinate of the centre of scale
743 * If cx & cy aren’t specified centre of the shape is used instead.
746 elproto.scale = function (sx, sy, cx, cy) {
750 sx = Str(sx).split(separator);
757 (sy == null) && (sy = sx);
758 (cy == null) && (cx = cy);
759 if (cx == null || cy == null) {
760 var bbox = this.getBBox(1);
762 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
763 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
764 this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
771 * Adds translation by given amount to the list of transformations of the element.
773 - dx (number) horisontal shift
774 - dy (number) vertical shift
777 elproto.translate = function (dx, dy) {
781 dx = Str(dx).split(separator);
785 dx = toFloat(dx[0]) || 0;
787 this.transform(this._.transform.concat([["t", dx, dy]]));
794 * Adds transformation to the element which is separate to other attributes,
795 * i.e. translation doesn’t change `x` or `y` of the rectange. The format
796 * of transformation string is similar to the path string syntax:
797 | "t100,100r30,100,100s2,2,100,100r45s1.5"
798 * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
799 * scale and `m` is for matrix.
801 * So, the example line above could be read like “translate by 100, 100; rotate 30° around 100, 100; scale twice around 100, 100;
802 * rotate 45° around centre; scale 1.5 times relative to centre”. As you can see rotate and scale commands have origin
803 * coordinates as optional parameters, the default is the centre point of the element.
804 * Matrix accepts six parameters.
806 | var el = paper.rect(10, 20, 300, 200);
807 | // translate 100, 100, rotate 45°, translate -100, 0
808 | el.transform("t100,100r45t-100,0");
809 | // if you want you can append or prepend transformations
810 | el.transform("...t50,50");
811 | el.transform("s2...");
813 | el.transform("t50,50...t-50-50");
814 | // to reset transformation call method with empty string
816 | // to get current value call it without parameters
817 | console.log(el.transform());
819 - tstr (string) #optional transformation string
820 * If tstr isn’t specified
821 = (string) current transformation string
825 elproto.transform = function (tstr) {
830 R._extractTransform(this, tstr);
832 this.clip && $(this.clip, {transform: this.matrix.invert()});
833 this.pattern && updatePosition(this);
834 this.node && $(this.node, {transform: this.matrix});
836 if (_.sx != 1 || _.sy != 1) {
837 var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
838 this.attr({"stroke-width": sw});
847 * Makes element invisible. See @Element.show.
850 elproto.hide = function () {
851 !this.removed && this.paper.safari(this.node.style.display = "none");
858 * Makes element visible. See @Element.hide.
861 elproto.show = function () {
862 !this.removed && this.paper.safari(this.node.style.display = "");
869 * Removes element form the paper.
871 elproto.remove = function () {
875 eve.unbind("*.*." + this.id);
876 R._tear(this, this.paper);
877 this.node.parentNode.removeChild(this.node);
878 for (var i in this) {
883 elproto._getBBox = function () {
884 if (this.node.style.display == "none") {
890 bbox = this.node.getBBox();
892 // Firefox 3.0.x plays badly here
903 * Sets the attributes of the element.
905 - attrName (string) attribute’s name
906 - value (string) value
908 - params (object) object of name/value pairs
910 - attrName (string) attribute’s name
912 - attrNames (array) in this case method returns array of current values for given attribute names
913 = (object) @Element if attrsName & value or params are passed in.
914 = (...) value of the attribute if only attrsName is passed in.
915 = (array) array of values of the attribute if attrsNames is passed in.
916 = (object) object of attributes if nothing is passed in.
917 > Possible parameters
918 # <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>
919 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`.
920 o clip-rect (string) comma or space separated values: x, y, width and height
921 o cursor (string) CSS type of the cursor
924 o fill (string) colour, gradient or image
925 o fill-opacity (number)
927 o font-family (string)
928 o font-size (number) font size in pixels
929 o font-weight (string)
931 o href (string) URL, if specified element behaves as hyperlink
933 o path (string) SVG path string format
937 o src (string) image URL, only works for @Element.image element
938 o stroke (string) stroke colour
939 o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
940 o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
941 o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
942 o stroke-miterlimit (number)
943 o stroke-opacity (number)
944 o stroke-width (number) stroke width in pixels, default is '1'
945 o target (string) used with href
946 o text (string) contents of the text element. Use `\n` for multiline text
947 o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
948 o title (string) will create tooltip with a given text
949 o transform (string) see @Element.transform
954 * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
955 * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
957 * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
958 * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
959 * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
961 # <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>
964 # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
965 # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
966 # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
967 # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200, 100, 0)</code>”)</li>
968 # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%, 175%, 0%)</code>”)</li>
969 # <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200, 100, 0, .5)</code>”)</li>
970 # <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%, 175%, 0%, 50%)</code>”)</li>
971 # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5, 0.25, 1)</code>”)</li>
972 # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
973 # <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
974 # <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>
975 # <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
976 # <li>hsla(•••, •••, •••, •••) — same as above, but with opacity</li>
977 # <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>
980 elproto.attr = function (name, value) {
986 for (var a in this.attrs) if (this.attrs[has](a)) {
987 res[a] = this.attrs[a];
989 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
990 res.transform = this._.transform;
993 if (value == null && R.is(name, "string")) {
994 if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
995 return this.attrs.gradient;
997 if (name == "transform") {
998 return this._.transform;
1000 var names = name.split(separator),
1002 for (var i = 0, ii = names.length; i < ii; i++) {
1004 if (name in this.attrs) {
1005 out[name] = this.attrs[name];
1006 } else if (R.is(this.paper.customAttributes[name], "function")) {
1007 out[name] = this.paper.customAttributes[name].def;
1009 out[name] = R._availableAttrs[name];
1012 return ii - 1 ? out : out[names[0]];
1014 if (value == null && R.is(name, "array")) {
1016 for (i = 0, ii = name.length; i < ii; i++) {
1017 out[name[i]] = this.attr(name[i]);
1021 if (value != null) {
1023 params[name] = value;
1024 } else if (name != null && R.is(name, "object")) {
1027 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1028 var par = this.paper.customAttributes[key].apply(this, [].concat(params[key]));
1029 this.attrs[key] = params[key];
1030 for (var subkey in par) if (par[has](subkey)) {
1031 params[subkey] = par[subkey];
1034 setFillAndStroke(this, params);
1041 * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
1044 elproto.toFront = function () {
1048 this.node.parentNode.appendChild(this.node);
1049 var svg = this.paper;
1050 svg.top != this && R._tofront(this, svg);
1057 * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
1060 elproto.toBack = function () {
1064 if (this.node.parentNode.firstChild != this.node) {
1065 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1066 R._toback(this, this.paper);
1067 var svg = this.paper;
1072 * Element.insertAfter
1075 * Inserts current object after the given one.
1078 elproto.insertAfter = function (element) {
1082 var node = element.node || element[element.length - 1].node;
1083 if (node.nextSibling) {
1084 node.parentNode.insertBefore(this.node, node.nextSibling);
1086 node.parentNode.appendChild(this.node);
1088 R._insertafter(this, element, this.paper);
1092 * Element.insertBefore
1095 * Inserts current object before the given one.
1098 elproto.insertBefore = function (element) {
1102 var node = element.node || element[0].node;
1103 node.parentNode.insertBefore(this.node, node);
1104 R._insertbefore(this, element, this.paper);
1107 elproto.blur = function (size) {
1108 // Experimental. No Safari support. Use it on your own risk.
1111 var fltr = $("filter"),
1112 blur = $("feGaussianBlur");
1113 t.attrs.blur = size;
1114 fltr.id = R._createUUID();
1115 $(blur, {stdDeviation: +size || 1.5});
1116 fltr.appendChild(blur);
1117 t.paper.defs.appendChild(fltr);
1119 $(t.node, {filter: "url(#" + fltr.id + ")"});
1122 t._blur.parentNode.removeChild(t._blur);
1124 delete t.attrs.blur;
1126 t.node.removeAttribute("filter");
1129 R._engine.circle = function (svg, x, y, r) {
1130 var el = $("circle");
1131 svg.canvas && svg.canvas.appendChild(el);
1132 var res = new Element(el, svg);
1133 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1134 res.type = "circle";
1138 R._engine.rect = function (svg, x, y, w, h, r) {
1140 svg.canvas && svg.canvas.appendChild(el);
1141 var res = new Element(el, svg);
1142 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1147 R._engine.ellipse = function (svg, x, y, rx, ry) {
1148 var el = $("ellipse");
1149 svg.canvas && svg.canvas.appendChild(el);
1150 var res = new Element(el, svg);
1151 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1152 res.type = "ellipse";
1156 R._engine.image = function (svg, src, x, y, w, h) {
1157 var el = $("image");
1158 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1159 el.setAttributeNS(xlink, "href", src);
1160 svg.canvas && svg.canvas.appendChild(el);
1161 var res = new Element(el, svg);
1162 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1166 R._engine.text = function (svg, x, y, text) {
1168 // $(el, {x: x, y: y, "text-anchor": "middle"});
1169 svg.canvas && svg.canvas.appendChild(el);
1170 var res = new Element(el, svg);
1174 "text-anchor": "middle",
1176 font: R._availableAttrs.font,
1181 setFillAndStroke(res, res.attrs);
1184 R._engine.setSize = function (width, height) {
1185 this.width = width || this.width;
1186 this.height = height || this.height;
1187 this.canvas.setAttribute("width", this.width);
1188 this.canvas.setAttribute("height", this.height);
1189 if (this._viewBox) {
1190 this.setViewBox.apply(this, this._viewBox);
1194 R._engine.create = function () {
1195 var con = R._getContainer.apply(0, arguments),
1196 container = con && con.container,
1200 height = con.height;
1202 throw new Error("SVG container not found.");
1204 var cnvs = $("svg"),
1205 css = "overflow:hidden;",
1209 width = width || 512;
1210 height = height || 342;
1215 xmlns: "http://www.w3.org/2000/svg"
1217 if (container == 1) {
1218 cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
1219 R._g.doc.body.appendChild(cnvs);
1222 cnvs.style.cssText = css + "position:relative";
1223 if (container.firstChild) {
1224 container.insertBefore(cnvs, container.firstChild);
1226 container.appendChild(cnvs);
1229 container = new R._Paper;
1230 container.width = width;
1231 container.height = height;
1232 container.canvas = cnvs;
1233 // plugins.call(container, container, R.fn);
1235 container._left = container._top = 0;
1236 isFloating && (container.renderfix = function () {});
1237 container.renderfix();
1240 R._engine.setViewBox = function (x, y, w, h, fit) {
1241 eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
1242 var size = mmax(w / this.width, h / this.height),
1244 aspectRatio = fit ? "meet" : "xMinYMin",
1251 delete this._vbSize;
1252 vb = "0 0 " + this.width + S + this.height;
1254 this._vbSize = size;
1255 vb = x + S + y + S + w + S + h;
1259 preserveAspectRatio: aspectRatio
1261 while (size && top) {
1262 sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
1263 top.attr({"stroke-width": sw});
1268 this._viewBox = [x, y, w, h, !!fit];
1275 * Fixes the issue of Firefox and IE9 regarding subpixel rendering. If paper is dependant
1276 * on other elements after reflow it could shift half pixel which cause for lines to lost their crispness.
1277 * This method fixes the issue.
1279 Special thanks to Mariusz Nowak (http://www.medikoo.com/) for this method.
1281 R.prototype.renderfix = function () {
1282 var cnvs = this.canvas,
1284 pos = cnvs.getScreenCTM(),
1289 this._left = (this._left + left) % 1;
1290 s.left = this._left + "px";
1293 this._top = (this._top + top) % 1;
1294 s.top = this._top + "px";
1302 * Clears the paper, i.e. removes all the elements.
1304 R.prototype.clear = function () {
1305 R.eve("clear", this);
1306 var c = this.canvas;
1307 while (c.firstChild) {
1308 c.removeChild(c.firstChild);
1310 this.bottom = this.top = null;
1311 (this.desc = $("desc")).appendChild(R._g.doc.createTextNode("Created with Rapha\xebl " + R.version));
1312 c.appendChild(this.desc);
1313 c.appendChild(this.defs = $("defs"));
1319 * Removes the paper from the DOM.
1321 R.prototype.remove = function () {
1322 eve("remove", this);
1323 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1324 for (var i in this) {
1325 this[i] = removed(i);
1328 var setproto = R.st;
1329 for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
1330 setproto[method] = (function (methodname) {
1331 return function () {
1332 var arg = arguments;
1333 return this.forEach(function (el) {
1334 el[methodname].apply(el, arg);