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