Split raphael into 3 development files. Added build process. Fixed transformation...
[raphael] / raphael.svg.js
1 // ┌─────────────────────────────────────────────────────────────────────┐ \\
2 // │ Raphaël 2 - JavaScript Vector Library                               │ \\
3 // ├─────────────────────────────────────────────────────────────────────┤ \\
4 // │ SVG Module                                                          │ \\
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",
12         Str = String,
13         toFloat = parseFloat,
14         toInt = parseInt,
15         math = Math,
16         mmax = math.max,
17         abs = math.abs,
18         separator = /[, ]+/,
19         E = "";
20     // SVG
21     var xlink = "http://www.w3.org/1999/xlink",
22         markers = {
23             block: "M5,0 0,2.5 5,5z",
24             classic: "M5,0 0,2.5 5,5 3.5,3 3.5,2z",
25             diamond: "M2.5,0 5,2.5 2.5,5 0,2.5z",
26             open: "M6,1 1,3.5 6,6",
27             oval: "M2.5,0A2.5,2.5,0,0,1,2.5,5 2.5,2.5,0,0,1,2.5,0z"
28         },
29         markerCounter = {};
30     R.toString = function () {
31         return  "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
32     };
33     var $ = function (el, attr) {
34         if (attr) {
35             if (typeof el == "string") {
36                 el = $(el);
37             }
38             for (var key in attr) if (attr[has](key)) {
39                 if (key.substring(0, 6) == "xlink:") {
40                     el.setAttributeNS(xlink, key.substring(6), Str(attr[key]));
41                 } else {
42                     el.setAttribute(key, Str(attr[key]));
43                 }
44             }
45         } else {
46             el = R._g.doc.createElementNS("http://www.w3.org/2000/svg", el);
47             el.style && (el.style.webkitTapHighlightColor = "rgba(0,0,0,0)");
48         }
49         return el;
50     },
51     gradients = {},
52     rgGrad = /^url\(#(.*)\)$/,
53     removeGradientFill = function (node, paper) {
54         var oid = node.getAttribute("fill");
55         oid = oid && oid.match(rgGrad);
56         if (oid && !--gradients[oid[1]]) {
57             delete gradients[oid[1]];
58             paper.defs.removeChild(g.doc.getElementById(oid[1]));
59         }
60     },
61     addGradientFill = function (element, gradient) {
62         var type = "linear",
63             id = element.id + gradient,
64             fx = .5, fy = .5,
65             o = element.node,
66             SVG = element.paper,
67             s = o.style,
68             el = R._g.doc.getElementById(id);
69         if (!el) {
70             gradient = Str(gradient).replace(R._radial_gradient, function (all, _fx, _fy) {
71                 type = "radial";
72                 if (_fx && _fy) {
73                     fx = toFloat(_fx);
74                     fy = toFloat(_fy);
75                     var dir = ((fy > .5) * 2 - 1);
76                     pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
77                         (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
78                         fy != .5 &&
79                         (fy = fy.toFixed(5) - 1e-5 * dir);
80                 }
81                 return E;
82             });
83             gradient = gradient.split(/\s*\-\s*/);
84             if (type == "linear") {
85                 var angle = gradient.shift();
86                 angle = -toFloat(angle);
87                 if (isNaN(angle)) {
88                     return null;
89                 }
90                 var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
91                     max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
92                 vector[2] *= max;
93                 vector[3] *= max;
94                 if (vector[2] < 0) {
95                     vector[0] = -vector[2];
96                     vector[2] = 0;
97                 }
98                 if (vector[3] < 0) {
99                     vector[1] = -vector[3];
100                     vector[3] = 0;
101                 }
102             }
103             var dots = R._parseDots(gradient);
104             if (!dots) {
105                 return null;
106             }
107             if (element.gradient) {
108                 SVG.defs.removeChild(element.gradient);
109                 delete element.gradient;
110             }
111
112             id = id.replace(/[\(\)\s,\xb0#]/g, "-");
113             el = $(type + "Gradient", {id: id});
114             element.gradient = el;
115             $(el, type == "radial" ? {
116                 fx: fx,
117                 fy: fy
118             } : {
119                 x1: vector[0],
120                 y1: vector[1],
121                 x2: vector[2],
122                 y2: vector[3],
123                 gradientTransform: element.matrix.invert()
124             });
125             SVG.defs.appendChild(el);
126             for (var i = 0, ii = dots.length; i < ii; i++) {
127                 el.appendChild($("stop", {
128                     offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
129                     "stop-color": dots[i].color || "#fff"
130                 }));
131             }
132         }
133         $(o, {
134             fill: "url(#" + id + ")",
135             opacity: 1,
136             "fill-opacity": 1
137         });
138         s.fill = E;
139         s.opacity = 1;
140         s.fillOpacity = 1;
141         return 1;
142     },
143     updatePosition = function (o) {
144         var bbox = o.getBBox(1);
145         $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
146     },
147     addArrow = function (o, value, isEnd) {
148         if (o.type == "path") {
149             var values = Str(value).toLowerCase().split("-"),
150                 p = o.paper,
151                 se = isEnd ? "end" : "start",
152                 node = o.node,
153                 attrs = o.attrs,
154                 stroke = attrs["stroke-width"],
155                 i = values.length,
156                 type = "classic",
157                 from,
158                 to,
159                 dx,
160                 refX,
161                 attr,
162                 w = 3,
163                 h = 3,
164                 t = 5;
165             while (i--) {
166                 switch (values[i]) {
167                     case "block":
168                     case "classic":
169                     case "oval":
170                     case "diamond":
171                     case "open":
172                     case "none":
173                         type = values[i];
174                         break;
175                     case "wide": h = 5; break;
176                     case "narrow": h = 2; break;
177                     case "long": w = 5; break;
178                     case "short": w = 2; break;
179                 }
180             }
181             if (type == "open") {
182                 w += 2;
183                 h += 2;
184                 t += 2;
185                 dx = 1;
186                 refX = isEnd ? 4 : 1;
187                 attr = {
188                     fill: "none",
189                     stroke: attrs.stroke
190                 };
191             } else {
192                 refX = dx = w / 2;
193                 attr = {
194                     fill: attrs.stroke,
195                     stroke: "none"
196                 };
197             }
198             if (o._.arrows) {
199                 if (isEnd) {
200                     o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
201                     o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
202                 } else {
203                     o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
204                     o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
205                 }
206             } else {
207                 o._.arrows = {};
208             }
209             if (type != "none") {
210                 var pathId = "raphael-marker-" + type,
211                     markerId = "raphael-marker-" + se + type + w + h;
212                 if (!g.doc.getElementById(pathId)) {
213                     p.defs.appendChild($($("path"), {
214                         "stroke-linecap": "round",
215                         d: markers[type],
216                         id: pathId
217                     }));
218                     markerCounter[pathId] = 1;
219                 } else {
220                     markerCounter[pathId]++;
221                 }
222                 var marker = g.doc.getElementById(markerId),
223                     use;
224                 if (!marker) {
225                     marker = $($("marker"), {
226                         id: markerId,
227                         markerHeight: h,
228                         markerWidth: w,
229                         orient: "auto",
230                         refX: refX,
231                         refY: h / 2
232                     });
233                     use = $($("use"), {
234                         "xlink:href": "#" + pathId,
235                         transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
236                         "stroke-width": 1 / ((w / t + h / t) / 2)
237                     });
238                     marker.appendChild(use);
239                     p.defs.appendChild(marker);
240                     markerCounter[markerId] = 1;
241                 } else {
242                     markerCounter[markerId]++;
243                     use = marker.getElementsByTagName("use")[0];
244                 }
245                 $(use, attr);
246                 var delta = dx * (type != "diamond" && type != "oval");
247                 if (isEnd) {
248                     from = o._.arrows.startdx * stroke || 0;
249                     to = R.getTotalLength(attrs.path) - delta * stroke;
250                 } else {
251                     from = delta * stroke;
252                     to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
253                 }
254                 attr = {};
255                 attr["marker-" + se] = "url(#" + markerId + ")";
256                 if (to || from) {
257                     attr.d = Raphael.getSubpath(attrs.path, from, to);
258                 }
259                 $(node, attr);
260                 o._.arrows[se + "Path"] = pathId;
261                 o._.arrows[se + "Marker"] = markerId;
262                 o._.arrows[se + "dx"] = delta;
263                 o._.arrows[se + "Type"] = type;
264                 o._.arrows[se + "String"] = value;
265             } else {
266                 if (isEnd) {
267                     from = o._.arrows.startdx * stroke || 0;
268                     to = R.getTotalLength(attrs.path) - from;
269                 } else {
270                     from = 0;
271                     to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
272                 }
273                 o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
274                 delete o._.arrows[se + "Path"];
275                 delete o._.arrows[se + "Marker"];
276                 delete o._.arrows[se + "dx"];
277                 delete o._.arrows[se + "Type"];
278                 delete o._.arrows[se + "String"];
279             }
280             for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
281                 var item = g.doc.getElementById(attr);
282                 item && item.parentNode.removeChild(item);
283             }
284         }
285     },
286     setFillAndStroke = function (o, params) {
287         var dasharray = {
288                 "": [0],
289                 "none": [0],
290                 "-": [3, 1],
291                 ".": [1, 1],
292                 "-.": [3, 1, 1, 1],
293                 "-..": [3, 1, 1, 1, 1, 1],
294                 ". ": [1, 3],
295                 "- ": [4, 3],
296                 "--": [8, 3],
297                 "- .": [4, 3, 1, 3],
298                 "--.": [8, 3, 1, 3],
299                 "--..": [8, 3, 1, 3, 1, 3]
300             },
301             node = o.node,
302             attrs = o.attrs,
303             addDashes = function (o, value) {
304                 value = dasharray[lowerCase.call(value)];
305                 if (value) {
306                     var width = o.attrs["stroke-width"] || "1",
307                         butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
308                         dashes = [],
309                         i = value.length;
310                     while (i--) {
311                         dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
312                     }
313                     $(node, {"stroke-dasharray": dashes.join(",")});
314                 }
315             };
316         for (var att in params) {
317             if (params[has](att)) {
318                 if (!R._availableAttrs[has](att)) {
319                     continue;
320                 }
321                 var value = params[att];
322                 attrs[att] = value;
323                 switch (att) {
324                     case "blur":
325                         o.blur(value);
326                         break;
327                     case "href":
328                     case "title":
329                     case "target":
330                         var pn = node.parentNode;
331                         if (lowerCase.call(pn.tagName) != "a") {
332                             var hl = $("a");
333                             pn.insertBefore(hl, node);
334                             hl.appendChild(node);
335                             pn = hl;
336                         }
337                         if (att == "target" && value == "blank") {
338                             pn.setAttributeNS(xlink, "show", "new");
339                         } else {
340                             pn.setAttributeNS(xlink, att, value);
341                         }
342                         break;
343                     case "cursor":
344                         node.style.cursor = value;
345                         break;
346                     case "transform":
347                         o.transform(value);
348                         break;
349                     case "arrow-start":
350                         addArrow(o, value);
351                         break;
352                     case "arrow-end":
353                         addArrow(o, value, 1);
354                         break;
355                     case "clip-rect":
356                         var rect = Str(value).split(separator);
357                         if (rect.length == 4) {
358                             o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
359                             var el = $("clipPath"),
360                                 rc = $("rect");
361                             el.id = R._createUUID();
362                             $(rc, {
363                                 x: rect[0],
364                                 y: rect[1],
365                                 width: rect[2],
366                                 height: rect[3]
367                             });
368                             el.appendChild(rc);
369                             o.paper.defs.appendChild(el);
370                             $(node, {"clip-path": "url(#" + el.id + ")"});
371                             o.clip = rc;
372                         }
373                         if (!value) {
374                             var clip = g.doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, E));
375                             clip && clip.parentNode.removeChild(clip);
376                             $(node, {"clip-path": E});
377                             delete o.clip;
378                         }
379                     break;
380                     case "path":
381                         if (o.type == "path") {
382                             $(node, {d: value ? attrs.path = R._pathToAbsolute(value) : "M0,0"});
383                             o._.dirty = 1;
384                             if (o._.arrows) {
385                                 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
386                                 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
387                             }
388                         }
389                         break;
390                     case "width":
391                         node.setAttribute(att, value);
392                         o._.dirty = 1;
393                         if (attrs.fx) {
394                             att = "x";
395                             value = attrs.x;
396                         } else {
397                             break;
398                         }
399                     case "x":
400                         if (attrs.fx) {
401                             value = -attrs.x - (attrs.width || 0);
402                         }
403                     case "rx":
404                         if (att == "rx" && o.type == "rect") {
405                             break;
406                         }
407                     case "cx":
408                         node.setAttribute(att, value);
409                         o.pattern && updatePosition(o);
410                         o._.dirty = 1;
411                         break;
412                     case "height":
413                         node.setAttribute(att, value);
414                         o._.dirty = 1;
415                         if (attrs.fy) {
416                             att = "y";
417                             value = attrs.y;
418                         } else {
419                             break;
420                         }
421                     case "y":
422                         if (attrs.fy) {
423                             value = -attrs.y - (attrs.height || 0);
424                         }
425                     case "ry":
426                         if (att == "ry" && o.type == "rect") {
427                             break;
428                         }
429                     case "cy":
430                         node.setAttribute(att, value);
431                         o.pattern && updatePosition(o);
432                         o._.dirty = 1;
433                         break;
434                     case "r":
435                         if (o.type == "rect") {
436                             $(node, {rx: value, ry: value});
437                         } else {
438                             node.setAttribute(att, value);
439                         }
440                         o._.dirty = 1;
441                         break;
442                     case "src":
443                         if (o.type == "image") {
444                             node.setAttributeNS(xlink, "href", value);
445                         }
446                         break;
447                     case "stroke-width":
448                         if (o._.sx != 1 || o._.sy != 1) {
449                             value /= mmax(abs(o._.sx), abs(o._.sy)) || 1;
450                         }
451                         if (o.paper._vbSize) {
452                             value *= o.paper._vbSize;
453                         }
454                         node.setAttribute(att, value);
455                         if (attrs["stroke-dasharray"]) {
456                             addDashes(o, attrs["stroke-dasharray"]);
457                         }
458                         if (o._.arrows) {
459                             "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
460                             "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
461                         }
462                         break;
463                     case "stroke-dasharray":
464                         addDashes(o, value);
465                         break;
466                     case "fill":
467                         var isURL = Str(value).match(R._ISURL);
468                         if (isURL) {
469                             el = $("pattern");
470                             var ig = $("image");
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]});
474                             el.appendChild(ig);
475
476                             (function (el) {
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});
482                                     o.paper.safari();
483                                 });
484                             })(el);
485                             o.paper.defs.appendChild(el);
486                             node.style.fill = "url(#" + el.id + ")";
487                             $(node, {fill: "url(#" + el.id + ")"});
488                             o.pattern = el;
489                             o.pattern && updatePosition(o);
490                             break;
491                         }
492                         var clr = R.getRGB(value);
493                         if (!clr.error) {
494                             delete params.gradient;
495                             delete attrs.gradient;
496                             !R.is(attrs.opacity, "undefined") &&
497                                 R.is(params.opacity, "undefined") &&
498                                 $(node, {opacity: attrs.opacity});
499                             !R.is(attrs["fill-opacity"], "undefined") &&
500                                 R.is(params["fill-opacity"], "undefined") &&
501                                 $(node, {"fill-opacity": attrs["fill-opacity"]});
502                         } else if ((o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value)) {
503                             if ("opacity" in attrs || "fill-opacity" in attrs) {
504                                 var gradient = g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
505                                 if (gradient) {
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)});
508                                 }
509                             }
510                             attrs.gradient = value;
511                             attrs.fill = "none";
512                             break;
513                         }
514                         clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
515                     case "stroke":
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);
522                         }
523                         break;
524                     case "gradient":
525                         (o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value);
526                         break;
527                     case "opacity":
528                         if (attrs.gradient && !attrs[has]("stroke-opacity")) {
529                             $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
530                         }
531                         // fall
532                     case "fill-opacity":
533                         if (attrs.gradient) {
534                             gradient = g.doc.getElementById(node.getAttribute("fill").replace(/^url\(#|\)$/g, E));
535                             if (gradient) {
536                                 stops = gradient.getElementsByTagName("stop");
537                                 $(stops[stops.length - 1], {"stop-opacity": value});
538                             }
539                             break;
540                         }
541                     default:
542                         att == "font-size" && (value = toInt(value, 10) + "px");
543                         var cssrule = att.replace(/(\-.)/g, function (w) {
544                             return w.substring(1).toUpperCase();
545                         });
546                         node.style[cssrule] = value;
547                         o._.dirty = 1;
548                         node.setAttribute(att, value);
549                         break;
550                 }
551             }
552         }
553
554         tuneText(o, params);
555     },
556     leading = 1.2,
557     tuneText = function (el, params) {
558         if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
559             return;
560         }
561         var a = el.attrs,
562             node = el.node,
563             fontSize = node.firstChild ? toInt(R._g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
564
565         if (params[has]("text")) {
566             a.text = params.text;
567             while (node.firstChild) {
568                 node.removeChild(node.firstChild);
569             }
570             var texts = Str(params.text).split("\n"),
571                 tspans = [],
572                 tspan;
573             for (var i = 0, ii = texts.length; i < ii; i++) {
574                 tspan = $("tspan");
575                 i && $(tspan, {dy: fontSize * leading, x: a.x});
576                 tspan.appendChild(R._g.doc.createTextNode(texts[i]));
577                 node.appendChild(tspan);
578                 tspans[i] = tspan;
579             }
580         } else {
581             tspans = node.getElementsByTagName("tspan");
582             for (i = 0, ii = tspans.length; i < ii; i++) if (i) {
583                 $(tspans[i], {dy: fontSize * leading, x: a.x});
584             } else {
585                 $(tspans[0], {dy: 0});
586             }
587         }
588         $(node, {x: a.x, y: a.y});
589         el._.dirty = 1;
590         var bb = el._getBBox(),
591             dif = a.y - (bb.y + bb.height / 2);
592         dif && R.is(dif, "finite") && $(tspans[0], {dy: dif});
593     },
594     Element = function (node, svg) {
595         var X = 0,
596             Y = 0;
597         /*\
598          * Element.node
599          [ property (object) ]
600          **
601          * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
602          > Usage
603          | // draw a circle at coordinate 10,10 with radius of 10
604          | var c = paper.circle(10, 10, 10);
605          | c.node.onclick = function () {
606          |     c.attr("fill", "red");
607          | };
608         \*/
609         this[0] = this.node = node;
610         /*\
611          * Element.raphael
612          [ property (object) ]
613          **
614          * Internal reference to @Raphael object. In case it is not available.
615          > Usage
616          | Raphael.el.red = function () {
617          |     var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
618          |     hsb.h = 1;
619          |     this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
620          | }
621         \*/
622         node.raphael = true;
623         /*\
624          * Element.id
625          [ property (number) ]
626          **
627          * Unique id of the element. Especially usesful when you want to listen to events of the element, 
628          * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
629         \*/
630         this.id = R._oid++;
631         node.raphaelid = this.id;
632         this.matrix = R.matrix();
633         this.realPath = null;
634         /*\
635          * Element.paper
636          [ property (object) ]
637          **
638          * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
639          > Usage
640          | Raphael.el.cross = function () {
641          |     this.attr({fill: "red"});
642          |     this.paper.path("M10,10L50,50M50,10L10,50")
643          |         .attr({stroke: "red"});
644          | }
645         \*/
646         this.paper = svg;
647         this.attrs = this.attrs || {};
648         this._ = {
649             transform: [],
650             sx: 1,
651             sy: 1,
652             deg: 0,
653             dx: 0,
654             dy: 0,
655             dirty: 1
656         };
657         !svg.bottom && (svg.bottom = this);
658         /*\
659          * Element.prev
660          [ property (object) ]
661          **
662          * Reference to the previous element in the hierarchy.
663         \*/
664         this.prev = svg.top;
665         svg.top && (svg.top.next = this);
666         svg.top = this;
667         /*\
668          * Element.next
669          [ property (object) ]
670          **
671          * Reference to the next element in the hierarchy.
672         \*/
673         this.next = null;
674     },
675     elproto = R.el;
676
677     Element.prototype = elproto;
678     elproto.constructor = Element;
679
680     R._engine.path = function (pathString, SVG) {
681         var el = $("path");
682         SVG.canvas && SVG.canvas.appendChild(el);
683         var p = new Element(el, SVG);
684         p.type = "path";
685         setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
686         return p;
687     };
688     /*\
689      * Element.rotate
690      [ method ]
691      **
692      * Adds rotation by given angle around given point to the list of
693      * transformations of the element.
694      > Parameters
695      - deg (number) angle in degrees
696      - cx (number) #optional x coordinate of the centre of rotation
697      - cy (number) #optional y coordinate of the centre of rotation
698      * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
699      = (object) @Element
700     \*/
701     elproto.rotate = function (deg, cx, cy) {
702         if (this.removed) {
703             return this;
704         }
705         deg = Str(deg).split(separator);
706         if (deg.length - 1) {
707             cx = toFloat(deg[1]);
708             cy = toFloat(deg[2]);
709         }
710         deg = toFloat(deg[0]);
711         (cy == null) && (cx = cy);
712         if (cx == null || cy == null) {
713             var bbox = this.getBBox(1);
714             cx = bbox.x + bbox.width / 2;
715             cy = bbox.y + bbox.height / 2;
716         }
717         this.transform(this._.transform.concat([["r", deg, cx, cy]]));
718         return this;
719     };
720     /*\
721      * Element.scale
722      [ method ]
723      **
724      * Adds scale by given amount relative to given point to the list of
725      * transformations of the element.
726      > Parameters
727      - sx (number) horisontal scale amount
728      - sy (number) vertical scale amount
729      - cx (number) #optional x coordinate of the centre of scale
730      - cy (number) #optional y coordinate of the centre of scale
731      * If cx & cy aren’t specified centre of the shape is used instead.
732      = (object) @Element
733     \*/
734     elproto.scale = function (sx, sy, cx, cy) {
735         if (this.removed) {
736             return this;
737         }
738         sx = Str(sx).split(separator);
739         if (sx.length - 1) {
740             sy = toFloat(sx[1]);
741             cx = toFloat(sx[2]);
742             cy = toFloat(sx[3]);
743         }
744         sx = toFloat(sx[0]);
745         (sy == null) && (sy = sx);
746         (cy == null) && (cx = cy);
747         if (cx == null || cy == null) {
748             var bbox = this.getBBox(1);
749         }
750         cx = cx == null ? bbox.x + bbox.width / 2 : cx;
751         cy = cy == null ? bbox.y + bbox.height / 2 : cy;
752         this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
753         return this;
754     };
755     /*\
756      * Element.translate
757      [ method ]
758      **
759      * Adds translation by given amount to the list of transformations of the element.
760      > Parameters
761      - dx (number) horisontal shift
762      - dy (number) vertical shift
763      = (object) @Element
764     \*/
765     elproto.translate = function (dx, dy) {
766         if (this.removed) {
767             return this;
768         }
769         dx = Str(dx).split(separator);
770         if (dx.length - 1) {
771             dy = toFloat(dx[1]);
772         }
773         dx = toFloat(dx[0]) || 0;
774         dy = +dy || 0;
775         this.transform(this._.transform.concat([["t", dx, dy]]));
776         return this;
777     };
778     /*\
779      * Element.transform
780      [ method ]
781      **
782      * Adds transformation to the element which is separate to other attributes,
783      * i.e. translation doesn’t change `x` or `y` of the rectange. The format
784      * of transformation string is similar to the path string syntax:
785      | "t100,100r30,100,100s2,2,100,100r45s1.5"
786      * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
787      * scale and `m` is for matrix.
788      *
789      * So, the example line above could be read like “translate by 100, 100; rotate 30° around 100, 100; scale twice around 100, 100;
790      * rotate 45° around centre; scale 1.5 times relative to centre”. As you can see rotate and scale commands have origin
791      * coordinates as optional parameters, the default is the centre point of the element.
792      * Matrix accepts six parameters.
793      > Usage
794      | var el = paper.rect(10, 20, 300, 200);
795      | // translate 100, 100, rotate 45°, translate -100, 0
796      | el.transform("t100,100r45t-100,0");
797      | // if you want you can append or prepend transformations
798      | el.transform("...t50,50");
799      | el.transform("s2...");
800      | // or even wrap
801      | el.transform("t50,50...t-50-50");
802      | // to reset transformation call method with empty string
803      | el.transform("");
804      | // to get current value call it without parameters
805      | console.log(el.transform());
806      > Parameters
807      - tstr (string) #optional transformation string
808      * If tstr isn’t specified
809      = (string) current transformation string
810      * else
811      = (object) @Element
812     \*/
813     elproto.transform = function (tstr) {
814         var _ = this._;
815         if (tstr == null) {
816             return _.transform;
817         }
818         R._extractTransform(this, tstr);
819
820         this.clip && $(this.clip, {transform: this.matrix.invert()});
821         this.pattern && updatePosition(this);
822         this.node && $(this.node, {transform: this.matrix});
823     
824         if (_.sx != 1 || _.sy != 1) {
825             var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
826             this.attr({"stroke-width": sw});
827         }
828
829         return this;
830     };
831     /*\
832      * Element.hide
833      [ method ]
834      **
835      * Makes element invisible. See @Element.show.
836      = (object) @Element
837     \*/
838     elproto.hide = function () {
839         !this.removed && this.paper.safari(this.node.style.display = "none");
840         return this;
841     };
842     /*\
843      * Element.show
844      [ method ]
845      **
846      * Makes element visible. See @Element.hide.
847      = (object) @Element
848     \*/
849     elproto.show = function () {
850         !this.removed && this.paper.safari(this.node.style.display = "");
851         return this;
852     };
853     /*\
854      * Element.remove
855      [ method ]
856      **
857      * Removes element form the paper.
858     \*/
859     elproto.remove = function () {
860         if (this.removed) {
861             return;
862         }
863         eve.unbind("*.*." + this.id);
864         R._tear(this, this.paper);
865         this.node.parentNode.removeChild(this.node);
866         for (var i in this) {
867             delete this[i];
868         }
869         this.removed = true;
870     };
871     elproto._getBBox = function () {
872         if (this.node.style.display == "none") {
873             this.show();
874             var hide = true;
875         }
876         var bbox = {};
877         try {
878             bbox = this.node.getBBox();
879         } catch(e) {
880             // Firefox 3.0.x plays badly here
881         } finally {
882             bbox = bbox || {};
883         }
884         hide && this.hide();
885         return bbox;
886     };
887     /*\
888      * Element.attr
889      [ method ]
890      **
891      * Sets the attributes of the element.
892      > Parameters
893      - attrName (string) attribute’s name
894      - value (string) value
895      * or
896      - params (object) object of name/value pairs
897      * or
898      - attrName (string) attribute’s name
899      * or
900      - attrNames (array) in this case method returns array of current values for given attribute names
901      = (object) @Element if attrsName & value or params are passed in.
902      = (...) value of the attribute if only attrsName is passed in.
903      = (array) array of values of the attribute if attrsNames is passed in.
904      = (object) object of attributes if nothing is passed in.
905      > Possible parameters
906      # <p>Please refer to the <a href="http://www.w3.org/TR/SVG/" title="The W3C Recommendation for the SVG language describes these properties in detail.">SVG specification</a> for an explanation of these parameters.</p>
907      o arrow-end (string) arrowhead on the end of the path. The format for string is `<type>[-<width>[-<length>]]`. Possible types: `classic`, `block`, `open`, `oval`, `diamond`, `none`, width: `wide`, `narrow`, `midium`, length: `long`, `short`, `midium`.
908      o clip-rect (string) comma or space separated values: x, y, width and height
909      o cursor (string) CSS type of the cursor
910      o cx (number)
911      o cy (number)
912      o fill (string) colour, gradient or image
913      o fill-opacity (number)
914      o font (string)
915      o font-family (string)
916      o font-size (number) font size in pixels
917      o font-weight (string)
918      o height (number)
919      o href (string) URL, if specified element behaves as hyperlink
920      o opacity (number)
921      o path (string) SVG path string format
922      o r (number)
923      o rx (number)
924      o ry (number)
925      o src (string) image URL, only works for @Element.image element
926      o stroke (string) stroke colour
927      o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
928      o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
929      o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
930      o stroke-miterlimit (number)
931      o stroke-opacity (number)
932      o stroke-width (number) stroke width in pixels, default is '1'
933      o target (string) used with href
934      o text (string) contents of the text element. Use `\n` for multiline text
935      o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
936      o title (string) will create tooltip with a given text
937      o transform (string) see @Element.transform
938      o width (number)
939      o x (number)
940      o y (number)
941      > Gradients
942      * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
943      * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
944      *
945      * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
946      * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
947      * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
948      > Path String
949      # <p>Please refer to <a href="http://www.w3.org/TR/SVG/paths.html#PathData" title="Details of a path’s data attribute’s format are described in the SVG specification.">SVG documentation regarding path string</a>. Raphaël fully supports it.</p>
950      > Colour Parsing
951      # <ul>
952      #     <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
953      #     <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
954      #     <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
955      #     <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200,&nbsp;100,&nbsp;0)</code>”)</li>
956      #     <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%,&nbsp;175%,&nbsp;0%)</code>”)</li>
957      #     <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200,&nbsp;100,&nbsp;0, .5)</code>”)</li>
958      #     <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%,&nbsp;175%,&nbsp;0%, 50%)</code>”)</li>
959      #     <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5,&nbsp;0.25,&nbsp;1)</code>”)</li>
960      #     <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
961      #     <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
962      #     <li>hsl(•••, •••, •••) — almost the same as hsb, see <a href="http://en.wikipedia.org/wiki/HSL_and_HSV" title="HSL and HSV - Wikipedia, the free encyclopedia">Wikipedia page</a></li>
963      #     <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
964      #     <li>hsla(•••, •••, •••) — same as above, but with opacity</li>
965      #     <li>Optionally for hsb and hsl you could specify hue as a degree: “<code>hsl(240deg,&nbsp;1,&nbsp;.5)</code>” or, if you want to go fancy, “<code>hsl(240°,&nbsp;1,&nbsp;.5)</code>”</li>
966      # </ul>
967     \*/
968     elproto.attr = function (name, value) {
969         if (this.removed) {
970             return this;
971         }
972         if (name == null) {
973             var res = {};
974             for (var a in this.attrs) if (this.attrs[has](a)) {
975                 res[a] = this.attrs[a];
976             }
977             res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
978             res.transform = this._.transform;
979             return res;
980         }
981         if (value == null && R.is(name, "string")) {
982             if (name == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
983                 return this.attrs.gradient;
984             }
985             if (name == "transform") {
986                 return this._.transform;
987             }
988             var names = name.split(separator),
989                 out = {};
990             for (var i = 0, ii = names.length; i < ii; i++) {
991                 name = names[i];
992                 if (name in this.attrs) {
993                     out[name] = this.attrs[name];
994                 } else if (R.is(this.paper.customAttributes[name], "function")) {
995                     out[name] = this.paper.customAttributes[name].def;
996                 } else {
997                     out[name] = R._availableAttrs[name];
998                 }
999             }
1000             return ii - 1 ? out : out[names[0]];
1001         }
1002         if (value == null && R.is(name, "array")) {
1003             out = {};
1004             for (i = 0, ii = name.length; i < ii; i++) {
1005                 out[name[i]] = this.attr(name[i]);
1006             }
1007             return out;
1008         }
1009         if (value != null) {
1010             var params = {};
1011             params[name] = value;
1012         } else if (name != null && R.is(name, "object")) {
1013             params = name;
1014         }
1015         for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
1016             var par = this.paper.customAttributes[key].apply(this, [].concat(params[key]));
1017             this.attrs[key] = params[key];
1018             for (var subkey in par) if (par[has](subkey)) {
1019                 params[subkey] = par[subkey];
1020             }
1021         }
1022         setFillAndStroke(this, params);
1023         return this;
1024     };
1025     /*\
1026      * Element.toFront
1027      [ method ]
1028      **
1029      * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
1030      = (object) @Element
1031     \*/
1032     elproto.toFront = function () {
1033         if (this.removed) {
1034             return this;
1035         }
1036         this.node.parentNode.appendChild(this.node);
1037         var svg = this.paper;
1038         svg.top != this && R._tofront(this, svg);
1039         return this;
1040     };
1041     /*\
1042      * Element.toBack
1043      [ method ]
1044      **
1045      * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
1046      = (object) @Element
1047     \*/
1048     elproto.toBack = function () {
1049         if (this.removed) {
1050             return this;
1051         }
1052         if (this.node.parentNode.firstChild != this.node) {
1053             this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
1054             R._toback(this, this.paper);
1055             var svg = this.paper;
1056         }
1057         return this;
1058     };
1059     /*\
1060      * Element.insertAfter
1061      [ method ]
1062      **
1063      * Inserts current object after the given one.
1064      = (object) @Element
1065     \*/
1066     elproto.insertAfter = function (element) {
1067         if (this.removed) {
1068             return this;
1069         }
1070         var node = element.node || element[element.length - 1].node;
1071         if (node.nextSibling) {
1072             node.parentNode.insertBefore(this.node, node.nextSibling);
1073         } else {
1074             node.parentNode.appendChild(this.node);
1075         }
1076         R._insertafter(this, element, this.paper);
1077         return this;
1078     };
1079     /*\
1080      * Element.insertBefore
1081      [ method ]
1082      **
1083      * Inserts current object before the given one.
1084      = (object) @Element
1085     \*/
1086     elproto.insertBefore = function (element) {
1087         if (this.removed) {
1088             return this;
1089         }
1090         var node = element.node || element[0].node;
1091         node.parentNode.insertBefore(this.node, node);
1092         R._insertbefore(this, element, this.paper);
1093         return this;
1094     };
1095     elproto.blur = function (size) {
1096         // Experimental. No Safari support. Use it on your own risk.
1097         var t = this;
1098         if (+size !== 0) {
1099             var fltr = $("filter"),
1100                 blur = $("feGaussianBlur");
1101             t.attrs.blur = size;
1102             fltr.id = R._createUUID();
1103             $(blur, {stdDeviation: +size || 1.5});
1104             fltr.appendChild(blur);
1105             t.paper.defs.appendChild(fltr);
1106             t._blur = fltr;
1107             $(t.node, {filter: "url(#" + fltr.id + ")"});
1108         } else {
1109             if (t._blur) {
1110                 t._blur.parentNode.removeChild(t._blur);
1111                 delete t._blur;
1112                 delete t.attrs.blur;
1113             }
1114             t.node.removeAttribute("filter");
1115         }
1116     };
1117     R._engine.circle = function (svg, x, y, r) {
1118         var el = $("circle");
1119         svg.canvas && svg.canvas.appendChild(el);
1120         var res = new Element(el, svg);
1121         res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
1122         res.type = "circle";
1123         $(el, res.attrs);
1124         return res;
1125     };
1126     R._engine.rect = function (svg, x, y, w, h, r) {
1127         var el = $("rect");
1128         svg.canvas && svg.canvas.appendChild(el);
1129         var res = new Element(el, svg);
1130         res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
1131         res.type = "rect";
1132         $(el, res.attrs);
1133         return res;
1134     };
1135     R._engine.ellipse = function (svg, x, y, rx, ry) {
1136         var el = $("ellipse");
1137         svg.canvas && svg.canvas.appendChild(el);
1138         var res = new Element(el, svg);
1139         res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
1140         res.type = "ellipse";
1141         $(el, res.attrs);
1142         return res;
1143     };
1144     R._engine.image = function (svg, src, x, y, w, h) {
1145         var el = $("image");
1146         $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
1147         el.setAttributeNS(xlink, "href", src);
1148         svg.canvas && svg.canvas.appendChild(el);
1149         var res = new Element(el, svg);
1150         res.attrs = {x: x, y: y, width: w, height: h, src: src};
1151         res.type = "image";
1152         return res;
1153     };
1154     R._engine.text = function (svg, x, y, text) {
1155         var el = $("text");
1156         // $(el, {x: x, y: y, "text-anchor": "middle"});
1157         svg.canvas && svg.canvas.appendChild(el);
1158         var res = new Element(el, svg);
1159         res.attrs = {
1160             x: x,
1161             y: y,
1162             "text-anchor": "middle",
1163             text: text,
1164             font: R._availableAttrs.font,
1165             stroke: "none",
1166             fill: "#000"
1167         };
1168         res.type = "text";
1169         setFillAndStroke(res, res.attrs);
1170         return res;
1171     };
1172     R._engine.setSize = function (width, height) {
1173         this.width = width || this.width;
1174         this.height = height || this.height;
1175         this.canvas.setAttribute("width", this.width);
1176         this.canvas.setAttribute("height", this.height);
1177         if (this._viewBox) {
1178             this.setViewBox.apply(this, this._viewBox);
1179         }
1180         return this;
1181     };
1182     R._engine.create = function () {
1183         var con = R._getContainer.apply(0, arguments),
1184             container = con && con.container,
1185             x = con.x,
1186             y = con.y,
1187             width = con.width,
1188             height = con.height;
1189         if (!container) {
1190             throw new Error("SVG container not found.");
1191         }
1192         var cnvs = $("svg"),
1193             css = "overflow:hidden;",
1194             isFloating;
1195         x = x || 0;
1196         y = y || 0;
1197         width = width || 512;
1198         height = height || 342;
1199         $(cnvs, {
1200             height: height,
1201             version: 1.1,
1202             width: width,
1203             xmlns: "http://www.w3.org/2000/svg"
1204         });
1205         if (container == 1) {
1206             cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
1207             R._g.doc.body.appendChild(cnvs);
1208             isFloating = 1;
1209         } else {
1210             cnvs.style.cssText = css + "position:relative";
1211             if (container.firstChild) {
1212                 container.insertBefore(cnvs, container.firstChild);
1213             } else {
1214                 container.appendChild(cnvs);
1215             }
1216         }
1217         container = new R._Paper;
1218         container.width = width;
1219         container.height = height;
1220         container.canvas = cnvs;
1221         // plugins.call(container, container, R.fn);
1222         container.clear();
1223         container._left = container._top = 0;
1224         isFloating && (container.renderfix = function () {});
1225         container.renderfix();
1226         return container;
1227     };
1228     R._engine.setViewBox = function (x, y, w, h, fit) {
1229         eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
1230         var size = mmax(w / this.width, h / this.height),
1231             top = this.top,
1232             aspectRatio = fit ? "meet" : "xMinYMin",
1233             vb,
1234             sw;
1235         if (x == null) {
1236             if (this._vbSize) {
1237                 size = 1;
1238             }
1239             delete this._vbSize;
1240             vb = "0 0 " + this.width + S + this.height;
1241         } else {
1242             this._vbSize = size;
1243             vb = x + S + y + S + w + S + h;
1244         }
1245         $(this.canvas, {
1246             viewBox: vb,
1247             preserveAspectRatio: aspectRatio
1248         });
1249         while (size && top) {
1250             sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
1251             top.attr({"stroke-width": sw});
1252             top._.dirty = 1;
1253             top._.dirtyT = 1;
1254             top = top.prev;
1255         }
1256         this._viewBox = [x, y, w, h, !!fit];
1257         return this;
1258     };
1259     /*\
1260      * Paper.renderfix
1261      [ method ]
1262      **
1263      * Fixes the issue of Firefox and IE9 regarding subpixel rendering. If paper is dependant
1264      * on other elements after reflow it could shift half pixel which cause for lines to lost their crispness.
1265      * This method fixes the issue.
1266      **
1267        Special thanks to Mariusz Nowak (http://www.medikoo.com/) for this method.
1268     \*/
1269     R.prototype.renderfix = function () {
1270         var cnvs = this.canvas,
1271             s = cnvs.style,
1272             pos = cnvs.getScreenCTM(),
1273             left = -pos.e % 1,
1274             top = -pos.f % 1;
1275         if (left || top) {
1276             if (left) {
1277                 this._left = (this._left + left) % 1;
1278                 s.left = this._left + "px";
1279             }
1280             if (top) {
1281                 this._top = (this._top + top) % 1;
1282                 s.top = this._top + "px";
1283             }
1284         }
1285     };
1286     /*\
1287      * Paper.clear
1288      [ method ]
1289      **
1290      * Clears the paper, i.e. removes all the elements.
1291     \*/
1292     R.prototype.clear = function () {
1293         R.eve("clear", this);
1294         var c = this.canvas;
1295         while (c.firstChild) {
1296             c.removeChild(c.firstChild);
1297         }
1298         this.bottom = this.top = null;
1299         (this.desc = $("desc")).appendChild(R._g.doc.createTextNode("Created with Rapha\xebl " + R.version));
1300         c.appendChild(this.desc);
1301         c.appendChild(this.defs = $("defs"));
1302     };
1303     /*\
1304      * Paper.remove
1305      [ method ]
1306      **
1307      * Removes the paper from the DOM.
1308     \*/
1309     R.prototype.remove = function () {
1310         eve("remove", this);
1311         this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
1312         for (var i in this) {
1313             this[i] = removed(i);
1314         }
1315     };
1316     var setproto = R.st;
1317     for (var method in elproto) if (elproto[has](method) && !setproto[has](method)) {
1318         setproto[method] = (function (methodname) {
1319             return function () {
1320                 var arg = arguments;
1321                 return this.forEach(function (el) {
1322                     el[methodname].apply(el, arg);
1323                 });
1324             };
1325         })(method);
1326     }
1327 }(window.Raphael);