Advertisement
Guest User

Untitled

a guest
May 8th, 2014
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = function () {
  2.  
  3.     var stopped = false;
  4.     var rotDeg = 0;
  5.     var rot = 0;
  6.     var dir = 1;
  7.  
  8.     var imgW = 32;
  9.     var imgH = 32;
  10.  
  11.     var frame = 0;
  12.     var maxFrame = 3;
  13.     var frameDir = 1;
  14.    
  15.     var fps = 5;
  16.  
  17.     //Game Canvas. 512x512
  18.     var gc = document.getElementById("gameCanvas");
  19.     console.log (gc);
  20.     var gctx = gc.getContext("2d");
  21.  
  22.     //This is the key ingredient for hard-pixel scaling without any interpolation
  23.     gctx.imageSmoothingEnabled = false;
  24.     gctx.webkitImageSmoothingEnabled = false;
  25.     gctx.mozImageSmoothingEnabled = false;
  26.  
  27.     //The non-scaled 32x32 canvas
  28.     var c = document.getElementById("smallCanvas");
  29.     console.log (c);
  30.     var ctx = c.getContext("2d");
  31.    
  32.  
  33.     //Load a spritesheet
  34.     var imageObj = new Image();
  35.  
  36.     imageObj.onload = function() {
  37.         gameIntervalID = setInterval (gameLoop, 1000/ fps);
  38.     };
  39.     imageObj.src = 'LowRezDerpSheet.png';
  40.  
  41.  
  42.     function gameLoop () {
  43.         console.log ("gameloop");
  44.        
  45.         updateImage ();
  46.  
  47.         if (stopped) {
  48.             clearInterval (gameIntervalID);
  49.         }
  50.     }
  51.  
  52.     function updateImage () {
  53.  
  54.         //Clear the canvases.
  55.         c.width = c.width;
  56.         gctx.clearRect (0, 0, 512, 512);
  57.  
  58.         //center the drawing on 1/2 sprite width
  59.         ctx.translate (16, 16);
  60.  
  61.         //Draw a frame of the animation
  62.         ctx.drawImage(imageObj,frame*imgW, 0, 32, 32,-16, -16, 32, 32);
  63.         gctx.drawImage (c, 0, 0, 512, 512);
  64.  
  65.         //Tick through frames, then go in reverse.
  66.         frame += frameDir;
  67.         if (frame >= maxFrame || frame <= 0) {
  68.             frameDir*= -1;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement