| | 1 | = Suggested Addition of MochiKit.SVG = |
| | 2 | |
| | 3 | SVG graphics is built-in for Firefox 1.5+, Safari 2.0+ and possibly other browsers. Below follows a suggestion for a new ''!MochiKit.SVG'' namespace with helper functions for creating and managing SVG DOM nodes. |
| | 4 | |
| | 5 | '''Note:''' The code below depends on some of the [wiki:MochiKitDOMExtensions MochiKit DOM Extensions] that have been suggested. |
| | 6 | |
| | 7 | {{{ |
| | 8 | #!text/x-javascript |
| | 9 | // The SVG namespace |
| | 10 | MochiKit.SVG = {}; |
| | 11 | |
| | 12 | /** |
| | 13 | * Creates an SVG document node. |
| | 14 | * |
| | 15 | * @param {Object} [attrs] the optional node attributes |
| | 16 | * @param {Object} [...] the nodes or text to add as children |
| | 17 | * |
| | 18 | * @return {Node} the SVG DOM document node created |
| | 19 | */ |
| | 20 | MochiKit.SVG.SVG = |
| | 21 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "svg", |
| | 22 | [], |
| | 23 | { version: "1.1", baseProfile: "full" }); |
| | 24 | |
| | 25 | /** |
| | 26 | * Creates an SVG definitions node. |
| | 27 | * |
| | 28 | * @param {Object} [attrs] the optional node attributes |
| | 29 | * @param {Object} [...] the nodes or text to add as children |
| | 30 | * |
| | 31 | * @return {Node} the SVG DOM document node created |
| | 32 | */ |
| | 33 | MochiKit.SVG.DEFS = |
| | 34 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "defs"); |
| | 35 | |
| | 36 | /** |
| | 37 | * Creates an SVG group node. |
| | 38 | * |
| | 39 | * @param {Object} [attrs] the optional node attributes |
| | 40 | * @param {Object} [...] the nodes or text to add as children |
| | 41 | * |
| | 42 | * @return {Node} the SVG DOM document node created |
| | 43 | */ |
| | 44 | MochiKit.SVG.G = |
| | 45 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "g"); |
| | 46 | |
| | 47 | /** |
| | 48 | * Creates an SVG line node. |
| | 49 | * |
| | 50 | * @param {String} x1 the x1 coordinate value |
| | 51 | * @param {String} y1 the y1 coordinate value |
| | 52 | * @param {String} x2 the x2 coordinate value |
| | 53 | * @param {String} y2 the y2 coordinate value |
| | 54 | * @param {Object} [attrs] the optional node attributes |
| | 55 | * |
| | 56 | * @return {Node} the SVG DOM document node created |
| | 57 | */ |
| | 58 | MochiKit.SVG.LINE = |
| | 59 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "line", |
| | 60 | ["x1", "y1", "x2", "y2"]); |
| | 61 | |
| | 62 | /** |
| | 63 | * Creates an SVG rectangle node. |
| | 64 | * |
| | 65 | * @param {String} x the x coordinate value |
| | 66 | * @param {String} y the y coordinate value |
| | 67 | * @param {String} width the width value |
| | 68 | * @param {String} height the height value |
| | 69 | * @param {Object} [attrs] the optional node attributes |
| | 70 | * |
| | 71 | * @return {Node} the SVG DOM document node created |
| | 72 | */ |
| | 73 | MochiKit.SVG.RECT = |
| | 74 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "rect", |
| | 75 | ["x", "y", "width", "height"]); |
| | 76 | |
| | 77 | /** |
| | 78 | * Creates an SVG circle node. |
| | 79 | * |
| | 80 | * @param {String} cx the center x coordinate value |
| | 81 | * @param {String} cy the center y coordinate value |
| | 82 | * @param {String} r the radius value |
| | 83 | * @param {Object} [attrs] the optional node attributes |
| | 84 | * |
| | 85 | * @return {Node} the SVG DOM document node created |
| | 86 | */ |
| | 87 | MochiKit.SVG.CIRCLE = |
| | 88 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "circle", |
| | 89 | ["cx", "cy", "r"]); |
| | 90 | |
| | 91 | /** |
| | 92 | * Creates an SVG path node. |
| | 93 | * |
| | 94 | * @param {String} d the path data value |
| | 95 | * @param {Object} [attrs] the optional node attributes |
| | 96 | * |
| | 97 | * @return {Node} the SVG DOM document node created |
| | 98 | */ |
| | 99 | MochiKit.SVG.PATH = |
| | 100 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "path", |
| | 101 | ["d"]); |
| | 102 | |
| | 103 | /** |
| | 104 | * Creates an SVG text node. |
| | 105 | * |
| | 106 | * @param {String} x the x coordinate value |
| | 107 | * @param {String} y the y coordinate value |
| | 108 | * @param {Object} [attrs] the optional node attributes |
| | 109 | * @param {Object} [...] the text to add as children |
| | 110 | * |
| | 111 | * @return {Node} the SVG DOM document node created |
| | 112 | */ |
| | 113 | MochiKit.SVG.TEXT = |
| | 114 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "text", |
| | 115 | ["x", "y"]); |
| | 116 | |
| | 117 | /** |
| | 118 | * Creates an SVG radial gradient node. |
| | 119 | * |
| | 120 | * @param {String} id the id of the node |
| | 121 | * @param {Object} [attrs] the optional node attributes |
| | 122 | * @param {Object} [...] the stop nodes to add as children |
| | 123 | * |
| | 124 | * @return {Node} the SVG DOM document node created |
| | 125 | */ |
| | 126 | MochiKit.SVG.RADIALGRADIENT = |
| | 127 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "radialGradient", |
| | 128 | ["id"], |
| | 129 | { gradientUnits: "objectBoundingBox", |
| | 130 | cx: "0.5", cy: "0.5", r: "0.5" }); |
| | 131 | |
| | 132 | /** |
| | 133 | * Creates an SVG gradient stop node. |
| | 134 | * |
| | 135 | * @param {String} offset the stop offset |
| | 136 | * @param {String} color the stop color |
| | 137 | * @param {Object} [attrs] the optional node attributes |
| | 138 | * |
| | 139 | * @return {Node} the SVG DOM document node created |
| | 140 | */ |
| | 141 | MochiKit.SVG.STOP = |
| | 142 | MochiKit.DOM.createDOMFuncExt(MochiKit.DOM.NS.SVG, "stop", |
| | 143 | ["offset", "stop-color"]); |
| | 144 | |
| | 145 | /** |
| | 146 | * Moves a node to the top of the SVG drawing order (i.e. z-index). |
| | 147 | * Note that this will only have effect on other SVG DOM nodes with |
| | 148 | * the same parent node. Otherwise, the node must be moved down in |
| | 149 | * the SVG DOM tree by changing parent node. |
| | 150 | * |
| | 151 | * @param {Node/String} node the SVG DOM node or unique id |
| | 152 | */ |
| | 153 | MochiKit.SVG.moveToTop = function(node) { |
| | 154 | node = MochiKit.DOM.getElement(node); |
| | 155 | if (node != null) { |
| | 156 | var parent = node.parentNode; |
| | 157 | if (parent && parent.lastChild !== node) { |
| | 158 | parent.appendChild(node); |
| | 159 | } |
| | 160 | } |
| | 161 | } |
| | 162 | |
| | 163 | /** |
| | 164 | * Moves a node to the bottom of the SVG drawing order (i.e. z-index). |
| | 165 | * Note that this will only have effect on other SVG DOM nodes with |
| | 166 | * the same parent node. Otherwise, the node must be moved up in |
| | 167 | * the SVG DOM tree by changing parent node. |
| | 168 | * |
| | 169 | * @param {Node/String} node the SVG DOM node or unique id |
| | 170 | */ |
| | 171 | MochiKit.SVG.moveToBottom = function(node) { |
| | 172 | node = MochiKit.DOM.getElement(node); |
| | 173 | if (node != null) { |
| | 174 | var parent = node.parentNode; |
| | 175 | if (parent && parent.firstChild !== node) { |
| | 176 | parent.insertBefore(node, parent.firstChild); |
| | 177 | } |
| | 178 | } |
| | 179 | } |
| | 180 | |
| | 181 | /** |
| | 182 | * Adds a rotation transform to a SVG DOM node. Any previous |
| | 183 | * rotation transform will be kept intact. |
| | 184 | * |
| | 185 | * @param {Node/String} node the SVG DOM node or unique id |
| | 186 | * @param {String/Number} angle the numeric angle |
| | 187 | * @param {String/Number} [x] the x coordinate value |
| | 188 | * @param {String/Number} [y] the y coordinate value |
| | 189 | */ |
| | 190 | MochiKit.SVG.rotate = function(node, angle, x, y) { |
| | 191 | var str = MochiKit.DOM.getNodeAttribute(node, "transform"); |
| | 192 | x = x || 0; |
| | 193 | y = y || 0; |
| | 194 | if (str == null || str == "") { |
| | 195 | str = ""; |
| | 196 | } else { |
| | 197 | str += " "; |
| | 198 | } |
| | 199 | str += "rotate(" + angle + "," + x + "," + y + ")"; |
| | 200 | MochiKit.DOM.setNodeAttribute(node, "transform", str); |
| | 201 | } |
| | 202 | }}} |