annotate buy_files/raphael.js @ 93:69b4defd835d laserkard

saving work
author Robert McIntyre <rlm@mit.edu>
date Mon, 26 Jul 2010 18:19:34 -0400
parents 26c2b3ad21c7
children
rev   line source
rlm@46 1 /*!
rlm@46 2 * Raphael 1.3.0 - JavaScript Vector Library
rlm@46 3 *
rlm@46 4 * Copyright (c) 2008 - 2009 Dmitry Baranovskiy (http://raphaeljs.com)
rlm@46 5 * Licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) license.
rlm@46 6 */
rlm@46 7
rlm@46 8
rlm@46 9 window.Raphael = (function () {
rlm@46 10 var separator = /[, ]+/,
rlm@46 11 elements = /^(circle|rect|path|ellipse|text|image)$/,
rlm@46 12 doc = document,
rlm@46 13 win = window,
rlm@46 14 oldRaphael = {
rlm@46 15 was: "Raphael" in win,
rlm@46 16 is: win.Raphael
rlm@46 17 },
rlm@46 18 R = function () {
rlm@46 19 if (R.is(arguments[0], "array")) {
rlm@46 20 var a = arguments[0],
rlm@46 21 cnv = create[apply](R, a.splice(0, 3 + R.is(a[0], nu))),
rlm@46 22 res = cnv.set();
rlm@46 23 for (var i = 0, ii = a[length]; i < ii; i++) {
rlm@46 24 var j = a[i] || {};
rlm@46 25 elements.test(j.type) && res[push](cnv[j.type]().attr(j));
rlm@46 26 }
rlm@46 27 return res;
rlm@46 28 }
rlm@46 29 return create[apply](R, arguments);
rlm@46 30 },
rlm@46 31 Paper = function () {},
rlm@46 32 appendChild = "appendChild",
rlm@46 33 apply = "apply",
rlm@46 34 concat = "concat",
rlm@46 35 E = "",
rlm@46 36 S = " ",
rlm@46 37 split = "split",
rlm@46 38 events = "click dblclick mousedown mousemove mouseout mouseover mouseup"[split](S),
rlm@46 39 has = "hasOwnProperty",
rlm@46 40 join = "join",
rlm@46 41 length = "length",
rlm@46 42 proto = "prototype",
rlm@46 43 lowerCase = String[proto].toLowerCase,
rlm@46 44 math = Math,
rlm@46 45 mmax = math.max,
rlm@46 46 mmin = math.min,
rlm@46 47 nu = "number",
rlm@46 48 toString = "toString",
rlm@46 49 objectToString = Object[proto][toString],
rlm@46 50 paper = {},
rlm@46 51 pow = math.pow,
rlm@46 52 push = "push",
rlm@46 53 rg = /^(?=[\da-f]$)/,
rlm@46 54 ISURL = /^url\(['"]?([^\)]+)['"]?\)$/i,
rlm@46 55 colourRegExp = /^\s*((#[a-f\d]{6})|(#[a-f\d]{3})|rgb\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|rgb\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\)|hs[bl]\(\s*([\d\.]+\s*,\s*[\d\.]+\s*,\s*[\d\.]+)\s*\)|hs[bl]\(\s*([\d\.]+%\s*,\s*[\d\.]+%\s*,\s*[\d\.]+%)\s*\))\s*$/i,
rlm@46 56 round = math.round,
rlm@46 57 setAttribute = "setAttribute",
rlm@46 58 toFloat = parseFloat,
rlm@46 59 toInt = parseInt,
rlm@46 60 upperCase = String[proto].toUpperCase,
rlm@46 61 availableAttrs = {"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@46 62 availableAnimAttrs = {along: "along", "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@46 63 rp = "replace";
rlm@46 64 R.version = "1.3.0";
rlm@46 65 R.type = (win.SVGAngle || doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML");
rlm@46 66 if (R.type == "VML") {
rlm@46 67 var d = document.createElement("div");
rlm@46 68 d.innerHTML = '<!--[if vml]><br><br><![endif]-->';
rlm@46 69 if (d.childNodes[length] != 2) {
rlm@46 70 return null;
rlm@46 71 }
rlm@46 72 }
rlm@46 73 R.svg = !(R.vml = R.type == "VML");
rlm@46 74 Paper[proto] = R[proto];
rlm@46 75 R._id = 0;
rlm@46 76 R._oid = 0;
rlm@46 77 R.fn = {};
rlm@46 78 R.is = function (o, type) {
rlm@46 79 type = lowerCase.call(type);
rlm@46 80 return ((type == "object" || type == "undefined") && typeof o == type) || (o == null && type == "null") || lowerCase.call(objectToString.call(o).slice(8, -1)) == type;
rlm@46 81 };
rlm@46 82 R.setWindow = function (newwin) {
rlm@46 83 win = newwin;
rlm@46 84 doc = win.document;
rlm@46 85 };
rlm@46 86 // colour utilities
rlm@46 87 var toHex = function (color) {
rlm@46 88 if (R.vml) {
rlm@46 89 // http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/
rlm@46 90 var trim = /^\s+|\s+$/g;
rlm@46 91 toHex = cacher(function (color) {
rlm@46 92 var bod;
rlm@46 93 color = (color + E)[rp](trim, E);
rlm@46 94 try {
rlm@46 95 var docum = new ActiveXObject("htmlfile");
rlm@46 96 docum.write("<body>");
rlm@46 97 docum.close();
rlm@46 98 bod = docum.body;
rlm@46 99 } catch(e) {
rlm@46 100 bod = createPopup().document.body;
rlm@46 101 }
rlm@46 102 var range = bod.createTextRange();
rlm@46 103 try {
rlm@46 104 bod.style.color = color;
rlm@46 105 var value = range.queryCommandValue("ForeColor");
rlm@46 106 value = ((value & 255) << 16) | (value & 65280) | ((value & 16711680) >>> 16);
rlm@46 107 return "#" + ("000000" + value[toString](16)).slice(-6);
rlm@46 108 } catch(e) {
rlm@46 109 return "none";
rlm@46 110 }
rlm@46 111 });
rlm@46 112 } else {
rlm@46 113 var i = doc.createElement("i");
rlm@46 114 i.title = "Rapha\xebl Colour Picker";
rlm@46 115 i.style.display = "none";
rlm@46 116 doc.body[appendChild](i);
rlm@46 117 toHex = cacher(function (color) {
rlm@46 118 i.style.color = color;
rlm@46 119 return doc.defaultView.getComputedStyle(i, E).getPropertyValue("color");
rlm@46 120 });
rlm@46 121 }
rlm@46 122 return toHex(color);
rlm@46 123 };
rlm@46 124 R.hsb2rgb = cacher(function (hue, saturation, brightness) {
rlm@46 125 if (R.is(hue, "object") && "h" in hue && "s" in hue && "b" in hue) {
rlm@46 126 brightness = hue.b;
rlm@46 127 saturation = hue.s;
rlm@46 128 hue = hue.h;
rlm@46 129 }
rlm@46 130 var red,
rlm@46 131 green,
rlm@46 132 blue;
rlm@46 133 if (brightness == 0) {
rlm@46 134 return {r: 0, g: 0, b: 0, hex: "#000"};
rlm@46 135 }
rlm@46 136 if (hue > 1 || saturation > 1 || brightness > 1) {
rlm@46 137 hue /= 255;
rlm@46 138 saturation /= 255;
rlm@46 139 brightness /= 255;
rlm@46 140 }
rlm@46 141 var i = ~~(hue * 6),
rlm@46 142 f = (hue * 6) - i,
rlm@46 143 p = brightness * (1 - saturation),
rlm@46 144 q = brightness * (1 - (saturation * f)),
rlm@46 145 t = brightness * (1 - (saturation * (1 - f)));
rlm@46 146 red = [brightness, q, p, p, t, brightness, brightness][i];
rlm@46 147 green = [t, brightness, brightness, q, p, p, t][i];
rlm@46 148 blue = [p, p, t, brightness, brightness, q, p][i];
rlm@46 149 red *= 255;
rlm@46 150 green *= 255;
rlm@46 151 blue *= 255;
rlm@46 152 var rgb = {r: red, g: green, b: blue},
rlm@46 153 r = (~~red)[toString](16),
rlm@46 154 g = (~~green)[toString](16),
rlm@46 155 b = (~~blue)[toString](16);
rlm@46 156 r = r[rp](rg, "0");
rlm@46 157 g = g[rp](rg, "0");
rlm@46 158 b = b[rp](rg, "0");
rlm@46 159 rgb.hex = "#" + r + g + b;
rlm@46 160 return rgb;
rlm@46 161 }, R);
rlm@46 162 R.rgb2hsb = cacher(function (red, green, blue) {
rlm@46 163 if (R.is(red, "object") && "r" in red && "g" in red && "b" in red) {
rlm@46 164 blue = red.b;
rlm@46 165 green = red.g;
rlm@46 166 red = red.r;
rlm@46 167 }
rlm@46 168 if (R.is(red, "string")) {
rlm@46 169 var clr = R.getRGB(red);
rlm@46 170 red = clr.r;
rlm@46 171 green = clr.g;
rlm@46 172 blue = clr.b;
rlm@46 173 }
rlm@46 174 if (red > 1 || green > 1 || blue > 1) {
rlm@46 175 red /= 255;
rlm@46 176 green /= 255;
rlm@46 177 blue /= 255;
rlm@46 178 }
rlm@46 179 var max = mmax(red, green, blue),
rlm@46 180 min = mmin(red, green, blue),
rlm@46 181 hue,
rlm@46 182 saturation,
rlm@46 183 brightness = max;
rlm@46 184 if (min == max) {
rlm@46 185 return {h: 0, s: 0, b: max};
rlm@46 186 } else {
rlm@46 187 var delta = (max - min);
rlm@46 188 saturation = delta / max;
rlm@46 189 if (red == max) {
rlm@46 190 hue = (green - blue) / delta;
rlm@46 191 } else if (green == max) {
rlm@46 192 hue = 2 + ((blue - red) / delta);
rlm@46 193 } else {
rlm@46 194 hue = 4 + ((red - green) / delta);
rlm@46 195 }
rlm@46 196 hue /= 6;
rlm@46 197 hue < 0 && hue++;
rlm@46 198 hue > 1 && hue--;
rlm@46 199 }
rlm@46 200 return {h: hue, s: saturation, b: brightness};
rlm@46 201 }, R);
rlm@46 202 var p2s = /,?([achlmqrstvxz]),?/gi;
rlm@46 203 R._path2string = function () {
rlm@46 204 return this.join(",")[rp](p2s, "$1");
rlm@46 205 };
rlm@46 206 function cacher(f, scope, postprocessor) {
rlm@46 207 function newf() {
rlm@46 208 var arg = Array[proto].slice.call(arguments, 0),
rlm@46 209 args = arg[join]("\u25ba"),
rlm@46 210 cache = newf.cache = newf.cache || {},
rlm@46 211 count = newf.count = newf.count || [];
rlm@46 212 if (cache[has](args)) {
rlm@46 213 return postprocessor ? postprocessor(cache[args]) : cache[args];
rlm@46 214 }
rlm@46 215 count[length] >= 1e3 && delete cache[count.shift()];
rlm@46 216 count[push](args);
rlm@46 217 cache[args] = f[apply](scope, arg);
rlm@46 218 return postprocessor ? postprocessor(cache[args]) : cache[args];
rlm@46 219 }
rlm@46 220 return newf;
rlm@46 221 }
rlm@46 222
rlm@46 223 R.getRGB = cacher(function (colour) {
rlm@46 224 if (!colour || !!((colour = colour + E).indexOf("-") + 1)) {
rlm@46 225 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
rlm@46 226 }
rlm@46 227 if (colour == "none") {
rlm@46 228 return {r: -1, g: -1, b: -1, hex: "none"};
rlm@46 229 }
rlm@46 230 !(({hs: 1, rg: 1})[has](colour.substring(0, 2)) || colour.charAt() == "#") && (colour = toHex(colour));
rlm@46 231 var res,
rlm@46 232 red,
rlm@46 233 green,
rlm@46 234 blue,
rlm@46 235 t,
rlm@46 236 rgb = colour.match(colourRegExp);
rlm@46 237 if (rgb) {
rlm@46 238 if (rgb[2]) {
rlm@46 239 blue = toInt(rgb[2].substring(5), 16);
rlm@46 240 green = toInt(rgb[2].substring(3, 5), 16);
rlm@46 241 red = toInt(rgb[2].substring(1, 3), 16);
rlm@46 242 }
rlm@46 243 if (rgb[3]) {
rlm@46 244 blue = toInt((t = rgb[3].charAt(3)) + t, 16);
rlm@46 245 green = toInt((t = rgb[3].charAt(2)) + t, 16);
rlm@46 246 red = toInt((t = rgb[3].charAt(1)) + t, 16);
rlm@46 247 }
rlm@46 248 if (rgb[4]) {
rlm@46 249 rgb = rgb[4][split](/\s*,\s*/);
rlm@46 250 red = toFloat(rgb[0]);
rlm@46 251 green = toFloat(rgb[1]);
rlm@46 252 blue = toFloat(rgb[2]);
rlm@46 253 }
rlm@46 254 if (rgb[5]) {
rlm@46 255 rgb = rgb[5][split](/\s*,\s*/);
rlm@46 256 red = toFloat(rgb[0]) * 2.55;
rlm@46 257 green = toFloat(rgb[1]) * 2.55;
rlm@46 258 blue = toFloat(rgb[2]) * 2.55;
rlm@46 259 }
rlm@46 260 if (rgb[6]) {
rlm@46 261 rgb = rgb[6][split](/\s*,\s*/);
rlm@46 262 red = toFloat(rgb[0]);
rlm@46 263 green = toFloat(rgb[1]);
rlm@46 264 blue = toFloat(rgb[2]);
rlm@46 265 return R.hsb2rgb(red, green, blue);
rlm@46 266 }
rlm@46 267 if (rgb[7]) {
rlm@46 268 rgb = rgb[7][split](/\s*,\s*/);
rlm@46 269 red = toFloat(rgb[0]) * 2.55;
rlm@46 270 green = toFloat(rgb[1]) * 2.55;
rlm@46 271 blue = toFloat(rgb[2]) * 2.55;
rlm@46 272 return R.hsb2rgb(red, green, blue);
rlm@46 273 }
rlm@46 274 rgb = {r: red, g: green, b: blue};
rlm@46 275 var r = (~~red)[toString](16),
rlm@46 276 g = (~~green)[toString](16),
rlm@46 277 b = (~~blue)[toString](16);
rlm@46 278 r = r[rp](rg, "0");
rlm@46 279 g = g[rp](rg, "0");
rlm@46 280 b = b[rp](rg, "0");
rlm@46 281 rgb.hex = "#" + r + g + b;
rlm@46 282 return rgb;
rlm@46 283 }
rlm@46 284 return {r: -1, g: -1, b: -1, hex: "none", error: 1};
rlm@46 285 }, R);
rlm@46 286 R.getColor = function (value) {
rlm@46 287 var start = this.getColor.start = this.getColor.start || {h: 0, s: 1, b: value || .75},
rlm@46 288 rgb = this.hsb2rgb(start.h, start.s, start.b);
rlm@46 289 start.h += .075;
rlm@46 290 if (start.h > 1) {
rlm@46 291 start.h = 0;
rlm@46 292 start.s -= .2;
rlm@46 293 start.s <= 0 && (this.getColor.start = {h: 0, s: 1, b: start.b});
rlm@46 294 }
rlm@46 295 return rgb.hex;
rlm@46 296 };
rlm@46 297 R.getColor.reset = function () {
rlm@46 298 delete this.start;
rlm@46 299 };
rlm@46 300 // path utilities
rlm@46 301 R.parsePathString = cacher(function (pathString) {
rlm@46 302 if (!pathString) {
rlm@46 303 return null;
rlm@46 304 }
rlm@46 305 var paramCounts = {a: 7, c: 6, h: 1, l: 2, m: 2, q: 4, s: 4, t: 2, v: 1, z: 0},
rlm@46 306 data = [];
rlm@46 307 if (R.is(pathString, "array") && R.is(pathString[0], "array")) { // rough assumption
rlm@46 308 data = pathClone(pathString);
rlm@46 309 }
rlm@46 310 if (!data[length]) {
rlm@46 311 (pathString + E)[rp](/([achlmqstvz])[\s,]*((-?\d*\.?\d*(?:e[-+]?\d+)?\s*,?\s*)+)/ig, function (a, b, c) {
rlm@46 312 var params = [],
rlm@46 313 name = lowerCase.call(b);
rlm@46 314 c[rp](/(-?\d*\.?\d*(?:e[-+]?\d+)?)\s*,?\s*/ig, function (a, b) {
rlm@46 315 b && params[push](+b);
rlm@46 316 });
rlm@46 317 while (params[length] >= paramCounts[name]) {
rlm@46 318 data[push]([b][concat](params.splice(0, paramCounts[name])));
rlm@46 319 if (!paramCounts[name]) {
rlm@46 320 break;
rlm@46 321 };
rlm@46 322 }
rlm@46 323 });
rlm@46 324 }
rlm@46 325 data[toString] = R._path2string;
rlm@46 326 return data;
rlm@46 327 });
rlm@46 328 R.findDotsAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
rlm@46 329 var t1 = 1 - t,
rlm@46 330 x = pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
rlm@46 331 y = pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y,
rlm@46 332 mx = p1x + 2 * t * (c1x - p1x) + t * t * (c2x - 2 * c1x + p1x),
rlm@46 333 my = p1y + 2 * t * (c1y - p1y) + t * t * (c2y - 2 * c1y + p1y),
rlm@46 334 nx = c1x + 2 * t * (c2x - c1x) + t * t * (p2x - 2 * c2x + c1x),
rlm@46 335 ny = c1y + 2 * t * (c2y - c1y) + t * t * (p2y - 2 * c2y + c1y),
rlm@46 336 ax = (1 - t) * p1x + t * c1x,
rlm@46 337 ay = (1 - t) * p1y + t * c1y,
rlm@46 338 cx = (1 - t) * c2x + t * p2x,
rlm@46 339 cy = (1 - t) * c2y + t * p2y,
rlm@46 340 alpha = (90 - math.atan((mx - nx) / (my - ny)) * 180 / math.PI);
rlm@46 341 (mx > nx || my < ny) && (alpha += 180);
rlm@46 342 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@46 343 };
rlm@46 344 var pathDimensions = cacher(function (path) {
rlm@46 345 if (!path) {
rlm@46 346 return {x: 0, y: 0, width: 0, height: 0};
rlm@46 347 }
rlm@46 348 path = path2curve(path);
rlm@46 349 var x = 0,
rlm@46 350 y = 0,
rlm@46 351 X = [],
rlm@46 352 Y = [],
rlm@46 353 p;
rlm@46 354 for (var i = 0, ii = path[length]; i < ii; i++) {
rlm@46 355 p = path[i];
rlm@46 356 if (p[0] == "M") {
rlm@46 357 x = p[1];
rlm@46 358 y = p[2];
rlm@46 359 X[push](x);
rlm@46 360 Y[push](y);
rlm@46 361 } else {
rlm@46 362 var dim = curveDim(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
rlm@46 363 X = X[concat](dim.min.x, dim.max.x);
rlm@46 364 Y = Y[concat](dim.min.y, dim.max.y);
rlm@46 365 x = p[5];
rlm@46 366 y = p[6];
rlm@46 367 }
rlm@46 368 }
rlm@46 369 var xmin = mmin[apply](0, X),
rlm@46 370 ymin = mmin[apply](0, Y);
rlm@46 371 return {
rlm@46 372 x: xmin,
rlm@46 373 y: ymin,
rlm@46 374 width: mmax[apply](0, X) - xmin,
rlm@46 375 height: mmax[apply](0, Y) - ymin
rlm@46 376 };
rlm@46 377 }),
rlm@46 378 pathClone = function (pathArray) {
rlm@46 379 var res = [];
rlm@46 380 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
rlm@46 381 pathArray = R.parsePathString(pathArray);
rlm@46 382 }
rlm@46 383 for (var i = 0, ii = pathArray[length]; i < ii; i++) {
rlm@46 384 res[i] = [];
rlm@46 385 for (var j = 0, jj = pathArray[i][length]; j < jj; j++) {
rlm@46 386 res[i][j] = pathArray[i][j];
rlm@46 387 }
rlm@46 388 }
rlm@46 389 res[toString] = R._path2string;
rlm@46 390 return res;
rlm@46 391 },
rlm@46 392 pathToRelative = cacher(function (pathArray) {
rlm@46 393 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
rlm@46 394 pathArray = R.parsePathString(pathArray);
rlm@46 395 }
rlm@46 396 var res = [],
rlm@46 397 x = 0,
rlm@46 398 y = 0,
rlm@46 399 mx = 0,
rlm@46 400 my = 0,
rlm@46 401 start = 0;
rlm@46 402 if (pathArray[0][0] == "M") {
rlm@46 403 x = pathArray[0][1];
rlm@46 404 y = pathArray[0][2];
rlm@46 405 mx = x;
rlm@46 406 my = y;
rlm@46 407 start++;
rlm@46 408 res[push](["M", x, y]);
rlm@46 409 }
rlm@46 410 for (var i = start, ii = pathArray[length]; i < ii; i++) {
rlm@46 411 var r = res[i] = [],
rlm@46 412 pa = pathArray[i];
rlm@46 413 if (pa[0] != lowerCase.call(pa[0])) {
rlm@46 414 r[0] = lowerCase.call(pa[0]);
rlm@46 415 switch (r[0]) {
rlm@46 416 case "a":
rlm@46 417 r[1] = pa[1];
rlm@46 418 r[2] = pa[2];
rlm@46 419 r[3] = pa[3];
rlm@46 420 r[4] = pa[4];
rlm@46 421 r[5] = pa[5];
rlm@46 422 r[6] = +(pa[6] - x).toFixed(3);
rlm@46 423 r[7] = +(pa[7] - y).toFixed(3);
rlm@46 424 break;
rlm@46 425 case "v":
rlm@46 426 r[1] = +(pa[1] - y).toFixed(3);
rlm@46 427 break;
rlm@46 428 case "m":
rlm@46 429 mx = pa[1];
rlm@46 430 my = pa[2];
rlm@46 431 default:
rlm@46 432 for (var j = 1, jj = pa[length]; j < jj; j++) {
rlm@46 433 r[j] = +(pa[j] - ((j % 2) ? x : y)).toFixed(3);
rlm@46 434 }
rlm@46 435 }
rlm@46 436 } else {
rlm@46 437 r = res[i] = [];
rlm@46 438 if (pa[0] == "m") {
rlm@46 439 mx = pa[1] + x;
rlm@46 440 my = pa[2] + y;
rlm@46 441 }
rlm@46 442 for (var k = 0, kk = pa[length]; k < kk; k++) {
rlm@46 443 res[i][k] = pa[k];
rlm@46 444 }
rlm@46 445 }
rlm@46 446 var len = res[i][length];
rlm@46 447 switch (res[i][0]) {
rlm@46 448 case "z":
rlm@46 449 x = mx;
rlm@46 450 y = my;
rlm@46 451 break;
rlm@46 452 case "h":
rlm@46 453 x += +res[i][len - 1];
rlm@46 454 break;
rlm@46 455 case "v":
rlm@46 456 y += +res[i][len - 1];
rlm@46 457 break;
rlm@46 458 default:
rlm@46 459 x += +res[i][len - 2];
rlm@46 460 y += +res[i][len - 1];
rlm@46 461 }
rlm@46 462 }
rlm@46 463 res[toString] = R._path2string;
rlm@46 464 return res;
rlm@46 465 }, 0, pathClone),
rlm@46 466 pathToAbsolute = cacher(function (pathArray) {
rlm@46 467 if (!R.is(pathArray, "array") || !R.is(pathArray && pathArray[0], "array")) { // rough assumption
rlm@46 468 pathArray = R.parsePathString(pathArray);
rlm@46 469 }
rlm@46 470 var res = [],
rlm@46 471 x = 0,
rlm@46 472 y = 0,
rlm@46 473 mx = 0,
rlm@46 474 my = 0,
rlm@46 475 start = 0;
rlm@46 476 if (pathArray[0][0] == "M") {
rlm@46 477 x = +pathArray[0][1];
rlm@46 478 y = +pathArray[0][2];
rlm@46 479 mx = x;
rlm@46 480 my = y;
rlm@46 481 start++;
rlm@46 482 res[0] = ["M", x, y];
rlm@46 483 }
rlm@46 484 for (var i = start, ii = pathArray[length]; i < ii; i++) {
rlm@46 485 var r = res[i] = [],
rlm@46 486 pa = pathArray[i];
rlm@46 487 if (pa[0] != upperCase.call(pa[0])) {
rlm@46 488 r[0] = upperCase.call(pa[0]);
rlm@46 489 switch (r[0]) {
rlm@46 490 case "A":
rlm@46 491 r[1] = pa[1];
rlm@46 492 r[2] = pa[2];
rlm@46 493 r[3] = pa[3];
rlm@46 494 r[4] = pa[4];
rlm@46 495 r[5] = pa[5];
rlm@46 496 r[6] = +(pa[6] + x);
rlm@46 497 r[7] = +(pa[7] + y);
rlm@46 498 break;
rlm@46 499 case "V":
rlm@46 500 r[1] = +pa[1] + y;
rlm@46 501 break;
rlm@46 502 case "H":
rlm@46 503 r[1] = +pa[1] + x;
rlm@46 504 break;
rlm@46 505 case "M":
rlm@46 506 mx = +pa[1] + x;
rlm@46 507 my = +pa[2] + y;
rlm@46 508 default:
rlm@46 509 for (var j = 1, jj = pa[length]; j < jj; j++) {
rlm@46 510 r[j] = +pa[j] + ((j % 2) ? x : y);
rlm@46 511 }
rlm@46 512 }
rlm@46 513 } else {
rlm@46 514 for (var k = 0, kk = pa[length]; k < kk; k++) {
rlm@46 515 res[i][k] = pa[k];
rlm@46 516 }
rlm@46 517 }
rlm@46 518 switch (r[0]) {
rlm@46 519 case "Z":
rlm@46 520 x = mx;
rlm@46 521 y = my;
rlm@46 522 break;
rlm@46 523 case "H":
rlm@46 524 x = r[1];
rlm@46 525 break;
rlm@46 526 case "V":
rlm@46 527 y = r[1];
rlm@46 528 break;
rlm@46 529 default:
rlm@46 530 x = res[i][res[i][length] - 2];
rlm@46 531 y = res[i][res[i][length] - 1];
rlm@46 532 }
rlm@46 533 }
rlm@46 534 res[toString] = R._path2string;
rlm@46 535 return res;
rlm@46 536 }, null, pathClone),
rlm@46 537 l2c = function (x1, y1, x2, y2) {
rlm@46 538 return [x1, y1, x2, y2, x2, y2];
rlm@46 539 },
rlm@46 540 q2c = function (x1, y1, ax, ay, x2, y2) {
rlm@46 541 var _13 = 1 / 3,
rlm@46 542 _23 = 2 / 3;
rlm@46 543 return [
rlm@46 544 _13 * x1 + _23 * ax,
rlm@46 545 _13 * y1 + _23 * ay,
rlm@46 546 _13 * x2 + _23 * ax,
rlm@46 547 _13 * y2 + _23 * ay,
rlm@46 548 x2,
rlm@46 549 y2
rlm@46 550 ];
rlm@46 551 },
rlm@46 552 a2c = function (x1, y1, rx, ry, angle, large_arc_flag, sweep_flag, x2, y2, recursive) {
rlm@46 553 // for more information of where this math came from visit:
rlm@46 554 // http://www.w3.org/TR/SVG11/implnote.html#ArcImplementationNotes
rlm@46 555 var PI = math.PI,
rlm@46 556 _120 = PI * 120 / 180,
rlm@46 557 rad = PI / 180 * (+angle || 0),
rlm@46 558 res = [],
rlm@46 559 xy,
rlm@46 560 rotate = cacher(function (x, y, rad) {
rlm@46 561 var X = x * math.cos(rad) - y * math.sin(rad),
rlm@46 562 Y = x * math.sin(rad) + y * math.cos(rad);
rlm@46 563 return {x: X, y: Y};
rlm@46 564 });
rlm@46 565 if (!recursive) {
rlm@46 566 xy = rotate(x1, y1, -rad);
rlm@46 567 x1 = xy.x;
rlm@46 568 y1 = xy.y;
rlm@46 569 xy = rotate(x2, y2, -rad);
rlm@46 570 x2 = xy.x;
rlm@46 571 y2 = xy.y;
rlm@46 572 var cos = math.cos(PI / 180 * angle),
rlm@46 573 sin = math.sin(PI / 180 * angle),
rlm@46 574 x = (x1 - x2) / 2,
rlm@46 575 y = (y1 - y2) / 2;
rlm@46 576 rx = mmax(rx, math.abs(x));
rlm@46 577 ry = mmax(ry, math.abs(y));
rlm@46 578 var rx2 = rx * rx,
rlm@46 579 ry2 = ry * ry,
rlm@46 580 k = (large_arc_flag == sweep_flag ? -1 : 1) *
rlm@46 581 math.sqrt(math.abs((rx2 * ry2 - rx2 * y * y - ry2 * x * x) / (rx2 * y * y + ry2 * x * x))),
rlm@46 582 cx = k * rx * y / ry + (x1 + x2) / 2,
rlm@46 583 cy = k * -ry * x / rx + (y1 + y2) / 2,
rlm@46 584 f1 = math.asin(((y1 - cy) / ry).toFixed(7)),
rlm@46 585 f2 = math.asin(((y2 - cy) / ry).toFixed(7));
rlm@46 586
rlm@46 587 f1 = x1 < cx ? PI - f1 : f1;
rlm@46 588 f2 = x2 < cx ? PI - f2 : f2;
rlm@46 589 f1 < 0 && (f1 = PI * 2 + f1);
rlm@46 590 f2 < 0 && (f2 = PI * 2 + f2);
rlm@46 591 if (sweep_flag && f1 > f2) {
rlm@46 592 f1 = f1 - PI * 2;
rlm@46 593 }
rlm@46 594 if (!sweep_flag && f2 > f1) {
rlm@46 595 f2 = f2 - PI * 2;
rlm@46 596 }
rlm@46 597 } else {
rlm@46 598 f1 = recursive[0];
rlm@46 599 f2 = recursive[1];
rlm@46 600 cx = recursive[2];
rlm@46 601 cy = recursive[3];
rlm@46 602 }
rlm@46 603 var df = f2 - f1;
rlm@46 604 if (math.abs(df) > _120) {
rlm@46 605 var f2old = f2,
rlm@46 606 x2old = x2,
rlm@46 607 y2old = y2;
rlm@46 608 f2 = f1 + _120 * (sweep_flag && f2 > f1 ? 1 : -1);
rlm@46 609 x2 = cx + rx * math.cos(f2);
rlm@46 610 y2 = cy + ry * math.sin(f2);
rlm@46 611 res = a2c(x2, y2, rx, ry, angle, 0, sweep_flag, x2old, y2old, [f2, f2old, cx, cy]);
rlm@46 612 }
rlm@46 613 df = f2 - f1;
rlm@46 614 var c1 = math.cos(f1),
rlm@46 615 s1 = math.sin(f1),
rlm@46 616 c2 = math.cos(f2),
rlm@46 617 s2 = math.sin(f2),
rlm@46 618 t = math.tan(df / 4),
rlm@46 619 hx = 4 / 3 * rx * t,
rlm@46 620 hy = 4 / 3 * ry * t,
rlm@46 621 m1 = [x1, y1],
rlm@46 622 m2 = [x1 + hx * s1, y1 - hy * c1],
rlm@46 623 m3 = [x2 + hx * s2, y2 - hy * c2],
rlm@46 624 m4 = [x2, y2];
rlm@46 625 m2[0] = 2 * m1[0] - m2[0];
rlm@46 626 m2[1] = 2 * m1[1] - m2[1];
rlm@46 627 if (recursive) {
rlm@46 628 return [m2, m3, m4][concat](res);
rlm@46 629 } else {
rlm@46 630 res = [m2, m3, m4][concat](res)[join]()[split](",");
rlm@46 631 var newres = [];
rlm@46 632 for (var i = 0, ii = res[length]; i < ii; i++) {
rlm@46 633 newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x;
rlm@46 634 }
rlm@46 635 return newres;
rlm@46 636 }
rlm@46 637 },
rlm@46 638 findDotAtSegment = function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t) {
rlm@46 639 var t1 = 1 - t;
rlm@46 640 return {
rlm@46 641 x: pow(t1, 3) * p1x + pow(t1, 2) * 3 * t * c1x + t1 * 3 * t * t * c2x + pow(t, 3) * p2x,
rlm@46 642 y: pow(t1, 3) * p1y + pow(t1, 2) * 3 * t * c1y + t1 * 3 * t * t * c2y + pow(t, 3) * p2y
rlm@46 643 };
rlm@46 644 },
rlm@46 645 curveDim = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
rlm@46 646 var a = (c2x - 2 * c1x + p1x) - (p2x - 2 * c2x + c1x),
rlm@46 647 b = 2 * (c1x - p1x) - 2 * (c2x - c1x),
rlm@46 648 c = p1x - c1x,
rlm@46 649 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a,
rlm@46 650 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a,
rlm@46 651 y = [p1y, p2y],
rlm@46 652 x = [p1x, p2x],
rlm@46 653 dot;
rlm@46 654 math.abs(t1) > 1e12 && (t1 = .5);
rlm@46 655 math.abs(t2) > 1e12 && (t2 = .5);
rlm@46 656 if (t1 > 0 && t1 < 1) {
rlm@46 657 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
rlm@46 658 x[push](dot.x);
rlm@46 659 y[push](dot.y);
rlm@46 660 }
rlm@46 661 if (t2 > 0 && t2 < 1) {
rlm@46 662 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
rlm@46 663 x[push](dot.x);
rlm@46 664 y[push](dot.y);
rlm@46 665 }
rlm@46 666 a = (c2y - 2 * c1y + p1y) - (p2y - 2 * c2y + c1y);
rlm@46 667 b = 2 * (c1y - p1y) - 2 * (c2y - c1y);
rlm@46 668 c = p1y - c1y;
rlm@46 669 t1 = (-b + math.sqrt(b * b - 4 * a * c)) / 2 / a;
rlm@46 670 t2 = (-b - math.sqrt(b * b - 4 * a * c)) / 2 / a;
rlm@46 671 math.abs(t1) > 1e12 && (t1 = .5);
rlm@46 672 math.abs(t2) > 1e12 && (t2 = .5);
rlm@46 673 if (t1 > 0 && t1 < 1) {
rlm@46 674 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t1);
rlm@46 675 x[push](dot.x);
rlm@46 676 y[push](dot.y);
rlm@46 677 }
rlm@46 678 if (t2 > 0 && t2 < 1) {
rlm@46 679 dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, t2);
rlm@46 680 x[push](dot.x);
rlm@46 681 y[push](dot.y);
rlm@46 682 }
rlm@46 683 return {
rlm@46 684 min: {x: mmin[apply](0, x), y: mmin[apply](0, y)},
rlm@46 685 max: {x: mmax[apply](0, x), y: mmax[apply](0, y)}
rlm@46 686 };
rlm@46 687 }),
rlm@46 688 path2curve = cacher(function (path, path2) {
rlm@46 689 var p = pathToAbsolute(path),
rlm@46 690 p2 = path2 && pathToAbsolute(path2),
rlm@46 691 attrs = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
rlm@46 692 attrs2 = {x: 0, y: 0, bx: 0, by: 0, X: 0, Y: 0, qx: null, qy: null},
rlm@46 693 processPath = function (path, d) {
rlm@46 694 var nx, ny;
rlm@46 695 if (!path) {
rlm@46 696 return ["C", d.x, d.y, d.x, d.y, d.x, d.y];
rlm@46 697 }
rlm@46 698 !(path[0] in {T:1, Q:1}) && (d.qx = d.qy = null);
rlm@46 699 switch (path[0]) {
rlm@46 700 case "M":
rlm@46 701 d.X = path[1];
rlm@46 702 d.Y = path[2];
rlm@46 703 break;
rlm@46 704 case "A":
rlm@46 705 path = ["C"][concat](a2c[apply](0, [d.x, d.y][concat](path.slice(1))));
rlm@46 706 break;
rlm@46 707 case "S":
rlm@46 708 nx = d.x + (d.x - (d.bx || d.x));
rlm@46 709 ny = d.y + (d.y - (d.by || d.y));
rlm@46 710 path = ["C", nx, ny][concat](path.slice(1));
rlm@46 711 break;
rlm@46 712 case "T":
rlm@46 713 d.qx = d.x + (d.x - (d.qx || d.x));
rlm@46 714 d.qy = d.y + (d.y - (d.qy || d.y));
rlm@46 715 path = ["C"][concat](q2c(d.x, d.y, d.qx, d.qy, path[1], path[2]));
rlm@46 716 break;
rlm@46 717 case "Q":
rlm@46 718 d.qx = path[1];
rlm@46 719 d.qy = path[2];
rlm@46 720 path = ["C"][concat](q2c(d.x, d.y, path[1], path[2], path[3], path[4]));
rlm@46 721 break;
rlm@46 722 case "L":
rlm@46 723 path = ["C"][concat](l2c(d.x, d.y, path[1], path[2]));
rlm@46 724 break;
rlm@46 725 case "H":
rlm@46 726 path = ["C"][concat](l2c(d.x, d.y, path[1], d.y));
rlm@46 727 break;
rlm@46 728 case "V":
rlm@46 729 path = ["C"][concat](l2c(d.x, d.y, d.x, path[1]));
rlm@46 730 break;
rlm@46 731 case "Z":
rlm@46 732 path = ["C"][concat](l2c(d.x, d.y, d.X, d.Y));
rlm@46 733 break;
rlm@46 734 }
rlm@46 735 return path;
rlm@46 736 },
rlm@46 737 fixArc = function (pp, i) {
rlm@46 738 if (pp[i][length] > 7) {
rlm@46 739 pp[i].shift();
rlm@46 740 var pi = pp[i];
rlm@46 741 while (pi[length]) {
rlm@46 742 pp.splice(i++, 0, ["C"][concat](pi.splice(0, 6)));
rlm@46 743 }
rlm@46 744 pp.splice(i, 1);
rlm@46 745 ii = mmax(p[length], p2 && p2[length] || 0);
rlm@46 746 }
rlm@46 747 },
rlm@46 748 fixM = function (path1, path2, a1, a2, i) {
rlm@46 749 if (path1 && path2 && path1[i][0] == "M" && path2[i][0] != "M") {
rlm@46 750 path2.splice(i, 0, ["M", a2.x, a2.y]);
rlm@46 751 a1.bx = 0;
rlm@46 752 a1.by = 0;
rlm@46 753 a1.x = path1[i][1];
rlm@46 754 a1.y = path1[i][2];
rlm@46 755 ii = mmax(p[length], p2 && p2[length] || 0);
rlm@46 756 }
rlm@46 757 };
rlm@46 758 for (var i = 0, ii = mmax(p[length], p2 && p2[length] || 0); i < ii; i++) {
rlm@46 759 p[i] = processPath(p[i], attrs);
rlm@46 760 fixArc(p, i);
rlm@46 761 p2 && (p2[i] = processPath(p2[i], attrs2));
rlm@46 762 p2 && fixArc(p2, i);
rlm@46 763 fixM(p, p2, attrs, attrs2, i);
rlm@46 764 fixM(p2, p, attrs2, attrs, i);
rlm@46 765 var seg = p[i],
rlm@46 766 seg2 = p2 && p2[i],
rlm@46 767 seglen = seg[length],
rlm@46 768 seg2len = p2 && seg2[length];
rlm@46 769 attrs.x = seg[seglen - 2];
rlm@46 770 attrs.y = seg[seglen - 1];
rlm@46 771 attrs.bx = toFloat(seg[seglen - 4]) || attrs.x;
rlm@46 772 attrs.by = toFloat(seg[seglen - 3]) || attrs.y;
rlm@46 773 attrs2.bx = p2 && (toFloat(seg2[seg2len - 4]) || attrs2.x);
rlm@46 774 attrs2.by = p2 && (toFloat(seg2[seg2len - 3]) || attrs2.y);
rlm@46 775 attrs2.x = p2 && seg2[seg2len - 2];
rlm@46 776 attrs2.y = p2 && seg2[seg2len - 1];
rlm@46 777 }
rlm@46 778 return p2 ? [p, p2] : p;
rlm@46 779 }, null, pathClone),
rlm@46 780 parseDots = cacher(function (gradient) {
rlm@46 781 var dots = [];
rlm@46 782 for (var i = 0, ii = gradient[length]; i < ii; i++) {
rlm@46 783 var dot = {},
rlm@46 784 par = gradient[i].match(/^([^:]*):?([\d\.]*)/);
rlm@46 785 dot.color = R.getRGB(par[1]);
rlm@46 786 if (dot.color.error) {
rlm@46 787 return null;
rlm@46 788 }
rlm@46 789 dot.color = dot.color.hex;
rlm@46 790 par[2] && (dot.offset = par[2] + "%");
rlm@46 791 dots[push](dot);
rlm@46 792 }
rlm@46 793 for (var i = 1, ii = dots[length] - 1; i < ii; i++) {
rlm@46 794 if (!dots[i].offset) {
rlm@46 795 var start = toFloat(dots[i - 1].offset || 0),
rlm@46 796 end = 0;
rlm@46 797 for (var j = i + 1; j < ii; j++) {
rlm@46 798 if (dots[j].offset) {
rlm@46 799 end = dots[j].offset;
rlm@46 800 break;
rlm@46 801 }
rlm@46 802 }
rlm@46 803 if (!end) {
rlm@46 804 end = 100;
rlm@46 805 j = ii;
rlm@46 806 }
rlm@46 807 end = toFloat(end);
rlm@46 808 var d = (end - start) / (j - i + 1);
rlm@46 809 for (; i < j; i++) {
rlm@46 810 start += d;
rlm@46 811 dots[i].offset = start + "%";
rlm@46 812 }
rlm@46 813 }
rlm@46 814 }
rlm@46 815 return dots;
rlm@46 816 }),
rlm@46 817 getContainer = function () {
rlm@46 818 var container,
rlm@46 819 x,
rlm@46 820 y,
rlm@46 821 width,
rlm@46 822 height;
rlm@46 823 if (R.is(arguments[0], "string") || R.is(arguments[0], "object")) {
rlm@46 824 if (R.is(arguments[0], "string")) {
rlm@46 825 container = doc.getElementById(arguments[0]);
rlm@46 826 } else {
rlm@46 827 container = arguments[0];
rlm@46 828 }
rlm@46 829 if (container.tagName) {
rlm@46 830 if (arguments[1] == null) {
rlm@46 831 return {
rlm@46 832 container: container,
rlm@46 833 width: container.style.pixelWidth || container.offsetWidth,
rlm@46 834 height: container.style.pixelHeight || container.offsetHeight
rlm@46 835 };
rlm@46 836 } else {
rlm@46 837 return {container: container, width: arguments[1], height: arguments[2]};
rlm@46 838 }
rlm@46 839 }
rlm@46 840 } else if (R.is(arguments[0], nu) && arguments[length] > 3) {
rlm@46 841 return {container: 1, x: arguments[0], y: arguments[1], width: arguments[2], height: arguments[3]};
rlm@46 842 }
rlm@46 843 },
rlm@46 844 plugins = function (con, add) {
rlm@46 845 var that = this;
rlm@46 846 for (var prop in add) if (add[has](prop) && !(prop in con)) {
rlm@46 847 switch (typeof add[prop]) {
rlm@46 848 case "function":
rlm@46 849 (function (f) {
rlm@46 850 con[prop] = con === that ? f : function () { return f[apply](that, arguments); };
rlm@46 851 })(add[prop]);
rlm@46 852 break;
rlm@46 853 case "object":
rlm@46 854 con[prop] = con[prop] || {};
rlm@46 855 plugins.call(this, con[prop], add[prop]);
rlm@46 856 break;
rlm@46 857 default:
rlm@46 858 con[prop] = add[prop];
rlm@46 859 break;
rlm@46 860 }
rlm@46 861 }
rlm@46 862 },
rlm@46 863 tear = function (el, paper) {
rlm@46 864 el == paper.top && (paper.top = el.prev);
rlm@46 865 el == paper.bottom && (paper.bottom = el.next);
rlm@46 866 el.next && (el.next.prev = el.prev);
rlm@46 867 el.prev && (el.prev.next = el.next);
rlm@46 868 },
rlm@46 869 tofront = function (el, paper) {
rlm@46 870 if (paper.top === el) {
rlm@46 871 return;
rlm@46 872 }
rlm@46 873 tear(el, paper);
rlm@46 874 el.next = null;
rlm@46 875 el.prev = paper.top;
rlm@46 876 paper.top.next = el;
rlm@46 877 paper.top = el;
rlm@46 878 },
rlm@46 879 toback = function (el, paper) {
rlm@46 880 if (paper.bottom === el) {
rlm@46 881 return;
rlm@46 882 }
rlm@46 883 tear(el, paper);
rlm@46 884 el.next = paper.bottom;
rlm@46 885 el.prev = null;
rlm@46 886 paper.bottom.prev = el;
rlm@46 887 paper.bottom = el;
rlm@46 888 },
rlm@46 889 insertafter = function (el, el2, paper) {
rlm@46 890 tear(el, paper);
rlm@46 891 el2 == paper.top && (paper.top = el);
rlm@46 892 el2.next && (el2.next.prev = el);
rlm@46 893 el.next = el2.next;
rlm@46 894 el.prev = el2;
rlm@46 895 el2.next = el;
rlm@46 896 },
rlm@46 897 insertbefore = function (el, el2, paper) {
rlm@46 898 tear(el, paper);
rlm@46 899 el2 == paper.bottom && (paper.bottom = el);
rlm@46 900 el2.prev && (el2.prev.next = el);
rlm@46 901 el.prev = el2.prev;
rlm@46 902 el2.prev = el;
rlm@46 903 el.next = el2;
rlm@46 904 },
rlm@46 905 removed = function (methodname) {
rlm@46 906 return function () {
rlm@46 907 throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object");
rlm@46 908 };
rlm@46 909 },
rlm@46 910 radial_gradient = /^r(?:\(([^,]+?)\s*,\s*([^\)]+?)\))?/;
rlm@46 911
rlm@46 912 // SVG
rlm@46 913 if (R.svg) {
rlm@46 914 Paper[proto].svgns = "http://www.w3.org/2000/svg";
rlm@46 915 Paper[proto].xlink = "http://www.w3.org/1999/xlink";
rlm@46 916 var round = function (num) {
rlm@46 917 return +num + (~~num === num) * .5;
rlm@46 918 },
rlm@46 919 roundPath = function (path) {
rlm@46 920 for (var i = 0, ii = path[length]; i < ii; i++) {
rlm@46 921 if (lowerCase.call(path[i][0]) != "a") {
rlm@46 922 for (var j = 1, jj = path[i][length]; j < jj; j++) {
rlm@46 923 path[i][j] = round(path[i][j]);
rlm@46 924 }
rlm@46 925 } else {
rlm@46 926 path[i][6] = round(path[i][6]);
rlm@46 927 path[i][7] = round(path[i][7]);
rlm@46 928 }
rlm@46 929 }
rlm@46 930 return path;
rlm@46 931 },
rlm@46 932 $ = function (el, attr) {
rlm@46 933 if (attr) {
rlm@46 934 for (var key in attr) if (attr[has](key)) {
rlm@46 935 el[setAttribute](key, attr[key]);
rlm@46 936 }
rlm@46 937 } else {
rlm@46 938 return doc.createElementNS(Paper[proto].svgns, el);
rlm@46 939 }
rlm@46 940 };
rlm@46 941 R[toString] = function () {
rlm@46 942 return "Your browser supports SVG.\nYou are running Rapha\xebl " + this.version;
rlm@46 943 };
rlm@46 944 var thePath = function (pathString, SVG) {
rlm@46 945 var el = $("path");
rlm@46 946 SVG.canvas && SVG.canvas[appendChild](el);
rlm@46 947 var p = new Element(el, SVG);
rlm@46 948 p.type = "path";
rlm@46 949 setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
rlm@46 950 return p;
rlm@46 951 };
rlm@46 952 var addGradientFill = function (o, gradient, SVG) {
rlm@46 953 var type = "linear",
rlm@46 954 fx = .5, fy = .5,
rlm@46 955 s = o.style;
rlm@46 956 gradient = (gradient + E)[rp](radial_gradient, function (all, _fx, _fy) {
rlm@46 957 type = "radial";
rlm@46 958 if (_fx && _fy) {
rlm@46 959 fx = toFloat(_fx);
rlm@46 960 fy = toFloat(_fy);
rlm@46 961 var dir = ((fy > .5) * 2 - 1);
rlm@46 962 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 &&
rlm@46 963 (fy = math.sqrt(.25 - pow(fx - .5, 2)) * dir + .5) &&
rlm@46 964 fy != .5 &&
rlm@46 965 (fy = fy.toFixed(5) - 1e-5 * dir);
rlm@46 966 }
rlm@46 967 return E;
rlm@46 968 });
rlm@46 969 gradient = gradient[split](/\s*\-\s*/);
rlm@46 970 if (type == "linear") {
rlm@46 971 var angle = gradient.shift();
rlm@46 972 angle = -toFloat(angle);
rlm@46 973 if (isNaN(angle)) {
rlm@46 974 return null;
rlm@46 975 }
rlm@46 976 var vector = [0, 0, math.cos(angle * math.PI / 180), math.sin(angle * math.PI / 180)],
rlm@46 977 max = 1 / (mmax(math.abs(vector[2]), math.abs(vector[3])) || 1);
rlm@46 978 vector[2] *= max;
rlm@46 979 vector[3] *= max;
rlm@46 980 if (vector[2] < 0) {
rlm@46 981 vector[0] = -vector[2];
rlm@46 982 vector[2] = 0;
rlm@46 983 }
rlm@46 984 if (vector[3] < 0) {
rlm@46 985 vector[1] = -vector[3];
rlm@46 986 vector[3] = 0;
rlm@46 987 }
rlm@46 988 }
rlm@46 989 var dots = parseDots(gradient);
rlm@46 990 if (!dots) {
rlm@46 991 return null;
rlm@46 992 }
rlm@46 993 var el = $(type + "Gradient");
rlm@46 994 el.id = "r" + (R._id++)[toString](36);
rlm@46 995 $(el, type == "radial" ? {fx: fx, fy: fy} : {x1: vector[0], y1: vector[1], x2: vector[2], y2: vector[3]});
rlm@46 996 SVG.defs[appendChild](el);
rlm@46 997 for (var i = 0, ii = dots[length]; i < ii; i++) {
rlm@46 998 var stop = $("stop");
rlm@46 999 $(stop, {
rlm@46 1000 offset: dots[i].offset ? dots[i].offset : !i ? "0%" : "100%",
rlm@46 1001 "stop-color": dots[i].color || "#fff"
rlm@46 1002 });
rlm@46 1003 el[appendChild](stop);
rlm@46 1004 };
rlm@46 1005 $(o, {
rlm@46 1006 fill: "url(#" + el.id + ")",
rlm@46 1007 opacity: 1,
rlm@46 1008 "fill-opacity": 1
rlm@46 1009 });
rlm@46 1010 s.fill = E;
rlm@46 1011 s.opacity = 1;
rlm@46 1012 s.fillOpacity = 1;
rlm@46 1013 return 1;
rlm@46 1014 };
rlm@46 1015 var updatePosition = function (o) {
rlm@46 1016 var bbox = o.getBBox();
rlm@46 1017 $(o.pattern, {patternTransform: R.format("translate({0},{1})", bbox.x, bbox.y)});
rlm@46 1018 };
rlm@46 1019 var setFillAndStroke = function (o, params) {
rlm@46 1020 var dasharray = {
rlm@46 1021 "": [0],
rlm@46 1022 "none": [0],
rlm@46 1023 "-": [3, 1],
rlm@46 1024 ".": [1, 1],
rlm@46 1025 "-.": [3, 1, 1, 1],
rlm@46 1026 "-..": [3, 1, 1, 1, 1, 1],
rlm@46 1027 ". ": [1, 3],
rlm@46 1028 "- ": [4, 3],
rlm@46 1029 "--": [8, 3],
rlm@46 1030 "- .": [4, 3, 1, 3],
rlm@46 1031 "--.": [8, 3, 1, 3],
rlm@46 1032 "--..": [8, 3, 1, 3, 1, 3]
rlm@46 1033 },
rlm@46 1034 node = o.node,
rlm@46 1035 attrs = o.attrs,
rlm@46 1036 rot = o.rotate(),
rlm@46 1037 addDashes = function (o, value) {
rlm@46 1038 value = dasharray[lowerCase.call(value)];
rlm@46 1039 if (value) {
rlm@46 1040 var width = o.attrs["stroke-width"] || "1",
rlm@46 1041 butt = {round: width, square: width, butt: 0}[o.attrs["stroke-linecap"] || params["stroke-linecap"]] || 0,
rlm@46 1042 dashes = [];
rlm@46 1043 var i = value[length];
rlm@46 1044 while (i--) {
rlm@46 1045 dashes[i] = value[i] * width + ((i % 2) ? 1 : -1) * butt;
rlm@46 1046 }
rlm@46 1047 $(node, {"stroke-dasharray": dashes[join](",")});
rlm@46 1048 }
rlm@46 1049 };
rlm@46 1050 params[has]("rotation") && (rot = params.rotation);
rlm@46 1051 var rotxy = (rot + E)[split](separator);
rlm@46 1052 if (!(rotxy.length - 1)) {
rlm@46 1053 rotxy = null;
rlm@46 1054 } else {
rlm@46 1055 rotxy[1] = +rotxy[1];
rlm@46 1056 rotxy[2] = +rotxy[2];
rlm@46 1057 }
rlm@46 1058 toFloat(rot) && o.rotate(0, true);
rlm@46 1059 for (var att in params) if (params[has](att)) {
rlm@46 1060 if (!availableAttrs[has](att)) {
rlm@46 1061 continue;
rlm@46 1062 }
rlm@46 1063 var value = params[att];
rlm@46 1064 attrs[att] = value;
rlm@46 1065 switch (att) {
rlm@46 1066 case "rotation":
rlm@46 1067 o.rotate(value, true);
rlm@46 1068 break;
rlm@46 1069 // Hyperlink
rlm@46 1070 case "href":
rlm@46 1071 case "title":
rlm@46 1072 case "target":
rlm@46 1073 var pn = node.parentNode;
rlm@46 1074 if (lowerCase.call(pn.tagName) != "a") {
rlm@46 1075 var hl = $("a");
rlm@46 1076 pn.insertBefore(hl, node);
rlm@46 1077 hl[appendChild](node);
rlm@46 1078 pn = hl;
rlm@46 1079 }
rlm@46 1080 pn.setAttributeNS(o.Paper[proto].xlink, att, value);
rlm@46 1081 break;
rlm@46 1082 case "cursor":
rlm@46 1083 node.style.cursor = value;
rlm@46 1084 break;
rlm@46 1085 case "clip-rect":
rlm@46 1086 var rect = (value + E)[split](separator);
rlm@46 1087 if (rect[length] == 4) {
rlm@46 1088 o.clip && o.clip.parentNode.parentNode.removeChild(o.clip.parentNode);
rlm@46 1089 var el = $("clipPath"),
rlm@46 1090 rc = $("rect");
rlm@46 1091 el.id = "r" + (R._id++)[toString](36);
rlm@46 1092 $(rc, {
rlm@46 1093 x: rect[0],
rlm@46 1094 y: rect[1],
rlm@46 1095 width: rect[2],
rlm@46 1096 height: rect[3]
rlm@46 1097 });
rlm@46 1098 el[appendChild](rc);
rlm@46 1099 o.paper.defs[appendChild](el);
rlm@46 1100 $(node, {"clip-path": "url(#" + el.id + ")"});
rlm@46 1101 o.clip = rc;
rlm@46 1102 }
rlm@46 1103 if (!value) {
rlm@46 1104 var clip = doc.getElementById(node.getAttribute("clip-path")[rp](/(^url\(#|\)$)/g, E));
rlm@46 1105 clip && clip.parentNode.removeChild(clip);
rlm@46 1106 $(node, {"clip-path": E});
rlm@46 1107 delete o.clip;
rlm@46 1108 }
rlm@46 1109 break;
rlm@46 1110 case "path":
rlm@46 1111 if (value && o.type == "path") {
rlm@46 1112 attrs.path = roundPath(pathToAbsolute(value));
rlm@46 1113 $(node, {d: attrs.path});
rlm@46 1114 }
rlm@46 1115 break;
rlm@46 1116 case "width":
rlm@46 1117 node[setAttribute](att, value);
rlm@46 1118 if (attrs.fx) {
rlm@46 1119 att = "x";
rlm@46 1120 value = attrs.x;
rlm@46 1121 } else {
rlm@46 1122 break;
rlm@46 1123 }
rlm@46 1124 case "x":
rlm@46 1125 if (attrs.fx) {
rlm@46 1126 value = -attrs.x - (attrs.width || 0);
rlm@46 1127 }
rlm@46 1128 case "rx":
rlm@46 1129 if (att == "rx" && o.type == "rect") {
rlm@46 1130 break;
rlm@46 1131 }
rlm@46 1132 case "cx":
rlm@46 1133 rotxy && (att == "x" || att == "cx") && (rotxy[1] += value - attrs[att]);
rlm@46 1134 node[setAttribute](att, round(value));
rlm@46 1135 o.pattern && updatePosition(o);
rlm@46 1136 break;
rlm@46 1137 case "height":
rlm@46 1138 node[setAttribute](att, value);
rlm@46 1139 if (attrs.fy) {
rlm@46 1140 att = "y";
rlm@46 1141 value = attrs.y;
rlm@46 1142 } else {
rlm@46 1143 break;
rlm@46 1144 }
rlm@46 1145 case "y":
rlm@46 1146 if (attrs.fy) {
rlm@46 1147 value = -attrs.y - (attrs.height || 0);
rlm@46 1148 }
rlm@46 1149 case "ry":
rlm@46 1150 if (att == "ry" && o.type == "rect") {
rlm@46 1151 break;
rlm@46 1152 }
rlm@46 1153 case "cy":
rlm@46 1154 rotxy && (att == "y" || att == "cy") && (rotxy[2] += value - attrs[att]);
rlm@46 1155 node[setAttribute](att, round(value));
rlm@46 1156 o.pattern && updatePosition(o);
rlm@46 1157 break;
rlm@46 1158 case "r":
rlm@46 1159 if (o.type == "rect") {
rlm@46 1160 $(node, {rx: value, ry: value});
rlm@46 1161 } else {
rlm@46 1162 node[setAttribute](att, value);
rlm@46 1163 }
rlm@46 1164 break;
rlm@46 1165 case "src":
rlm@46 1166 if (o.type == "image") {
rlm@46 1167 node.setAttributeNS(o.paper.xlink, "href", value);
rlm@46 1168 }
rlm@46 1169 break;
rlm@46 1170 case "stroke-width":
rlm@46 1171 node.style.strokeWidth = value;
rlm@46 1172 // Need following line for Firefox
rlm@46 1173 node[setAttribute](att, value);
rlm@46 1174 if (attrs["stroke-dasharray"]) {
rlm@46 1175 addDashes(o, attrs["stroke-dasharray"]);
rlm@46 1176 }
rlm@46 1177 break;
rlm@46 1178 case "stroke-dasharray":
rlm@46 1179 addDashes(o, value);
rlm@46 1180 break;
rlm@46 1181 case "translation":
rlm@46 1182 var xy = (value + E)[split](separator);
rlm@46 1183 xy[0] = +xy[0] || 0;
rlm@46 1184 xy[1] = +xy[1] || 0;
rlm@46 1185 if (rotxy) {
rlm@46 1186 rotxy[1] += xy[0];
rlm@46 1187 rotxy[2] += xy[1];
rlm@46 1188 }
rlm@46 1189 translate.call(o, xy[0], xy[1]);
rlm@46 1190 break;
rlm@46 1191 case "scale":
rlm@46 1192 var xy = (value + E)[split](separator);
rlm@46 1193 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
rlm@46 1194 break;
rlm@46 1195 case "fill":
rlm@46 1196 var isURL = (value + E).match(ISURL);
rlm@46 1197 if (isURL) {
rlm@46 1198 var el = $("pattern"),
rlm@46 1199 ig = $("image");
rlm@46 1200 el.id = "r" + (R._id++)[toString](36);
rlm@46 1201 $(el, {x: 0, y: 0, patternUnits: "userSpaceOnUse", height: 1, width: 1});
rlm@46 1202 $(ig, {x: 0, y: 0});
rlm@46 1203 ig.setAttributeNS(o.paper.xlink, "href", isURL[1]);
rlm@46 1204 el[appendChild](ig);
rlm@46 1205
rlm@46 1206 var img = doc.createElement("img");
rlm@46 1207 img.style.cssText = "position:absolute;left:-9999em;top-9999em";
rlm@46 1208 img.onload = function () {
rlm@46 1209 $(el, {width: this.offsetWidth, height: this.offsetHeight});
rlm@46 1210 $(ig, {width: this.offsetWidth, height: this.offsetHeight});
rlm@46 1211 doc.body.removeChild(this);
rlm@46 1212 o.paper.safari();
rlm@46 1213 };
rlm@46 1214 doc.body[appendChild](img);
rlm@46 1215 img.src = isURL[1];
rlm@46 1216 o.paper.defs[appendChild](el);
rlm@46 1217 node.style.fill = "url(#" + el.id + ")";
rlm@46 1218 $(node, {fill: "url(#" + el.id + ")"});
rlm@46 1219 o.pattern = el;
rlm@46 1220 o.pattern && updatePosition(o);
rlm@46 1221 break;
rlm@46 1222 }
rlm@46 1223 if (!R.getRGB(value).error) {
rlm@46 1224 delete params.gradient;
rlm@46 1225 delete attrs.gradient;
rlm@46 1226 !R.is(attrs.opacity, "undefined") &&
rlm@46 1227 R.is(params.opacity, "undefined") &&
rlm@46 1228 $(node, {opacity: attrs.opacity});
rlm@46 1229 !R.is(attrs["fill-opacity"], "undefined") &&
rlm@46 1230 R.is(params["fill-opacity"], "undefined") &&
rlm@46 1231 $(node, {"fill-opacity": attrs["fill-opacity"]});
rlm@46 1232 } else if ((({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper)) {
rlm@46 1233 attrs.gradient = value;
rlm@46 1234 attrs.fill = "none";
rlm@46 1235 break;
rlm@46 1236 }
rlm@46 1237 case "stroke":
rlm@46 1238 node[setAttribute](att, R.getRGB(value).hex);
rlm@46 1239 break;
rlm@46 1240 case "gradient":
rlm@46 1241 (({circle: 1, ellipse: 1})[has](o.type) || (value + E).charAt() != "r") && addGradientFill(node, value, o.paper);
rlm@46 1242 break;
rlm@46 1243 case "opacity":
rlm@46 1244 case "fill-opacity":
rlm@46 1245 if (attrs.gradient) {
rlm@46 1246 var gradient = doc.getElementById(node.getAttribute("fill")[rp](/^url\(#|\)$/g, E));
rlm@46 1247 if (gradient) {
rlm@46 1248 var stops = gradient.getElementsByTagName("stop");
rlm@46 1249 stops[stops[length] - 1][setAttribute]("stop-opacity", value);
rlm@46 1250 }
rlm@46 1251 break;
rlm@46 1252 }
rlm@46 1253 default:
rlm@46 1254 att == "font-size" && (value = toInt(value, 10) + "px");
rlm@46 1255 var cssrule = att[rp](/(\-.)/g, function (w) {
rlm@46 1256 return upperCase.call(w.substring(1));
rlm@46 1257 });
rlm@46 1258 node.style[cssrule] = value;
rlm@46 1259 // Need following line for Firefox
rlm@46 1260 node[setAttribute](att, value);
rlm@46 1261 break;
rlm@46 1262 }
rlm@46 1263 }
rlm@46 1264
rlm@46 1265 tuneText(o, params);
rlm@46 1266 if (rotxy) {
rlm@46 1267 o.rotate(rotxy.join(S));
rlm@46 1268 } else {
rlm@46 1269 toFloat(rot) && o.rotate(rot, true);
rlm@46 1270 }
rlm@46 1271 };
rlm@46 1272 var leading = 1.2;
rlm@46 1273 var tuneText = function (el, params) {
rlm@46 1274 if (el.type != "text" || !(params[has]("text") || params[has]("font") || params[has]("font-size") || params[has]("x") || params[has]("y"))) {
rlm@46 1275 return;
rlm@46 1276 }
rlm@46 1277 var a = el.attrs,
rlm@46 1278 node = el.node,
rlm@46 1279 fontSize = node.firstChild ? toInt(doc.defaultView.getComputedStyle(node.firstChild, E).getPropertyValue("font-size"), 10) : 10;
rlm@46 1280
rlm@46 1281 if (params[has]("text")) {
rlm@46 1282 a.text = params.text;
rlm@46 1283 while (node.firstChild) {
rlm@46 1284 node.removeChild(node.firstChild);
rlm@46 1285 }
rlm@46 1286 var texts = (params.text + E)[split]("\n");
rlm@46 1287 for (var i = 0, ii = texts[length]; i < ii; i++) if (texts[i]) {
rlm@46 1288 var tspan = $("tspan");
rlm@46 1289 i && $(tspan, {dy: fontSize * leading, x: a.x});
rlm@46 1290 tspan[appendChild](doc.createTextNode(texts[i]));
rlm@46 1291 node[appendChild](tspan);
rlm@46 1292 }
rlm@46 1293 } else {
rlm@46 1294 var texts = node.getElementsByTagName("tspan");
rlm@46 1295 for (var i = 0, ii = texts[length]; i < ii; i++) {
rlm@46 1296 i && $(texts[i], {dy: fontSize * leading, x: a.x});
rlm@46 1297 }
rlm@46 1298 }
rlm@46 1299 $(node, {y: a.y});
rlm@46 1300 var bb = el.getBBox(),
rlm@46 1301 dif = a.y - (bb.y + bb.height / 2);
rlm@46 1302 dif && isFinite(dif) && $(node, {y: a.y + dif});
rlm@46 1303 };
rlm@46 1304 var Element = function (node, svg) {
rlm@46 1305 var X = 0,
rlm@46 1306 Y = 0;
rlm@46 1307 this[0] = node;
rlm@46 1308 this.id = R._oid++;
rlm@46 1309 this.node = node;
rlm@46 1310 node.raphael = this;
rlm@46 1311 this.paper = svg;
rlm@46 1312 this.attrs = this.attrs || {};
rlm@46 1313 this.transformations = []; // rotate, translate, scale
rlm@46 1314 this._ = {
rlm@46 1315 tx: 0,
rlm@46 1316 ty: 0,
rlm@46 1317 rt: {deg: 0, cx: 0, cy: 0},
rlm@46 1318 sx: 1,
rlm@46 1319 sy: 1
rlm@46 1320 };
rlm@46 1321 !svg.bottom && (svg.bottom = this);
rlm@46 1322 this.prev = svg.top;
rlm@46 1323 svg.top && (svg.top.next = this);
rlm@46 1324 svg.top = this;
rlm@46 1325 this.next = null;
rlm@46 1326 };
rlm@46 1327 Element[proto].rotate = function (deg, cx, cy) {
rlm@46 1328 if (this.removed) {
rlm@46 1329 return this;
rlm@46 1330 }
rlm@46 1331 if (deg == null) {
rlm@46 1332 if (this._.rt.cx) {
rlm@46 1333 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
rlm@46 1334 }
rlm@46 1335 return this._.rt.deg;
rlm@46 1336 }
rlm@46 1337 var bbox = this.getBBox();
rlm@46 1338 deg = (deg + E)[split](separator);
rlm@46 1339 if (deg[length] - 1) {
rlm@46 1340 cx = toFloat(deg[1]);
rlm@46 1341 cy = toFloat(deg[2]);
rlm@46 1342 }
rlm@46 1343 deg = toFloat(deg[0]);
rlm@46 1344 if (cx != null) {
rlm@46 1345 this._.rt.deg = deg;
rlm@46 1346 } else {
rlm@46 1347 this._.rt.deg += deg;
rlm@46 1348 }
rlm@46 1349 (cy == null) && (cx = null);
rlm@46 1350 this._.rt.cx = cx;
rlm@46 1351 this._.rt.cy = cy;
rlm@46 1352 cx = cx == null ? bbox.x + bbox.width / 2 : cx;
rlm@46 1353 cy = cy == null ? bbox.y + bbox.height / 2 : cy;
rlm@46 1354 if (this._.rt.deg) {
rlm@46 1355 this.transformations[0] = R.format("rotate({0} {1} {2})", this._.rt.deg, cx, cy);
rlm@46 1356 this.clip && $(this.clip, {transform: R.format("rotate({0} {1} {2})", -this._.rt.deg, cx, cy)});
rlm@46 1357 } else {
rlm@46 1358 this.transformations[0] = E;
rlm@46 1359 this.clip && $(this.clip, {transform: E});
rlm@46 1360 }
rlm@46 1361 $(this.node, {transform: this.transformations[join](S)});
rlm@46 1362 return this;
rlm@46 1363 };
rlm@46 1364 Element[proto].hide = function () {
rlm@46 1365 !this.removed && (this.node.style.display = "none");
rlm@46 1366 return this;
rlm@46 1367 };
rlm@46 1368 Element[proto].show = function () {
rlm@46 1369 !this.removed && (this.node.style.display = "");
rlm@46 1370 return this;
rlm@46 1371 };
rlm@46 1372 Element[proto].remove = function () {
rlm@46 1373 if (this.removed) {
rlm@46 1374 return;
rlm@46 1375 }
rlm@46 1376 tear(this, this.paper);
rlm@46 1377 this.node.parentNode.removeChild(this.node);
rlm@46 1378 for (var i in this) {
rlm@46 1379 delete this[i];
rlm@46 1380 }
rlm@46 1381 this.removed = true;
rlm@46 1382 };
rlm@46 1383 Element[proto].getBBox = function () {
rlm@46 1384 if (this.removed) {
rlm@46 1385 return this;
rlm@46 1386 }
rlm@46 1387 if (this.type == "path") {
rlm@46 1388 return pathDimensions(this.attrs.path);
rlm@46 1389 }
rlm@46 1390 if (this.node.style.display == "none") {
rlm@46 1391 this.show();
rlm@46 1392 var hide = true;
rlm@46 1393 }
rlm@46 1394 var bbox = {};
rlm@46 1395 try {
rlm@46 1396 bbox = this.node.getBBox();
rlm@46 1397 } catch(e) {
rlm@46 1398 // Firefox 3.0.x plays badly here
rlm@46 1399 } finally {
rlm@46 1400 bbox = bbox || {};
rlm@46 1401 }
rlm@46 1402 if (this.type == "text") {
rlm@46 1403 bbox = {x: bbox.x, y: Infinity, width: 0, height: 0};
rlm@46 1404 for (var i = 0, ii = this.node.getNumberOfChars(); i < ii; i++) {
rlm@46 1405 var bb = this.node.getExtentOfChar(i);
rlm@46 1406 (bb.y < bbox.y) && (bbox.y = bb.y);
rlm@46 1407 (bb.y + bb.height - bbox.y > bbox.height) && (bbox.height = bb.y + bb.height - bbox.y);
rlm@46 1408 (bb.x + bb.width - bbox.x > bbox.width) && (bbox.width = bb.x + bb.width - bbox.x);
rlm@46 1409 }
rlm@46 1410 }
rlm@46 1411 hide && this.hide();
rlm@46 1412 return bbox;
rlm@46 1413 };
rlm@46 1414 Element[proto].attr = function () {
rlm@46 1415 if (this.removed) {
rlm@46 1416 return this;
rlm@46 1417 }
rlm@46 1418 if (arguments[length] == 0) {
rlm@46 1419 var res = {};
rlm@46 1420 for (var i in this.attrs) if (this.attrs[has](i)) {
rlm@46 1421 res[i] = this.attrs[i];
rlm@46 1422 }
rlm@46 1423 this._.rt.deg && (res.rotation = this.rotate());
rlm@46 1424 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
rlm@46 1425 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
rlm@46 1426 return res;
rlm@46 1427 }
rlm@46 1428 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
rlm@46 1429 if (arguments[0] == "translation") {
rlm@46 1430 return translate.call(this);
rlm@46 1431 }
rlm@46 1432 if (arguments[0] == "rotation") {
rlm@46 1433 return this.rotate();
rlm@46 1434 }
rlm@46 1435 if (arguments[0] == "scale") {
rlm@46 1436 return this.scale();
rlm@46 1437 }
rlm@46 1438 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
rlm@46 1439 return this.attrs.gradient;
rlm@46 1440 }
rlm@46 1441 return this.attrs[arguments[0]];
rlm@46 1442 }
rlm@46 1443 if (arguments[length] == 1 && R.is(arguments[0], "array")) {
rlm@46 1444 var values = {};
rlm@46 1445 for (var j in arguments[0]) if (arguments[0][has](j)) {
rlm@46 1446 values[arguments[0][j]] = this.attrs[arguments[0][j]];
rlm@46 1447 }
rlm@46 1448 return values;
rlm@46 1449 }
rlm@46 1450 if (arguments[length] == 2) {
rlm@46 1451 var params = {};
rlm@46 1452 params[arguments[0]] = arguments[1];
rlm@46 1453 setFillAndStroke(this, params);
rlm@46 1454 } else if (arguments[length] == 1 && R.is(arguments[0], "object")) {
rlm@46 1455 setFillAndStroke(this, arguments[0]);
rlm@46 1456 }
rlm@46 1457 return this;
rlm@46 1458 };
rlm@46 1459 Element[proto].toFront = function () {
rlm@46 1460 if (this.removed) {
rlm@46 1461 return this;
rlm@46 1462 }
rlm@46 1463 this.node.parentNode[appendChild](this.node);
rlm@46 1464 var svg = this.paper;
rlm@46 1465 svg.top != this && tofront(this, svg);
rlm@46 1466 return this;
rlm@46 1467 };
rlm@46 1468 Element[proto].toBack = function () {
rlm@46 1469 if (this.removed) {
rlm@46 1470 return this;
rlm@46 1471 }
rlm@46 1472 if (this.node.parentNode.firstChild != this.node) {
rlm@46 1473 this.node.parentNode.insertBefore(this.node, this.node.parentNode.firstChild);
rlm@46 1474 toback(this, this.paper);
rlm@46 1475 var svg = this.paper;
rlm@46 1476 }
rlm@46 1477 return this;
rlm@46 1478 };
rlm@46 1479 Element[proto].insertAfter = function (element) {
rlm@46 1480 if (this.removed) {
rlm@46 1481 return this;
rlm@46 1482 }
rlm@46 1483 var node = element.node;
rlm@46 1484 if (node.nextSibling) {
rlm@46 1485 node.parentNode.insertBefore(this.node, node.nextSibling);
rlm@46 1486 } else {
rlm@46 1487 node.parentNode[appendChild](this.node);
rlm@46 1488 }
rlm@46 1489 insertafter(this, element, this.paper);
rlm@46 1490 return this;
rlm@46 1491 };
rlm@46 1492 Element[proto].insertBefore = function (element) {
rlm@46 1493 if (this.removed) {
rlm@46 1494 return this;
rlm@46 1495 }
rlm@46 1496 var node = element.node;
rlm@46 1497 node.parentNode.insertBefore(this.node, node);
rlm@46 1498 insertbefore(this, element, this.paper);
rlm@46 1499 return this;
rlm@46 1500 };
rlm@46 1501 var theCircle = function (svg, x, y, r) {
rlm@46 1502 x = round(x);
rlm@46 1503 y = round(y);
rlm@46 1504 var el = $("circle");
rlm@46 1505 svg.canvas && svg.canvas[appendChild](el);
rlm@46 1506 var res = new Element(el, svg);
rlm@46 1507 res.attrs = {cx: x, cy: y, r: r, fill: "none", stroke: "#000"};
rlm@46 1508 res.type = "circle";
rlm@46 1509 $(el, res.attrs);
rlm@46 1510 return res;
rlm@46 1511 };
rlm@46 1512 var theRect = function (svg, x, y, w, h, r) {
rlm@46 1513 x = round(x);
rlm@46 1514 y = round(y);
rlm@46 1515 var el = $("rect");
rlm@46 1516 svg.canvas && svg.canvas[appendChild](el);
rlm@46 1517 var res = new Element(el, svg);
rlm@46 1518 res.attrs = {x: x, y: y, width: w, height: h, r: r || 0, rx: r || 0, ry: r || 0, fill: "none", stroke: "#000"};
rlm@46 1519 res.type = "rect";
rlm@46 1520 $(el, res.attrs);
rlm@46 1521 return res;
rlm@46 1522 };
rlm@46 1523 var theEllipse = function (svg, x, y, rx, ry) {
rlm@46 1524 x = round(x);
rlm@46 1525 y = round(y);
rlm@46 1526 var el = $("ellipse");
rlm@46 1527 svg.canvas && svg.canvas[appendChild](el);
rlm@46 1528 var res = new Element(el, svg);
rlm@46 1529 res.attrs = {cx: x, cy: y, rx: rx, ry: ry, fill: "none", stroke: "#000"};
rlm@46 1530 res.type = "ellipse";
rlm@46 1531 $(el, res.attrs);
rlm@46 1532 return res;
rlm@46 1533 };
rlm@46 1534 var theImage = function (svg, src, x, y, w, h) {
rlm@46 1535 var el = $("image");
rlm@46 1536 $(el, {x: x, y: y, width: w, height: h, preserveAspectRatio: "none"});
rlm@46 1537 el.setAttributeNS(svg.xlink, "href", src);
rlm@46 1538 svg.canvas && svg.canvas[appendChild](el);
rlm@46 1539 var res = new Element(el, svg);
rlm@46 1540 res.attrs = {x: x, y: y, width: w, height: h, src: src};
rlm@46 1541 res.type = "image";
rlm@46 1542 return res;
rlm@46 1543 };
rlm@46 1544 var theText = function (svg, x, y, text) {
rlm@46 1545 var el = $("text");
rlm@46 1546 $(el, {x: x, y: y, "text-anchor": "middle"});
rlm@46 1547 svg.canvas && svg.canvas[appendChild](el);
rlm@46 1548 var res = new Element(el, svg);
rlm@46 1549 res.attrs = {x: x, y: y, "text-anchor": "middle", text: text, font: availableAttrs.font, stroke: "none", fill: "#000"};
rlm@46 1550 res.type = "text";
rlm@46 1551 setFillAndStroke(res, res.attrs);
rlm@46 1552 return res;
rlm@46 1553 };
rlm@46 1554 var setSize = function (width, height) {
rlm@46 1555 this.width = width || this.width;
rlm@46 1556 this.height = height || this.height;
rlm@46 1557 this.canvas[setAttribute]("width", this.width);
rlm@46 1558 this.canvas[setAttribute]("height", this.height);
rlm@46 1559 return this;
rlm@46 1560 };
rlm@46 1561 var create = function () {
rlm@46 1562 var con = getContainer[apply](null, arguments),
rlm@46 1563 container = con && con.container,
rlm@46 1564 x = con.x,
rlm@46 1565 y = con.y,
rlm@46 1566 width = con.width,
rlm@46 1567 height = con.height;
rlm@46 1568 if (!container) {
rlm@46 1569 throw new Error("SVG container not found.");
rlm@46 1570 }
rlm@46 1571 var cnvs = $("svg");
rlm@46 1572 width = width || 512;
rlm@46 1573 height = height || 342;
rlm@46 1574 $(cnvs, {
rlm@46 1575 xmlns: "http://www.w3.org/2000/svg",
rlm@46 1576 version: 1.1,
rlm@46 1577 width: width,
rlm@46 1578 height: height
rlm@46 1579 });
rlm@46 1580 if (container == 1) {
rlm@46 1581 cnvs.style.cssText = "position:absolute;left:" + x + "px;top:" + y + "px";
rlm@46 1582 doc.body[appendChild](cnvs);
rlm@46 1583 } else {
rlm@46 1584 if (container.firstChild) {
rlm@46 1585 container.insertBefore(cnvs, container.firstChild);
rlm@46 1586 } else {
rlm@46 1587 container[appendChild](cnvs);
rlm@46 1588 }
rlm@46 1589 }
rlm@46 1590 container = new Paper;
rlm@46 1591 container.width = width;
rlm@46 1592 container.height = height;
rlm@46 1593 container.canvas = cnvs;
rlm@46 1594 plugins.call(container, container, R.fn);
rlm@46 1595 container.clear();
rlm@46 1596 return container;
rlm@46 1597 };
rlm@46 1598 Paper[proto].clear = function () {
rlm@46 1599 var c = this.canvas;
rlm@46 1600 while (c.firstChild) {
rlm@46 1601 c.removeChild(c.firstChild);
rlm@46 1602 }
rlm@46 1603 this.bottom = this.top = null;
rlm@46 1604 (this.desc = $("desc"))[appendChild](doc.createTextNode("Created with Rapha\xebl"));
rlm@46 1605 c[appendChild](this.desc);
rlm@46 1606 c[appendChild](this.defs = $("defs"));
rlm@46 1607 };
rlm@46 1608 Paper[proto].remove = function () {
rlm@46 1609 this.canvas.parentNode && this.canvas.parentNode.removeChild(this.canvas);
rlm@46 1610 for (var i in this) {
rlm@46 1611 this[i] = removed(i);
rlm@46 1612 }
rlm@46 1613 };
rlm@46 1614 }
rlm@46 1615
rlm@46 1616 // VML
rlm@46 1617 if (R.vml) {
rlm@46 1618 var path2vml = function (path) {
rlm@46 1619 var total = /[ahqstv]/ig,
rlm@46 1620 command = pathToAbsolute;
rlm@46 1621 (path + E).match(total) && (command = path2curve);
rlm@46 1622 total = /[clmz]/g;
rlm@46 1623 if (command == pathToAbsolute && !(path + E).match(total)) {
rlm@46 1624 var map = {M: "m", L: "l", C: "c", Z: "x", m: "t", l: "r", c: "v", z: "x"},
rlm@46 1625 bites = /([clmz]),?([^clmz]*)/gi,
rlm@46 1626 val = /-?[^,\s-]+/g;
rlm@46 1627 var res = (path + E)[rp](bites, function (all, command, args) {
rlm@46 1628 var vals = [];
rlm@46 1629 args[rp](val, function (value) {
rlm@46 1630 vals[push](round(value));
rlm@46 1631 });
rlm@46 1632 return map[command] + vals;
rlm@46 1633 });
rlm@46 1634 return res;
rlm@46 1635 }
rlm@46 1636 var pa = command(path), p, res = [], r;
rlm@46 1637 for (var i = 0, ii = pa[length]; i < ii; i++) {
rlm@46 1638 p = pa[i];
rlm@46 1639 r = lowerCase.call(pa[i][0]);
rlm@46 1640 r == "z" && (r = "x");
rlm@46 1641 for (var j = 1, jj = p[length]; j < jj; j++) {
rlm@46 1642 r += round(p[j]) + (j != jj - 1 ? "," : E);
rlm@46 1643 }
rlm@46 1644 res[push](r);
rlm@46 1645 }
rlm@46 1646 return res[join](S);
rlm@46 1647 };
rlm@46 1648
rlm@46 1649 R[toString] = function () {
rlm@46 1650 return "Your browser doesn\u2019t support SVG. Falling down to VML.\nYou are running Rapha\xebl " + this.version;
rlm@46 1651 };
rlm@46 1652 var thePath = function (pathString, VML) {
rlm@46 1653 var g = createNode("group");
rlm@46 1654 g.style.cssText = "position:absolute;left:0;top:0;width:" + VML.width + "px;height:" + VML.height + "px";
rlm@46 1655 g.coordsize = VML.coordsize;
rlm@46 1656 g.coordorigin = VML.coordorigin;
rlm@46 1657 var el = createNode("shape"), ol = el.style;
rlm@46 1658 ol.width = VML.width + "px";
rlm@46 1659 ol.height = VML.height + "px";
rlm@46 1660 el.coordsize = this.coordsize;
rlm@46 1661 el.coordorigin = this.coordorigin;
rlm@46 1662 g[appendChild](el);
rlm@46 1663 var p = new Element(el, g, VML);
rlm@46 1664 p.isAbsolute = true;
rlm@46 1665 p.type = "path";
rlm@46 1666 p.path = [];
rlm@46 1667 p.Path = E;
rlm@46 1668 pathString && setFillAndStroke(p, {fill: "none", stroke: "#000", path: pathString});
rlm@46 1669 VML.canvas[appendChild](g);
rlm@46 1670 return p;
rlm@46 1671 };
rlm@46 1672 var setFillAndStroke = function (o, params) {
rlm@46 1673 o.attrs = o.attrs || {};
rlm@46 1674 var node = o.node,
rlm@46 1675 a = o.attrs,
rlm@46 1676 s = node.style,
rlm@46 1677 xy,
rlm@46 1678 res = o;
rlm@46 1679 for (var par in params) if (params[has](par)) {
rlm@46 1680 a[par] = params[par];
rlm@46 1681 }
rlm@46 1682 params.href && (node.href = params.href);
rlm@46 1683 params.title && (node.title = params.title);
rlm@46 1684 params.target && (node.target = params.target);
rlm@46 1685 params.cursor && (s.cursor = params.cursor);
rlm@46 1686 if (params.path && o.type == "path") {
rlm@46 1687 a.path = params.path;
rlm@46 1688 node.path = path2vml(a.path);
rlm@46 1689 }
rlm@46 1690 if (params.rotation != null) {
rlm@46 1691 o.rotate(params.rotation, true);
rlm@46 1692 }
rlm@46 1693 if (params.translation) {
rlm@46 1694 xy = (params.translation + E)[split](separator);
rlm@46 1695 translate.call(o, xy[0], xy[1]);
rlm@46 1696 if (o._.rt.cx != null) {
rlm@46 1697 o._.rt.cx +=+ xy[0];
rlm@46 1698 o._.rt.cy +=+ xy[1];
rlm@46 1699 o.setBox(o.attrs, xy[0], xy[1]);
rlm@46 1700 }
rlm@46 1701 }
rlm@46 1702 if (params.scale) {
rlm@46 1703 xy = (params.scale + E)[split](separator);
rlm@46 1704 o.scale(+xy[0] || 1, +xy[1] || +xy[0] || 1, +xy[2] || null, +xy[3] || null);
rlm@46 1705 }
rlm@46 1706 if ("clip-rect" in params) {
rlm@46 1707 var rect = (params["clip-rect"] + E)[split](separator);
rlm@46 1708 if (rect[length] == 4) {
rlm@46 1709 rect[2] = +rect[2] + (+rect[0]);
rlm@46 1710 rect[3] = +rect[3] + (+rect[1]);
rlm@46 1711 var div = node.clipRect || doc.createElement("div"),
rlm@46 1712 dstyle = div.style,
rlm@46 1713 group = node.parentNode;
rlm@46 1714 dstyle.clip = R.format("rect({1}px {2}px {3}px {0}px)", rect);
rlm@46 1715 if (!node.clipRect) {
rlm@46 1716 dstyle.position = "absolute";
rlm@46 1717 dstyle.top = 0;
rlm@46 1718 dstyle.left = 0;
rlm@46 1719 dstyle.width = o.paper.width + "px";
rlm@46 1720 dstyle.height = o.paper.height + "px";
rlm@46 1721 group.parentNode.insertBefore(div, group);
rlm@46 1722 div[appendChild](group);
rlm@46 1723 node.clipRect = div;
rlm@46 1724 }
rlm@46 1725 }
rlm@46 1726 if (!params["clip-rect"]) {
rlm@46 1727 node.clipRect && (node.clipRect.style.clip = E);
rlm@46 1728 }
rlm@46 1729 }
rlm@46 1730 if (o.type == "image" && params.src) {
rlm@46 1731 node.src = params.src;
rlm@46 1732 }
rlm@46 1733 if (o.type == "image" && params.opacity) {
rlm@46 1734 node.filterOpacity = " progid:DXImageTransform.Microsoft.Alpha(opacity=" + (params.opacity * 100) + ")";
rlm@46 1735 s.filter = (node.filterMatrix || E) + (node.filterOpacity || E);
rlm@46 1736 }
rlm@46 1737 params.font && (s.font = params.font);
rlm@46 1738 params["font-family"] && (s.fontFamily = '"' + params["font-family"][split](",")[0][rp](/^['"]+|['"]+$/g, E) + '"');
rlm@46 1739 params["font-size"] && (s.fontSize = params["font-size"]);
rlm@46 1740 params["font-weight"] && (s.fontWeight = params["font-weight"]);
rlm@46 1741 params["font-style"] && (s.fontStyle = params["font-style"]);
rlm@46 1742 if (params.opacity != null ||
rlm@46 1743 params["stroke-width"] != null ||
rlm@46 1744 params.fill != null ||
rlm@46 1745 params.stroke != null ||
rlm@46 1746 params["stroke-width"] != null ||
rlm@46 1747 params["stroke-opacity"] != null ||
rlm@46 1748 params["fill-opacity"] != null ||
rlm@46 1749 params["stroke-dasharray"] != null ||
rlm@46 1750 params["stroke-miterlimit"] != null ||
rlm@46 1751 params["stroke-linejoin"] != null ||
rlm@46 1752 params["stroke-linecap"] != null) {
rlm@46 1753 node = o.shape || node;
rlm@46 1754 var fill = (node.getElementsByTagName("fill") && node.getElementsByTagName("fill")[0]),
rlm@46 1755 newfill = false;
rlm@46 1756 !fill && (newfill = fill = createNode("fill"));
rlm@46 1757 if ("fill-opacity" in params || "opacity" in params) {
rlm@46 1758 var opacity = ((+a["fill-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1);
rlm@46 1759 opacity < 0 && (opacity = 0);
rlm@46 1760 opacity > 1 && (opacity = 1);
rlm@46 1761 fill.opacity = opacity;
rlm@46 1762 }
rlm@46 1763 params.fill && (fill.on = true);
rlm@46 1764 if (fill.on == null || params.fill == "none") {
rlm@46 1765 fill.on = false;
rlm@46 1766 }
rlm@46 1767 if (fill.on && params.fill) {
rlm@46 1768 var isURL = params.fill.match(ISURL);
rlm@46 1769 if (isURL) {
rlm@46 1770 fill.src = isURL[1];
rlm@46 1771 fill.type = "tile";
rlm@46 1772 } else {
rlm@46 1773 fill.color = R.getRGB(params.fill).hex;
rlm@46 1774 fill.src = E;
rlm@46 1775 fill.type = "solid";
rlm@46 1776 if (R.getRGB(params.fill).error && (res.type in {circle: 1, ellipse: 1} || (params.fill + E).charAt() != "r") && addGradientFill(res, params.fill)) {
rlm@46 1777 a.fill = "none";
rlm@46 1778 a.gradient = params.fill;
rlm@46 1779 }
rlm@46 1780 }
rlm@46 1781 }
rlm@46 1782 newfill && node[appendChild](fill);
rlm@46 1783 var stroke = (node.getElementsByTagName("stroke") && node.getElementsByTagName("stroke")[0]),
rlm@46 1784 newstroke = false;
rlm@46 1785 !stroke && (newstroke = stroke = createNode("stroke"));
rlm@46 1786 if ((params.stroke && params.stroke != "none") ||
rlm@46 1787 params["stroke-width"] ||
rlm@46 1788 params["stroke-opacity"] != null ||
rlm@46 1789 params["stroke-dasharray"] ||
rlm@46 1790 params["stroke-miterlimit"] ||
rlm@46 1791 params["stroke-linejoin"] ||
rlm@46 1792 params["stroke-linecap"]) {
rlm@46 1793 stroke.on = true;
rlm@46 1794 }
rlm@46 1795 (params.stroke == "none" || stroke.on == null || params.stroke == 0 || params["stroke-width"] == 0) && (stroke.on = false);
rlm@46 1796 stroke.on && params.stroke && (stroke.color = R.getRGB(params.stroke).hex);
rlm@46 1797 var opacity = ((+a["stroke-opacity"] + 1 || 2) - 1) * ((+a.opacity + 1 || 2) - 1),
rlm@46 1798 width = (toFloat(params["stroke-width"]) || 1) * .75;
rlm@46 1799 opacity < 0 && (opacity = 0);
rlm@46 1800 opacity > 1 && (opacity = 1);
rlm@46 1801 params["stroke-width"] == null && (width = a["stroke-width"]);
rlm@46 1802 params["stroke-width"] && (stroke.weight = width);
rlm@46 1803 width && width < 1 && (opacity *= width) && (stroke.weight = 1);
rlm@46 1804 stroke.opacity = opacity;
rlm@46 1805
rlm@46 1806 params["stroke-linejoin"] && (stroke.joinstyle = params["stroke-linejoin"] || "miter");
rlm@46 1807 stroke.miterlimit = params["stroke-miterlimit"] || 8;
rlm@46 1808 params["stroke-linecap"] && (stroke.endcap = params["stroke-linecap"] == "butt" ? "flat" : params["stroke-linecap"] == "square" ? "square" : "round");
rlm@46 1809 if (params["stroke-dasharray"]) {
rlm@46 1810 var dasharray = {
rlm@46 1811 "-": "shortdash",
rlm@46 1812 ".": "shortdot",
rlm@46 1813 "-.": "shortdashdot",
rlm@46 1814 "-..": "shortdashdotdot",
rlm@46 1815 ". ": "dot",
rlm@46 1816 "- ": "dash",
rlm@46 1817 "--": "longdash",
rlm@46 1818 "- .": "dashdot",
rlm@46 1819 "--.": "longdashdot",
rlm@46 1820 "--..": "longdashdotdot"
rlm@46 1821 };
rlm@46 1822 stroke.dashstyle = dasharray[has](params["stroke-dasharray"]) ? dasharray[params["stroke-dasharray"]] : E;
rlm@46 1823 }
rlm@46 1824 newstroke && node[appendChild](stroke);
rlm@46 1825 }
rlm@46 1826 if (res.type == "text") {
rlm@46 1827 var s = res.paper.span.style;
rlm@46 1828 a.font && (s.font = a.font);
rlm@46 1829 a["font-family"] && (s.fontFamily = a["font-family"]);
rlm@46 1830 a["font-size"] && (s.fontSize = a["font-size"]);
rlm@46 1831 a["font-weight"] && (s.fontWeight = a["font-weight"]);
rlm@46 1832 a["font-style"] && (s.fontStyle = a["font-style"]);
rlm@46 1833 res.node.string && (res.paper.span.innerHTML = (res.node.string + E)[rp](/</g, "&#60;")[rp](/&/g, "&#38;")[rp](/\n/g, "<br>"));
rlm@46 1834 res.W = a.w = res.paper.span.offsetWidth;
rlm@46 1835 res.H = a.h = res.paper.span.offsetHeight;
rlm@46 1836 res.X = a.x;
rlm@46 1837 res.Y = a.y + round(res.H / 2);
rlm@46 1838
rlm@46 1839 // text-anchor emulationm
rlm@46 1840 switch (a["text-anchor"]) {
rlm@46 1841 case "start":
rlm@46 1842 res.node.style["v-text-align"] = "left";
rlm@46 1843 res.bbx = round(res.W / 2);
rlm@46 1844 break;
rlm@46 1845 case "end":
rlm@46 1846 res.node.style["v-text-align"] = "right";
rlm@46 1847 res.bbx = -round(res.W / 2);
rlm@46 1848 break;
rlm@46 1849 default:
rlm@46 1850 res.node.style["v-text-align"] = "center";
rlm@46 1851 break;
rlm@46 1852 }
rlm@46 1853 }
rlm@46 1854 };
rlm@46 1855 var addGradientFill = function (o, gradient) {
rlm@46 1856 o.attrs = o.attrs || {};
rlm@46 1857 var attrs = o.attrs,
rlm@46 1858 fill = o.node.getElementsByTagName("fill"),
rlm@46 1859 type = "linear",
rlm@46 1860 fxfy = ".5 .5";
rlm@46 1861 o.attrs.gradient = gradient;
rlm@46 1862 gradient = (gradient + E)[rp](radial_gradient, function (all, fx, fy) {
rlm@46 1863 type = "radial";
rlm@46 1864 if (fx && fy) {
rlm@46 1865 fx = toFloat(fx);
rlm@46 1866 fy = toFloat(fy);
rlm@46 1867 pow(fx - .5, 2) + pow(fy - .5, 2) > .25 && (fy = math.sqrt(.25 - pow(fx - .5, 2)) * ((fy > .5) * 2 - 1) + .5);
rlm@46 1868 fxfy = fx + S + fy;
rlm@46 1869 }
rlm@46 1870 return E;
rlm@46 1871 });
rlm@46 1872 gradient = gradient[split](/\s*\-\s*/);
rlm@46 1873 if (type == "linear") {
rlm@46 1874 var angle = gradient.shift();
rlm@46 1875 angle = -toFloat(angle);
rlm@46 1876 if (isNaN(angle)) {
rlm@46 1877 return null;
rlm@46 1878 }
rlm@46 1879 }
rlm@46 1880 var dots = parseDots(gradient);
rlm@46 1881 if (!dots) {
rlm@46 1882 return null;
rlm@46 1883 }
rlm@46 1884 o = o.shape || o.node;
rlm@46 1885 fill = fill[0] || createNode("fill");
rlm@46 1886 if (dots[length]) {
rlm@46 1887 fill.on = true;
rlm@46 1888 fill.method = "none";
rlm@46 1889 fill.type = (type == "radial") ? "gradientradial" : "gradient";
rlm@46 1890 fill.color = dots[0].color;
rlm@46 1891 fill.color2 = dots[dots[length] - 1].color;
rlm@46 1892 var clrs = [];
rlm@46 1893 for (var i = 0, ii = dots[length]; i < ii; i++) {
rlm@46 1894 dots[i].offset && clrs[push](dots[i].offset + S + dots[i].color);
rlm@46 1895 }
rlm@46 1896 fill.colors && (fill.colors.value = clrs[length] ? clrs[join](",") : "0% " + fill.color);
rlm@46 1897 if (type == "radial") {
rlm@46 1898 fill.focus = "100%";
rlm@46 1899 fill.focussize = fxfy;
rlm@46 1900 fill.focusposition = fxfy;
rlm@46 1901 } else {
rlm@46 1902 fill.angle = (270 - angle) % 360;
rlm@46 1903 }
rlm@46 1904 }
rlm@46 1905 return 1;
rlm@46 1906 };
rlm@46 1907 var Element = function (node, group, vml) {
rlm@46 1908 var Rotation = 0,
rlm@46 1909 RotX = 0,
rlm@46 1910 RotY = 0,
rlm@46 1911 Scale = 1;
rlm@46 1912 this[0] = node;
rlm@46 1913 this.id = R._oid++;
rlm@46 1914 this.node = node;
rlm@46 1915 node.raphael = this;
rlm@46 1916 this.X = 0;
rlm@46 1917 this.Y = 0;
rlm@46 1918 this.attrs = {};
rlm@46 1919 this.Group = group;
rlm@46 1920 this.paper = vml;
rlm@46 1921 this._ = {
rlm@46 1922 tx: 0,
rlm@46 1923 ty: 0,
rlm@46 1924 rt: {deg:0},
rlm@46 1925 sx: 1,
rlm@46 1926 sy: 1
rlm@46 1927 };
rlm@46 1928 !vml.bottom && (vml.bottom = this);
rlm@46 1929 this.prev = vml.top;
rlm@46 1930 vml.top && (vml.top.next = this);
rlm@46 1931 vml.top = this;
rlm@46 1932 this.next = null;
rlm@46 1933 };
rlm@46 1934 Element[proto].rotate = function (deg, cx, cy) {
rlm@46 1935 if (this.removed) {
rlm@46 1936 return this;
rlm@46 1937 }
rlm@46 1938 if (deg == null) {
rlm@46 1939 if (this._.rt.cx) {
rlm@46 1940 return [this._.rt.deg, this._.rt.cx, this._.rt.cy][join](S);
rlm@46 1941 }
rlm@46 1942 return this._.rt.deg;
rlm@46 1943 }
rlm@46 1944 deg = (deg + E)[split](separator);
rlm@46 1945 if (deg[length] - 1) {
rlm@46 1946 cx = toFloat(deg[1]);
rlm@46 1947 cy = toFloat(deg[2]);
rlm@46 1948 }
rlm@46 1949 deg = toFloat(deg[0]);
rlm@46 1950 if (cx != null) {
rlm@46 1951 this._.rt.deg = deg;
rlm@46 1952 } else {
rlm@46 1953 this._.rt.deg += deg;
rlm@46 1954 }
rlm@46 1955 cy == null && (cx = null);
rlm@46 1956 this._.rt.cx = cx;
rlm@46 1957 this._.rt.cy = cy;
rlm@46 1958 this.setBox(this.attrs, cx, cy);
rlm@46 1959 this.Group.style.rotation = this._.rt.deg;
rlm@46 1960 // gradient fix for rotation. TODO
rlm@46 1961 // var fill = (this.shape || this.node).getElementsByTagName("fill");
rlm@46 1962 // fill = fill[0] || {};
rlm@46 1963 // var b = ((360 - this._.rt.deg) - 270) % 360;
rlm@46 1964 // !R.is(fill.angle, "undefined") && (fill.angle = b);
rlm@46 1965 return this;
rlm@46 1966 };
rlm@46 1967 Element[proto].setBox = function (params, cx, cy) {
rlm@46 1968 if (this.removed) {
rlm@46 1969 return this;
rlm@46 1970 }
rlm@46 1971 var gs = this.Group.style,
rlm@46 1972 os = (this.shape && this.shape.style) || this.node.style;
rlm@46 1973 params = params || {};
rlm@46 1974 for (var i in params) if (params[has](i)) {
rlm@46 1975 this.attrs[i] = params[i];
rlm@46 1976 }
rlm@46 1977 cx = cx || this._.rt.cx;
rlm@46 1978 cy = cy || this._.rt.cy;
rlm@46 1979 var attr = this.attrs,
rlm@46 1980 x,
rlm@46 1981 y,
rlm@46 1982 w,
rlm@46 1983 h;
rlm@46 1984 switch (this.type) {
rlm@46 1985 case "circle":
rlm@46 1986 x = attr.cx - attr.r;
rlm@46 1987 y = attr.cy - attr.r;
rlm@46 1988 w = h = attr.r * 2;
rlm@46 1989 break;
rlm@46 1990 case "ellipse":
rlm@46 1991 x = attr.cx - attr.rx;
rlm@46 1992 y = attr.cy - attr.ry;
rlm@46 1993 w = attr.rx * 2;
rlm@46 1994 h = attr.ry * 2;
rlm@46 1995 break;
rlm@46 1996 case "rect":
rlm@46 1997 case "image":
rlm@46 1998 x = +attr.x;
rlm@46 1999 y = +attr.y;
rlm@46 2000 w = attr.width || 0;
rlm@46 2001 h = attr.height || 0;
rlm@46 2002 break;
rlm@46 2003 case "text":
rlm@46 2004 this.textpath.v = ["m", round(attr.x), ", ", round(attr.y - 2), "l", round(attr.x) + 1, ", ", round(attr.y - 2)][join](E);
rlm@46 2005 x = attr.x - round(this.W / 2);
rlm@46 2006 y = attr.y - this.H / 2;
rlm@46 2007 w = this.W;
rlm@46 2008 h = this.H;
rlm@46 2009 break;
rlm@46 2010 case "path":
rlm@46 2011 if (!this.attrs.path) {
rlm@46 2012 x = 0;
rlm@46 2013 y = 0;
rlm@46 2014 w = this.paper.width;
rlm@46 2015 h = this.paper.height;
rlm@46 2016 } else {
rlm@46 2017 var dim = pathDimensions(this.attrs.path);
rlm@46 2018 x = dim.x;
rlm@46 2019 y = dim.y;
rlm@46 2020 w = dim.width;
rlm@46 2021 h = dim.height;
rlm@46 2022 }
rlm@46 2023 break;
rlm@46 2024 default:
rlm@46 2025 x = 0;
rlm@46 2026 y = 0;
rlm@46 2027 w = this.paper.width;
rlm@46 2028 h = this.paper.height;
rlm@46 2029 break;
rlm@46 2030 }
rlm@46 2031 cx = (cx == null) ? x + w / 2 : cx;
rlm@46 2032 cy = (cy == null) ? y + h / 2 : cy;
rlm@46 2033 var left = cx - this.paper.width / 2,
rlm@46 2034 top = cy - this.paper.height / 2;
rlm@46 2035 if (this.type == "path" || this.type == "text") {
rlm@46 2036 (gs.left != left + "px") && (gs.left = left + "px");
rlm@46 2037 (gs.top != top + "px") && (gs.top = top + "px");
rlm@46 2038 this.X = this.type == "text" ? x : -left;
rlm@46 2039 this.Y = this.type == "text" ? y : -top;
rlm@46 2040 this.W = w;
rlm@46 2041 this.H = h;
rlm@46 2042 (os.left != -left + "px") && (os.left = -left + "px");
rlm@46 2043 (os.top != -top + "px") && (os.top = -top + "px");
rlm@46 2044 } else {
rlm@46 2045 (gs.left != left + "px") && (gs.left = left + "px");
rlm@46 2046 (gs.top != top + "px") && (gs.top = top + "px");
rlm@46 2047 this.X = x;
rlm@46 2048 this.Y = y;
rlm@46 2049 this.W = w;
rlm@46 2050 this.H = h;
rlm@46 2051 (gs.width != this.paper.width + "px") && (gs.width = this.paper.width + "px");
rlm@46 2052 (gs.height != this.paper.height + "px") && (gs.height = this.paper.height + "px");
rlm@46 2053 (os.left != x - left + "px") && (os.left = x - left + "px");
rlm@46 2054 (os.top != y - top + "px") && (os.top = y - top + "px");
rlm@46 2055 (os.width != w + "px") && (os.width = w + "px");
rlm@46 2056 (os.height != h + "px") && (os.height = h + "px");
rlm@46 2057 var arcsize = (+params.r || 0) / mmin(w, h);
rlm@46 2058 if (this.type == "rect" && this.arcsize.toFixed(4) != arcsize.toFixed(4) && (arcsize || this.arcsize)) {
rlm@46 2059 // We should replace element with the new one
rlm@46 2060 var o = createNode("roundrect"),
rlm@46 2061 a = {},
rlm@46 2062 i = 0,
rlm@46 2063 ii = this.events && this.events[length];
rlm@46 2064 o.arcsize = arcsize;
rlm@46 2065 o.raphael = this;
rlm@46 2066 this.Group[appendChild](o);
rlm@46 2067 this.Group.removeChild(this.node);
rlm@46 2068 this[0] = this.node = o;
rlm@46 2069 this.arcsize = arcsize;
rlm@46 2070 for (var i in attr) {
rlm@46 2071 a[i] = attr[i];
rlm@46 2072 }
rlm@46 2073 delete a.scale;
rlm@46 2074 this.attr(a);
rlm@46 2075 if (this.events) for (; i < ii; i++) {
rlm@46 2076 this.events[i].unbind = addEvent(this.node, this.events[i].name, this.events[i].f, this);
rlm@46 2077 }
rlm@46 2078 }
rlm@46 2079 }
rlm@46 2080 };
rlm@46 2081 Element[proto].hide = function () {
rlm@46 2082 !this.removed && (this.Group.style.display = "none");
rlm@46 2083 return this;
rlm@46 2084 };
rlm@46 2085 Element[proto].show = function () {
rlm@46 2086 !this.removed && (this.Group.style.display = "block");
rlm@46 2087 return this;
rlm@46 2088 };
rlm@46 2089 Element[proto].getBBox = function () {
rlm@46 2090 if (this.removed) {
rlm@46 2091 return this;
rlm@46 2092 }
rlm@46 2093 if (this.type == "path") {
rlm@46 2094 return pathDimensions(this.attrs.path);
rlm@46 2095 }
rlm@46 2096 return {
rlm@46 2097 x: this.X + (this.bbx || 0),
rlm@46 2098 y: this.Y,
rlm@46 2099 width: this.W,
rlm@46 2100 height: this.H
rlm@46 2101 };
rlm@46 2102 };
rlm@46 2103 Element[proto].remove = function () {
rlm@46 2104 if (this.removed) {
rlm@46 2105 return;
rlm@46 2106 }
rlm@46 2107 tear(this, this.paper);
rlm@46 2108 this.node.parentNode.removeChild(this.node);
rlm@46 2109 this.Group.parentNode.removeChild(this.Group);
rlm@46 2110 this.shape && this.shape.parentNode.removeChild(this.shape);
rlm@46 2111 for (var i in this) {
rlm@46 2112 delete this[i];
rlm@46 2113 }
rlm@46 2114 this.removed = true;
rlm@46 2115 };
rlm@46 2116 Element[proto].attr = function () {
rlm@46 2117 if (this.removed) {
rlm@46 2118 return this;
rlm@46 2119 }
rlm@46 2120 if (arguments[length] == 0) {
rlm@46 2121 var res = {};
rlm@46 2122 for (var i in this.attrs) if (this.attrs[has](i)) {
rlm@46 2123 res[i] = this.attrs[i];
rlm@46 2124 }
rlm@46 2125 this._.rt.deg && (res.rotation = this.rotate());
rlm@46 2126 (this._.sx != 1 || this._.sy != 1) && (res.scale = this.scale());
rlm@46 2127 res.gradient && res.fill == "none" && (res.fill = res.gradient) && delete res.gradient;
rlm@46 2128 return res;
rlm@46 2129 }
rlm@46 2130 if (arguments[length] == 1 && R.is(arguments[0], "string")) {
rlm@46 2131 if (arguments[0] == "translation") {
rlm@46 2132 return translate.call(this);
rlm@46 2133 }
rlm@46 2134 if (arguments[0] == "rotation") {
rlm@46 2135 return this.rotate();
rlm@46 2136 }
rlm@46 2137 if (arguments[0] == "scale") {
rlm@46 2138 return this.scale();
rlm@46 2139 }
rlm@46 2140 if (arguments[0] == "fill" && this.attrs.fill == "none" && this.attrs.gradient) {
rlm@46 2141 return this.attrs.gradient;
rlm@46 2142 }
rlm@46 2143 return this.attrs[arguments[0]];
rlm@46 2144 }
rlm@46 2145 if (this.attrs && arguments[length] == 1 && R.is(arguments[0], "array")) {
rlm@46 2146 var values = {};
rlm@46 2147 for (var i = 0, ii = arguments[0][length]; i < ii; i++) {
rlm@46 2148 values[arguments[0][i]] = this.attrs[arguments[0][i]];
rlm@46 2149 };
rlm@46 2150 return values;
rlm@46 2151 }
rlm@46 2152 var params;
rlm@46 2153 if (arguments[length] == 2) {
rlm@46 2154 params = {};
rlm@46 2155 params[arguments[0]] = arguments[1];
rlm@46 2156 }
rlm@46 2157 arguments[length] == 1 && R.is(arguments[0], "object") && (params = arguments[0]);
rlm@46 2158 if (params) {
rlm@46 2159 if (params.text && this.type == "text") {
rlm@46 2160 this.node.string = params.text;
rlm@46 2161 }
rlm@46 2162 setFillAndStroke(this, params);
rlm@46 2163 if (params.gradient && (({circle: 1, ellipse: 1})[has](this.type) || (params.gradient + E).charAt() != "r")) {
rlm@46 2164 addGradientFill(this, params.gradient);
rlm@46 2165 }
rlm@46 2166 (this.type != "path" || this._.rt.deg) && this.setBox(this.attrs);
rlm@46 2167 }
rlm@46 2168 return this;
rlm@46 2169 };
rlm@46 2170 Element[proto].toFront = function () {
rlm@46 2171 !this.removed && this.Group.parentNode[appendChild](this.Group);
rlm@46 2172 this.paper.top != this && tofront(this, this.paper);
rlm@46 2173 return this;
rlm@46 2174 };
rlm@46 2175 Element[proto].toBack = function () {
rlm@46 2176 if (this.removed) {
rlm@46 2177 return this;
rlm@46 2178 }
rlm@46 2179 if (this.Group.parentNode.firstChild != this.Group) {
rlm@46 2180 this.Group.parentNode.insertBefore(this.Group, this.Group.parentNode.firstChild);
rlm@46 2181 toback(this, this.paper);
rlm@46 2182 }
rlm@46 2183 return this;
rlm@46 2184 };
rlm@46 2185 Element[proto].insertAfter = function (element) {
rlm@46 2186 if (this.removed) {
rlm@46 2187 return this;
rlm@46 2188 }
rlm@46 2189 if (element.Group.nextSibling) {
rlm@46 2190 element.Group.parentNode.insertBefore(this.Group, element.Group.nextSibling);
rlm@46 2191 } else {
rlm@46 2192 element.Group.parentNode[appendChild](this.Group);
rlm@46 2193 }
rlm@46 2194 insertafter(this, element, this.paper);
rlm@46 2195 return this;
rlm@46 2196 };
rlm@46 2197 Element[proto].insertBefore = function (element) {
rlm@46 2198 if (this.removed) {
rlm@46 2199 return this;
rlm@46 2200 }
rlm@46 2201 element.Group.parentNode.insertBefore(this.Group, element.Group);
rlm@46 2202 insertbefore(this, element, this.paper);
rlm@46 2203 return this;
rlm@46 2204 };
rlm@46 2205
rlm@46 2206 var theCircle = function (vml, x, y, r) {
rlm@46 2207 var g = createNode("group"),
rlm@46 2208 o = createNode("oval"),
rlm@46 2209 ol = o.style;
rlm@46 2210 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@46 2211 g.coordsize = vml.coordsize;
rlm@46 2212 g.coordorigin = vml.coordorigin;
rlm@46 2213 g[appendChild](o);
rlm@46 2214 var res = new Element(o, g, vml);
rlm@46 2215 res.type = "circle";
rlm@46 2216 setFillAndStroke(res, {stroke: "#000", fill: "none"});
rlm@46 2217 res.attrs.cx = x;
rlm@46 2218 res.attrs.cy = y;
rlm@46 2219 res.attrs.r = r;
rlm@46 2220 res.setBox({x: x - r, y: y - r, width: r * 2, height: r * 2});
rlm@46 2221 vml.canvas[appendChild](g);
rlm@46 2222 return res;
rlm@46 2223 },
rlm@46 2224 theRect = function (vml, x, y, w, h, r) {
rlm@46 2225 var g = createNode("group"),
rlm@46 2226 o = createNode("roundrect"),
rlm@46 2227 arcsize = (+r || 0) / (mmin(w, h));
rlm@46 2228 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@46 2229 g.coordsize = vml.coordsize;
rlm@46 2230 g.coordorigin = vml.coordorigin;
rlm@46 2231 g[appendChild](o);
rlm@46 2232 o.arcsize = arcsize;
rlm@46 2233 var res = new Element(o, g, vml);
rlm@46 2234 res.type = "rect";
rlm@46 2235 setFillAndStroke(res, {stroke: "#000"});
rlm@46 2236 res.arcsize = arcsize;
rlm@46 2237 res.setBox({x: x, y: y, width: w, height: h, r: r});
rlm@46 2238 vml.canvas[appendChild](g);
rlm@46 2239 return res;
rlm@46 2240 },
rlm@46 2241 theEllipse = function (vml, x, y, rx, ry) {
rlm@46 2242 var g = createNode("group"),
rlm@46 2243 o = createNode("oval"),
rlm@46 2244 ol = o.style;
rlm@46 2245 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@46 2246 g.coordsize = vml.coordsize;
rlm@46 2247 g.coordorigin = vml.coordorigin;
rlm@46 2248 g[appendChild](o);
rlm@46 2249 var res = new Element(o, g, vml);
rlm@46 2250 res.type = "ellipse";
rlm@46 2251 setFillAndStroke(res, {stroke: "#000"});
rlm@46 2252 res.attrs.cx = x;
rlm@46 2253 res.attrs.cy = y;
rlm@46 2254 res.attrs.rx = rx;
rlm@46 2255 res.attrs.ry = ry;
rlm@46 2256 res.setBox({x: x - rx, y: y - ry, width: rx * 2, height: ry * 2});
rlm@46 2257 vml.canvas[appendChild](g);
rlm@46 2258 return res;
rlm@46 2259 },
rlm@46 2260 theImage = function (vml, src, x, y, w, h) {
rlm@46 2261 var g = createNode("group"),
rlm@46 2262 o = createNode("image"),
rlm@46 2263 ol = o.style;
rlm@46 2264 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@46 2265 g.coordsize = vml.coordsize;
rlm@46 2266 g.coordorigin = vml.coordorigin;
rlm@46 2267 o.src = src;
rlm@46 2268 g[appendChild](o);
rlm@46 2269 var res = new Element(o, g, vml);
rlm@46 2270 res.type = "image";
rlm@46 2271 res.attrs.src = src;
rlm@46 2272 res.attrs.x = x;
rlm@46 2273 res.attrs.y = y;
rlm@46 2274 res.attrs.w = w;
rlm@46 2275 res.attrs.h = h;
rlm@46 2276 res.setBox({x: x, y: y, width: w, height: h});
rlm@46 2277 vml.canvas[appendChild](g);
rlm@46 2278 return res;
rlm@46 2279 },
rlm@46 2280 theText = function (vml, x, y, text) {
rlm@46 2281 var g = createNode("group"),
rlm@46 2282 el = createNode("shape"),
rlm@46 2283 ol = el.style,
rlm@46 2284 path = createNode("path"),
rlm@46 2285 ps = path.style,
rlm@46 2286 o = createNode("textpath");
rlm@46 2287 g.style.cssText = "position:absolute;left:0;top:0;width:" + vml.width + "px;height:" + vml.height + "px";
rlm@46 2288 g.coordsize = vml.coordsize;
rlm@46 2289 g.coordorigin = vml.coordorigin;
rlm@46 2290 path.v = R.format("m{0},{1}l{2},{1}", round(x), round(y), round(x) + 1);
rlm@46 2291 path.textpathok = true;
rlm@46 2292 ol.width = vml.width;
rlm@46 2293 ol.height = vml.height;
rlm@46 2294 o.string = text + E;
rlm@46 2295 o.on = true;
rlm@46 2296 el[appendChild](o);
rlm@46 2297 el[appendChild](path);
rlm@46 2298 g[appendChild](el);
rlm@46 2299 var res = new Element(o, g, vml);
rlm@46 2300 res.shape = el;
rlm@46 2301 res.textpath = path;
rlm@46 2302 res.type = "text";
rlm@46 2303 res.attrs.text = text;
rlm@46 2304 res.attrs.x = x;
rlm@46 2305 res.attrs.y = y;
rlm@46 2306 res.attrs.w = 1;
rlm@46 2307 res.attrs.h = 1;
rlm@46 2308 setFillAndStroke(res, {font: availableAttrs.font, stroke: "none", fill: "#000"});
rlm@46 2309 res.setBox();
rlm@46 2310 vml.canvas[appendChild](g);
rlm@46 2311 return res;
rlm@46 2312 },
rlm@46 2313 setSize = function (width, height) {
rlm@46 2314 var cs = this.canvas.style;
rlm@46 2315 width == +width && (width += "px");
rlm@46 2316 height == +height && (height += "px");
rlm@46 2317 cs.width = width;
rlm@46 2318 cs.height = height;
rlm@46 2319 cs.clip = "rect(0 " + width + " " + height + " 0)";
rlm@46 2320 return this;
rlm@46 2321 },
rlm@46 2322 createNode;
rlm@46 2323 doc.createStyleSheet().addRule(".rvml", "behavior:url(#default#VML)");
rlm@46 2324 try {
rlm@46 2325 !doc.namespaces.rvml && doc.namespaces.add("rvml", "urn:schemas-microsoft-com:vml");
rlm@46 2326 createNode = function (tagName) {
rlm@46 2327 return doc.createElement('<rvml:' + tagName + ' class="rvml">');
rlm@46 2328 };
rlm@46 2329 } catch (e) {
rlm@46 2330 createNode = function (tagName) {
rlm@46 2331 return doc.createElement('<' + tagName + ' xmlns="urn:schemas-microsoft.com:vml" class="rvml">');
rlm@46 2332 };
rlm@46 2333 }
rlm@46 2334 var create = function () {
rlm@46 2335 var con = getContainer[apply](null, arguments),
rlm@46 2336 container = con.container,
rlm@46 2337 height = con.height,
rlm@46 2338 s,
rlm@46 2339 width = con.width,
rlm@46 2340 x = con.x,
rlm@46 2341 y = con.y;
rlm@46 2342 if (!container) {
rlm@46 2343 throw new Error("VML container not found.");
rlm@46 2344 }
rlm@46 2345 var res = new Paper,
rlm@46 2346 c = res.canvas = doc.createElement("div"),
rlm@46 2347 cs = c.style;
rlm@46 2348 width = width || 512;
rlm@46 2349 height = height || 342;
rlm@46 2350 width == +width && (width += "px");
rlm@46 2351 height == +height && (height += "px");
rlm@46 2352 res.width = 1e3;
rlm@46 2353 res.height = 1e3;
rlm@46 2354 res.coordsize = "1000 1000";
rlm@46 2355 res.coordorigin = "0 0";
rlm@46 2356 res.span = doc.createElement("span");
rlm@46 2357 res.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
rlm@46 2358 c[appendChild](res.span);
rlm@46 2359 cs.cssText = R.format("width:{0};height:{1};position:absolute;clip:rect(0 {0} {1} 0);overflow:hidden", width, height);
rlm@46 2360 if (container == 1) {
rlm@46 2361 doc.body[appendChild](c);
rlm@46 2362 cs.left = x + "px";
rlm@46 2363 cs.top = y + "px";
rlm@46 2364 } else {
rlm@46 2365 container.style.width = width;
rlm@46 2366 container.style.height = height;
rlm@46 2367 if (container.firstChild) {
rlm@46 2368 container.insertBefore(c, container.firstChild);
rlm@46 2369 } else {
rlm@46 2370 container[appendChild](c);
rlm@46 2371 }
rlm@46 2372 }
rlm@46 2373 plugins.call(res, res, R.fn);
rlm@46 2374 return res;
rlm@46 2375 };
rlm@46 2376 Paper[proto].clear = function () {
rlm@46 2377 this.canvas.innerHTML = E;
rlm@46 2378 this.span = doc.createElement("span");
rlm@46 2379 this.span.style.cssText = "position:absolute;left:-9999em;top:-9999em;padding:0;margin:0;line-height:1;display:inline;";
rlm@46 2380 this.canvas[appendChild](this.span);
rlm@46 2381 this.bottom = this.top = null;
rlm@46 2382 };
rlm@46 2383 Paper[proto].remove = function () {
rlm@46 2384 this.canvas.parentNode.removeChild(this.canvas);
rlm@46 2385 for (var i in this) {
rlm@46 2386 this[i] = removed(i);
rlm@46 2387 }
rlm@46 2388 };
rlm@46 2389 }
rlm@46 2390
rlm@46 2391 // rest
rlm@46 2392 // Safari or Chrome (WebKit) rendering bug workaround method
rlm@46 2393 if ((/^Apple|^Google/).test(navigator.vendor) && !(navigator.userAgent.indexOf("Version/4.0") + 1)) {
rlm@46 2394 Paper[proto].safari = function () {
rlm@46 2395 var rect = this.rect(-99, -99, this.width + 99, this.height + 99);
rlm@46 2396 setTimeout(function () {rect.remove();});
rlm@46 2397 };
rlm@46 2398 } else {
rlm@46 2399 Paper[proto].safari = function () {};
rlm@46 2400 }
rlm@46 2401
rlm@46 2402 // Events
rlm@46 2403 var addEvent = (function () {
rlm@46 2404 if (doc.addEventListener) {
rlm@46 2405 return function (obj, type, fn, element) {
rlm@46 2406 var f = function (e) {
rlm@46 2407 return fn.call(element, e);
rlm@46 2408 };
rlm@46 2409 obj.addEventListener(type, f, false);
rlm@46 2410 return function () {
rlm@46 2411 obj.removeEventListener(type, f, false);
rlm@46 2412 return true;
rlm@46 2413 };
rlm@46 2414 };
rlm@46 2415 } else if (doc.attachEvent) {
rlm@46 2416 return function (obj, type, fn, element) {
rlm@46 2417 var f = function (e) {
rlm@46 2418 return fn.call(element, e || win.event);
rlm@46 2419 };
rlm@46 2420 obj.attachEvent("on" + type, f);
rlm@46 2421 var detacher = function () {
rlm@46 2422 obj.detachEvent("on" + type, f);
rlm@46 2423 return true;
rlm@46 2424 };
rlm@46 2425 return detacher;
rlm@46 2426 };
rlm@46 2427 }
rlm@46 2428 })();
rlm@46 2429 for (var i = events[length]; i--;) {
rlm@46 2430 (function (eventName) {
rlm@46 2431 Element[proto][eventName] = function (fn) {
rlm@46 2432 if (R.is(fn, "function")) {
rlm@46 2433 this.events = this.events || [];
rlm@46 2434 this.events.push({name: eventName, f: fn, unbind: addEvent(this.shape || this.node, eventName, fn, this)});
rlm@46 2435 }
rlm@46 2436 return this;
rlm@46 2437 };
rlm@46 2438 Element[proto]["un" + eventName] = function (fn) {
rlm@46 2439 var events = this.events,
rlm@46 2440 l = events[length];
rlm@46 2441 while (l--) if (events[l].name == eventName && events[l].f == fn) {
rlm@46 2442 events[l].unbind();
rlm@46 2443 events.splice(l, 1);
rlm@46 2444 !events.length && delete this.events;
rlm@46 2445 return this;
rlm@46 2446 }
rlm@46 2447 return this;
rlm@46 2448 };
rlm@46 2449 })(events[i]);
rlm@46 2450 }
rlm@46 2451 Element[proto].hover = function (f_in, f_out) {
rlm@46 2452 return this.mouseover(f_in).mouseout(f_out);
rlm@46 2453 };
rlm@46 2454 Element[proto].unhover = function (f_in, f_out) {
rlm@46 2455 return this.unmouseover(f_in).unmouseout(f_out);
rlm@46 2456 };
rlm@46 2457 Paper[proto].circle = function (x, y, r) {
rlm@46 2458 return theCircle(this, x || 0, y || 0, r || 0);
rlm@46 2459 };
rlm@46 2460 Paper[proto].rect = function (x, y, w, h, r) {
rlm@46 2461 return theRect(this, x || 0, y || 0, w || 0, h || 0, r || 0);
rlm@46 2462 };
rlm@46 2463 Paper[proto].ellipse = function (x, y, rx, ry) {
rlm@46 2464 return theEllipse(this, x || 0, y || 0, rx || 0, ry || 0);
rlm@46 2465 };
rlm@46 2466 Paper[proto].path = function (pathString) {
rlm@46 2467 pathString && !R.is(pathString, "string") && !R.is(pathString[0], "array") && (pathString += E);
rlm@46 2468 return thePath(R.format[apply](R, arguments), this);
rlm@46 2469 };
rlm@46 2470 Paper[proto].image = function (src, x, y, w, h) {
rlm@46 2471 return theImage(this, src || "about:blank", x || 0, y || 0, w || 0, h || 0);
rlm@46 2472 };
rlm@46 2473 Paper[proto].text = function (x, y, text) {
rlm@46 2474 return theText(this, x || 0, y || 0, text || E);
rlm@46 2475 };
rlm@46 2476 Paper[proto].set = function (itemsArray) {
rlm@46 2477 arguments[length] > 1 && (itemsArray = Array[proto].splice.call(arguments, 0, arguments[length]));
rlm@46 2478 return new Set(itemsArray);
rlm@46 2479 };
rlm@46 2480 Paper[proto].setSize = setSize;
rlm@46 2481 Paper[proto].top = Paper[proto].bottom = null;
rlm@46 2482 Paper[proto].raphael = R;
rlm@46 2483 function x_y() {
rlm@46 2484 return this.x + S + this.y;
rlm@46 2485 };
rlm@46 2486 Element[proto].scale = function (x, y, cx, cy) {
rlm@46 2487 if (x == null && y == null) {
rlm@46 2488 return {
rlm@46 2489 x: this._.sx,
rlm@46 2490 y: this._.sy,
rlm@46 2491 toString: x_y
rlm@46 2492 };
rlm@46 2493 }
rlm@46 2494 y = y || x;
rlm@46 2495 !+y && (y = x);
rlm@46 2496 var dx,
rlm@46 2497 dy,
rlm@46 2498 dcx,
rlm@46 2499 dcy,
rlm@46 2500 a = this.attrs;
rlm@46 2501 if (x != 0) {
rlm@46 2502 var bb = this.getBBox(),
rlm@46 2503 rcx = bb.x + bb.width / 2,
rlm@46 2504 rcy = bb.y + bb.height / 2,
rlm@46 2505 kx = x / this._.sx,
rlm@46 2506 ky = y / this._.sy;
rlm@46 2507 cx = (+cx || cx == 0) ? cx : rcx;
rlm@46 2508 cy = (+cy || cy == 0) ? cy : rcy;
rlm@46 2509 var dirx = ~~(x / math.abs(x)),
rlm@46 2510 diry = ~~(y / math.abs(y)),
rlm@46 2511 s = this.node.style,
rlm@46 2512 ncx = cx + (rcx - cx) * kx,
rlm@46 2513 ncy = cy + (rcy - cy) * ky;
rlm@46 2514 switch (this.type) {
rlm@46 2515 case "rect":
rlm@46 2516 case "image":
rlm@46 2517 var neww = a.width * dirx * kx,
rlm@46 2518 newh = a.height * diry * ky;
rlm@46 2519 this.attr({
rlm@46 2520 height: newh,
rlm@46 2521 r: a.r * mmin(dirx * kx, diry * ky),
rlm@46 2522 width: neww,
rlm@46 2523 x: ncx - neww / 2,
rlm@46 2524 y: ncy - newh / 2
rlm@46 2525 });
rlm@46 2526 break;
rlm@46 2527 case "circle":
rlm@46 2528 case "ellipse":
rlm@46 2529 this.attr({
rlm@46 2530 rx: a.rx * dirx * kx,
rlm@46 2531 ry: a.ry * diry * ky,
rlm@46 2532 r: a.r * mmin(dirx * kx, diry * ky),
rlm@46 2533 cx: ncx,
rlm@46 2534 cy: ncy
rlm@46 2535 });
rlm@46 2536 break;
rlm@46 2537 case "path":
rlm@46 2538 var path = pathToRelative(a.path),
rlm@46 2539 skip = true;
rlm@46 2540 for (var i = 0, ii = path[length]; i < ii; i++) {
rlm@46 2541 var p = path[i],
rlm@46 2542 j,
rlm@46 2543 P0 = upperCase.call(p[0]);
rlm@46 2544 if (P0 == "M" && skip) {
rlm@46 2545 continue;
rlm@46 2546 } else {
rlm@46 2547 skip = false;
rlm@46 2548 }
rlm@46 2549 if (P0 == "A") {
rlm@46 2550 p[path[i][length] - 2] *= kx;
rlm@46 2551 p[path[i][length] - 1] *= ky;
rlm@46 2552 p[1] *= dirx * kx;
rlm@46 2553 p[2] *= diry * ky;
rlm@46 2554 p[5] = +(dirx + diry ? !!+p[5] : !+p[5]);
rlm@46 2555 } else if (P0 == "H") {
rlm@46 2556 for (j = 1, jj = p[length]; j < jj; j++) {
rlm@46 2557 p[j] *= kx;
rlm@46 2558 }
rlm@46 2559 } else if (P0 == "V") {
rlm@46 2560 for (j = 1, jj = p[length]; j < jj; j++) {
rlm@46 2561 p[j] *= ky;
rlm@46 2562 }
rlm@46 2563 } else {
rlm@46 2564 for (j = 1, jj = p[length]; j < jj; j++) {
rlm@46 2565 p[j] *= (j % 2) ? kx : ky;
rlm@46 2566 }
rlm@46 2567 }
rlm@46 2568 }
rlm@46 2569 var dim2 = pathDimensions(path),
rlm@46 2570 dx = ncx - dim2.x - dim2.width / 2,
rlm@46 2571 dy = ncy - dim2.y - dim2.height / 2;
rlm@46 2572 path[0][1] += dx;
rlm@46 2573 path[0][2] += dy;
rlm@46 2574 this.attr({path: path});
rlm@46 2575 break;
rlm@46 2576 }
rlm@46 2577 if (this.type in {text: 1, image:1} && (dirx != 1 || diry != 1)) {
rlm@46 2578 if (this.transformations) {
rlm@46 2579 this.transformations[2] = "scale("[concat](dirx, ",", diry, ")");
rlm@46 2580 this.node[setAttribute]("transform", this.transformations[join](S));
rlm@46 2581 dx = (dirx == -1) ? -a.x - (neww || 0) : a.x;
rlm@46 2582 dy = (diry == -1) ? -a.y - (newh || 0) : a.y;
rlm@46 2583 this.attr({x: dx, y: dy});
rlm@46 2584 a.fx = dirx - 1;
rlm@46 2585 a.fy = diry - 1;
rlm@46 2586 } else {
rlm@46 2587 this.node.filterMatrix = " progid:DXImageTransform.Microsoft.Matrix(M11="[concat](dirx,
rlm@46 2588 ", M12=0, M21=0, M22=", diry,
rlm@46 2589 ", Dx=0, Dy=0, sizingmethod='auto expand', filtertype='bilinear')");
rlm@46 2590 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
rlm@46 2591 }
rlm@46 2592 } else {
rlm@46 2593 if (this.transformations) {
rlm@46 2594 this.transformations[2] = E;
rlm@46 2595 this.node[setAttribute]("transform", this.transformations[join](S));
rlm@46 2596 a.fx = 0;
rlm@46 2597 a.fy = 0;
rlm@46 2598 } else {
rlm@46 2599 this.node.filterMatrix = E;
rlm@46 2600 s.filter = (this.node.filterMatrix || E) + (this.node.filterOpacity || E);
rlm@46 2601 }
rlm@46 2602 }
rlm@46 2603 a.scale = [x, y, cx, cy][join](S);
rlm@46 2604 this._.sx = x;
rlm@46 2605 this._.sy = y;
rlm@46 2606 }
rlm@46 2607 return this;
rlm@46 2608 };
rlm@46 2609 Element[proto].clone = function () {
rlm@46 2610 var attr = this.attr();
rlm@46 2611 delete attr.scale;
rlm@46 2612 delete attr.translation;
rlm@46 2613 return this.paper[this.type]().attr(attr);
rlm@46 2614 };
rlm@46 2615 var getLengthFactory = function (istotal, subpath) {
rlm@46 2616 return function (path, length, onlystart) {
rlm@46 2617 path = path2curve(path);
rlm@46 2618 var x, y, p, l, sp = "", subpaths = {}, point,
rlm@46 2619 len = 0;
rlm@46 2620 for (var i = 0, ii = path.length; i < ii; i++) {
rlm@46 2621 p = path[i];
rlm@46 2622 if (p[0] == "M") {
rlm@46 2623 x = +p[1];
rlm@46 2624 y = +p[2];
rlm@46 2625 } else {
rlm@46 2626 l = segmentLength(x, y, p[1], p[2], p[3], p[4], p[5], p[6]);
rlm@46 2627 if (len + l > length) {
rlm@46 2628 if (subpath && !subpaths.start) {
rlm@46 2629 point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
rlm@46 2630 sp += ["C", point.start.x, point.start.y, point.m.x, point.m.y, point.x, point.y];
rlm@46 2631 if (onlystart) {
rlm@46 2632 return sp;
rlm@46 2633 }
rlm@46 2634 subpaths.start = sp;
rlm@46 2635 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@46 2636 len += l;
rlm@46 2637 x = +p[5];
rlm@46 2638 y = +p[6];
rlm@46 2639 continue;
rlm@46 2640 }
rlm@46 2641 if (!istotal && !subpath) {
rlm@46 2642 point = R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], (length - len) / l);
rlm@46 2643 return {x: point.x, y: point.y, alpha: point.alpha};
rlm@46 2644 }
rlm@46 2645 }
rlm@46 2646 len += l;
rlm@46 2647 x = +p[5];
rlm@46 2648 y = +p[6];
rlm@46 2649 }
rlm@46 2650 sp += p;
rlm@46 2651 }
rlm@46 2652 subpaths.end = sp;
rlm@46 2653 point = istotal ? len : subpath ? subpaths : R.findDotsAtSegment(x, y, p[1], p[2], p[3], p[4], p[5], p[6], 1);
rlm@46 2654 point.alpha && (point = {x: point.x, y: point.y, alpha: point.alpha});
rlm@46 2655 return point;
rlm@46 2656 };
rlm@46 2657 },
rlm@46 2658 segmentLength = cacher(function (p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y) {
rlm@46 2659 var old = {x: 0, y: 0},
rlm@46 2660 len = 0;
rlm@46 2661 for (var i = 0; i < 1.01; i+=.01) {
rlm@46 2662 var dot = findDotAtSegment(p1x, p1y, c1x, c1y, c2x, c2y, p2x, p2y, i);
rlm@46 2663 i && (len += math.sqrt(pow(old.x - dot.x, 2) + pow(old.y - dot.y, 2)));
rlm@46 2664 old = dot;
rlm@46 2665 }
rlm@46 2666 return len;
rlm@46 2667 });
rlm@46 2668 var getTotalLength = getLengthFactory(1),
rlm@46 2669 getPointAtLength = getLengthFactory(),
rlm@46 2670 getSubpathsAtLength = getLengthFactory(0, 1);
rlm@46 2671 Element[proto].getTotalLength = function () {
rlm@46 2672 if (this.type != "path") return;
rlm@46 2673 return getTotalLength(this.attrs.path);
rlm@46 2674 };
rlm@46 2675 Element[proto].getPointAtLength = function (length) {
rlm@46 2676 if (this.type != "path") return;
rlm@46 2677 return getPointAtLength(this.attrs.path, length);
rlm@46 2678 };
rlm@46 2679 Element[proto].getSubpath = function (from, to) {
rlm@46 2680 if (this.type != "path") return;
rlm@46 2681 var a = getSubpathsAtLength(this.attrs.path, to, 1);
rlm@46 2682 return from ? getSubpathsAtLength(a, from).end : a;
rlm@46 2683 };
rlm@46 2684
rlm@46 2685 // animation easing formulas
rlm@46 2686 R.easing_formulas = {
rlm@46 2687 linear: function (n) {
rlm@46 2688 return n;
rlm@46 2689 },
rlm@46 2690 "<": function (n) {
rlm@46 2691 return pow(n, 3);
rlm@46 2692 },
rlm@46 2693 ">": function (n) {
rlm@46 2694 return pow(n - 1, 3) + 1;
rlm@46 2695 },
rlm@46 2696 "<>": function (n) {
rlm@46 2697 n = n * 2;
rlm@46 2698 if (n < 1) {
rlm@46 2699 return pow(n, 3) / 2;
rlm@46 2700 }
rlm@46 2701 n -= 2;
rlm@46 2702 return (pow(n, 3) + 2) / 2;
rlm@46 2703 },
rlm@46 2704 backIn: function (n) {
rlm@46 2705 var s = 1.70158;
rlm@46 2706 return n * n * ((s + 1) * n - s);
rlm@46 2707 },
rlm@46 2708 backOut: function (n) {
rlm@46 2709 n = n - 1;
rlm@46 2710 var s = 1.70158;
rlm@46 2711 return n * n * ((s + 1) * n + s) + 1;
rlm@46 2712 },
rlm@46 2713 elastic: function (n) {
rlm@46 2714 if (n == 0 || n == 1) {
rlm@46 2715 return n;
rlm@46 2716 }
rlm@46 2717 var p = .3,
rlm@46 2718 s = p / 4;
rlm@46 2719 return pow(2, -10 * n) * math.sin((n - s) * (2 * math.PI) / p) + 1;
rlm@46 2720 },
rlm@46 2721 bounce: function (n) {
rlm@46 2722 var s = 7.5625,
rlm@46 2723 p = 2.75,
rlm@46 2724 l;
rlm@46 2725 if (n < (1 / p)) {
rlm@46 2726 l = s * n * n;
rlm@46 2727 } else {
rlm@46 2728 if (n < (2 / p)) {
rlm@46 2729 n -= (1.5 / p);
rlm@46 2730 l = s * n * n + .75;
rlm@46 2731 } else {
rlm@46 2732 if (n < (2.5 / p)) {
rlm@46 2733 n -= (2.25 / p);
rlm@46 2734 l = s * n * n + .9375;
rlm@46 2735 } else {
rlm@46 2736 n -= (2.625 / p);
rlm@46 2737 l = s * n * n + .984375;
rlm@46 2738 }
rlm@46 2739 }
rlm@46 2740 }
rlm@46 2741 return l;
rlm@46 2742 }
rlm@46 2743 };
rlm@46 2744
rlm@46 2745 var animationElements = {length : 0},
rlm@46 2746 animation = function () {
rlm@46 2747 var Now = +new Date;
rlm@46 2748 for (var l in animationElements) if (l != "length" && animationElements[has](l)) {
rlm@46 2749 var e = animationElements[l];
rlm@46 2750 if (e.stop) {
rlm@46 2751 delete animationElements[l];
rlm@46 2752 animationElements[length]--;
rlm@46 2753 continue;
rlm@46 2754 }
rlm@46 2755 var time = Now - e.start,
rlm@46 2756 ms = e.ms,
rlm@46 2757 easing = e.easing,
rlm@46 2758 from = e.from,
rlm@46 2759 diff = e.diff,
rlm@46 2760 to = e.to,
rlm@46 2761 t = e.t,
rlm@46 2762 prev = e.prev || 0,
rlm@46 2763 that = e.el,
rlm@46 2764 callback = e.callback,
rlm@46 2765 set = {},
rlm@46 2766 now;
rlm@46 2767 if (time < ms) {
rlm@46 2768 var pos = R.easing_formulas[easing] ? R.easing_formulas[easing](time / ms) : time / ms;
rlm@46 2769 for (var attr in from) if (from[has](attr)) {
rlm@46 2770 switch (availableAnimAttrs[attr]) {
rlm@46 2771 case "along":
rlm@46 2772 now = pos * ms * diff[attr];
rlm@46 2773 to.back && (now = to.len - now);
rlm@46 2774 var point = getPointAtLength(to[attr], now);
rlm@46 2775 that.translate(diff.sx - diff.x || 0, diff.sy - diff.y || 0);
rlm@46 2776 diff.x = point.x;
rlm@46 2777 diff.y = point.y;
rlm@46 2778 that.translate(point.x - diff.sx, point.y - diff.sy);
rlm@46 2779 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
rlm@46 2780 break;
rlm@46 2781 case "number":
rlm@46 2782 now = +from[attr] + pos * ms * diff[attr];
rlm@46 2783 break;
rlm@46 2784 case "colour":
rlm@46 2785 now = "rgb(" + [
rlm@46 2786 upto255(round(from[attr].r + pos * ms * diff[attr].r)),
rlm@46 2787 upto255(round(from[attr].g + pos * ms * diff[attr].g)),
rlm@46 2788 upto255(round(from[attr].b + pos * ms * diff[attr].b))
rlm@46 2789 ][join](",") + ")";
rlm@46 2790 break;
rlm@46 2791 case "path":
rlm@46 2792 now = [];
rlm@46 2793 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
rlm@46 2794 now[i] = [from[attr][i][0]];
rlm@46 2795 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
rlm@46 2796 now[i][j] = +from[attr][i][j] + pos * ms * diff[attr][i][j];
rlm@46 2797 }
rlm@46 2798 now[i] = now[i][join](S);
rlm@46 2799 }
rlm@46 2800 now = now[join](S);
rlm@46 2801 break;
rlm@46 2802 case "csv":
rlm@46 2803 switch (attr) {
rlm@46 2804 case "translation":
rlm@46 2805 var x = diff[attr][0] * (time - prev),
rlm@46 2806 y = diff[attr][1] * (time - prev);
rlm@46 2807 t.x += x;
rlm@46 2808 t.y += y;
rlm@46 2809 now = x + S + y;
rlm@46 2810 break;
rlm@46 2811 case "rotation":
rlm@46 2812 now = +from[attr][0] + pos * ms * diff[attr][0];
rlm@46 2813 from[attr][1] && (now += "," + from[attr][1] + "," + from[attr][2]);
rlm@46 2814 break;
rlm@46 2815 case "scale":
rlm@46 2816 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@46 2817 break;
rlm@46 2818 case "clip-rect":
rlm@46 2819 now = [];
rlm@46 2820 var i = 4;
rlm@46 2821 while (i--) {
rlm@46 2822 now[i] = +from[attr][i] + pos * ms * diff[attr][i];
rlm@46 2823 }
rlm@46 2824 break;
rlm@46 2825 }
rlm@46 2826 break;
rlm@46 2827 }
rlm@46 2828 set[attr] = now;
rlm@46 2829 }
rlm@46 2830 that.attr(set);
rlm@46 2831 that._run && that._run.call(that);
rlm@46 2832 } else {
rlm@46 2833 if (to.along) {
rlm@46 2834 var point = getPointAtLength(to.along, to.len * !to.back);
rlm@46 2835 that.translate(diff.sx - (diff.x || 0) + point.x - diff.sx, diff.sy - (diff.y || 0) + point.y - diff.sy);
rlm@46 2836 to.rot && that.rotate(diff.r + point.alpha, point.x, point.y);
rlm@46 2837 }
rlm@46 2838 (t.x || t.y) && that.translate(-t.x, -t.y);
rlm@46 2839 to.scale && (to.scale = to.scale + E);
rlm@46 2840 that.attr(to);
rlm@46 2841 delete animationElements[l];
rlm@46 2842 animationElements[length]--;
rlm@46 2843 that.in_animation = null;
rlm@46 2844 R.is(callback, "function") && callback.call(that);
rlm@46 2845 }
rlm@46 2846 e.prev = time;
rlm@46 2847 }
rlm@46 2848 R.svg && that && that.paper.safari();
rlm@46 2849 animationElements[length] && setTimeout(animation);
rlm@46 2850 },
rlm@46 2851 upto255 = function (color) {
rlm@46 2852 return color > 255 ? 255 : (color < 0 ? 0 : color);
rlm@46 2853 },
rlm@46 2854 translate = function (x, y) {
rlm@46 2855 if (x == null) {
rlm@46 2856 return {x: this._.tx, y: this._.ty, toString: x_y};
rlm@46 2857 }
rlm@46 2858 this._.tx += +x;
rlm@46 2859 this._.ty += +y;
rlm@46 2860 switch (this.type) {
rlm@46 2861 case "circle":
rlm@46 2862 case "ellipse":
rlm@46 2863 this.attr({cx: +x + this.attrs.cx, cy: +y + this.attrs.cy});
rlm@46 2864 break;
rlm@46 2865 case "rect":
rlm@46 2866 case "image":
rlm@46 2867 case "text":
rlm@46 2868 this.attr({x: +x + this.attrs.x, y: +y + this.attrs.y});
rlm@46 2869 break;
rlm@46 2870 case "path":
rlm@46 2871 var path = pathToRelative(this.attrs.path);
rlm@46 2872 path[0][1] += +x;
rlm@46 2873 path[0][2] += +y;
rlm@46 2874 this.attr({path: path});
rlm@46 2875 break;
rlm@46 2876 }
rlm@46 2877 return this;
rlm@46 2878 };
rlm@46 2879 Element[proto].animateWith = function (element, params, ms, easing, callback) {
rlm@46 2880 animationElements[element.id] && (params.start = animationElements[element.id].start);
rlm@46 2881 return this.animate(params, ms, easing, callback);
rlm@46 2882 };
rlm@46 2883 Element[proto].animateAlong = along();
rlm@46 2884 Element[proto].animateAlongBack = along(1);
rlm@46 2885 function along(isBack) {
rlm@46 2886 return function (path, ms, rotate, callback) {
rlm@46 2887 var params = {back: isBack};
rlm@46 2888 R.is(rotate, "function") ? (callback = rotate) : (params.rot = rotate);
rlm@46 2889 path && path.constructor == Element && (path = path.attrs.path);
rlm@46 2890 path && (params.along = path);
rlm@46 2891 return this.animate(params, ms, callback);
rlm@46 2892 };
rlm@46 2893 }
rlm@46 2894 Element[proto].onAnimation = function (f) {
rlm@46 2895 this._run = f || 0;
rlm@46 2896 return this;
rlm@46 2897 };
rlm@46 2898 Element[proto].animate = function (params, ms, easing, callback) {
rlm@46 2899 if (R.is(easing, "function") || !easing) {
rlm@46 2900 callback = easing || null;
rlm@46 2901 }
rlm@46 2902 var from = {},
rlm@46 2903 to = {},
rlm@46 2904 diff = {};
rlm@46 2905 for (var attr in params) if (params[has](attr)) {
rlm@46 2906 if (availableAnimAttrs[has](attr)) {
rlm@46 2907 from[attr] = this.attr(attr);
rlm@46 2908 (from[attr] == null) && (from[attr] = availableAttrs[attr]);
rlm@46 2909 to[attr] = params[attr];
rlm@46 2910 switch (availableAnimAttrs[attr]) {
rlm@46 2911 case "along":
rlm@46 2912 var len = getTotalLength(params[attr]),
rlm@46 2913 point = getPointAtLength(params[attr], len * !!params.back),
rlm@46 2914 bb = this.getBBox();
rlm@46 2915 diff[attr] = len / ms;
rlm@46 2916 diff.tx = bb.x;
rlm@46 2917 diff.ty = bb.y;
rlm@46 2918 diff.sx = point.x;
rlm@46 2919 diff.sy = point.y;
rlm@46 2920 to.rot = params.rot;
rlm@46 2921 to.back = params.back;
rlm@46 2922 to.len = len;
rlm@46 2923 params.rot && (diff.r = toFloat(this.rotate()) || 0);
rlm@46 2924 break;
rlm@46 2925 case "number":
rlm@46 2926 diff[attr] = (to[attr] - from[attr]) / ms;
rlm@46 2927 break;
rlm@46 2928 case "colour":
rlm@46 2929 from[attr] = R.getRGB(from[attr]);
rlm@46 2930 var toColour = R.getRGB(to[attr]);
rlm@46 2931 diff[attr] = {
rlm@46 2932 r: (toColour.r - from[attr].r) / ms,
rlm@46 2933 g: (toColour.g - from[attr].g) / ms,
rlm@46 2934 b: (toColour.b - from[attr].b) / ms
rlm@46 2935 };
rlm@46 2936 break;
rlm@46 2937 case "path":
rlm@46 2938 var pathes = path2curve(from[attr], to[attr]);
rlm@46 2939 from[attr] = pathes[0];
rlm@46 2940 var toPath = pathes[1];
rlm@46 2941 diff[attr] = [];
rlm@46 2942 for (var i = 0, ii = from[attr][length]; i < ii; i++) {
rlm@46 2943 diff[attr][i] = [0];
rlm@46 2944 for (var j = 1, jj = from[attr][i][length]; j < jj; j++) {
rlm@46 2945 diff[attr][i][j] = (toPath[i][j] - from[attr][i][j]) / ms;
rlm@46 2946 }
rlm@46 2947 }
rlm@46 2948 break;
rlm@46 2949 case "csv":
rlm@46 2950 var values = (params[attr] + E)[split](separator),
rlm@46 2951 from2 = (from[attr] + E)[split](separator);
rlm@46 2952 switch (attr) {
rlm@46 2953 case "translation":
rlm@46 2954 from[attr] = [0, 0];
rlm@46 2955 diff[attr] = [values[0] / ms, values[1] / ms];
rlm@46 2956 break;
rlm@46 2957 case "rotation":
rlm@46 2958 from[attr] = (from2[1] == values[1] && from2[2] == values[2]) ? from2 : [0, values[1], values[2]];
rlm@46 2959 diff[attr] = [(values[0] - from[attr][0]) / ms, 0, 0];
rlm@46 2960 break;
rlm@46 2961 case "scale":
rlm@46 2962 params[attr] = values;
rlm@46 2963 from[attr] = (from[attr] + E)[split](separator);
rlm@46 2964 diff[attr] = [(values[0] - from[attr][0]) / ms, (values[1] - from[attr][1]) / ms, 0, 0];
rlm@46 2965 break;
rlm@46 2966 case "clip-rect":
rlm@46 2967 from[attr] = (from[attr] + E)[split](separator);
rlm@46 2968 diff[attr] = [];
rlm@46 2969 var i = 4;
rlm@46 2970 while (i--) {
rlm@46 2971 diff[attr][i] = (values[i] - from[attr][i]) / ms;
rlm@46 2972 }
rlm@46 2973 break;
rlm@46 2974 }
rlm@46 2975 to[attr] = values;
rlm@46 2976 }
rlm@46 2977 }
rlm@46 2978 }
rlm@46 2979 this.stop();
rlm@46 2980 this.in_animation = 1;
rlm@46 2981 animationElements[this.id] = {
rlm@46 2982 start: params.start || +new Date,
rlm@46 2983 ms: ms,
rlm@46 2984 easing: easing,
rlm@46 2985 from: from,
rlm@46 2986 diff: diff,
rlm@46 2987 to: to,
rlm@46 2988 el: this,
rlm@46 2989 callback: callback,
rlm@46 2990 t: {x: 0, y: 0}
rlm@46 2991 };
rlm@46 2992 ++animationElements[length] == 1 && animation();
rlm@46 2993 return this;
rlm@46 2994 };
rlm@46 2995 Element[proto].stop = function () {
rlm@46 2996 animationElements[this.id] && animationElements[length]--;
rlm@46 2997 delete animationElements[this.id];
rlm@46 2998 return this;
rlm@46 2999 };
rlm@46 3000 Element[proto].translate = function (x, y) {
rlm@46 3001 return this.attr({translation: x + " " + y});
rlm@46 3002 };
rlm@46 3003 Element[proto][toString] = function () {
rlm@46 3004 return "Rapha\xebl\u2019s object";
rlm@46 3005 };
rlm@46 3006 R.ae = animationElements;
rlm@46 3007
rlm@46 3008 // Set
rlm@46 3009 var Set = function (items) {
rlm@46 3010 this.items = [];
rlm@46 3011 this[length] = 0;
rlm@46 3012 if (items) {
rlm@46 3013 for (var i = 0, ii = items[length]; i < ii; i++) {
rlm@46 3014 if (items[i] && (items[i].constructor == Element || items[i].constructor == Set)) {
rlm@46 3015 this[this.items[length]] = this.items[this.items[length]] = items[i];
rlm@46 3016 this[length]++;
rlm@46 3017 }
rlm@46 3018 }
rlm@46 3019 }
rlm@46 3020 };
rlm@46 3021 Set[proto][push] = function () {
rlm@46 3022 var item,
rlm@46 3023 len;
rlm@46 3024 for (var i = 0, ii = arguments[length]; i < ii; i++) {
rlm@46 3025 item = arguments[i];
rlm@46 3026 if (item && (item.constructor == Element || item.constructor == Set)) {
rlm@46 3027 len = this.items[length];
rlm@46 3028 this[len] = this.items[len] = item;
rlm@46 3029 this[length]++;
rlm@46 3030 }
rlm@46 3031 }
rlm@46 3032 return this;
rlm@46 3033 };
rlm@46 3034 Set[proto].pop = function () {
rlm@46 3035 delete this[this[length]--];
rlm@46 3036 return this.items.pop();
rlm@46 3037 };
rlm@46 3038 for (var method in Element[proto]) if (Element[proto][has](method)) {
rlm@46 3039 Set[proto][method] = (function (methodname) {
rlm@46 3040 return function () {
rlm@46 3041 for (var i = 0, ii = this.items[length]; i < ii; i++) {
rlm@46 3042 this.items[i][methodname][apply](this.items[i], arguments);
rlm@46 3043 }
rlm@46 3044 return this;
rlm@46 3045 };
rlm@46 3046 })(method);
rlm@46 3047 }
rlm@46 3048 Set[proto].attr = function (name, value) {
rlm@46 3049 if (name && R.is(name, "array") && R.is(name[0], "object")) {
rlm@46 3050 for (var j = 0, jj = name[length]; j < jj; j++) {
rlm@46 3051 this.items[j].attr(name[j]);
rlm@46 3052 }
rlm@46 3053 } else {
rlm@46 3054 for (var i = 0, ii = this.items[length]; i < ii; i++) {
rlm@46 3055 this.items[i].attr[apply](this.items[i], arguments);
rlm@46 3056 }
rlm@46 3057 }
rlm@46 3058 return this;
rlm@46 3059 };
rlm@46 3060 Set[proto].animate = function (params, ms, easing, callback) {
rlm@46 3061 (R.is(easing, "function") || !easing) && (callback = easing || null);
rlm@46 3062 var len = this.items[length],
rlm@46 3063 i = len,
rlm@46 3064 set = this,
rlm@46 3065 collector;
rlm@46 3066 callback && (collector = function () {
rlm@46 3067 !--len && callback.call(set);
rlm@46 3068 });
rlm@46 3069 this.items[--i].animate(params, ms, easing || collector, collector);
rlm@46 3070 while (i--) {
rlm@46 3071 this.items[i].animateWith(this.items[len - 1], params, ms, easing || collector, collector);
rlm@46 3072 }
rlm@46 3073 return this;
rlm@46 3074 };
rlm@46 3075 Set[proto].insertAfter = function (el) {
rlm@46 3076 var i = this.items[length];
rlm@46 3077 while (i--) {
rlm@46 3078 this.items[i].insertAfter(el);
rlm@46 3079 }
rlm@46 3080 return this;
rlm@46 3081 };
rlm@46 3082 Set[proto].getBBox = function () {
rlm@46 3083 var x = [],
rlm@46 3084 y = [],
rlm@46 3085 w = [],
rlm@46 3086 h = [];
rlm@46 3087 for (var i = this.items[length]; i--;) {
rlm@46 3088 var box = this.items[i].getBBox();
rlm@46 3089 x[push](box.x);
rlm@46 3090 y[push](box.y);
rlm@46 3091 w[push](box.x + box.width);
rlm@46 3092 h[push](box.y + box.height);
rlm@46 3093 }
rlm@46 3094 x = mmin[apply](0, x);
rlm@46 3095 y = mmin[apply](0, y);
rlm@46 3096 return {
rlm@46 3097 x: x,
rlm@46 3098 y: y,
rlm@46 3099 width: mmax[apply](0, w) - x,
rlm@46 3100 height: mmax[apply](0, h) - y
rlm@46 3101 };
rlm@46 3102 };
rlm@46 3103
rlm@46 3104 R.registerFont = function (font) {
rlm@46 3105 if (!font.face) {
rlm@46 3106 return font;
rlm@46 3107 }
rlm@46 3108 this.fonts = this.fonts || {};
rlm@46 3109 var fontcopy = {
rlm@46 3110 w: font.w,
rlm@46 3111 face: {},
rlm@46 3112 glyphs: {}
rlm@46 3113 },
rlm@46 3114 family = font.face["font-family"];
rlm@46 3115 for (var prop in font.face) if (font.face[has](prop)) {
rlm@46 3116 fontcopy.face[prop] = font.face[prop];
rlm@46 3117 }
rlm@46 3118 if (this.fonts[family]) {
rlm@46 3119 this.fonts[family][push](fontcopy);
rlm@46 3120 } else {
rlm@46 3121 this.fonts[family] = [fontcopy];
rlm@46 3122 }
rlm@46 3123 if (!font.svg) {
rlm@46 3124 fontcopy.face["units-per-em"] = toInt(font.face["units-per-em"], 10);
rlm@46 3125 for (var glyph in font.glyphs) if (font.glyphs[has](glyph)) {
rlm@46 3126 var path = font.glyphs[glyph];
rlm@46 3127 fontcopy.glyphs[glyph] = {
rlm@46 3128 w: path.w,
rlm@46 3129 k: {},
rlm@46 3130 d: path.d && "M" + path.d[rp](/[mlcxtrv]/g, function (command) {
rlm@46 3131 return {l: "L", c: "C", x: "z", t: "m", r: "l", v: "c"}[command] || "M";
rlm@46 3132 }) + "z"
rlm@46 3133 };
rlm@46 3134 if (path.k) {
rlm@46 3135 for (var k in path.k) if (path[has](k)) {
rlm@46 3136 fontcopy.glyphs[glyph].k[k] = path.k[k];
rlm@46 3137 }
rlm@46 3138 }
rlm@46 3139 }
rlm@46 3140 }
rlm@46 3141 return font;
rlm@46 3142 };
rlm@46 3143 Paper[proto].getFont = function (family, weight, style, stretch) {
rlm@46 3144 stretch = stretch || "normal";
rlm@46 3145 style = style || "normal";
rlm@46 3146 weight = +weight || {normal: 400, bold: 700, lighter: 300, bolder: 800}[weight] || 400;
rlm@46 3147 var font = R.fonts[family];
rlm@46 3148 if (!font) {
rlm@46 3149 var name = new RegExp("(^|\\s)" + family[rp](/[^\w\d\s+!~.:_-]/g, E) + "(\\s|$)", "i");
rlm@46 3150 for (var fontName in R.fonts) if (R.fonts[has](fontName)) {
rlm@46 3151 if (name.test(fontName)) {
rlm@46 3152 font = R.fonts[fontName];
rlm@46 3153 break;
rlm@46 3154 }
rlm@46 3155 }
rlm@46 3156 }
rlm@46 3157 var thefont;
rlm@46 3158 if (font) {
rlm@46 3159 for (var i = 0, ii = font[length]; i < ii; i++) {
rlm@46 3160 thefont = font[i];
rlm@46 3161 if (thefont.face["font-weight"] == weight && (thefont.face["font-style"] == style || !thefont.face["font-style"]) && thefont.face["font-stretch"] == stretch) {
rlm@46 3162 break;
rlm@46 3163 }
rlm@46 3164 }
rlm@46 3165 }
rlm@46 3166 return thefont;
rlm@46 3167 };
rlm@46 3168 Paper[proto].print = function (x, y, string, font, size, origin) {
rlm@46 3169 origin = origin || "middle"; // baseline|middle
rlm@46 3170 var out = this.set(),
rlm@46 3171 letters = (string + E)[split](E),
rlm@46 3172 shift = 0,
rlm@46 3173 path = E,
rlm@46 3174 scale;
rlm@46 3175 R.is(font, "string") && (font = this.getFont(font));
rlm@46 3176 if (font) {
rlm@46 3177 scale = (size || 16) / font.face["units-per-em"];
rlm@46 3178 var bb = font.face.bbox.split(separator),
rlm@46 3179 top = +bb[0],
rlm@46 3180 height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2);
rlm@46 3181 for (var i = 0, ii = letters[length]; i < ii; i++) {
rlm@46 3182 var prev = i && font.glyphs[letters[i - 1]] || {},
rlm@46 3183 curr = font.glyphs[letters[i]];
rlm@46 3184 shift += i ? (prev.w || font.w) + (prev.k && prev.k[letters[i]] || 0) : 0;
rlm@46 3185 curr && curr.d && out[push](this.path(curr.d).attr({fill: "#000", stroke: "none", translation: [shift, 0]}));
rlm@46 3186 }
rlm@46 3187 out.scale(scale, scale, top, height).translate(x - top, y - height);
rlm@46 3188 }
rlm@46 3189 return out;
rlm@46 3190 };
rlm@46 3191
rlm@46 3192 R.format = function (token) {
rlm@46 3193 var args = R.is(arguments[1], "array") ? [0][concat](arguments[1]) : arguments,
rlm@46 3194 rg = /\{(\d+)\}/g;
rlm@46 3195 token && R.is(token, "string") && args[length] - 1 && (token = token[rp](rg, function (str, i) {
rlm@46 3196 return args[++i] == null ? E : args[i];
rlm@46 3197 }));
rlm@46 3198 return token || E;
rlm@46 3199 };
rlm@46 3200 R.ninja = function () {
rlm@46 3201 var r = win.Raphael, u;
rlm@46 3202 if (oldRaphael.was) {
rlm@46 3203 win.Raphael = oldRaphael.is;
rlm@46 3204 } else {
rlm@46 3205 try {
rlm@46 3206 delete win.Raphael;
rlm@46 3207 } catch (e) {
rlm@46 3208 win.Raphael = u;
rlm@46 3209 }
rlm@46 3210 }
rlm@46 3211 return r;
rlm@46 3212 };
rlm@46 3213 R.el = Element[proto];
rlm@46 3214 return R;
rlm@46 3215 })();