SpriteSheet in Dart, just cause.

For no particular reason, here is a very simple sprite sheet in Dart.

#import('dart:html');

class DartSpriteSheet {
  var img;
  var canvas;
  var numFrames;
  var curFrame = 0;
  var frameSize;
  
  // size assumes its a perfect rect. Awesome!
  DartSpriteSheet(num, size, fileName) {
    numFrames = num;
    frameSize = size;
    img = new Element.tag("img");
    img.src = fileName;
    
    var c = new Element.tag("canvas");
    canvas = c.getContext("2d");
    document.body.elements.add(c);
  }
  
  nextFrame() {
    curFrame = ++curFrame % numFrames;
    var sx = curFrame * frameSize;
    canvas.clearRect(0,0,frameSize,frameSize);
    canvas.drawImage(img, sx,0,frameSize,frameSize,0,0,frameSize,frameSize);
  }
}

void main() {
  var s = new DartSpriteSheet(8, 24, "sheet.png");
  window.setInterval(s.nextFrame, 100);
}

Fun!

Tagged:

web-developer, programmer, game-dev, code, programming, games, open source

You can contact me on Twitter.

Recent & Popular Articles

Browse All >