I’m attempting to create an iPadOS/iOS webclip of my p5 code and I’m encountering some strange behavior on rotation with windowResized().
Rotation (and windowResize()) works fine within Safari, but as soon as I turn it into a web clip using the “Add to Home Screen” option, rotation seems to permanently change the size of the sketch into a square.
I’m using the following code to allow the Safari UI elements to be removed when users “Add to Home Screen” from the website:
In this solution, the website is simply reloaded and the new orientation is honored correctly. It doesn’t feel seamless by any means because it shuts down the existing program and reloads. It’s just not as convenient as a windowResized() style approach, but it does function.
For anyone else who may run into this, here is my approach as adapted from the link above:
Create a function called checkOrientation() which you place in the draw() loop:
function checkOrientation() {
if (window.DeviceOrientationEvent) {
window.addEventListener(
"orientationchange",
function () {
location.reload();
},
false
);
}
}
I’d still welcome any other solutions that allows data to persist in an orientation change.
Note: I did try calling resizeCanvas() instead of location.reload() and that does not work.
Hi, reviving this topic because I ran into the same issue and did not find any other posted solutions. I tried my hand at a few things too and found one trick that did the job.
In my project I had some other HTML DOM objects overlaying a p5 canvas background, and I noticed that the other DOM objects were being resized correctly, even in the mobile app mode. (For reference I basically did this https://www.youtube.com/watch?v=OIfEHD3KqCg)
Then I created an invisible DOM object with style width: 100% and height: 100%, and used a ResizeObserver to trigger a custom resize handler. In the handler I had to resize the canvas to the dimensions of the DOM object using getBoundingClientRect() (not windowWidth or windowHeight).
Hope this will be useful for anyone encountering the same issue…