[Solved] Lock orientation in browser

You cannot block orientation sadly. But you can block your game on unwanted orientation change. Example:
Create a scene “game”.
Create a scene “portrait”
Adapt the portrait scene to show a Sprite asking for switching again to landscape
Use once the following block:

if (window.orientation == 90 || window.orientation == -90) {
    gdjs.landscape = true //set default
} else {
    gdjs.landscape = false //set default
}
window.addEventListener("orientationchange", function() {
    if (window.orientation == 90 || window.orientation == -90) {
        gdjs.landscape = true; // landscape mode
    } else {
        gdjs.landscape = false; // portrait mode
    }
});

Then in your game scene add

if (!gdjs.landscape) {
    runtimeScene.requestChange(gdjs.RuntimeScene.PUSH_SCENE, "portrait");
}

And invert it for the portrait scene:

if (gdjs.landscape) {
    runtimeScene.requestChange(gdjs.RuntimeScene.POP_SCENE);
}

And there you go! On landscape, it shows the game, and if the player would switch it would also switch to the blocking scene.

3 Likes