Path: blob/trunk/third_party/closure/goog/graphics/element.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 A thin wrapper around the DOM element returned from17* the different draw methods of the graphics implementation, and18* all interfaces that the various element types support.19* @author [email protected] (Erik Arvidsson)20*/212223goog.provide('goog.graphics.Element');2425goog.require('goog.asserts');26goog.require('goog.events');27goog.require('goog.events.EventTarget');28goog.require('goog.events.Listenable');29goog.require('goog.graphics.AffineTransform');30goog.require('goog.math');31323334/**35* Base class for a thin wrapper around the DOM element returned from36* the different draw methods of the graphics.37* You should not construct objects from this constructor. The graphics38* will return the object for you.39* @param {Element} element The DOM element to wrap.40* @param {goog.graphics.AbstractGraphics} graphics The graphics creating41* this element.42* @constructor43* @extends {goog.events.EventTarget}44* @deprecated goog.graphics is deprecated. It existed to abstract over browser45* differences before the canvas tag was widely supported. See46* http://en.wikipedia.org/wiki/Canvas_element for details.47*/48goog.graphics.Element = function(element, graphics) {49goog.events.EventTarget.call(this);50this.element_ = element;51this.graphics_ = graphics;52// Overloading EventTarget field to state that this is not a custom event.53// TODO(user) Should be handled in EventTarget.js (see bug 846824).54this[goog.events.Listenable.IMPLEMENTED_BY_PROP] = false;55};56goog.inherits(goog.graphics.Element, goog.events.EventTarget);575859/**60* The graphics object that contains this element.61* @type {goog.graphics.AbstractGraphics?}62* @private63*/64goog.graphics.Element.prototype.graphics_ = null;656667/**68* The native browser element this class wraps.69* @type {Element}70* @private71*/72goog.graphics.Element.prototype.element_ = null;737475/**76* The transformation applied to this element.77* @type {goog.graphics.AffineTransform?}78* @private79*/80goog.graphics.Element.prototype.transform_ = null;818283/**84* Returns the underlying object.85* @return {Element} The underlying element.86*/87goog.graphics.Element.prototype.getElement = function() {88return this.element_;89};909192/**93* Returns the graphics.94* @return {goog.graphics.AbstractGraphics} The graphics that created the95* element.96*/97goog.graphics.Element.prototype.getGraphics = function() {98return this.graphics_;99};100101102/**103* Set the translation and rotation of the element.104*105* If a more general affine transform is needed than this provides106* (e.g. skew and scale) then use setTransform.107* @param {number} x The x coordinate of the translation transform.108* @param {number} y The y coordinate of the translation transform.109* @param {number} rotate The angle of the rotation transform.110* @param {number} centerX The horizontal center of the rotation transform.111* @param {number} centerY The vertical center of the rotation transform.112*/113goog.graphics.Element.prototype.setTransformation = function(114x, y, rotate, centerX, centerY) {115this.transform_ =116goog.graphics.AffineTransform117.getRotateInstance(goog.math.toRadians(rotate), centerX, centerY)118.translate(x, y);119this.getGraphics().setElementTransform(this, x, y, rotate, centerX, centerY);120};121122123/**124* @return {!goog.graphics.AffineTransform} The transformation applied to125* this element.126*/127goog.graphics.Element.prototype.getTransform = function() {128return this.transform_ ? this.transform_.clone() :129new goog.graphics.AffineTransform();130};131132133/**134* Set the affine transform of the element.135* @param {!goog.graphics.AffineTransform} affineTransform The136* transformation applied to this element.137*/138goog.graphics.Element.prototype.setTransform = function(affineTransform) {139this.transform_ = affineTransform.clone();140this.getGraphics().setElementAffineTransform(this, affineTransform);141};142143144/** @override */145goog.graphics.Element.prototype.addEventListener = function(146type, handler, opt_capture, opt_handlerScope) {147goog.events.listen(148this.element_, type, handler, opt_capture, opt_handlerScope);149};150151152/** @override */153goog.graphics.Element.prototype.removeEventListener = function(154type, handler, opt_capture, opt_handlerScope) {155goog.events.unlisten(156this.element_, type, handler, opt_capture, opt_handlerScope);157};158159160/** @override */161goog.graphics.Element.prototype.disposeInternal = function() {162goog.graphics.Element.superClass_.disposeInternal.call(this);163goog.asserts.assert(this.element_);164goog.events.removeAll(this.element_);165};166167168