windowWidth / windowHeight under iOS 7, 4-inch (iPhone 5 + 5S)
Hello, I'm unable to get full-window sketches created using windowWidth and windowHeight working reliably under iOS 7 on 4-inch iPhones. I've tried both on an iPhone 5S and in Apple's simulator; I assume that this is also true for the iPhone 5.
I'm testing with the most basic sketch possible:
function setup() {
createCanvas( windowWidth, windowHeight )
}
function draw() {
background( 100 )
}
Are other people experiencing this? It seems to work fine with iPads, 3.5-inch phones etc. - Charlie
thanks! will take a look
can you log the values for windowWidth, windowHeight and window.innerWidth, window.innerHeight?
from what I've read it seems like these should be correct, indicating an issue with createCanvas or elsewhere... but just want to make sure.
(jsconsole.com useful for grabbing console.log values off a mobile device)
To get it to work on iPhone or iPad you have to make sure to add meta tags to the HTML that contains the p5 canvas.... You have to add a meta tag to the html in between the <head> </head> part at the top. See below example with the meta tag that sets the viewport:
<head>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1">
<script src="//cdn.jsdelivr.net/p5.js/0.3.5/p5.min.js"></script>
<script language="javascript" src="sketch.js"></script>
<style> body {padding: 0; margin: 0} </style> <!-- this line removes any default padding -->
</head>
<body>
</body>
Sorry for the delayed response on these measurements. I had previously tried the meta tag; it only causes it to work sporadically. Typically if I refresh the page after loading it successfully, the viewport no longer is correct.
With meta tag:
windowWidth: 980
windowHeight:1409
window.innerWidth: 320
window.innerHeight:460
Without meta tag:
windowWidth:980
windowHeight:1409
window.innerWidth:980
window.innerHeight:1409
This is running in the simulator with a 4-inch screen. I didn't perform the measurements on my phone, but the problem is identical to what occurs in the simulator. Hope this helps. - Charlie
hm, I'm having trouble with this on ios8 on iphone5s as well. for me it seems not to be a problem with the variables, as it isn't displaying properly if I just put in 980,1409. will look into it further...
I believe the problem is that we're hitting max canvas size at full screen when scaling it to account for pixel density http://stackoverflow.com/questions/6081483/maximum-size-of-a-canvas-element
I'm having trouble just getting this to show up safari iphone 5s, does that make sense?
<head>
<style> body {padding: 0; margin: 0; } </style>
<script>
window.onload = function() {
var w = window.innerWidth;
var h = window.innerHeight;
var c = document.createElement('canvas');
var context = c.getContext('2d');
var devicePixelRatio = window.devicePixelRatio || 1;
var backingStoreRatio = context.webkitBackingStorePixelRatio ||
context.mozBackingStorePixelRatio ||
context.msBackingStorePixelRatio ||
context.oBackingStorePixelRatio ||
context.backingStorePixelRatio || 1;
var ratio = devicePixelRatio / backingStoreRatio;
c.width = w * ratio;
c.height = h * ratio;
c.style.width = w + 'px';
c.style.height = h + 'px';
document.body.appendChild(c);
context.scale(ratio, ratio);
context.fillStyle = '#FF0000';
context.fillRect(0, 0, w, h);
}
</script>
</head>
<body>
</body>
based on example here: http://www.html5rocks.com/en/tutorials/canvas/hidpi/
we should at least add a method to turn off auto scaling for retina displays. but should it be possible to support this with canvas, or are we just hitting the canvas size limit when we try to do this?
I just tried to get it to work on my iphone 5s (iOS8.0.2) and it just shows a white screen. The jsconsole shows no errors.
But it works on my iPad 4th generation running iOS7.1.2 it works -- shows a red screen.
yes, I believe the issue is hitting a max canvas size on iphone with the scaling to account for retina. I'm guessing ipad has a different max. however, this is confusing because it seems like a properly scaled full screen canvas should be within iphone5 size limit. one solution that I'm working on is to add a feature to p5.js that lets you turn off the auto scaling for retina. this wouldn't look as good but would keep the fullscreen canvas within the limit.
actually, checking again, this and the above native js look ok on iphone5 if you add the meta line. could someone try running this code with the most recent p5.js either on iphone5 or others?
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no">
<style> body {padding: 0; margin: 0; } </style>
<script src="p5.min.js"></script>
<script>
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
}
function draw() {
background(255, 0, 0);
}
</script>
</head>
<body>
</body>
Just tried the code with p5 version 0.3.8 and it shows properly a red screen on iPhone 5s on iOS8.0.2.
I added console.log to show that:
"windowWidth=320, windowHeight=460"
by the way... displayWidth and displayHeight are not set with the correct device orientation on iPhone or iPad. When the device is turned for LANDSCAPE mode, the displayWidth/displayHeight show as if the device were PORTRAIT. The window.innerWidth and window.innerHeight are correct. I had to add some clunky code (see below). The console log output on iPhone5s when it is LANDSCAPE is:
remote console.loglink "landscape" remote console.loglink "correctedWidth=568, correctedHeight=320, w=568, h=320, displayWidth=320, displayHeight=568, window.innerWidth=568, window.innerHeight=320"
window.onresize = function() { checkOrientation(); }; var checkOrientation = function() { var w = window.innerWidth; var h = window.innerHeight; var dw0 = displayWidth; var dh0 = displayHeight; var dw; var dh; if ( w > h ) { console.log('landscape'); // due to bug displayWidth always shows the portrait width so double check the dw0/dh0 if ( dw0 > dh0 ) { dw = dw0; dh = dh0; } else { dw = dh0; dh = dw0; } } else { console.log('portrait'); if ( dw0 < dh0 ) { dw = dw0; dh = dh0; } else { dw = dh0; dh = dw0; } } console.log('correctedWidth='+dw+', correctedHeight='+dh+', w='+w+', h='+h+', displayWidth='+dw0+", displayHeight="+dh0+", window.innerWidth="+window.innerWidth+", window.innerHeight="+window.innerHeight); return {width:dw,height:dh}; };
function setup() { var displaySize = checkOrientation(); createCanvas(displaySize.width, displaySize.height); }