1
var scaleBody = function (container, mobileWidth) {
2
// height and width of the page-container element; note that these
3
// should be set in CSS so we can do measure once here
4
var computedStyles = window.getComputedStyle(container);
5
var containerWidth = parseInt(computedStyles.width.replace('px', ''), 10);
6
var containerHeight = parseInt(computedStyles.height.replace('px', ''), 10);
7
var containerStyle = container.style;
8
containerStyle.position = 'absolute';
9
containerStyle['-webkit-transform-origin'] = '0 0 0';
10
containerStyle.transform = '0 0 0';
11
containerStyle.display = 'none';
13
// resize page so that it fills it either horizontally or vertically
14
var scaleLandscape = function () {
15
// available height and width
16
var availableWidth = document.documentElement.clientWidth;
17
var availableHeight = document.documentElement.clientHeight;
19
// work out ratio of available height to container height,
20
// and the same for width
21
var scaleWidth = availableWidth / containerWidth;
22
var scaleHeight = availableHeight / containerHeight;
24
// use a single scaling value for both width and height:
25
// whichever is smaller, vertical or horizontal scale
26
var scaleBoth = scaleWidth;
27
if (scaleHeight < scaleWidth) {
28
scaleBoth = scaleHeight;
31
var left = (availableWidth - (containerWidth * scaleBoth)) / 2;
32
left = parseInt(left * (1 / scaleBoth), 10);
34
var top = (availableHeight - (containerHeight * scaleBoth)) / 2;
35
top = parseInt(top * (1 / scaleBoth), 10);
37
var scaleTransform = 'scale(' + scaleBoth + ',' + scaleBoth + ')';
38
var translateTransform = 'translate(' + left + 'px, ' + top + 'px)';
40
containerStyle['-webkit-transform'] = scaleTransform + ' ' +
42
containerStyle.transform = scaleTransform + ' ' +
46
// manually apply a pseudo landscape orientation where the client
47
// width is smaller than its height
48
var scaleWithPseudoOrientation = function () {
49
// we rotate by 90deg around the top left corner
50
var rotateTransform = 'rotate(90deg)';
52
// figure out the available height and width
53
var availableWidth = document.documentElement.clientWidth;
54
var availableHeight = document.documentElement.clientHeight;
56
// we have to fit the container's width into the client height
57
// and container's height into its width, as it's rotated
58
// work out ratio of available height to container height,
59
// and the same for width
60
var scaleWidth = availableWidth / containerHeight;
61
var scaleHeight = availableHeight / containerWidth;
63
// use a single scaling value for both width and height:
64
// whichever is smaller, vertical or horizontal scale
65
var scaleBoth = scaleWidth;
66
if (scaleHeight < scaleWidth) {
67
scaleBoth = scaleHeight;
70
var scaleTransform = 'scale(' + scaleBoth + ',' + scaleBoth + ')';
72
// now we translate to centre the container in the client
73
var left = (availableWidth - (containerHeight * scaleBoth)) / 2;
74
left = parseInt(left * (1 / scaleBoth), 10) + containerHeight;
76
var top = (availableHeight - (containerWidth * scaleBoth)) / 2;
77
top = parseInt(top * (1 / scaleBoth), 10);
79
var translateTransform = 'translate(' + left + 'px, ' + top + 'px)';
81
containerStyle['-webkit-transform'] = scaleTransform + ' ' +
82
translateTransform + ' ' +
84
containerStyle.transform = scaleTransform + ' ' +
85
translateTransform + ' ' +
90
var scale = function () {
91
if ('lockOrientation' in screen) {
92
// we are locked to landscape already
96
var doc = document.documentElement;
98
// if the window is small, assume we're on mobile
99
var onMobile = (doc.clientWidth <= mobileWidth);
101
if (doc.clientWidth < doc.clientHeight && onMobile) {
102
// we haven't locked to landscape orientation and the window is
103
// taller than it is wide, so we apply a pseudo-landscape transform
104
scaleWithPseudoOrientation();
112
var nextTickScale = function () {
113
setTimeout(scale, 0);
116
// lock orientation if possible
117
if (screen.lockOrientation) {
118
screen.lockOrientation('landscape');
122
window.onresize = nextTickScale;
123
window.addEventListener('orientationchange', nextTickScale);
128
// apply borders to the page container once it has had its first resize
129
setTimeout(function () {
130
containerStyle.display = 'block';