# Fullscreen() choice of return values

**URL:** <https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618>\
**Category:** Beginners\
**Created:** [April 26, 2022, 8:31pm UTC](https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618 "2022-04-26T20:31:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fabiopettinati](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fabiopettinati/32/15727_2.png) [@fabiopettinati](https://discourse.processing.org/u/fabiopettinati)\
**Post date:** [April 26, 2022, 8:31pm UTC](https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618/1 "2022-04-26T20:31:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 27, 2022, 1:52pm UTC](https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618/2 "2022-04-27T13:52:47Z")

</div>

> [@fabiopettinati](#):
>
> Question 1: Why the seemingly odd values of fs? Why not use booleans instead?

Indeed [**fullscreen()**](https://p5js.org/reference/#/p5/fullscreen) got a couple of poor implementation oversights:

> <https://github.com/processing/p5.js/blob/v1.4.1/src/core/environment.js#L530-L548>

This is my take on refactoring it:

```auto
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
};

```

> [@fabiopettinati](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![fabiopettinati](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fabiopettinati/32/15727_2.png) [@fabiopettinati](https://discourse.processing.org/u/fabiopettinati)\
**Post date:** [April 28, 2022, 5:20pm UTC](https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618/3 "2022-04-28T17:20:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 28, 2022, 5:32pm UTC](https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618/4 "2022-04-28T17:32:33Z")

</div>

> [@fabiopettinati](#):
>
> 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:

> **[Element: requestFullscreen() method - Web APIs | MDN](https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullScreen)**
>
> The Element.requestFullscreen()
> method issues an asynchronous request to make the element be displayed in fullscreen
> mode.

---

<div class="post-metadata">

**Author:** ![fabiopettinati](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fabiopettinati/32/15727_2.png) [@fabiopettinati](https://discourse.processing.org/u/fabiopettinati)\
**Post date:** [April 28, 2022, 5:53pm UTC](https://discourse.processing.org/t/fullscreen-choice-of-return-values/36618/5 "2022-04-28T17:53:58Z")

</div>

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! 🕶
