Skip to content

Commit 5ad6f5c

Browse files
committed
built
1 parent 15d931b commit 5ad6f5c

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

build/nodegame-window.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* # GameWindow
3-
* Copyright(c) 2019 Stefano Balietti <ste@nodegame.org>
3+
* Copyright(c) 2020 Stefano Balietti <ste@nodegame.org>
44
* MIT Licensed
55
*
66
* API to interface nodeGame with the browser window
@@ -2186,9 +2186,8 @@
21862186

21872187
func();
21882188

2189-
// Important. We need a timeout (2nd param), because some changes
2190-
// might take time to be reflected in the DOM.
2191-
W.adjustFrameHeight(0, 120);
2189+
adjustFrameHeightAfterLoad();
2190+
21922191
};
21932192

21942193
if (loadCache) reloadScripts(iframe, afterScripts);
@@ -2413,6 +2412,46 @@
24132412
}
24142413
}
24152414

2415+
/**
2416+
* ### adjustFrameHeightAfterLoad
2417+
*
2418+
* Adjusts the frame height after frame is loaded
2419+
*
2420+
* It waits 120ms or until all images are loaded.
2421+
*
2422+
* @see handleFrameLoad
2423+
*/
2424+
function adjustFrameHeightAfterLoad() {
2425+
2426+
// Kudos:
2427+
// https://stackoverflow.com/questions/11071314/
2428+
// javascript-execute-after-all-images-have-loaded
2429+
2430+
var doc, imgs, len, counter, increment;
2431+
doc = W.getFrameDocument();
2432+
imgs = doc.images;
2433+
len = imgs.length;
2434+
2435+
// If there are no images, we wait a fixed 120 milliseconds.
2436+
if (!len) {
2437+
// Important. We need a timeout (2nd param), because some changes
2438+
// might take time to be reflected in the DOM.
2439+
W.adjustFrameHeight(0, 120);
2440+
return;
2441+
}
2442+
2443+
// Else we wait until all images are loaded.
2444+
counter = 0;
2445+
increment = function() {
2446+
if (++counter === len) W.adjustFrameHeight();
2447+
};
2448+
2449+
[].forEach.call(imgs, function(img) {
2450+
if (img.complete) increment();
2451+
else img.addEventListener('load', increment, false);
2452+
});
2453+
}
2454+
24162455
//Expose GameWindow prototype to the global object.
24172456
node.GameWindow = GameWindow;
24182457

0 commit comments

Comments
 (0)