Do you really need to rotate here?

On all devices I've seen, the list of preview sizes is reported in the form WxH, where W is always greater than H.

The original code here depended on the fact that the camera app is locked in portrait mode where H is greater than W. So that is why it swapped width and height before comparing preview sizes to the screen size.

The way you are rotating the screen size here would only work if the camera hardware reports preview sizes in a way that depends on sensor angle.  Is that true?  I don't have any documentation about sensorAngle, so I don't know how it works.  Are there any devices that report preview sizes where the width is smaller than the height?  If so, then I suppose this code is correct. Otherwise, I think it is wrong.

Actually, it seems to me that the original code would be better if it did not depend on the camera app being locked in portrait mode. Instead, it could set var screenWidth = Math.max(window.innerWidth, wndow.innerHeight), and use Math.min() for screenHeight.