Path: blob/trunk/third_party/closure/goog/graphics/svgelement.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.131415/**16* @fileoverview Thin wrappers around the DOM element returned from17* the different draw methods of the graphics. This is the SVG implementation.18* @author [email protected] (Erik Arvidsson)19*/2021goog.provide('goog.graphics.SvgEllipseElement');22goog.provide('goog.graphics.SvgGroupElement');23goog.provide('goog.graphics.SvgImageElement');24goog.provide('goog.graphics.SvgPathElement');25goog.provide('goog.graphics.SvgRectElement');26goog.provide('goog.graphics.SvgTextElement');272829goog.require('goog.dom');30goog.require('goog.graphics.EllipseElement');31goog.require('goog.graphics.GroupElement');32goog.require('goog.graphics.ImageElement');33goog.require('goog.graphics.PathElement');34goog.require('goog.graphics.RectElement');35goog.require('goog.graphics.TextElement');36373839/**40* Thin wrapper for SVG group elements.41* You should not construct objects from this constructor. The graphics42* will return the object for you.43* @param {Element} element The DOM element to wrap.44* @param {goog.graphics.SvgGraphics} graphics The graphics creating45* this element.46* @constructor47* @extends {goog.graphics.GroupElement}48* @deprecated goog.graphics is deprecated. It existed to abstract over browser49* differences before the canvas tag was widely supported. See50* http://en.wikipedia.org/wiki/Canvas_element for details.51* @final52*/53goog.graphics.SvgGroupElement = function(element, graphics) {54goog.graphics.GroupElement.call(this, element, graphics);55};56goog.inherits(goog.graphics.SvgGroupElement, goog.graphics.GroupElement);575859/**60* Remove all drawing elements from the group.61* @override62*/63goog.graphics.SvgGroupElement.prototype.clear = function() {64goog.dom.removeChildren(this.getElement());65};666768/**69* Set the size of the group element.70* @param {number|string} width The width of the group element.71* @param {number|string} height The height of the group element.72* @override73*/74goog.graphics.SvgGroupElement.prototype.setSize = function(width, height) {75this.getGraphics().setElementAttributes(76this.getElement(), {'width': width, 'height': height});77};78798081/**82* Thin wrapper for SVG ellipse elements.83* This is an implementation of the goog.graphics.EllipseElement interface.84* You should not construct objects from this constructor. The graphics85* will return the object for you.86* @param {Element} element The DOM element to wrap.87* @param {goog.graphics.SvgGraphics} graphics The graphics creating88* this element.89* @param {goog.graphics.Stroke?} stroke The stroke to use for this element.90* @param {goog.graphics.Fill?} fill The fill to use for this element.91* @constructor92* @extends {goog.graphics.EllipseElement}93* @final94*/95goog.graphics.SvgEllipseElement = function(element, graphics, stroke, fill) {96goog.graphics.EllipseElement.call(this, element, graphics, stroke, fill);97};98goog.inherits(goog.graphics.SvgEllipseElement, goog.graphics.EllipseElement);99100101/**102* Update the center point of the ellipse.103* @param {number} cx Center X coordinate.104* @param {number} cy Center Y coordinate.105* @override106*/107goog.graphics.SvgEllipseElement.prototype.setCenter = function(cx, cy) {108this.getGraphics().setElementAttributes(109this.getElement(), {'cx': cx, 'cy': cy});110};111112113/**114* Update the radius of the ellipse.115* @param {number} rx Radius length for the x-axis.116* @param {number} ry Radius length for the y-axis.117* @override118*/119goog.graphics.SvgEllipseElement.prototype.setRadius = function(rx, ry) {120this.getGraphics().setElementAttributes(121this.getElement(), {'rx': rx, 'ry': ry});122};123124125126/**127* Thin wrapper for SVG rectangle elements.128* This is an implementation of the goog.graphics.RectElement interface.129* You should not construct objects from this constructor. The graphics130* will return the object for you.131* @param {Element} element The DOM element to wrap.132* @param {goog.graphics.SvgGraphics} graphics The graphics creating133* this element.134* @param {goog.graphics.Stroke?} stroke The stroke to use for this element.135* @param {goog.graphics.Fill?} fill The fill to use for this element.136* @constructor137* @extends {goog.graphics.RectElement}138* @final139*/140goog.graphics.SvgRectElement = function(element, graphics, stroke, fill) {141goog.graphics.RectElement.call(this, element, graphics, stroke, fill);142};143goog.inherits(goog.graphics.SvgRectElement, goog.graphics.RectElement);144145146/**147* Update the position of the rectangle.148* @param {number} x X coordinate (left).149* @param {number} y Y coordinate (top).150* @override151*/152goog.graphics.SvgRectElement.prototype.setPosition = function(x, y) {153this.getGraphics().setElementAttributes(this.getElement(), {'x': x, 'y': y});154};155156157/**158* Update the size of the rectangle.159* @param {number} width Width of rectangle.160* @param {number} height Height of rectangle.161* @override162*/163goog.graphics.SvgRectElement.prototype.setSize = function(width, height) {164this.getGraphics().setElementAttributes(165this.getElement(), {'width': width, 'height': height});166};167168169170/**171* Thin wrapper for SVG path elements.172* This is an implementation of the goog.graphics.PathElement interface.173* You should not construct objects from this constructor. The graphics174* will return the object for you.175* @param {Element} element The DOM element to wrap.176* @param {goog.graphics.SvgGraphics} graphics The graphics creating177* 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* @constructor181* @extends {goog.graphics.PathElement}182* @final183*/184goog.graphics.SvgPathElement = function(element, graphics, stroke, fill) {185goog.graphics.PathElement.call(this, element, graphics, stroke, fill);186};187goog.inherits(goog.graphics.SvgPathElement, goog.graphics.PathElement);188189190/**191* Update the underlying path.192* @param {!goog.graphics.Path} path The path object to draw.193* @override194*/195goog.graphics.SvgPathElement.prototype.setPath = function(path) {196/** @suppress {missingRequire} goog.graphics.SvgGraphics */197this.getGraphics().setElementAttributes(198this.getElement(), {'d': goog.graphics.SvgGraphics.getSvgPath(path)});199};200201202203/**204* Thin wrapper for SVG text elements.205* This is an implementation of the goog.graphics.TextElement interface.206* You should not construct objects from this constructor. The graphics207* will return the object for you.208* @param {Element} element The DOM element to wrap.209* @param {goog.graphics.SvgGraphics} graphics The graphics creating210* this element.211* @param {goog.graphics.Stroke?} stroke The stroke to use for this element.212* @param {goog.graphics.Fill?} fill The fill to use for this element.213* @constructor214* @extends {goog.graphics.TextElement}215* @final216*/217goog.graphics.SvgTextElement = function(element, graphics, stroke, fill) {218goog.graphics.TextElement.call(this, element, graphics, stroke, fill);219};220goog.inherits(goog.graphics.SvgTextElement, goog.graphics.TextElement);221222223/**224* Update the displayed text of the element.225* @param {string} text The text to draw.226* @override227*/228goog.graphics.SvgTextElement.prototype.setText = function(text) {229this.getElement().firstChild.data = text;230};231232233234/**235* Thin wrapper for SVG image elements.236* This is an implementation of the goog.graphics.ImageElement interface.237* You should not construct objects from this constructor. The graphics238* will return the object for you.239* @param {Element} element The DOM element to wrap.240* @param {goog.graphics.SvgGraphics} graphics The graphics creating241* this element.242* @constructor243* @extends {goog.graphics.ImageElement}244* @final245*/246goog.graphics.SvgImageElement = function(element, graphics) {247goog.graphics.ImageElement.call(this, element, graphics);248};249goog.inherits(goog.graphics.SvgImageElement, goog.graphics.ImageElement);250251252/**253* Update the position of the image.254* @param {number} x X coordinate (left).255* @param {number} y Y coordinate (top).256* @override257*/258goog.graphics.SvgImageElement.prototype.setPosition = function(x, y) {259this.getGraphics().setElementAttributes(this.getElement(), {'x': x, 'y': y});260};261262263/**264* Update the size of the image.265* @param {number} width Width of image.266* @param {number} height Height of image.267* @override268*/269goog.graphics.SvgImageElement.prototype.setSize = function(width, height) {270this.getGraphics().setElementAttributes(271this.getElement(), {'width': width, 'height': height});272};273274275/**276* Update the source of the image.277* @param {string} src Source of the image.278* @override279*/280goog.graphics.SvgImageElement.prototype.setSource = function(src) {281this.getGraphics().setElementAttributes(282this.getElement(), {'xlink:href': src});283};284285286