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 document.body.appendChild(this.canvas);
20 RenderContext.prototype.unload = function unload() {
21 if (this.canvas.parentNode) {
22 this.canvas.parentNode.removeChild(this.canvas);
26 RenderContext.prototype.clear = function clear() {
27 this.canvas.width = this.canvas.width;
30 RenderContext.prototype.renderFrame = function renderFrame(frameIndex) {
31 var context = this.context;
32 var sourceData = this.sourceData;
34 var transforms = this.frameData[frameIndex];
35 var count = transforms.length;
38 // Reset view and transforms
39 context.canvas.width = context.canvas.width;
41 for (i = 0; i < count; ++i) {
42 var m = transforms[i].matrix;
43 context.setTransform(m[0], m[1], m[3], m[4], m[2], m[5]);
44 sourceData.drawToCanvas(context, 0, 0, frameIndex);
49 return function (element, frameData) {
50 return new RenderContext(element, frameData);