Path: blob/trunk/third_party/closure/goog/fx/cssspriteanimation.js
2868 views
// Copyright 2008 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.1314/**15* @fileoverview An animation class that animates CSS sprites by changing the16* CSS background-position.17*18* @author [email protected] (Erik Arvidsson)19* @see ../demos/cssspriteanimation.html20*/2122goog.provide('goog.fx.CssSpriteAnimation');2324goog.require('goog.fx.Animation');25262728/**29* This animation class is used to animate a CSS sprite (moving a background30* image). This moves through a series of images in a single image sprite. By31* default, the animation loops when done. Looping can be disabled by setting32* {@code opt_disableLoop} and results in the animation stopping on the last33* image in the image sprite. You should set up the {@code background-image}34* and size in a CSS rule for the relevant element.35*36* @param {Element} element The HTML element to animate the background for.37* @param {goog.math.Size} size The size of one image in the image sprite.38* @param {goog.math.Box} box The box describing the layout of the sprites to39* use in the large image. The sprites can be position horizontally or40* vertically and using a box here allows the implementation to know which41* way to go.42* @param {number} time The duration in milliseconds for one iteration of the43* animation. For example, if the sprite contains 4 images and the duration44* is set to 400ms then each sprite will be displayed for 100ms.45* @param {function(number) : number=} opt_acc Acceleration function,46* returns 0-1 for inputs 0-1. This can be used to make certain frames be47* shown for a longer period of time.48* @param {boolean=} opt_disableLoop Whether the animation should be halted49* after a single loop of the images in the sprite.50*51* @constructor52* @struct53* @extends {goog.fx.Animation}54* @final55*/56goog.fx.CssSpriteAnimation = function(57element, size, box, time, opt_acc, opt_disableLoop) {58var start = [box.left, box.top];59// We never draw for the end so we do not need to subtract for the size60var end = [box.right, box.bottom];61goog.fx.CssSpriteAnimation.base(62this, 'constructor', start, end, time, opt_acc);6364/**65* HTML element that will be used in the animation.66* @type {Element}67* @private68*/69this.element_ = element;7071/**72* The size of an individual sprite in the image sprite.73* @type {goog.math.Size}74* @private75*/76this.size_ = size;7778/**79* Whether the animation should be halted after a single loop of the images80* in the sprite.81* @type {boolean}82* @private83*/84this.disableLoop_ = !!opt_disableLoop;85};86goog.inherits(goog.fx.CssSpriteAnimation, goog.fx.Animation);878889/** @override */90goog.fx.CssSpriteAnimation.prototype.onAnimate = function() {91// Round to nearest sprite.92var x = -Math.floor(this.coords[0] / this.size_.width) * this.size_.width;93var y = -Math.floor(this.coords[1] / this.size_.height) * this.size_.height;94this.element_.style.backgroundPosition = x + 'px ' + y + 'px';9596goog.fx.CssSpriteAnimation.base(this, 'onAnimate');97};9899100/** @override */101goog.fx.CssSpriteAnimation.prototype.onFinish = function() {102if (!this.disableLoop_) {103this.play(true);104}105goog.fx.CssSpriteAnimation.base(this, 'onFinish');106};107108109/**110* Clears the background position style set directly on the element111* by the animation. Allows to apply CSS styling for background position on the112* same element when the sprite animation is not runniing.113*/114goog.fx.CssSpriteAnimation.prototype.clearSpritePosition = function() {115var style = this.element_.style;116style.backgroundPosition = '';117118if (typeof style.backgroundPositionX != 'undefined') {119// IE needs to clear x and y to actually clear the position120style.backgroundPositionX = '';121style.backgroundPositionY = '';122}123};124125126/** @override */127goog.fx.CssSpriteAnimation.prototype.disposeInternal = function() {128goog.fx.CssSpriteAnimation.superClass_.disposeInternal.call(this);129this.element_ = null;130};131132133