1 define([ 'util/ensureCallback' ], function (ensureCallback) {
2 function DomContext(sourceData, frameData, onLoad) {
4 var onLoadExpected = 0;
5 var onLoadReady = false;
7 function checkOnLoad() {
9 if (onLoadCalled === onLoadExpected) {
16 var elementPool = [ ];
20 for (i = 0; i < frameData.length; ++i) {
21 var objectTransforms = frameData[i];
22 var frameElements = [ ];
23 var sourceElements = [ ];
25 for (j = 0; j < objectTransforms.length; ++j) {
27 var img = sourceData.getImage(i);
28 var index = sourcePool.indexOf(img);
31 // Image is available in the pool; take it
32 element = elementPool.splice(index, 1)[0];
33 sourcePool.splice(index, 1);
35 // Image not in the pool; add it
36 element = img.cloneNode(true);
37 element.style.position = 'absolute';
38 element.style.left = '0';
39 element.style.top = '0';
41 if (element instanceof Image) {
43 element.onload = function () {
47 element.src = element.src;
51 sourceElements.push(img);
52 frameElements.push(element);
55 sourcePool.push.apply(sourcePool, sourceElements);
56 elementPool.push.apply(elementPool, frameElements);
57 elements.push(frameElements);
63 this.elements = elements;
64 this.activeElements = null;
66 this.transformData = null;
69 DomContext.prototype.unload = function unload() {
70 this.activeElements.forEach(function (el) {
72 el.parentNode.removeChild(el);
76 this.activeElements = null;
79 DomContext.prototype.renderFrame = function renderFrame(frameIndex) {
80 var transforms = this.transformData[frameIndex];
81 var elements = this.elements[frameIndex];
83 this.processElements(elements, transforms);
85 // Elements no longer displayed must be removed from the DOM
86 var activeElements = this.activeElements;
88 var count = activeElements.length;
90 for (i = 0; i < count; ++i) {
91 var element = activeElements[i];
92 if (element.parentNode && elements.indexOf(element) < 0) {
93 element.parentNode.removeChild(element);
98 this.activeElements = elements;