reverse.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.fx.ext-dojo.reverse"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.fx.ext-dojo.reverse"] = true;
  8. dojo.provide("dojox.fx.ext-dojo.reverse");
  9. dojo.require("dojo.fx.easing");
  10. dojo.require("dojo.fx");
  11. dojo.extend(dojo.Animation, {
  12. // summary:
  13. // A dojo.Animation extension that enables an easy reversal.
  14. // description:
  15. // To use, simply require dojox.fx.ext-dojo.reverse and a reverse()
  16. // method will be added to all dojo.Animations.
  17. // It can be used at any time during the animation. It does not
  18. // need to be called when it ends. It also reverses the easing -
  19. // if dojo.fx.easing.quadIn is used, dojo.fx.easing.quadOut will
  20. // be used when animating backwards.
  21. //
  22. _reversed: false,
  23. reverse: function(/*Boolean*/keepPaused, /*Function ? */reverseEase){
  24. // summary:
  25. // The key method added to an animation to enable reversal.
  26. // keepPaused: Boolean
  27. // By default, calling reverse() will play the animation if
  28. // it was paused. Pass in true to keep it paused (will have
  29. // no effect if reverse is called while animation is playing).
  30. // reverseEase: Function
  31. // A function to use for the reverse easing. This allows for
  32. // the possibility of custom eases that are not in the dojo.fx
  33. // library.
  34. //
  35. var playing = this.status() == "playing";
  36. this.pause();
  37. this._reversed = !this._reversed;
  38. var d = this.duration,
  39. sofar = d * this._percent,
  40. togo = d - sofar,
  41. curr = new Date().valueOf(),
  42. cp = this.curve._properties,
  43. p = this.properties,
  44. nm
  45. ;
  46. this._endTime = curr + sofar;
  47. this._startTime = curr - togo;
  48. if(playing){
  49. this.gotoPercent(togo / d)
  50. }
  51. for(nm in p){
  52. var tmp = p[nm].start;
  53. p[nm].start = cp[nm].start = p[nm].end;
  54. p[nm].end = cp[nm].end = tmp;
  55. }
  56. if(this._reversed){
  57. if(!this.rEase){
  58. this.fEase = this.easing;
  59. if(reverseEase){
  60. this.rEase = reverseEase;
  61. }else{
  62. // loop through dojo.fx.easing to find the matching ease
  63. var de = dojo.fx.easing, found, eName;
  64. for(nm in de){
  65. if(this.easing == de[nm]){
  66. // get ease's name
  67. found = nm; break;
  68. }
  69. }
  70. if(found){
  71. // find ease's opposite
  72. if(/InOut/.test(nm) || !/In|Out/i.test(nm)){
  73. this.rEase = this.easing;
  74. }else if(/In/.test(nm)){
  75. eName = nm.replace("In", "Out");
  76. }else{
  77. eName = nm.replace("Out", "In");
  78. }
  79. if(eName){
  80. this.rEase = dojo.fx.easing[eName];
  81. }
  82. }else{
  83. // default ease, and other's like linear do not have an opposite
  84. console.info("ease function to reverse not found");
  85. this.rEase = this.easing;
  86. }
  87. }
  88. }
  89. this.easing = this.rEase;
  90. }else{
  91. this.easing = this.fEase;
  92. }
  93. if(!keepPaused && this.status() != "playing"){
  94. this.play();
  95. }
  96. return this;
  97. }
  98. });
  99. }