What is the easiest to get the screen size?

Hi ,

I want to check if a touch event is on the left side or right side of the screen. What is the best way to implement it?

Thanks,

Ang

Something like this:

app.touch.on("touchstart", function (e) {
    var width = app.graphicsDevice.width;

    var touch = e.changedTouches[0];
    if (touch.x < width/2) {
        // left
    } else {
        // right
    }
});

I’m doing this, in case Dave’s answer doesn’t work out for some reason:

var width = app.graphicsDevice.canvas.clientWidth;
var height = app.graphicsDevice.canvas.clientHeight;

But, canvas size actually does not necessary represents screen resolution, especially in a case of Respected DPI on mobile (tick in project settings), or in case of non-filled canvas size.
So better to use window.innerWidth and window.innerHeight - this gives you actual window size ready for use in DOM coordinate system, also taking in account DPI.

1 Like