annotate awesome_js/raphael.js @ 35:ae22c2d59891 laserkard

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