graphics.SvgGraphics Extends goog.graphics.AbstractGraphics
A Graphics implementation for drawing using SVG.

Inheritance

Object
     goog.Disposable
          goog.events.EventTarget
               goog.ui.Component
                    goog.graphics.AbstractGraphics
                         goog.graphics.SvgGraphics

Constructor

goog.graphics.SvgGraphics(widthheightopt_coordWidthopt_coordHeightopt_domHelper)

Parameters

width :
The width in pixels. Strings expressing percentages of parent with (e.g. '80%') are also accepted.
height :
The height in pixels. Strings expressing percentages of parent with (e.g. '80%') are also accepted.
opt_coordWidth :
The coordinate width - if omitted or null, defaults to same as width.
opt_coordHeight :
The coordinate height - if omitted or null, defaults to same as height.
opt_domHelper :
The DOM helper object for the document we want to render in.

Instance Methods

Public Protected Private
addDef_(defKeydefElement)
Adds a defintion of an element to the global definitions.
Arguments:
defKey :
This is a key that should be unique in a way that if two definitions are equal the should have the same key.
defElement :
DOM element to add as a definition. It must have an id attribute set.
Returns:   The assigned id of the defElement.
code »
append_(elementopt_group)
Appends an element.
Arguments:
element :
The element wrapper.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
code »
clear()
No description.
code »
createDom()
No description.
code »
createGroup(opt_group)
Create an empty group of drawing elements.
Arguments:
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created group.
code »
createSvgElement_(tagNameopt_attributes)
Creates an SVG element. Used internally and by different SVG classes.
Arguments:
tagName :
The type of element to create.
opt_attributes :
Map of name-value pairs for attributes.
Returns:   The created element.
code »
disposeInternal()
No description.
code »
drawEllipse(cxcyrxrystrokefillopt_group)
Draw an ellipse.
Arguments:
cx :
Center X coordinate.
cy :
Center Y coordinate.
rx :
Radius length for the x-axis.
ry :
Radius length for the y-axis.
stroke :
Stroke object describing the stroke.
fill :
Fill object describing the fill.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created element.
code »
drawImage(xywidthheightsrcopt_group)
Draw an image.
Arguments:
x :
X coordinate (left).
y :
Y coordinate (top).
width :
Width of the image.
height :
Height of the image.
src :
The source fo the image.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created image wrapped in a rectangle element.
code »
drawPath(pathstrokefillopt_group)
Draw a path.
Arguments:
path :
The path object to draw.
stroke :
Stroke object describing the stroke.
fill :
Fill object describing the fill.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created element.
code »
drawRect(xywidthheightstrokefillopt_group)
Draw a rectangle.
Arguments:
x :
X coordinate (left).
y :
Y coordinate (top).
width :
Width of rectangle.
height :
Height of rectangle.
stroke :
Stroke object describing the stroke.
fill :
Fill object describing the fill.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created element.
code »
drawTextOnLine(textx1y1x2y2alignfontstrokefillopt_group)
Draw a text string vertically centered on a given line.
Arguments:
text :
The text to draw.
x1 :
X coordinate of start of line.
y1 :
Y coordinate of start of line.
x2 :
X coordinate of end of line.
y2 :
Y coordinate of end of line.
align :
Horizontal alignment: left (default), center, right.
font :
Font describing the font properties.
stroke :
Stroke object describing the stroke.
fill :
Fill object describing the fill.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created element.
code »
enterDocument()
No description.
code »
exitDocument()
No description.
code »
getDef_(defKey)
Returns the id of a definition element.
Arguments:
defKey :
This is a key that should be unique in a way that if two definitions are equal the should have the same key.
Returns:   The id of the found definition element or null if not found.
code »
getPixelSize()
No description.
code »
getTextWidth(textfont)
Measure and return the width (in pixels) of a given text string. Text measurement is needed to make sure a text can fit in the allocated area. The way text length is measured is by writing it into a div that is after the visible area, measure the div width, and immediatly erase the written value.
Arguments:
text :
The text string to measure.
font :
The font object describing the font style.
code »
getViewBox_()
No description.
Returns:   The view box string.
code »
isDomClonable()
No description.
code »
setCoordOrigin(lefttop)
Changes the coordinate system position.
Arguments:
left :
The coordinate system left bound.
top :
The coordinate system top bound.
code »
setCoordSize(coordWidthcoordHeight)
Changes the coordinate size.
Arguments:
coordWidth :
The coordinate width.
coordHeight :
The coordinate height.
code »
setElementAttributes(elementattributes)
Sets properties to an SVG element. Used internally and by different SVG elements.
Arguments:
element :
The svg element.
attributes :
Map of name-value pairs for attributes.
code »
setElementFill(elementfill)
Sets the fill of the given element.
Arguments:
element :
The element wrapper.
fill :
The fill object.
code »
setElementStroke(elementstroke)
Sets the stroke of the given element.
Arguments:
element :
The element wrapper.
stroke :
The stroke object.
code »
setElementTransform(elementxyanglecenterXcenterY)
Set the transformation of an element.
Arguments:
element :
The element wrapper.
x :
The x coordinate of the translation transform.
y :
The y coordinate of the translation transform.
angle :
The angle of the rotation transform.
centerX :
The horizontal center of the rotation transform.
centerY :
The vertical center of the rotation transform.
code »
setSize(pixelWidthpixelHeight)
Change the size of the canvas.
Arguments:
pixelWidth :
The width in pixels.
pixelHeight :
The height in pixels.
code »
setViewBox_()
Sets up the view box.
code »
updateManualViewBox_()
Updates the transform of the root element to fake a viewBox. Should only be called when useManualViewbox_ is set.
code »
clear()
No description.
code »
createGroup()
Create an empty group of drawing elements.
Returns:   The newly created group.
code »
createPath()
Create an empty path.
Returns:   The path.
code »
drawCircle(cxcyrstrokefillopt_group)
Draw a circle
Arguments:
cx :
Center X coordinate.
cy :
Center Y coordinate.
r :
Radius length.
stroke :
Stroke object describing the stroke.
fill :
Fill object describing the fill.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created element.
code »
drawEllipse()
Draw an ellipse
Returns:   The newly created element.
code »
drawPath()
Draw a path.
Returns:   The newly created element.
code »
drawRect()
Draw a rectangle
Returns:   The newly created element.
code »
drawText(textxywidthheightalignvAlignfontstrokefillopt_group)
Draw a text string within a rectangle (drawing is horizontal)
Arguments:
text :
The text to draw.
x :
X coordinate (left).
y :
Y coordinate (top).
width :
Width of rectangle.
height :
Height of rectangle.
align :
Horizontal alignment: left (default), center, right.
vAlign :
Vertical alignment: top (default), center, bottom.
font :
Font describing the font properties.
stroke :
Stroke object describing the stroke.
fill :
Fill object describing the fill.
opt_group :
The group wrapper element to append to. If not specified, appends to the main canvas.
Returns:   The newly created element.
code »
drawTextOnLine()
Draw a text string vertically centered on a given line.
Returns:   The newly created element.
code »
getCanvasElement()
No description.
Returns:   The root level canvas element.
code »
getCoordOrigin()
No description.
Returns:   The coordinate system position.
code »
getCoordSize()
No description.
Returns:   The coordinate size.
code »
getPixelScaleX()
No description.
Returns:   Returns the number of pixels per unit in the x direction.
code »
getPixelScaleY()
No description.
Returns:   Returns the number of pixels per unit in the y direction.
code »
getPixelSize()
No description.
Returns:   Returns the number of pixels spanned by the surface, or null if the size could not be computed due to the size being specified in percentage points and the component not being in the document.
code »
getSize()
No description.
Returns:   The size of canvas.
code »
getTextWidth()
Measure and return the width (in pixels) of a given text string. Text measurement is needed to make sure a text can fit in the allocated area. The way text length is measured is by writing it into a div that is after the visible area, measure the div width, and immediatly erase the written value.
Returns:   The width in pixels of the text strings.
code »
isDomClonable()
No description.
Returns:   Whether the underlying element can be cloned resulting in an accurate reproduction of the graphics contents.
code »
removeElement(element)
Remove a single drawing element from the surface. The default implementation assumes a DOM based drawing surface.
Arguments:
element :
The element to remove.
code »
resume()
No description.
code »
setCoordOrigin()
Changes the coordinate system position.
code »
setCoordSize(coordWidthcoordHeight)
Changes the coordinate size.
Arguments:
coordWidth :
The coordinate width.
coordHeight :
The coordinate height.
code »
setElementFill()
Sets the fill for the given element.
code »
setElementStroke()
Sets the stroke for the given element.
code »
setElementTransform()
Set the transformation of an element.
code »
setSize()
Change the size of the canvas.
code »
suspend()
No description.
code »
addChild(childopt_render)
Adds the specified component as the last child of this component. See {@link goog.ui.Component#addChildAt} for detailed semantics.
Arguments:
child :
The new child component.
opt_render :
If true, the child component will be rendered into the parent.
code »
addChildAt(childindexopt_render)
Adds the specified component as a child of this component at the given 0-based index. Both {@code addChild} and {@code addChildAt} assume the following contract between parent and child components:
  • the child component's element must be a descendant of the parent component's element, and
  • the DOM state of the child component must be consistent with the DOM state of the parent component (see {@code isInDocument}).
In particular, {@code parent.addChild(child)} will throw an error if the child component is already in the document, but the parent isn't. Clients of this API may call {@code addChild} and {@code addChildAt} with {@code opt_render} set to true. If {@code opt_render} is true, calling these methods will automatically render the child component's element into the parent component's element. However, {@code parent.addChild(child, true)} will throw an error if:
  • the parent component has no DOM (i.e. {@code parent.getElement()} is null), or
  • the child component is already in the document, regardless of the parent's DOM state.
Finally, this method also throws an error if the new child already has a different parent, or the given index is out of bounds.
Arguments:
child :
The new child component.
index :
0-based index at which the new child component is to be added; must be between 0 and the current child count (inclusive).
opt_render :
If true, the child component will be rendered into the parent.
code »
canDecorate(element)
Determines if a given element can be decorated by this type of component. This method should be overridden by inheriting objects.
Arguments:
element :
Element to decorate.
Returns:   True if the element can be decorated, false otherwise.
code »
createDom()
No description.
code »
decorate(element)
Decorates the element for the UI component.
Arguments:
element :
Element to decorate.
code »
decorateInternal(element)
Actually decorates the element. Should be overridden by inheriting objects. This method can assume there are checks to ensure the component has not already been rendered have occurred and that enter document will be called afterwards. This method is considered protected.
Arguments:
element :
Element to decorate.
code »
disposeInternal()
Disposes of the component. Calls {@code exitDocument}, which is expected to remove event handlers and clean up the component. Propagates the call to the component's children, if any. Removes the component's DOM from the document unless it was decorated.
code »
enterDocument()
No description.
code »
exitDocument()
No description.
code »
forEachChild(fopt_obj)
Calls the given function on each of this component's children in order. If {@code opt_obj} is provided, it will be used as the 'this' object in the function when called. The function should take two arguments: the child component and its 0-based index. The return value is ignored.
Arguments:
f :
The function to call for every child component; should take 2 arguments (the child and its index).
opt_obj :
Used as the 'this' object in f when called.
code »
getChild(id)
Returns the child with the given ID, or null if no such child exists.
Arguments:
id :
Child component ID.
Returns:   The child with the given ID; null if none.
code »
getChildAt(index)
Returns the child at the given index, or null if the index is out of bounds.
Arguments:
index :
0-based index.
Returns:   The child at the given index; null if none.
code »
getChildCount()
Returns the number of children of this component.
Returns:   The number of children.
code »
getChildIds()
Returns an array containing the IDs of the children of this component, or an empty array if the component has no children.
Returns:   Child component IDs.
code »
getContentElement()
Returns the DOM element into which child components are to be rendered, or null if the component itself hasn't been rendered yet. This default implementation returns the component's root element. Subclasses with complex DOM structures must override this method.
Returns:   Element to contain child elements (null if none).
code »
getDomHelper()
Returns the dom helper that is being used on this component.
Returns:   The dom helper used on this component.
code »
getElement()
Gets the component's element.
Returns:   The element for the component.
code »
getElementByFragment(idFragment)
Helper function for returning an element in the document with a unique id generated using makeId().
Arguments:
idFragment :
The partial id.
Returns:   The element with the unique id, or null if it cannot be found.
code »
getFragmentFromId(id)
Helper function for returning the fragment portion of an id generated using makeId().
Arguments:
id :
Id generated with makeId().
Returns:   Fragment.
code »
getHandler()
Returns the event handler for this component, lazily created the first time this method is called.
Returns:   Event handler for this component.
code »
getId()
Gets the unique ID for the instance of this component. If the instance doesn't already have an ID, generates one on the fly.
Returns:   Unique component ID.
code »
getModel()
*
Returns the model associated with the UI component.
Returns: 
*
  The model.
code »
getParent()
Returns the component's parent, if any.
Returns:   The parent component.
code »
hasChildren()
Returns true if the component has children.
Returns:   True if the component has children.
code »
indexOfChild(child)
Returns the 0-based index of the given child component, or -1 if no such child is found.
Arguments:
child :
The child component.
Returns:   0-based index of the child component; -1 if not found.
code »
isInDocument()
Determines whether the component has been added to the document.
Returns:   TRUE if rendered. Otherwise, FALSE.
code »
isRightToLeft()
Returns true if the component is rendered right-to-left, false otherwise. The first time this function is invoked, the right-to-left rendering property is set if it has not been already.
Returns:   Whether the control is rendered right-to-left.
code »
makeId(idFragment)
Helper function for subclasses that gets a unique id for a given fragment, this can be used by components to generate unique string ids for DOM elements
Arguments:
idFragment :
A partial id.
Returns:   Unique element id.
code »
removeChild(childopt_unrender)
Removes the given child from this component, and returns it. Throws an error if the argument is invalid or if the specified child isn't found in the parent component. The argument can either be a string (interpreted as the ID of the child component to remove) or the child component itself. If {@code opt_unrender} is true, calls {@link goog.ui.component#exitDocument} on the removed child, and subsequently detaches the child's DOM from the document. Otherwise it is the caller's responsibility to clean up the child component's DOM.
Arguments:
child :
The ID of the child to remove, or the child component itself.
opt_unrender :
If true, calls {@code exitDocument} on the removed child component, and detaches its DOM from the document.
Returns:   The removed component, if any.
code »
removeChildAt(indexopt_unrender)
Removes the child at the given index from this component, and returns it. Throws an error if the argument is out of bounds, or if the specified child isn't found in the parent. See {@link goog.ui.Component#removeChild} for detailed semantics.
Arguments:
index :
0-based index of the child to remove.
opt_unrender :
If true, calls {@code exitDocument} on the removed child component, and detaches its DOM from the document.
Returns:   The removed component, if any.
code »
removeChildren(opt_unrender)
Removes every child component attached to this one.
Arguments:
opt_unrender :
If true, calls {@link #exitDocument} on the removed child components, and detaches their DOM from the document.
code »
render(opt_parentElement)
Renders the component. If a parent element is supplied, it should already be in the document and then the component's element will be appended to it. If there is no optional parent element and the element doesn't have a parentNode then it will be appended to the document body. Throws an Error if the component is already rendered.
Arguments:
opt_parentElement :
Optional parent element to render the component into.
code »
renderBefore(siblingElement)
Renders the component before another element. The other element should be in the document already. Throws an Error if the component is already rendered.
Arguments:
siblingElement :
Element to render the component before.
code »
render_(opt_parentElementopt_beforeElement)
Renders the component. If a parent element is supplied, it should already be in the document and then the component's element will be appended to it. If there is no optional parent element and the element doesn't have a parentNode then it will be appended to the document body. Throws an Error if the component is already rendered.
Arguments:
opt_parentElement :
Optional parent element to render the component into.
opt_beforeElement :
Element before which the component is to be rendered. If left out the node is appended to the parent element.
code »
setElementInternal(element)
Sets the component's root element to the given element. Considered protected and final.
Arguments:
element :
Root element for the component.
code »
setId(id)
Assigns an ID to this component instance. It is the caller's responsibility to guarantee that the ID is unique. If the component is a child of a parent component, then the parent component's child index is updated to reflect the new ID; this may throw an error if the parent already has a child with an ID that conflicts with the new ID.
Arguments:
id :
Unique component ID.
code »
setModel(obj)
Sets the model associated with the UI component.
Arguments:
obj :
*
The model.
code »
setParent(parent)
Sets the parent of this component to use for event bubbling. Throws an error if the component already has a parent or if an attempt is made to add a component to itself as a child. Callers must use {@code removeChild} or {@code removeChildAt} to remove components from their containers before calling this method.
Arguments:
parent :
The parent component.
code »
setParentEventTarget(parent)
Overrides {@link goog.events.EventTarget#setParentEventTarget} to throw an error if the parent component is set, and the argument is not the parent.
Arguments:
parent :
Parent EventTarget (null if none).
code »
setRightToLeft(rightToLeft)
Set is right-to-left. This function should be used if the component needs to know the rendering direction during dom creation (i.e. before {@link #enterDocument} is called and is right-to-left is set).
Arguments:
rightToLeft :
Whether the component is rendered right-to-left.
code »
wasDecorated()
No description.
Returns:   Whether the component was decorated.
code »
addEventListener(typehandleropt_captureopt_handlerScope)
Adds an event listener to the event target. The same handler can only be added once per the type. Even if you add the same handler multiple times using the same type then it will only be called once when the event is dispatched. Supported for legacy but use goog.events.listen(src, type, handler) instead.
Arguments:
type :
The type of the event to listen for.
handler :
The function to handle the event. The handler can also be an object that implements the handleEvent method which takes the event object as argument.
opt_capture :
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope :
Object in whose scope to call the listener.
code »
dispatchEvent(e)
Dispatches an event (or event like object) and calls all listeners listening for events of this type. The type of the event is decided by the type property on the event object. If any of the listeners returns false OR calls preventDefault then this function will return false. If one of the capture listeners calls stopPropagation, then the bubble listeners won't fire.
Arguments:
e :
Event object.
Returns:   If anyone called preventDefault on the event object (or if any of the handlers returns false this will also return false.
code »
disposeInternal()
Unattach listeners from this object. Classes that extend EventTarget may need to override this method in order to remove references to DOM Elements and additional listeners, it should be something like this:
MyClass.prototype.disposeInternal = function() {
MyClass.superClass_.disposeInternal.call(this);
// Dispose logic for MyClass
};
code »
getParentEventTarget()
Returns the parent of this event target to use for bubbling.
Returns:   The parent EventTarget or null if there is no parent.
code »
removeEventListener(typehandleropt_captureopt_handlerScope)
Removes an event listener from the event target. The handler must be the same object as the one added. If the handler has not been added then nothing is done.
Arguments:
type :
The type of the event to listen for.
handler :
The function to handle the event. The handler can also be an object that implements the handleEvent method which takes the event object as argument.
opt_capture :
In DOM-compliant browsers, this determines whether the listener is fired during the capture or bubble phase of the event.
opt_handlerScope :
Object in whose scope to call the listener.
code »
setParentEventTarget(parent)
Sets the parent of this event target to use for bubbling.
Arguments:
parent :
Parent EventTarget (null if none).
code »
dispose()
No description.
code »
disposeInternal()
Deletes or nulls out any references to COM objects, DOM nodes, or other disposable objects. Classes that extend {@code goog.Disposable} should override this method. For example:
mypackage.MyClass = function() {
goog.Disposable.call(this);
// Constructor logic specific to MyClass.
...
};
goog.inherits(mypackage.MyClass, goog.Disposable);

mypackage.MyClass.prototype.disposeInternal = function() {
mypackage.MyClass.superClass_.disposeInternal.call(this);
// Dispose logic specific to MyClass.
...
};
code »
getDisposed()
No description.
Returns:   Whether the object has been disposed of.
code »
isDisposed()
No description.
Returns:   Whether the object has been disposed of.
code »

Instance Properties

canvasElement :
The root level group element.
Code »
coordLeft :
Left coordinate of the view box
Code »
coordTop :
Top coordinate of the view box
Code »
childIndex_ :
Map of child component IDs to child components. Used for constant-time random access to child components by ID. Lazily initialized on first use. Must be kept in sync with {@code children_}. This property is strictly private and must not be accessed directly outside of this class! We use a plain Object, not a {@link goog.structs.Map}, for simplicity. This means components can't have children with IDs such as 'constructor' or 'valueOf', but this shouldn't really be an issue in practice, and if it is, we can always fix it later without changing the API.
Code »
children_ :
Array of child components. Lazily initialized on first use. Must be kept in sync with {@code childIndex_}. This property is strictly private and must not be accessed directly outside of this class!
Code »
DomHelper used to interact with the document, allowing components to be created in a different window.
Code »
element_ :
The DOM element for the component.
Code »
idGenerator_ :
Generator for unique IDs.
Code »
id_ :
Unique ID of the component, lazily initialized in {@link goog.ui.Component#getId} if needed. This property is strictly private and must not be accessed directly outside of this class!
Code »
inDocument_ :
Whether the component is in the document.
Code »
parent_ :
Parent component to which events will be propagated. This property is strictly private and must not be accessed directly outside of this class!
Code »
rightToLeft_ :
Whether the component is rendered right-to-left. Right-to-left is set lazily when {@link #isRightToLeft} is called the first time, unless it has been set by calling {@link #setRightToLeft} explicitly.
Code »
wasDecorated_ :
Flag used to keep track of whether a component decorated an already existing element or whether it created the DOM itself. If an element was decorated dispose will remove the node from the document, it is left up to the app.
Code »
customEvent_ :
Used to tell if an event is a real event in goog.events.listen() so we don't get listen() calling addEventListener() and vice-versa.
Code »
parentEventTarget_ :
Parent event target, used during event bubbling.
Code »
disposed_ :
Whether the object has been disposed of.
Code »

Static Methods

goog.graphics.SvgGraphics.getResizeCheckTimer_()
No description.
Returns:   The centralized timer object used for interval timing.
code »
goog.graphics.SvgGraphics.getSvgPath(path)
Returns a string representation of a logical path suitable for use in an SVG element.
Arguments:
path :
The logical path.
Returns:   The SVG path representation.
code »

Static Properties

goog.graphics.SvgGraphics.DEF_ID_PREFIX_ :
The name prefix for def entries
Code »
goog.graphics.SvgGraphics.SVG_NS_ :
The SVG namespace URN
Code »
goog.graphics.SvgGraphics.nextDefId_ :
The next available unique identifier for a def entry. This is a static variable, so that when multiple graphics are used in one document, the same def id can not be re-defined by another SvgGraphics.
Code »

Package graphics

Package Reference