cssspriteanimation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2008 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 An animation class that animates CSS sprites by changing the
  16. * CSS background-position.
  17. *
  18. * @author arv@google.com (Erik Arvidsson)
  19. * @see ../demos/cssspriteanimation.html
  20. */
  21. goog.provide('goog.fx.CssSpriteAnimation');
  22. goog.require('goog.fx.Animation');
  23. /**
  24. * This animation class is used to animate a CSS sprite (moving a background
  25. * image). This moves through a series of images in a single image sprite. By
  26. * default, the animation loops when done. Looping can be disabled by setting
  27. * {@code opt_disableLoop} and results in the animation stopping on the last
  28. * image in the image sprite. You should set up the {@code background-image}
  29. * and size in a CSS rule for the relevant element.
  30. *
  31. * @param {Element} element The HTML element to animate the background for.
  32. * @param {goog.math.Size} size The size of one image in the image sprite.
  33. * @param {goog.math.Box} box The box describing the layout of the sprites to
  34. * use in the large image. The sprites can be position horizontally or
  35. * vertically and using a box here allows the implementation to know which
  36. * way to go.
  37. * @param {number} time The duration in milliseconds for one iteration of the
  38. * animation. For example, if the sprite contains 4 images and the duration
  39. * is set to 400ms then each sprite will be displayed for 100ms.
  40. * @param {function(number) : number=} opt_acc Acceleration function,
  41. * returns 0-1 for inputs 0-1. This can be used to make certain frames be
  42. * shown for a longer period of time.
  43. * @param {boolean=} opt_disableLoop Whether the animation should be halted
  44. * after a single loop of the images in the sprite.
  45. *
  46. * @constructor
  47. * @struct
  48. * @extends {goog.fx.Animation}
  49. * @final
  50. */
  51. goog.fx.CssSpriteAnimation = function(
  52. element, size, box, time, opt_acc, opt_disableLoop) {
  53. var start = [box.left, box.top];
  54. // We never draw for the end so we do not need to subtract for the size
  55. var end = [box.right, box.bottom];
  56. goog.fx.CssSpriteAnimation.base(
  57. this, 'constructor', start, end, time, opt_acc);
  58. /**
  59. * HTML element that will be used in the animation.
  60. * @type {Element}
  61. * @private
  62. */
  63. this.element_ = element;
  64. /**
  65. * The size of an individual sprite in the image sprite.
  66. * @type {goog.math.Size}
  67. * @private
  68. */
  69. this.size_ = size;
  70. /**
  71. * Whether the animation should be halted after a single loop of the images
  72. * in the sprite.
  73. * @type {boolean}
  74. * @private
  75. */
  76. this.disableLoop_ = !!opt_disableLoop;
  77. };
  78. goog.inherits(goog.fx.CssSpriteAnimation, goog.fx.Animation);
  79. /** @override */
  80. goog.fx.CssSpriteAnimation.prototype.onAnimate = function() {
  81. // Round to nearest sprite.
  82. var x = -Math.floor(this.coords[0] / this.size_.width) * this.size_.width;
  83. var y = -Math.floor(this.coords[1] / this.size_.height) * this.size_.height;
  84. this.element_.style.backgroundPosition = x + 'px ' + y + 'px';
  85. goog.fx.CssSpriteAnimation.base(this, 'onAnimate');
  86. };
  87. /** @override */
  88. goog.fx.CssSpriteAnimation.prototype.onFinish = function() {
  89. if (!this.disableLoop_) {
  90. this.play(true);
  91. }
  92. goog.fx.CssSpriteAnimation.base(this, 'onFinish');
  93. };
  94. /**
  95. * Clears the background position style set directly on the element
  96. * by the animation. Allows to apply CSS styling for background position on the
  97. * same element when the sprite animation is not runniing.
  98. */
  99. goog.fx.CssSpriteAnimation.prototype.clearSpritePosition = function() {
  100. var style = this.element_.style;
  101. style.backgroundPosition = '';
  102. if (typeof style.backgroundPositionX != 'undefined') {
  103. // IE needs to clear x and y to actually clear the position
  104. style.backgroundPositionX = '';
  105. style.backgroundPositionY = '';
  106. }
  107. };
  108. /** @override */
  109. goog.fx.CssSpriteAnimation.prototype.disposeInternal = function() {
  110. goog.fx.CssSpriteAnimation.superClass_.disposeInternal.call(this);
  111. this.element_ = null;
  112. };