dcsimg

The 7 Best HTML5 APIs for Creating Interactive Websites

By Madan Pariyar

WEBINAR:
On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


HTML5 has changed how websites are created and it has introduced new APIs that add a host of cool feature for web developers and designers. These APIs can be used to create interactive websites that were not possible earlier. It can also be used for game development and create a host of other interactive projects. With HTML5 APIs you can create animations, code test, measure performance and much more.

A few of the select website builders also uses these popular HTML5 APIs for ensuring a seamless drag-and-drop user experience.

In today's article, we will go through the seven best HTML5 APIs for creating interactive websites. These APIs will surely help you in your journey to becoming a better developer.

1. FullScreen API

The FullScreen API allows web developers to present the full screen to the user. It needs to be approved by the user before it takes over the screen. The API works by creating an element and its children so that it can occupy the full screen. This also eliminates other on-screen elements and makes it easy for a distraction-free experience.

// Calling the API on a element
function launchIntoFullscreen(element) {
  if(element.requestFullscreen) {
    element.requestFullscreen();
  } else if(element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if(element.webkitRequestFullscreen) {
    element.webkitRequestFullscreen();
  } else if(element.msRequestFullscreen) {
    element.msRequestFullscreen();
  }
}

// Launch fullscreen if supported
launchIntoFullscreen(document.documentElement); // Full page
launchIntoFullscreen(document.getElementById("videoElement")); // Individual Element

This API can be useful for anyone who is starting as a blogger as it can be utilized it to enable full-screen mode for visitors so that they can read without any extraneous content.

2. High Resolution Time API

If you are building a real-time web application using HTML, you need to use the High Resolution Timer API. This API provides accurate time down to milliseconds that doesn't depend on the system clock and its inaccuracies.

Technically, it works by returning a DOMHighResTimeStamp variable that contains the time in milliseconds. It is exposed through now(), which belongs to window.performance object. The timestamp shared by the now() method ensures that the time is highly precise.

The function can help you to perform accurate tests and understand the true performance of the code. At the time of writing, it supports all the major browsers including IE, Opera, Chrome, and Firefox. However, do check which versions of these browsers support it. You can check the compatibility here.

To use it, all you need to do is run the following line of code.

var time = performance.now();

3. getUserMedia API

If you are creating an interactive website, you need to get access to the different device media used by the consumer. For example, a camera or a webcam can then be used to identify an object or take a picture of the consumer to be used in the application.

To use it, you first need to add event listeners to your code. Once done, you need to grab settings, elements and then put video listeners into it. It can be done easily by using the following code:

// Event listeners
window.addEventListener("DOMContentLoaded", function() {
  // Grab elements, create settings, etc.
  var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    video = document.getElementById("video"),
    videoObj = { "video": true },
    errBack = function(error) {
      console.log("Video capture error: ", error.code); 
    };

  // Video Listeners
  if(navigator.getUserMedia) { // Standard browser
    navigator.getUserMedia(videoObj, function(stream) {
      video.src = stream;
      video.play();
    }, errBack);
  } else if(navigator.webkitGetUserMedia) { // WebKit-prefixed browser
    navigator.webkitGetUserMedia(videoObj, function(stream){
      video.src = window.webkitURL.createObjectURL(stream);
      video.play();
    }, errBack);
  }
}, false); 

4. Battery Status API

To ensure that the user has the optimal experience, you need to know regularly about the user's device as well. This is where the Battery Status API comes in. As you might already guess, it allows you to access the battery status of the device and fire events according to conditions that you set in your code.

With the API, you can access four properties of the battery including charging, discharingTime, chargingTime, and level. All these properties can be used in your code. Also, it shows the time required to complete a charge or discharge. The window.navigator object offers the battery property.

This API is handy considering you can change the operation of an app or website based on the battery status. For example, you can turn off some data streaming services or lower its quality if you find the battery level to be below the threshold.

To get the battery status, all you need to do is use the following line of code.

var percentageLevel = navigator.battery.level * 100; 

5. WebSocket API

WebSocket API is one of the most useful HTML5 APIs. It provides the means to established socket connection between the server and the browser. By doing so, a persistent connection is created between the server and the client making it easy for data transfer.

It provides two methods: send() and close(). The first method, send(), is used to send data to the server whereas the close() method is used for closing the connection. Other attributes are also provided that help you further refine the connection and trigger events on special conditions.

All the current browsers support the WebSocket API so that you shouldn't have any issue using it.

6. Link Prefetching

Link Prefetching is a way of preloading the website and ensuring a more fluid experience for the user. This API is useful and must be used to provide an interactive website.

<!-- full page prefetch -->
<link rel="prefetch" href="#" />

<!-- image prefetch -->
<link rel="prefetch" href="#" />

7. Page Visibility API

The Page Visibility API is useful for knowing the current status of the page. This lets you know what the user is up to and also let you control the resources used by your website. For example, if you see that your website is in the background for more than five minutes, you can put it in suspend mode, saving both energy and bandwidth.

The API offers one main event known as visibilitychange. By using it, you can code your app. It works on all the major browsers.

Conclusion

Of Course, there are many other amazing HTML5 APIs and if you think we missed one, don't forget to comment below and let us know. We are listening.

About the Author

Madan Pariyar is a blogger at WebPrecious and a digital marketing strategist helping clients to resolve their website woes. When not busy with all these things, you may find Madan occasionally watching movies, traveling and spending time with family.



Make a Comment

Loading Comments...

  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.

    By submitting your information, you agree that htmlgoodies.com may send you HTMLGOODIES offers via email, phone and text message, as well as email offers about other products and services that HTMLGOODIES believes may be of interest to you. HTMLGOODIES will process your information in accordance with the Quinstreet Privacy Policy.

  •  
  •  
  •  
Thanks for your registration, follow us on our social networks to keep up-to-date