svgelement.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright 2007 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Thin wrappers around the DOM element returned from
  16. * the different draw methods of the graphics. This is the SVG implementation.
  17. * @author arv@google.com (Erik Arvidsson)
  18. */
  19. goog.provide('goog.graphics.SvgEllipseElement');
  20. goog.provide('goog.graphics.SvgGroupElement');
  21. goog.provide('goog.graphics.SvgImageElement');
  22. goog.provide('goog.graphics.SvgPathElement');
  23. goog.provide('goog.graphics.SvgRectElement');
  24. goog.provide('goog.graphics.SvgTextElement');
  25. goog.require('goog.dom');
  26. goog.require('goog.graphics.EllipseElement');
  27. goog.require('goog.graphics.GroupElement');
  28. goog.require('goog.graphics.ImageElement');
  29. goog.require('goog.graphics.PathElement');
  30. goog.require('goog.graphics.RectElement');
  31. goog.require('goog.graphics.TextElement');
  32. /**
  33. * Thin wrapper for SVG group elements.
  34. * You should not construct objects from this constructor. The graphics
  35. * will return the object for you.
  36. * @param {Element} element The DOM element to wrap.
  37. * @param {goog.graphics.SvgGraphics} graphics The graphics creating
  38. * this element.
  39. * @constructor
  40. * @extends {goog.graphics.GroupElement}
  41. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  42. * differences before the canvas tag was widely supported. See
  43. * http://en.wikipedia.org/wiki/Canvas_element for details.
  44. * @final
  45. */
  46. goog.graphics.SvgGroupElement = function(element, graphics) {
  47. goog.graphics.GroupElement.call(this, element, graphics);
  48. };
  49. goog.inherits(goog.graphics.SvgGroupElement, goog.graphics.GroupElement);
  50. /**
  51. * Remove all drawing elements from the group.
  52. * @override
  53. */
  54. goog.graphics.SvgGroupElement.prototype.clear = function() {
  55. goog.dom.removeChildren(this.getElement());
  56. };
  57. /**
  58. * Set the size of the group element.
  59. * @param {number|string} width The width of the group element.
  60. * @param {number|string} height The height of the group element.
  61. * @override
  62. */
  63. goog.graphics.SvgGroupElement.prototype.setSize = function(width, height) {
  64. this.getGraphics().setElementAttributes(
  65. this.getElement(), {'width': width, 'height': height});
  66. };
  67. /**
  68. * Thin wrapper for SVG ellipse elements.
  69. * This is an implementation of the goog.graphics.EllipseElement interface.
  70. * You should not construct objects from this constructor. The graphics
  71. * will return the object for you.
  72. * @param {Element} element The DOM element to wrap.
  73. * @param {goog.graphics.SvgGraphics} graphics The graphics creating
  74. * this element.
  75. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  76. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  77. * @constructor
  78. * @extends {goog.graphics.EllipseElement}
  79. * @final
  80. */
  81. goog.graphics.SvgEllipseElement = function(element, graphics, stroke, fill) {
  82. goog.graphics.EllipseElement.call(this, element, graphics, stroke, fill);
  83. };
  84. goog.inherits(goog.graphics.SvgEllipseElement, goog.graphics.EllipseElement);
  85. /**
  86. * Update the center point of the ellipse.
  87. * @param {number} cx Center X coordinate.
  88. * @param {number} cy Center Y coordinate.
  89. * @override
  90. */
  91. goog.graphics.SvgEllipseElement.prototype.setCenter = function(cx, cy) {
  92. this.getGraphics().setElementAttributes(
  93. this.getElement(), {'cx': cx, 'cy': cy});
  94. };
  95. /**
  96. * Update the radius of the ellipse.
  97. * @param {number} rx Radius length for the x-axis.
  98. * @param {number} ry Radius length for the y-axis.
  99. * @override
  100. */
  101. goog.graphics.SvgEllipseElement.prototype.setRadius = function(rx, ry) {
  102. this.getGraphics().setElementAttributes(
  103. this.getElement(), {'rx': rx, 'ry': ry});
  104. };
  105. /**
  106. * Thin wrapper for SVG rectangle elements.
  107. * This is an implementation of the goog.graphics.RectElement interface.
  108. * You should not construct objects from this constructor. The graphics
  109. * will return the object for you.
  110. * @param {Element} element The DOM element to wrap.
  111. * @param {goog.graphics.SvgGraphics} graphics The graphics creating
  112. * this element.
  113. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  114. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  115. * @constructor
  116. * @extends {goog.graphics.RectElement}
  117. * @final
  118. */
  119. goog.graphics.SvgRectElement = function(element, graphics, stroke, fill) {
  120. goog.graphics.RectElement.call(this, element, graphics, stroke, fill);
  121. };
  122. goog.inherits(goog.graphics.SvgRectElement, goog.graphics.RectElement);
  123. /**
  124. * Update the position of the rectangle.
  125. * @param {number} x X coordinate (left).
  126. * @param {number} y Y coordinate (top).
  127. * @override
  128. */
  129. goog.graphics.SvgRectElement.prototype.setPosition = function(x, y) {
  130. this.getGraphics().setElementAttributes(this.getElement(), {'x': x, 'y': y});
  131. };
  132. /**
  133. * Update the size of the rectangle.
  134. * @param {number} width Width of rectangle.
  135. * @param {number} height Height of rectangle.
  136. * @override
  137. */
  138. goog.graphics.SvgRectElement.prototype.setSize = function(width, height) {
  139. this.getGraphics().setElementAttributes(
  140. this.getElement(), {'width': width, 'height': height});
  141. };
  142. /**
  143. * Thin wrapper for SVG path elements.
  144. * This is an implementation of the goog.graphics.PathElement interface.
  145. * You should not construct objects from this constructor. The graphics
  146. * will return the object for you.
  147. * @param {Element} element The DOM element to wrap.
  148. * @param {goog.graphics.SvgGraphics} graphics The graphics creating
  149. * this element.
  150. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  151. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  152. * @constructor
  153. * @extends {goog.graphics.PathElement}
  154. * @final
  155. */
  156. goog.graphics.SvgPathElement = function(element, graphics, stroke, fill) {
  157. goog.graphics.PathElement.call(this, element, graphics, stroke, fill);
  158. };
  159. goog.inherits(goog.graphics.SvgPathElement, goog.graphics.PathElement);
  160. /**
  161. * Update the underlying path.
  162. * @param {!goog.graphics.Path} path The path object to draw.
  163. * @override
  164. */
  165. goog.graphics.SvgPathElement.prototype.setPath = function(path) {
  166. /** @suppress {missingRequire} goog.graphics.SvgGraphics */
  167. this.getGraphics().setElementAttributes(
  168. this.getElement(), {'d': goog.graphics.SvgGraphics.getSvgPath(path)});
  169. };
  170. /**
  171. * Thin wrapper for SVG text elements.
  172. * This is an implementation of the goog.graphics.TextElement interface.
  173. * You should not construct objects from this constructor. The graphics
  174. * will return the object for you.
  175. * @param {Element} element The DOM element to wrap.
  176. * @param {goog.graphics.SvgGraphics} graphics The graphics creating
  177. * this element.
  178. * @param {goog.graphics.Stroke?} stroke The stroke to use for this element.
  179. * @param {goog.graphics.Fill?} fill The fill to use for this element.
  180. * @constructor
  181. * @extends {goog.graphics.TextElement}
  182. * @final
  183. */
  184. goog.graphics.SvgTextElement = function(element, graphics, stroke, fill) {
  185. goog.graphics.TextElement.call(this, element, graphics, stroke, fill);
  186. };
  187. goog.inherits(goog.graphics.SvgTextElement, goog.graphics.TextElement);
  188. /**
  189. * Update the displayed text of the element.
  190. * @param {string} text The text to draw.
  191. * @override
  192. */
  193. goog.graphics.SvgTextElement.prototype.setText = function(text) {
  194. // This is actually SVGTextElement but we don't have it in externs.
  195. /** @type {!Text} */ (this.getElement().firstChild).data = text;
  196. };
  197. /**
  198. * Thin wrapper for SVG image elements.
  199. * This is an implementation of the goog.graphics.ImageElement interface.
  200. * You should not construct objects from this constructor. The graphics
  201. * will return the object for you.
  202. * @param {Element} element The DOM element to wrap.
  203. * @param {goog.graphics.SvgGraphics} graphics The graphics creating
  204. * this element.
  205. * @constructor
  206. * @extends {goog.graphics.ImageElement}
  207. * @final
  208. */
  209. goog.graphics.SvgImageElement = function(element, graphics) {
  210. goog.graphics.ImageElement.call(this, element, graphics);
  211. };
  212. goog.inherits(goog.graphics.SvgImageElement, goog.graphics.ImageElement);
  213. /**
  214. * Update the position of the image.
  215. * @param {number} x X coordinate (left).
  216. * @param {number} y Y coordinate (top).
  217. * @override
  218. */
  219. goog.graphics.SvgImageElement.prototype.setPosition = function(x, y) {
  220. this.getGraphics().setElementAttributes(this.getElement(), {'x': x, 'y': y});
  221. };
  222. /**
  223. * Update the size of the image.
  224. * @param {number} width Width of image.
  225. * @param {number} height Height of image.
  226. * @override
  227. */
  228. goog.graphics.SvgImageElement.prototype.setSize = function(width, height) {
  229. this.getGraphics().setElementAttributes(
  230. this.getElement(), {'width': width, 'height': height});
  231. };
  232. /**
  233. * Update the source of the image.
  234. * @param {string} src Source of the image.
  235. * @override
  236. */
  237. goog.graphics.SvgImageElement.prototype.setSource = function(src) {
  238. this.getGraphics().setElementAttributes(
  239. this.getElement(), {'xlink:href': src});
  240. };