-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathoffscreen.js
More file actions
55 lines (50 loc) · 1.98 KB
/
Copy pathoffscreen.js
File metadata and controls
55 lines (50 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright 2005 Google Inc.
// All Rights Reserved
//
// Author: Bret Taylor
// Sizes the given HTML element by drawing it offscreen and calculating the
// the actual, rendered size. We call the given callback function with the
// the size of the content element.
//
// Since we draw the content element offscreen, the content element will be
// added to a node offscreen. Therefore, it should not be in the DOM before
// this method is called.
function offscreenSize(contentElem, callback, opt_maxWidth) {
offscreenSizeMultiple([contentElem], function(sizes) {
callback(sizes[0][0], sizes[0][1]);
}, opt_maxWidth);
}
// Like offscreenSize, but sizes the entire array of HTML elements and calls
// a single callback with an array of sizes.
function offscreenSizeMultiple(contentElems, callback, opt_maxWidth) {
var offscreenArea = document.createElement("div");
offscreenArea.style.position = "absolute";
offscreenArea.style.left = -screen.width + "px";
offscreenArea.style.top = -screen.height + "px";
var maxWidth = opt_maxWidth || screen.width;
offscreenArea.style.width = maxWidth + "px";
offscreenArea.style.height = screen.height + "px";
var contentContainers = [];
for (var i = 0; i < contentElems.length; i++) {
var container = document.createElement("div");
container.style.position = "absolute";
container.style.left = "0px";
container.style.top = "0px";
offscreenArea.appendChild(container);
container.appendChild(contentElems[i]);
contentContainers.push(container);
}
document.body.appendChild(offscreenArea);
window.setTimeout(function() {
var sizes = [];
for (var i = 0; i < contentContainers.length; i++) {
var container = contentContainers[i];
sizes.push([container.offsetWidth, container.offsetHeight]);
container.removeChild(contentElems[i]);
container.parentNode.removeChild(container);
}
offscreenArea.parentNode.removeChild(offscreenArea);
contentContainers = null;
callback(sizes);
}, 0);
}