Fullscreen() choice of return values

I was playing with fullscreen() in p5.js and the prototypical way of toggling state in the reference:

let fs = fullscreen();
fullscreen(!fs);

The values returned for fs depend on the state:

  • Windowed mode… undefined
  • Full screen mode… … that is, the entire html page and contents

Applying !fs, in the first case we get true, and in the second, false.

Question 1: Why the seemingly odd values of fs? Why not use booleans instead?

Question 2: given that !fs always maps to true or false, could I use:
fullscreen(true) to enter full screen mode
fullscreen(false) to leave full screen mode

Thanks!

Indeed fullscreen() got a couple of poor implementation oversights:

This is my take on refactoring it:

p5.prototype.fullscreen = function(val) {
  p5._validateParameters('fullscreen', arguments);

  if (!arguments.length) // no arguments; return fullscreen or not
    return !!( // boolean type coercion
      document.fullscreenElement ||
      document.webkitFullscreenElement ||
      document.mozFullScreenElement ||
      document.msFullscreenElement
    );

  // otherwise set to fullscreen or not
  val? launchFullscreen(document.documentElement) : exitFullscreen();

  return this; // make it call-chainable
};

Absolutely! You could also toggle it like this: fullscreen(!fullscreen());

1 Like

Thanks @GoToLoop for the reply. Makes perfect sense.

One additional question though… entering full screen mode has to be done via a user-initiated action (e.g. clicking a button) and not programmatically, i.e. I cannot simply write fullscreen(true) inside draw() and expect that it will work. What’s the reason for this browser imposed limitation?

There are some browser features (sound, alerts, fullscreen, camera, microphone) that can be abused if it’s done w/o the user’s permission.

That’s why fullscreen is requested and can be denied:

Aha… Now I get it. As they say in Spain: “A la cama no te irás, sin saber una cosa más” equivalent to “you have to learn something new every day”… and I just did! :sunglasses: