JAVASCRIPT   9

Anime.js

Guest on 18th May 2021 04:41:26 AM

  1. ///
  2. //  Anime v 1.0
  3. //  Russell Lowke, Febrauary 27th
  4. //
  5. //  Copyright (c) Lowke Media
  6. //  see http://www.lowkemedia.com for more information
  7. //
  8. //  Permission is hereby granted, free of charge, to any person obtaining a
  9. //  copy of this software and associated documentation files (the "Software"),
  10. //  to deal in the Software without restriction, including without limitation
  11. //  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. //  and/or sell copies of the Software, and to permit persons to whom the
  13. //  Software is furnished to do so, subject to the following conditions:
  14. //
  15. //  The above copyright notice and this permission notice shall be included in
  16. //  all copies or substantial portions of the Software.
  17. //
  18. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. //  IN THE SOFTWARE.
  25. //
  26. //
  27.  
  28. var Anime = function(animatem,          // (Animatem) Controlling animator
  29.                      target)            // (object) Target being animatedp
  30. {
  31.     this.animatem = animatem;           // (Animatem) controlling animatem object
  32.     this.target = target;               // (object) target associated with this anime
  33.     this.effectsList = new Array();     // (array) list of Effects
  34.     this.effectsDict = new Object();    // (object) dictionary of effects keyed by name for fast lookup
  35.     this.persist = false                // (boolean) if true then anime is not automatically removed when without effects
  36.  
  37.  
  38.     this.update = function (updateTime)
  39.     {
  40.         /*
  41.         // if there are no effects on anime then remove the anime
  42.         if (this.effectsList.length === 0 && ! this.persist) {
  43.             // remove();
  44.         }
  45.         */
  46.        
  47.         // iterate through effects
  48.         for (var i = 0; i < this.effectsList.length; ++i) {
  49.             this.effectsList[i].resolve(updateTime);
  50.         }
  51.     }
  52.    
  53.     //
  54.     // helper methods for addFX()
  55.    
  56.     this.addEffect = function(effect, funct, args, obj) {
  57.         return this.addFX({effect:effect, name:Anime.REPLACE, funct:funct, args:args, obj:obj});
  58.     }
  59.    
  60.     this.nameEffect = function(effect, name, funct, args, obj) {
  61.         if (! name) {
  62.             name = Anime.AUTO_RENAME;
  63.         }
  64.         return this.addFX({effect:effect, name:name, funct:funct, args:args, obj:obj});
  65.     }
  66.    
  67.     // UPDATE TO HOW THIS IS DEATH WITH UNDER Anime.as
  68.     this.addFX = function(objParams)
  69.     {
  70.         // collect parameters
  71.         var effect = objParams.effect;      // (BaseFx) effect being added to this Anime
  72.         var name    = objParams.name;       // (string) name to add effect as
  73.         var funct   = objParams.funct;      // (function) function callback to call when effect finishes
  74.         var args    = objParams.args;       // (array) arguments to pass to function callback
  75.         var obj     = objParams.obj;        // (object) object to pass function callback to.  NEEDED?
  76.        
  77.         // rename effect before adding it to the anime list
  78.         //  Note: renaming efects allows you to stack effects of the same type
  79.         if (name &&
  80.             name !== Anime.REPLACE &&
  81.             name !== Anime.AUTO_RENAME) {
  82.             effect.name = name;
  83.         }
  84.        
  85.         // if effect with this name already exists then...
  86.         var existingEffect = this.getEffect(effect.name, false);
  87.         if (existingEffect) {
  88.             if (name === Anime.REPLACE) {
  89.                 LogAs.debug("Effect \"" + effect.name + "\" being replaced on anime " + this.target, Animatem.DEBUG_EFFECT_BEING_REPLACED);
  90.                
  91.                 // ...remove existing effect so no conflict occurs
  92.                 this.removeEffect(existingEffect);
  93.                
  94.             } else if (name === Anime.AUTO_RENAME) {
  95.                
  96.                 // ...rename effect so no conflict occurs
  97.                 var counter = 1;
  98.                 var newName;
  99.                 do {
  100.                     ++counter;
  101.                     newName = effect.name + "_" + counter;
  102.                 } while (this.getEffect(newName, false));
  103.                 effect.name = newName;
  104.                
  105.             } else {
  106.                
  107.                 // can't replace existing effect
  108.                 throw new Error("Effect \"" + effect.name + "\" already exists on anime.\r");
  109.  
  110.                 return;
  111.             }
  112.         }
  113.        
  114.         this.effectsList.push(effect);
  115.         this.effectsDict[effect.name] = effect;
  116.         effect.initialize(this.animatem, this, this.target, new Date().getTime());
  117.        
  118.         if (funct) {
  119.             $(effect).bind(BaseFx.EFFECT_DONE_EVENT, function() {
  120.                 funct.apply(obj, args)
  121.             });
  122.         }
  123.        
  124.         return effect;
  125.        
  126.     }
  127.    
  128.     //
  129.     // get effect from effects list by name
  130.     this.getEffect = function(effectName,           // (string) name of effect being searched for
  131.                               giveWarning)          // (boolean) if true warning given if effect not found
  132.     {
  133.         var effect = this.effectsDict[effectName];
  134.         if (effect) {
  135.             return effect;
  136.         } else if (giveWarning) {
  137.             LogAs.warning("Could not find effect \"" + effectName + "\" on " + this.target + ".\r" +
  138.                 "Effects that were found were the following: " + dumpEffects() + ".", Animatem.WARNING_CANT_FIND_EFFECT);
  139.         }
  140.        
  141.         return;
  142.     }
  143.    
  144.    
  145.     //
  146.     // remove effect from effects list
  147.     this.removeEffect = function(effect,            // (BaseFX) effect being removed
  148.                                  giveWarning)       // (boolean) if true warning given if effect not found
  149.     {  
  150.         if (effect === this.effectsDict[effect.name]) {
  151.            
  152.             // have the effect cleanup anything it added
  153.             effect.cleanup();
  154.            
  155.             // Note: cleanup() can change the effects list order
  156.             intIndex = this.effectsList.indexOf(effect);
  157.            
  158.             // delete from effects array and dictionary
  159.             this.effectsList.splice(intIndex, 1);
  160.             delete this.effectsDict[effect.name];
  161.            
  162.             return effect;
  163.            
  164.         } else if (giveWarning) {
  165.            
  166.             // otherwise give warning
  167.             LogAs.warning("Could not remove effect \"" + effect.name + "\" on " + this.target + ".\r" +
  168.                     "Effects that were found were the following: " + dumpEffects() + ".", Animatem.WARNING_CANT_REMOVE_EFFECT);
  169.         }
  170.     }
  171.    
  172.     this.dumpEffects = function()
  173.     {  
  174.         var nEffects = this.effectsList.length;
  175.         if (nEffects == 0) {
  176.             return "[ none ]";
  177.         }
  178.            
  179.         // iterate through effects
  180.         var str = "";
  181.         for (var i = 0; i < nEffects; ++i) {
  182.                
  183.             if (i == 0) { str += "[ "; }
  184.             str += this.effectsList[i];
  185.             str += (i == (nEffects - 1)) ? " ]" : ", ";
  186.         }
  187.            
  188.         return str;
  189.     }
  190. }
  191.  
  192. // constants used when setting effect name
  193. Anime.REPLACE = "replace";
  194. Anime.AUTO_RENAME = "rename";

Raw Paste


Login or Register to edit or fork this paste. It's free.