1 define([ 'util/ensureCallback' ], function (ensureCallback) {
2 var IMAGE_SRC = 'assets/monstro-fada.png';
4 var FRAME_WIDTH = 37 * 3;
5 var FRAME_HEIGHT = 58 * 3;
10 var TOTAL_FRAMES = FRAMES_HORIZ * FRAMES_VERT;
12 function ImageSource(img) {
15 var canvas = document.createElement('canvas');
16 canvas.width = FRAME_WIDTH;
17 canvas.height = FRAME_HEIGHT;
19 var context = canvas.getContext('2d');
20 context.globalCompositeOperation = 'copy';
22 this.frameImages = [ ];
23 this.frameInfos = [ ];
25 for (y = 0; y < FRAMES_VERT; ++y) {
26 for (x = 0; x < FRAMES_HORIZ; ++x) {
27 var px = x * FRAME_WIDTH;
28 var py = y * FRAME_WIDTH;
33 FRAME_WIDTH, FRAME_HEIGHT,
35 FRAME_WIDTH, FRAME_HEIGHT
38 var frameImage = new window.Image();
39 frameImage.src = canvas.toDataURL();
41 this.frameInfos.push({
52 // TODO Cycle frameInfos
55 ImageSource.prototype.getImage = function getImage(frameIndex) {
56 return this.frameInfos[frameIndex % TOTAL_FRAMES].image;
59 ImageSource.prototype.drawToCanvas = function drawToCanvas(context, dx, dy, frameIndex) {
60 var frameInfo = this.frameInfos[frameIndex % TOTAL_FRAMES];
64 frameInfo.x, frameInfo.y,
65 frameInfo.width, frameInfo.height,
67 frameInfo.width, frameInfo.height
71 ImageSource.prototype.getFrameInfo = function getFrameInfo(frameIndex) {
72 return this.frameInfos[frameIndex % TOTAL_FRAMES];
75 return function spriteSheet(callback) {
76 callback = ensureCallback(callback);
78 var img = new window.Image();
79 img.onload = function () {
80 var imageSource = new ImageSource(img);
81 callback(null, imageSource);