X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=raphael.core.js;h=8344f1476eaed14f4463f2ad77ac1275dd221d93;hb=9dbae9c67d7949b0689b67e2e1f28a11114b1ede;hp=55641dfd5de82be5c7842622e355499b093f41d2;hpb=a3d3b76d37f7452fbc43e716cc57ca4d795b957c;p=raphael diff --git a/raphael.core.js b/raphael.core.js index 55641df..8344f14 100644 --- a/raphael.core.js +++ b/raphael.core.js @@ -1,10 +1,11 @@ // ┌─────────────────────────────────────────────────────────────────────┐ \\ -// │ "Raphaël 2.0" - JavaScript Vector Library │ \\ +// │ "Raphaël 2.0.1" - JavaScript Vector Library │ \\ // ├─────────────────────────────────────────────────────────────────────┤ \\ // │ Copyright (c) 2008-2011 Dmitry Baranovskiy (http://raphaeljs.com) │ \\ // │ Copyright (c) 2008-2011 Sencha Labs (http://sencha.com) │ \\ // │ Licensed under the MIT (http://raphaeljs.com/license.html) license. │ \\ // └─────────────────────────────────────────────────────────────────────┘ \\ + (function () { /*\ * Raphael @@ -26,7 +27,7 @@ - height (number) - callback (function) #optional callback function which is going to be executed in the context of newly created paper * or - - all (array) (first 3 or 4 elements in the array are equal to [containerID, width, height] or [x, y, width, height]. The rest are element descriptions in format {type: type, }) + - all (array) (first 3 or 4 elements in the array are equal to [containerID, width, height] or [x, y, width, height]. The rest are element descriptions in format {type: type, }). See @Paper.add. - callback (function) #optional callback function which is going to be executed in the context of newly created paper * or - onReadyCallback (function) function that is going to be called on DOM ready event. You can also subscribe to this event via Eve’s “DOMLoad” event. In this case method returns `undefined`. @@ -60,17 +61,7 @@ if (R.is(first, "function")) { return loaded ? first() : eve.on("DOMload", first); } else if (R.is(first, array)) { - var a = first, - cnv = R._engine.create[apply](R, a.splice(0, 3 + R.is(a[0], nu))), - res = cnv.set(), - i = 0, - ii = a.length, - j; - for (; i < ii; i++) { - j = a[i] || {}; - elements[has](j.type) && res.push(cnv[j.type]().attr(j)); - } - return res; + return R._engine.create[apply](R, first.splice(0, 3 + R.is(first[0], nu))).add(first); } else { var args = Array.prototype.slice.call(arguments, 0); if (R.is(args[args.length - 1], "function")) { @@ -83,7 +74,7 @@ } } } - R.version = "2.0.0"; + R.version = "2.0.1"; R.eve = eve; var loaded, separator = /[, ]+/, @@ -100,6 +91,12 @@ is: g.win.Raphael }, Paper = function () { + /*\ + * Paper.ca + [ property (object) ] + ** + * Shortcut for @Paper.customAttributes + \*/ /*\ * Paper.customAttributes [ property (object) ] @@ -109,7 +106,7 @@ > Usage | paper.customAttributes.hue = function (num) { | num = num % 1; - | return {fill: "hsb(" + num + ", .75, 1)"}; + | return {fill: "hsb(" + num + ", 0.75, 1)"}; | }; | // Custom attribute “hue” will change fill | // to be given hue with fixed saturation and brightness. @@ -123,10 +120,10 @@ | paper.customAttributes.hsb = function (h, s, b) { | return {fill: "hsb(" + [h, s, b].join(",") + ")"}; | }; - | c.attr({hsb: ".5 .8 1"}); - | c.animate({hsb: "1 0 .5"}, 1e3); + | c.attr({hsb: "0.5 .8 1"}); + | c.animate({hsb: [1, 0, 0.5]}, 1e3); \*/ - this.customAttributes = {}; + this.ca = this.customAttributes = {}; }, paperproto, appendChild = "appendChild", @@ -137,7 +134,7 @@ S = " ", Str = String, split = "split", - events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend touchcancel".split(S), + events = "click dblclick mousedown mousemove mouseout mouseover mouseup touchstart touchmove touchend touchcancel"[split](S), touchMap = { mousedown: "touchstart", mousemove: "touchmove", @@ -185,6 +182,7 @@ gradient: 0, height: 0, href: "http://raphaeljs.com/", + "letter-spacing": 0, opacity: 1, path: "M0,0", r: 0, @@ -287,7 +285,7 @@ if (!matrix) { return path; } - var x, y, i, j, pathi; + var x, y, i, j, ii, jj, pathi; path = path2curve(path); for (i = 0, ii = path.length; i < ii; i++) { pathi = path[i]; @@ -308,7 +306,8 @@ ** * Can be “SVG”, “VML” or empty, depending on browser support. \*/ - R.type = (g.win.SVGAngle || g.doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML"); + R.type = (g.win.SVGAngle || + g.doc.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1") ? "SVG" : "VML"); if (R.type == "VML") { var d = g.doc.createElement("div"), b; @@ -340,9 +339,10 @@ ** * You can add your own method to the canvas. For example if you want to draw a pie chart, * you can create your own pie chart function and ship it as a Raphaël plugin. To do this - * you need to extend the `Raphael.fn` object. Please note that you can create your own namespaces - * inside the `fn` object — methods will be run in the context of canvas anyway. You should alter - * the `fn` object before a Raphaël instance is created, otherwise it will take no effect. + * you need to extend the `Raphael.fn` object. You should modify the `fn` object before a + * Raphaël instance is created, otherwise it will take no effect. Please note that the + * ability for namespaced plugins was removed in Raphael 2.0. It is up to the plugin to + * ensure any namespacing ensures proper context. > Usage | Raphael.fn.arrow = function (x1, y1, x2, y2, size) { | return this.path( ... ); @@ -381,7 +381,7 @@ return o instanceof Array; } return (type == "null" && o === null) || - (type == typeof o) || + (type == typeof o && o !== null) || (type == "object" && o === Object(o)) || (type == "array" && Array.isArray && Array.isArray(o)) || objectToString.call(o).slice(8, -1).toLowerCase() == type; @@ -495,8 +495,8 @@ eve("setWindow", R, g.win, newwin); g.win = newwin; g.doc = g.win.document; - if (initWin) { - initWin(g.win); + if (R._engine.initWin) { + R._engine.initWin(g.win); } }; var toHex = function (color) { @@ -626,7 +626,7 @@ clr.v = rgb.b; } else { clr = {hex: "none"}; - crl.r = clr.g = clr.b = clr.h = clr.s = clr.v = clr.l = -1; + clr.r = clr.g = clr.b = clr.h = clr.s = clr.v = clr.l = -1; } } clr.toString = rgbtoString; @@ -809,7 +809,7 @@ var preload = R._preload = function (src, f) { var img = g.doc.createElement("img"); - img.style.cssText = "position:absolute;left:-9999em;top-9999em"; + img.style.cssText = "position:absolute;left:-9999em;top:-9999em"; img.onload = function () { f.call(this); this.onload = null; @@ -881,7 +881,7 @@ red = toInt((t = rgb[3].charAt(1)) + t, 16); } if (rgb[4]) { - values = rgb[4].split(commaSpaces); + values = rgb[4][split](commaSpaces); red = toFloat(values[0]); values[0].slice(-1) == "%" && (red *= 2.55); green = toFloat(values[1]); @@ -892,7 +892,7 @@ values[3] && values[3].slice(-1) == "%" && (opacity /= 100); } if (rgb[5]) { - values = rgb[5].split(commaSpaces); + values = rgb[5][split](commaSpaces); red = toFloat(values[0]); values[0].slice(-1) == "%" && (red *= 2.55); green = toFloat(values[1]); @@ -905,7 +905,7 @@ return R.hsb2rgb(red, green, blue, opacity); } if (rgb[6]) { - values = rgb[6].split(commaSpaces); + values = rgb[6][split](commaSpaces); red = toFloat(values[0]); values[0].slice(-1) == "%" && (red *= 2.55); green = toFloat(values[1]); @@ -1168,6 +1168,11 @@ alpha: alpha }; }; + R._removedFactory = function (methodname) { + return function () { + throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object"); + }; + }; var pathDimensions = cacher(function (path) { if (!path) { return {x: 0, y: 0, width: 0, height: 0}; @@ -1484,7 +1489,7 @@ if (recursive) { return [m2, m3, m4][concat](res); } else { - res = [m2, m3, m4][concat](res).join().split(","); + res = [m2, m3, m4][concat](res).join()[split](","); var newres = []; for (var i = 0, ii = res.length; i < ii; i++) { newres[i] = i % 2 ? rotate(res[i - 1], res[i], rad).y : rotate(res[i], res[i + 1], rad).x; @@ -1713,11 +1718,6 @@ el2.prev = el; el.next = el2; }, - removed = function (methodname) { - return function () { - throw new Error("Rapha\xebl: you are calling to method \u201c" + methodname + "\u201d of removed object"); - }; - }, extractTransform = R._extractTransform = function (el, tstr) { if (tstr == null) { return el._.transform; @@ -1736,24 +1736,25 @@ for (var i = 0, ii = tdata.length; i < ii; i++) { var t = tdata[i], tlen = t.length, - absolute = t[0] != (t[0] = Str(t[0]).toLowerCase()), + command = Str(t[0]).toLowerCase(), + absolute = t[0] != command, inver = absolute ? m.invert() : 0, x1, y1, x2, y2, bb; - if (t[0] == "t" && tlen == 3) { + if (command == "t" && tlen == 3) { if (absolute) { - x1 = inver.x(0, 0), - y1 = inver.y(0, 0), + x1 = inver.x(0, 0); + y1 = inver.y(0, 0); x2 = inver.x(t[1], t[2]); y2 = inver.y(t[1], t[2]); m.translate(x2 - x1, y2 - y1); } else { m.translate(t[1], t[2]); } - } else if (t[0] == "r") { + } else if (command == "r") { if (tlen == 2) { bb = bb || el.getBBox(1); m.rotate(t[1], bb.x + bb.width / 2, bb.y + bb.height / 2); @@ -1768,7 +1769,7 @@ } deg += t[1]; } - } else if (t[0] == "s") { + } else if (command == "s") { if (tlen == 2 || tlen == 3) { bb = bb || el.getBBox(1); m.scale(t[1], t[tlen - 1], bb.x + bb.width / 2, bb.y + bb.height / 2); @@ -1785,7 +1786,7 @@ sx *= t[1]; sy *= t[2]; } - } else if (t[0] == "m" && tlen == 7) { + } else if (command == "m" && tlen == 7) { m.add(t[1], t[2], t[3], t[4], t[5], t[6]); } _.dirtyT = 1; @@ -2166,9 +2167,14 @@ = (string) transform string \*/ matrixproto.toTransformString = function (shorter) { - var s = shorter || this.split(); + var s = shorter || this[split](); if (s.isSimple) { - return "t" + [s.dx, s.dy] + "s" + [s.scalex, s.scaley, 0, 0] + "r" + [s.rotate, 0, 0]; + s.scalex = +s.scalex.toFixed(4); + s.scaley = +s.scaley.toFixed(4); + s.rotate = +s.rotate.toFixed(4); + return (s.dx && s.dy ? "t" + [s.dx, s.dy] : E) + + (s.scalex != 1 || s.scaley != 1 ? "s" + [s.scalex, s.scaley, 0, 0] : E) + + (s.rotate ? "r" + [s.rotate, 0, 0] : E); } else { return "m" + [this.get(0), this.get(1), this.get(2), this.get(3), this.get(4), this.get(5)]; } @@ -2640,6 +2646,7 @@ elproto.unhover = function (f_in, f_out) { return this.unmouseover(f_in).unmouseout(f_out); }; + var draggable = []; /*\ * Element.drag [ method ] @@ -2686,6 +2693,7 @@ eve("drag.start." + this.id, start_scope || move_scope || this, e.clientX + scrollX, e.clientY + scrollY, e); } this._drag = {}; + draggable.push({el: this, start: start}); this.mousedown(start); return this; }; @@ -2707,13 +2715,13 @@ * Removes all drag event handlers from given element. \*/ elproto.undrag = function () { - var i = drag.length; - while (i--) if (drag[i].el == this) { - R.unmousedown(drag[i].start); - drag.splice(i++, 1); + var i = draggable.length; + while (i--) if (draggable[i].el == this) { + this.unmousedown(draggable[i].start); + draggable.splice(i, 1); eve.unbind("drag.*." + this.id); } - !drag.length && R.unmousemove(dragMove).unmouseup(dragUp); + !draggable.length && R.unmousemove(dragMove).unmouseup(dragUp); }; /*\ * Paper.circle @@ -3476,7 +3484,9 @@ t = e.t, that = e.el, set = {}, - now; + now, + init = {}, + key; if (e.initstatus) { time = (e.initstatus * e.anim.top - e.prev) / (e.percent - e.prev) * ms; e.status = e.initstatus; @@ -3540,7 +3550,7 @@ } break; default: - var from2 = [].concat(from[attr]); + var from2 = [][concat](from[attr]); now = []; i = that.paper.customAttributes[attr].length; while (i--) { @@ -3567,6 +3577,10 @@ that.attr(to); animationElements.splice(l--, 1); if (e.repeat > 1 && !e.next) { + for (key in to) if (to[has](key)) { + init[key] = e.totalOrigin[key]; + } + e.el.attr(init); runAnimation(e.anim, e.el, e.anim.percents[0], null, e.totalOrigin, e.repeat - 1); } if (e.next && !e.stop) { @@ -3602,7 +3616,7 @@ = (object) original element \*/ elproto.animateWith = function (element, anim, params, ms, easing, callback) { - var a = params ? R.animation(params, ms, easing, callback) : anim; + var a = params ? R.animation(params, ms, easing, callback) : anim, status = element.status(anim); return this.animate(a).status(a, status * anim.ms / a.ms); }; @@ -3763,7 +3777,7 @@ return; } if (!isInAnim) { - for (attr in params) if (params[has](attr)) { + for (var attr in params) if (params[has](attr)) { if (availableAnimAttrs[has](attr) || element.paper.customAttributes[has](attr)) { from[attr] = element.attr(attr); (from[attr] == null) && (from[attr] = availableAttrs[attr]); @@ -3846,8 +3860,8 @@ } break; case "csv": - var values = Str(params[attr]).split(separator), - from2 = Str(from[attr]).split(separator); + var values = Str(params[attr])[split](separator), + from2 = Str(from[attr])[split](separator); if (attr == "clip-rect") { from[attr] = from2; diff[attr] = []; @@ -3859,8 +3873,8 @@ to[attr] = values; break; default: - values = [].concat(params[attr]); - from2 = [].concat(from[attr]); + values = [][concat](params[attr]); + from2 = [][concat](from[attr]); diff[attr] = []; i = element.paper.customAttributes[attr].length; while (i--) { @@ -4469,14 +4483,14 @@ origin = origin || "middle"; // baseline|middle letter_spacing = mmax(mmin(letter_spacing || 0, 1), -1); var out = this.set(), - letters = Str(string).split(E), + letters = Str(string)[split](E), shift = 0, path = E, scale; R.is(font, string) && (font = this.getFont(font)); if (font) { scale = (size || 16) / font.face["units-per-em"]; - var bb = font.face.bbox.split(separator), + var bb = font.face.bbox[split](separator), top = +bb[0], height = +bb[1] + (origin == "baseline" ? bb[3] - bb[1] + (+font.face.descent) : (bb[3] - bb[1]) / 2); for (var i = 0, ii = letters.length; i < ii; i++) { @@ -4494,6 +4508,48 @@ return out; }; + /*\ + * Paper.add + [ method ] + ** + * Imports elements in JSON array in format `{type: type, }` + ** + > Parameters + ** + - json (array) + = (object) resulting set of imported elements + > Usage + | paper.import([ + | { + | type: "circle", + | cx: 10, + | cy: 10, + | r: 5 + | }, + | { + | type: "rect", + | x: 10, + | y: 10, + | width: 10, + | height: 10, + | fill: "#fc0" + | } + | ]); + \*/ + paperproto.add = function (json) { + if (R.is(json, "array")) { + var res = this.set(), + i = 0, + ii = json.length, + j; + for (; i < ii; i++) { + j = json[i] || {}; + elements[has](j.type) && res.push(this[j.type]().attr(j)); + } + } + return res; + }; + /*\ * Raphael.format [ method ] @@ -4511,7 +4567,7 @@ | width = 40, | height = 50; | // this will draw a rectangular shape equivalent to "M10,20h40v50h-40z" - | paper.path(Raphael.format("M{1},{2}h{3}v{4}h{5}z", x, y, width, height, -width)); + | paper.path(Raphael.format("M{0},{1}h{2}v{3}h{4}z", x, y, width, height, -width)); \*/ R.format = function (token, params) { var args = R.is(params, array) ? [0][concat](params) : arguments; @@ -4625,4 +4681,4 @@ eve.on("DOMload", function () { loaded = true; }); -})(); \ No newline at end of file +})();