Advertisement
PsyOps

wtf

Oct 10th, 2015
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // -------------------- Variables -------------------- //
  2. var canvas = document.getElementById('myCanvas');
  3. var context = canvas.getContext('2d');
  4.  
  5. var requestAnimFrame = (function(){
  6.       return  window.requestAnimationFrame       ||
  7.               window.webkitRequestAnimationFrame ||
  8.               window.mozRequestAnimationFrame    ||
  9.               window.oRequestAnimationFrame      ||
  10.               window.msRequestAnimationFrame     ||
  11.               function(/* function */ callback, /* DOMElement */ element){
  12.                 window.setTimeout(callback, 1000 / fps);
  13.               };
  14. })();
  15. animate();
  16.  
  17. // FPS timer
  18. var FpsTimer = new Date().getTime();
  19. // This is request for update
  20. var fps = 100; 
  21. // Current count of fps
  22. var FpsCounter = 0;
  23. // FPS actual holds the last recorded fps
  24. var FpsActual = 0;
  25. // FPS timer
  26.  
  27. var TileSet = new Image();
  28.     TileSet.src = 'images/test.tileset.png';
  29. var TileSetLoaded = false;
  30. TileSet.onload = function(){
  31.     TileSetLoaded = true;
  32. }
  33.  
  34.  var idTimer = new Date().getTime();
  35.  var idUpdate = 500;
  36. var nextID = 2;
  37. var getNextId = function(){
  38.     if ( new Date().getTime() - idTimer >= idUpdate){
  39.         idTimer = new Date().getTime();
  40.         nextID++;
  41.         if (nextID > 189)
  42.             nextID = 1;
  43.     }
  44.     return nextID;
  45. }
  46.  
  47. var tileID = 0;
  48. var tileGetID = [];
  49. // Load tileGetID for tileid retrieval
  50. // Tiledisplay for coordinates to each tile image
  51. for ( var i = 0; i < 19 ; i++)
  52. {
  53.     tileGetID[i] = {x:0,y:0};
  54.     for ( var j = 0; j < 10 ; j++)
  55.     {
  56.         tileGetID[tileID] =  { x : (i * 16), y: (j * 16)};
  57.         tileID++;
  58.     }
  59. }
  60.    
  61. var drawTile = function(  id,  xC, yC){
  62.     context.drawImage(TileSet,tileGetID[id].x,tileGetID[id].y,16,16,xC,yC,16,16);
  63. }
  64.  
  65.  // This helps keep track of actual FPS
  66.  function updateFPS()
  67.  {
  68.     FpsCounter+=1;
  69.     var fpsTime = new Date().getTime() - FpsTimer;
  70.    
  71.     //reset counter/timer and log fps
  72.     if (fpsTime >= 1000)
  73.     {
  74.         FpsTimer = new Date().getTime();
  75.         FpsActual = FpsCounter;
  76.         FpsCounter = 0;
  77.     }
  78.  }
  79.  
  80. function animate() {
  81.     requestAnimFrame( animate );
  82.     // Clear and recenter the screen
  83.     context.clearRect(0, 0, canvas.width, canvas.height);
  84.     draw();
  85. }
  86.  
  87. function draw()
  88. {      
  89.         //updating our fps tracker
  90.         updateFPS();
  91.         context.fillText("FPS :[ "+ FpsActual +" ]",20,15);
  92.        
  93.         if(TileSetLoaded == true){
  94.             var next = getNextId();
  95.             drawTile(next,50,50);
  96.             context.fillText("Tile :[ "+ next +" ]",50,100);
  97.             context.fillText("Time :[ "+ (new Date().getTime() - idTimer) +" ]",50,115);
  98.             context.fillText("Tile :[ X:"+ tileGetID[next].x +" |Y:" + tileGetID[next].y + " ]",50,130);
  99.         }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement