1 define([ 'util/ensureCallback', 'features', 'Modernizr', 'sprites/container' ], function (ensureCallback, features, Modernizr, container) {
2 function RenderContext(sourceData, frameData) {
3 if (!Modernizr.csstransforms) {
7 this.sourceData = sourceData;
9 this.transformData = frameData.map(function (objectTransforms) {
10 return objectTransforms.map(function (t) {
11 return t.cssTransform2d;
15 this.elements = frameData[0].map(function () {
16 var el = document.createElement('div');
17 el.style.position = 'absolute';
20 el.style.display = 'block';
21 el.style[features.transformOriginStyleProperty] = '0 0';
25 this.containerElement = container();
28 RenderContext.prototype.load = function load(callback) {
29 callback = ensureCallback(callback);
31 if (!Modernizr.csstransforms) {
32 callback(new Error('Not supported'));
36 this.elements.forEach(function (element) {
37 this.containerElement.appendChild(element);
40 document.body.appendChild(this.containerElement);
45 RenderContext.prototype.unload = function unload() {
46 this.containerElement.parentNode.removeChild(this.containerElement);
48 this.elements.forEach(function (element) {
49 if (element.parentNode) {
50 element.parentNode.removeChild(element);
55 var transformStyleProperty = features.transformStyleProperty;
57 RenderContext.prototype.renderFrame = function renderFrame(frameIndex) {
58 var transforms = this.transformData[frameIndex];
59 var elements = this.elements;
60 var count = elements.length;
62 var frameInfo = this.sourceData.getFrameInfo(frameIndex);
65 for (i = 0; i < count; ++i) {
66 var element = elements[i];
68 var backgroundImage = 'url(' + frameInfo.sheetImage.src + ')';
69 if (element.style.backgroundImage !== backgroundImage) {
70 // Chrome has flickering issues without this check
71 element.style.backgroundImage = backgroundImage;
74 element.style[transformStyleProperty] = transforms[i];
75 element.style.backgroundPosition = -frameInfo.x + 'px ' + -frameInfo.y + 'px';
76 element.style.width = frameInfo.width + 'px';
77 element.style.height = frameInfo.height + 'px';
81 return function (sourceData, frameData) {
82 return new RenderContext(sourceData, frameData);