JAVASCRIPT   11

Animatem.js

Guest on 18th May 2021 04:42:47 AM

  1. ///
  2. //  Animatem v 1.0
  3. //  Russell Lowke, January 31st
  4. //
  5. //  Copyright (c)  Lowke Media
  6.  
  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. // Starting to convert my Animatem AS3 package to JavaScript
  29.  
  30. var Animatem = function()
  31. {
  32.     this.updateTime = new Date().getTime();         // (uint) time of last update
  33.     this.timePassed = 0;                            // (uint) time passed since update
  34.     this.animeList = new Array();                   // (array) list array of anime for iteration
  35.  
  36.     this.anime = function(target)
  37.     {
  38.         if (! target) {
  39.             throw new Error("anime() recieved a null target./r");
  40.         }
  41.        
  42.         // try to get the anime
  43.         var anime = this.getAnime(target);
  44.        
  45.         // if anime not found...
  46.         if (! anime) {
  47.            
  48.             // ...then create a new anime
  49.             anime = new Anime(this, target);
  50.             this.animeList.push(anime);
  51.         }
  52.        
  53.         return anime;
  54.     }
  55.    
  56.     this.getAnime = function(target)
  57.     {
  58.         var index = this.animeList.indexOf(target);
  59.         if (index !== -1) {
  60.             return this.animeList[index];
  61.         }
  62.         return;
  63.     }
  64.    
  65.     this.update = function()
  66.     {
  67.         var updateTime = new Date().getTime();
  68.         this.timePassed = updateTime - this.updateTime;
  69.         this.updateTime = updateTime;
  70.  
  71.         for (var i = 0; i < this.animeList.length; ++i) {
  72.             this.animeList[i].update(this.updateTime);
  73.         }
  74.        
  75.         // dispatchEvent(new Event(UPDATE));
  76.        
  77.     }
  78.    
  79.     this.remove = function()
  80.     {
  81.         var anime = this.getAnime(target);
  82.         if (anime) {
  83.             anime.remove();
  84.         }
  85.     }
  86.    
  87.     this.clear = function()
  88.     {
  89.         for (var i = 0; i < this.animeList.length; ++i) {
  90.             remove(this.animeList[i].target);
  91.         }
  92.     }
  93.    
  94.    
  95.     this.pause = function()
  96.     {
  97.         for (var i = 0; i < this.animeList.length; ++i) {
  98.             this.animeList[i].pause();
  99.         }
  100.     }
  101.    
  102.     this.unpause = function()
  103.     {
  104.         for (var i = 0; i < this.animeList.length; ++i) {
  105.             this.animeList[i].pause();
  106.         }
  107.     }
  108.    
  109.    
  110.     /* WORRY ABOUT toString LATER
  111.     this.dump = function dump()
  112.     {
  113.         var str = "";
  114.        
  115.         // iterate through each anime
  116.         for each (var i:Anime in _animeVect) {
  117.             str += i;
  118.         }
  119.         for (var i = 0; i < this.animeList.length; ++i) {
  120.             str += animeList[i];
  121.         }
  122.  
  123.         return str;
  124.     }
  125.    
  126.     this.toString = function () {
  127.         return dump();
  128.     }
  129.     */
  130. }
  131.  
  132.  
  133. // error, warning and ifo IDs
  134. Animatem.WARNING_CANT_FIND_EFFECT               = 3000;
  135. Animatem.WARNING_CANT_REMOVE_EFFECT             = 3001;
  136. // Animatem.WARNING_CANT_FIND_STAGE             = 3002;
  137. // Animatem.WARNING_SNAP_TO_END_NOT_SUPPORTED   = 3003;
  138. // Animatem.WARNING_CYCLE_NOT_SUPPORTED         = 3004;
  139. // Animatem.WARNING_REVERSE_NOT_SUPPORTED       = 3005;
  140. // Animatem.WARNING_PAUSE_NOT_SUPPORTED         = 3006;
  141. // Animatem.WARNING_UNPAUSE_NOT_SUPPORTED       = 3007;
  142. // Animatem.DEBUG_REMOVED_FROM_STAGE            = 3008;
  143. Animatem.DEBUG_EFFECT_BEING_REPLACED            = 3009;
  144.  
  145.        
  146. //
  147. // static helper methods
  148.  
  149. Animatem.xyToAngle = function(x, y)
  150. {  
  151.     if (x === 0) {                  // avoid error due to divide by zero.
  152.         if (y > 0) {
  153.             return 90;                  //  down
  154.         } else {
  155.             return 270;                 //  up
  156.         }
  157.        
  158.     } else {                        // quadrants
  159.  
  160.         var degrees = Animatem.radiansToDegrees(Math.atan(y/x));
  161.         if (x > 0) {
  162.             if (degrees < 0) {
  163.                 return 360 + degrees;
  164.             } else {
  165.                 return degrees;
  166.             }
  167.         } else {
  168.             return 180 + degrees;
  169.         }
  170.     }
  171. }
  172.    
  173. ///
  174. // Convert radians to degrees.
  175. // degrees = radians*180/PI
  176. //
  177. Animatem.radiansToDegrees = function(numRad) {
  178.     // precalculate 180/PI = 57.295779513082289
  179.     return numRad*57.295779513082289;
  180. }
  181.    
  182. ///
  183. // Convert degrees to radians.
  184. // radians = degrees*PI/180
  185. //
  186. Animatem.degreesToRadians = function(numDeg) {
  187.     // precalculate PI/180 = 0.01745329251994
  188.     return numDeg*0.01745329251994;
  189. }

Raw Paste


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