graphics.js 6.3 KB

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