reverse.js 2.8 KB

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