rlm@83: /*!
rlm@83: * Raphael 1.4.7 - JavaScript Vector Library
rlm@83: *
rlm@83: * Copyright (c) 2010 Dmitry Baranovskiy (http://raphaeljs.com)
rlm@83: * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
rlm@83: */
rlm@83:
rlm@83: Raphael = (function () {
rlm@83: function R() {
rlm@83: if (R.is(arguments[0], array)) {
rlm@83: var a = arguments[0],
rlm@83: cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
rlm@83: res = cnv.set();
rlm@83: for (var i = 0, ii = a[length]; i < ii; i++) {
rlm@83: var j = a[i] || {};
rlm@83: elements.test(j.type) && res[push](cnv[j.type]().attr(j));
rlm@83: }
rlm@83: return res;
rlm@83: }
rlm@83: return create[apply](R, arguments);
rlm@83: }
rlm@83: R.version = "1.4.7";
rlm@83: var separator = /[, ]+/,
rlm@83: elements = /^(circle|rect|path|ellipse|text|image)$/,
rlm@83: proto = "prototype",
rlm@83: has = "hasOwnProperty",
rlm@83: doc = document,
rlm@83: win = window,
rlm@83: oldRaphael = {
rlm@83: was: Object[proto][has].call(win, "Raphael"),
rlm@83: is: win.Raphael
rlm@83: },
rlm@83: Paper = function () {},
rlm@83: appendChild = "appendChild",
rlm@83: apply = "apply",
rlm@83: concat = "concat",
rlm@83: supportsTouch = "createTouch" in doc,
rlm@83: E = "",
rlm@83: S = " ",
rlm@83: Str = String,
rlm@83: split = "split",
rlm@83: events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend orientationchange touchcancel gesturestart gesturechange gestureend"[split](S),
rlm@83: touchMap = {
rlm@83: mousedown: "touchstart",
rlm@83: mousemove: "touchmove",
rlm@83: mouseup: "touchend"
rlm@83: },
rlm@83: join = "join",
rlm@83: length = "length",
rlm@83: lowerCase = String[proto].toLowerCase,
rlm@83: math = Math,
rlm@83: mmax = math.max,
rlm@83: mmin = math.min,
rlm@83: nu = "number",
rlm@83: string = "string",
rlm@83: array = "array",
rlm@83: toString = "toString",
rlm@83: fillString = "fill",
rlm@83: objectToString = Object[proto][toString],
rlm@83: paper = {},
rlm@83: pow = math.pow,
rlm@83: push = "push",
rlm@83: rg = /^(?=[\da-f]$)/,
rlm@83: ISURL = /^url\(['"]?([^\)]+?)['"]?\)$/i,
rlm@83: colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgba?\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+(?:\s*,\s*[\d\.]+)?)\s*\)|rgba?\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%(?:\s*,\s*[\d\.]+%)?)\s*\)|hsb\(\s*([\d\.]+(?:deg|\xb0)?\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsb\(\s*([\d\.]+(?:deg|\xb0|%)\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hsl\(\s*([\d\.]+(?:deg|\xb0)?\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hsl\(\s*([\d\.]+(?:deg|\xb0|%)\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
rlm@83: round = math.round,
rlm@83: setAttribute = "setAttribute",
rlm@83: toFloat = parseFloat,
rlm@83: toInt = parseInt,
rlm@83: ms = " progid:DXImageTransform.Microsoft",
rlm@83: upperCase = String[proto].toUpperCase,
rlm@83: availableAttrs = {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, rotation: 0, rx: 0, ry: 0, scale: "1 1", 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", translation: "0 0", width: 0, x: 0, y: 0},
rlm@83: availableAnimAttrs = {along: "along", 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, rotation: "csv", rx: nu, ry: nu, scale: "csv", stroke: "colour", "stroke-opacity": nu, "stroke-width": nu, translation: "csv", width: nu, x: nu, y: nu},
rlm@83: rp = "replace";
rlm@83: R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
rlm@83: if (R.type == "VML") {
rlm@83: var d = doc.createElement("div"),
rlm@83: b;
rlm@83: d.innerHTML = '';
rlm@83: b = d.firstChild;
rlm@83: b.style.behavior = "url(#default#VML)";
rlm@83: if (!(b && typeof b.adj == "object")) {
rlm@83: return R.type = null;
rlm@83: }
rlm@83: d = null;
rlm@83: }
rlm@83: R.svg = !(R.vml = R.type == "VML");
rlm@83: Paper[proto] = R[proto];
rlm@83: R._id = 0;
rlm@83: R._oid = 0;
rlm@83: R.fn = {};
rlm@83: R.is = function (o, type) {
rlm@83: type = lowerCase.call(type);
rlm@83: return (type == "object" && o === Object(o)) ||
rlm@83: (type == "undefined" && typeof o == type) ||
rlm@83: (type == "null" && o == null) ||
rlm@83: (type == "array" && Array.isArray && Array.isArray(o)) ||
rlm@83: lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
rlm@83: };
rlm@83:
rlm@83: R.setWindow = function (newwin) {
rlm@83: win = newwin;
rlm@83: doc = win.document;
rlm@83: };
rlm@83: // colour utilities
rlm@83: var toHex = function (color) {
rlm@83: if (R.vml) {
rlm@83: // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
rlm@83: var trim = /^\s+|\s+$/g;
rlm@83: toHex = cacher(function (color) {
rlm@83: var bod;
rlm@83: color = Str(color)[rp](trim, E);
rlm@83: try {
rlm@83: var docum = new win.ActiveXObject("htmlfile");
rlm@83: docum.write("
");
rlm@83: docum.close();
rlm@83: bod = docum.body;
rlm@83: } catch(e) {
rlm@83: bod = win.createPopup().document.body;
rlm@83: }
rlm@83: var range = bod.createTextRange();
rlm@83: try {
rlm@83: bod.style.color = color;
rlm@83: var value = range.queryCommandValue("ForeColor");
rlm@83: value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
rlm@83: return "#" + ("000000" + value[toString](16)).slice(-6);
rlm@83: } catch(e) {
rlm@83: return "none";
rlm@83: }
rlm@83: });
rlm@83: } else {
rlm@83: var i = doc.createElement("i");
rlm@83: i.title = "Rapha\xebl Colour Picker";
rlm@83: i.style.display = "none";
rlm@83: doc.body[appendChild](i);
rlm@83: toHex = cacher(function (color) {
rlm@83: i.style.color = color;
rlm@83: return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
rlm@83: });
rlm@83: }
rlm@83: return toHex(color);
rlm@83: },
rlm@83: hsbtoString = function () {
rlm@83: return "hsb(" + [this.h, this.s, this.b] + ")";
rlm@83: },
rlm@83: hsltoString = function () {
rlm@83: return "hsl(" + [this.h, this.s, this.l] + ")";
rlm@83: },
rlm@83: rgbtoString = function () {
rlm@83: return this.hex;
rlm@83: };
rlm@83: R.hsb2rgb = function (h, s, b) {
rlm@83: if (R.is(h, "object") && "h" in h && "s" in h && "b" in h) {
rlm@83: b = h.b;
rlm@83: s = h.s;
rlm@83: h = h.h;
rlm@83: }
rlm@83: return R.hsl2rgb(h, s, b / 2);
rlm@83: };
rlm@83: R.hsl2rgb = function (h, s, l) {
rlm@83: if (R.is(h, "object") && "h" in h && "s" in h && "l" in h) {
rlm@83: l = h.l;
rlm@83: s = h.s;
rlm@83: h = h.h;
rlm@83: }
rlm@83: if (h > 1 || s > 1 || l > 1) {
rlm@83: h /= 255;
rlm@83: s /= 255;
rlm@83: l /= 255;
rlm@83: }
rlm@83: var rgb = {},
rlm@83: channels = ["r", "g", "b"],
rlm@83: t2, t1, t3, r, g, b;
rlm@83: if (!s) {
rlm@83: rgb = {
rlm@83: r: l,
rlm@83: g: l,
rlm@83: b: l
rlm@83: };
rlm@83: } else {
rlm@83: if (l < .5) {
rlm@83: t2 = l * (1 + s);
rlm@83: } else {
rlm@83: t2 = l + s - l * s;
rlm@83: }
rlm@83: t1 = 2 * l - t2;
rlm@83: for (var i = 0, ii = channels.length; i < ii; i++) {
rlm@83: t3 = h + 1 / 3 * -(i - 1);
rlm@83: t3 < 0 && t3++;
rlm@83: t3 > 1 && t3--;
rlm@83: if (t3 * 6 < 1) {
rlm@83: rgb[channels[i]] = t1 + (t2 - t1) * 6 * t3;
rlm@83: } else if (t3 * 2 < 1) {
rlm@83: rgb[channels[i]] = t2;
rlm@83: } else if (t3 * 3 < 2) {
rlm@83: rgb[channels[i]] = t1 + (t2 - t1) * (2 / 3 - t3) * 6;
rlm@83: } else {
rlm@83: rgb[channels[i]] = t1;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: rgb.r *= 255;
rlm@83: rgb.g *= 255;
rlm@83: rgb.b *= 255;
rlm@83: r = (~~rgb.r)[toString](16);
rlm@83: g = (~~rgb.g)[toString](16);
rlm@83: b = (~~rgb.b)[toString](16);
rlm@83: r = r[rp](rg, "0");
rlm@83: g = g[rp](rg, "0");
rlm@83: b = b[rp](rg, "0");
rlm@83: rgb.hex = "#" + r + g + b;
rlm@83: rgb.toString = rgbtoString;
rlm@83: return rgb;
rlm@83: };
rlm@83: R.rgb2hsb = function (red, green, blue) {
rlm@83: if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
rlm@83: blue = red.b;
rlm@83: green = red.g;
rlm@83: red = red.r;
rlm@83: }
rlm@83: if (green == null && R.is(red, string)) {
rlm@83: var clr = R.getRGB(red);
rlm@83: red = clr.r;
rlm@83: green = clr.g;
rlm@83: blue = clr.b;
rlm@83: }
rlm@83: if (red > 1 || green > 1 || blue > 1) {
rlm@83: red /= 255;
rlm@83: green /= 255;
rlm@83: blue /= 255;
rlm@83: }
rlm@83: var max = mmax(red, green, blue),
rlm@83: min = mmin(red, green, blue),
rlm@83: hue,
rlm@83: saturation,
rlm@83: brightness = max;
rlm@83: if (min == max) {
rlm@83: return {h: 0, s: 0, b: max, toString: hsbtoString};
rlm@83: } else {
rlm@83: var delta = (max - min);
rlm@83: saturation = delta / max;
rlm@83: if (red == max) {
rlm@83: hue = (green - blue) / delta;
rlm@83: } else if (green == max) {
rlm@83: hue = 2 + ((blue - red) / delta);
rlm@83: } else {
rlm@83: hue = 4 + ((red - green) / delta);
rlm@83: }
rlm@83: hue /= 6;
rlm@83: hue < 0 && hue++;
rlm@83: hue > 1 && hue--;
rlm@83: }
rlm@83: return {h: hue, s: saturation, b: brightness, toString: hsbtoString};
rlm@83: };
rlm@83: R.rgb2hsl = function (red, green, blue) {
rlm@83: if (green == null && R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
rlm@83: blue = red.b;
rlm@83: green = red.g;
rlm@83: red = red.r;
rlm@83: }
rlm@83: if (green == null && R.is(red, string)) {
rlm@83: var clr = R.getRGB(red);
rlm@83: red = clr.r;
rlm@83: green = clr.g;
rlm@83: blue = clr.b;
rlm@83: }
rlm@83: if (red > 1 || green > 1 || blue > 1) {
rlm@83: red /= 255;
rlm@83: green /= 255;
rlm@83: blue /= 255;
rlm@83: }
rlm@83: var max = mmax(red, green, blue),
rlm@83: min = mmin(red, green, blue),
rlm@83: h,
rlm@83: s,
rlm@83: l = (max + min) / 2,
rlm@83: hsl;
rlm@83: if (min == max) {
rlm@83: hsl = {h: 0, s: 0, l: l};
rlm@83: } else {
rlm@83: var delta = max - min;
rlm@83: s = l < .5 ? delta / (max + min) : delta / (2 - max - min);
rlm@83: if (red == max) {
rlm@83: h = (green - blue) / delta;
rlm@83: } else if (green == max) {
rlm@83: h = 2 + (blue - red) / delta;
rlm@83: } else {
rlm@83: h = 4 + (red - green) / delta;
rlm@83: }
rlm@83: h /= 6;
rlm@83: h < 0 && h++;
rlm@83: h > 1 && h--;
rlm@83: hsl = {h: h, s: s, l: l};
rlm@83: }
rlm@83: hsl.toString = hsltoString;
rlm@83: return hsl;
rlm@83: };
rlm@83: var p2s = /,?([achlmqrstvxz]),?/gi,
rlm@83: commaSpaces = /\s*,\s*/,
rlm@83: hsrg = {hs: 1, rg: 1};
rlm@83: R._path2string = function () {
rlm@83: return this.join(",")[rp](p2s, "$1");
rlm@83: };
rlm@83: function cacher(f, scope, postprocessor) {
rlm@83: function newf() {
rlm@83: var arg = Array[proto].slice.call(arguments, 0),
rlm@83: args = arg[join]("\u25ba"),
rlm@83: cache = newf.cache = newf.cache || {},
rlm@83: count = newf.count = newf.count || [];
rlm@83: if (cache[has](args)) {
rlm@83: return postprocessor ? postprocessor(cache[args]) : cache[args];
rlm@83: }
rlm@83: count[length] >= 1e3 && delete cache[count.shift()];
rlm@83: count[push](args);
rlm@83: cache[args] = f[apply](scope, arg);
rlm@83: return postprocessor ? postprocessor(cache[args]) : cache[args];
rlm@83: }
rlm@83: return newf;
rlm@83: }
rlm@83:
rlm@83: R.getRGB = cacher(function (colour) {
rlm@83: if (!colour || !!((colour = Str(colour)).indexOf("-") + 1)) {
rlm@83: return {r: -1, g: -1, b: -1, hex: "none", error: 1};
rlm@83: }
rlm@83: if (colour == "none") {
rlm@83: return {r: -1, g: -1, b: -1, hex: "none"};
rlm@83: }
rlm@83: !(hsrg[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
rlm@83: var res,
rlm@83: red,
rlm@83: green,
rlm@83: blue,
rlm@83: opacity,
rlm@83: t,
rlm@83: rgb = colour.match(colourRegExp);
rlm@83: if (rgb) {
rlm@83: if (rgb[2]) {
rlm@83: blue = toInt(rgb[2].substring(5), 16);
rlm@83: green = toInt(rgb[2].substring(3, 5), 16);
rlm@83: red = toInt(rgb[2].substring(1, 3), 16);
rlm@83: }
rlm@83: if (rgb[3]) {
rlm@83: blue = toInt((t = rgb[3].charAt(3)) + t, 16);
rlm@83: green = toInt((t = rgb[3].charAt(2)) + t, 16);
rlm@83: red = toInt((t = rgb[3].charAt(1)) + t, 16);
rlm@83: }
rlm@83: if (rgb[4]) {
rlm@83: rgb = rgb[4][split](commaSpaces);
rlm@83: red = toFloat(rgb[0]);
rlm@83: green = toFloat(rgb[1]);
rlm@83: blue = toFloat(rgb[2]);
rlm@83: opacity = toFloat(rgb[3]);
rlm@83: }
rlm@83: if (rgb[5]) {
rlm@83: rgb = rgb[5][split](commaSpaces);
rlm@83: red = toFloat(rgb[0]) * 2.55;
rlm@83: green = toFloat(rgb[1]) * 2.55;
rlm@83: blue = toFloat(rgb[2]) * 2.55;
rlm@83: opacity = toFloat(rgb[3]);
rlm@83: }
rlm@83: if (rgb[6]) {
rlm@83: rgb = rgb[6][split](commaSpaces);
rlm@83: red = toFloat(rgb[0]);
rlm@83: green = toFloat(rgb[1]);
rlm@83: blue = toFloat(rgb[2]);
rlm@83: (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
rlm@83: return R.hsb2rgb(red, green, blue);
rlm@83: }
rlm@83: if (rgb[7]) {
rlm@83: rgb = rgb[7][split](commaSpaces);
rlm@83: red = toFloat(rgb[0]) * 2.55;
rlm@83: green = toFloat(rgb[1]) * 2.55;
rlm@83: blue = toFloat(rgb[2]) * 2.55;
rlm@83: (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
rlm@83: return R.hsb2rgb(red, green, blue);
rlm@83: }
rlm@83: if (rgb[8]) {
rlm@83: rgb = rgb[8][split](commaSpaces);
rlm@83: red = toFloat(rgb[0]);
rlm@83: green = toFloat(rgb[1]);
rlm@83: blue = toFloat(rgb[2]);
rlm@83: (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360);
rlm@83: return R.hsl2rgb(red, green, blue);
rlm@83: }
rlm@83: if (rgb[9]) {
rlm@83: rgb = rgb[9][split](commaSpaces);
rlm@83: red = toFloat(rgb[0]) * 2.55;
rlm@83: green = toFloat(rgb[1]) * 2.55;
rlm@83: blue = toFloat(rgb[2]) * 2.55;
rlm@83: (rgb[0].slice(-3) == "deg" || rgb[0].slice(-1) == "\xb0") && (red /= 360 * 2.55);
rlm@83: return R.hsl2rgb(red, green, blue);
rlm@83: }
rlm@83: rgb = {r: red, g: green, b: blue};
rlm@83: var r = (~~red)[toString](16),
rlm@83: g = (~~green)[toString](16),
rlm@83: b = (~~blue)[toString](16);
rlm@83: r = r[rp](rg, "0");
rlm@83: g = g[rp](rg, "0");
rlm@83: b = b[rp](rg, "0");
rlm@83: rgb.hex = "#" + r + g + b;
rlm@83: isFinite(toFloat(opacity)) && (rgb.o = opacity);
rlm@83: return rgb;
rlm@83: }
rlm@83: return {r: -1, g: -1, b: -1, hex: "none", error: 1};
rlm@83: }, R);
rlm@83: R.getColor = function (value) {
rlm@83: var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
rlm@83: rgb = this.hsb2rgb(start.h, start.s, start.b);
rlm@83: start.h += .075;
rlm@83: if (start.h > 1) {
rlm@83: start.h = 0;
rlm@83: start.s -= .2;
rlm@83: start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
rlm@83: }
rlm@83: return rgb.hex;
rlm@83: };
rlm@83: R.getColor.reset = function () {
rlm@83: delete this.start;
rlm@83: };
rlm@83: // path utilities
rlm@83: var pathCommand = /([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig,
rlm@83: pathValues = /(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig;
rlm@83: R.parsePathString = cacher(function (pathString) {
rlm@83: if (!pathString) {
rlm@83: return null;
rlm@83: }
rlm@83: var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
rlm@83: data = [];
rlm@83: if (R.is(pathString, array) && R.is(pathString[0], array)) { // rough assumption
rlm@83: data = pathClone(pathString);
rlm@83: }
rlm@83: if (!data[length]) {
rlm@83: Str(pathString)[rp](pathCommand, function (a, b, c) {
rlm@83: var params = [],
rlm@83: name = lowerCase.call(b);
rlm@83: c[rp](pathValues, function (a, b) {
rlm@83: b && params[push](+b);
rlm@83: });
rlm@83: if (name == "m" && params[length] > 2) {
rlm@83: data[push]([b][concat](params.splice(0, 2)));
rlm@83: name = "l";
rlm@83: b = b == "m" ? "l" : "L";
rlm@83: }
rlm@83: while (params[length] >= paramCounts[name]) {
rlm@83: data[push]([b][concat](params.splice(0, paramCounts[name])));
rlm@83: if (!paramCounts[name]) {
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: });
rlm@83: }
rlm@83: data[toString] = R._path2string;
rlm@83: return data;
rlm@83: });
rlm@83: R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
rlm@83: var t1 = 1 - t,
rlm@83: x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
rlm@83: y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
rlm@83: mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
rlm@83: my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
rlm@83: nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
rlm@83: ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
rlm@83: ax = (1 - t) * p1x + t * c1x,
rlm@83: ay = (1 - t) * p1y + t * c1y,
rlm@83: cx = (1 - t) * c2x + t * p2x,
rlm@83: cy = (1 - t) * c2y + t * p2y,
rlm@83: alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
rlm@83: (mx > nx || my < ny) && (alpha += 180);
rlm@83: 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};
rlm@83: };
rlm@83: var pathDimensions = cacher(function (path) {
rlm@83: if (!path) {
rlm@83: return {x: 0, y: 0, width: 0, height: 0};
rlm@83: }
rlm@83: path = path2curve(path);
rlm@83: var x = 0,
rlm@83: y = 0,
rlm@83: X = [],
rlm@83: Y = [],
rlm@83: p;
rlm@83: for (var i = 0, ii = path[length]; i < ii; i++) {
rlm@83: p = path[i];
rlm@83: if (p[0] == "M") {
rlm@83: x = p[1];
rlm@83: y = p[2];
rlm@83: X[push](x);
rlm@83: Y[push](y);
rlm@83: } else {
rlm@83: var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
rlm@83: X = X[concat](dim.min.x, dim.max.x);
rlm@83: Y = Y[concat](dim.min.y, dim.max.y);
rlm@83: x = p[5];
rlm@83: y = p[6];
rlm@83: }
rlm@83: }
rlm@83: var xmin = mmin[apply](0, X),
rlm@83: ymin = mmin[apply](0, Y);
rlm@83: return {
rlm@83: x: xmin,
rlm@83: y: ymin,
rlm@83: width: mmax[apply](0, X) - xmin,
rlm@83: height: mmax[apply](0, Y) - ymin
rlm@83: };
rlm@83: }),
rlm@83: pathClone = function (pathArray) {
rlm@83: var res = [];
rlm@83: if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
rlm@83: pathArray = R.parsePathString(pathArray);
rlm@83: }
rlm@83: for (var i = 0, ii = pathArray[length]; i < ii; i++) {
rlm@83: res[i] = [];
rlm@83: for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
rlm@83: res[i][j] = pathArray[i][j];
rlm@83: }
rlm@83: }
rlm@83: res[toString] = R._path2string;
rlm@83: return res;
rlm@83: },
rlm@83: pathToRelative = cacher(function (pathArray) {
rlm@83: if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
rlm@83: pathArray = R.parsePathString(pathArray);
rlm@83: }
rlm@83: var res = [],
rlm@83: x = 0,
rlm@83: y = 0,
rlm@83: mx = 0,
rlm@83: my = 0,
rlm@83: start = 0;
rlm@83: if (pathArray[0][0] == "M") {
rlm@83: x = pathArray[0][1];
rlm@83: y = pathArray[0][2];
rlm@83: mx = x;
rlm@83: my = y;
rlm@83: start++;
rlm@83: res[push](["M", x, y]);
rlm@83: }
rlm@83: for (var i = start, ii = pathArray[length]; i < ii; i++) {
rlm@83: var r = res[i] = [],
rlm@83: pa = pathArray[i];
rlm@83: if (pa[0] != lowerCase.call(pa[0])) {
rlm@83: r[0] = lowerCase.call(pa[0]);
rlm@83: switch (r[0]) {
rlm@83: case "a":
rlm@83: r[1] = pa[1];
rlm@83: r[2] = pa[2];
rlm@83: r[3] = pa[3];
rlm@83: r[4] = pa[4];
rlm@83: r[5] = pa[5];
rlm@83: r[6] = +(pa[6] - x).toFixed(3);
rlm@83: r[7] = +(pa[7] - y).toFixed(3);
rlm@83: break;
rlm@83: case "v":
rlm@83: r[1] = +(pa[1] - y).toFixed(3);
rlm@83: break;
rlm@83: case "m":
rlm@83: mx = pa[1];
rlm@83: my = pa[2];
rlm@83: default:
rlm@83: for (var j = 1, jj = pa[length]; j < jj; j++) {
rlm@83: r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
rlm@83: }
rlm@83: }
rlm@83: } else {
rlm@83: r = res[i] = [];
rlm@83: if (pa[0] == "m") {
rlm@83: mx = pa[1] + x;
rlm@83: my = pa[2] + y;
rlm@83: }
rlm@83: for (var k = 0, kk = pa[length]; k < kk; k++) {
rlm@83: res[i][k] = pa[k];
rlm@83: }
rlm@83: }
rlm@83: var len = res[i][length];
rlm@83: switch (res[i][0]) {
rlm@83: case "z":
rlm@83: x = mx;
rlm@83: y = my;
rlm@83: break;
rlm@83: case "h":
rlm@83: x += +res[i][len - 1];
rlm@83: break;
rlm@83: case "v":
rlm@83: y += +res[i][len - 1];
rlm@83: break;
rlm@83: default:
rlm@83: x += +res[i][len - 2];
rlm@83: y += +res[i][len - 1];
rlm@83: }
rlm@83: }
rlm@83: res[toString] = R._path2string;
rlm@83: return res;
rlm@83: }, 0, pathClone),
rlm@83: pathToAbsolute = cacher(function (pathArray) {
rlm@83: if (!R.is(pathArray, array) || !R.is(pathArray && pathArray[0], array)) { // rough assumption
rlm@83: pathArray = R.parsePathString(pathArray);
rlm@83: }
rlm@83: var res = [],
rlm@83: x = 0,
rlm@83: y = 0,
rlm@83: mx = 0,
rlm@83: my = 0,
rlm@83: start = 0;
rlm@83: if (pathArray[0][0] == "M") {
rlm@83: x = +pathArray[0][1];
rlm@83: y = +pathArray[0][2];
rlm@83: mx = x;
rlm@83: my = y;
rlm@83: start++;
rlm@83: res[0] = ["M", x, y];
rlm@83: }
rlm@83: for (var i = start, ii = pathArray[length]; i < ii; i++) {
rlm@83: var r = res[i] = [],
rlm@83: pa = pathArray[i];
rlm@83: if (pa[0] != upperCase.call(pa[0])) {
rlm@83: r[0] = upperCase.call(pa[0]);
rlm@83: switch (r[0]) {
rlm@83: case "A":
rlm@83: r[1] = pa[1];
rlm@83: r[2] = pa[2];
rlm@83: r[3] = pa[3];
rlm@83: r[4] = pa[4];
rlm@83: r[5] = pa[5];
rlm@83: r[6] = +(pa[6] + x);
rlm@83: r[7] = +(pa[7] + y);
rlm@83: break;
rlm@83: case "V":
rlm@83: r[1] = +pa[1] + y;
rlm@83: break;
rlm@83: case "H":
rlm@83: r[1] = +pa[1] + x;
rlm@83: break;
rlm@83: case "M":
rlm@83: mx = +pa[1] + x;
rlm@83: my = +pa[2] + y;
rlm@83: default:
rlm@83: for (var j = 1, jj = pa[length]; j < jj; j++) {
rlm@83: r[j] = +pa[j] + ((j % 2) ? x : y);
rlm@83: }
rlm@83: }
rlm@83: } else {
rlm@83: for (var k = 0, kk = pa[length]; k < kk; k++) {
rlm@83: res[i][k] = pa[k];
rlm@83: }
rlm@83: }
rlm@83: switch (r[0]) {
rlm@83: case "Z":
rlm@83: x = mx;
rlm@83: y = my;
rlm@83: break;
rlm@83: case "H":
rlm@83: x = r[1];
rlm@83: break;
rlm@83: case "V":
rlm@83: y = r[1];
rlm@83: break;
rlm@83: case "M":
rlm@83: mx = res[i][res[i][length] - 2];
rlm@83: my = res[i][res[i][length] - 1];
rlm@83: default:
rlm@83: x = res[i][res[i][length] - 2];
rlm@83: y = res[i][res[i][length] - 1];
rlm@83: }
rlm@83: }
rlm@83: res[toString] = R._path2string;
rlm@83: return res;
rlm@83: }, null, pathClone),
rlm@83: l2c = function (x1, y1, x2, y2) {
rlm@83: return [x1, y1, x2, y2, x2, y2];
rlm@83: },
rlm@83: q2c = function (x1, y1, ax, ay, x2, y2) {
rlm@83: var _13 = 1 / 3,
rlm@83: _23 = 2 / 3;
rlm@83: return [
rlm@83: _13 * x1 + _23 * ax,
rlm@83: _13 * y1 + _23 * ay,
rlm@83: _13 * x2 + _23 * ax,
rlm@83: _13 * y2 + _23 * ay,
rlm@83: x2,
rlm@83: y2
rlm@83: ];
rlm@83: },
rlm@83: a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
rlm@83: // for more information of where this math came from visit:
rlm@83: // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
rlm@83: var PI = math.PI,
rlm@83: _120 = PI * 120 / 180,
rlm@83: rad = PI / 180 * (+angle || 0),
rlm@83: res = [],
rlm@83: xy,
rlm@83: rotate = cacher(function (x, y, rad) {
rlm@83: var X = x * math.cos(rad) - y * math.sin(rad),
rlm@83: Y = x * math.sin(rad) + y * math.cos(rad);
rlm@83: return {x: X, y: Y};
rlm@83: });
rlm@83: if (!recursive) {
rlm@83: xy = rotate(x1, y1, -rad);
rlm@83: x1 = xy.x;
rlm@83: y1 = xy.y;
rlm@83: xy = rotate(x2, y2, -rad);
rlm@83: x2 = xy.x;
rlm@83: y2 = xy.y;
rlm@83: var cos = math.cos(PI / 180 * angle),
rlm@83: sin = math.sin(PI / 180 * angle),
rlm@83: x = (x1 - x2) / 2,
rlm@83: y = (y1 - y2) / 2;
rlm@83: var h = (x * x) / (rx * rx) + (y * y) / (ry * ry);
rlm@83: if (h > 1) {
rlm@83: h = math.sqrt(h);
rlm@83: rx = h * rx;
rlm@83: ry = h * ry;
rlm@83: }
rlm@83: var rx2 = rx * rx,
rlm@83: ry2 = ry * ry,
rlm@83: k = (large_arc_flag == sweep_flag ? -1 : 1) *
rlm@83: math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
rlm@83: cx = k * rx * y / ry + (x1 + x2) / 2,
rlm@83: cy = k * -ry * x / rx + (y1 + y2) / 2,
rlm@83: f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
rlm@83: f2 = math.asin(((y2 - cy) / ry).toFixed(7));
rlm@83:
rlm@83: f1 = x1 < cx ? PI - f1 : f1;
rlm@83: f2 = x2 < cx ? PI - f2 : f2;
rlm@83: f1 < 0 && (f1 = PI * 2 + f1);
rlm@83: f2 < 0 && (f2 = PI * 2 + f2);
rlm@83: if (sweep_flag && f1 > f2) {
rlm@83: f1 = f1 - PI * 2;
rlm@83: }
rlm@83: if (!sweep_flag && f2 > f1) {
rlm@83: f2 = f2 - PI * 2;
rlm@83: }
rlm@83: } else {
rlm@83: f1 = recursive[0];
rlm@83: f2 = recursive[1];
rlm@83: cx = recursive[2];
rlm@83: cy = recursive[3];
rlm@83: }
rlm@83: var df = f2 - f1;
rlm@83: if (math.abs(df) > _120) {
rlm@83: var f2old = f2,
rlm@83: x2old = x2,
rlm@83: y2old = y2;
rlm@83: f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
rlm@83: x2 = cx + rx * math.cos(f2);
rlm@83: y2 = cy + ry * math.sin(f2);
rlm@83: res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
rlm@83: }
rlm@83: df = f2 - f1;
rlm@83: var c1 = math.cos(f1),
rlm@83: s1 = math.sin(f1),
rlm@83: c2 = math.cos(f2),
rlm@83: s2 = math.sin(f2),
rlm@83: t = math.tan(df / 4),
rlm@83: hx = 4 / 3 * rx * t,
rlm@83: hy = 4 / 3 * ry * t,
rlm@83: m1 = [x1, y1],
rlm@83: m2 = [x1 + hx * s1, y1 - hy * c1],
rlm@83: m3 = [x2 + hx * s2, y2 - hy * c2],
rlm@83: m4 = [x2, y2];
rlm@83: m2[0] = 2 * m1[0] - m2[0];
rlm@83: m2[1] = 2 * m1[1] - m2[1];
rlm@83: if (recursive) {
rlm@83: return [m2, m3, m4][concat](res);
rlm@83: } else {
rlm@83: res = [m2, m3, m4][concat](res)[join]()[split](",");
rlm@83: var newres = [];
rlm@83: for (var i = 0, ii = res[length]; i < ii; i++) {
rlm@83: newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
rlm@83: }
rlm@83: return newres;
rlm@83: }
rlm@83: },
rlm@83: findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
rlm@83: var t1 = 1 - t;
rlm@83: return {
rlm@83: x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
rlm@83: y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
rlm@83: };
rlm@83: },
rlm@83: curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
rlm@83: var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
rlm@83: b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
rlm@83: c = p1x - c1x,
rlm@83: t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
rlm@83: t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
rlm@83: y = [p1y, p2y],
rlm@83: x = [p1x, p2x],
rlm@83: dot;
rlm@83: math.abs(t1) > 1e12 && (t1 = .5);
rlm@83: math.abs(t2) > 1e12 && (t2 = .5);
rlm@83: if (t1 > 0 && t1 < 1) {
rlm@83: dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
rlm@83: x[push](dot.x);
rlm@83: y[push](dot.y);
rlm@83: }
rlm@83: if (t2 > 0 && t2 < 1) {
rlm@83: dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
rlm@83: x[push](dot.x);
rlm@83: y[push](dot.y);
rlm@83: }
rlm@83: a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
rlm@83: b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
rlm@83: c = p1y - c1y;
rlm@83: t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
rlm@83: t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
rlm@83: math.abs(t1) > 1e12 && (t1 = .5);
rlm@83: math.abs(t2) > 1e12 && (t2 = .5);
rlm@83: if (t1 > 0 && t1 < 1) {
rlm@83: dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
rlm@83: x[push](dot.x);
rlm@83: y[push](dot.y);
rlm@83: }
rlm@83: if (t2 > 0 && t2 < 1) {
rlm@83: dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
rlm@83: x[push](dot.x);
rlm@83: y[push](dot.y);
rlm@83: }
rlm@83: return {
rlm@83: min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
rlm@83: max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
rlm@83: };
rlm@83: }),
rlm@83: path2curve = cacher(function (path, path2) {
rlm@83: var p = pathToAbsolute(path),
rlm@83: p2 = path2 && pathToAbsolute(path2),
rlm@83: attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
rlm@83: attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
rlm@83: processPath = function (path, d) {
rlm@83: var nx, ny;
rlm@83: if (!path) {
rlm@83: return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
rlm@83: }
rlm@83: !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
rlm@83: switch (path[0]) {
rlm@83: case "M":
rlm@83: d.X = path[1];
rlm@83: d.Y = path[2];
rlm@83: break;
rlm@83: case "A":
rlm@83: path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
rlm@83: break;
rlm@83: case "S":
rlm@83: nx = d.x + (d.x - (d.bx || d.x));
rlm@83: ny = d.y + (d.y - (d.by || d.y));
rlm@83: path = ["C", nx, ny][concat](path.slice(1));
rlm@83: break;
rlm@83: case "T":
rlm@83: d.qx = d.x + (d.x - (d.qx || d.x));
rlm@83: d.qy = d.y + (d.y - (d.qy || d.y));
rlm@83: path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
rlm@83: break;
rlm@83: case "Q":
rlm@83: d.qx = path[1];
rlm@83: d.qy = path[2];
rlm@83: path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
rlm@83: break;
rlm@83: case "L":
rlm@83: path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
rlm@83: break;
rlm@83: case "H":
rlm@83: path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
rlm@83: break;
rlm@83: case "V":
rlm@83: path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
rlm@83: break;
rlm@83: case "Z":
rlm@83: path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
rlm@83: break;
rlm@83: }
rlm@83: return path;
rlm@83: },
rlm@83: fixArc = function (pp, i) {
rlm@83: if (pp[i][length] > 7) {
rlm@83: pp[i].shift();
rlm@83: var pi = pp[i];
rlm@83: while (pi[length]) {
rlm@83: pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
rlm@83: }
rlm@83: pp.splice(i, 1);
rlm@83: ii = mmax(p[length], p2 && p2[length] || 0);
rlm@83: }
rlm@83: },
rlm@83: fixM = function (path1, path2, a1, a2, i) {
rlm@83: if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
rlm@83: path2.splice(i, 0, ["M", a2.x, a2.y]);
rlm@83: a1.bx = 0;
rlm@83: a1.by = 0;
rlm@83: a1.x = path1[i][1];
rlm@83: a1.y = path1[i][2];
rlm@83: ii = mmax(p[length], p2 && p2[length] || 0);
rlm@83: }
rlm@83: };
rlm@83: for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
rlm@83: p[i] = processPath(p[i], attrs);
rlm@83: fixArc(p, i);
rlm@83: p2 && (p2[i] = processPath(p2[i], attrs2));
rlm@83: p2 && fixArc(p2, i);
rlm@83: fixM(p, p2, attrs, attrs2, i);
rlm@83: fixM(p2, p, attrs2, attrs, i);
rlm@83: var seg = p[i],
rlm@83: seg2 = p2 && p2[i],
rlm@83: seglen = seg[length],
rlm@83: seg2len = p2 && seg2[length];
rlm@83: attrs.x = seg[seglen - 2];
rlm@83: attrs.y = seg[seglen - 1];
rlm@83: attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
rlm@83: attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
rlm@83: attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
rlm@83: attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
rlm@83: attrs2.x = p2 && seg2[seg2len - 2];
rlm@83: attrs2.y = p2 && seg2[seg2len - 1];
rlm@83: }
rlm@83: return p2 ? [p, p2] : p;
rlm@83: }, null, pathClone),
rlm@83: parseDots = cacher(function (gradient) {
rlm@83: var dots = [];
rlm@83: for (var i = 0, ii = gradient[length]; i < ii; i++) {
rlm@83: var dot = {},
rlm@83: par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
rlm@83: dot.color = R.getRGB(par[1]);
rlm@83: if (dot.color.error) {
rlm@83: return null;
rlm@83: }
rlm@83: dot.color = dot.color.hex;
rlm@83: par[2] && (dot.offset = par[2] + "%");
rlm@83: dots[push](dot);
rlm@83: }
rlm@83: for (i = 1, ii = dots[length] - 1; i < ii; i++) {
rlm@83: if (!dots[i].offset) {
rlm@83: var start = toFloat(dots[i - 1].offset || 0),
rlm@83: end = 0;
rlm@83: for (var j = i + 1; j < ii; j++) {
rlm@83: if (dots[j].offset) {
rlm@83: end = dots[j].offset;
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: if (!end) {
rlm@83: end = 100;
rlm@83: j = ii;
rlm@83: }
rlm@83: end = toFloat(end);
rlm@83: var d = (end - start) / (j - i + 1);
rlm@83: for (; i < j; i++) {
rlm@83: start += d;
rlm@83: dots[i].offset = start + "%";
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: return dots;
rlm@83: }),
rlm@83: getContainer = function (x, y, w, h) {
rlm@83: var container;
rlm@83: if (R.is(x, string) || R.is(x, "object")) {
rlm@83: container = R.is(x, string) ? doc.getElementById(x) : x;
rlm@83: if (container.tagName) {
rlm@83: if (y == null) {
rlm@83: return {
rlm@83: container: container,
rlm@83: width: container.style.pixelWidth || container.offsetWidth,
rlm@83: height: container.style.pixelHeight || container.offsetHeight
rlm@83: };
rlm@83: } else {
rlm@83: return {container: container, width: y, height: w};
rlm@83: }
rlm@83: }
rlm@83: } else {
rlm@83: return {container: 1, x: x, y: y, width: w, height: h};
rlm@83: }
rlm@83: },
rlm@83: plugins = function (con, add) {
rlm@83: var that = this;
rlm@83: for (var prop in add) {
rlm@83: if (add[has](prop) && !(prop in con)) {
rlm@83: switch (typeof add[prop]) {
rlm@83: case "function":
rlm@83: (function (f) {
rlm@83: con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
rlm@83: })(add[prop]);
rlm@83: break;
rlm@83: case "object":
rlm@83: con[prop] = con[prop] || {};
rlm@83: plugins.call(this, con[prop], add[prop]);
rlm@83: break;
rlm@83: default:
rlm@83: con[prop] = add[prop];
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: },
rlm@83: tear = function (el, paper) {
rlm@83: el == paper.top && (paper.top = el.prev);
rlm@83: el == paper.bottom && (paper.bottom = el.next);
rlm@83: el.next && (el.next.prev = el.prev);
rlm@83: el.prev && (el.prev.next = el.next);
rlm@83: },
rlm@83: tofront = function (el, paper) {
rlm@83: if (paper.top === el) {
rlm@83: return;
rlm@83: }
rlm@83: tear(el, paper);
rlm@83: el.next = null;
rlm@83: el.prev = paper.top;
rlm@83: paper.top.next = el;
rlm@83: paper.top = el;
rlm@83: },
rlm@83: toback = function (el, paper) {
rlm@83: if (paper.bottom === el) {
rlm@83: return;
rlm@83: }
rlm@83: tear(el, paper);
rlm@83: el.next = paper.bottom;
rlm@83: el.prev = null;
rlm@83: paper.bottom.prev = el;
rlm@83: paper.bottom = el;
rlm@83: },
rlm@83: insertafter = function (el, el2, paper) {
rlm@83: tear(el, paper);
rlm@83: el2 == paper.top && (paper.top = el);
rlm@83: el2.next && (el2.next.prev = el);
rlm@83: el.next = el2.next;
rlm@83: el.prev = el2;
rlm@83: el2.next = el;
rlm@83: },
rlm@83: insertbefore = function (el, el2, paper) {
rlm@83: tear(el, paper);
rlm@83: el2 == paper.bottom && (paper.bottom = el);
rlm@83: el2.prev && (el2.prev.next = el);
rlm@83: el.prev = el2.prev;
rlm@83: el2.prev = el;
rlm@83: el.next = el2;
rlm@83: },
rlm@83: removed = function (methodname) {
rlm@83: return function () {
rlm@83: throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
rlm@83: };
rlm@83: },
rlm@83: radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
rlm@83: R.pathToRelative = pathToRelative;
rlm@83: // SVG
rlm@83: if (R.svg) {
rlm@83: Paper[proto].svgns = "http://www.w3.org/2000/svg";
rlm@83: Paper[proto].xlink = "http://www.w3.org/1999/xlink";
rlm@83: round = function (num) {
rlm@83: return +num + (~~num === num) * .5;
rlm@83: };
rlm@83: var $ = function (el, attr) {
rlm@83: if (attr) {
rlm@83: for (var key in attr) {
rlm@83: if (attr[has](key)) {
rlm@83: el[setAttribute](key, Str(attr[key]));
rlm@83: }
rlm@83: }
rlm@83: } else {
rlm@83: el = doc.createElementNS(Paper[proto].svgns, el);
rlm@83: el.style.webkitTapHighlightColor = "rgba(0,0,0,0)";
rlm@83: return el;
rlm@83: }
rlm@83: };
rlm@83: R[toString] = function () {
rlm@83: return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
rlm@83: };
rlm@83: var thePath = function (pathString, SVG) {
rlm@83: var el = $("path");
rlm@83: SVG.canvas && SVG.canvas[appendChild](el);
rlm@83: var p = new Element(el, SVG);
rlm@83: p.type = "path";
rlm@83: setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
rlm@83: return p;
rlm@83: };
rlm@83: var addGradientFill = function (o, gradient, SVG) {
rlm@83: var type = "linear",
rlm@83: fx = .5, fy = .5,
rlm@83: s = o.style;
rlm@83: gradient = Str(gradient)[rp](radial_gradient, function (all, _fx, _fy) {
rlm@83: type = "radial";
rlm@83: if (_fx && _fy) {
rlm@83: fx = toFloat(_fx);
rlm@83: fy = toFloat(_fy);
rlm@83: var dir = ((fy > .5) * 2 - 1);
rlm@83: pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
rlm@83: (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
rlm@83: fy != .5 &&
rlm@83: (fy = fy.toFixed(5) - 1e-5 * dir);
rlm@83: }
rlm@83: return E;
rlm@83: });
rlm@83: gradient = gradient[split](/\s*\-\s*/);
rlm@83: if (type == "linear") {
rlm@83: var angle = gradient.shift();
rlm@83: angle = -toFloat(angle);
rlm@83: if (isNaN(angle)) {
rlm@83: return null;
rlm@83: }
rlm@83: var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
rlm@83: max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
rlm@83: vector[2] *= max;
rlm@83: vector[3] *= max;
rlm@83: if (vector[2] < 0) {
rlm@83: vector[0] = -vector[2];
rlm@83: vector[2] = 0;
rlm@83: }
rlm@83: if (vector[3] < 0) {
rlm@83: vector[1] = -vector[3];
rlm@83: vector[3] = 0;
rlm@83: }
rlm@83: }
rlm@83: var dots = parseDots(gradient);
rlm@83: if (!dots) {
rlm@83: return null;
rlm@83: }
rlm@83: var id = o.getAttribute(fillString);
rlm@83: id = id.match(/^url\(#(.*)\)$/);
rlm@83: id && SVG.defs.removeChild(doc.getElementById(id[1]));
rlm@83:
rlm@83: var el = $(type + "Gradient");
rlm@83: el.id = "r" + (R._id++)[toString](36);
rlm@83: $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
rlm@83: SVG.defs[appendChild](el);
rlm@83: for (var i = 0, ii = dots[length]; i < ii; i++) {
rlm@83: var stop = $("stop");
rlm@83: $(stop, {
rlm@83: offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
rlm@83: "stop-color": dots[i].color || "#fff"
rlm@83: });
rlm@83: el[appendChild](stop);
rlm@83: }
rlm@83: $(o, {
rlm@83: fill: "url(#" + el.id + ")",
rlm@83: opacity: 1,
rlm@83: "fill-opacity": 1
rlm@83: });
rlm@83: s.fill = E;
rlm@83: s.opacity = 1;
rlm@83: s.fillOpacity = 1;
rlm@83: return 1;
rlm@83: };
rlm@83: var updatePosition = function (o) {
rlm@83: var bbox = o.getBBox();
rlm@83: $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
rlm@83: };
rlm@83: var setFillAndStroke = function (o, params) {
rlm@83: var dasharray = {
rlm@83: "": [0],
rlm@83: "none": [0],
rlm@83: "-": [3, 1],
rlm@83: ".": [1, 1],
rlm@83: "-.": [3, 1, 1, 1],
rlm@83: "-..": [3, 1, 1, 1, 1, 1],
rlm@83: ". ": [1, 3],
rlm@83: "- ": [4, 3],
rlm@83: "--": [8, 3],
rlm@83: "- .": [4, 3, 1, 3],
rlm@83: "--.": [8, 3, 1, 3],
rlm@83: "--..": [8, 3, 1, 3, 1, 3]
rlm@83: },
rlm@83: node = o.node,
rlm@83: attrs = o.attrs,
rlm@83: rot = o.rotate(),
rlm@83: addDashes = function (o, value) {
rlm@83: value = dasharray[lowerCase.call(value)];
rlm@83: if (value) {
rlm@83: var width = o.attrs["stroke-width"] || "1",
rlm@83: butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
rlm@83: dashes = [];
rlm@83: var i = value[length];
rlm@83: while (i--) {
rlm@83: dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
rlm@83: }
rlm@83: $(node, {"stroke-dasharray": dashes[join](",")});
rlm@83: }
rlm@83: };
rlm@83: params[has]("rotation") && (rot = params.rotation);
rlm@83: var rotxy = Str(rot)[split](separator);
rlm@83: if (!(rotxy.length - 1)) {
rlm@83: rotxy = null;
rlm@83: } else {
rlm@83: rotxy[1] = +rotxy[1];
rlm@83: rotxy[2] = +rotxy[2];
rlm@83: }
rlm@83: toFloat(rot) && o.rotate(0, true);
rlm@83: for (var att in params) {
rlm@83: if (params[has](att)) {
rlm@83: if (!availableAttrs[has](att)) {
rlm@83: continue;
rlm@83: }
rlm@83: var value = params[att];
rlm@83: attrs[att] = value;
rlm@83: switch (att) {
rlm@83: case "blur":
rlm@83: o.blur(value);
rlm@83: break;
rlm@83: case "rotation":
rlm@83: o.rotate(value, true);
rlm@83: break;
rlm@83: case "href":
rlm@83: case "title":
rlm@83: case "target":
rlm@83: var pn = node.parentNode;
rlm@83: if (lowerCase.call(pn.tagName) != "a") {
rlm@83: var hl = $("a");
rlm@83: pn.insertBefore(hl, node);
rlm@83: hl[appendChild](node);
rlm@83: pn = hl;
rlm@83: }
rlm@83: pn.setAttributeNS(o.paper.xlink, att, value);
rlm@83: break;
rlm@83: case "cursor":
rlm@83: node.style.cursor = value;
rlm@83: break;
rlm@83: case "clip-rect":
rlm@83: var rect = Str(value)[split](separator);
rlm@83: if (rect[length] == 4) {
rlm@83: o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
rlm@83: var el = $("clipPath"),
rlm@83: rc = $("rect");
rlm@83: el.id = "r" + (R._id++)[toString](36);
rlm@83: $(rc, {
rlm@83: x: rect[0],
rlm@83: y: rect[1],
rlm@83: width: rect[2],
rlm@83: height: rect[3]
rlm@83: });
rlm@83: el[appendChild](rc);
rlm@83: o.paper.defs[appendChild](el);
rlm@83: $(node, {"clip-path": "url(#" + el.id + ")"});
rlm@83: o.clip = rc;
rlm@83: }
rlm@83: if (!value) {
rlm@83: var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
rlm@83: clip && clip.parentNode.removeChild(clip);
rlm@83: $(node, {"clip-path": E});
rlm@83: delete o.clip;
rlm@83: }
rlm@83: break;
rlm@83: case "path":
rlm@83: if (o.type == "path") {
rlm@83: $(node, {d: value ? attrs.path = pathToAbsolute(value) : "M0,0"});
rlm@83: }
rlm@83: break;
rlm@83: case "width":
rlm@83: node[setAttribute](att, value);
rlm@83: if (attrs.fx) {
rlm@83: att = "x";
rlm@83: value = attrs.x;
rlm@83: } else {
rlm@83: break;
rlm@83: }
rlm@83: case "x":
rlm@83: if (attrs.fx) {
rlm@83: value = -attrs.x - (attrs.width || 0);
rlm@83: }
rlm@83: case "rx":
rlm@83: if (att == "rx" && o.type == "rect") {
rlm@83: break;
rlm@83: }
rlm@83: case "cx":
rlm@83: rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
rlm@83: node[setAttribute](att, value);
rlm@83: o.pattern && updatePosition(o);
rlm@83: break;
rlm@83: case "height":
rlm@83: node[setAttribute](att, value);
rlm@83: if (attrs.fy) {
rlm@83: att = "y";
rlm@83: value = attrs.y;
rlm@83: } else {
rlm@83: break;
rlm@83: }
rlm@83: case "y":
rlm@83: if (attrs.fy) {
rlm@83: value = -attrs.y - (attrs.height || 0);
rlm@83: }
rlm@83: case "ry":
rlm@83: if (att == "ry" && o.type == "rect") {
rlm@83: break;
rlm@83: }
rlm@83: case "cy":
rlm@83: rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
rlm@83: node[setAttribute](att, value);
rlm@83: o.pattern && updatePosition(o);
rlm@83: break;
rlm@83: case "r":
rlm@83: if (o.type == "rect") {
rlm@83: $(node, {rx: value, ry: value});
rlm@83: } else {
rlm@83: node[setAttribute](att, value);
rlm@83: }
rlm@83: break;
rlm@83: case "src":
rlm@83: if (o.type == "image") {
rlm@83: node.setAttributeNS(o.paper.xlink, "href", value);
rlm@83: }
rlm@83: break;
rlm@83: case "stroke-width":
rlm@83: node.style.strokeWidth = value;
rlm@83: // Need following line for Firefox
rlm@83: node[setAttribute](att, value);
rlm@83: if (attrs["stroke-dasharray"]) {
rlm@83: addDashes(o, attrs["stroke-dasharray"]);
rlm@83: }
rlm@83: break;
rlm@83: case "stroke-dasharray":
rlm@83: addDashes(o, value);
rlm@83: break;
rlm@83: case "translation":
rlm@83: var xy = Str(value)[split](separator);
rlm@83: xy[0] = +xy[0] || 0;
rlm@83: xy[1] = +xy[1] || 0;
rlm@83: if (rotxy) {
rlm@83: rotxy[1] += xy[0];
rlm@83: rotxy[2] += xy[1];
rlm@83: }
rlm@83: translate.call(o, xy[0], xy[1]);
rlm@83: break;
rlm@83: case "scale":
rlm@83: xy = Str(value)[split](separator);
rlm@83: o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, isNaN(toFloat(xy[2])) ? null : +xy[2], isNaN(toFloat(xy[3])) ? null : +xy[3]);
rlm@83: break;
rlm@83: case fillString:
rlm@83: var isURL = Str(value).match(ISURL);
rlm@83: if (isURL) {
rlm@83: el = $("pattern");
rlm@83: var ig = $("image");
rlm@83: el.id = "r" + (R._id++)[toString](36);
rlm@83: $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
rlm@83: $(ig, {x: 0, y: 0});
rlm@83: ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
rlm@83: el[appendChild](ig);
rlm@83:
rlm@83: var img = doc.createElement("img");
rlm@83: img.style.cssText = "position:absolute;left:-9999em;top-9999em";
rlm@83: img.onload = function () {
rlm@83: $(el, {width: this.offsetWidth, height: this.offsetHeight});
rlm@83: $(ig, {width: this.offsetWidth, height: this.offsetHeight});
rlm@83: doc.body.removeChild(this);
rlm@83: o.paper.safari();
rlm@83: };
rlm@83: doc.body[appendChild](img);
rlm@83: img.src = isURL[1];
rlm@83: o.paper.defs[appendChild](el);
rlm@83: node.style.fill = "url(#" + el.id + ")";
rlm@83: $(node, {fill: "url(#" + el.id + ")"});
rlm@83: o.pattern = el;
rlm@83: o.pattern && updatePosition(o);
rlm@83: break;
rlm@83: }
rlm@83: var clr = R.getRGB(value);
rlm@83: if (!clr.error) {
rlm@83: delete params.gradient;
rlm@83: delete attrs.gradient;
rlm@83: !R.is(attrs.opacity, "undefined") &&
rlm@83: R.is(params.opacity, "undefined") &&
rlm@83: $(node, {opacity: attrs.opacity});
rlm@83: !R.is(attrs["fill-opacity"], "undefined") &&
rlm@83: R.is(params["fill-opacity"], "undefined") &&
rlm@83: $(node, {"fill-opacity": attrs["fill-opacity"]});
rlm@83: } else if ((({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper)) {
rlm@83: attrs.gradient = value;
rlm@83: attrs.fill = "none";
rlm@83: break;
rlm@83: }
rlm@83: clr[has]("o") && $(node, {"fill-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
rlm@83: case "stroke":
rlm@83: clr = R.getRGB(value);
rlm@83: node[setAttribute](att, clr.hex);
rlm@83: att == "stroke" && clr[has]("o") && $(node, {"stroke-opacity": clr.o > 1 ? clr.o / 100 : clr.o});
rlm@83: break;
rlm@83: case "gradient":
rlm@83: (({circle: 1, ellipse: 1})[has](o.type) || Str(value).charAt() != "r") && addGradientFill(node, value, o.paper);
rlm@83: break;
rlm@83: case "opacity":
rlm@83: case "fill-opacity":
rlm@83: if (attrs.gradient) {
rlm@83: var gradient = doc.getElementById(node.getAttribute(fillString)[rp](/^url\(#|\)$/g, E));
rlm@83: if (gradient) {
rlm@83: var stops = gradient.getElementsByTagName("stop");
rlm@83: stops[stops[length] - 1][setAttribute]("stop-opacity", value);
rlm@83: }
rlm@83: break;
rlm@83: }
rlm@83: default:
rlm@83: att == "font-size" && (value = toInt(value, 10) + "px");
rlm@83: var cssrule = att[rp](/(\-.)/g, function (w) {
rlm@83: return upperCase.call(w.substring(1));
rlm@83: });
rlm@83: node.style[cssrule] = value;
rlm@83: // Need following line for Firefox
rlm@83: node[setAttribute](att, value);
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83:
rlm@83: tuneText(o, params);
rlm@83: if (rotxy) {
rlm@83: o.rotate(rotxy.join(S));
rlm@83: } else {
rlm@83: toFloat(rot) && o.rotate(rot, true);
rlm@83: }
rlm@83: };
rlm@83: var leading = 1.2,
rlm@83: tuneText = function (el, params) {
rlm@83: if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
rlm@83: return;
rlm@83: }
rlm@83: var a = el.attrs,
rlm@83: node = el.node,
rlm@83: fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
rlm@83:
rlm@83: if (params[has]("text")) {
rlm@83: a.text = params.text;
rlm@83: while (node.firstChild) {
rlm@83: node.removeChild(node.firstChild);
rlm@83: }
rlm@83: var texts = Str(params.text)[split]("\n");
rlm@83: for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
rlm@83: var tspan = $("tspan");
rlm@83: i && $(tspan, {dy: fontSize * leading, x: a.x});
rlm@83: tspan[appendChild](doc.createTextNode(texts[i]));
rlm@83: node[appendChild](tspan);
rlm@83: }
rlm@83: } else {
rlm@83: texts = node.getElementsByTagName("tspan");
rlm@83: for (i = 0, ii = texts[length]; i < ii; i++) {
rlm@83: i && $(texts[i], {dy: fontSize * leading, x: a.x});
rlm@83: }
rlm@83: }
rlm@83: $(node, {y: a.y});
rlm@83: var bb = el.getBBox(),
rlm@83: dif = a.y - (bb.y + bb.height / 2);
rlm@83: dif && isFinite(dif) && $(node, {y: a.y + dif});
rlm@83: },
rlm@83: Element = function (node, svg) {
rlm@83: var X = 0,
rlm@83: Y = 0;
rlm@83: this[0] = node;
rlm@83: this.id = R._oid++;
rlm@83: this.node = node;
rlm@83: node.raphael = this;
rlm@83: this.paper = svg;
rlm@83: this.attrs = this.attrs || {};
rlm@83: this.transformations = []; // rotate, translate, scale
rlm@83: this._ = {
rlm@83: tx: 0,
rlm@83: ty: 0,
rlm@83: rt: {deg: 0, cx: 0, cy: 0},
rlm@83: sx: 1,
rlm@83: sy: 1
rlm@83: };
rlm@83: !svg.bottom && (svg.bottom = this);
rlm@83: this.prev = svg.top;
rlm@83: svg.top && (svg.top.next = this);
rlm@83: svg.top = this;
rlm@83: this.next = null;
rlm@83: };
rlm@83: Element[proto].rotate = function (deg, cx, cy) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (deg == null) {
rlm@83: if (this._.rt.cx) {
rlm@83: return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
rlm@83: }
rlm@83: return this._.rt.deg;
rlm@83: }
rlm@83: var bbox = this.getBBox();
rlm@83: deg = Str(deg)[split](separator);
rlm@83: if (deg[length] - 1) {
rlm@83: cx = toFloat(deg[1]);
rlm@83: cy = toFloat(deg[2]);
rlm@83: }
rlm@83: deg = toFloat(deg[0]);
rlm@83: if (cx != null) {
rlm@83: this._.rt.deg = deg;
rlm@83: } else {
rlm@83: this._.rt.deg += deg;
rlm@83: }
rlm@83: (cy == null) && (cx = null);
rlm@83: this._.rt.cx = cx;
rlm@83: this._.rt.cy = cy;
rlm@83: cx = cx == null ? bbox.x + bbox.width / 2 : cx;
rlm@83: cy = cy == null ? bbox.y + bbox.height / 2 : cy;
rlm@83: if (this._.rt.deg) {
rlm@83: this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
rlm@83: this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
rlm@83: } else {
rlm@83: this.transformations[0] = E;
rlm@83: this.clip && $(this.clip, {transform: E});
rlm@83: }
rlm@83: $(this.node, {transform: this.transformations[join](S)});
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].hide = function () {
rlm@83: !this.removed && (this.node.style.display = "none");
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].show = function () {
rlm@83: !this.removed && (this.node.style.display = "");
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].remove = function () {
rlm@83: if (this.removed) {
rlm@83: return;
rlm@83: }
rlm@83: tear(this, this.paper);
rlm@83: this.node.parentNode.removeChild(this.node);
rlm@83: for (var i in this) {
rlm@83: delete this[i];
rlm@83: }
rlm@83: this.removed = true;
rlm@83: };
rlm@83: Element[proto].getBBox = function () {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (this.type == "path") {
rlm@83: return pathDimensions(this.attrs.path);
rlm@83: }
rlm@83: if (this.node.style.display == "none") {
rlm@83: this.show();
rlm@83: var hide = true;
rlm@83: }
rlm@83: var bbox = {};
rlm@83: try {
rlm@83: bbox = this.node.getBBox();
rlm@83: } catch(e) {
rlm@83: // Firefox 3.0.x plays badly here
rlm@83: } finally {
rlm@83: bbox = bbox || {};
rlm@83: }
rlm@83: if (this.type == "text") {
rlm@83: bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
rlm@83: for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
rlm@83: var bb = this.node.getExtentOfChar(i);
rlm@83: (bb.y < bbox.y) && (bbox.y = bb.y);
rlm@83: (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
rlm@83: (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
rlm@83: }
rlm@83: }
rlm@83: hide && this.hide();
rlm@83: return bbox;
rlm@83: };
rlm@83: Element[proto].attr = function (name, value) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (name == null) {
rlm@83: var res = {};
rlm@83: for (var i in this.attrs) if (this.attrs[has](i)) {
rlm@83: res[i] = this.attrs[i];
rlm@83: }
rlm@83: this._.rt.deg && (res.rotation = this.rotate());
rlm@83: (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
rlm@83: res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
rlm@83: return res;
rlm@83: }
rlm@83: if (value == null && R.is(name, string)) {
rlm@83: if (name == "translation") {
rlm@83: return translate.call(this);
rlm@83: }
rlm@83: if (name == "rotation") {
rlm@83: return this.rotate();
rlm@83: }
rlm@83: if (name == "scale") {
rlm@83: return this.scale();
rlm@83: }
rlm@83: if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
rlm@83: return this.attrs.gradient;
rlm@83: }
rlm@83: return this.attrs[name];
rlm@83: }
rlm@83: if (value == null && R.is(name, array)) {
rlm@83: var values = {};
rlm@83: for (var j = 0, jj = name.length; j < jj; j++) {
rlm@83: values[name[j]] = this.attr(name[j]);
rlm@83: }
rlm@83: return values;
rlm@83: }
rlm@83: if (value != null) {
rlm@83: var params = {};
rlm@83: params[name] = value;
rlm@83: setFillAndStroke(this, params);
rlm@83: } else if (name != null && R.is(name, "object")) {
rlm@83: setFillAndStroke(this, name);
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].toFront = function () {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: this.node.parentNode[appendChild](this.node);
rlm@83: var svg = this.paper;
rlm@83: svg.top != this && tofront(this, svg);
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].toBack = function () {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (this.node.parentNode.firstChild != this.node) {
rlm@83: this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
rlm@83: toback(this, this.paper);
rlm@83: var svg = this.paper;
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].insertAfter = function (element) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: var node = element.node || element[element.length].node;
rlm@83: if (node.nextSibling) {
rlm@83: node.parentNode.insertBefore(this.node, node.nextSibling);
rlm@83: } else {
rlm@83: node.parentNode[appendChild](this.node);
rlm@83: }
rlm@83: insertafter(this, element, this.paper);
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].insertBefore = function (element) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: var node = element.node || element[0].node;
rlm@83: node.parentNode.insertBefore(this.node, node);
rlm@83: insertbefore(this, element, this.paper);
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].blur = function (size) {
rlm@83: // Experimental. No Safari support. Use it on your own risk.
rlm@83: var t = this;
rlm@83: if (+size !== 0) {
rlm@83: var fltr = $("filter"),
rlm@83: blur = $("feGaussianBlur");
rlm@83: t.attrs.blur = size;
rlm@83: fltr.id = "r" + (R._id++)[toString](36);
rlm@83: $(blur, {stdDeviation: +size || 1.5});
rlm@83: fltr.appendChild(blur);
rlm@83: t.paper.defs.appendChild(fltr);
rlm@83: t._blur = fltr;
rlm@83: $(t.node, {filter: "url(#" + fltr.id + ")"});
rlm@83: } else {
rlm@83: if (t._blur) {
rlm@83: t._blur.parentNode.removeChild(t._blur);
rlm@83: delete t._blur;
rlm@83: delete t.attrs.blur;
rlm@83: }
rlm@83: t.node.removeAttribute("filter");
rlm@83: }
rlm@83: };
rlm@83: var theCircle = function (svg, x, y, r) {
rlm@83: var el = $("circle");
rlm@83: svg.canvas && svg.canvas[appendChild](el);
rlm@83: var res = new Element(el, svg);
rlm@83: res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
rlm@83: res.type = "circle";
rlm@83: $(el, res.attrs);
rlm@83: return res;
rlm@83: };
rlm@83: var theRect = function (svg, x, y, w, h, r) {
rlm@83: var el = $("rect");
rlm@83: svg.canvas && svg.canvas[appendChild](el);
rlm@83: var res = new Element(el, svg);
rlm@83: res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
rlm@83: res.type = "rect";
rlm@83: $(el, res.attrs);
rlm@83: return res;
rlm@83: };
rlm@83: var theEllipse = function (svg, x, y, rx, ry) {
rlm@83: var el = $("ellipse");
rlm@83: svg.canvas && svg.canvas[appendChild](el);
rlm@83: var res = new Element(el, svg);
rlm@83: res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
rlm@83: res.type = "ellipse";
rlm@83: $(el, res.attrs);
rlm@83: return res;
rlm@83: };
rlm@83: var theImage = function (svg, src, x, y, w, h) {
rlm@83: var el = $("image");
rlm@83: $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
rlm@83: el.setAttributeNS(svg.xlink, "href", src);
rlm@83: svg.canvas && svg.canvas[appendChild](el);
rlm@83: var res = new Element(el, svg);
rlm@83: res.attrs = {x: x, y: y, width: w, height: h, src: src};
rlm@83: res.type = "image";
rlm@83: return res;
rlm@83: };
rlm@83: var theText = function (svg, x, y, text) {
rlm@83: var el = $("text");
rlm@83: $(el, {x: x, y: y, "text-anchor": "middle"});
rlm@83: svg.canvas && svg.canvas[appendChild](el);
rlm@83: var res = new Element(el, svg);
rlm@83: res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
rlm@83: res.type = "text";
rlm@83: setFillAndStroke(res, res.attrs);
rlm@83: return res;
rlm@83: };
rlm@83: var setSize = function (width, height) {
rlm@83: this.width = width || this.width;
rlm@83: this.height = height || this.height;
rlm@83: this.canvas[setAttribute]("width", this.width);
rlm@83: this.canvas[setAttribute]("height", this.height);
rlm@83: return this;
rlm@83: };
rlm@83: var create = function () {
rlm@83: var con = getContainer[apply](0, arguments),
rlm@83: container = con && con.container,
rlm@83: x = con.x,
rlm@83: y = con.y,
rlm@83: width = con.width,
rlm@83: height = con.height;
rlm@83: if (!container) {
rlm@83: throw new Error("SVG container not found.");
rlm@83: }
rlm@83: var cnvs = $("svg");
rlm@83: x = x || 0;
rlm@83: y = y || 0;
rlm@83: width = width || 512;
rlm@83: height = height || 342;
rlm@83: $(cnvs, {
rlm@83: xmlns: "http://www.w3.org/2000/svg",
rlm@83: version: 1.1,
rlm@83: width: width,
rlm@83: height: height
rlm@83: });
rlm@83: if (container == 1) {
rlm@83: cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
rlm@83: doc.body[appendChild](cnvs);
rlm@83: } else {
rlm@83: if (container.firstChild) {
rlm@83: container.insertBefore(cnvs, container.firstChild);
rlm@83: } else {
rlm@83: container[appendChild](cnvs);
rlm@83: }
rlm@83: }
rlm@83: container = new Paper;
rlm@83: container.width = width;
rlm@83: container.height = height;
rlm@83: container.canvas = cnvs;
rlm@83: plugins.call(container, container, R.fn);
rlm@83: container.clear();
rlm@83: return container;
rlm@83: };
rlm@83: Paper[proto].clear = function () {
rlm@83: var c = this.canvas;
rlm@83: while (c.firstChild) {
rlm@83: c.removeChild(c.firstChild);
rlm@83: }
rlm@83: this.bottom = this.top = null;
rlm@83: (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
rlm@83: c[appendChild](this.desc);
rlm@83: c[appendChild](this.defs = $("defs"));
rlm@83: };
rlm@83: Paper[proto].remove = function () {
rlm@83: this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
rlm@83: for (var i in this) {
rlm@83: this[i] = removed(i);
rlm@83: }
rlm@83: };
rlm@83: }
rlm@83:
rlm@83: // VML
rlm@83: if (R.vml) {
rlm@83: var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
rlm@83: bites = /([clmz]),?([^clmz]*)/gi,
rlm@83: val = /-?[^,\s-]+/g,
rlm@83: coordsize = 1e3 + S + 1e3,
rlm@83: zoom = 10,
rlm@83: pathlike = {path: 1, rect: 1},
rlm@83: path2vml = function (path) {
rlm@83: var total = /[ahqstv]/ig,
rlm@83: command = pathToAbsolute;
rlm@83: Str(path).match(total) && (command = path2curve);
rlm@83: total = /[clmz]/g;
rlm@83: if (command == pathToAbsolute && !Str(path).match(total)) {
rlm@83: var res = Str(path)[rp](bites, function (all, command, args) {
rlm@83: var vals = [],
rlm@83: isMove = lowerCase.call(command) == "m",
rlm@83: res = map[command];
rlm@83: args[rp](val, function (value) {
rlm@83: if (isMove && vals[length] == 2) {
rlm@83: res += vals + map[command == "m" ? "l" : "L"];
rlm@83: vals = [];
rlm@83: }
rlm@83: vals[push](round(value * zoom));
rlm@83: });
rlm@83: return res + vals;
rlm@83: });
rlm@83: return res;
rlm@83: }
rlm@83: var pa = command(path), p, r;
rlm@83: res = [];
rlm@83: for (var i = 0, ii = pa[length]; i < ii; i++) {
rlm@83: p = pa[i];
rlm@83: r = lowerCase.call(pa[i][0]);
rlm@83: r == "z" && (r = "x");
rlm@83: for (var j = 1, jj = p[length]; j < jj; j++) {
rlm@83: r += round(p[j] * zoom) + (j != jj - 1 ? "," : E);
rlm@83: }
rlm@83: res[push](r);
rlm@83: }
rlm@83: return res[join](S);
rlm@83: };
rlm@83:
rlm@83: R[toString] = function () {
rlm@83: return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
rlm@83: };
rlm@83: thePath = function (pathString, vml) {
rlm@83: var g = createNode("group");
rlm@83: g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@83: g.coordsize = vml.coordsize;
rlm@83: g.coordorigin = vml.coordorigin;
rlm@83: var el = createNode("shape"), ol = el.style;
rlm@83: ol.width = vml.width + "px";
rlm@83: ol.height = vml.height + "px";
rlm@83: el.coordsize = coordsize;
rlm@83: el.coordorigin = vml.coordorigin;
rlm@83: g[appendChild](el);
rlm@83: var p = new Element(el, g, vml),
rlm@83: attr = {fill: "none", stroke: "#000"};
rlm@83: pathString && (attr.path = pathString);
rlm@83: p.isAbsolute = true;
rlm@83: p.type = "path";
rlm@83: p.path = [];
rlm@83: p.Path = E;
rlm@83: setFillAndStroke(p, attr);
rlm@83: vml.canvas[appendChild](g);
rlm@83: return p;
rlm@83: };
rlm@83: setFillAndStroke = function (o, params) {
rlm@83: o.attrs = o.attrs || {};
rlm@83: var node = o.node,
rlm@83: a = o.attrs,
rlm@83: s = node.style,
rlm@83: xy,
rlm@83: newpath = (params.x != a.x || params.y != a.y || params.width != a.width || params.height != a.height || params.r != a.r) && o.type == "rect",
rlm@83: res = o;
rlm@83:
rlm@83: for (var par in params) if (params[has](par)) {
rlm@83: a[par] = params[par];
rlm@83: }
rlm@83: if (newpath) {
rlm@83: a.path = rectPath(a.x, a.y, a.width, a.height, a.r);
rlm@83: o.X = a.x;
rlm@83: o.Y = a.y;
rlm@83: o.W = a.width;
rlm@83: o.H = a.height;
rlm@83: }
rlm@83: params.href && (node.href = params.href);
rlm@83: params.title && (node.title = params.title);
rlm@83: params.target && (node.target = params.target);
rlm@83: params.cursor && (s.cursor = params.cursor);
rlm@83: "blur" in params && o.blur(params.blur);
rlm@83: if (params.path && o.type == "path" || newpath) {
rlm@83: node.path = path2vml(a.path);
rlm@83: }
rlm@83: if (params.rotation != null) {
rlm@83: o.rotate(params.rotation, true);
rlm@83: }
rlm@83: if (params.translation) {
rlm@83: xy = Str(params.translation)[split](separator);
rlm@83: translate.call(o, xy[0], xy[1]);
rlm@83: if (o._.rt.cx != null) {
rlm@83: o._.rt.cx +=+ xy[0];
rlm@83: o._.rt.cy +=+ xy[1];
rlm@83: o.setBox(o.attrs, xy[0], xy[1]);
rlm@83: }
rlm@83: }
rlm@83: if (params.scale) {
rlm@83: xy = Str(params.scale)[split](separator);
rlm@83: o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
rlm@83: }
rlm@83: if ("clip-rect" in params) {
rlm@83: var rect = Str(params["clip-rect"])[split](separator);
rlm@83: if (rect[length] == 4) {
rlm@83: rect[2] = +rect[2] + (+rect[0]);
rlm@83: rect[3] = +rect[3] + (+rect[1]);
rlm@83: var div = node.clipRect || doc.createElement("div"),
rlm@83: dstyle = div.style,
rlm@83: group = node.parentNode;
rlm@83: dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
rlm@83: if (!node.clipRect) {
rlm@83: dstyle.position = "absolute";
rlm@83: dstyle.top = 0;
rlm@83: dstyle.left = 0;
rlm@83: dstyle.width = o.paper.width + "px";
rlm@83: dstyle.height = o.paper.height + "px";
rlm@83: group.parentNode.insertBefore(div, group);
rlm@83: div[appendChild](group);
rlm@83: node.clipRect = div;
rlm@83: }
rlm@83: }
rlm@83: if (!params["clip-rect"]) {
rlm@83: node.clipRect && (node.clipRect.style.clip = E);
rlm@83: }
rlm@83: }
rlm@83: if (o.type == "image" && params.src) {
rlm@83: node.src = params.src;
rlm@83: }
rlm@83: if (o.type == "image" && params.opacity) {
rlm@83: node.filterOpacity = ms + ".Alpha(opacity=" + (params.opacity * 100) + ")";
rlm@83: s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
rlm@83: }
rlm@83: params.font && (s.font = params.font);
rlm@83: params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
rlm@83: params["font-size"] && (s.fontSize = params["font-size"]);
rlm@83: params["font-weight"] && (s.fontWeight = params["font-weight"]);
rlm@83: params["font-style"] && (s.fontStyle = params["font-style"]);
rlm@83: if (params.opacity != null ||
rlm@83: params["stroke-width"] != null ||
rlm@83: params.fill != null ||
rlm@83: params.stroke != null ||
rlm@83: params["stroke-width"] != null ||
rlm@83: params["stroke-opacity"] != null ||
rlm@83: params["fill-opacity"] != null ||
rlm@83: params["stroke-dasharray"] != null ||
rlm@83: params["stroke-miterlimit"] != null ||
rlm@83: params["stroke-linejoin"] != null ||
rlm@83: params["stroke-linecap"] != null) {
rlm@83: node = o.shape || node;
rlm@83: var fill = (node.getElementsByTagName(fillString) && node.getElementsByTagName(fillString)[0]),
rlm@83: newfill = false;
rlm@83: !fill && (newfill = fill = createNode(fillString));
rlm@83: if ("fill-opacity" in params || "opacity" in params) {
rlm@83: var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+R.getRGB(params.fill).o + 1 || 2) - 1);
rlm@83: opacity < 0 && (opacity = 0);
rlm@83: opacity > 1 && (opacity = 1);
rlm@83: fill.opacity = opacity;
rlm@83: }
rlm@83: params.fill && (fill.on = true);
rlm@83: if (fill.on == null || params.fill == "none") {
rlm@83: fill.on = false;
rlm@83: }
rlm@83: if (fill.on && params.fill) {
rlm@83: var isURL = params.fill.match(ISURL);
rlm@83: if (isURL) {
rlm@83: fill.src = isURL[1];
rlm@83: fill.type = "tile";
rlm@83: } else {
rlm@83: fill.color = R.getRGB(params.fill).hex;
rlm@83: fill.src = E;
rlm@83: fill.type = "solid";
rlm@83: if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || Str(params.fill).charAt() != "r") && addGradientFill(res, params.fill)) {
rlm@83: a.fill = "none";
rlm@83: a.gradient = params.fill;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: newfill && node[appendChild](fill);
rlm@83: var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
rlm@83: newstroke = false;
rlm@83: !stroke && (newstroke = stroke = createNode("stroke"));
rlm@83: if ((params.stroke && params.stroke != "none") ||
rlm@83: params["stroke-width"] ||
rlm@83: params["stroke-opacity"] != null ||
rlm@83: params["stroke-dasharray"] ||
rlm@83: params["stroke-miterlimit"] ||
rlm@83: params["stroke-linejoin"] ||
rlm@83: params["stroke-linecap"]) {
rlm@83: stroke.on = true;
rlm@83: }
rlm@83: (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
rlm@83: var strokeColor = R.getRGB(params.stroke);
rlm@83: stroke.on && params.stroke && (stroke.color = strokeColor.hex);
rlm@83: opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1) * ((+strokeColor.o + 1 || 2) - 1);
rlm@83: var width = (toFloat(params["stroke-width"]) || 1) * .75;
rlm@83: opacity < 0 && (opacity = 0);
rlm@83: opacity > 1 && (opacity = 1);
rlm@83: params["stroke-width"] == null && (width = a["stroke-width"]);
rlm@83: params["stroke-width"] && (stroke.weight = width);
rlm@83: width && width < 1 && (opacity *= width) && (stroke.weight = 1);
rlm@83: stroke.opacity = opacity;
rlm@83:
rlm@83: params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
rlm@83: stroke.miterlimit = params["stroke-miterlimit"] || 8;
rlm@83: params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
rlm@83: if (params["stroke-dasharray"]) {
rlm@83: var dasharray = {
rlm@83: "-": "shortdash",
rlm@83: ".": "shortdot",
rlm@83: "-.": "shortdashdot",
rlm@83: "-..": "shortdashdotdot",
rlm@83: ". ": "dot",
rlm@83: "- ": "dash",
rlm@83: "--": "longdash",
rlm@83: "- .": "dashdot",
rlm@83: "--.": "longdashdot",
rlm@83: "--..": "longdashdotdot"
rlm@83: };
rlm@83: stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
rlm@83: }
rlm@83: newstroke && node[appendChild](stroke);
rlm@83: }
rlm@83: if (res.type == "text") {
rlm@83: s = res.paper.span.style;
rlm@83: a.font && (s.font = a.font);
rlm@83: a["font-family"] && (s.fontFamily = a["font-family"]);
rlm@83: a["font-size"] && (s.fontSize = a["font-size"]);
rlm@83: a["font-weight"] && (s.fontWeight = a["font-weight"]);
rlm@83: a["font-style"] && (s.fontStyle = a["font-style"]);
rlm@83: res.node.string && (res.paper.span.innerHTML = Str(res.node.string)[rp](/"));
rlm@83: res.W = a.w = res.paper.span.offsetWidth;
rlm@83: res.H = a.h = res.paper.span.offsetHeight;
rlm@83: res.X = a.x;
rlm@83: res.Y = a.y + round(res.H / 2);
rlm@83:
rlm@83: // text-anchor emulationm
rlm@83: switch (a["text-anchor"]) {
rlm@83: case "start":
rlm@83: res.node.style["v-text-align"] = "left";
rlm@83: res.bbx = round(res.W / 2);
rlm@83: break;
rlm@83: case "end":
rlm@83: res.node.style["v-text-align"] = "right";
rlm@83: res.bbx = -round(res.W / 2);
rlm@83: break;
rlm@83: default:
rlm@83: res.node.style["v-text-align"] = "center";
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: };
rlm@83: addGradientFill = function (o, gradient) {
rlm@83: o.attrs = o.attrs || {};
rlm@83: var attrs = o.attrs,
rlm@83: fill,
rlm@83: type = "linear",
rlm@83: fxfy = ".5 .5";
rlm@83: o.attrs.gradient = gradient;
rlm@83: gradient = Str(gradient)[rp](radial_gradient, function (all, fx, fy) {
rlm@83: type = "radial";
rlm@83: if (fx && fy) {
rlm@83: fx = toFloat(fx);
rlm@83: fy = toFloat(fy);
rlm@83: pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
rlm@83: fxfy = fx + S + fy;
rlm@83: }
rlm@83: return E;
rlm@83: });
rlm@83: gradient = gradient[split](/\s*\-\s*/);
rlm@83: if (type == "linear") {
rlm@83: var angle = gradient.shift();
rlm@83: angle = -toFloat(angle);
rlm@83: if (isNaN(angle)) {
rlm@83: return null;
rlm@83: }
rlm@83: }
rlm@83: var dots = parseDots(gradient);
rlm@83: if (!dots) {
rlm@83: return null;
rlm@83: }
rlm@83: o = o.shape || o.node;
rlm@83: fill = o.getElementsByTagName(fillString)[0] || createNode(fillString);
rlm@83: !fill.parentNode && o.appendChild(fill);
rlm@83: if (dots[length]) {
rlm@83: fill.on = true;
rlm@83: fill.method = "none";
rlm@83: fill.color = dots[0].color;
rlm@83: fill.color2 = dots[dots[length] - 1].color;
rlm@83: var clrs = [];
rlm@83: for (var i = 0, ii = dots[length]; i < ii; i++) {
rlm@83: dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
rlm@83: }
rlm@83: fill.colors && (fill.colors.value = clrs[length] ? clrs[join]() : "0% " + fill.color);
rlm@83: if (type == "radial") {
rlm@83: fill.type = "gradientradial";
rlm@83: fill.focus = "100%";
rlm@83: fill.focussize = fxfy;
rlm@83: fill.focusposition = fxfy;
rlm@83: } else {
rlm@83: fill.type = "gradient";
rlm@83: fill.angle = (270 - angle) % 360;
rlm@83: }
rlm@83: }
rlm@83: return 1;
rlm@83: };
rlm@83: Element = function (node, group, vml) {
rlm@83: var Rotation = 0,
rlm@83: RotX = 0,
rlm@83: RotY = 0,
rlm@83: Scale = 1;
rlm@83: this[0] = node;
rlm@83: this.id = R._oid++;
rlm@83: this.node = node;
rlm@83: node.raphael = this;
rlm@83: this.X = 0;
rlm@83: this.Y = 0;
rlm@83: this.attrs = {};
rlm@83: this.Group = group;
rlm@83: this.paper = vml;
rlm@83: this._ = {
rlm@83: tx: 0,
rlm@83: ty: 0,
rlm@83: rt: {deg:0},
rlm@83: sx: 1,
rlm@83: sy: 1
rlm@83: };
rlm@83: !vml.bottom && (vml.bottom = this);
rlm@83: this.prev = vml.top;
rlm@83: vml.top && (vml.top.next = this);
rlm@83: vml.top = this;
rlm@83: this.next = null;
rlm@83: };
rlm@83: Element[proto].rotate = function (deg, cx, cy) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (deg == null) {
rlm@83: if (this._.rt.cx) {
rlm@83: return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
rlm@83: }
rlm@83: return this._.rt.deg;
rlm@83: }
rlm@83: deg = Str(deg)[split](separator);
rlm@83: if (deg[length] - 1) {
rlm@83: cx = toFloat(deg[1]);
rlm@83: cy = toFloat(deg[2]);
rlm@83: }
rlm@83: deg = toFloat(deg[0]);
rlm@83: if (cx != null) {
rlm@83: this._.rt.deg = deg;
rlm@83: } else {
rlm@83: this._.rt.deg += deg;
rlm@83: }
rlm@83: cy == null && (cx = null);
rlm@83: this._.rt.cx = cx;
rlm@83: this._.rt.cy = cy;
rlm@83: this.setBox(this.attrs, cx, cy);
rlm@83: this.Group.style.rotation = this._.rt.deg;
rlm@83: // gradient fix for rotation. TODO
rlm@83: // var fill = (this.shape || this.node).getElementsByTagName(fillString);
rlm@83: // fill = fill[0] || {};
rlm@83: // var b = ((360 - this._.rt.deg) - 270) % 360;
rlm@83: // !R.is(fill.angle, "undefined") && (fill.angle = b);
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].setBox = function (params, cx, cy) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: var gs = this.Group.style,
rlm@83: os = (this.shape && this.shape.style) || this.node.style;
rlm@83: params = params || {};
rlm@83: for (var i in params) if (params[has](i)) {
rlm@83: this.attrs[i] = params[i];
rlm@83: }
rlm@83: cx = cx || this._.rt.cx;
rlm@83: cy = cy || this._.rt.cy;
rlm@83: var attr = this.attrs,
rlm@83: x,
rlm@83: y,
rlm@83: w,
rlm@83: h;
rlm@83: switch (this.type) {
rlm@83: case "circle":
rlm@83: x = attr.cx - attr.r;
rlm@83: y = attr.cy - attr.r;
rlm@83: w = h = attr.r * 2;
rlm@83: break;
rlm@83: case "ellipse":
rlm@83: x = attr.cx - attr.rx;
rlm@83: y = attr.cy - attr.ry;
rlm@83: w = attr.rx * 2;
rlm@83: h = attr.ry * 2;
rlm@83: break;
rlm@83: case "image":
rlm@83: x = +attr.x;
rlm@83: y = +attr.y;
rlm@83: w = attr.width || 0;
rlm@83: h = attr.height || 0;
rlm@83: break;
rlm@83: case "text":
rlm@83: this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
rlm@83: x = attr.x - round(this.W / 2);
rlm@83: y = attr.y - this.H / 2;
rlm@83: w = this.W;
rlm@83: h = this.H;
rlm@83: break;
rlm@83: case "rect":
rlm@83: case "path":
rlm@83: if (!this.attrs.path) {
rlm@83: x = 0;
rlm@83: y = 0;
rlm@83: w = this.paper.width;
rlm@83: h = this.paper.height;
rlm@83: } else {
rlm@83: var dim = pathDimensions(this.attrs.path);
rlm@83: x = dim.x;
rlm@83: y = dim.y;
rlm@83: w = dim.width;
rlm@83: h = dim.height;
rlm@83: }
rlm@83: break;
rlm@83: default:
rlm@83: x = 0;
rlm@83: y = 0;
rlm@83: w = this.paper.width;
rlm@83: h = this.paper.height;
rlm@83: break;
rlm@83: }
rlm@83: cx = (cx == null) ? x + w / 2 : cx;
rlm@83: cy = (cy == null) ? y + h / 2 : cy;
rlm@83: var left = cx - this.paper.width / 2,
rlm@83: top = cy - this.paper.height / 2, t;
rlm@83: gs.left != (t = left + "px") && (gs.left = t);
rlm@83: gs.top != (t = top + "px") && (gs.top = t);
rlm@83: this.X = pathlike[has](this.type) ? -left : x;
rlm@83: this.Y = pathlike[has](this.type) ? -top : y;
rlm@83: this.W = w;
rlm@83: this.H = h;
rlm@83: if (pathlike[has](this.type)) {
rlm@83: os.left != (t = -left * zoom + "px") && (os.left = t);
rlm@83: os.top != (t = -top * zoom + "px") && (os.top = t);
rlm@83: } else if (this.type == "text") {
rlm@83: os.left != (t = -left + "px") && (os.left = t);
rlm@83: os.top != (t = -top + "px") && (os.top = t);
rlm@83: } else {
rlm@83: gs.width != (t = this.paper.width + "px") && (gs.width = t);
rlm@83: gs.height != (t = this.paper.height + "px") && (gs.height = t);
rlm@83: os.left != (t = x - left + "px") && (os.left = t);
rlm@83: os.top != (t = y - top + "px") && (os.top = t);
rlm@83: os.width != (t = w + "px") && (os.width = t);
rlm@83: os.height != (t = h + "px") && (os.height = t);
rlm@83: }
rlm@83: };
rlm@83: Element[proto].hide = function () {
rlm@83: !this.removed && (this.Group.style.display = "none");
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].show = function () {
rlm@83: !this.removed && (this.Group.style.display = "block");
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].getBBox = function () {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (pathlike[has](this.type)) {
rlm@83: return pathDimensions(this.attrs.path);
rlm@83: }
rlm@83: return {
rlm@83: x: this.X + (this.bbx || 0),
rlm@83: y: this.Y,
rlm@83: width: this.W,
rlm@83: height: this.H
rlm@83: };
rlm@83: };
rlm@83: Element[proto].remove = function () {
rlm@83: if (this.removed) {
rlm@83: return;
rlm@83: }
rlm@83: tear(this, this.paper);
rlm@83: this.node.parentNode.removeChild(this.node);
rlm@83: this.Group.parentNode.removeChild(this.Group);
rlm@83: this.shape && this.shape.parentNode.removeChild(this.shape);
rlm@83: for (var i in this) {
rlm@83: delete this[i];
rlm@83: }
rlm@83: this.removed = true;
rlm@83: };
rlm@83: Element[proto].attr = function (name, value) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (name == null) {
rlm@83: var res = {};
rlm@83: for (var i in this.attrs) if (this.attrs[has](i)) {
rlm@83: res[i] = this.attrs[i];
rlm@83: }
rlm@83: this._.rt.deg && (res.rotation = this.rotate());
rlm@83: (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
rlm@83: res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
rlm@83: return res;
rlm@83: }
rlm@83: if (value == null && R.is(name, string)) {
rlm@83: if (name == "translation") {
rlm@83: return translate.call(this);
rlm@83: }
rlm@83: if (name == "rotation") {
rlm@83: return this.rotate();
rlm@83: }
rlm@83: if (name == "scale") {
rlm@83: return this.scale();
rlm@83: }
rlm@83: if (name == fillString && this.attrs.fill == "none" && this.attrs.gradient) {
rlm@83: return this.attrs.gradient;
rlm@83: }
rlm@83: return this.attrs[name];
rlm@83: }
rlm@83: if (this.attrs && value == null && R.is(name, array)) {
rlm@83: var ii, values = {};
rlm@83: for (i = 0, ii = name[length]; i < ii; i++) {
rlm@83: values[name[i]] = this.attr(name[i]);
rlm@83: }
rlm@83: return values;
rlm@83: }
rlm@83: var params;
rlm@83: if (value != null) {
rlm@83: params = {};
rlm@83: params[name] = value;
rlm@83: }
rlm@83: value == null && R.is(name, "object") && (params = name);
rlm@83: if (params) {
rlm@83: if (params.text && this.type == "text") {
rlm@83: this.node.string = params.text;
rlm@83: }
rlm@83: setFillAndStroke(this, params);
rlm@83: if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || Str(params.gradient).charAt() != "r")) {
rlm@83: addGradientFill(this, params.gradient);
rlm@83: }
rlm@83: (!pathlike[has](this.type) || this._.rt.deg) && this.setBox(this.attrs);
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].toFront = function () {
rlm@83: !this.removed && this.Group.parentNode[appendChild](this.Group);
rlm@83: this.paper.top != this && tofront(this, this.paper);
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].toBack = function () {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (this.Group.parentNode.firstChild != this.Group) {
rlm@83: this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
rlm@83: toback(this, this.paper);
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].insertAfter = function (element) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (element.constructor == Set) {
rlm@83: element = element[element.length];
rlm@83: }
rlm@83: if (element.Group.nextSibling) {
rlm@83: element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
rlm@83: } else {
rlm@83: element.Group.parentNode[appendChild](this.Group);
rlm@83: }
rlm@83: insertafter(this, element, this.paper);
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].insertBefore = function (element) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (element.constructor == Set) {
rlm@83: element = element[0];
rlm@83: }
rlm@83: element.Group.parentNode.insertBefore(this.Group, element.Group);
rlm@83: insertbefore(this, element, this.paper);
rlm@83: return this;
rlm@83: };
rlm@83: var blurregexp = / progid:\S+Blur\([^\)]+\)/g;
rlm@83: Element[proto].blur = function (size) {
rlm@83: var s = this.node.runtimeStyle,
rlm@83: f = s.filter;
rlm@83: f = f.replace(blurregexp, E);
rlm@83: if (+size !== 0) {
rlm@83: this.attrs.blur = size;
rlm@83: s.filter = f + S + ms + ".Blur(pixelradius=" + (+size || 1.5) + ")";
rlm@83: s.margin = R.format("-{0}px 0 0 -{0}px", round(+size || 1.5));
rlm@83: } else {
rlm@83: s.filter = f;
rlm@83: s.margin = 0;
rlm@83: delete this.attrs.blur;
rlm@83: }
rlm@83: };
rlm@83:
rlm@83: theCircle = function (vml, x, y, r) {
rlm@83: var g = createNode("group"),
rlm@83: o = createNode("oval"),
rlm@83: ol = o.style;
rlm@83: g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@83: g.coordsize = coordsize;
rlm@83: g.coordorigin = vml.coordorigin;
rlm@83: g[appendChild](o);
rlm@83: var res = new Element(o, g, vml);
rlm@83: res.type = "circle";
rlm@83: setFillAndStroke(res, {stroke: "#000", fill: "none"});
rlm@83: res.attrs.cx = x;
rlm@83: res.attrs.cy = y;
rlm@83: res.attrs.r = r;
rlm@83: res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
rlm@83: vml.canvas[appendChild](g);
rlm@83: return res;
rlm@83: };
rlm@83: function rectPath(x, y, w, h, r) {
rlm@83: if (r) {
rlm@83: return R.format("M{0},{1}l{2},0a{3},{3},0,0,1,{3},{3}l0,{5}a{3},{3},0,0,1,{4},{3}l{6},0a{3},{3},0,0,1,{4},{4}l0,{7}a{3},{3},0,0,1,{3},{4}z", x + r, y, w - r * 2, r, -r, h - r * 2, r * 2 - w, r * 2 - h);
rlm@83: } else {
rlm@83: return R.format("M{0},{1}l{2},0,0,{3},{4},0z", x, y, w, h, -w);
rlm@83: }
rlm@83: }
rlm@83: theRect = function (vml, x, y, w, h, r) {
rlm@83: var path = rectPath(x, y, w, h, r),
rlm@83: res = vml.path(path),
rlm@83: a = res.attrs;
rlm@83: res.X = a.x = x;
rlm@83: res.Y = a.y = y;
rlm@83: res.W = a.width = w;
rlm@83: res.H = a.height = h;
rlm@83: a.r = r;
rlm@83: a.path = path;
rlm@83: res.type = "rect";
rlm@83: return res;
rlm@83: };
rlm@83: theEllipse = function (vml, x, y, rx, ry) {
rlm@83: var g = createNode("group"),
rlm@83: o = createNode("oval"),
rlm@83: ol = o.style;
rlm@83: g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@83: g.coordsize = coordsize;
rlm@83: g.coordorigin = vml.coordorigin;
rlm@83: g[appendChild](o);
rlm@83: var res = new Element(o, g, vml);
rlm@83: res.type = "ellipse";
rlm@83: setFillAndStroke(res, {stroke: "#000"});
rlm@83: res.attrs.cx = x;
rlm@83: res.attrs.cy = y;
rlm@83: res.attrs.rx = rx;
rlm@83: res.attrs.ry = ry;
rlm@83: res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
rlm@83: vml.canvas[appendChild](g);
rlm@83: return res;
rlm@83: };
rlm@83: theImage = function (vml, src, x, y, w, h) {
rlm@83: var g = createNode("group"),
rlm@83: o = createNode("image"),
rlm@83: ol = o.style;
rlm@83: g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@83: g.coordsize = coordsize;
rlm@83: g.coordorigin = vml.coordorigin;
rlm@83: o.src = src;
rlm@83: g[appendChild](o);
rlm@83: var res = new Element(o, g, vml);
rlm@83: res.type = "image";
rlm@83: res.attrs.src = src;
rlm@83: res.attrs.x = x;
rlm@83: res.attrs.y = y;
rlm@83: res.attrs.w = w;
rlm@83: res.attrs.h = h;
rlm@83: res.setBox({x: x, y: y, width: w, height: h});
rlm@83: vml.canvas[appendChild](g);
rlm@83: return res;
rlm@83: };
rlm@83: theText = function (vml, x, y, text) {
rlm@83: var g = createNode("group"),
rlm@83: el = createNode("shape"),
rlm@83: ol = el.style,
rlm@83: path = createNode("path"),
rlm@83: ps = path.style,
rlm@83: o = createNode("textpath");
rlm@83: g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@83: g.coordsize = coordsize;
rlm@83: g.coordorigin = vml.coordorigin;
rlm@83: path.v = R.format("m{0},{1}l{2},{1}", round(x * 10), round(y * 10), round(x * 10) + 1);
rlm@83: path.textpathok = true;
rlm@83: ol.width = vml.width;
rlm@83: ol.height = vml.height;
rlm@83: o.string = Str(text);
rlm@83: o.on = true;
rlm@83: el[appendChild](o);
rlm@83: el[appendChild](path);
rlm@83: g[appendChild](el);
rlm@83: var res = new Element(o, g, vml);
rlm@83: res.shape = el;
rlm@83: res.textpath = path;
rlm@83: res.type = "text";
rlm@83: res.attrs.text = text;
rlm@83: res.attrs.x = x;
rlm@83: res.attrs.y = y;
rlm@83: res.attrs.w = 1;
rlm@83: res.attrs.h = 1;
rlm@83: setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
rlm@83: res.setBox();
rlm@83: vml.canvas[appendChild](g);
rlm@83: return res;
rlm@83: };
rlm@83: setSize = function (width, height) {
rlm@83: var cs = this.canvas.style;
rlm@83: width == +width && (width += "px");
rlm@83: height == +height && (height += "px");
rlm@83: cs.width = width;
rlm@83: cs.height = height;
rlm@83: cs.clip = "rect(0 " + width + " " + height + " 0)";
rlm@83: return this;
rlm@83: };
rlm@83: var createNode;
rlm@83: doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
rlm@83: try {
rlm@83: !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
rlm@83: createNode = function (tagName) {
rlm@83: return doc.createElement('');
rlm@83: };
rlm@83: } catch (e) {
rlm@83: createNode = function (tagName) {
rlm@83: return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
rlm@83: };
rlm@83: }
rlm@83: create = function () {
rlm@83: var con = getContainer[apply](0, arguments),
rlm@83: container = con.container,
rlm@83: height = con.height,
rlm@83: s,
rlm@83: width = con.width,
rlm@83: x = con.x,
rlm@83: y = con.y;
rlm@83: if (!container) {
rlm@83: throw new Error("VML container not found.");
rlm@83: }
rlm@83: var res = new Paper,
rlm@83: c = res.canvas = doc.createElement("div"),
rlm@83: cs = c.style;
rlm@83: x = x || 0;
rlm@83: y = y || 0;
rlm@83: width = width || 512;
rlm@83: height = height || 342;
rlm@83: width == +width && (width += "px");
rlm@83: height == +height && (height += "px");
rlm@83: res.width = 1e3;
rlm@83: res.height = 1e3;
rlm@83: res.coordsize = zoom * 1e3 + S + zoom * 1e3;
rlm@83: res.coordorigin = "0 0";
rlm@83: res.span = doc.createElement("span");
rlm@83: res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
rlm@83: c[appendChild](res.span);
rlm@83: cs.cssText = R.format("width:{0};height:{1};display:inline-block;position:relative;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
rlm@83: if (container == 1) {
rlm@83: doc.body[appendChild](c);
rlm@83: cs.left = x + "px";
rlm@83: cs.top = y + "px";
rlm@83: cs.position = "absolute";
rlm@83: } else {
rlm@83: if (container.firstChild) {
rlm@83: container.insertBefore(c, container.firstChild);
rlm@83: } else {
rlm@83: container[appendChild](c);
rlm@83: }
rlm@83: }
rlm@83: plugins.call(res, res, R.fn);
rlm@83: return res;
rlm@83: };
rlm@83: Paper[proto].clear = function () {
rlm@83: this.canvas.innerHTML = E;
rlm@83: this.span = doc.createElement("span");
rlm@83: this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
rlm@83: this.canvas[appendChild](this.span);
rlm@83: this.bottom = this.top = null;
rlm@83: };
rlm@83: Paper[proto].remove = function () {
rlm@83: this.canvas.parentNode.removeChild(this.canvas);
rlm@83: for (var i in this) {
rlm@83: this[i] = removed(i);
rlm@83: }
rlm@83: return true;
rlm@83: };
rlm@83: }
rlm@83:
rlm@83: // rest
rlm@83: // WebKit rendering bug workaround method
rlm@83: if ((navigator.vendor == "Apple Computer, Inc.") && (navigator.userAgent.match(/Version\/(.*?)\s/)[1] < 4 || win.navigator.platform.slice(0, 2) == "iP")) {
rlm@83: Paper[proto].safari = function () {
rlm@83: var rect = this.rect(-99, -99, this.width + 99, this.height + 99).attr({stroke: "none"});
rlm@83: win.setTimeout(function () {rect.remove();});
rlm@83: };
rlm@83: } else {
rlm@83: Paper[proto].safari = function () {};
rlm@83: }
rlm@83:
rlm@83: // Events
rlm@83: var preventDefault = function () {
rlm@83: this.returnValue = false;
rlm@83: },
rlm@83: preventTouch = function () {
rlm@83: return this.originalEvent.preventDefault();
rlm@83: },
rlm@83: stopPropagation = function () {
rlm@83: this.cancelBubble = true;
rlm@83: },
rlm@83: stopTouch = function () {
rlm@83: return this.originalEvent.stopPropagation();
rlm@83: },
rlm@83: addEvent = (function () {
rlm@83: if (doc.addEventListener) {
rlm@83: return function (obj, type, fn, element) {
rlm@83: var realName = supportsTouch && touchMap[type] ? touchMap[type] : type;
rlm@83: var f = function (e) {
rlm@83: if (supportsTouch && touchMap[has](type)) {
rlm@83: for (var i = 0, ii = e.targetTouches && e.targetTouches.length; i < ii; i++) {
rlm@83: if (e.targetTouches[i].target == obj) {
rlm@83: var olde = e;
rlm@83: e = e.targetTouches[i];
rlm@83: e.originalEvent = olde;
rlm@83: e.preventDefault = preventTouch;
rlm@83: e.stopPropagation = stopTouch;
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: return fn.call(element, e);
rlm@83: };
rlm@83: obj.addEventListener(realName, f, false);
rlm@83: return function () {
rlm@83: obj.removeEventListener(realName, f, false);
rlm@83: return true;
rlm@83: };
rlm@83: };
rlm@83: } else if (doc.attachEvent) {
rlm@83: return function (obj, type, fn, element) {
rlm@83: var f = function (e) {
rlm@83: e = e || win.event;
rlm@83: e.preventDefault = e.preventDefault || preventDefault;
rlm@83: e.stopPropagation = e.stopPropagation || stopPropagation;
rlm@83: return fn.call(element, e);
rlm@83: };
rlm@83: obj.attachEvent("on" + type, f);
rlm@83: var detacher = function () {
rlm@83: obj.detachEvent("on" + type, f);
rlm@83: return true;
rlm@83: };
rlm@83: return detacher;
rlm@83: };
rlm@83: }
rlm@83: })(),
rlm@83: drag = [],
rlm@83: dragMove = function (e) {
rlm@83: var x = e.clientX,
rlm@83: y = e.clientY,
rlm@83: dragi,
rlm@83: j = drag.length;
rlm@83: while (j--) {
rlm@83: dragi = drag[j];
rlm@83: if (supportsTouch) {
rlm@83: var i = e.touches.length,
rlm@83: touch;
rlm@83: while (i--) {
rlm@83: touch = e.touches[i];
rlm@83: if (touch.identifier == dragi.el._drag.id) {
rlm@83: x = touch.clientX;
rlm@83: y = touch.clientY;
rlm@83: (e.originalEvent ? e.originalEvent : e).preventDefault();
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: } else {
rlm@83: e.preventDefault();
rlm@83: }
rlm@83: dragi.move && dragi.move.call(dragi.el, x - dragi.el._drag.x, y - dragi.el._drag.y, x, y);
rlm@83: }
rlm@83: },
rlm@83: dragUp = function () {
rlm@83: R.unmousemove(dragMove).unmouseup(dragUp);
rlm@83: var i = drag.length,
rlm@83: dragi;
rlm@83: while (i--) {
rlm@83: dragi = drag[i];
rlm@83: dragi.el._drag = {};
rlm@83: dragi.end && dragi.end.call(dragi.el);
rlm@83: }
rlm@83: drag = [];
rlm@83: };
rlm@83: for (var i = events[length]; i--;) {
rlm@83: (function (eventName) {
rlm@83: R[eventName] = Element[proto][eventName] = function (fn) {
rlm@83: if (R.is(fn, "function")) {
rlm@83: this.events = this.events || [];
rlm@83: this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node || doc, eventName, fn, this)});
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: R["un" + eventName] = Element[proto]["un" + eventName] = function (fn) {
rlm@83: var events = this.events,
rlm@83: l = events[length];
rlm@83: while (l--) if (events[l].name == eventName && events[l].f == fn) {
rlm@83: events[l].unbind();
rlm@83: events.splice(l, 1);
rlm@83: !events.length && delete this.events;
rlm@83: return this;
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: })(events[i]);
rlm@83: }
rlm@83: Element[proto].hover = function (f_in, f_out) {
rlm@83: return this.mouseover(f_in).mouseout(f_out);
rlm@83: };
rlm@83: Element[proto].unhover = function (f_in, f_out) {
rlm@83: return this.unmouseover(f_in).unmouseout(f_out);
rlm@83: };
rlm@83: Element[proto].drag = function (onmove, onstart, onend) {
rlm@83: this._drag = {};
rlm@83: this.mousedown(function (e) {
rlm@83: (e.originalEvent || e).preventDefault();
rlm@83: this._drag.x = e.clientX;
rlm@83: this._drag.y = e.clientY;
rlm@83: this._drag.id = e.identifier;
rlm@83: onstart && onstart.call(this, e.clientX, e.clientY);
rlm@83: !drag.length && R.mousemove(dragMove).mouseup(dragUp);
rlm@83: drag.push({el: this, move: onmove, end: onend});
rlm@83: });
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].undrag = function (onmove, onstart, onend) {
rlm@83: var i = drag.length;
rlm@83: while (i--) {
rlm@83: drag[i].el == this && (drag[i].move == onmove && drag[i].end == onend) && drag.splice(i, 1);
rlm@83: !drag.length && R.unmousemove(dragMove).unmouseup(dragUp);
rlm@83: }
rlm@83: };
rlm@83: Paper[proto].circle = function (x, y, r) {
rlm@83: return theCircle(this, x || 0, y || 0, r || 0);
rlm@83: };
rlm@83: Paper[proto].rect = function (x, y, w, h, r) {
rlm@83: return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
rlm@83: };
rlm@83: Paper[proto].ellipse = function (x, y, rx, ry) {
rlm@83: return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
rlm@83: };
rlm@83: Paper[proto].path = function (pathString) {
rlm@83: pathString && !R.is(pathString, string) && !R.is(pathString[0], array) && (pathString += E);
rlm@83: return thePath(R.format[apply](R, arguments), this);
rlm@83: };
rlm@83: Paper[proto].image = function (src, x, y, w, h) {
rlm@83: return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
rlm@83: };
rlm@83: Paper[proto].text = function (x, y, text) {
rlm@83: return theText(this, x || 0, y || 0, text || E);
rlm@83: };
rlm@83: Paper[proto].set = function (itemsArray) {
rlm@83: arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
rlm@83: return new Set(itemsArray);
rlm@83: };
rlm@83: Paper[proto].setSize = setSize;
rlm@83: Paper[proto].top = Paper[proto].bottom = null;
rlm@83: Paper[proto].raphael = R;
rlm@83: function x_y() {
rlm@83: return this.x + S + this.y;
rlm@83: }
rlm@83: Element[proto].resetScale = function () {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: this._.sx = 1;
rlm@83: this._.sy = 1;
rlm@83: this.attrs.scale = "1 1";
rlm@83: };
rlm@83: Element[proto].scale = function (x, y, cx, cy) {
rlm@83: if (this.removed) {
rlm@83: return this;
rlm@83: }
rlm@83: if (x == null && y == null) {
rlm@83: return {
rlm@83: x: this._.sx,
rlm@83: y: this._.sy,
rlm@83: toString: x_y
rlm@83: };
rlm@83: }
rlm@83: y = y || x;
rlm@83: !+y && (y = x);
rlm@83: var dx,
rlm@83: dy,
rlm@83: dcx,
rlm@83: dcy,
rlm@83: a = this.attrs;
rlm@83: if (x != 0) {
rlm@83: var bb = this.getBBox(),
rlm@83: rcx = bb.x + bb.width / 2,
rlm@83: rcy = bb.y + bb.height / 2,
rlm@83: kx = x / this._.sx,
rlm@83: ky = y / this._.sy;
rlm@83: cx = (+cx || cx == 0) ? cx : rcx;
rlm@83: cy = (+cy || cy == 0) ? cy : rcy;
rlm@83: var dirx = ~~(x / math.abs(x)),
rlm@83: diry = ~~(y / math.abs(y)),
rlm@83: s = this.node.style,
rlm@83: ncx = cx + (rcx - cx) * kx,
rlm@83: ncy = cy + (rcy - cy) * ky;
rlm@83: switch (this.type) {
rlm@83: case "rect":
rlm@83: case "image":
rlm@83: var neww = a.width * dirx * kx,
rlm@83: newh = a.height * diry * ky;
rlm@83: this.attr({
rlm@83: height: newh,
rlm@83: r: a.r * mmin(dirx * kx, diry * ky),
rlm@83: width: neww,
rlm@83: x: ncx - neww / 2,
rlm@83: y: ncy - newh / 2
rlm@83: });
rlm@83: break;
rlm@83: case "circle":
rlm@83: case "ellipse":
rlm@83: this.attr({
rlm@83: rx: a.rx * dirx * kx,
rlm@83: ry: a.ry * diry * ky,
rlm@83: r: a.r * mmin(dirx * kx, diry * ky),
rlm@83: cx: ncx,
rlm@83: cy: ncy
rlm@83: });
rlm@83: break;
rlm@83: case "text":
rlm@83: this.attr({
rlm@83: x: ncx,
rlm@83: y: ncy
rlm@83: });
rlm@83: break;
rlm@83: case "path":
rlm@83: var path = pathToRelative(a.path),
rlm@83: skip = true;
rlm@83: for (var i = 0, ii = path[length]; i < ii; i++) {
rlm@83: var p = path[i],
rlm@83: P0 = upperCase.call(p[0]);
rlm@83: if (P0 == "M" && skip) {
rlm@83: continue;
rlm@83: } else {
rlm@83: skip = false;
rlm@83: }
rlm@83: if (P0 == "A") {
rlm@83: p[path[i][length] - 2] *= kx;
rlm@83: p[path[i][length] - 1] *= ky;
rlm@83: p[1] *= dirx * kx;
rlm@83: p[2] *= diry * ky;
rlm@83: p[5] = +!(dirx + diry ? !+p[5] : +p[5]);
rlm@83: } else if (P0 == "H") {
rlm@83: for (var j = 1, jj = p[length]; j < jj; j++) {
rlm@83: p[j] *= kx;
rlm@83: }
rlm@83: } else if (P0 == "V") {
rlm@83: for (j = 1, jj = p[length]; j < jj; j++) {
rlm@83: p[j] *= ky;
rlm@83: }
rlm@83: } else {
rlm@83: for (j = 1, jj = p[length]; j < jj; j++) {
rlm@83: p[j] *= (j % 2) ? kx : ky;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: var dim2 = pathDimensions(path);
rlm@83: dx = ncx - dim2.x - dim2.width / 2;
rlm@83: dy = ncy - dim2.y - dim2.height / 2;
rlm@83: path[0][1] += dx;
rlm@83: path[0][2] += dy;
rlm@83: this.attr({path: path});
rlm@83: break;
rlm@83: }
rlm@83: if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
rlm@83: if (this.transformations) {
rlm@83: this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
rlm@83: this.node[setAttribute]("transform", this.transformations[join](S));
rlm@83: dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
rlm@83: dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
rlm@83: this.attr({x: dx, y: dy});
rlm@83: a.fx = dirx - 1;
rlm@83: a.fy = diry - 1;
rlm@83: } else {
rlm@83: this.node.filterMatrix = ms + ".Matrix(M11="[concat](dirx,
rlm@83: ", M12=0, M21=0, M22=", diry,
rlm@83: ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
rlm@83: s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
rlm@83: }
rlm@83: } else {
rlm@83: if (this.transformations) {
rlm@83: this.transformations[2] = E;
rlm@83: this.node[setAttribute]("transform", this.transformations[join](S));
rlm@83: a.fx = 0;
rlm@83: a.fy = 0;
rlm@83: } else {
rlm@83: this.node.filterMatrix = E;
rlm@83: s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
rlm@83: }
rlm@83: }
rlm@83: a.scale = [x, y, cx, cy][join](S);
rlm@83: this._.sx = x;
rlm@83: this._.sy = y;
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].clone = function () {
rlm@83: if (this.removed) {
rlm@83: return null;
rlm@83: }
rlm@83: var attr = this.attr();
rlm@83: delete attr.scale;
rlm@83: delete attr.translation;
rlm@83: return this.paper[this.type]().attr(attr);
rlm@83: };
rlm@83: var getPointAtSegmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, length) {
rlm@83: var len = 0,
rlm@83: old;
rlm@83: for (var i = 0; i < 1.01; i+=.01) {
rlm@83: var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
rlm@83: i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
rlm@83: if (len >= length) {
rlm@83: return dot;
rlm@83: }
rlm@83: old = dot;
rlm@83: }
rlm@83: }),
rlm@83: getLengthFactory = function (istotal, subpath) {
rlm@83: return function (path, length, onlystart) {
rlm@83: path = path2curve(path);
rlm@83: var x, y, p, l, sp = "", subpaths = {}, point,
rlm@83: len = 0;
rlm@83: for (var i = 0, ii = path.length; i < ii; i++) {
rlm@83: p = path[i];
rlm@83: if (p[0] == "M") {
rlm@83: x = +p[1];
rlm@83: y = +p[2];
rlm@83: } else {
rlm@83: l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
rlm@83: if (len + l > length) {
rlm@83: if (subpath && !subpaths.start) {
rlm@83: point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
rlm@83: sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
rlm@83: if (onlystart) {return sp;}
rlm@83: subpaths.start = sp;
rlm@83: sp = ["M", point.x, point.y + "C", point.n.x, point.n.y, point.end.x, point.end.y, p[5], p[6]][join]();
rlm@83: len += l;
rlm@83: x = +p[5];
rlm@83: y = +p[6];
rlm@83: continue;
rlm@83: }
rlm@83: if (!istotal && !subpath) {
rlm@83: point = getPointAtSegmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6], length - len);
rlm@83: return {x: point.x, y: point.y, alpha: point.alpha};
rlm@83: }
rlm@83: }
rlm@83: len += l;
rlm@83: x = +p[5];
rlm@83: y = +p[6];
rlm@83: }
rlm@83: sp += p;
rlm@83: }
rlm@83: subpaths.end = sp;
rlm@83: point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
rlm@83: point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
rlm@83: return point;
rlm@83: };
rlm@83: },
rlm@83: segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
rlm@83: var old = {x: 0, y: 0},
rlm@83: len = 0;
rlm@83: for (var i = 0; i < 1.01; i+=.01) {
rlm@83: var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
rlm@83: i && (len += pow(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2), .5));
rlm@83: old = dot;
rlm@83: }
rlm@83: return len;
rlm@83: });
rlm@83: var getTotalLength = getLengthFactory(1),
rlm@83: getPointAtLength = getLengthFactory(),
rlm@83: getSubpathsAtLength = getLengthFactory(0, 1);
rlm@83: Element[proto].getTotalLength = function () {
rlm@83: if (this.type != "path") {return;}
rlm@83: if (this.node.getTotalLength) {
rlm@83: return this.node.getTotalLength();
rlm@83: }
rlm@83: return getTotalLength(this.attrs.path);
rlm@83: };
rlm@83: Element[proto].getPointAtLength = function (length) {
rlm@83: if (this.type != "path") {return;}
rlm@83: if (this.node.getPointAtLength) {
rlm@83: return this.node.getPointAtLength(length);
rlm@83: }
rlm@83: return getPointAtLength(this.attrs.path, length);
rlm@83: };
rlm@83: Element[proto].getSubpath = function (from, to) {
rlm@83: if (this.type != "path") {return;}
rlm@83: if (math.abs(this.getTotalLength() - to) < 1e-6) {
rlm@83: return getSubpathsAtLength(this.attrs.path, from).end;
rlm@83: }
rlm@83: var a = getSubpathsAtLength(this.attrs.path, to, 1);
rlm@83: return from ? getSubpathsAtLength(a, from).end : a;
rlm@83: };
rlm@83:
rlm@83: // animation easing formulas
rlm@83: R.easing_formulas = {
rlm@83: linear: function (n) {
rlm@83: return n;
rlm@83: },
rlm@83: "<": function (n) {
rlm@83: return pow(n, 3);
rlm@83: },
rlm@83: ">": function (n) {
rlm@83: return pow(n - 1, 3) + 1;
rlm@83: },
rlm@83: "<>": function (n) {
rlm@83: n = n * 2;
rlm@83: if (n < 1) {
rlm@83: return pow(n, 3) / 2;
rlm@83: }
rlm@83: n -= 2;
rlm@83: return (pow(n, 3) + 2) / 2;
rlm@83: },
rlm@83: backIn: function (n) {
rlm@83: var s = 1.70158;
rlm@83: return n * n * ((s + 1) * n - s);
rlm@83: },
rlm@83: backOut: function (n) {
rlm@83: n = n - 1;
rlm@83: var s = 1.70158;
rlm@83: return n * n * ((s + 1) * n + s) + 1;
rlm@83: },
rlm@83: elastic: function (n) {
rlm@83: if (n == 0 || n == 1) {
rlm@83: return n;
rlm@83: }
rlm@83: var p = .3,
rlm@83: s = p / 4;
rlm@83: return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
rlm@83: },
rlm@83: bounce: function (n) {
rlm@83: var s = 7.5625,
rlm@83: p = 2.75,
rlm@83: l;
rlm@83: if (n < (1 / p)) {
rlm@83: l = s * n * n;
rlm@83: } else {
rlm@83: if (n < (2 / p)) {
rlm@83: n -= (1.5 / p);
rlm@83: l = s * n * n + .75;
rlm@83: } else {
rlm@83: if (n < (2.5 / p)) {
rlm@83: n -= (2.25 / p);
rlm@83: l = s * n * n + .9375;
rlm@83: } else {
rlm@83: n -= (2.625 / p);
rlm@83: l = s * n * n + .984375;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: return l;
rlm@83: }
rlm@83: };
rlm@83:
rlm@83: var animationElements = {length : 0},
rlm@83: animation = function () {
rlm@83: var Now = +new Date;
rlm@83: for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
rlm@83: var e = animationElements[l];
rlm@83: if (e.stop || e.el.removed) {
rlm@83: delete animationElements[l];
rlm@83: animationElements[length]--;
rlm@83: continue;
rlm@83: }
rlm@83: var time = Now - e.start,
rlm@83: ms = e.ms,
rlm@83: easing = e.easing,
rlm@83: from = e.from,
rlm@83: diff = e.diff,
rlm@83: to = e.to,
rlm@83: t = e.t,
rlm@83: prev = e.prev || 0,
rlm@83: that = e.el,
rlm@83: callback = e.callback,
rlm@83: set = {},
rlm@83: now;
rlm@83: if (time < ms) {
rlm@83: var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
rlm@83: for (var attr in from) if (from[has](attr)) {
rlm@83: switch (availableAnimAttrs[attr]) {
rlm@83: case "along":
rlm@83: now = pos * ms * diff[attr];
rlm@83: to.back && (now = to.len - now);
rlm@83: var point = getPointAtLength(to[attr], now);
rlm@83: that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
rlm@83: diff.x = point.x;
rlm@83: diff.y = point.y;
rlm@83: that.translate(point.x - diff.sx, point.y - diff.sy);
rlm@83: to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
rlm@83: break;
rlm@83: case nu:
rlm@83: now = +from[attr] + pos * ms * diff[attr];
rlm@83: break;
rlm@83: case "colour":
rlm@83: now = "rgb(" + [
rlm@83: upto255(round(from[attr].r + pos * ms * diff[attr].r)),
rlm@83: upto255(round(from[attr].g + pos * ms * diff[attr].g)),
rlm@83: upto255(round(from[attr].b + pos * ms * diff[attr].b))
rlm@83: ][join](",") + ")";
rlm@83: break;
rlm@83: case "path":
rlm@83: now = [];
rlm@83: for (var i = 0, ii = from[attr][length]; i < ii; i++) {
rlm@83: now[i] = [from[attr][i][0]];
rlm@83: for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
rlm@83: now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
rlm@83: }
rlm@83: now[i] = now[i][join](S);
rlm@83: }
rlm@83: now = now[join](S);
rlm@83: break;
rlm@83: case "csv":
rlm@83: switch (attr) {
rlm@83: case "translation":
rlm@83: var x = diff[attr][0] * (time - prev),
rlm@83: y = diff[attr][1] * (time - prev);
rlm@83: t.x += x;
rlm@83: t.y += y;
rlm@83: now = x + S + y;
rlm@83: break;
rlm@83: case "rotation":
rlm@83: now = +from[attr][0] + pos * ms * diff[attr][0];
rlm@83: from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
rlm@83: break;
rlm@83: case "scale":
rlm@83: now = [+from[attr][0] + pos * ms * diff[attr][0], +from[attr][1] + pos * ms * diff[attr][1], (2 in to[attr] ? to[attr][2] : E), (3 in to[attr] ? to[attr][3] : E)][join](S);
rlm@83: break;
rlm@83: case "clip-rect":
rlm@83: now = [];
rlm@83: i = 4;
rlm@83: while (i--) {
rlm@83: now[i] = +from[attr][i] + pos * ms * diff[attr][i];
rlm@83: }
rlm@83: break;
rlm@83: }
rlm@83: break;
rlm@83: }
rlm@83: set[attr] = now;
rlm@83: }
rlm@83: that.attr(set);
rlm@83: that._run && that._run.call(that);
rlm@83: } else {
rlm@83: if (to.along) {
rlm@83: point = getPointAtLength(to.along, to.len * !to.back);
rlm@83: that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
rlm@83: to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
rlm@83: }
rlm@83: (t.x || t.y) && that.translate(-t.x, -t.y);
rlm@83: to.scale && (to.scale += E);
rlm@83: that.attr(to);
rlm@83: delete animationElements[l];
rlm@83: animationElements[length]--;
rlm@83: that.in_animation = null;
rlm@83: R.is(callback, "function") && callback.call(that);
rlm@83: }
rlm@83: e.prev = time;
rlm@83: }
rlm@83: R.svg && that && that.paper && that.paper.safari();
rlm@83: animationElements[length] && win.setTimeout(animation);
rlm@83: },
rlm@83: upto255 = function (color) {
rlm@83: return mmax(mmin(color, 255), 0);
rlm@83: },
rlm@83: translate = function (x, y) {
rlm@83: if (x == null) {
rlm@83: return {x: this._.tx, y: this._.ty, toString: x_y};
rlm@83: }
rlm@83: this._.tx += +x;
rlm@83: this._.ty += +y;
rlm@83: switch (this.type) {
rlm@83: case "circle":
rlm@83: case "ellipse":
rlm@83: this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
rlm@83: break;
rlm@83: case "rect":
rlm@83: case "image":
rlm@83: case "text":
rlm@83: this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
rlm@83: break;
rlm@83: case "path":
rlm@83: var path = pathToRelative(this.attrs.path);
rlm@83: path[0][1] += +x;
rlm@83: path[0][2] += +y;
rlm@83: this.attr({path: path});
rlm@83: break;
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].animateWith = function (element, params, ms, easing, callback) {
rlm@83: animationElements[element.id] && (params.start = animationElements[element.id].start);
rlm@83: return this.animate(params, ms, easing, callback);
rlm@83: };
rlm@83: Element[proto].animateAlong = along();
rlm@83: Element[proto].animateAlongBack = along(1);
rlm@83: function along(isBack) {
rlm@83: return function (path, ms, rotate, callback) {
rlm@83: var params = {back: isBack};
rlm@83: R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
rlm@83: path && path.constructor == Element && (path = path.attrs.path);
rlm@83: path && (params.along = path);
rlm@83: return this.animate(params, ms, callback);
rlm@83: };
rlm@83: }
rlm@83: Element[proto].onAnimation = function (f) {
rlm@83: this._run = f || 0;
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].animate = function (params, ms, easing, callback) {
rlm@83: if (R.is(easing, "function") || !easing) {
rlm@83: callback = easing || null;
rlm@83: }
rlm@83: var from = {},
rlm@83: to = {},
rlm@83: diff = {};
rlm@83: for (var attr in params) if (params[has](attr)) {
rlm@83: if (availableAnimAttrs[has](attr)) {
rlm@83: from[attr] = this.attr(attr);
rlm@83: (from[attr] == null) && (from[attr] = availableAttrs[attr]);
rlm@83: to[attr] = params[attr];
rlm@83: switch (availableAnimAttrs[attr]) {
rlm@83: case "along":
rlm@83: var len = getTotalLength(params[attr]);
rlm@83: var point = getPointAtLength(params[attr], len * !!params.back);
rlm@83: var bb = this.getBBox();
rlm@83: diff[attr] = len / ms;
rlm@83: diff.tx = bb.x;
rlm@83: diff.ty = bb.y;
rlm@83: diff.sx = point.x;
rlm@83: diff.sy = point.y;
rlm@83: to.rot = params.rot;
rlm@83: to.back = params.back;
rlm@83: to.len = len;
rlm@83: params.rot && (diff.r = toFloat(this.rotate()) || 0);
rlm@83: break;
rlm@83: case nu:
rlm@83: diff[attr] = (to[attr] - from[attr]) / ms;
rlm@83: break;
rlm@83: case "colour":
rlm@83: from[attr] = R.getRGB(from[attr]);
rlm@83: var toColour = R.getRGB(to[attr]);
rlm@83: diff[attr] = {
rlm@83: r: (toColour.r - from[attr].r) / ms,
rlm@83: g: (toColour.g - from[attr].g) / ms,
rlm@83: b: (toColour.b - from[attr].b) / ms
rlm@83: };
rlm@83: break;
rlm@83: case "path":
rlm@83: var pathes = path2curve(from[attr], to[attr]);
rlm@83: from[attr] = pathes[0];
rlm@83: var toPath = pathes[1];
rlm@83: diff[attr] = [];
rlm@83: for (var i = 0, ii = from[attr][length]; i < ii; i++) {
rlm@83: diff[attr][i] = [0];
rlm@83: for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
rlm@83: diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
rlm@83: }
rlm@83: }
rlm@83: break;
rlm@83: case "csv":
rlm@83: var values = Str(params[attr])[split](separator),
rlm@83: from2 = Str(from[attr])[split](separator);
rlm@83: switch (attr) {
rlm@83: case "translation":
rlm@83: from[attr] = [0, 0];
rlm@83: diff[attr] = [values[0] / ms, values[1] / ms];
rlm@83: break;
rlm@83: case "rotation":
rlm@83: from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
rlm@83: diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
rlm@83: break;
rlm@83: case "scale":
rlm@83: params[attr] = values;
rlm@83: from[attr] = Str(from[attr])[split](separator);
rlm@83: diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
rlm@83: break;
rlm@83: case "clip-rect":
rlm@83: from[attr] = Str(from[attr])[split](separator);
rlm@83: diff[attr] = [];
rlm@83: i = 4;
rlm@83: while (i--) {
rlm@83: diff[attr][i] = (values[i] - from[attr][i]) / ms;
rlm@83: }
rlm@83: break;
rlm@83: }
rlm@83: to[attr] = values;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: this.stop();
rlm@83: this.in_animation = 1;
rlm@83: animationElements[this.id] = {
rlm@83: start: params.start || +new Date,
rlm@83: ms: ms,
rlm@83: easing: easing,
rlm@83: from: from,
rlm@83: diff: diff,
rlm@83: to: to,
rlm@83: el: this,
rlm@83: callback: callback,
rlm@83: t: {x: 0, y: 0}
rlm@83: };
rlm@83: ++animationElements[length] == 1 && animation();
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].stop = function () {
rlm@83: animationElements[this.id] && animationElements[length]--;
rlm@83: delete animationElements[this.id];
rlm@83: return this;
rlm@83: };
rlm@83: Element[proto].translate = function (x, y) {
rlm@83: return this.attr({translation: x + " " + y});
rlm@83: };
rlm@83: Element[proto][toString] = function () {
rlm@83: return "Rapha\xebl\u2019s object";
rlm@83: };
rlm@83: R.ae = animationElements;
rlm@83:
rlm@83: // Set
rlm@83: var Set = function (items) {
rlm@83: this.items = [];
rlm@83: this[length] = 0;
rlm@83: this.type = "set";
rlm@83: if (items) {
rlm@83: for (var i = 0, ii = items[length]; i < ii; i++) {
rlm@83: if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
rlm@83: this[this.items[length]] = this.items[this.items[length]] = items[i];
rlm@83: this[length]++;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: };
rlm@83: Set[proto][push] = function () {
rlm@83: var item,
rlm@83: len;
rlm@83: for (var i = 0, ii = arguments[length]; i < ii; i++) {
rlm@83: item = arguments[i];
rlm@83: if (item && (item.constructor == Element || item.constructor == Set)) {
rlm@83: len = this.items[length];
rlm@83: this[len] = this.items[len] = item;
rlm@83: this[length]++;
rlm@83: }
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Set[proto].pop = function () {
rlm@83: delete this[this[length]--];
rlm@83: return this.items.pop();
rlm@83: };
rlm@83: for (var method in Element[proto]) if (Element[proto][has](method)) {
rlm@83: Set[proto][method] = (function (methodname) {
rlm@83: return function () {
rlm@83: for (var i = 0, ii = this.items[length]; i < ii; i++) {
rlm@83: this.items[i][methodname][apply](this.items[i], arguments);
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: })(method);
rlm@83: }
rlm@83: Set[proto].attr = function (name, value) {
rlm@83: if (name && R.is(name, array) && R.is(name[0], "object")) {
rlm@83: for (var j = 0, jj = name[length]; j < jj; j++) {
rlm@83: this.items[j].attr(name[j]);
rlm@83: }
rlm@83: } else {
rlm@83: for (var i = 0, ii = this.items[length]; i < ii; i++) {
rlm@83: this.items[i].attr(name, value);
rlm@83: }
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Set[proto].animate = function (params, ms, easing, callback) {
rlm@83: (R.is(easing, "function") || !easing) && (callback = easing || null);
rlm@83: var len = this.items[length],
rlm@83: i = len,
rlm@83: item,
rlm@83: set = this,
rlm@83: collector;
rlm@83: callback && (collector = function () {
rlm@83: !--len && callback.call(set);
rlm@83: });
rlm@83: easing = R.is(easing, string) ? easing : collector;
rlm@83: item = this.items[--i].animate(params, ms, easing, collector);
rlm@83: while (i--) {
rlm@83: this.items[i].animateWith(item, params, ms, easing, collector);
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Set[proto].insertAfter = function (el) {
rlm@83: var i = this.items[length];
rlm@83: while (i--) {
rlm@83: this.items[i].insertAfter(el);
rlm@83: }
rlm@83: return this;
rlm@83: };
rlm@83: Set[proto].getBBox = function () {
rlm@83: var x = [],
rlm@83: y = [],
rlm@83: w = [],
rlm@83: h = [];
rlm@83: for (var i = this.items[length]; i--;) {
rlm@83: var box = this.items[i].getBBox();
rlm@83: x[push](box.x);
rlm@83: y[push](box.y);
rlm@83: w[push](box.x + box.width);
rlm@83: h[push](box.y + box.height);
rlm@83: }
rlm@83: x = mmin[apply](0, x);
rlm@83: y = mmin[apply](0, y);
rlm@83: return {
rlm@83: x: x,
rlm@83: y: y,
rlm@83: width: mmax[apply](0, w) - x,
rlm@83: height: mmax[apply](0, h) - y
rlm@83: };
rlm@83: };
rlm@83: Set[proto].clone = function (s) {
rlm@83: s = new Set;
rlm@83: for (var i = 0, ii = this.items[length]; i < ii; i++) {
rlm@83: s[push](this.items[i].clone());
rlm@83: }
rlm@83: return s;
rlm@83: };
rlm@83:
rlm@83: R.registerFont = function (font) {
rlm@83: if (!font.face) {
rlm@83: return font;
rlm@83: }
rlm@83: this.fonts = this.fonts || {};
rlm@83: var fontcopy = {
rlm@83: w: font.w,
rlm@83: face: {},
rlm@83: glyphs: {}
rlm@83: },
rlm@83: family = font.face["font-family"];
rlm@83: for (var prop in font.face) if (font.face[has](prop)) {
rlm@83: fontcopy.face[prop] = font.face[prop];
rlm@83: }
rlm@83: if (this.fonts[family]) {
rlm@83: this.fonts[family][push](fontcopy);
rlm@83: } else {
rlm@83: this.fonts[family] = [fontcopy];
rlm@83: }
rlm@83: if (!font.svg) {
rlm@83: fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
rlm@83: for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
rlm@83: var path = font.glyphs[glyph];
rlm@83: fontcopy.glyphs[glyph] = {
rlm@83: w: path.w,
rlm@83: k: {},
rlm@83: d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
rlm@83: return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
rlm@83: }) + "z"
rlm@83: };
rlm@83: if (path.k) {
rlm@83: for (var k in path.k) if (path[has](k)) {
rlm@83: fontcopy.glyphs[glyph].k[k] = path.k[k];
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: return font;
rlm@83: };
rlm@83: Paper[proto].getFont = function (family, weight, style, stretch) {
rlm@83: stretch = stretch || "normal";
rlm@83: style = style || "normal";
rlm@83: weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
rlm@83: if (!R.fonts) {
rlm@83: return;
rlm@83: }
rlm@83: var font = R.fonts[family];
rlm@83: if (!font) {
rlm@83: var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
rlm@83: for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
rlm@83: if (name.test(fontName)) {
rlm@83: font = R.fonts[fontName];
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: var thefont;
rlm@83: if (font) {
rlm@83: for (var i = 0, ii = font[length]; i < ii; i++) {
rlm@83: thefont = font[i];
rlm@83: if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
rlm@83: break;
rlm@83: }
rlm@83: }
rlm@83: }
rlm@83: return thefont;
rlm@83: };
rlm@83: Paper[proto].print = function (x, y, string, font, size, origin) {
rlm@83: origin = origin || "middle"; // baseline|middle
rlm@83: var out = this.set(),
rlm@83: letters = Str(string)[split](E),
rlm@83: shift = 0,
rlm@83: path = E,
rlm@83: scale;
rlm@83: R.is(font, string) && (font = this.getFont(font));
rlm@83: if (font) {
rlm@83: scale = (size || 16) / font.face["units-per-em"];
rlm@83: var bb = font.face.bbox.split(separator),
rlm@83: top = +bb[0],
rlm@83: height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
rlm@83: for (var i = 0, ii = letters[length]; i < ii; i++) {
rlm@83: var prev = i && font.glyphs[letters[i - 1]] || {},
rlm@83: curr = font.glyphs[letters[i]];
rlm@83: shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
rlm@83: curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
rlm@83: }
rlm@83: out.scale(scale, scale, top, height).translate(x - top, y - height);
rlm@83: }
rlm@83: return out;
rlm@83: };
rlm@83:
rlm@83: var formatrg = /\{(\d+)\}/g;
rlm@83: R.format = function (token, params) {
rlm@83: var args = R.is(params, array) ? [0][concat](params) : arguments;
rlm@83: token && R.is(token, string) && args[length] - 1 && (token = token[rp](formatrg, function (str, i) {
rlm@83: return args[++i] == null ? E : args[i];
rlm@83: }));
rlm@83: return token || E;
rlm@83: };
rlm@83: R.ninja = function () {
rlm@83: oldRaphael.was ? (Raphael = oldRaphael.is) : delete Raphael;
rlm@83: return R;
rlm@83: };
rlm@83: R.el = Element[proto];
rlm@83: return R;
rlm@83: })();