Skip to main content

Using HTML5's FullScreen API


I've been looking into ways in which a web application can be made more user friendly and HTML5 does include some powerful features such as PageVisibility, Navigation Timing, etc,. that can be used to provide good user experience. Today I was looking into FullScreen API that allows you show any of the elements in the DOM in Full-Screen mode. Suppose you have a HTML5 video embedded in a page and would like to play that video in full-screen mode then it is now possible with the FullScreen API. Similarly say if you have an image element, it can also be shown in full screen mode.

To present a element in the full-screen mode you have to call the method requestFullScreen() on that element. I've created a demo that shows a picture and when you click on a button it shows it in the full-screen mode and then changes the picture every three seconds creating a slideshow like experience.

Here's the code:

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> //list of images for the slideshow var imageList = ["Image3.jpg", "Image4.jpg", "Image5.jpg"]; var count = 0; //invoke this when user wants to see a Full Screen Slideshow function enterFullScreen(){ var elem = document.getElementById("slideshow"); //show full screen elem.webkitRequestFullScreen(); setTimeout(changePicture, 3000); } function changePicture(){ count++; //cycle through the list of images document.getElementById("image1").src = imageList[count % imageList.length]; if (document.webkitIsFullScreen) setTimeout(changePicture, 3000); else //if user exits the full screen mode document.getElementById("image1").src = imageList[0]; } </script> </head> <body> <div id="slideshow"> <img id="image1" src="Image3.jpg"><img> </div> <br> <input type="button" value="Enter full screen mode" onclick=enterFullScreen()> </body> </html>

When you click the button - 'Enter full screen mode' it calls the user defined method - enterFullScreen. In this method a call is made to the method webkitRequestFullScreen on the element that we want to show in full-screen mode. I'm using Google chrome and hence the method requestFullScreen with a prefix 'webkit'. On firefox it would be 'moz'. A slideshow like effect is created by looping through the images in the function changePicture.

To exit from the full-screen mode you can press the 'Esc' key. In the changePicture method I'm checking whether the full-screen mode is still activated by referring to the variable webkitIsFullScreen defined in the document object. It returns true if full-screen mode is activated and false otherwise. 


Comments

  1. Hei thanks for the code. I searched many sites, at last I got your web link. check here for future use HTML5 Slider and HTML5 Slideshow

    ReplyDelete
  2. For requesting method in IE, I have used msRequestFullScreen.......But it is not working...What is the solution???

    ReplyDelete
  3. For IE i have used msRequestFullScreen....but it is not working...What is the solution???

    ReplyDelete
  4. FullScreen API is supported in Chrome, Safari and Firefox. Unfortunately it is not supported in IE.

    ReplyDelete

Post a Comment

Popular posts from this blog

How to use the APP_INITIALIZER token to hook into the Angular bootstrap process

I've been building applications using Angular as a framework of choice for more than a year and this post is not about another React vs Angular or the quirks of each framework. Honestly, I like Angular and every day I discover something new which makes development easier and makes me look like a guy who built something very complex in a matter of hours which would've taken a long time to put the correct architecture in place if I had chosen a different framework. The first thing that I learned in Angular is the use of the APP_INITIALIZER token.

On GraphQL and building an application using React Apollo

When I visualize building an application, I would think of using React and Redux on the front-end which talks to a set of RESTful services built with Node and Hapi (or Express). However, over a period of time, I've realized that this approach does not scale well when you add new features to the front-end. For example, consider a page that displays user information along with courses that a user has enrolled in. At a later point, you decide to add a section that displays popular book titles that one can view and purchase. If every entity is considered as a microservice then to get data from three different microservices would require three http  requests to be sent by the front-end app. The performance of the app would degrade with the increase in the number of http requests. I read about GraphQL and knew that it is an ideal way of building an app and I need not look forward to anything else. The GraphQL layer can be viewed as a facade which sits on top of your RESTful services o...

Using MobX to manage application state in a React application

I have been writing applications using React and Redux for quite some time now and thought of trying other state management solutions out there. It's not that I have faced any issues with Redux; however, I wanted to explore other approaches to state management. I recently came across MobX  and thought of giving it a try. The library uses the premise of  `Observables` to tie the application state with the view layer (React). It's also an implementation of the Flux pattern wherein it uses multiple stores to save the application state; each store referring to a particular entity. Redux, on the other hand, uses a single store with top-level state variables referring to various entities.