2 * Raphaël 2.0.0 - JavaScript Vector Library
4 * Copyright (c) 2011 Dmitry Baranovskiy (http://raphaeljs.com)
5 * Copyright (c) 2011 Sencha Labs (http://sencha.com)
6 * Licensed under the MIT (http://raphaeljs.com/license.html) license.
13 * Creates a canvas object on which to draw.
14 * You must do this first, as all future calls to drawing methods
15 * from this instance will be bound to this canvas.
18 - container (HTMLElement|string) DOM element or it’s id which is going to be a parent for drawing surface
27 - all (array) (first 3 or 4 elements in the array are equal to [containerID, width, height] or [x, y, width, height]. The rest are element descriptions in format {type: type, <attributes>})
29 - onReadyCallback (function) function that is going to be called on DOM ready event. You can also subscribe to this event via Eve’s “DOMLoad” event. In this case method returns `undefined`.
32 | // Each of the following examples create a canvas that is 320px wide by 200px high
33 | // Canvas is created at the viewport’s 10,50 coordinate
34 | var paper = Raphael(10, 50, 320, 200);
35 | // Canvas is created at the top left corner of the #notepad element
36 | // (or its top right corner in dir="rtl" elements)
37 | var paper = Raphael(document.getElementById("notepad"), 320, 200);
39 | var paper = Raphael("notepad", 320, 200);
41 | var set = Raphael(["notepad", 320, 200, {
56 if (R.is(first, "function")) {
57 return eve.on("DOMload", first);
58 } else if (R.is(first, array)) {
60 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
67 elements[has](j.type) && res.push(cnv[j.type]().attr(j));
71 return create[apply](R, arguments);
74 var separator = /[, ]+/,
75 elements = {circle: 1, rect: 1, path: 1, ellipse: 1, text: 1, image: 1},
76 formatrg = /\{(\d+)\}/g,
78 has = "hasOwnProperty",
84 was: Object.prototype[has].call(g.win, "Raphael"),
87 Paper = function () {},
89 appendChild = "appendChild",
92 supportsTouch = "createTouch" in g.doc,
97 events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend".split(S),
99 mousedown: "touchstart",
100 mousemove: "touchmove",
103 lowerCase = Str.prototype.toLowerCase,
113 toString = "toString",
115 objectToString = Object.prototype.toString,
118 ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
119 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+%?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsba?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\)|hsla?\(\s*([\d\.]+(?:deg|\xb0|%)?\s*,\s*[\d\.]+%?\s*,\s*[\d\.]+(?:%?\s*,\s*[\d\.]+)?)%?\s*\))\s*$/i,
120 isnan = {"NaN": 1, "Infinity": 1, "-Infinity": 1},
121 bezierrg = /^(?:cubic-)?bezier\(([^,]+),([^,]+),([^,]+),([^\)]+)\)/,
123 setAttribute = "setAttribute",
124 toFloat = parseFloat,
126 ms = " progid:DXImageTransform.Microsoft",
127 upperCase = Str.prototype.toUpperCase,
128 availableAttrs = {"arrow-end": "none", "arrow-start": "none", blur: 0, "clip-rect": "0 0 1e9 1e9", cursor: "default", cx: 0, cy: 0, fill: "#fff", "fill-opacity": 1, font: '10px "Arial"', "font-family": '"Arial"', "font-size": "10", "font-style": "normal", "font-weight": 400, gradient: 0, height: 0, href: "http://raphaeljs.com/", opacity: 1, path: "M0,0", r: 0, rx: 0, ry: 0, src: "", stroke: "#000", "stroke-dasharray": "", "stroke-linecap": "butt", "stroke-linejoin": "butt", "stroke-miterlimit": 0, "stroke-opacity": 1, "stroke-width": 1, target: "_blank", "text-anchor": "middle", title: "Raphael", transform: "", width: 0, x: 0, y: 0},
129 availableAnimAttrs = {blur: nu, "clip-rect": "csv", cx: nu, cy: nu, fill: "colour", "fill-opacity": nu, "font-size": nu, height: nu, opacity: nu, path: "path", r: nu, rx: nu, ry: nu, stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, transform: "transform", width: nu, x: nu, y: nu},
130 commaSpaces = /\s*,\s*/,
131 hsrg = {hs: 1, rg: 1},
132 p2s = /,?([achlmqrstvxz]),?/gi,
133 pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
134 tCommand = /([rstm])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
135 pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig,
136 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/,
137 sortByKey = function (a, b) {
138 return a.key - b.key;
140 sortByNumber = function (a, b) {
143 fun = function () {},
144 pipe = function (x) {
147 rectPath = function (x, y, w, h, r) {
149 return [["M", x + r, y], ["l", w - r * 2, 0], ["a", r, r, 0, 0, 1, r, r], ["l", 0, h - r * 2], ["a", r, r, 0, 0, 1, -r, r], ["l", r * 2 - w, 0], ["a", r, r, 0, 0, 1, -r, -r], ["l", 0, r * 2 - h], ["a", r, r, 0, 0, 1, r, -r], ["z"]];
151 return [["M", x, y], ["l", w, 0], ["l", 0, h], ["l", -w, 0], ["z"]];
153 ellipsePath = function (x, y, rx, ry) {
157 return [["M", x, y], ["m", 0, -ry], ["a", rx, ry, 0, 1, 1, 0, 2 * ry], ["a", rx, ry, 0, 1, 1, 0, -2 * ry], ["z"]];
160 path: function (el) {
161 return el.attr("path");
163 circle: function (el) {
165 return ellipsePath(a.cx, a.cy, a.r);
167 ellipse: function (el) {
169 return ellipsePath(a.cx, a.cy, a.rx, a.ry);
171 rect: function (el) {
173 return rectPath(a.x, a.y, a.width, a.height, a.r);
175 image: function (el) {
177 return rectPath(a.x, a.y, a.width, a.height);
179 text: function (el) {
180 var bbox = el._getBBox();
181 return rectPath(bbox.x, bbox.y, bbox.width, bbox.height);
184 mapPath = function (path, matrix) {
188 var x, y, i, j, pathi;
189 path = path2curve(path);
190 for (i = 0, ii = path.length; i < ii; i++) {
192 for (j = 1, jj = pathi.length; j < jj; j += 2) {
193 x = matrix.x(pathi[j], pathi[j + 1]);
194 y = matrix.y(pathi[j], pathi[j + 1]);
204 [ property (string) ]
206 * Can be “SVG”, “VML” or empty, depending on browser support.
208 R.type = (g.win.SVGAngle || g.doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
209 if (R.type == "VML") {
210 var d = g.doc.createElement("div"),
212 d.innerHTML = '<v:shape adj="1"/>';
214 b.style.behavior = "url(#default#VML)";
215 if (!(b && typeof b.adj == "object")) {
222 [ property (boolean) ]
224 * `true` if browser supports SVG.
228 [ property (boolean) ]
230 * `true` if browser supports VML.
232 R.svg = !(R.vml = R.type == "VML");
233 paperproto = Paper.prototype = R.prototype;
235 * Paper.customAttributes
236 [ property (object) ]
238 * If you have a set of attributes that you would like to represent
239 * as a function of some number you can do it easily with custom attributes:
241 | paper.customAttributes.hue = function (num) {
243 | return {fill: "hsb(" + num + ", .75, 1)"};
245 | // Custom attribute “hue” will change fill
246 | // to be given hue with fixed saturation and brightness.
247 | // Now you can use it like this:
248 | var c = paper.circle(10, 10, 10).attr({hue: .45});
249 | // or even like this:
250 | c.animate({hue: 1}, 1e3);
252 | // You could also create custom attribute
253 | // with multiple parameters:
254 | paper.customAttributes.hsb = function (h, s, b) {
255 | return {fill: "hsb(" + [h, s, b].join(",") + ")"};
257 | c.attr({hsb: ".5 .8 1"});
258 | c.animate({hsb: "1 0 .5"}, 1e3);
260 paperproto.customAttributes = {};
265 [ property (object) ]
267 * You can add your own method to the canvas. For example if you want to draw pie chart,
268 * you can create your own pie chart function and ship it as a Raphaël plugin. To do this
269 * you need to extend `Raphael.fn` object. Please note that you can create your own namespaces
270 * inside `fn` object. Methods will be run in context of canvas anyway. You should alter `fn`
271 * object before Raphaël instance was created, otherwise it will take no effect.
273 | Raphael.fn.arrow = function (x1, y1, x2, y2, size) {
274 | return this.path( ... );
276 | // or create namespace
277 | Raphael.fn.mystuff = {
278 | arrow: function () {…},
279 | star: function () {…},
282 | var paper = Raphael(10, 10, 630, 480);
284 | paper.arrow(10, 10, 30, 30, 5).attr({fill: "#f00"});
285 | paper.mystuff.arrow();
286 | paper.mystuff.star();
293 * Handfull replacement for `typeof` operator.
295 - o (…) any object or primitive
296 - type (string) name of the type, i.e. “string”, “function”, “number”, etc.
297 = (boolean) is given value is of given type
299 R.is = function (o, type) {
300 type = lowerCase.call(type);
301 if (type == "finite") {
302 return !isnan[has](+o);
304 return (type == "null" && o === null) ||
305 (type == typeof o) ||
306 (type == "object" && o === Object(o)) ||
307 (type == "array" && Array.isArray && Array.isArray(o)) ||
308 objectToString.call(o).slice(8, -1).toLowerCase() == type;
314 * Returns angle between two or three points
316 - x1 (number) x coord of first point
317 - y1 (number) y coord of first point
318 - x2 (number) x coord of second point
319 - y2 (number) y coord of second point
320 - x3 (number) #optional x coord of third point
321 - y3 (number) #optional y coord of third point
322 = (number) angle in degrees.
324 R.angle = function (x1, y1, x2, y2, x3, y3) {
331 return (180 + math.atan2(-y, -x) * 180 / PI + 360) % 360;
333 return R.angle(x1, y1, x3, y3) - R.angle(x2, y2, x3, y3);
340 * Transform angle to radians
342 - deg (number) angle in degrees
343 = (number) angle in radians.
345 R.rad = function (deg) {
346 return deg % 360 * PI / 180;
352 * Transform angle to degrees
354 - deg (number) angle in radians
355 = (number) angle in degrees.
357 R.deg = function (rad) {
358 return rad * 180 / PI % 360;
364 * Snaps given value to given grid.
366 - values (array|number) given array of values or step of the grid
367 - value (number) value to adjust
368 - tolerance (number) #optional tolerance for snapping. Default is `10`.
369 = (number) adjusted value.
371 R.snapTo = function (values, value, tolerance) {
372 tolerance = R.is(tolerance, "finite") ? tolerance : 10;
373 if (R.is(values, array)) {
374 var i = values.length;
375 while (i--) if (abs(values[i] - value) <= tolerance) {
380 var rem = value % values;
381 if (rem < tolerance) {
384 if (rem > values - tolerance) {
385 return value - rem + values;
391 var createUUID = (function (uuidRegEx, uuidReplacer) {
393 return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(uuidRegEx, uuidReplacer).toUpperCase();
395 })(/[xy]/g, function (c) {
396 var r = math.random() * 16 | 0,
397 v = c == "x" ? r : (r & 3 | 8);
398 return v.toString(16);
405 * Used when you need to draw in `<iframe>`. Switched window to the iframe one.
407 - newwin (window) new window object
409 R.setWindow = function (newwin) {
410 eve("setWindow", R, g.win, newwin);
412 g.doc = g.win.document;
418 var toHex = function (color) {
420 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
421 var trim = /^\s+|\s+$/g;
424 var docum = new ActiveXObject("htmlfile");
425 docum.write("<body>");
429 bod = createPopup().document.body;
431 var range = bod.createTextRange();
432 toHex = cacher(function (color) {
434 bod.style.color = Str(color).replace(trim, E);
435 var value = range.queryCommandValue("ForeColor");
436 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
437 return "#" + ("000000" + value.toString(16)).slice(-6);
443 var i = g.doc.createElement("i");
444 i.title = "Rapha\xebl Colour Picker";
445 i.style.display = "none";
446 g.doc.body.appendChild(i);
447 toHex = cacher(function (color) {
448 i.style.color = color;
449 return g.doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
454 hsbtoString = function () {
455 return "hsb(" + [this.h, this.s, this.b] + ")";
457 hsltoString = function () {
458 return "hsl(" + [this.h, this.s, this.l] + ")";
460 rgbtoString = function () {
463 prepareRGB = function (r, g, b) {
464 if (g == null && R.is(r, "object") && "r" in r && "g" in r && "b" in r) {
469 if (g == null && R.is(r, string)) {
470 var clr = R.getRGB(r);
475 if (r > 1 || g > 1 || b > 1) {
483 packageRGB = function (r, g, b, o) {
492 toString: rgbtoString
494 R.is(o, "finite") && (rgb.opacity = o);
501 * Converts HSB values to RGB object.
504 - s (number) saturation
505 - v (number) value or brightness
506 = (object) RGB object in format:
511 o hex (string) color in HTML/CSS format: #••••••
514 R.hsb2rgb = function (h, s, v, o) {
515 if (this.is(h, "object") && "h" in h && "s" in h && "b" in h) {
525 X = C * (1 - abs(h % 2 - 1));
529 R += [C, X, 0, 0, X, C][h];
530 G += [X, C, C, X, 0, 0][h];
531 B += [0, 0, X, C, C, X][h];
532 return packageRGB(R, G, B, o);
538 * Converts HSL values to RGB object.
541 - s (number) saturation
542 - l (number) luminosity
543 = (object) RGB object in format:
548 o hex (string) color in HTML/CSS format: #••••••
551 R.hsl2rgb = function (h, s, l, o) {
552 if (this.is(h, "object") && "h" in h && "s" in h && "l" in h) {
557 if (h > 1 || s > 1 || l > 1) {
565 C = 2 * s * (l < .5 ? l : 1 - l);
566 X = C * (1 - abs(h % 2 - 1));
567 R = G = B = l - C / 2;
570 R += [C, X, 0, 0, X, C][h];
571 G += [X, C, C, X, 0, 0][h];
572 B += [0, 0, X, C, C, X][h];
573 return packageRGB(R, G, B, o);
579 * Converts RGB values to HSB object.
584 = (object) HSB object in format:
587 o s (number) saturation
588 o b (number) brightness
591 R.rgb2hsb = function (r, g, b) {
592 b = prepareRGB(r, g, b);
599 C = V - mmin(r, g, b);
601 V == r ? (g - b) / C :
602 V == g ? (b - r) / C + 2 :
605 S = C == 0 ? 0 : C / V;
606 return {h: H, s: S, b: V, toString: hsbtoString};
612 * Converts RGB values to HSL object.
617 = (object) HSL object in format:
620 o s (number) saturation
621 o l (number) luminosity
624 R.rgb2hsl = function (r, g, b) {
625 b = prepareRGB(r, g, b);
630 var H, S, L, M, m, C;
635 M == r ? (g - b) / C :
636 M == g ? (b - r) / C + 2 :
641 L < .5 ? C / (2 * L) :
643 return {h: H, s: S, l: L, toString: hsltoString};
645 R._path2string = function () {
646 return this.join(",").replace(p2s, "$1");
648 function cacher(f, scope, postprocessor) {
650 var arg = Array.prototype.slice.call(arguments, 0),
651 args = arg.join("\u2400"),
652 cache = newf.cache = newf.cache || {},
653 count = newf.count = newf.count || [];
654 if (cache[has](args)) {
655 return postprocessor ? postprocessor(cache[args]) : cache[args];
657 count.length >= 1e3 && delete cache[count.shift()];
659 cache[args] = f[apply](scope, arg);
660 return postprocessor ? postprocessor(cache[args]) : cache[args];
665 function preload(src, f) {
666 var img = g.doc.createElement("img");
667 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
668 img.onload = function () {
671 g.doc.body.removeChild(this);
673 img.onerror = function () {
674 g.doc.body.removeChild(this);
676 g.doc.body.appendChild(img);
684 * Parses colour string as RGB object
686 - colour (string) colour string in one of formats:
688 <li>Colour name (“<samp>red</samp>”, “<samp>green</samp>”, “<samp>cornflowerblue</samp>”, etc)</li>
689 <li>#••• — shortened HTML colour: (“<samp>#000</samp>”, “<samp>#fc0</samp>”, etc)</li>
690 <li>#•••••• — full length HTML colour: (“<samp>#000000</samp>”, “<samp>#bd2300</samp>”)</li>
691 <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<samp>rgb(200, 100, 0)</samp>”)</li>
692 <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<samp>rgb(100%, 175%, 0%)</samp>”)</li>
693 <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<samp>hsb(0.5, 0.25, 1)</samp>”)</li>
694 <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
695 <li>hsl(•••, •••, •••) — same as hsb</li>
696 <li>hsl(•••%, •••%, •••%) — same as hsb</li>
698 = (object) RGB object in format:
703 o hex (string) color in HTML/CSS format: #••••••,
704 o error (boolean) true if string can’t be parsed
707 R.getRGB = cacher(function (colour) {
708 if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
709 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
711 if (colour == "none") {
712 return {r: -1, g: -1, b: -1, hex: "none"};
714 !(hsrg[has](colour.toLowerCase().substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
722 rgb = colour.match(colourRegExp);
725 blue = toInt(rgb[2].substring(5), 16);
726 green = toInt(rgb[2].substring(3, 5), 16);
727 red = toInt(rgb[2].substring(1, 3), 16);
730 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
731 green = toInt((t = rgb[3].charAt(2)) + t, 16);
732 red = toInt((t = rgb[3].charAt(1)) + t, 16);
735 values = rgb[4].split(commaSpaces);
736 red = toFloat(values[0]);
737 values[0].slice(-1) == "%" && (red *= 2.55);
738 green = toFloat(values[1]);
739 values[1].slice(-1) == "%" && (green *= 2.55);
740 blue = toFloat(values[2]);
741 values[2].slice(-1) == "%" && (blue *= 2.55);
742 rgb[1].toLowerCase().slice(0, 4) == "rgba" && (opacity = toFloat(values[3]));
743 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
746 values = rgb[5].split(commaSpaces);
747 red = toFloat(values[0]);
748 values[0].slice(-1) == "%" && (red *= 2.55);
749 green = toFloat(values[1]);
750 values[1].slice(-1) == "%" && (green *= 2.55);
751 blue = toFloat(values[2]);
752 values[2].slice(-1) == "%" && (blue *= 2.55);
753 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
754 rgb[1].toLowerCase().slice(0, 4) == "hsba" && (opacity = toFloat(values[3]));
755 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
756 return R.hsb2rgb(red, green, blue, opacity);
759 values = rgb[6].split(commaSpaces);
760 red = toFloat(values[0]);
761 values[0].slice(-1) == "%" && (red *= 2.55);
762 green = toFloat(values[1]);
763 values[1].slice(-1) == "%" && (green *= 2.55);
764 blue = toFloat(values[2]);
765 values[2].slice(-1) == "%" && (blue *= 2.55);
766 (values[0].slice(-3) == "deg" || values[0].slice(-1) == "\xb0") && (red /= 360);
767 rgb[1].toLowerCase().slice(0, 4) == "hsla" && (opacity = toFloat(values[3]));
768 values[3] && values[3].slice(-1) == "%" && (opacity /= 100);
769 return R.hsl2rgb(red, green, blue, opacity);
771 rgb = {r: red, g: green, b: blue};
772 rgb.hex = "#" + (16777216 | blue | (green << 8) | (red << 16)).toString(16).slice(1);
773 R.is(opacity, "finite") && (rgb.opacity = opacity);
776 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
782 * Converts HSB values to hex representation of the colour.
785 - s (number) saturation
786 - b (number) value or brightness
787 = (string) hex representation of the colour.
789 R.hsb = cacher(function (h, s, b) {
790 return R.hsb2rgb(h, s, b).hex;
796 * Converts HSL values to hex representation of the colour.
799 - s (number) saturation
800 - l (number) luminosity
801 = (string) hex representation of the colour.
803 R.hsl = cacher(function (h, s, l) {
804 return R.hsl2rgb(h, s, l).hex;
810 * Converts RGB values to hex representation of the colour.
815 = (string) hex representation of the colour.
817 R.rgb = cacher(function (r, g, b) {
818 return "#" + (16777216 | b | (g << 8) | (r << 16)).toString(16).slice(1);
824 * On each call returns next colour in the spectrum. To reset it back to red call @Raphael.getColor.reset
826 - value (number) #optional brightness, default is `0.75`
827 = (string) hex representation of the colour.
829 R.getColor = function (value) {
830 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
831 rgb = this.hsb2rgb(start.h, start.s, start.b);
836 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
841 * Raphael.getColor.reset
844 * Resets spectrum position for @Raphael.getColor back to red.
846 R.getColor.reset = function () {
851 * Raphael.parsePathString
856 * Parses given path string into an array of arrays of path segments.
858 - pathString (string|array) path string or array of segments (in the last case it will be returned stright away)
859 = (array) array of segments.
861 R.parsePathString = cacher(function (pathString) {
865 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
867 if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
868 data = pathClone(pathString);
871 Str(pathString).replace(pathCommand, function (a, b, c) {
873 name = lowerCase.call(b);
874 c.replace(pathValues, function (a, b) {
875 b && params.push(+b);
877 if (name == "m" && params.length > 2) {
878 data.push([b][concat](params.splice(0, 2)));
880 b = b == "m" ? "l" : "L";
882 while (params.length >= paramCounts[name]) {
883 data.push([b][concat](params.splice(0, paramCounts[name])));
884 if (!paramCounts[name]) {
890 data.toString = R._path2string;
894 * Raphael.parseTransformString
899 * Parses given path string into an array of transformations.
901 - TString (string|array) transform string or array of transformations (in the last case it will be returned stright away)
902 = (array) array of transformations.
904 R.parseTransformString = cacher(function (TString) {
908 var paramCounts = {r: 3, s: 4, t: 2, m: 6},
910 if (R.is(TString, array) && R.is(TString[0], array)) { // rough assumption
911 data = pathClone(TString);
914 Str(TString).replace(tCommand, function (a, b, c) {
916 name = lowerCase.call(b);
917 c.replace(pathValues, function (a, b) {
918 b && params.push(+b);
920 data.push([name][concat](params));
923 data.toString = R._path2string;
927 * Raphael.findDotsAtSegment
932 * Find dot coordinates on the given cubic bezier curve at the given t.
934 - p1x (number) x of the first point of the curve
935 - p1y (number) y of the first point of the curve
936 - c1x (number) x of the first anchor of the curve
937 - c1y (number) y of the first anchor of the curve
938 - c2x (number) x of the second anchor of the curve
939 - c2y (number) y of the second anchor of the curve
940 - p2x (number) x of the second point of the curve
941 - p2y (number) y of the second point of the curve
942 - t (number) position on the curve (0..1)
943 = (object) point information in format:
945 o x: (number) x coordinate of the point
946 o y: (number) y coordinate of the point
948 o x: (number) x coordinate of the left anchor
949 o y: (number) y coordinate of the left anchor
952 o x: (number) x coordinate of the right anchor
953 o y: (number) y coordinate of the right anchor
956 o x: (number) x coordinate of the start of the curve
957 o y: (number) y coordinate of the start of the curve
960 o x: (number) x coordinate of the end of the curve
961 o y: (number) y coordinate of the end of the curve
963 o alpha: (number) angle of the curve derivative at the point
966 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
968 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
969 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
970 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
971 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
972 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
973 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
974 ax = (1 - t) * p1x + t * c1x,
975 ay = (1 - t) * p1y + t * c1y,
976 cx = (1 - t) * c2x + t * p2x,
977 cy = (1 - t) * c2y + t * p2y,
978 alpha = (90 - math.atan2(mx - nx, my - ny) * 180 / PI);
979 (mx > nx || my < ny) && (alpha += 180);
980 return {x: x, y: y, m: {x: mx, y: my}, n: {x: nx, y: ny}, start: {x: ax, y: ay}, end: {x: cx, y: cy}, alpha: alpha};
982 var pathDimensions = cacher(function (path) {
984 return {x: 0, y: 0, width: 0, height: 0};
986 path = path2curve(path);
992 for (var i = 0, ii = path.length; i < ii; i++) {
1000 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
1001 X = X[concat](dim.min.x, dim.max.x);
1002 Y = Y[concat](dim.min.y, dim.max.y);
1007 var xmin = mmin[apply](0, X),
1008 ymin = mmin[apply](0, Y);
1012 width: mmax[apply](0, X) - xmin,
1013 height: mmax[apply](0, Y) - ymin
1016 pathClone = function (pathArray) {
1018 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
1019 pathArray = R.parsePathString(pathArray);
1021 for (var i = 0, ii = pathArray.length; i < ii; i++) {
1023 for (var j = 0, jj = pathArray[i].length; j < jj; j++) {
1024 res[i][j] = pathArray[i][j];
1027 res.toString = R._path2string;
1030 pathToRelative = cacher(function (pathArray) {
1031 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
1032 pathArray = R.parsePathString(pathArray);
1040 if (pathArray[0][0] == "M") {
1041 x = pathArray[0][1];
1042 y = pathArray[0][2];
1046 res.push(["M", x, y]);
1048 for (var i = start, ii = pathArray.length; i < ii; i++) {
1049 var r = res[i] = [],
1051 if (pa[0] != lowerCase.call(pa[0])) {
1052 r[0] = lowerCase.call(pa[0]);
1060 r[6] = +(pa[6] - x).toFixed(3);
1061 r[7] = +(pa[7] - y).toFixed(3);
1064 r[1] = +(pa[1] - y).toFixed(3);
1070 for (var j = 1, jj = pa.length; j < jj; j++) {
1071 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
1080 for (var k = 0, kk = pa.length; k < kk; k++) {
1084 var len = res[i].length;
1085 switch (res[i][0]) {
1091 x += +res[i][len - 1];
1094 y += +res[i][len - 1];
1097 x += +res[i][len - 2];
1098 y += +res[i][len - 1];
1101 res.toString = R._path2string;
1104 pathToAbsolute = cacher(function (pathArray) {
1105 if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
1106 pathArray = R.parsePathString(pathArray);
1114 if (pathArray[0][0] == "M") {
1115 x = +pathArray[0][1];
1116 y = +pathArray[0][2];
1120 res[0] = ["M", x, y];
1122 for (var i = start, ii = pathArray.length; i < ii; i++) {
1123 var r = res[i] = [],
1125 if (pa[0] != upperCase.call(pa[0])) {
1126 r[0] = upperCase.call(pa[0]);
1134 r[6] = +(pa[6] + x);
1135 r[7] = +(pa[7] + y);
1147 for (var j = 1, jj = pa.length; j < jj; j++) {
1148 r[j] = +pa[j] + ((j % 2) ? x : y);
1152 for (var k = 0, kk = pa.length; k < kk; k++) {
1168 mx = res[i][res[i].length - 2];
1169 my = res[i][res[i].length - 1];
1171 x = res[i][res[i].length - 2];
1172 y = res[i][res[i].length - 1];
1175 res.toString = R._path2string;
1177 }, null, pathClone),
1178 l2c = function (x1, y1, x2, y2) {
1179 return [x1, y1, x2, y2, x2, y2];
1181 q2c = function (x1, y1, ax, ay, x2, y2) {
1185 _13 * x1 + _23 * ax,
1186 _13 * y1 + _23 * ay,
1187 _13 * x2 + _23 * ax,
1188 _13 * y2 + _23 * ay,
1193 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
1194 // for more information of where this math came from visit:
1195 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
1196 var _120 = PI * 120 / 180,
1197 rad = PI / 180 * (+angle || 0),
1200 rotate = cacher(function (x, y, rad) {
1201 var X = x * math.cos(rad) - y * math.sin(rad),
1202 Y = x * math.sin(rad) + y * math.cos(rad);
1203 return {x: X, y: Y};
1206 xy = rotate(x1, y1, -rad);
1209 xy = rotate(x2, y2, -rad);
1212 var cos = math.cos(PI / 180 * angle),
1213 sin = math.sin(PI / 180 * angle),
1216 var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
1224 k = (large_arc_flag == sweep_flag ? -1 : 1) *
1225 math.sqrt(abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
1226 cx = k * rx * y / ry + (x1 + x2) / 2,
1227 cy = k * -ry * x / rx + (y1 + y2) / 2,
1228 f1 = math.asin(((y1 - cy) / ry).toFixed(9)),
1229 f2 = math.asin(((y2 - cy) / ry).toFixed(9));
1231 f1 = x1 < cx ? PI - f1 : f1;
1232 f2 = x2 < cx ? PI - f2 : f2;
1233 f1 < 0 && (f1 = PI * 2 + f1);
1234 f2 < 0 && (f2 = PI * 2 + f2);
1235 if (sweep_flag && f1 > f2) {
1238 if (!sweep_flag && f2 > f1) {
1248 if (abs(df) > _120) {
1252 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
1253 x2 = cx + rx * math.cos(f2);
1254 y2 = cy + ry * math.sin(f2);
1255 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
1258 var c1 = math.cos(f1),
1262 t = math.tan(df / 4),
1263 hx = 4 / 3 * rx * t,
1264 hy = 4 / 3 * ry * t,
1266 m2 = [x1 + hx * s1, y1 - hy * c1],
1267 m3 = [x2 + hx * s2, y2 - hy * c2],
1269 m2[0] = 2 * m1[0] - m2[0];
1270 m2[1] = 2 * m1[1] - m2[1];
1272 return [m2, m3, m4][concat](res);
1274 res = [m2, m3, m4][concat](res).join().split(",");
1276 for (var i = 0, ii = res.length; i < ii; i++) {
1277 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
1282 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
1285 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
1286 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
1289 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
1290 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
1291 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
1293 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
1294 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
1298 abs(t1) > "1e12" && (t1 = .5);
1299 abs(t2) > "1e12" && (t2 = .5);
1300 if (t1 > 0 && t1 < 1) {
1301 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
1305 if (t2 > 0 && t2 < 1) {
1306 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
1310 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
1311 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
1313 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
1314 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
1315 abs(t1) > "1e12" && (t1 = .5);
1316 abs(t2) > "1e12" && (t2 = .5);
1317 if (t1 > 0 && t1 < 1) {
1318 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
1322 if (t2 > 0 && t2 < 1) {
1323 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
1328 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
1329 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
1332 path2curve = cacher(function (path, path2) {
1333 var p = pathToAbsolute(path),
1334 p2 = path2 && pathToAbsolute(path2),
1335 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
1336 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
1337 processPath = function (path, d) {
1340 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
1342 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
1349 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
1352 nx = d.x + (d.x - (d.bx || d.x));
1353 ny = d.y + (d.y - (d.by || d.y));
1354 path = ["C", nx, ny][concat](path.slice(1));
1357 d.qx = d.x + (d.x - (d.qx || d.x));
1358 d.qy = d.y + (d.y - (d.qy || d.y));
1359 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
1364 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
1367 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
1370 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
1373 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
1376 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
1381 fixArc = function (pp, i) {
1382 if (pp[i].length > 7) {
1386 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
1389 ii = mmax(p.length, p2 && p2.length || 0);
1392 fixM = function (path1, path2, a1, a2, i) {
1393 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
1394 path2.splice(i, 0, ["M", a2.x, a2.y]);
1399 ii = mmax(p.length, p2 && p2.length || 0);
1402 for (var i = 0, ii = mmax(p.length, p2 && p2.length || 0); i < ii; i++) {
1403 p[i] = processPath(p[i], attrs);
1405 p2 && (p2[i] = processPath(p2[i], attrs2));
1406 p2 && fixArc(p2, i);
1407 fixM(p, p2, attrs, attrs2, i);
1408 fixM(p2, p, attrs2, attrs, i);
1411 seglen = seg.length,
1412 seg2len = p2 && seg2.length;
1413 attrs.x = seg[seglen - 2];
1414 attrs.y = seg[seglen - 1];
1415 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
1416 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
1417 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
1418 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
1419 attrs2.x = p2 && seg2[seg2len - 2];
1420 attrs2.y = p2 && seg2[seg2len - 1];
1422 return p2 ? [p, p2] : p;
1423 }, null, pathClone),
1424 parseDots = cacher(function (gradient) {
1426 for (var i = 0, ii = gradient.length; i < ii; i++) {
1428 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
1429 dot.color = R.getRGB(par[1]);
1430 if (dot.color.error) {
1433 dot.color = dot.color.hex;
1434 par[2] && (dot.offset = par[2] + "%");
1437 for (i = 1, ii = dots.length - 1; i < ii; i++) {
1438 if (!dots[i].offset) {
1439 var start = toFloat(dots[i - 1].offset || 0),
1441 for (var j = i + 1; j < ii; j++) {
1442 if (dots[j].offset) {
1443 end = dots[j].offset;
1452 var d = (end - start) / (j - i + 1);
1453 for (; i < j; i++) {
1455 dots[i].offset = start + "%";
1461 getContainer = function (x, y, w, h) {
1463 container = h == null && !R.is(x, "object") ? g.doc.getElementById(x) : x;
1464 if (container == null) {
1467 if (container.tagName) {
1470 container: container,
1471 width: container.style.pixelWidth || container.offsetWidth,
1472 height: container.style.pixelHeight || container.offsetHeight
1475 return {container: container, width: y, height: w};
1478 return {container: 1, x: x, y: y, width: w, height: h};
1480 plugins = function (con, add) {
1482 for (var prop in add) {
1483 if (add[has](prop) && !(prop in con)) {
1484 switch (typeof add[prop]) {
1487 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
1491 con[prop] = con[prop] || {};
1492 plugins.call(this, con[prop], add[prop]);
1495 con[prop] = add[prop];
1501 tear = function (el, paper) {
1502 el == paper.top && (paper.top = el.prev);
1503 el == paper.bottom && (paper.bottom = el.next);
1504 el.next && (el.next.prev = el.prev);
1505 el.prev && (el.prev.next = el.next);
1507 tofront = function (el, paper) {
1508 if (paper.top === el) {
1513 el.prev = paper.top;
1514 paper.top.next = el;
1517 toback = function (el, paper) {
1518 if (paper.bottom === el) {
1522 el.next = paper.bottom;
1524 paper.bottom.prev = el;
1527 insertafter = function (el, el2, paper) {
1529 el2 == paper.top && (paper.top = el);
1530 el2.next && (el2.next.prev = el);
1535 insertbefore = function (el, el2, paper) {
1537 el2 == paper.bottom && (paper.bottom = el);
1538 el2.prev && (el2.prev.next = el);
1543 removed = function (methodname) {
1544 return function () {
1545 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
1548 extractTransform = function (el, tstr) {
1550 return el._.transform;
1552 tstr = Str(tstr).replace(/\.{3}|\u2026/g, el._.transform || E);
1553 var tdata = R.parseTransformString(tstr),
1561 _.transform = tdata || [];
1563 for (var i = 0, ii = tdata.length; i < ii; i++) {
1567 t[0] = Str(t[0]).toLowerCase();
1568 if (t[0] == "t" && tlen == 3) {
1569 m.translate(t[1], t[2]);
1570 } else if (t[0] == "r") {
1572 bb = bb || el.getBBox(1);
1573 m.rotate(t[1], bb.x + bb.width / 2, bb.y + bb.height / 2);
1575 } else if (tlen == 4) {
1576 m.rotate(t[1], t[2], t[3]);
1579 } else if (t[0] == "s") {
1580 if (tlen == 2 || tlen == 3) {
1581 bb = bb || el.getBBox(1);
1582 m.scale(t[1], t[tlen - 1], bb.x + bb.width / 2, bb.y + bb.height / 2);
1585 } else if (tlen == 5) {
1586 m.scale(t[1], t[2], t[3], t[4]);
1590 } else if (t[0] == "m" && tlen == 7) {
1591 m.add(t[1], t[2], t[3], t[4], t[5], t[6]);
1603 _.dx = dx = m.m[0][2];
1604 _.dy = dy = m.m[1][2];
1606 if (sx == 1 && sy == 1 && !deg && _.bbox) {
1613 getEmpty = function (item) {
1615 case "t": return ["t", 0, 0];
1616 case "m": return ["m", 1, 0, 0, 1, 0, 0];
1617 case "r": if (item.length == 4) {
1618 return ["r", 0, item[2], item[3]];
1622 case "s": if (item.length == 5) {
1623 return ["s", 1, 1, item[3], item[4]];
1624 } else if (item.length == 3) {
1631 equaliseTransform = function (t1, t2) {
1632 t1 = R.parseTransformString(t1) || [];
1633 t2 = R.parseTransformString(t2) || [];
1634 var maxlength = mmax(t1.length, t2.length),
1639 for (; i < maxlength; i++) {
1640 tt1 = t1[i] || getEmpty(t2[i]);
1641 tt2 = t2[i] || getEmpty(tt1);
1642 if ( (tt1[0] != tt2[0]) ||
1643 (tt1[0] == "r" && (tt1[2] != tt2[2] || tt1[3] != tt2[3])) ||
1644 (tt1[0] == "s" && (tt1[3] != tt2[3] || tt1[4] != tt2[4]))
1650 for (j = 0, jj = mmax(tt1.length, tt2.length); j < jj; j++) {
1651 j in tt1 && (from[i][j] = tt1[j]);
1652 j in tt2 && (to[i][j] = tt2[j]);
1661 * Raphael.pathToRelative
1666 * Converts path to relative form
1668 - pathString (string|array) path string or array of segments
1669 = (array) array of segments.
1671 R.pathToRelative = pathToRelative;
1673 * Raphael.path2curve
1678 * Converts path to path where all segments are cubic bezier curves.
1680 - pathString (string|array) path string or array of segments
1681 = (array) array of segments.
1683 R.path2curve = path2curve;
1685 // var m = document.createElementNS("http://www.w3.org/2000/svg", "svg").createSVGMatrix();
1686 function Matrix(a, b, c, d, e, f) {
1688 this.m = [[a, c, e], [b, d, f], [0, 0, 1]];
1690 this.m = [[1, 0, 0], [0, 1, 0], [0, 0, 1]];
1693 var matrixproto = Matrix.prototype;
1694 matrixproto.add = function (a, b, c, d, e, f) {
1695 var out = [[], [], []],
1696 matrix = [[a, c, e], [b, d, f], [0, 0, 1]],
1699 for (x = 0; x < 3; x++) {
1700 for (y = 0; y < 3; y++) {
1702 for (z = 0; z < 3; z++) {
1703 res += this.m[x][z] * matrix[z][y];
1710 matrixproto.invert = function () {
1711 var a = this.m[0][0],
1718 return new Matrix(d / x, -b / x, -c / x, a / x, (c * f - d * e) / x, (b * e - a * f) / x);
1720 matrixproto.clone = function () {
1721 var a = this.m[0][0],
1727 return new Matrix(a, b, c, d, e, f);
1729 matrixproto.translate = function (x, y) {
1730 this.add(1, 0, 0, 1, x, y);
1732 matrixproto.scale = function (x, y, cx, cy) {
1733 y == null && (y = x);
1734 this.add(1, 0, 0, 1, cx, cy);
1735 this.add(x, 0, 0, y, 0, 0);
1736 this.add(1, 0, 0, 1, -cx, -cy);
1738 matrixproto.rotate = function (a, x, y) {
1740 var cos = +math.cos(a).toFixed(9),
1741 sin = +math.sin(a).toFixed(9);
1742 this.add(cos, sin, -sin, cos, x, y);
1743 this.add(1, 0, 0, 1, -x, -y);
1745 matrixproto.x = function (x, y) {
1746 return x * this.m[0][0] + y * this.m[0][1] + this.m[0][2];
1748 matrixproto.y = function (x, y) {
1749 return x * this.m[1][0] + y * this.m[1][1] + this.m[1][2];
1751 matrixproto.get = function (i, j) {
1752 return +this.m[i][j].toFixed(4);
1754 matrixproto.toString = function () {
1756 "matrix(" + [this.get(0, 0), this.get(1, 0), this.get(0, 1), this.get(1, 1), this.get(0, 2), this.get(1, 2)].join() + ")" :
1757 [this.get(0, 0), this.get(0, 1), this.get(1, 0), this.get(1, 1), 0, 0].join();
1759 matrixproto.toFilter = function () {
1760 return "progid:DXImageTransform.Microsoft.Matrix(M11=" + this.get(0, 0) +
1761 ", M12=" + this.get(0, 1) + ", M21=" + this.get(1, 0) + ", M22=" + this.get(1, 1) +
1762 ", Dx=" + this.get(0, 2) + ", Dy=" + this.get(1, 2) + ", sizingmedthod='auto expand')";
1764 matrixproto.offset = function () {
1765 return [this.m[0][2].toFixed(4), this.m[1][2].toFixed(4)];
1772 var xlink = "http://www.w3.org/1999/xlink",
1774 block: "M5,0 0,2.5 5,5z",
1775 classic: "M5,0 0,2.5 5,5 3.5,3 3.5,2z",
1776 diamond: "M2.5,0 5,2.5 2.5,5 0,2.5z",
1777 open: "M6,1 1,3.5 6,6",
1778 oval: "M2.5,0A2.5,2.5,0,0,1,2.5,5 2.5,2.5,0,0,1,2.5,0z"
1781 R.toString = function () {
1782 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
1784 var $ = function (el, attr) {
1786 if (typeof el == "string") {
1789 for (var key in attr) if (attr[has](key)) {
1790 if (key.substring(0, 6) == "xlink:") {
1791 el.setAttributeNS(xlink, key.substring(6), Str(attr[key]));
1793 el[setAttribute](key, Str(attr[key]));
1797 el = g.doc.createElementNS("http://www.w3.org/2000/svg", el);
1798 el.style && (el.style.webkitTapHighlightColor = "rgba(0,0,0,0)");
1802 thePath = function (pathString, SVG) {
1804 SVG.canvas && SVG.canvas.appendChild(el);
1805 var p = new Element(el, SVG);
1807 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
1811 rgGrad = /^url\(#(.*)\)$/,
1812 removeGradientFill = function (node, paper) {
1813 var oid = node.getAttribute(fillString);
1814 oid = oid && oid.match(rgGrad);
1815 if (oid && !--gradients[oid[1]]) {
1816 delete gradients[oid[1]];
1817 paper.defs.removeChild(g.doc.getElementById(oid[1]));
1820 addGradientFill = function (element, gradient) {
1821 var type = "linear",
1822 id = element.id + gradient,
1825 SVG = element.paper,
1827 el = g.doc.getElementById(id);
1829 gradient = Str(gradient).replace(radial_gradient, function (all, _fx, _fy) {
1834 var dir = ((fy > .5) * 2 - 1);
1835 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
1836 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
1838 (fy = fy.toFixed(5) - 1e-5 * dir);
1842 gradient = gradient.split(/\s*\-\s*/);
1843 if (type == "linear") {
1844 var angle = gradient.shift();
1845 angle = -toFloat(angle);
1849 var vector = [0, 0, math.cos(R.rad(angle)), math.sin(R.rad(angle))],
1850 max = 1 / (mmax(abs(vector[2]), abs(vector[3])) || 1);
1853 if (vector[2] < 0) {
1854 vector[0] = -vector[2];
1857 if (vector[3] < 0) {
1858 vector[1] = -vector[3];
1862 var dots = parseDots(gradient);
1866 if (element.gradient) {
1867 SVG.defs.removeChild(element.gradient);
1868 delete element.gradient;
1871 el = $(type + "Gradient", {id: id});
1872 element.gradient = el;
1873 $(el, type == "radial" ? {
1881 gradientTransform: element.matrix.invert()
1883 SVG.defs.appendChild(el);
1884 for (var i = 0, ii = dots.length; i < ii; i++) {
1885 el.appendChild($("stop", {
1886 offset: dots[i].offset ? dots[i].offset : i ? "100%" : "0%",
1887 "stop-color": dots[i].color || "#fff"
1892 fill: "url(#" + id + ")",
1901 updatePosition = function (o) {
1902 var bbox = o.getBBox(1);
1903 $(o.pattern, {patternTransform: o.matrix.invert() + " translate(" + bbox.x + "," + bbox.y + ")"});
1905 addArrow = function (o, value, isEnd) {
1906 if (o.type == "path") {
1907 var values = Str(value).toLowerCase().split("-"),
1909 se = isEnd ? "end" : "start",
1912 stroke = attrs["stroke-width"],
1924 switch (values[i]) {
1933 case "wide": h = 5; break;
1934 case "narrow": h = 2; break;
1935 case "long": w = 5; break;
1936 case "short": w = 2; break;
1939 if (type == "open") {
1944 refX = isEnd ? 4 : 1;
1947 stroke: attrs.stroke
1958 o._.arrows.endPath && markerCounter[o._.arrows.endPath]--;
1959 o._.arrows.endMarker && markerCounter[o._.arrows.endMarker]--;
1961 o._.arrows.startPath && markerCounter[o._.arrows.startPath]--;
1962 o._.arrows.startMarker && markerCounter[o._.arrows.startMarker]--;
1967 if (type != "none") {
1968 var pathId = "raphael-marker-" + type,
1969 markerId = "raphael-marker-" + se + type + w + h;
1970 if (!g.doc.getElementById(pathId)) {
1971 p.defs.appendChild($($("path"), {
1972 "stroke-linecap": "round",
1976 markerCounter[pathId] = 1;
1978 markerCounter[pathId]++;
1980 var marker = g.doc.getElementById(markerId),
1983 marker = $($("marker"), {
1992 "xlink:href": "#" + pathId,
1993 transform: (isEnd ? " rotate(180 " + w / 2 + " " + h / 2 + ") " : S) + "scale(" + w / t + "," + h / t + ")",
1994 "stroke-width": 1 / ((w / t + h / t) / 2)
1996 marker.appendChild(use);
1997 p.defs.appendChild(marker);
1998 markerCounter[markerId] = 1;
2000 markerCounter[markerId]++;
2001 use = marker.getElementsByTagName("use")[0];
2004 var delta = dx * (type != "diamond" && type != "oval");
2006 from = o._.arrows.startdx * stroke || 0;
2007 to = R.getTotalLength(attrs.path) - delta * stroke;
2009 from = delta * stroke;
2010 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
2013 attr["marker-" + se] = "url(#" + markerId + ")";
2015 attr.d = Raphael.getSubpath(attrs.path, from, to);
2018 o._.arrows[se + "Path"] = pathId;
2019 o._.arrows[se + "Marker"] = markerId;
2020 o._.arrows[se + "dx"] = delta;
2021 o._.arrows[se + "Type"] = type;
2022 o._.arrows[se + "String"] = value;
2025 from = o._.arrows.startdx * stroke || 0;
2026 to = R.getTotalLength(attrs.path) - from;
2029 to = R.getTotalLength(attrs.path) - (o._.arrows.enddx * stroke || 0);
2031 o._.arrows[se + "Path"] && $(node, {d: Raphael.getSubpath(attrs.path, from, to)});
2032 delete o._.arrows[se + "Path"];
2033 delete o._.arrows[se + "Marker"];
2034 delete o._.arrows[se + "dx"];
2035 delete o._.arrows[se + "Type"];
2036 delete o._.arrows[se + "String"];
2038 for (attr in markerCounter) if (markerCounter[has](attr) && !markerCounter[attr]) {
2039 var item = g.doc.getElementById(attr);
2040 item && item.parentNode.removeChild(item);
2044 setFillAndStroke = function (o, params) {
2051 "-..": [3, 1, 1, 1, 1, 1],
2055 "- .": [4, 3, 1, 3],
2056 "--.": [8, 3, 1, 3],
2057 "--..": [8, 3, 1, 3, 1, 3]
2061 addDashes = function (o, value) {
2062 value = dasharray[lowerCase.call(value)];
2064 var width = o.attrs["stroke-width"] || "1",
2065 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
2069 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
2071 $(node, {"stroke-dasharray": dashes.join(",")});
2074 for (var att in params) {
2075 if (params[has](att)) {
2076 if (!availableAttrs[has](att)) {
2079 var value = params[att];
2088 var pn = node.parentNode;
2089 if (lowerCase.call(pn.tagName) != "a") {
2091 pn.insertBefore(hl, node);
2092 hl.appendChild(node);
2095 if (att == "target" && value == "blank") {
2096 pn.setAttributeNS(xlink, "show", "new");
2098 pn.setAttributeNS(xlink, att, value);
2102 node.style.cursor = value;
2111 addArrow(o, value, 1);
2114 var rect = Str(value).split(separator);
2115 if (rect.length == 4) {
2116 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
2117 var el = $("clipPath"),
2119 el.id = createUUID();
2127 o.paper.defs.appendChild(el);
2128 $(node, {"clip-path": "url(#" + el.id + ")"});
2132 var clip = g.doc.getElementById(node.getAttribute("clip-path").replace(/(^url\(#|\)$)/g, E));
2133 clip && clip.parentNode.removeChild(clip);
2134 $(node, {"clip-path": E});
2139 if (o.type == "path") {
2140 $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
2143 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
2144 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
2149 node[setAttribute](att, value);
2159 value = -attrs.x - (attrs.width || 0);
2162 if (att == "rx" && o.type == "rect") {
2166 node[setAttribute](att, value);
2167 o.pattern && updatePosition(o);
2171 node[setAttribute](att, value);
2181 value = -attrs.y - (attrs.height || 0);
2184 if (att == "ry" && o.type == "rect") {
2188 node[setAttribute](att, value);
2189 o.pattern && updatePosition(o);
2193 if (o.type == "rect") {
2194 $(node, {rx: value, ry: value});
2196 node[setAttribute](att, value);
2201 if (o.type == "image") {
2202 node.setAttributeNS(xlink, "href", value);
2205 case "stroke-width":
2206 if (o._.sx != 1 || o._.sy != 1) {
2207 value /= mmax(abs(o._.sx), abs(o._.sy)) || 1;
2209 if (o.paper._vbSize) {
2210 value *= o.paper._vbSize;
2212 node[setAttribute](att, value);
2213 if (attrs["stroke-dasharray"]) {
2214 addDashes(o, attrs["stroke-dasharray"]);
2217 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
2218 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
2221 case "stroke-dasharray":
2222 addDashes(o, value);
2225 var isURL = Str(value).match(ISURL);
2228 var ig = $("image");
2229 el.id = createUUID();
2230 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
2231 $(ig, {x: 0, y: 0, "xlink:href": isURL[1]});
2235 preload(isURL[1], function () {
2236 var w = this.offsetWidth,
2237 h = this.offsetHeight;
2238 $(el, {width: w, height: h});
2239 $(ig, {width: w, height: h});
2243 o.paper.defs.appendChild(el);
2244 node.style.fill = "url(#" + el.id + ")";
2245 $(node, {fill: "url(#" + el.id + ")"});
2247 o.pattern && updatePosition(o);
2250 var clr = R.getRGB(value);
2252 delete params.gradient;
2253 delete attrs.gradient;
2254 !R.is(attrs.opacity, "undefined") &&
2255 R.is(params.opacity, "undefined") &&
2256 $(node, {opacity: attrs.opacity});
2257 !R.is(attrs["fill-opacity"], "undefined") &&
2258 R.is(params["fill-opacity"], "undefined") &&
2259 $(node, {"fill-opacity": attrs["fill-opacity"]});
2260 } else if ((o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value)) {
2261 if ("opacity" in attrs || "fill-opacity" in attrs) {
2262 var gradient = g.doc.getElementById(node.getAttribute(fillString).replace(/^url\(#|\)$/g, E));
2264 var stops = gradient.getElementsByTagName("stop");
2265 $(stops[stops.length - 1], {"stop-opacity": ("opacity" in attrs ? attrs.opacity : 1) * ("fill-opacity" in attrs ? attrs["fill-opacity"] : 1)});
2268 attrs.gradient = value;
2269 attrs.fill = "none";
2272 clr[has]("opacity") && $(node, {"fill-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
2274 clr = R.getRGB(value);
2275 node[setAttribute](att, clr.hex);
2276 att == "stroke" && clr[has]("opacity") && $(node, {"stroke-opacity": clr.opacity > 1 ? clr.opacity / 100 : clr.opacity});
2277 if (att == "stroke" && o._.arrows) {
2278 "startString" in o._.arrows && addArrow(o, o._.arrows.startString);
2279 "endString" in o._.arrows && addArrow(o, o._.arrows.endString, 1);
2283 (o.type == "circle" || o.type == "ellipse" || Str(value).charAt() != "r") && addGradientFill(o, value);
2286 if (attrs.gradient && !attrs[has]("stroke-opacity")) {
2287 $(node, {"stroke-opacity": value > 1 ? value / 100 : value});
2290 case "fill-opacity":
2291 if (attrs.gradient) {
2292 gradient = g.doc.getElementById(node.getAttribute(fillString).replace(/^url\(#|\)$/g, E));
2294 stops = gradient.getElementsByTagName("stop");
2295 $(stops[stops.length - 1], {"stop-opacity": value});
2300 att == "font-size" && (value = toInt(value, 10) + "px");
2301 var cssrule = att.replace(/(\-.)/g, function (w) {
2302 return upperCase.call(w.substring(1));
2304 node.style[cssrule] = value;
2306 node[setAttribute](att, value);
2312 tuneText(o, params);
2315 tuneText = function (el, params) {
2316 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
2321 fontSize = node.firstChild ? toInt(g.doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
2323 if (params[has]("text")) {
2324 a.text = params.text;
2325 while (node.firstChild) {
2326 node.removeChild(node.firstChild);
2328 var texts = Str(params.text).split("\n"),
2331 for (var i = 0, ii = texts.length; i < ii; i++) if (texts[i]) {
2333 i && $(tspan, {dy: fontSize * leading, x: a.x});
2334 tspan.appendChild(g.doc.createTextNode(texts[i]));
2335 node.appendChild(tspan);
2339 tspans = node.getElementsByTagName("tspan");
2340 for (i = 0, ii = tspans.length; i < ii; i++) {
2341 i && $(tspans[i], {dy: fontSize * leading, x: a.x});
2346 var bb = el._getBBox(),
2347 dif = a.y - (bb.y + bb.height / 2);
2348 dif && R.is(dif, "finite") && $(tspans[0], {dy: a.y + dif});
2350 Element = function (node, svg) {
2355 [ property (object) ]
2357 * Gives you a reference to the DOM object, so you can assign event handlers or just mess around.
2359 | // draw a circle at coordinate 10,10 with radius of 10
2360 | var c = paper.circle(10, 10, 10);
2361 | c.node.onclick = function () {
2362 | c.attr("fill", "red");
2365 this[0] = this.node = node;
2368 [ property (object) ]
2370 * Internal reference to @Raphael object. In case it is not available.
2372 | Raphael.el.red = function () {
2373 | var hsb = this.paper.raphael.rgb2hsb(this.attr("fill"));
2375 | this.attr({fill: this.paper.raphael.hsb2rgb(hsb).hex});
2378 node.raphael = true;
2381 [ property (number) ]
2383 * Unique id of the element. Especially usesful when you want to listen to events of the element,
2384 * because all events are fired in format `<module>.<action>.<id>`. Also useful for @Paper.getById method.
2387 node.raphaelid = this.id;
2388 this.matrix = new Matrix;
2389 this.realPath = null;
2392 [ property (object) ]
2394 * Internal reference to “paper” where object drawn. Mainly for use in plugins and element extensions.
2396 | Raphael.el.cross = function () {
2397 | this.attr({fill: "red"});
2398 | this.paper.path("M10,10L50,50M50,10L10,50")
2399 | .attr({stroke: "red"});
2403 this.attrs = this.attrs || {};
2413 !svg.bottom && (svg.bottom = this);
2416 [ property (object) ]
2418 * Reference to the previous element in the hierarchy.
2420 this.prev = svg.top;
2421 svg.top && (svg.top.next = this);
2425 [ property (object) ]
2427 * Reference to the next element in the hierarchy.
2431 elproto = Element.prototype;
2436 * Adds rotation by given angle around given point to the list of
2437 * transformations of the element.
2439 - deg (number) angle in degrees
2440 - cx (number) #optional x coordinate of the centre of rotation
2441 - cy (number) #optional y coordinate of the centre of rotation
2442 * If cx & cy aren’t specified centre of the shape is used as a point of rotation.
2445 elproto.rotate = function (deg, cx, cy) {
2449 deg = Str(deg).split(separator);
2450 if (deg.length - 1) {
2451 cx = toFloat(deg[1]);
2452 cy = toFloat(deg[2]);
2454 deg = toFloat(deg[0]);
2455 (cy == null) && (cx = cy);
2456 if (cx == null || cy == null) {
2457 var bbox = this.getBBox(1);
2458 cx = bbox.x + bbox.width / 2;
2459 cy = bbox.y + bbox.height / 2;
2461 this.transform(this._.transform.concat([["r", deg, cx, cy]]));
2468 * Adds scale by given amount relative to given point to the list of
2469 * transformations of the element.
2471 - sx (number) horisontal scale amount
2472 - sy (number) vertical scale amount
2473 - cx (number) #optional x coordinate of the centre of scale
2474 - cy (number) #optional y coordinate of the centre of scale
2475 * If cx & cy aren’t specified centre of the shape is used instead.
2478 elproto.scale = function (sx, sy, cx, cy) {
2482 sx = Str(sx).split(separator);
2483 if (sx.length - 1) {
2484 sy = toFloat(sx[1]);
2485 cx = toFloat(sx[2]);
2486 cy = toFloat(sx[3]);
2488 sx = toFloat(sx[0]);
2489 (sy == null) && (sy = sx);
2490 (cy == null) && (cx = cy);
2491 if (cx == null || cy == null) {
2492 var bbox = this.getBBox(1);
2494 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
2495 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
2496 this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
2503 * Adds translation by given amount to the list of transformations of the element.
2505 - dx (number) horisontal shift
2506 - dy (number) vertical shift
2509 elproto.translate = function (dx, dy) {
2513 dx = Str(dx).split(separator);
2514 if (dx.length - 1) {
2515 dy = toFloat(dx[1]);
2517 dx = toFloat(dx[0]) || 0;
2519 this.transform(this._.transform.concat([["t", dx, dy]]));
2526 * Adds transformation to the element which is separate to other attributes,
2527 * i.e. translation doesn’t change `x` or `y` of the rectange. The format
2528 * of transformation string is similar to the path string syntax:
2529 | "t100,100r30,100,100s2,2,100,100r45s1.5"
2530 * Each letter is a command. There are four commands: `t` is for translate, `r` is for rotate, `s` is for
2531 * scale and `m` is for matrix.
2533 * So, example line could be read like “translate by 100, 100, rotate 30° around 100, 100, scale twice around 100, 100
2534 * rotate 45° around centre and scale 1.5 times relative to centre”. As you see rotate and scale commands has origin
2535 * coordinates as a optional parameters.
2536 * Matrix accepts six parameters.
2538 | var el = paper.rect(10, 20, 300, 200);
2539 | // translate 100, 100, rotate 45°, translate -100, 0
2540 | el.transform("t100,100r45t-100,0");
2541 | // if you want you can append or prepend transformations
2542 | el.transform("...t50,50");
2543 | el.transform("s2...");
2545 | el.transform("t50,50...t-50-50");
2546 | // to reset transformation call method with empty string
2548 | // to get current value call it without parameters
2549 | console.log(el.transform());
2551 - tstr (string) #optional transformation string
2552 * If tstr isn’t specified
2553 = (string) current transformation string
2557 elproto.transform = function (tstr) {
2562 extractTransform(this, tstr);
2564 this.clip && $(this.clip, {transform: this.matrix.invert()});
2565 // this.gradient && $(this.gradient, {gradientTransform: this.matrix.invert()});
2566 this.pattern && updatePosition(this);
2567 this.node && $(this.node, {transform: this.matrix});
2569 if (_.sx != 1 || _.sy != 1) {
2570 var sw = this.attrs[has]("stroke-width") ? this.attrs["stroke-width"] : 1;
2571 this.attr({"stroke-width": sw});
2580 * Makes element invisible. See @Element.show.
2583 elproto.hide = function () {
2584 !this.removed && this.paper.safari(this.node.style.display = "none");
2591 * Makes element visible. See @Element.hide.
2594 elproto.show = function () {
2595 !this.removed && this.paper.safari(this.node.style.display = "");
2602 * Removes element form the paper.
2604 elproto.remove = function () {
2608 eve.unbind("*.*." + this.id);
2609 tear(this, this.paper);
2610 this.node.parentNode.removeChild(this.node);
2611 for (var i in this) {
2614 this.removed = true;
2616 elproto._getBBox = function () {
2617 if (this.node.style.display == "none") {
2623 bbox = this.node.getBBox();
2625 // Firefox 3.0.x plays badly here
2629 hide && this.hide();
2636 * Sets the attributes of the element.
2638 - attrName (string) attribute’s name
2639 - value (string) value
2641 - params (object) object of name/value pairs
2643 - attrName (string) attribute’s name
2645 - attrNames (array) in this case method returns array of current values for given attribute names
2646 = (object) @Element if attrsName & value or params are passed in.
2647 = (...) value of the attribute if only attrsName is passed in.
2648 = (array) array of values of the attribute if attrsNames is passed in.
2649 = (object) object of attributes if nothing is passed in.
2650 > Possible parameters
2651 # <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>
2652 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`.
2653 o clip-rect (string) comma or space separated values: x, y, width and height
2654 o cursor (string) CSS type of the cursor
2657 o fill (string) colour, gradient or image
2658 o fill-opacity (number)
2660 o font-family (string)
2661 o font-size (number) font size in pixels
2662 o font-weight (string)
2664 o href (string) URL, if specified element behaves as hyperlink
2666 o path (string) SVG path string format
2670 o src (string) image URL, only works for @Element.image element
2671 o stroke (string) stroke colour
2672 o stroke-dasharray (string) [“”, “`-`”, “`.`”, “`-.`”, “`-..`”, “`. `”, “`- `”, “`--`”, “`- .`”, “`--.`”, “`--..`”]
2673 o stroke-linecap (string) [“`butt`”, “`square`”, “`round`”]
2674 o stroke-linejoin (string) [“`bevel`”, “`round`”, “`miter`”]
2675 o stroke-miterlimit (number)
2676 o stroke-opacity (number)
2677 o stroke-width (number) stroke width in pixels, default is '1'
2678 o target (string) used with href
2679 o text (string) contents of the text element. Use `\n` for multiline text
2680 o text-anchor (string) [“`start`”, “`middle`”, “`end`”], default is “`middle`”
2681 o title (string) will create tooltip with a given text
2682 o transform (string) see @Element.transform
2687 * Linear gradient format: “`‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`90-#fff-#000`” – 90°
2688 * gradient from white to black or “`0-#fff-#f00:20-#000`” – 0° gradient from white via red (at 20%) to black.
2690 * radial gradient: “`r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›`”, example: “`r#fff-#000`” –
2691 * gradient from white to black or “`r(0.25, 0.75)#fff-#000`” – gradient from white to black with focus point
2692 * at 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
2694 # <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>
2697 # <li>Colour name (“<code>red</code>”, “<code>green</code>”, “<code>cornflowerblue</code>”, etc)</li>
2698 # <li>#••• — shortened HTML colour: (“<code>#000</code>”, “<code>#fc0</code>”, etc)</li>
2699 # <li>#•••••• — full length HTML colour: (“<code>#000000</code>”, “<code>#bd2300</code>”)</li>
2700 # <li>rgb(•••, •••, •••) — red, green and blue channels’ values: (“<code>rgb(200, 100, 0)</code>”)</li>
2701 # <li>rgb(•••%, •••%, •••%) — same as above, but in %: (“<code>rgb(100%, 175%, 0%)</code>”)</li>
2702 # <li>rgba(•••, •••, •••, •••) — red, green and blue channels’ values: (“<code>rgba(200, 100, 0, .5)</code>”)</li>
2703 # <li>rgba(•••%, •••%, •••%, •••%) — same as above, but in %: (“<code>rgba(100%, 175%, 0%, 50%)</code>”)</li>
2704 # <li>hsb(•••, •••, •••) — hue, saturation and brightness values: (“<code>hsb(0.5, 0.25, 1)</code>”)</li>
2705 # <li>hsb(•••%, •••%, •••%) — same as above, but in %</li>
2706 # <li>hsba(•••, •••, •••, •••) — same as above, but with opacity</li>
2707 # <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>
2708 # <li>hsl(•••%, •••%, •••%) — same as above, but in %</li>
2709 # <li>hsla(•••, •••, •••) — same as above, but with opacity</li>
2710 # <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>
2713 elproto.attr = function (name, value) {
2719 for (var i in this.attrs) if (this.attrs[has](i)) {
2720 res[i] = this.attrs[i];
2722 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
2723 res.transform = this._.transform;
2726 if (value == null && R.is(name, string)) {
2727 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
2728 return this.attrs.gradient;
2730 if (name == "transform") {
2731 return this._.transform;
2733 if (name in this.attrs) {
2734 return this.attrs[name];
2735 } else if (R.is(this.paper.customAttributes[name], "function")) {
2736 return this.paper.customAttributes[name].def;
2738 return availableAttrs[name];
2741 if (value == null && R.is(name, array)) {
2743 for (var j = 0, jj = name.length; j < jj; j++) {
2744 values[name[j]] = this.attr(name[j]);
2748 if (value != null) {
2750 params[name] = value;
2751 } else if (name != null && R.is(name, "object")) {
2754 for (var key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
2755 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
2756 this.attrs[key] = params[key];
2757 for (var subkey in par) if (par[has](subkey)) {
2758 params[subkey] = par[subkey];
2761 setFillAndStroke(this, params);
2768 * Moves the element so it is the closest to the viewer’s eyes, on top of other elements.
2771 elproto.toFront = function () {
2775 this.node.parentNode.appendChild(this.node);
2776 var svg = this.paper;
2777 svg.top != this && tofront(this, svg);
2784 * Moves the element so it is the furthest from the viewer’s eyes, behind other elements.
2787 elproto.toBack = function () {
2791 if (this.node.parentNode.firstChild != this.node) {
2792 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
2793 toback(this, this.paper);
2794 var svg = this.paper;
2799 * Element.insertAfter
2802 * Inserts current object after the given one.
2805 elproto.insertAfter = function (element) {
2809 var node = element.node || element[element.length - 1].node;
2810 if (node.nextSibling) {
2811 node.parentNode.insertBefore(this.node, node.nextSibling);
2813 node.parentNode.appendChild(this.node);
2815 insertafter(this, element, this.paper);
2819 * Element.insertBefore
2822 * Inserts current object before the given one.
2825 elproto.insertBefore = function (element) {
2829 var node = element.node || element[0].node;
2830 node.parentNode.insertBefore(this.node, node);
2831 insertbefore(this, element, this.paper);
2834 elproto.blur = function (size) {
2835 // Experimental. No Safari support. Use it on your own risk.
2838 var fltr = $("filter"),
2839 blur = $("feGaussianBlur");
2840 t.attrs.blur = size;
2841 fltr.id = createUUID();
2842 $(blur, {stdDeviation: +size || 1.5});
2843 fltr.appendChild(blur);
2844 t.paper.defs.appendChild(fltr);
2846 $(t.node, {filter: "url(#" + fltr.id + ")"});
2849 t._blur.parentNode.removeChild(t._blur);
2851 delete t.attrs.blur;
2853 t.node.removeAttribute("filter");
2856 var theCircle = function (svg, x, y, r) {
2857 var el = $("circle");
2858 svg.canvas && svg.canvas.appendChild(el);
2859 var res = new Element(el, svg);
2860 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
2861 res.type = "circle";
2865 theRect = function (svg, x, y, w, h, r) {
2867 svg.canvas && svg.canvas.appendChild(el);
2868 var res = new Element(el, svg);
2869 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
2874 theEllipse = function (svg, x, y, rx, ry) {
2875 var el = $("ellipse");
2876 svg.canvas && svg.canvas.appendChild(el);
2877 var res = new Element(el, svg);
2878 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
2879 res.type = "ellipse";
2883 theImage = function (svg, src, x, y, w, h) {
2884 var el = $("image");
2885 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
2886 el.setAttributeNS(xlink, "href", src);
2887 svg.canvas && svg.canvas.appendChild(el);
2888 var res = new Element(el, svg);
2889 res.attrs = {x: x, y: y, width: w, height: h, src: src};
2893 theText = function (svg, x, y, text) {
2895 $(el, {x: x, y: y, "text-anchor": "middle"});
2896 svg.canvas && svg.canvas.appendChild(el);
2897 var res = new Element(el, svg);
2898 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
2900 setFillAndStroke(res, res.attrs);
2903 setSize = function (width, height) {
2904 this.width = width || this.width;
2905 this.height = height || this.height;
2906 this.canvas[setAttribute]("width", this.width);
2907 this.canvas[setAttribute]("height", this.height);
2908 if (this._viewBox) {
2909 this.setViewBox.apply(this, this._viewBox);
2913 create = function () {
2914 var con = getContainer[apply](0, arguments),
2915 container = con && con.container,
2919 height = con.height;
2921 throw new Error("SVG container not found.");
2923 var cnvs = $("svg"),
2924 css = "overflow:hidden;";
2927 width = width || 512;
2928 height = height || 342;
2933 xmlns: "http://www.w3.org/2000/svg"
2935 if (container == 1) {
2936 cnvs.style.cssText = css + "position:absolute;left:" + x + "px;top:" + y + "px";
2937 g.doc.body.appendChild(cnvs);
2939 cnvs.style.cssText = css;
2940 if (container.firstChild) {
2941 container.insertBefore(cnvs, container.firstChild);
2943 container.appendChild(cnvs);
2946 container = new Paper;
2947 container.width = width;
2948 container.height = height;
2949 container.canvas = cnvs;
2950 plugins.call(container, container, R.fn);
2954 setViewBox = function (x, y, w, h, fit) {
2955 eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
2956 var size = mmax(w / this.width, h / this.height),
2958 aspectRatio = fit ? "meet" : "xMinYMin",
2965 delete this._vbSize;
2966 vb = "0 0 " + this.width + S + this.height;
2968 this._vbSize = size;
2969 vb = x + S + y + S + w + S + h;
2973 preserveAspectRatio: aspectRatio
2975 while (size && top) {
2976 sw = "stroke-width" in top.attrs ? top.attrs["stroke-width"] : 1;
2977 top.attr({"stroke-width": sw});
2982 this._viewBox = [x, y, w, h, !!fit];
2989 * Clears the paper, i.e. removes all the elements.
2991 paperproto.clear = function () {
2993 var c = this.canvas;
2994 while (c.firstChild) {
2995 c.removeChild(c.firstChild);
2997 this.bottom = this.top = null;
2998 (this.desc = $("desc")).appendChild(g.doc.createTextNode("Created with Rapha\xebl " + R.version));
2999 c.appendChild(this.desc);
3000 c.appendChild(this.defs = $("defs"));
3006 * Removes the paper from the DOM.
3008 paperproto.remove = function () {
3009 eve("remove", this);
3010 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
3011 for (var i in this) {
3012 this[i] = removed(i);
3019 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
3020 bites = /([clmz]),?([^clmz]*)/gi,
3021 blurregexp = / progid:\S+Blur\([^\)]+\)/g,
3022 val = /-?[^,\s-]+/g,
3023 cssDot = "position:absolute;left:0;top:0;width:1px;height:1px",
3025 pathTypes = {path: 1, rect: 1},
3026 ovalTypes = {circle: 1, ellipse: 1},
3027 path2vml = function (path) {
3028 var total = /[ahqstv]/ig,
3029 command = pathToAbsolute;
3030 Str(path).match(total) && (command = path2curve);
3032 if (command == pathToAbsolute && !Str(path).match(total)) {
3033 var res = Str(path).replace(bites, function (all, command, args) {
3035 isMove = lowerCase.call(command) == "m",
3037 args.replace(val, function (value) {
3038 if (isMove && vals.length == 2) {
3039 res += vals + map[command == "m" ? "l" : "L"];
3042 vals.push(round(value * zoom));
3048 var pa = command(path), p, r;
3050 for (var i = 0, ii = pa.length; i < ii; i++) {
3052 r = lowerCase.call(pa[i][0]);
3053 r == "z" && (r = "x");
3054 for (var j = 1, jj = p.length; j < jj; j++) {
3055 r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
3061 compensation = function (deg, dx, dy) {
3063 m.rotate(-deg, .5, .5);
3069 setCoords = function (p) {
3076 fillpos = _.fillpos,
3085 s.visibility = "hidden";
3086 o.coordsize = abs(kx) + S + abs(ky);
3087 s.rotation = deg * (sx * sy < 0 ? -1 : 1);
3089 var c = compensation(deg, dx, dy);
3093 sx < 0 && (flip += "x");
3094 sy < 0 && (flip += " y") && (y = -1);
3096 o.coordorigin = (dx * -kx) + S + (dy * -ky);
3097 if (fillpos || _.fillsize) {
3098 var fill = o.getElementsByTagName(fillString);
3099 fill = fill && fill[0];
3100 o.removeChild(fill);
3102 c = compensation(deg, m.x(fillpos[0], fillpos[1]), m.y(fillpos[0], fillpos[1]));
3103 fill.position = c.dx * y + S + c.dy * y;
3106 fill.size = _.fillsize[0] * abs(sx) + S + _.fillsize[1] * abs(sy);
3108 o.appendChild(fill);
3110 s.visibility = "visible";
3112 R.toString = function () {
3113 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
3115 addArrow = function (o, value, isEnd) {
3116 var values = Str(value).toLowerCase().split("-"),
3117 se = isEnd ? "end" : "start",
3123 switch (values[i]) {
3133 case "narrow": h = values[i]; break;
3135 case "short": w = values[i]; break;
3138 var stroke = o.node.getElementsByTagName("stroke")[0];
3139 stroke[se + "arrow"] = type;
3140 stroke[se + "arrowlength"] = w;
3141 stroke[se + "arrowwidth"] = h;
3143 setFillAndStroke = function (o, params) {
3144 o.paper.canvas.style.display = "none";
3145 o.attrs = o.attrs || {};
3150 newpath = pathTypes[o.type] && (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.cx != a.cx || params.cy != a.cy || params.rx != a.rx || params.ry != a.ry || params.r != a.r),
3151 isOval = ovalTypes[o.type] && (a.cx != params.cx || a.cy != params.cy || a.r != params.r || a.rx != params.rx || a.ry != params.ry),
3155 for (var par in params) if (params[has](par)) {
3156 a[par] = params[par];
3159 a.path = getPath[o.type](o);
3162 params.href && (node.href = params.href);
3163 params.title && (node.title = params.title);
3164 params.target && (node.target = params.target);
3165 params.cursor && (s.cursor = params.cursor);
3166 "blur" in params && o.blur(params.blur);
3167 "transform" in params && o.transform(params.transform);
3168 if (params.path && o.type == "path" || newpath) {
3169 node.path = path2vml(a.path);
3174 rx = a.rx || a.r || 0,
3175 ry = a.ry || a.r || 0;
3176 node.path = R.format("ar{0},{1},{2},{3},{4},{1},{4},{1}x", round((cx - rx) * zoom), round((cy - ry) * zoom), round((cx + rx) * zoom), round((cy + ry) * zoom), round(cx * zoom));
3178 if ("clip-rect" in params) {
3179 var rect = Str(params["clip-rect"]).split(separator);
3180 if (rect.length == 4) {
3181 rect[2] = +rect[2] + (+rect[0]);
3182 rect[3] = +rect[3] + (+rect[1]);
3183 var div = node.clipRect || g.doc.createElement("div"),
3185 group = node.parentNode;
3186 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
3187 if (!node.clipRect) {
3188 dstyle.position = "absolute";
3191 dstyle.width = o.paper.width + "px";
3192 dstyle.height = o.paper.height + "px";
3193 group.parentNode.insertBefore(div, group);
3194 div.appendChild(group);
3195 node.clipRect = div;
3198 if (!params["clip-rect"]) {
3199 node.clipRect && (node.clipRect.style.clip = E);
3203 var textpathStyle = o.textpath.style;
3204 params.font && (textpathStyle.font = params.font);
3205 params["font-family"] && (textpathStyle.fontFamily = '"' + params["font-family"].split(",")[0].replace(/^['"]+|['"]+$/g, E) + '"');
3206 params["font-size"] && (textpathStyle.fontSize = params["font-size"]);
3207 params["font-weight"] && (textpathStyle.fontWeight = params["font-weight"]);
3208 params["font-style"] && (textpathStyle.fontStyle = params["font-style"]);
3210 if ("arrow-start" in params) {
3211 addArrow(res, params["arrow-start"]);
3213 if ("arrow-end" in params) {
3214 addArrow(res, params["arrow-end"], 1);
3216 if (params.opacity != null ||
3217 params["stroke-width"] != null ||
3218 params.fill != null ||
3219 params.src != null ||
3220 params.stroke != null ||
3221 params["stroke-width"] != null ||
3222 params["stroke-opacity"] != null ||
3223 params["fill-opacity"] != null ||
3224 params["stroke-dasharray"] != null ||
3225 params["stroke-miterlimit"] != null ||
3226 params["stroke-linejoin"] != null ||
3227 params["stroke-linecap"] != null) {
3228 var fill = node.getElementsByTagName(fillString),
3230 fill = fill && fill[0];
3231 !fill && (newfill = fill = createNode(fillString));
3232 if (o.type == "image" && params.src) {
3233 fill.src = params.src;
3235 if ("fill-opacity" in params || "opacity" in params) {
3236 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
3237 opacity = mmin(mmax(opacity, 0), 1);
3238 fill.opacity = opacity;
3240 params.fill && (fill.on = true);
3241 if (fill.on == null || params.fill == "none" || params.fill === null) {
3244 if (fill.on && params.fill) {
3245 var isURL = params.fill.match(ISURL);
3247 fill.parentNode == node && node.removeChild(fill);
3249 fill.src = isURL[1];
3251 var bbox = o.getBBox(1);
3252 fill.position = bbox.x + S + bbox.y;
3253 o._.fillpos = [bbox.x, bbox.y];
3255 preload(isURL[1], function () {
3256 o._.fillsize = [this.offsetWidth, this.offsetHeight];
3259 fill.color = R.getRGB(params.fill).hex;
3261 fill.type = "solid";
3262 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill, fill)) {
3264 a.gradient = params.fill;
3265 fill.rotate = false;
3269 node.appendChild(fill);
3270 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
3272 !stroke && (newstroke = stroke = createNode("stroke"));
3273 if ((params.stroke && params.stroke != "none") ||
3274 params["stroke-width"] ||
3275 params["stroke-opacity"] != null ||
3276 params["stroke-dasharray"] ||
3277 params["stroke-miterlimit"] ||
3278 params["stroke-linejoin"] ||
3279 params["stroke-linecap"]) {
3282 (params.stroke == "none" || params.stroke === null || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
3283 var strokeColor = R.getRGB(params.stroke);
3284 stroke.on && params.stroke && (stroke.color = strokeColor.hex);
3285 opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
3286 var width = (toFloat(params["stroke-width"]) || 1) * .75;
3287 opacity = mmin(mmax(opacity, 0), 1);
3288 params["stroke-width"] == null && (width = a["stroke-width"]);
3289 params["stroke-width"] && (stroke.weight = width);
3290 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
3291 stroke.opacity = opacity;
3293 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
3294 stroke.miterlimit = params["stroke-miterlimit"] || 8;
3295 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
3296 if (params["stroke-dasharray"]) {
3300 "-.": "shortdashdot",
3301 "-..": "shortdashdotdot",
3306 "--.": "longdashdot",
3307 "--..": "longdashdotdot"
3309 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
3311 newstroke && node.appendChild(stroke);
3313 if (res.type == "text") {
3314 res.paper.canvas.style.display = E;
3315 var span = res.paper.span,
3317 fontSize = a.font && a.font.match(/\d+(?:\.\d*)?(?=px)/);
3319 a.font && (s.font = a.font);
3320 a["font-family"] && (s.fontFamily = a["font-family"]);
3321 a["font-weight"] && (s.fontWeight = a["font-weight"]);
3322 a["font-style"] && (s.fontStyle = a["font-style"]);
3323 fontSize = toFloat(fontSize ? fontSize[0] : a["font-size"]);
3324 s.fontSize = fontSize * m + "px";
3325 res.textpath.string && (span.innerHTML = Str(res.textpath.string).replace(/</g, "<").replace(/&/g, "&").replace(/\n/g, "<br>"));
3326 var brect = span.getBoundingClientRect();
3327 res.W = a.w = (brect.right - brect.left) / m;
3328 res.H = a.h = (brect.bottom - brect.top) / m;
3329 res.paper.canvas.style.display = "none";
3331 res.Y = a.y + res.H / 2;
3333 ("x" in params || "y" in params) && (res.path.v = R.format("m{0},{1}l{2},{1}", round(a.x * zoom), round(a.y * zoom), round(a.x * zoom) + 1));
3334 var dirtyattrs = ["x", "y", "text", "font", "font-family", "font-weight", "font-style", "font-size"];
3335 for (var d = 0, dd = dirtyattrs.length; d < dd; d++) if (dirtyattrs[d] in params) {
3340 // text-anchor emulation
3341 switch (a["text-anchor"]) {
3343 res.textpath.style["v-text-align"] = "left";
3344 res.bbx = res.W / 2;
3347 res.textpath.style["v-text-align"] = "right";
3348 res.bbx = -res.W / 2;
3351 res.textpath.style["v-text-align"] = "center";
3355 res.textpath.style["v-text-kern"] = true;
3357 res.paper.canvas.style.display = E;
3359 addGradientFill = function (o, gradient, fill) {
3360 o.attrs = o.attrs || {};
3361 var attrs = o.attrs,
3364 o.attrs.gradient = gradient;
3365 gradient = Str(gradient).replace(radial_gradient, function (all, fx, fy) {
3370 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
3375 gradient = gradient.split(/\s*\-\s*/);
3376 if (type == "linear") {
3377 var angle = gradient.shift();
3378 angle = -toFloat(angle);
3383 var dots = parseDots(gradient);
3387 o = o.shape || o.node;
3389 o.removeChild(fill);
3391 fill.method = "none";
3392 fill.color = dots[0].color;
3393 fill.color2 = dots[dots.length - 1].color;
3395 for (var i = 0, ii = dots.length; i < ii; i++) {
3396 dots[i].offset && clrs.push(dots[i].offset + S + dots[i].color);
3398 fill.colors && (fill.colors.value = clrs.length ? clrs.join() : "0% " + fill.color);
3399 if (type == "radial") {
3400 fill.type = "gradientTitle";
3401 fill.focus = "100%";
3402 fill.focussize = "0 0";
3403 fill.focusposition = fxfy;
3406 // fill.rotate= true;
3407 fill.type = "gradient";
3408 fill.angle = (270 - angle) % 360;
3410 o.appendChild(fill);
3411 // alert(fill.outerHTML);
3415 Element = function (node, vml) {
3416 this[0] = this.node = node;
3417 node.raphael = true;
3419 node.raphaelid = this.id;
3424 this.matrix = new Matrix;
3435 !vml.bottom && (vml.bottom = this);
3436 this.prev = vml.top;
3437 vml.top && (vml.top.next = this);
3441 elproto = Element.prototype;
3442 elproto.transform = function (tstr) {
3444 return this._.transform;
3446 extractTransform(this, tstr);
3447 var matrix = this.matrix.clone(),
3449 matrix.translate(-.5, -.5);
3450 if (this.type == "image") {
3451 if (Str(tstr).indexOf("m") + 1) {
3452 this.node.style.filter = matrix.toFilter();
3453 var bb = this.getBBox(),
3454 bbt = this.getBBox(1),
3455 im = matrix.invert(),
3456 dx = im.x(bb.x, bb.y) - im.x(bbt.x, bbt.y),
3457 dy = im.y(bb.x, bb.y) - im.y(bbt.x, bbt.y);
3458 // skew.offset = dx + S + dy;
3459 // this.node.getElementsByTagName(fillString)[0].position = skew.offset;
3461 this.node.style.filter = E;
3467 // fillpos = _.fillpos,
3469 // matrix = this.matrix;
3470 // fill = o.getElementsByTagName(fillString)[0],
3471 // angle = fill.angle;
3473 this.node.style.filter = E;
3474 skew.matrix = matrix;
3475 skew.offset = matrix.offset();
3478 // angle = R.rad(270 - angle);
3479 // var dx = 100 * math.cos(angle),
3480 // dy = 100 * math.sin(angle),
3481 // zx = matrix.x(0, 0),
3482 // zy = matrix.y(0, 0),
3483 // mx = matrix.x(dx, dy),
3484 // my = matrix.y(dx, dy);
3485 // angle = R.angle(zx, zy, mx, my);
3486 // fill.angle = (270 - angle) % 360;
3491 elproto.rotate = function (deg, cx, cy) {
3498 deg = Str(deg).split(separator);
3499 if (deg.length - 1) {
3500 cx = toFloat(deg[1]);
3501 cy = toFloat(deg[2]);
3503 deg = toFloat(deg[0]);
3504 (cy == null) && (cx = cy);
3505 if (cx == null || cy == null) {
3506 var bbox = this.getBBox(1);
3507 cx = bbox.x + bbox.width / 2;
3508 cy = bbox.y + bbox.height / 2;
3511 this.transform(this._.transform.concat([["r", deg, cx, cy]]));
3514 elproto.translate = function (dx, dy) {
3518 dx = Str(dx).split(separator);
3519 if (dx.length - 1) {
3520 dy = toFloat(dx[1]);
3522 dx = toFloat(dx[0]) || 0;
3525 this._.bbox.x += dx;
3526 this._.bbox.y += dy;
3528 this.transform(this._.transform.concat([["t", dx, dy]]));
3531 elproto.scale = function (sx, sy, cx, cy) {
3535 sx = Str(sx).split(separator);
3536 if (sx.length - 1) {
3537 sy = toFloat(sx[1]);
3538 cx = toFloat(sx[2]);
3539 cy = toFloat(sx[3]);
3540 isNaN(cx) && (cx = null);
3541 isNaN(cy) && (cy = null);
3543 sx = toFloat(sx[0]);
3544 (sy == null) && (sy = sx);
3545 (cy == null) && (cx = cy);
3546 if (cx == null || cy == null) {
3547 var bbox = this.getBBox(1);
3549 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
3550 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
3552 this.transform(this._.transform.concat([["s", sx, sy, cx, cy]]));
3556 elproto.hide = function () {
3557 !this.removed && (this.node.style.display = "none");
3560 elproto.show = function () {
3561 !this.removed && (this.node.style.display = E);
3564 elproto._getBBox = function () {
3568 if (this.type == "text") {
3570 x: this.X + (this.bbx || 0) - this.W / 2,
3576 return pathDimensions(this.attrs.path);
3579 elproto.remove = function () {
3583 eve.unbind("*.*." + this.id);
3584 tear(this, this.paper);
3585 this.node.parentNode.removeChild(this.node);
3586 this.shape && this.shape.parentNode.removeChild(this.shape);
3587 for (var i in this) {
3590 this.removed = true;
3592 elproto.attr = function (name, value) {
3598 for (var i in this.attrs) if (this.attrs[has](i)) {
3599 res[i] = this.attrs[i];
3601 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
3604 if (value == null && R.is(name, "string")) {
3605 if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
3606 return this.attrs.gradient;
3608 if (name in this.attrs) {
3609 return this.attrs[name];
3610 } else if (R.is(this.paper.customAttributes[name], "function")) {
3611 return this.paper.customAttributes[name].def;
3613 return availableAttrs[name];
3616 if (this.attrs && value == null && R.is(name, array)) {
3617 var ii, values = {};
3618 for (i = 0, ii = name.length; i < ii; i++) {
3619 values[name[i]] = this.attr(name[i]);
3624 if (value != null) {
3626 params[name] = value;
3628 value == null && R.is(name, "object") && (params = name);
3629 for (var key in params) {
3630 eve("attr." + key + "." + this.id, this, params[key]);
3633 for (key in this.paper.customAttributes) if (this.paper.customAttributes[has](key) && params[has](key) && R.is(this.paper.customAttributes[key], "function")) {
3634 var par = this.paper.customAttributes[key].apply(this, [][concat](params[key]));
3635 this.attrs[key] = params[key];
3636 for (var subkey in par) if (par[has](subkey)) {
3637 params[subkey] = par[subkey];
3640 // this.paper.canvas.style.display = "none";
3641 if (params.text && this.type == "text") {
3642 this.textpath.string = params.text;
3644 setFillAndStroke(this, params);
3645 // this.paper.canvas.style.display = E;
3649 elproto.toFront = function () {
3650 !this.removed && this.node.parentNode.appendChild(this.node);
3651 this.paper.top != this && tofront(this, this.paper);
3654 elproto.toBack = function () {
3658 if (this.node.parentNode.firstChild != this.node) {
3659 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
3660 toback(this, this.paper);
3664 elproto.insertAfter = function (element) {
3668 if (element.constructor == Set) {
3669 element = element[element.length - 1];
3671 if (element.node.nextSibling) {
3672 element.node.parentNode.insertBefore(this.node, element.node.nextSibling);
3674 element.node.parentNode.appendChild(this.node);
3676 insertafter(this, element, this.paper);
3679 elproto.insertBefore = function (element) {
3683 if (element.constructor == Set) {
3684 element = element[0];
3686 element.node.parentNode.insertBefore(this.node, element.node);
3687 insertbefore(this, element, this.paper);
3690 elproto.blur = function (size) {
3691 var s = this.node.runtimeStyle,
3693 f = f.replace(blurregexp, E);
3695 this.attrs.blur = size;
3696 s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
3697 s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
3701 delete this.attrs.blur;
3705 thePath = function (pathString, vml) {
3706 var el = createNode("shape");
3707 el.style.cssText = cssDot;
3708 el.coordsize = zoom + S + zoom;
3709 el.coordorigin = vml.coordorigin;
3710 var p = new Element(el, vml),
3711 attr = {fill: "none", stroke: "#000"};
3712 pathString && (attr.path = pathString);
3716 setFillAndStroke(p, attr);
3717 vml.canvas.appendChild(el);
3718 var skew = createNode("skew");
3720 el.appendChild(skew);
3725 theRect = function (vml, x, y, w, h, r) {
3726 var path = rectPath(x, y, w, h, r),
3727 res = vml.path(path),
3731 res.W = a.width = w;
3732 res.H = a.height = h;
3738 theEllipse = function (vml, x, y, rx, ry) {
3739 var res = vml.path(),
3745 res.type = "ellipse";
3746 setFillAndStroke(res, {
3754 theCircle = function (vml, x, y, r) {
3755 var res = vml.path(),
3759 res.W = res.H = r * 2;
3760 res.type = "circle";
3761 setFillAndStroke(res, {
3768 theImage = function (vml, src, x, y, w, h) {
3769 var path = rectPath(x, y, w, h),
3770 res = vml.path(path).attr({stroke: "none"}),
3773 fill = node.getElementsByTagName(fillString)[0];
3777 res.W = a.width = w;
3778 res.H = a.height = h;
3781 fill.parentNode == node && node.removeChild(fill);
3785 res._.fillpos = [x, y];
3786 res._.fillsize = [w, h];
3787 node.appendChild(fill);
3791 theText = function (vml, x, y, text) {
3792 var el = createNode("shape"),
3793 path = createNode("path"),
3794 o = createNode("textpath");
3798 path.v = R.format("m{0},{1}l{2},{1}", round(x * zoom), round(y * zoom), round(x * zoom) + 1);
3799 path.textpathok = true;
3800 o.string = Str(text);
3802 el.style.cssText = "position:absolute;left:0;top:0;width:1;height:1";
3803 el.coordsize = zoom + S + zoom;
3804 el.coordorigin = "0 0";
3805 var p = new Element(el, vml),
3806 attr = {fill: "#000", stroke: "none", font: availableAttrs.font, text: text};
3811 p.attrs.text = Str(text);
3816 setFillAndStroke(p, attr);
3818 el.appendChild(path);
3819 vml.canvas.appendChild(el);
3820 var skew = createNode("skew");
3822 el.appendChild(skew);
3827 setSize = function (width, height) {
3828 var cs = this.canvas.style;
3830 this.height = height;
3831 width == +width && (width += "px");
3832 height == +height && (height += "px");
3835 cs.clip = "rect(0 " + width + " " + height + " 0)";
3836 if (this._viewBox) {
3837 setViewBox.apply(this, this._viewBox);
3841 setViewBox = function (x, y, w, h, fit) {
3842 eve("setViewBox", this, this._viewBox, [x, y, w, h, fit]);
3843 var width = this.width,
3844 height = this.height,
3845 size = 1e3 * mmax(w / width, h / height),
3850 if (w * H < width) {
3851 x -= (width - w * H) / 2 / H;
3853 if (h * W < height) {
3854 y -= (height - h * W) / 2 / W;
3857 this._viewBox = [x, y, w, h, !!fit];
3858 this.forEach(function (el) {
3859 el.transform("...");
3864 initWin = function (win) {
3865 var doc = win.document;
3866 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
3868 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
3869 createNode = function (tagName) {
3870 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
3873 createNode = function (tagName) {
3874 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
3879 create = function () {
3880 var con = getContainer[apply](0, arguments),
3881 container = con.container,
3882 height = con.height,
3888 throw new Error("VML container not found.");
3890 var res = new Paper,
3891 c = res.canvas = g.doc.createElement("div"),
3895 width = width || 512;
3896 height = height || 342;
3898 res.height = height;
3899 width == +width && (width += "px");
3900 height == +height && (height += "px");
3901 res.coordsize = zoom * 1e3 + S + zoom * 1e3;
3902 res.coordorigin = "0 0";
3903 res.span = g.doc.createElement("span");
3904 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;";
3905 c.appendChild(res.span);
3906 cs.cssText = R.format("top:0;left:0;width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
3907 if (container == 1) {
3908 g.doc.body.appendChild(c);
3911 cs.position = "absolute";
3913 if (container.firstChild) {
3914 container.insertBefore(c, container.firstChild);
3916 container.appendChild(c);
3919 plugins.call(res, res, R.fn);
3922 paperproto.clear = function () {
3924 this.canvas.innerHTML = E;
3925 this.span = g.doc.createElement("span");
3926 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
3927 this.canvas.appendChild(this.span);
3928 this.bottom = this.top = null;
3930 paperproto.remove = function () {
3931 eve("remove", this);
3932 this.canvas.parentNode.removeChild(this.canvas);
3933 for (var i in this) {
3934 this[i] = removed(i);
3940 // WebKit rendering bug workaround method
3941 var version = navigator.userAgent.match(/Version\/(.*?)\s/) || navigator.userAgent.match(/Chrome\/(\d+)/);
3942 if ((navigator.vendor == "Apple Computer, Inc.") && (version && version[1] < 4 || navigator.platform.slice(0, 2) == "iP") ||
3943 (navigator.vendor == "Google Inc." && version && version[1] < 8)) {
3948 * Adds event handlers for hover for the element.
3950 - f_in (function) handler for hover in
3951 - f_out (function) handler for hover out
3952 - icontext (object) #optional context for hover in handler
3953 - ocontext (object) #optional context for hover out handler
3956 paperproto.safari = function () {
3957 var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
3958 setTimeout(function () {rect.remove();});
3961 paperproto.safari = fun;
3965 var preventDefault = function () {
3966 this.returnValue = false;
3968 preventTouch = function () {
3969 return this.originalEvent.preventDefault();
3971 stopPropagation = function () {
3972 this.cancelBubble = true;
3974 stopTouch = function () {
3975 return this.originalEvent.stopPropagation();
3977 addEvent = (function () {
3978 if (g.doc.addEventListener) {
3979 return function (obj, type, fn, element) {
3980 var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
3981 var f = function (e) {
3982 if (supportsTouch && touchMap[has](type)) {
3983 for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
3984 if (e.targetTouches[i].target == obj) {
3986 e = e.targetTouches[i];
3987 e.originalEvent = olde;
3988 e.preventDefault = preventTouch;
3989 e.stopPropagation = stopTouch;
3994 return fn.call(element, e);
3996 obj.addEventListener(realName, f, false);
3997 return function () {
3998 obj.removeEventListener(realName, f, false);
4002 } else if (g.doc.attachEvent) {
4003 return function (obj, type, fn, element) {
4004 var f = function (e) {
4005 e = e || g.win.event;
4006 e.preventDefault = e.preventDefault || preventDefault;
4007 e.stopPropagation = e.stopPropagation || stopPropagation;
4008 return fn.call(element, e);
4010 obj.attachEvent("on" + type, f);
4011 var detacher = function () {
4012 obj.detachEvent("on" + type, f);
4020 dragMove = function (e) {
4023 scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
4024 scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft,
4029 if (supportsTouch) {
4030 var i = e.touches.length,
4033 touch = e.touches[i];
4034 if (touch.identifier == dragi.el._drag.id) {
4037 (e.originalEvent ? e.originalEvent : e).preventDefault();
4044 var node = dragi.el.node,
4046 next = node.nextSibling,
4047 parent = node.parentNode,
4048 display = node.style.display;
4049 g.win.opera && parent.removeChild(node);
4050 node.style.display = "none";
4051 o = dragi.el.paper.getElementByPoint(x, y);
4052 node.style.display = display;
4053 g.win.opera && (next ? parent.insertBefore(node, next) : parent.appendChild(node));
4054 o && eve("drag.over." + dragi.el.id, dragi.el, o);
4057 eve("drag.move." + dragi.el.id, dragi.move_scope || dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y, e);
4060 dragUp = function (e) {
4061 R.unmousemove(dragMove).unmouseup(dragUp);
4062 var i = drag.length,
4066 dragi.el._drag = {};
4067 eve("drag.end." + dragi.el.id, dragi.end_scope || dragi.start_scope || dragi.move_scope || dragi.el, e);
4071 for (var i = events.length; i--;) {
4072 (function (eventName) {
4073 R[eventName] = Element.prototype[eventName] = function (fn, scope) {
4074 if (R.is(fn, "function")) {
4075 this.events = this.events || [];
4076 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || g.doc, eventName, fn, scope || this)});
4080 R["un" + eventName] = Element.prototype["un" + eventName] = function (fn) {
4081 var events = this.events,
4083 while (l--) if (events[l].name == eventName && events[l].f == fn) {
4085 events.splice(l, 1);
4086 !events.length && delete this.events;
4097 * Adds event handlers for hover for the element.
4099 - f_in (function) handler for hover in
4100 - f_out (function) handler for hover out
4101 - icontext (object) #optional context for hover in handler
4102 - ocontext (object) #optional context for hover out handler
4105 elproto.hover = function (f_in, f_out, scope_in, scope_out) {
4106 return this.mouseover(f_in, scope_in).mouseout(f_out, scope_out || scope_in);
4112 * Removes event handlers for hover for the element.
4114 - f_in (function) handler for hover in
4115 - f_out (function) handler for hover out
4118 elproto.unhover = function (f_in, f_out) {
4119 return this.unmouseover(f_in).unmouseout(f_out);
4125 * Adds event handlers for drag of the element.
4127 - onmove (function) handler for moving
4128 - onstart (function) handler for drag start
4129 - onend (function) handler for drag end
4130 - mcontext (object) #optional context for moving handler
4131 - scontext (object) #optional context for drag start handler
4132 - econtext (object) #optional context for drag end handler
4133 * Additionaly following `drag` events will be triggered: `drag.start.<id>` on start,
4134 * `drag.end.<id>` on end and `drag.move.<id>` on every move. When element will be dragged over another element
4135 * `drag.over.<id>` will be fired as well.
4137 * Start event and start handler will be called in specified context or in context of the element with following parameters:
4138 o x (number) x position of the mouse
4139 o y (number) y position of the mouse
4140 o event (object) DOM event object
4141 * Move event and move handler will be called in specified context or in context of the element with following parameters:
4142 o dx (number) shift by x from the start point
4143 o dy (number) shift by y from the start point
4144 o x (number) x position of the mouse
4145 o y (number) y position of the mouse
4146 o event (object) DOM event object
4147 * End event and end handler will be called in specified context or in context of the element with following parameters:
4148 o event (object) DOM event object
4151 elproto.drag = function (onmove, onstart, onend, move_scope, start_scope, end_scope) {
4153 (e.originalEvent || e).preventDefault();
4154 var scrollY = g.doc.documentElement.scrollTop || g.doc.body.scrollTop,
4155 scrollX = g.doc.documentElement.scrollLeft || g.doc.body.scrollLeft;
4156 this._drag.x = e.clientX + scrollX;
4157 this._drag.y = e.clientY + scrollY;
4158 this._drag.id = e.identifier;
4159 !drag.length && R.mousemove(dragMove).mouseup(dragUp);
4160 drag.push({el: this, move_scope: move_scope, start_scope: start_scope, end_scope: end_scope});
4161 onstart && eve.on("drag.start." + this.id, onstart);
4162 onmove && eve.on("drag.move." + this.id, onmove);
4163 onend && eve.on("drag.end." + this.id, onend);
4164 eve("drag.start." + this.id, start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e);
4167 this.mousedown(start);
4171 * Element.onDragOver
4174 * Shortcut for assigning event handler for `drag.over.<id>` event, where id is id of the element (see @Element.id).
4176 - f (function) handler for event
4178 elproto.onDragOver = function (f) {
4179 f ? eve.on("drag.over." + this.id, f) : eve.unbind("drag.over." + this.id);
4185 * Removes all drag event handlers from given element.
4187 elproto.undrag = function () {
4188 var i = drag.length;
4189 while (i--) if (drag[i].el == this) {
4190 R.unmousedown(drag[i].start);
4191 drag.splice(i++, 1);
4192 eve.unbind("drag.*." + this.id);
4194 !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
4204 - x (number) x coordinate of the centre
4205 - y (number) y coordinate of the centre
4207 = (object) Raphaël element object with type “circle”
4210 | var c = paper.circle(50, 50, 40);
4212 paperproto.circle = function (x, y, r) {
4213 return theCircle(this, x || 0, y || 0, r || 0);
4219 * Draws a rectangle.
4223 - x (number) x coordinate of the top left corner
4224 - y (number) y coordinate of the top left corner
4225 - width (number) width
4226 - height (number) height
4227 - r (number) @optional radius for rounded corners, default is 0
4228 = (object) Raphaël element object with type “rect”
4231 | // regular rectangle
4232 | var c = paper.rect(10, 10, 50, 50);
4233 | // rectangle with rounded corners
4234 | var c = paper.rect(40, 40, 50, 50, 10);
4236 paperproto.rect = function (x, y, w, h, r) {
4237 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
4247 - x (number) x coordinate of the centre
4248 - y (number) y coordinate of the centre
4249 - rx (number) horisontal radius
4250 - ry (number) vertical radius
4251 = (object) Raphaël element object with type “ellipse”
4254 | var c = paper.ellipse(50, 50, 40, 20);
4256 paperproto.ellipse = function (x, y, rx, ry) {
4257 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
4263 * Creates a path element by given path data string.
4267 - pathString (string) path data in SVG path string format.
4268 = (object) Raphaël element object with type “ellipse”
4269 # Details of a path's data attribute's format are described in the <a href="http://www.w3.org/TR/SVG/paths.html#PathData">SVG specification</a>.
4272 | var c = paper.path("M10 10L90 90");
4273 | // draw a diagonal line:
4274 | // move to 10,10, line to 90,90
4276 paperproto.path = function (pathString) {
4277 pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
4278 return thePath(R.format[apply](R, arguments), this);
4284 * Embeds an image into the surface.
4288 - src (string) URI of the source image
4289 - x (number) x coordinate position
4290 - y (number) y coordinate position
4291 - width (number) width of the image
4292 - height (number) height of the image
4293 = (object) Raphaël element object with type “image”
4296 | var c = paper.image("apple.png", 10, 10, 80, 80);
4298 paperproto.image = function (src, x, y, w, h) {
4299 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
4305 * Draws a text string. If you need line breaks, put “\n” in the string.
4309 - x (number) x coordinate position
4310 - y (number) y coordinate position
4311 - text (string) The text string to draw
4312 = (object) Raphaël element object with type “text”
4315 | var t = paper.text(50, 50, "Raphaël\nkicks\nbutt!");
4317 paperproto.text = function (x, y, text) {
4318 return theText(this, x || 0, y || 0, Str(text));
4324 * Creates array-like object to keep and operate couple of elements at once.
4325 * Warning: it doesn’t create any elements for itself in the page.
4326 = (object) array-like object that represents set of elements
4329 | var st = paper.set();
4331 | paper.circle(10, 10, 5),
4332 | paper.circle(30, 10, 5)
4334 | st.attr({fill: "red"});
4336 paperproto.set = function (itemsArray) {
4337 arguments.length > 1 && (itemsArray = Array.prototype.splice.call(arguments, 0, arguments.length));
4338 return new Set(itemsArray);
4344 * If you need to change dimensions of the canvas call this method
4348 - width (number) new width of the canvas
4349 - height (number) new height of the canvas
4351 | var st = paper.set();
4353 | paper.circle(10, 10, 5),
4354 | paper.circle(30, 10, 5)
4356 | st.attr({fill: "red"});
4358 paperproto.setSize = setSize;
4363 * Sets the view box of the paper. Practically it gives you ability to zoom and pan whole paper surface by
4364 * specifying new boundaries.
4369 - x (number) new x position, default is `0`
4370 - y (number) new y position, default is `0`
4371 - w (number) new width of the canvas
4372 - h (number) new height of the canvas
4373 - fit (boolean) `true` if you want graphics to fit into new boundary box
4375 paperproto.setViewBox = setViewBox;
4380 * Points to the topmost element on the paper
4386 * Points to the bottom element on the paper
4388 paperproto.top = paperproto.bottom = null;
4393 * Points to the @Raphael object/function
4395 paperproto.raphael = R;
4396 var getOffset = function (elem) {
4397 var box = elem.getBoundingClientRect(),
4398 doc = elem.ownerDocument,
4400 docElem = doc.documentElement,
4401 clientTop = docElem.clientTop || body.clientTop || 0, clientLeft = docElem.clientLeft || body.clientLeft || 0,
4402 top = box.top + (g.win.pageYOffset || docElem.scrollTop || body.scrollTop ) - clientTop,
4403 left = box.left + (g.win.pageXOffset || docElem.scrollLeft || body.scrollLeft) - clientLeft;
4410 * Paper.getElementByPoint
4413 * Returns you topmost element under given point.
4415 = (object) Raphaël element object
4418 - x (number) x coordinate from the top left corner of the window
4419 - y (number) y coordinate from the top left corner of the window
4421 | paper.getElementByPoint(mouseX, mouseY).attr({stroke: "#f00"});
4423 paperproto.getElementByPoint = function (x, y) {
4426 target = g.doc.elementFromPoint(x, y);
4427 if (g.win.opera && target.tagName == "svg") {
4428 var so = getOffset(svg),
4429 sr = svg.createSVGRect();
4432 sr.width = sr.height = 1;
4433 var hits = svg.getIntersectionList(sr, null);
4435 target = hits[hits.length - 1];
4441 while (target.parentNode && target != svg.parentNode && !target.raphael) {
4442 target = target.parentNode;
4444 target == paper.canvas.parentNode && (target = svg);
4445 target = target && target.raphael ? paper.getById(target.raphaelid) : null;
4452 * Returns you element by it’s internal ID.
4457 = (object) Raphaël element object
4459 paperproto.getById = function (id) {
4460 var bot = this.bottom;
4473 * Executes given function for each element on the paper
4475 * If function returns `false` it will stop loop running.
4479 - callback (function) function to run
4480 - thisArg (object) context object for the callback
4481 = (object) Paper object
4483 paperproto.forEach = function (callback, thisArg) {
4484 var bot = this.bottom;
4486 if (callback.call(thisArg, bot) === false) {
4494 return this.x + S + this.y;
4496 function x_y_w_h() {
4497 return this.x + S + this.y + S + this.width + "\xd7" + this.height;
4503 * Return bounding box for a given element
4507 - isWithoutTransform (boolean) flag, `true` if you want to have bounding box before transformations. Default is `false`.
4508 = (object) Bounding box object:
4510 o x: (number) top left corner x
4511 o y: (number) top left corner y
4512 o width: (number) width
4513 o height: (number) height
4516 elproto.getBBox = function (isWithoutTransform) {
4521 if (isWithoutTransform) {
4522 if (_.dirty || !_.bboxwt) {
4523 this.realPath = getPath[this.type](this);
4524 _.bboxwt = pathDimensions(this.realPath);
4525 _.bboxwt.toString = x_y_w_h;
4530 if (_.dirty || _.dirtyT || !_.bbox) {
4531 if (_.dirty || !this.realPath) {
4533 this.realPath = getPath[this.type](this);
4535 _.bbox = pathDimensions(mapPath(this.realPath, this.matrix));
4536 _.bbox.toString = x_y_w_h;
4537 _.dirty = _.dirtyT = 0;
4545 = (object) clone of a given element
4548 elproto.clone = function () {
4552 var attr = this.attr();
4554 delete attr.translation;
4555 return this.paper[this.type]().attr(attr);
4561 * Return set of elements that create glow-like effect around given element. See @Paper.set.
4563 * Note: Glow is not connected to the elment. If you change element attributes it won’t adjust itself.
4565 = (object) @Paper.set of elements that represents glow
4567 elproto.glow = function (glow) {
4568 if (this.type == "text") {
4573 width: glow.width || 10,
4574 fill: glow.fill || false,
4575 opacity: glow.opacity || .5,
4576 offsetx: glow.offsetx || 0,
4577 offsety: glow.offsety || 0,
4578 color: glow.color || "#000"
4583 path = this.realPath || getPath[this.type](this);
4584 path = this.matrix ? mapPath(path, this.matrix) : path;
4585 for (var i = 1; i < c + 1; i++) {
4586 out.push(r.path(path).attr({stroke: s.color, fill: s.fill ? s.color : "none", "stroke-linejoin": "round", "stroke-linecap": "round", "stroke-width": +(s.width / c * i).toFixed(3), opacity: +(s.opacity / c).toFixed(3)}));
4588 return out.insertBefore(this).translate(s.offsetx, s.offsety);
4590 var curveslengths = {},
4591 getPointAtSegmentLength = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
4594 name = [p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y].join(),
4595 cache = curveslengths[name],
4597 !cache && (curveslengths[name] = cache = {data: []});
4598 cache.timer && clearTimeout(cache.timer);
4599 cache.timer = setTimeout(function () {delete curveslengths[name];}, 2e3);
4600 if (length != null && !cache.precision) {
4601 var total = getPointAtSegmentLength(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y);
4602 cache.precision = ~~total * 10;
4605 precision = cache.precision || precision;
4606 for (var i = 0; i < precision + 1; i++) {
4607 if (cache.data[i * precision]) {
4608 dot = cache.data[i * precision];
4610 dot = R.findDotsAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i / precision);
4611 cache.data[i * precision] = dot;
4613 i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
4614 if (length != null && len >= length) {
4619 if (length == null) {
4623 getLengthFactory = function (istotal, subpath) {
4624 return function (path, length, onlystart) {
4625 path = path2curve(path);
4626 var x, y, p, l, sp = "", subpaths = {}, point,
4628 for (var i = 0, ii = path.length; i < ii; i++) {
4634 l = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
4635 if (len + l > length) {
4636 if (subpath && !subpaths.start) {
4637 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
4638 sp += ["C" + point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
4639 if (onlystart) {return sp;}
4640 subpaths.start = sp;
4641 sp = ["M" + point.x, point.y + "C" + point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]].join();
4647 if (!istotal && !subpath) {
4648 point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
4649 return {x: point.x, y: point.y, alpha: point.alpha};
4656 sp += p.shift() + p;
4659 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
4660 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
4664 var getTotalLength = getLengthFactory(1),
4665 getPointAtLength = getLengthFactory(),
4666 getSubpathsAtLength = getLengthFactory(0, 1);
4668 * Raphael.getTotalLength
4671 * Returns length of the given path in pixels.
4675 - path (string) SVG path string.
4679 R.getTotalLength = getTotalLength;
4681 * Raphael.getPointAtLength
4684 * Return coordinates of the point located at the given length on the given path.
4688 - path (string) SVG path string
4691 = (object) representation of the point:
4693 o x: (number) x coordinate
4694 o y: (number) y coordinate
4695 o alpha: (number) angle of derivative
4698 R.getPointAtLength = getPointAtLength;
4700 * Raphael.getSubpath
4703 * Return subpath of a given path from given length to given length.
4707 - path (string) SVG path string
4708 - from (number) position of the start of the segment
4709 - to (number) position of the end of the segment
4711 = (string) pathstring for the segment
4713 R.getSubpath = function (path, from, to) {
4714 if (abs(this.getTotalLength(path) - to) < 1e-6) {
4715 return getSubpathsAtLength(path, from).end;
4717 var a = getSubpathsAtLength(path, to, 1);
4718 return from ? getSubpathsAtLength(a, from).end : a;
4721 * Element.getTotalLength
4724 * Returns length of the path in pixels. Only works for element of “path” type.
4727 elproto.getTotalLength = function () {
4728 if (this.type != "path") {return;}
4729 if (this.node.getTotalLength) {
4730 return this.node.getTotalLength();
4732 return getTotalLength(this.attrs.path);
4735 * Element.getPointAtLength
4738 * Return coordinates of the point located at the given length on the given path. Only works for element of “path” type.
4744 = (object) representation of the point:
4746 o x: (number) x coordinate
4747 o y: (number) y coordinate
4748 o alpha: (number) angle of derivative
4751 elproto.getPointAtLength = function (length) {
4752 if (this.type != "path") {return;}
4753 return getPointAtLength(this.attrs.path, length);
4756 * Element.getSubpath
4759 * Return subpath of a given element from given length to given length. Only works for element of “path” type.
4763 - from (number) position of the start of the segment
4764 - to (number) position of the end of the segment
4766 = (string) pathstring for the segment
4768 elproto.getSubpath = function (from, to) {
4769 if (this.type != "path") {return;}
4770 return R.getSubpath(this.attrs.path, from, to);
4773 * Raphael.easing_formulas
4776 * Object that contains easing formulas for animation. You could extend it with your owns. By default it has following list of easing:
4779 # <li>“<” or “easeIn” or “ease-in”</li>
4780 # <li>“>” or “easeOut” or “ease-out”</li>
4781 # <li>“<>” or “easeInOut” or “ease-in-out”</li>
4782 # <li>“backIn” or “back-in”</li>
4783 # <li>“backOut” or “back-out”</li>
4784 # <li>“elastic”</li>
4787 # <p>See also <a href="http://raphaeljs.com/easing.html">Easing demo</a>.</p>
4789 var ef = R.easing_formulas = {
4790 linear: function (n) {
4799 "<>": function (n) {
4800 var q = .48 - n / 1.04,
4801 Q = math.sqrt(.1734 + q * q),
4803 X = pow(abs(x), 1 / 3) * (x < 0 ? -1 : 1),
4805 Y = pow(abs(y), 1 / 3) * (y < 0 ? -1 : 1),
4807 return (1 - t) * 3 * t * t + t * t * t;
4809 backIn: function (n) {
4811 return n * n * ((s + 1) * n - s);
4813 backOut: function (n) {
4816 return n * n * ((s + 1) * n + s) + 1;
4818 elastic: function (n) {
4822 return pow(2, -10 * n) * math.sin((n - .075) * (2 * PI) / .3) + 1;
4824 bounce: function (n) {
4833 l = s * n * n + .75;
4835 if (n < (2.5 / p)) {
4837 l = s * n * n + .9375;
4840 l = s * n * n + .984375;
4847 ef.easeIn = ef["ease-in"] = ef["<"];
4848 ef.easeOut = ef["ease-out"] = ef[">"];
4849 ef.easeInOut = ef["ease-in-out"] = ef["<>"];
4850 ef["back-in"] = ef.backIn;
4851 ef["back-out"] = ef.backOut;
4853 var animationElements = [],
4854 requestAnimFrame = window.requestAnimationFrame ||
4855 window.webkitRequestAnimationFrame ||
4856 window.mozRequestAnimationFrame ||
4857 window.oRequestAnimationFrame ||
4858 window.msRequestAnimationFrame ||
4859 function (callback) {
4860 setTimeout(callback, 16);
4862 animation = function () {
4863 var Now = +new Date,
4865 for (; l < animationElements.length; l++) {
4866 var e = animationElements[l];
4867 if (e.el.removed || e.paused) {
4870 var time = Now - e.start,
4881 time = (e.initstatus * e.anim.top - e.prev) / (e.percent - e.prev) * ms;
4882 e.status = e.initstatus;
4883 delete e.initstatus;
4884 e.stop && animationElements.splice(l--, 1);
4886 e.status = (e.prev + (e.percent - e.prev) * (time / ms)) / e.anim.top;
4892 var pos = easing(time / ms);
4893 for (var attr in from) if (from[has](attr)) {
4894 switch (availableAnimAttrs[attr]) {
4896 now = +from[attr] + pos * ms * diff[attr];
4900 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
4901 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
4902 upto255(round(from[attr].b + pos * ms * diff[attr].b))
4907 for (var i = 0, ii = from[attr].length; i < ii; i++) {
4908 now[i] = [from[attr][i][0]];
4909 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
4910 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
4912 now[i] = now[i].join(S);
4917 if (diff[attr].real) {
4919 for (i = 0, ii = from[attr].length; i < ii; i++) {
4920 now[i] = [from[attr][i][0]];
4921 for (j = 1, jj = from[attr][i].length; j < jj; j++) {
4922 now[i][j] = from[attr][i][j] + pos * ms * diff[attr][i][j];
4926 var get = function (i) {
4927 return +from[attr][i] + pos * ms * diff[attr][i];
4929 // now = [["r", get(2), 0, 0], ["t", get(3), get(4)], ["s", get(0), get(1), 0, 0]];
4930 now = [["m", get(0), get(1), get(2), get(3), get(4), get(5)]];
4934 if (attr == "clip-rect") {
4938 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
4943 var from2 = [].concat(from[attr]);
4945 i = that.paper.customAttributes[attr].length;
4947 now[i] = +from2[i] + pos * ms * diff[attr][i];
4954 (function (id, that, anim) {
4955 setTimeout(function () {
4956 eve("anim.frame." + id, that, anim);
4958 })(that.id, that, e.anim);
4960 (function(f, el, a) {
4961 setTimeout(function() {
4962 eve("anim.finish." + el.id, el, a);
4963 R.is(f, "function") && f.call(el);
4965 })(e.callback, that, e.anim);
4967 that.attr(e.origin);
4971 animationElements.splice(l--, 1);
4973 if (e.next && !e.stop) {
4974 runAnimation(e.anim, e.el, e.next, null, e.totalOrigin);
4978 R.svg && that && that.paper && that.paper.safari();
4979 animationElements.length && requestAnimFrame(animation);
4981 upto255 = function (color) {
4982 return mmax(mmin(color, 255), 0);
4985 * Element.animateWith
4988 * Acts similar to @Element.animate, but ensure that given animation runs in sync with another given element.
4992 - params (object) final attributes for the element, see also @Element.attr
4993 - ms (number) number of milliseconds for animation to run
4994 - easing (string) #optional easing type. Accept on of @Raphael.easing_formulas or CSS format: `cubic‐bezier(XX, XX, XX, XX)`
4995 - callback (function) #optional callback function. Will be called at the end of animation.
4997 - animation (object) animation object, see @Raphael.animation
4999 = (object) original element
5001 elproto.animateWith = function (element, params, ms, easing, callback) {
5002 for (var i = 0, ii = animationElements.length; i < ii; i++) {
5003 if (animationElements[i].el.id == element.id) {
5004 params.start = animationElements[i].timestamp;
5008 return this.animate(params, ms, easing, callback);
5010 function CubicBezierAtTime(t, p1x, p1y, p2x, p2y, duration) {
5012 bx = 3 * (p2x - p1x) - cx,
5015 by = 3 * (p2y - p1y) - cy,
5017 function sampleCurveX(t) {
5018 return ((ax * t + bx) * t + cx) * t;
5020 function solve(x, epsilon) {
5021 var t = solveCurveX(x, epsilon);
5022 return ((ay * t + by) * t + cy) * t;
5024 function solveCurveX(x, epsilon) {
5025 var t0, t1, t2, x2, d2, i;
5026 for(t2 = x, i = 0; i < 8; i++) {
5027 x2 = sampleCurveX(t2) - x;
5028 if (abs(x2) < epsilon) {
5031 d2 = (3 * ax * t2 + 2 * bx) * t2 + cx;
5032 if (abs(d2) < 1e-6) {
5047 x2 = sampleCurveX(t2);
5048 if (abs(x2 - x) < epsilon) {
5056 t2 = (t1 - t0) / 2 + t0;
5060 return solve(t, 1 / (200 * duration));
5062 elproto.onAnimation = function (f) {
5063 f ? eve.on("anim.frame." + this.id, f) : eve.unbind("anim.frame." + this.id);
5066 function Animation(anim, ms) {
5072 for (var attr in this.anim) if (this.anim[has](attr)) {
5073 percents.push(+attr);
5075 percents.sort(sortByNumber);
5077 this.top = percents[percents.length - 1];
5078 this.percents = percents;
5084 * Creates copy of existing animation object with given delay.
5088 - delay (number) number of ms to pass between animation start and actual animation
5090 = (object) new altered Animation object
5092 Animation.prototype.delay = function (delay) {
5093 var a = new Animation(this.anim, this.ms);
5094 a.times = this.times;
5095 a.del = +delay || 0;
5102 * Creates copy of existing animation object with given repetition.
5106 - repeat (number) number iterations of animation. For infinite animation pass `Infinity`
5108 = (object) new altered Animation object
5110 Animation.prototype.repeat = function (times) {
5111 var a = new Animation(this.anim, this.ms);
5113 a.times = math.floor(mmax(times, 0)) || 1;
5116 function runAnimation(anim, element, percent, status, totalOrigin) {
5117 percent = toFloat(percent);
5130 for (i = 0, ii = animationElements.length; i < ii; i++) {
5131 var e = animationElements[i];
5132 if (e.el.id == element.id && e.anim == anim) {
5133 if (e.percent != percent) {
5134 animationElements.splice(i, 1);
5139 element.attr(e.totalOrigin);
5146 for (var i = 0, ii = anim.percents.length; i < ii; i++) {
5147 if (anim.percents[i] == percent || anim.percents[i] > status * anim.top) {
5148 percent = anim.percents[i];
5149 prev = anim.percents[i - 1] || 0;
5150 ms = ms / anim.top * (percent - prev);
5151 next = anim.percents[i + 1];
5152 params = anim.anim[percent];
5154 } else if (status) {
5155 element.attr(anim.anim[anim.percents[i]]);
5162 for (attr in params) if (params[has](attr)) {
5163 if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) {
5164 from[attr] = element.attr(attr);
5165 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
5166 to[attr] = params[attr];
5167 switch (availableAnimAttrs[attr]) {
5169 diff[attr] = (to[attr] - from[attr]) / ms;
5172 from[attr] = R.getRGB(from[attr]);
5173 var toColour = R.getRGB(to[attr]);
5175 r: (toColour.r - from[attr].r) / ms,
5176 g: (toColour.g - from[attr].g) / ms,
5177 b: (toColour.b - from[attr].b) / ms
5181 var pathes = path2curve(from[attr], to[attr]),
5183 from[attr] = pathes[0];
5185 for (i = 0, ii = from[attr].length; i < ii; i++) {
5186 diff[attr][i] = [0];
5187 for (var j = 1, jj = from[attr][i].length; j < jj; j++) {
5188 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
5194 eq = equaliseTransform(_[attr], to[attr]);
5196 from[attr] = eq.from;
5199 diff[attr].real = true;
5200 for (i = 0, ii = from[attr].length; i < ii; i++) {
5201 diff[attr][i] = [from[attr][i][0]];
5202 for (j = 1, jj = from[attr][i].length; j < jj; j++) {
5203 diff[attr][i][j] = (to[attr][i][j] - from[attr][i][j]) / ms;
5207 var m = (element.matrix || new Matrix).m,
5208 to2 = {_:{transform: _.transform}, getBBox: function () { return element.getBBox(); }};
5217 extractTransform(to2, to[attr]);
5218 to[attr] = to2._.transform;
5220 (to2.matrix.m[0][0] - m[0][0]) / ms,
5221 (to2.matrix.m[1][0] - m[1][0]) / ms,
5222 (to2.matrix.m[0][1] - m[0][1]) / ms,
5223 (to2.matrix.m[1][1] - m[1][1]) / ms,
5224 (to2.matrix.m[0][2] - m[0][2]) / ms,
5225 (to2.matrix.m[1][2] - m[1][2]) / ms
5227 // from[attr] = [_.sx, _.sy, _.deg, _.dx, _.dy];
5228 // var to2 = {_:{}, getBBox: function () { return element.getBBox(); }};
5229 // extractTransform(to2, to[attr]);
5231 // (to2._.sx - _.sx) / ms,
5232 // (to2._.sy - _.sy) / ms,
5233 // (to2._.deg - _.deg) / ms,
5234 // (to2._.dx - _.dx) / ms,
5235 // (to2._.dy - _.dy) / ms
5240 var values = Str(params[attr]).split(separator),
5241 from2 = Str(from[attr]).split(separator);
5242 if (attr == "clip-rect") {
5247 diff[attr][i] = (values[i] - from[attr][i]) / ms;
5253 values = [].concat(params[attr]);
5254 from2 = [].concat(from[attr]);
5256 i = element.paper.customAttributes[attr].length;
5258 diff[attr][i] = ((values[i] || 0) - (from2[i] || 0)) / ms;
5264 var easing = params.easing,
5265 easyeasy = R.easing_formulas[easing];
5267 easyeasy = Str(easing).match(bezierrg);
5268 if (easyeasy && easyeasy.length == 5) {
5269 var curve = easyeasy;
5270 easyeasy = function (t) {
5271 return CubicBezierAtTime(t, +curve[1], +curve[2], +curve[3], +curve[4], ms);
5277 timestamp = params.start || anim.start || +new Date;
5281 timestamp: timestamp,
5282 start: timestamp + (anim.del || 0),
5284 initstatus: status || 0,
5292 callback: params.callback,
5296 origin: element.attr(),
5297 totalOrigin: totalOrigin
5299 animationElements.push(e);
5300 if (status && !isInAnim) {
5302 e.start = new Date - ms * status;
5303 if (animationElements.length == 1) {
5307 animationElements.length == 1 && requestAnimFrame(animation);
5309 isInAnim.initstatus = status;
5310 isInAnim.start = new Date - isInAnim.ms * status;
5312 eve("anim.start." + element.id, element, anim);
5318 * Creates animation object. That later could be used for @Element.animate or @Element.animateWith methods.
5319 * See also @Animation.delay and @Animation.repeat methods.
5323 - params (object) final attributes for the element, see also @Element.attr
5324 - ms (number) number of milliseconds for animation to run
5325 - easing (string) #optional easing type. Accept one of @Raphael.easing_formulas or CSS format: `cubic‐bezier(XX, XX, XX, XX)`
5326 - callback (function) #optional callback function. Will be called at the end of animation.
5328 = (object) @Animation
5330 R.animation = function (params, ms, easing, callback) {
5331 if (R.is(easing, "function") || !easing) {
5332 callback = callback || easing || null;
5335 params = Object(params);
5340 for (attr in params) if (params[has](attr) && toFloat(attr) != attr) {
5342 p[attr] = params[attr];
5345 return new Animation(params, ms);
5347 easing && (p.easing = easing);
5348 callback && (p.callback = callback);
5349 return new Animation({100: p}, ms);
5356 * Creates and starts animation for given element.
5360 - params (object) final attributes for the element, see also @Element.attr
5361 - ms (number) number of milliseconds for animation to run
5362 - easing (string) #optional easing type. Accept on of @Raphael.easing_formulas or CSS format: `cubic‐bezier(XX, XX, XX, XX)`
5363 - callback (function) #optional callback function. Will be called at the end of animation.
5365 - animation (object) animation object, see @Raphael.animation
5367 = (object) original element
5369 elproto.animate = function (params, ms, easing, callback) {
5371 if (element.removed) {
5372 callback && callback.call(element);
5375 var anim = params instanceof Animation ? params : R.animation(params, ms, easing, callback);
5376 runAnimation(anim, element, anim.percents[0], null, element.attr());
5383 * Sets the status of animation of the element in milliseconds. Similar to @Element.status method.
5387 - anim (object) animation object
5388 - value (number) number of milliseconds from the beginning of the animation
5390 = (object) original element if `value` is specified
5391 * Note, that during animation following events are triggered:
5393 * On each animation frame event `anim.frame.<id>`, on start `anim.start.<id>` and on end `anim.finish.<id>`.
5395 elproto.setTime = function (anim, value) {
5396 if (anim && value != null) {
5397 this.status(anim, mmin(value, anim.ms) / anim.ms);
5405 * Gets or sets the status of animation of the element.
5409 - anim (object) #optional animation object
5410 - value (number) #optional 0 – 1. If specified, method works like a setter and sets the status of a given animation to the value. This will cause animation to jump to the given position.
5414 = (array) status if `anim` is not specified. Array of objects in format:
5416 o anim: (object) animation object
5417 o status: (number) status
5420 = (object) original element if `value` is specified
5422 elproto.status = function (anim, value) {
5427 if (value != null) {
5428 runAnimation(anim, this, -1, mmin(value, 1));
5431 len = animationElements.length;
5432 for (; i < len; i++) {
5433 e = animationElements[i];
5434 if (e.el.id == this.id && (!anim || e.anim == anim)) {
5438 out.push({anim: e.anim, status: e.status});
5451 * Stops animation of the element with ability to resume it later on.
5455 - anim (object) #optional animation object
5457 = (object) original element
5459 elproto.pause = function (anim) {
5460 for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
5461 if (eve("anim.pause." + this.id, this, animationElements[i].anim) !== false) {
5462 animationElements[i].paused = true;
5471 * Resumes animation if it was paused with @Element.pause method.
5475 - anim (object) #optional animation object
5477 = (object) original element
5479 elproto.resume = function (anim) {
5480 for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
5481 var e = animationElements[i];
5482 if (eve("anim.resume." + this.id, this, e.anim) !== false) {
5484 this.status(e.anim, e.status);
5493 * Stops animation of the element.
5497 - anim (object) #optional animation object
5499 = (object) original element
5501 elproto.stop = function (anim) {
5502 for (var i = 0; i < animationElements.length; i++) if (animationElements[i].el.id == this.id && (!anim || animationElements[i].anim == anim)) {
5503 if (eve("anim.stop." + this.id, this, animationElements[i].anim) !== false) {
5504 animationElements.splice(i--, 1);
5509 elproto.toString = function () {
5510 return "Rapha\xebl\u2019s object";
5514 var Set = function (items) {
5519 for (var i = 0, ii = items.length; i < ii; i++) {
5520 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
5521 this[this.items.length] = this.items[this.items.length] = items[i];
5527 setproto = Set.prototype;
5532 * Adds each argument to the current set.
5533 = (object) original element
5535 setproto.push = function () {
5538 for (var i = 0, ii = arguments.length; i < ii; i++) {
5539 item = arguments[i];
5540 if (item && (item.constructor == Element || item.constructor == Set)) {
5541 len = this.items.length;
5542 this[len] = this.items[len] = item;
5552 * Removes last element and returns it.
5555 setproto.pop = function () {
5556 this.length && delete this[this.length--];
5557 return this.items.pop();
5563 * Executes given function for each element in the set.
5565 * If function returns `false` it will stop loop running.
5569 - callback (function) function to run
5570 - thisArg (object) context object for the callback
5571 = (object) Set object
5573 setproto.forEach = function (callback, thisArg) {
5574 for (var i = 0, ii = this.items.length; i < ii; i++) {
5575 if (callback.call(thisArg, this.items[i]) === false) {
5581 for (var method in elproto) if (elproto[has](method)) {
5582 setproto[method] = (function (methodname) {
5583 return function () {
5584 var arg = arguments;
5585 return this.forEach(function (el) {
5586 el[methodname][apply](el, arg);
5591 setproto.attr = function (name, value) {
5592 if (name && R.is(name, array) && R.is(name[0], "object")) {
5593 for (var j = 0, jj = name.length; j < jj; j++) {
5594 this.items[j].attr(name[j]);
5597 for (var i = 0, ii = this.items.length; i < ii; i++) {
5598 this.items[i].attr(name, value);
5603 setproto.animate = function (params, ms, easing, callback) {
5604 (R.is(easing, "function") || !easing) && (callback = easing || null);
5605 var len = this.items.length,
5610 callback && (collector = function () {
5611 !--len && callback.call(set);
5613 easing = R.is(easing, string) ? easing : collector;
5614 var anim = params instanceof Animation ? params : R.animation(params, ms, easing, collector);
5615 item = this.items[--i].animate(anim);
5617 this.items[i] && !this.items[i].removed && this.items[i].animateWith(item, anim);
5621 setproto.insertAfter = function (el) {
5622 var i = this.items.length;
5624 this.items[i].insertAfter(el);
5628 setproto.getBBox = function () {
5633 for (var i = this.items.length; i--;) if (!this.items[i].removed) {
5634 var box = this.items[i].getBBox();
5637 w.push(box.x + box.width);
5638 h.push(box.y + box.height);
5640 x = mmin[apply](0, x);
5641 y = mmin[apply](0, y);
5645 width: mmax[apply](0, w) - x,
5646 height: mmax[apply](0, h) - y
5649 setproto.clone = function (s) {
5651 for (var i = 0, ii = this.items.length; i < ii; i++) {
5652 s.push(this.items[i].clone());
5656 setproto.toString = function () {
5657 return "Rapha\xebl\u2018s set";
5660 R.registerFont = function (font) {
5664 this.fonts = this.fonts || {};
5670 family = font.face["font-family"];
5671 for (var prop in font.face) if (font.face[has](prop)) {
5672 fontcopy.face[prop] = font.face[prop];
5674 if (this.fonts[family]) {
5675 this.fonts[family].push(fontcopy);
5677 this.fonts[family] = [fontcopy];
5680 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
5681 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
5682 var path = font.glyphs[glyph];
5683 fontcopy.glyphs[glyph] = {
5686 d: path.d && "M" + path.d.replace(/[mlcxtrv]/g, function (command) {
5687 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
5691 for (var k in path.k) if (path[has](k)) {
5692 fontcopy.glyphs[glyph].k[k] = path.k[k];
5699 paperproto.getFont = function (family, weight, style, stretch) {
5700 stretch = stretch || "normal";
5701 style = style || "normal";
5702 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
5706 var font = R.fonts[family];
5708 var name = new RegExp("(^|\\s)" + family.replace(/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
5709 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
5710 if (name.test(fontName)) {
5711 font = R.fonts[fontName];
5718 for (var i = 0, ii = font.length; i < ii; i++) {
5720 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
5727 paperproto.print = function (x, y, string, font, size, origin, letter_spacing) {
5728 origin = origin || "middle"; // baseline|middle
5729 letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1);
5730 var out = this.set(),
5731 letters = Str(string).split(E),
5735 R.is(font, string) && (font = this.getFont(font));
5737 scale = (size || 16) / font.face["units-per-em"];
5738 var bb = font.face.bbox.split(separator),
5740 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
5741 for (var i = 0, ii = letters.length; i < ii; i++) {
5742 var prev = i && font.glyphs[letters[i - 1]] || {},
5743 curr = font.glyphs[letters[i]];
5744 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) + (font.w * letter_spacing) : 0;
5745 curr && curr.d && out.push(this.path(curr.d).attr({fill: "#000", stroke: "none", transform: [["t", shift, 0]]}));
5747 out.scale(scale, scale, top, height).translate(x - top, y - height);
5752 R.format = function (token, params) {
5753 var args = R.is(params, array) ? [0][concat](params) : arguments;
5754 token && R.is(token, string) && args.length - 1 && (token = token.replace(formatrg, function (str, i) {
5755 return args[++i] == null ? E : args[i];
5759 R.ninja = function () {
5760 oldRaphael.was ? (g.win.Raphael = oldRaphael.is) : delete Raphael;
5765 [ property (object) ]
5767 * You can add your own method to elements. This is usefull when you want to hack default functionality or
5768 * want to wrap some common transformation or attributes in one method. In difference to canvas methods,
5769 * you can redefine element method at any time. Expending element methods wouldn’t affect set
5771 | Raphael.el.red = function () {
5772 | this.attr({fill: "#f00"});
5775 | paper.circle(100, 100, 20).red();
5779 // Firefox <3.6 fix: http://webreflection.blogspot.com/2009/11/195-chars-to-help-lazy-loading.html
5780 (function (doc, loaded, f) {
5781 if (doc.readyState == null && doc.addEventListener){
5782 doc.addEventListener(loaded, f = function () {
5783 doc.removeEventListener(loaded, f, false);
5784 doc.readyState = "complete";
5786 doc.readyState = "loading";
5788 function isLoaded() {
5789 (/in/).test(doc.readyState) ? setTimeout(isLoaded, 9) : eve("DOMload");
5792 })(document, "DOMContentLoaded");
5794 oldRaphael.was ? (g.win.Raphael = R) : (Raphael = R);
5797 * Eve 0.2.1 - JavaScript Events Library
5799 * Copyright (c) 2010 Dmitry Baranovskiy (http://dmitry.baranovskiy.com/)
5800 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
5803 var eve = R.eve = (function () {
5804 var version = "0.2.1",
5805 has = "hasOwnProperty",
5806 separator = /[\.\/]/,
5809 eve = function (name, scope) {
5811 args = Array.prototype.slice.call(arguments, 2),
5812 listeners = eve.listeners(name),
5814 for (var i = 0, ii = listeners.length; i < ii; i++) {
5816 listeners[i].apply(scope, args);
5818 errors.push({error: ex && ex.message || ex, func: listeners[i]});
5821 if (errors.length) {
5825 eve.listeners = function (name) {
5826 var names = name.split(separator),
5838 for (i = 0, ii = names.length; i < ii; i++) {
5840 for (j = 0, jj = es.length; j < jj; j++) {
5842 items = [e[names[i]], e[wildcard]];
5848 out = out.concat(item.f || []);
5856 eve.on = function (name, f) {
5857 var names = name.split(separator),
5859 for (var i = 0, ii = names.length; i < ii; i++) {
5861 !e[names[i]] && (e[names[i]] = {n: {}});
5865 for (i = 0, ii = e.f.length; i < ii; i++) if (e.f[i] == f) {
5870 eve.unbind = function (name, f) {
5871 var names = name.split(separator),
5876 for (var i = 0, ii = names.length; i < ii; i++) {
5877 for (var j = 0; j < cur.length; j += splice.length - 2) {
5880 if (names[i] != wildcard) {
5882 splice.push(e[names[i]]);
5885 for (key in e) if (e[has](key)) {
5886 splice.push(e[key]);
5889 cur.splice.apply(cur, splice);
5892 for (i = 0, ii = cur.length; i < ii; i++) {
5897 for (i = 0, ii = e.f.length; i < ii; i++) if (e.f[i] == f) {
5901 !e.f.length && delete e.f;
5903 for (key in e.n) if (e.n[has](key) && e.n[key].f) {
5904 var funcs = e.n[key].f;
5905 for (i = 0, ii = funcs.length; i < ii; i++) if (funcs[i] == f) {
5909 !funcs.length && delete e.n[key].f;
5913 for (key in e.n) if (e.n[has](key) && e.n[key].f) {
5922 eve.version = version;
5923 eve.toString = function () {
5924 return "You are running Eve " + version;