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",
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)");
54 rgGrad = /^url\(#(.*)\)$/,
55 removeGradientFill = function (node, paper) {
56 var oid = node.getAttribute("fill");
57 oid = oid && oid.match(rgGrad);
58 if (oid && !--gradients[oid[1]]) {
59 delete gradients[oid[1]];
60 paper.defs.removeChild(R._g.doc.getElementById(oid[1]));
63 addGradientFill = function (element, gradient) {
65 id = element.id + gradient,
70 el = R._g.doc.getElementById(id);
72 gradient = Str(gradient).replace(R._radial_gradient, function (all, _fx, _fy) {
77 var dir = ((fy > .5) * 2 - 1);
78 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
79 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
81 (fy = fy.toFixed(5) - 1e-5 * dir);
85 gradient = gradient.split(/\s*\-\s*/);
86 if (type == "linear") {
87 var angle = gradient.shift();
88 angle = -toFloat(angle);
92 var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
93 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
97 vector[0] = -vector[2];
101 vector[1] = -vector[3];
105 var dots = R._parseDots(gradient);
109 if (element.gradient) {
110 SVG.defs.removeChild(element.gradient);
111 delete element.gradient;
114 id = id.replace(/[\(\)\s,\xb0#]/g, "-");
115 el = $(type + "Gradient", {id: id});
116 element.gradient = el;
117 $(el, type == "radial" ? {
125 gradientTransform: element.matrix.invert()
127 SVG.defs.appendChild(el);
128 for (var i = 0, ii = dots.length; i < ii; i++) {
129 el.appendChild($("stop", {
130 offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
131 "stop-color": dots[i].color || "#fff"
136 fill: "url(#" + id + ")",
145 updatePosition = function (o) {
146 var bbox = o.getBBox(1);
147 $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
149 addArrow = function (o, value, isEnd) {
150 if (o.type == "path") {
151 var values = Str(value).toLowerCase().split("-"),
153 se = isEnd ? "end" : "start",
156 stroke = attrs["stroke-width"],
177 case "wide": h = 5; break;
178 case "narrow": h = 2; break;
179 case "long": w = 5; break;
180 case "short": w = 2; break;
183 if (type == "open") {
188 refX = isEnd ? 4 : 1;
202 o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
203 o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
205 o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
206 o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
211 if (type != "none") {
212 var pathId = "raphael-marker-" + type,
213 markerId = "raphael-marker-" + se + type + w + h;
214 if (!R._g.doc.getElementById(pathId)) {
215 p.defs.appendChild($($("path"), {
216 "stroke-linecap": "round",
220 markerCounter[pathId] = 1;
222 markerCounter[pathId]++;
224 var marker = R._g.doc.getElementById(markerId),
227 marker = $($("marker"), {
236 "xlink:href": "#" + pathId,
237 transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
238 "stroke-width": 1 / ((w / t + h / t) / 2)
240 marker.appendChild(use);
241 p.defs.appendChild(marker);
242 markerCounter[markerId] = 1;
244 markerCounter[markerId]++;
245 use = marker.getElementsByTagName("use")[0];
248 var delta = dx * (type != "diamond" && type != "oval");
250 from = o._.arrows.startdx * stroke || 0;
251 to = R.getTotalLength(attrs.path) - delta * stroke;
253 from = delta * stroke;
254 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
257 attr["marker-" + se] = "url(#" + markerId + ")";
259 attr.d = Raphael.getSubpath(attrs.path, from, to);
262 o._.arrows[se + "Path"] = pathId;
263 o._.arrows[se + "Marker"] = markerId;
264 o._.arrows[se + "dx"] = delta;
265 o._.arrows[se + "Type"] = type;
266 o._.arrows[se + "String"] = value;
269 from = o._.arrows.startdx * stroke || 0;
270 to = R.getTotalLength(attrs.path) - from;
273 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
275 o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
276 delete o._.arrows[se + "Path"];
277 delete o._.arrows[se + "Marker"];
278 delete o._.arrows[se + "dx"];
279 delete o._.arrows[se + "Type"];
280 delete o._.arrows[se + "String"];
282 for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
283 var item = R._g.doc.getElementById(attr);
284 item && item.parentNode.removeChild(item);
294 "-..": [3, 1, 1, 1, 1, 1],
300 "--..": [8, 3, 1, 3, 1, 3]
302 addDashes = function (o, value, params) {
303 value = dasharray[Str(value).toLowerCase()];
305 var width = o.attrs["stroke-width"] || "1",
306 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
310 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
312 $(o.node, {"stroke-dasharray": dashes.join(",")});
315 setFillAndStroke = function (o, params) {
318 vis = node.style.visibility;
319 node.style.visibility = "hidden";
320 for (var att in params) {
321 if (params[has](att)) {
322 if (!R._availableAttrs[has](att)) {
325 var value = params[att];
334 var pn = node.parentNode;
335 if (pn.tagName.toLowerCase() != "a") {
337 pn.insertBefore(hl, node);
338 hl.appendChild(node);
341 if (att == "target" && value == "blank") {
342 pn.setAttributeNS(xlink, "show", "new");
344 pn.setAttributeNS(xlink, att, value);
348 node.style.cursor = value;
357 addArrow(o, value, 1);
360 var rect = Str(value).split(separator);
361 if (rect.length == 4) {
362 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
363 var el = $("clipPath"),
365 el.id = R._createUUID();
373 o.paper.defs.appendChild(el);
374 $(node, {"clip-path": "url(#" + el.id + ")"});
378 var clip = R._g.doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, E));
379 clip && clip.parentNode.removeChild(clip);
380 $(node, {"clip-path": E});
385 if (o.type == "path") {
386 $(node, {d: value ? attrs.path = R._pathToAbsolute(value) : "M0,0"});
389 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
390 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
395 node.setAttribute(att, value);
405 value = -attrs.x - (attrs.width || 0);
408 if (att == "rx" && o.type == "rect") {
412 node.setAttribute(att, value);
413 o.pattern && updatePosition(o);
417 node.setAttribute(att, value);
427 value = -attrs.y - (attrs.height || 0);
430 if (att == "ry" && o.type == "rect") {
434 node.setAttribute(att, value);
435 o.pattern && updatePosition(o);
439 if (o.type == "rect") {
440 $(node, {rx: value, ry: value});
442 node.setAttribute(att, value);
447 if (o.type == "image") {
448 node.setAttributeNS(xlink, "href", value);
452 if (o._.sx != 1 || o._.sy != 1) {
453 value /= mmax(abs(o._.sx), abs(o._.sy)) || 1;
455 if (o.paper._vbSize) {
456 value *= o.paper._vbSize;
458 node.setAttribute(att, value);
459 if (attrs["stroke-dasharray"]) {
460 addDashes(o, attrs["stroke-dasharray"], params);
463 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
464 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
467 case "stroke-dasharray":
468 addDashes(o, value, params);
471 var isURL = Str(value).match(R._ISURL);
475 el.id = R._createUUID();
476 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
477 $(ig, {x: 0, y: 0, "xlink:href": isURL[1]});
481 R._preload(isURL[1], function () {
482 var w = this.offsetWidth,
483 h = this.offsetHeight;
484 $(el, {width: w, height: h});
485 $(ig, {width: w, height: h});
489 o.paper.defs.appendChild(el);
490 node.style.fill = "url(#" + el.id + ")";
491 $(node, {fill: "url(#" + el.id + ")"});
493 o.pattern && updatePosition(o);
496 var clr = R.getRGB(value);
498 delete params.gradient;
499 delete attrs.gradient;
500 !R.is(attrs.opacity, "undefined") &&
501 R.is(params.opacity, "undefined") &&
502 $(node, {opacity: attrs.opacity});
503 !R.is(attrs["fill-opacity"], "undefined") &&
504 R.is(params["fill-opacity"], "undefined") &&
505 $(node, {"fill-opacity": attrs["fill-opacity"]});
506 } else if ((o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value)) {
507 if ("opacity" in attrs || "fill-opacity" in attrs) {
508 var gradient = R._g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
510 var stops = gradient.getElementsByTagName("stop");
511 $(stops[stops.length - 1], {"stop-opacity": ("opacity" in attrs ? attrs.opacity : 1) * ("fill-opacity" in attrs ? attrs["fill-opacity"] : 1)});
514 attrs.gradient = value;
518 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
520 clr = R.getRGB(value);
521 node.setAttribute(att, clr.hex);
522 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
523 if (att == "stroke" && o._.arrows) {
524 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
525 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
529 (o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value);
532 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
533 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
537 if (attrs.gradient) {
538 gradient = R._g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
540 stops = gradient.getElementsByTagName("stop");
541 $(stops[stops.length - 1], {"stop-opacity": value});
546 att == "font-size" && (value = toInt(value, 10) + "px");
547 var cssrule = att.replace(/(\-.)/g, function (w) {
548 return w.substring(1).toUpperCase();
550 node.style[cssrule] = value;
552 node.setAttribute(att, value);
559 node.style.visibility = vis;
562 tuneText = function (el, params) {
563 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
568 fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
570 if (params[has]("text")) {
571 a.text = params.text;
572 while (node.firstChild) {
573 node.removeChild(node.firstChild);
575 var texts = Str(params.text).split("\n"),
578 for (var i = 0, ii = texts.length; i < ii; i++) {
580 i && $(tspan, {dy: fontSize * leading, x: a.x});
581 tspan.appendChild(R._g.doc.createTextNode(texts[i]));
582 node.appendChild(tspan);
586 tspans = node.getElementsByTagName("tspan");
587 for (i = 0, ii = tspans.length; i < ii; i++) if (i) {
588 $(tspans[i], {dy: fontSize * leading, x: a.x});
590 $(tspans[0], {dy: 0});
593 $(node, {x: a.x, y: a.y});
595 var bb = el._getBBox(),
596 dif = a.y - (bb.y + bb.height / 2);
597 dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
599 Element = function (node, svg) {
604 [ property (object) ]
606 * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
608 * Note: Don’t mess with it.
610 | // draw a circle at coordinate 10,10 with radius of 10
611 | var c = paper.circle(10, 10, 10);
612 | c.node.onclick = function () {
613 | c.attr("fill", "red");
616 this[0] = this.node = node;
619 [ property (object) ]
621 * Internal reference to @Raphael object. In case it is not available.
623 | Raphael.el.red = function () {
624 | var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
626 | this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
632 [ property (number) ]
634 * Unique id of the element. Especially usesful when you want to listen to events of the element,
635 * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
638 node.raphaelid = this.id;
639 this.matrix = R.matrix();
640 this.realPath = null;
643 [ property (object) ]
645 * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
647 | Raphael.el.cross = function () {
648 | this.attr({fill: "red"});
649 | this.paper.path("M10,10L50,50M50,10L10,50")
650 | .attr({stroke: "red"});
654 this.attrs = this.attrs || {};
664 !svg.bottom && (svg.bottom = this);
667 [ property (object) ]
669 * Reference to the previous element in the hierarchy.
672 svg.top && (svg.top.next = this);
676 [ property (object) ]
678 * Reference to the next element in the hierarchy.
684 Element.prototype = elproto;
685 elproto.constructor = Element;
691 * Creates a path element by given path data string.
693 - pathString (string) #optional path string in SVG format.
694 * Path string consists of one-letter commands, followed by comma seprarated arguments in numercal form. Example:
696 * Here we can see two commands: “M”, with arguments `(10, 20)` and “L” with arguments `(30, 40)`. Upper case letter mean command is absolute, lower case—relative.
698 x <p>Here is short list of commands available, for more details see <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 path string format</a>.</p>
699 # <table><thead><tr><th>Command</th><th>Name</th><th>Parameters</th></tr></thead><tbody>
700 # <tr><td>M</td><td>moveto</td><td>(x y)+</td></tr>
701 # <tr><td>Z</td><td>closepath</td><td>(none)</td></tr>
702 # <tr><td>L</td><td>lineto</td><td>(x y)+</td></tr>
703 # <tr><td>H</td><td>horizontal lineto</td><td>x+</td></tr>
704 # <tr><td>V</td><td>vertical lineto</td><td>y+</td></tr>
705 # <tr><td>C</td><td>curveto</td><td>(x1 y1 x2 y2 x y)+</td></tr>
706 # <tr><td>S</td><td>smooth curveto</td><td>(x2 y2 x y)+</td></tr>
707 # <tr><td>Q</td><td>quadratic Bézier curveto</td><td>(x1 y1 x y)+</td></tr>
708 # <tr><td>T</td><td>smooth quadratic Bézier curveto</td><td>(x y)+</td></tr>
709 # <tr><td>A</td><td>elliptical arc</td><td>(rx ry x-axis-rotation large-arc-flag sweep-flag x y)+</td></tr>
710 # <tr><td>R</td><td><a href="http://en.wikipedia.org/wiki/Catmull–Rom_spline#Catmull.E2.80.93Rom_spline">Catmull-Rom curveto</a>*</td><td>x1 y1 (x y)+</td></tr></tbody></table>
711 * * “Catmull-Rom curveto” is a not standard SVG command and added in 2.0 to make life easier.
713 | var c = paper.path("M10 10L90 90");
714 | // draw a diagonal line:
715 | // move to 10,10, line to 90,90
717 R._engine.path = function (pathString, SVG) {
719 SVG.canvas && SVG.canvas.appendChild(el);
720 var p = new Element(el, SVG);
722 setFillAndStroke(p, {
733 * Adds rotation by given angle around given point to the list of
734 * transformations of the element.
736 - deg (number) angle in degrees
737 - cx (number) #optional x coordinate of the centre of rotation
738 - cy (number) #optional y coordinate of the centre of rotation
739 * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
742 elproto.rotate = function (deg, cx, cy) {
746 deg = Str(deg).split(separator);
747 if (deg.length - 1) {
748 cx = toFloat(deg[1]);
749 cy = toFloat(deg[2]);
751 deg = toFloat(deg[0]);
752 (cy == null) && (cx = cy);
753 if (cx == null || cy == null) {
754 var bbox = this.getBBox(1);
755 cx = bbox.x + bbox.width / 2;
756 cy = bbox.y + bbox.height / 2;
758 this.transform(this._.transform.concat([["r", deg, cx, cy]]));
765 * Adds scale by given amount relative to given point to the list of
766 * transformations of the element.
768 - sx (number) horisontal scale amount
769 - sy (number) vertical scale amount
770 - cx (number) #optional x coordinate of the centre of scale
771 - cy (number) #optional y coordinate of the centre of scale
772 * If cx & cy aren’t specified centre of the shape is used instead.
775 elproto.scale = function (sx, sy, cx, cy) {
779 sx = Str(sx).split(separator);
786 (sy == null) && (sy = sx);
787 (cy == null) && (cx = cy);
788 if (cx == null || cy == null) {
789 var bbox = this.getBBox(1);
791 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
792 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
793 this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
800 * Adds translation by given amount to the list of transformations of the element.
802 - dx (number) horisontal shift
803 - dy (number) vertical shift
806 elproto.translate = function (dx, dy) {
810 dx = Str(dx).split(separator);
814 dx = toFloat(dx[0]) || 0;
816 this.transform(this._.transform.concat([["t", dx, dy]]));
823 * Adds transformation to the element which is separate to other attributes,
824 * i.e. translation doesn’t change `x` or `y` of the rectange. The format
825 * of transformation string is similar to the path string syntax:
826 | "t100,100r30,100,100s2,2,100,100r45s1.5"
827 * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
828 * scale and `m` is for matrix.
830 * So, the example line above could be read like “translate by 100, 100; rotate 30° around 100, 100; scale twice around 100, 100;
831 * rotate 45° around centre; scale 1.5 times relative to centre”. As you can see rotate and scale commands have origin
832 * coordinates as optional parameters, the default is the centre point of the element.
833 * Matrix accepts six parameters.
835 | var el = paper.rect(10, 20, 300, 200);
836 | // translate 100, 100, rotate 45°, translate -100, 0
837 | el.transform("t100,100r45t-100,0");
838 | // if you want you can append or prepend transformations
839 | el.transform("...t50,50");
840 | el.transform("s2...");
842 | el.transform("t50,50...t-50-50");
843 | // to reset transformation call method with empty string
845 | // to get current value call it without parameters
846 | console.log(el.transform());
848 - tstr (string) #optional transformation string
849 * If tstr isn’t specified
850 = (string) current transformation string
854 elproto.transform = function (tstr) {
859 R._extractTransform(this, tstr);
861 this.clip && $(this.clip, {transform: this.matrix.invert()});
862 this.pattern && updatePosition(this);
863 this.node && $(this.node, {transform: this.matrix});
865 if (_.sx != 1 || _.sy != 1) {
866 var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
867 this.attr({"stroke-width": sw});
876 * Makes element invisible. See @Element.show.
879 elproto.hide = function () {
880 !this.removed && this.paper.safari(this.node.style.display = "none");
887 * Makes element visible. See @Element.hide.
890 elproto.show = function () {
891 !this.removed && this.paper.safari(this.node.style.display = "");
898 * Removes element form the paper.
900 elproto.remove = function () {
904 eve.unbind("*.*." + this.id);
905 R._tear(this, this.paper);
906 this.node.parentNode.removeChild(this.node);
907 for (var i in this) {
912 elproto._getBBox = function () {
913 if (this.node.style.display == "none") {
919 bbox = this.node.getBBox();
921 // Firefox 3.0.x plays badly here
932 * Sets the attributes of the element.
934 - attrName (string) attribute’s name
935 - value (string) value
937 - params (object) object of name/value pairs
939 - attrName (string) attribute’s name
941 - attrNames (array) in this case method returns array of current values for given attribute names
942 = (object) @Element if attrsName & value or params are passed in.
943 = (...) value of the attribute if only attrsName is passed in.
944 = (array) array of values of the attribute if attrsNames is passed in.
945 = (object) object of attributes if nothing is passed in.
946 > Possible parameters
947 # <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>
948 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`.
949 o clip-rect (string) comma or space separated values: x, y, width and height
950 o cursor (string) CSS type of the cursor
953 o fill (string) colour, gradient or image
954 o fill-opacity (number)
956 o font-family (string)
957 o font-size (number) font size in pixels
958 o font-weight (string)
960 o href (string) URL, if specified element behaves as hyperlink
962 o path (string) SVG path string format
966 o src (string) image URL, only works for @Element.image element
967 o stroke (string) stroke colour
968 o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
969 o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
970 o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
971 o stroke-miterlimit (number)
972 o stroke-opacity (number)
973 o stroke-width (number) stroke width in pixels, default is '1'
974 o target (string) used with href
975 o text (string) contents of the text element. Use `\n` for multiline text
976 o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
977 o title (string) will create tooltip with a given text
978 o transform (string) see @Element.transform
983 * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
984 * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
986 * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
987 * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
988 * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
990 # <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>
993 # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
994 # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
995 # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
996 # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200, 100, 0)</code>”)</li>
997 # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%, 175%, 0%)</code>”)</li>
998 # <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200, 100, 0, .5)</code>”)</li>
999 # <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%, 175%, 0%, 50%)</code>”)</li>
1000 # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5, 0.25, 1)</code>”)</li>
1001 # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
1002 # <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
1003 # <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>
1004 # <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
1005 # <li>hsla(•••, •••, •••, •••) — same as above, but with opacity</li>
1006 # <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>
1009 elproto.attr = function (name, value) {
1015 for (var a in this.attrs) if (this.attrs[has](a)) {
1016 res[a] = this.attrs[a];
1018 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
1019 res.transform = this._.transform;
1022 if (value == null && R.is(name, "string")) {
1023 if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
1024 return this.attrs.gradient;
1026 if (name == "transform") {
1027 return this._.transform;
1029 var names = name.split(separator),
1031 for (var i = 0, ii = names.length; i < ii; i++) {
1033 if (name in this.attrs) {
1034 out[name] = this.attrs[name];
1035 } else if (R.is(this.paper.customAttributes[name], "function")) {
1036 out[name] = this.paper.customAttributes[name].def;
1038 out[name] = R._availableAttrs[name];
1041 return ii - 1 ? out : out[names[0]];
1043 if (value == null && R.is(name, "array")) {
1045 for (i = 0, ii = name.length; i < ii; i++) {
1046 out[name[i]] = this.attr(name[i]);
1050 if (value != null) {
1052 params[name] = value;
1053 } else if (name != null && R.is(name, "object")) {
1056 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1057 var par = this.paper.customAttributes[key].apply(this, [].concat(params[key]));
1058 this.attrs[key] = params[key];
1059 for (var subkey in par) if (par[has](subkey)) {
1060 params[subkey] = par[subkey];
1063 setFillAndStroke(this, params);
1070 * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
1073 elproto.toFront = function () {
1077 this.node.parentNode.appendChild(this.node);
1078 var svg = this.paper;
1079 svg.top != this && R._tofront(this, svg);
1086 * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
1089 elproto.toBack = function () {
1093 if (this.node.parentNode.firstChild != this.node) {
1094 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1095 R._toback(this, this.paper);
1096 var svg = this.paper;
1101 * Element.insertAfter
1104 * Inserts current object after the given one.
1107 elproto.insertAfter = function (element) {
1111 var node = element.node || element[element.length - 1].node;
1112 if (node.nextSibling) {
1113 node.parentNode.insertBefore(this.node, node.nextSibling);
1115 node.parentNode.appendChild(this.node);
1117 R._insertafter(this, element, this.paper);
1121 * Element.insertBefore
1124 * Inserts current object before the given one.
1127 elproto.insertBefore = function (element) {
1131 var node = element.node || element[0].node;
1132 node.parentNode.insertBefore(this.node, node);
1133 R._insertbefore(this, element, this.paper);
1136 elproto.blur = function (size) {
1137 // Experimental. No Safari support. Use it on your own risk.
1140 var fltr = $("filter"),
1141 blur = $("feGaussianBlur");
1142 t.attrs.blur = size;
1143 fltr.id = R._createUUID();
1144 $(blur, {stdDeviation: +size || 1.5});
1145 fltr.appendChild(blur);
1146 t.paper.defs.appendChild(fltr);
1148 $(t.node, {filter: "url(#" + fltr.id + ")"});
1151 t._blur.parentNode.removeChild(t._blur);
1153 delete t.attrs.blur;
1155 t.node.removeAttribute("filter");
1158 R._engine.circle = function (svg, x, y, r) {
1159 var el = $("circle");
1160 svg.canvas && svg.canvas.appendChild(el);
1161 var res = new Element(el, svg);
1162 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1163 res.type = "circle";
1167 R._engine.rect = function (svg, x, y, w, h, r) {
1169 svg.canvas && svg.canvas.appendChild(el);
1170 var res = new Element(el, svg);
1171 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1176 R._engine.ellipse = function (svg, x, y, rx, ry) {
1177 var el = $("ellipse");
1178 svg.canvas && svg.canvas.appendChild(el);
1179 var res = new Element(el, svg);
1180 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1181 res.type = "ellipse";
1185 R._engine.image = function (svg, src, x, y, w, h) {
1186 var el = $("image");
1187 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1188 el.setAttributeNS(xlink, "href", src);
1189 svg.canvas && svg.canvas.appendChild(el);
1190 var res = new Element(el, svg);
1191 res.attrs = {x: x, y: y, width: w, height: h, src: src};
1195 R._engine.text = function (svg, x, y, text) {
1197 // $(el, {x: x, y: y, "text-anchor": "middle"});
1198 svg.canvas && svg.canvas.appendChild(el);
1199 var res = new Element(el, svg);
1203 "text-anchor": "middle",
1205 font: R._availableAttrs.font,
1210 setFillAndStroke(res, res.attrs);
1213 R._engine.setSize = function (width, height) {
1214 this.width = width || this.width;
1215 this.height = height || this.height;
1216 this.canvas.setAttribute("width", this.width);
1217 this.canvas.setAttribute("height", this.height);
1218 if (this._viewBox) {
1219 this.setViewBox.apply(this, this._viewBox);
1223 R._engine.create = function () {
1224 var con = R._getContainer.apply(0, arguments),
1225 container = con && con.container,
1229 height = con.height;
1231 throw new Error("SVG container not found.");
1233 var cnvs = $("svg"),
1234 css = "overflow:hidden;",
1238 width = width || 512;
1239 height = height || 342;
1244 xmlns: "http://www.w3.org/2000/svg"
1246 if (container == 1) {
1247 cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
1248 R._g.doc.body.appendChild(cnvs);
1251 cnvs.style.cssText = css + "position:relative";
1252 if (container.firstChild) {
1253 container.insertBefore(cnvs, container.firstChild);
1255 container.appendChild(cnvs);
1258 container = new R._Paper;
1259 container.width = width;
1260 container.height = height;
1261 container.canvas = cnvs;
1262 // plugins.call(container, container, R.fn);
1264 container._left = container._top = 0;
1265 isFloating && (container.renderfix = function () {});
1266 container.renderfix();
1269 R._engine.setViewBox = function (x, y, w, h, fit) {
1270 eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
1271 var size = mmax(w / this.width, h / this.height),
1273 aspectRatio = fit ? "meet" : "xMinYMin",
1280 delete this._vbSize;
1281 vb = "0 0 " + this.width + S + this.height;
1283 this._vbSize = size;
1284 vb = x + S + y + S + w + S + h;
1288 preserveAspectRatio: aspectRatio
1290 while (size && top) {
1291 sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
1292 top.attr({"stroke-width": sw});
1297 this._viewBox = [x, y, w, h, !!fit];
1304 * Fixes the issue of Firefox and IE9 regarding subpixel rendering. If paper is dependant
1305 * on other elements after reflow it could shift half pixel which cause for lines to lost their crispness.
1306 * This method fixes the issue.
1308 Special thanks to Mariusz Nowak (http://www.medikoo.com/) for this method.
1310 R.prototype.renderfix = function () {
1311 var cnvs = this.canvas,
1313 pos = cnvs.getScreenCTM(),
1318 this._left = (this._left + left) % 1;
1319 s.left = this._left + "px";
1322 this._top = (this._top + top) % 1;
1323 s.top = this._top + "px";
1331 * Clears the paper, i.e. removes all the elements.
1333 R.prototype.clear = function () {
1334 R.eve("clear", this);
1335 var c = this.canvas;
1336 while (c.firstChild) {
1337 c.removeChild(c.firstChild);
1339 this.bottom = this.top = null;
1340 (this.desc = $("desc")).appendChild(R._g.doc.createTextNode("Created with Rapha\xebl " + R.version));
1341 c.appendChild(this.desc);
1342 c.appendChild(this.defs = $("defs"));
1348 * Removes the paper from the DOM.
1350 R.prototype.remove = function () {
1351 eve("remove", this);
1352 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1353 for (var i in this) {
1354 this[i] = removed(i);
1357 var setproto = R.st;
1358 for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
1359 setproto[method] = (function (methodname) {
1360 return function () {
1361 var arg = arguments;
1362 return this.forEach(function (el) {
1363 el[methodname].apply(el, arg);