element.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 A thin wrapper around the DOM element returned from
  16. * the different draw methods of the graphics implementation, and
  17. * all interfaces that the various element types support.
  18. * @author arv@google.com (Erik Arvidsson)
  19. */
  20. goog.provide('goog.graphics.Element');
  21. goog.require('goog.asserts');
  22. goog.require('goog.events');
  23. goog.require('goog.events.EventTarget');
  24. goog.require('goog.events.Listenable');
  25. goog.require('goog.graphics.AffineTransform');
  26. goog.require('goog.math');
  27. /**
  28. * Base class for a thin wrapper around the DOM element returned from
  29. * the different draw methods of the graphics.
  30. * You should not construct objects from this constructor. The graphics
  31. * will return the object for you.
  32. * @param {Element} element The DOM element to wrap.
  33. * @param {goog.graphics.AbstractGraphics} graphics The graphics creating
  34. * this element.
  35. * @constructor
  36. * @extends {goog.events.EventTarget}
  37. * @deprecated goog.graphics is deprecated. It existed to abstract over browser
  38. * differences before the canvas tag was widely supported. See
  39. * http://en.wikipedia.org/wiki/Canvas_element for details.
  40. */
  41. goog.graphics.Element = function(element, graphics) {
  42. goog.events.EventTarget.call(this);
  43. this.element_ = element;
  44. this.graphics_ = graphics;
  45. // Overloading EventTarget field to state that this is not a custom event.
  46. // TODO(user) Should be handled in EventTarget.js (see bug 846824).
  47. this[goog.events.Listenable.IMPLEMENTED_BY_PROP] = false;
  48. };
  49. goog.inherits(goog.graphics.Element, goog.events.EventTarget);
  50. /**
  51. * The graphics object that contains this element.
  52. * @type {goog.graphics.AbstractGraphics?}
  53. * @private
  54. */
  55. goog.graphics.Element.prototype.graphics_ = null;
  56. /**
  57. * The native browser element this class wraps.
  58. * @type {Element}
  59. * @private
  60. */
  61. goog.graphics.Element.prototype.element_ = null;
  62. /**
  63. * The transformation applied to this element.
  64. * @type {goog.graphics.AffineTransform?}
  65. * @private
  66. */
  67. goog.graphics.Element.prototype.transform_ = null;
  68. /**
  69. * Returns the underlying object.
  70. * @return {Element} The underlying element.
  71. */
  72. goog.graphics.Element.prototype.getElement = function() {
  73. return this.element_;
  74. };
  75. /**
  76. * Returns the graphics.
  77. * @return {goog.graphics.AbstractGraphics} The graphics that created the
  78. * element.
  79. */
  80. goog.graphics.Element.prototype.getGraphics = function() {
  81. return this.graphics_;
  82. };
  83. /**
  84. * Set the translation and rotation of the element.
  85. *
  86. * If a more general affine transform is needed than this provides
  87. * (e.g. skew and scale) then use setTransform.
  88. * @param {number} x The x coordinate of the translation transform.
  89. * @param {number} y The y coordinate of the translation transform.
  90. * @param {number} rotate The angle of the rotation transform.
  91. * @param {number} centerX The horizontal center of the rotation transform.
  92. * @param {number} centerY The vertical center of the rotation transform.
  93. */
  94. goog.graphics.Element.prototype.setTransformation = function(
  95. x, y, rotate, centerX, centerY) {
  96. this.transform_ =
  97. goog.graphics.AffineTransform
  98. .getRotateInstance(goog.math.toRadians(rotate), centerX, centerY)
  99. .translate(x, y);
  100. this.getGraphics().setElementTransform(this, x, y, rotate, centerX, centerY);
  101. };
  102. /**
  103. * @return {!goog.graphics.AffineTransform} The transformation applied to
  104. * this element.
  105. */
  106. goog.graphics.Element.prototype.getTransform = function() {
  107. return this.transform_ ? this.transform_.clone() :
  108. new goog.graphics.AffineTransform();
  109. };
  110. /**
  111. * Set the affine transform of the element.
  112. * @param {!goog.graphics.AffineTransform} affineTransform The
  113. * transformation applied to this element.
  114. */
  115. goog.graphics.Element.prototype.setTransform = function(affineTransform) {
  116. this.transform_ = affineTransform.clone();
  117. this.getGraphics().setElementAffineTransform(this, affineTransform);
  118. };
  119. /** @override */
  120. goog.graphics.Element.prototype.addEventListener = function(
  121. type, handler, opt_capture, opt_handlerScope) {
  122. goog.events.listen(
  123. this.element_, type, handler, opt_capture, opt_handlerScope);
  124. };
  125. /** @override */
  126. goog.graphics.Element.prototype.removeEventListener = function(
  127. type, handler, opt_capture, opt_handlerScope) {
  128. goog.events.unlisten(
  129. this.element_, type, handler, opt_capture, opt_handlerScope);
  130. };
  131. /** @override */
  132. goog.graphics.Element.prototype.disposeInternal = function() {
  133. goog.graphics.Element.superClass_.disposeInternal.call(this);
  134. goog.asserts.assert(this.element_);
  135. goog.events.removeAll(this.element_);
  136. };