| 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 |
|
| 16 |
/** |
| 17 |
* @fileoverview Graphics surface type. |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
|
| 22 |
goog.provide('goog.graphics.ext.Graphics'); |
| 23 |
|
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.events.EventType'); |
| 26 |
goog.require('goog.graphics'); |
| 27 |
goog.require('goog.graphics.ext.Group'); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Wrapper for a graphics surface. |
| 33 |
* @param {string|number} width The width in pixels. Strings |
| 34 |
* expressing percentages of parent with (e.g. '80%') are also accepted. |
| 35 |
* @param {string|number} height The height in pixels. Strings |
| 36 |
* expressing percentages of parent with (e.g. '80%') are also accepted. |
| 37 |
* @param {?number=} opt_coordWidth The coordinate width - if |
| 38 |
* omitted or null, defaults to same as width. |
| 39 |
* @param {?number=} opt_coordHeight The coordinate height. - if |
| 40 |
* omitted or null, defaults to same as height. |
| 41 |
* @param {goog.dom.DomHelper=} opt_domHelper The DOM helper object for the |
| 42 |
* document we want to render in. |
| 43 |
* @param {boolean=} opt_isSimple Flag used to indicate the graphics object will |
| 44 |
* be drawn to in a single pass, and the fastest implementation for this |
| 45 |
* scenario should be favored. NOTE: Setting to true may result in |
| 46 |
* degradation of text support. |
| 47 |
* @constructor |
| 48 |
* @extends {goog.graphics.ext.Group} |
| 49 |
* @final |
| 50 |
*/ |
| 51 |
goog.graphics.ext.Graphics = function( |
| 52 |
width, height, opt_coordWidth, opt_coordHeight, opt_domHelper, |
| 53 |
opt_isSimple) { |
| 54 |
var surface = opt_isSimple ? |
| 55 |
goog.graphics.createSimpleGraphics( |
| 56 |
width, height, opt_coordWidth, opt_coordHeight, opt_domHelper) : |
| 57 |
goog.graphics.createGraphics( |
| 58 |
width, height, opt_coordWidth, opt_coordHeight, opt_domHelper); |
| 59 |
this.implementation_ = surface; |
| 60 |
|
| 61 |
goog.graphics.ext.Group.call(this, null, surface.getCanvasElement()); |
| 62 |
|
| 63 |
goog.events.listen( |
| 64 |
surface, goog.events.EventType.RESIZE, this.updateChildren, false, this); |
| 65 |
}; |
| 66 |
goog.inherits(goog.graphics.ext.Graphics, goog.graphics.ext.Group); |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* The root level graphics implementation. |
| 71 |
* @type {goog.graphics.AbstractGraphics} |
| 72 |
* @private |
| 73 |
*/ |
| 74 |
goog.graphics.ext.Graphics.prototype.implementation_; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* @return {goog.graphics.AbstractGraphics} The graphics implementation layer. |
| 79 |
*/ |
| 80 |
goog.graphics.ext.Graphics.prototype.getImplementation = function() { |
| 81 |
return this.implementation_; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Changes the coordinate size. |
| 87 |
* @param {number} coordWidth The coordinate width. |
| 88 |
* @param {number} coordHeight The coordinate height. |
| 89 |
*/ |
| 90 |
goog.graphics.ext.Graphics.prototype.setCoordSize = function( |
| 91 |
coordWidth, coordHeight) { |
| 92 |
this.implementation_.setCoordSize(coordWidth, coordHeight); |
| 93 |
goog.graphics.ext.Graphics.superClass_.setSize.call( |
| 94 |
this, coordWidth, coordHeight); |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* @return {goog.math.Size} The coordinate size. |
| 100 |
*/ |
| 101 |
goog.graphics.ext.Graphics.prototype.getCoordSize = function() { |
| 102 |
return this.implementation_.getCoordSize(); |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Changes the coordinate system position. |
| 108 |
* @param {number} left The coordinate system left bound. |
| 109 |
* @param {number} top The coordinate system top bound. |
| 110 |
*/ |
| 111 |
goog.graphics.ext.Graphics.prototype.setCoordOrigin = function(left, top) { |
| 112 |
this.implementation_.setCoordOrigin(left, top); |
| 113 |
}; |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* @return {!goog.math.Coordinate} The coordinate system position. |
| 118 |
*/ |
| 119 |
goog.graphics.ext.Graphics.prototype.getCoordOrigin = function() { |
| 120 |
return this.implementation_.getCoordOrigin(); |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
/** |
| 125 |
* Change the size of the canvas. |
| 126 |
* @param {number} pixelWidth The width in pixels. |
| 127 |
* @param {number} pixelHeight The height in pixels. |
| 128 |
*/ |
| 129 |
goog.graphics.ext.Graphics.prototype.setPixelSize = function( |
| 130 |
pixelWidth, pixelHeight) { |
| 131 |
this.implementation_.setSize(pixelWidth, pixelHeight); |
| 132 |
|
| 133 |
var coordSize = this.getCoordSize(); |
| 134 |
goog.graphics.ext.Graphics.superClass_.setSize.call( |
| 135 |
this, coordSize.width, coordSize.height); |
| 136 |
}; |
| 137 |
|
| 138 |
|
| 139 |
/** |
| 140 |
* @return {goog.math.Size?} Returns the number of pixels spanned by the |
| 141 |
* surface, or null if the size could not be computed due to the size being |
| 142 |
* specified in percentage points and the component not being in the |
| 143 |
* document. |
| 144 |
*/ |
| 145 |
goog.graphics.ext.Graphics.prototype.getPixelSize = function() { |
| 146 |
return this.implementation_.getPixelSize(); |
| 147 |
}; |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* @return {number} The coordinate width of the canvas. |
| 152 |
* @override |
| 153 |
*/ |
| 154 |
goog.graphics.ext.Graphics.prototype.getWidth = function() { |
| 155 |
return this.implementation_.getCoordSize().width; |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* @return {number} The coordinate width of the canvas. |
| 161 |
* @override |
| 162 |
*/ |
| 163 |
goog.graphics.ext.Graphics.prototype.getHeight = function() { |
| 164 |
return this.implementation_.getCoordSize().height; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* @return {number} Returns the number of pixels per unit in the x direction. |
| 170 |
* @override |
| 171 |
*/ |
| 172 |
goog.graphics.ext.Graphics.prototype.getPixelScaleX = function() { |
| 173 |
return this.implementation_.getPixelScaleX(); |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* @return {number} Returns the number of pixels per unit in the y direction. |
| 179 |
* @override |
| 180 |
*/ |
| 181 |
goog.graphics.ext.Graphics.prototype.getPixelScaleY = function() { |
| 182 |
return this.implementation_.getPixelScaleY(); |
| 183 |
}; |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* @return {Element} The root element of the graphics surface. |
| 188 |
*/ |
| 189 |
goog.graphics.ext.Graphics.prototype.getElement = function() { |
| 190 |
return this.implementation_.getElement(); |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Renders the underlying graphics. |
| 196 |
* |
| 197 |
* @param {Element} parentElement Parent element to render the component into. |
| 198 |
*/ |
| 199 |
goog.graphics.ext.Graphics.prototype.render = function(parentElement) { |
| 200 |
this.implementation_.render(parentElement); |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
/** |
| 205 |
* Never transform a surface. |
| 206 |
* @override |
| 207 |
*/ |
| 208 |
goog.graphics.ext.Graphics.prototype.transform = goog.nullFunction; |
| 209 |
|
| 210 |
|
| 211 |
/** |
| 212 |
* Called from the parent class, this method resets any pre-computed positions |
| 213 |
* and sizes. |
| 214 |
* @protected |
| 215 |
* @override |
| 216 |
*/ |
| 217 |
goog.graphics.ext.Graphics.prototype.redraw = function() { |
| 218 |
this.transformChildren(); |
| 219 |
}; |