1 define([ 'util/ensureCallback', 'sprites/canvas' ], function (ensureCallback, canvas) {
2 function RenderContext(sourceData, frameData) {
3 this.sourceData = sourceData;
4 this.frameData = frameData;
6 this.canvas = canvas();
8 this.context = this.canvas.getContext('2d');
9 this.context.globalCompositeOperation = 'source-over';
12 RenderContext.prototype.load = function load(callback) {
13 callback = ensureCallback(callback);
15 // Anything not simply translated is disallowed
16 var isValid = this.frameData.every(function (frame) {
17 return frame.every(function (transform) {
18 return transform.matrix[0] === 1
19 && transform.matrix[1] === 0
20 && transform.matrix[3] === 0
21 && transform.matrix[4] === 1;
26 callback(new Error("Not supported"));
30 document.body.appendChild(this.canvas);
35 RenderContext.prototype.unload = function unload() {
36 if (this.canvas.parentNode) {
37 this.canvas.parentNode.removeChild(this.canvas);
41 RenderContext.prototype.clear = function clear() {
42 this.canvas.width = this.canvas.width;
45 RenderContext.prototype.renderFrame = function renderFrame(frameIndex) {
46 var context = this.context;
47 var sourceData = this.sourceData;
49 var transforms = this.frameData[frameIndex];
50 var count = transforms.length;
53 // Reset view and transforms
54 context.canvas.width = context.canvas.width;
56 for (i = 0; i < count; ++i) {
57 var transform = transforms[i];
58 context.setTransform(1, 0, 0, 1, Math.floor(transform.x), Math.floor(transform.y));
59 sourceData.drawToCanvas(context, 0, 0, frameIndex);
63 return function (element, frameData) {
64 return new RenderContext(element, frameData);