Path: blob/trunk/third_party/closure/goog/graphics/abstractgraphics.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 Graphics utility functions and factory methods.17* @author [email protected] (Erik Arvidsson)18*/192021goog.provide('goog.graphics.AbstractGraphics');2223goog.require('goog.dom');24goog.require('goog.graphics.AffineTransform');25goog.require('goog.graphics.Element');26goog.require('goog.graphics.EllipseElement');27goog.require('goog.graphics.Fill');28goog.require('goog.graphics.Font');29goog.require('goog.graphics.GroupElement');30goog.require('goog.graphics.Path');31goog.require('goog.graphics.PathElement');32goog.require('goog.graphics.RectElement');33goog.require('goog.graphics.Stroke');34goog.require('goog.graphics.StrokeAndFillElement');35goog.require('goog.graphics.TextElement');36goog.require('goog.math.Coordinate');37goog.require('goog.math.Size');38goog.require('goog.style');39goog.require('goog.ui.Component');40414243/**44* Base class for the different graphics. You should never construct objects45* of this class. Instead us goog.graphics.createGraphics46* @param {number|string} width The width in pixels or percent.47* @param {number|string} height The height in pixels or percent.48* @param {?number=} opt_coordWidth Optional coordinate system width - if49* omitted or null, defaults to same as width.50* @param {?number=} opt_coordHeight Optional coordinate system height - if51* omitted or null, defaults to same as height.52* @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the53* document we want to render in.54* @constructor55* @extends {goog.ui.Component}56*/57goog.graphics.AbstractGraphics = function(58width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) {59goog.ui.Component.call(this, opt_domHelper);6061/**62* Width of graphics in pixels or percentage points.63* @type {number|string}64* @protected65*/66this.width = width;6768/**69* Height of graphics in pixels or percentage points.70* @type {number|string}71* @protected72*/73this.height = height;7475/**76* Width of coordinate system in units.77* @type {?number}78* @protected79*/80this.coordWidth = opt_coordWidth || null;8182/**83* Height of coordinate system in units.84* @type {?number}85* @protected86*/87this.coordHeight = opt_coordHeight || null;88};89goog.inherits(goog.graphics.AbstractGraphics, goog.ui.Component);909192/**93* The root level group element.94* @type {goog.graphics.GroupElement?}95* @protected96*/97goog.graphics.AbstractGraphics.prototype.canvasElement = null;9899100/**101* Left coordinate of the view box102* @type {number}103* @protected104*/105goog.graphics.AbstractGraphics.prototype.coordLeft = 0;106107108/**109* Top coordinate of the view box110* @type {number}111* @protected112*/113goog.graphics.AbstractGraphics.prototype.coordTop = 0;114115116/**117* @return {goog.graphics.GroupElement} The root level canvas element.118*/119goog.graphics.AbstractGraphics.prototype.getCanvasElement = function() {120return this.canvasElement;121};122123124/**125* Changes the coordinate size.126* @param {number} coordWidth The coordinate width.127* @param {number} coordHeight The coordinate height.128*/129goog.graphics.AbstractGraphics.prototype.setCoordSize = function(130coordWidth, coordHeight) {131this.coordWidth = coordWidth;132this.coordHeight = coordHeight;133};134135136/**137* @return {goog.math.Size} The coordinate size.138*/139goog.graphics.AbstractGraphics.prototype.getCoordSize = function() {140if (this.coordWidth) {141return new goog.math.Size(142this.coordWidth,143/** @type {number} */ (this.coordHeight));144} else {145return this.getPixelSize();146}147};148149150/**151* Changes the coordinate system position.152* @param {number} left The coordinate system left bound.153* @param {number} top The coordinate system top bound.154*/155goog.graphics.AbstractGraphics.prototype.setCoordOrigin = goog.abstractMethod;156157158/**159* @return {!goog.math.Coordinate} The coordinate system position.160*/161goog.graphics.AbstractGraphics.prototype.getCoordOrigin = function() {162return new goog.math.Coordinate(this.coordLeft, this.coordTop);163};164165166/**167* Change the size of the canvas.168* @param {number} pixelWidth The width in pixels.169* @param {number} pixelHeight The height in pixels.170*/171goog.graphics.AbstractGraphics.prototype.setSize = goog.abstractMethod;172173174/**175* @return {goog.math.Size} The size of canvas.176* @deprecated Use getPixelSize.177*/178goog.graphics.AbstractGraphics.prototype.getSize = function() {179return this.getPixelSize();180};181182183/**184* @return {goog.math.Size?} Returns the number of pixels spanned by the185* surface, or null if the size could not be computed due to the size being186* specified in percentage points and the component not being in the187* document.188*/189goog.graphics.AbstractGraphics.prototype.getPixelSize = function() {190if (this.isInDocument()) {191return goog.style.getSize(this.getElement());192}193if (goog.isNumber(this.width) && goog.isNumber(this.height)) {194return new goog.math.Size(this.width, this.height);195}196return null;197};198199200/**201* @return {number} Returns the number of pixels per unit in the x direction.202*/203goog.graphics.AbstractGraphics.prototype.getPixelScaleX = function() {204var pixelSize = this.getPixelSize();205return pixelSize ? pixelSize.width / this.getCoordSize().width : 0;206};207208209/**210* @return {number} Returns the number of pixels per unit in the y direction.211*/212goog.graphics.AbstractGraphics.prototype.getPixelScaleY = function() {213var pixelSize = this.getPixelSize();214return pixelSize ? pixelSize.height / this.getCoordSize().height : 0;215};216217218/**219* Remove all drawing elements from the graphics.220*/221goog.graphics.AbstractGraphics.prototype.clear = goog.abstractMethod;222223224/**225* Remove a single drawing element from the surface. The default implementation226* assumes a DOM based drawing surface.227* @param {goog.graphics.Element} element The element to remove.228*/229goog.graphics.AbstractGraphics.prototype.removeElement = function(element) {230goog.dom.removeNode(element.getElement());231};232233234/**235* Sets the fill for the given element.236* @param {goog.graphics.StrokeAndFillElement} element The element wrapper.237* @param {goog.graphics.Fill?} fill The fill object.238*/239goog.graphics.AbstractGraphics.prototype.setElementFill = goog.abstractMethod;240241242/**243* Sets the stroke for the given element.244* @param {goog.graphics.StrokeAndFillElement} element The element wrapper.245* @param {goog.graphics.Stroke?} stroke The stroke object.246*/247goog.graphics.AbstractGraphics.prototype.setElementStroke = goog.abstractMethod;248249250/**251* Set the transformation of an element.252*253* If a more general affine transform is needed than this provides254* (e.g. skew and scale) then use setElementAffineTransform.255* @param {goog.graphics.Element} element The element wrapper.256* @param {number} x The x coordinate of the translation transform.257* @param {number} y The y coordinate of the translation transform.258* @param {number} angle The angle of the rotation transform.259* @param {number} centerX The horizontal center of the rotation transform.260* @param {number} centerY The vertical center of the rotation transform.261*/262goog.graphics.AbstractGraphics.prototype.setElementTransform =263goog.abstractMethod;264265266/**267* Set the affine transform of an element.268* @param {!goog.graphics.Element} element The element wrapper.269* @param {!goog.graphics.AffineTransform} affineTransform The270* transformation applied to this element.271*/272goog.graphics.AbstractGraphics.prototype.setElementAffineTransform =273goog.abstractMethod;274275276/**277* Draw a circle278*279* @param {number} cx Center X coordinate.280* @param {number} cy Center Y coordinate.281* @param {number} r Radius length.282* @param {goog.graphics.Stroke?} stroke Stroke object describing the283* stroke.284* @param {goog.graphics.Fill?} fill Fill object describing the fill.285* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to286* append to. If not specified, appends to the main canvas.287*288* @return {goog.graphics.EllipseElement} The newly created element.289*/290goog.graphics.AbstractGraphics.prototype.drawCircle = function(291cx, cy, r, stroke, fill, opt_group) {292return this.drawEllipse(cx, cy, r, r, stroke, fill, opt_group);293};294295296/**297* Draw an ellipse298*299* @param {number} cx Center X coordinate.300* @param {number} cy Center Y coordinate.301* @param {number} rx Radius length for the x-axis.302* @param {number} ry Radius length for the y-axis.303* @param {goog.graphics.Stroke?} stroke Stroke object describing the304* stroke.305* @param {goog.graphics.Fill?} fill Fill object describing the fill.306* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to307* append to. If not specified, appends to the main canvas.308*309* @return {goog.graphics.EllipseElement} The newly created element.310*/311goog.graphics.AbstractGraphics.prototype.drawEllipse = goog.abstractMethod;312313314/**315* Draw a rectangle316*317* @param {number} x X coordinate (left).318* @param {number} y Y coordinate (top).319* @param {number} width Width of rectangle.320* @param {number} height Height of rectangle.321* @param {goog.graphics.Stroke?} stroke Stroke object describing the322* stroke.323* @param {goog.graphics.Fill?} fill Fill object describing the fill.324* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to325* append to. If not specified, appends to the main canvas.326*327* @return {goog.graphics.RectElement} The newly created element.328*/329goog.graphics.AbstractGraphics.prototype.drawRect = goog.abstractMethod;330331332/**333* Draw a text string within a rectangle (drawing is horizontal)334*335* @param {string} text The text to draw.336* @param {number} x X coordinate (left).337* @param {number} y Y coordinate (top).338* @param {number} width Width of rectangle.339* @param {number} height Height of rectangle.340* @param {string} align Horizontal alignment: left (default), center, right.341* @param {string} vAlign Vertical alignment: top (default), center, bottom.342* @param {goog.graphics.Font} font Font describing the font properties.343* @param {goog.graphics.Stroke?} stroke Stroke object describing the344* stroke.345* @param {goog.graphics.Fill?} fill Fill object describing the fill.346* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to347* append to. If not specified, appends to the main canvas.348*349* @return {goog.graphics.TextElement} The newly created element.350*/351goog.graphics.AbstractGraphics.prototype.drawText = function(352text, x, y, width, height, align, vAlign, font, stroke, fill, opt_group) {353var baseline = font.size / 2; // Baseline is middle of line354var textY;355if (vAlign == 'bottom') {356textY = y + height - baseline;357} else if (vAlign == 'center') {358textY = y + height / 2;359} else {360textY = y + baseline;361}362363return this.drawTextOnLine(364text, x, textY, x + width, textY, align, font, stroke, fill, opt_group);365};366367368/**369* Draw a text string vertically centered on a given line.370*371* @param {string} text The text to draw.372* @param {number} x1 X coordinate of start of line.373* @param {number} y1 Y coordinate of start of line.374* @param {number} x2 X coordinate of end of line.375* @param {number} y2 Y coordinate of end of line.376* @param {string} align Horizontal alingnment: left (default), center, right.377* @param {goog.graphics.Font} font Font describing the font properties.378* @param {goog.graphics.Stroke?} stroke Stroke object describing the379* stroke.380* @param {goog.graphics.Fill?} fill Fill object describing the fill.381* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to382* append to. If not specified, appends to the main canvas.383*384* @return {goog.graphics.TextElement} The newly created element.385*/386goog.graphics.AbstractGraphics.prototype.drawTextOnLine = goog.abstractMethod;387388389/**390* Draw a path.391*392* @param {!goog.graphics.Path} path The path object to draw.393* @param {goog.graphics.Stroke?} stroke Stroke object describing the394* stroke.395* @param {goog.graphics.Fill?} fill Fill object describing the fill.396* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to397* append to. If not specified, appends to the main canvas.398*399* @return {goog.graphics.PathElement} The newly created element.400*/401goog.graphics.AbstractGraphics.prototype.drawPath = goog.abstractMethod;402403404/**405* Create an empty group of drawing elements.406*407* @param {goog.graphics.GroupElement=} opt_group The group wrapper element to408* append to. If not specified, appends to the main canvas.409*410* @return {goog.graphics.GroupElement} The newly created group.411*/412goog.graphics.AbstractGraphics.prototype.createGroup = goog.abstractMethod;413414415/**416* Create an empty path.417*418* @return {!goog.graphics.Path} The path.419* @deprecated Use {@code new goog.graphics.Path()}.420*/421goog.graphics.AbstractGraphics.prototype.createPath = function() {422return new goog.graphics.Path();423};424425426/**427* Measure and return the width (in pixels) of a given text string.428* Text measurement is needed to make sure a text can fit in the allocated429* area. The way text length is measured is by writing it into a div that is430* after the visible area, measure the div width, and immediately erase the431* written value.432*433* @param {string} text The text string to measure.434* @param {goog.graphics.Font} font The font object describing the font style.435*436* @return {number} The width in pixels of the text strings.437*/438goog.graphics.AbstractGraphics.prototype.getTextWidth = goog.abstractMethod;439440441/**442* @return {boolean} Whether the underlying element can be cloned resulting in443* an accurate reproduction of the graphics contents.444*/445goog.graphics.AbstractGraphics.prototype.isDomClonable = function() {446return false;447};448449450/**451* Start preventing redraws - useful for chaining large numbers of changes452* together. Not guaranteed to do anything - i.e. only use this for453* optimization of a single code path.454*/455goog.graphics.AbstractGraphics.prototype.suspend = function() {};456457458/**459* Stop preventing redraws. If any redraws had been prevented, a redraw will460* be done now.461*/462goog.graphics.AbstractGraphics.prototype.resume = function() {};463464465