Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Three.js Cookbook

You're reading from   Three.js Cookbook Over 80 shortcuts, solutions, and recipes that allow you to create the most stunning visualizations and 3D scenes using the Three.js library

Arrow left icon
Product type Paperback
Published in Jan 2015
Publisher Packt
ISBN-13 9781783981182
Length 300 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jos Dirksen Jos Dirksen
Author Profile Icon Jos Dirksen
Jos Dirksen
Arrow right icon
View More author details
Toc

Table of Contents (9) Chapters Close

Preface 1. Getting Started 2. Geometries and Meshes FREE CHAPTER 3. Working with the Camera 4. Materials and Textures 5. Lights and Custom Shaders 6. Point Clouds and Postprocessing 7. Animation and Physics Index

Detecting WebGL support

Not all browsers currently support WebGL. When you create a page that uses the THREE.WebGLRenderer object, it is a good idea to make sure that the browser supports WebGL. If a browser doesn't support it, this will result in all kinds of strange JavaScript errors in the JavaScript console and an empty screen for the end user. To make sure that your WebGL projects work as expected, we'll explain how to detect WebGL support in a browser in this recipe.

Getting ready

In this recipe, as an example, we will use the 01.04-detect-webgl-support.html file, which you can find in the sources provided with this book. If you open this file, you'll see the following result if your browser doesn't support WebGL:

Getting ready

Let's take a look at the recipe to create the preceding example.

How to do it...

To detect WebGL and create the message WebGL is not-supported, we need to perform the following steps:

  1. First, we'll create the CSS for the pop up to show when WebGL isn't supported.
  2. Then, we need to detect whether the browser WebGL is supported. For this, we'll write a method that returns the values true or false.
  3. Finally, we'll use the result from the previous step to either show the pop up or just continue.

    In the following section, we'll look at these steps in detail:

  4. The first thing you need to do is set up the CSS that we'll use:
    <!DOCTYPE html>
    <html>
      <head>
        <style>
          .black_overlay {
            display: none;
            position: absolute;
            top: 0;
            left: 0%;
            width: 100%;
            height: 100%;
            background-color: black;
            z-index: 1001;
            opacity: .80;
          }
    
          .white-content {
            display: none;
            position: absolute;
            top: 25%;
            left: 25%;
            width: 50%;
            height: 70px;
            padding: 16px;
            border: 2px solid grey;
            background-color: black;
            z-index: 1002;
          }
    
          .big-message {
            width: 80%;
            height: auto;
            margin: 0 auto;
            padding: 5px;
            text-align: center;
            color: white;
    
            font-family: serif;
            font-size: 20px;
          }
    
        </style>
        <title></title>
      </head>
      <body>

    As you can see, there is nothing special in this CSS. The only thing that we will do here is create a number of classes that we'll use to create a pop-up message and hide the background. Next, we will define the HTML that is used to create the pop ups.

  5. The following snippet shows you the HTML code, which will contain the message. Using the CSS that we previously defined we can show or hide this element:
        <!-- Lightbox to show when WebGL is supported or not-->
        <div id="lightbox" class="white-content">
        <div class="big-message" id="message">
        
        </div>
        <a href="javascript:void(0)" onclick="hideLightbox()">Close</a>
        </div>
        <div id="fade" class="black_overlay"></div>

    As you can see, we just create a number of div elements that are currently hidden. When we detect that WebGL isn't supported this will be shown by the two div elements by changing their visibility.

  6. Next, let's take a look at the JavaScript you need to add to detect WebGL. We'll create the following function for it:
        // loosely based on the http://get.webgl.org function detectWebGL() {
    
          // first create a canvas element
          var testCanvas = document.createElement("canvas");
          // and from that canvas get the webgl context
          var gl = null;
    
          // if exceptions are thrown, indicates webgl is null
          try {
            gl = testCanvas.getContext("webgl");
          } catch (x) {
            gl = null;
          }
    
          // if still null try experimental
          if (gl == null) {
            try {
            gl = testCanvas.getContext("experimental-webgl");
            } catch (x) {
            gl = null;
            }
    
        }
        // if webgl is all good return true;
        if (gl) {
          return true;
        } else {
          return false;
        }
    }

    As you can see, we create an HTML canvas element and then try to create a WebGL context with the getContext function. If this fails, the gl variable is set to null but if it succeeds, the gl variable will contain the WebGL context. If the gl variable isn't null, it will return true. On the hand, if it is, it will return false.

  7. Now that we're able to detect whether a browser supports WebGL or not, we'll use this function to show a pop up. For this recipe, we'll also show you a pop up when WebGL is supported:
        var hasGl = detectWebGL();
        if (hasGl) {
          showLightbox("WebGL is supported");
        } else {
        showLightbox("WebGL is not-supported");
        }
    
        function showLightbox(message) {
          var lightBox = document.getElementById('light');
          lightBox.style.display = 'block';
    
          var fadeBox = document.getElementById('fade');
          fadeBox.style.display = 'block'
    
          var msg = document.getElementById('message');
          msg.textContent = message;
        }
    
        function hideLightbox() {
          var lightBox = document.getElementById('light');
          lightBox.style.display = 'none';
    
          var fadeBox = document.getElementById('fade');
          fadeBox.style.display = 'none'
        }

And that is it for this recipe. If we add this to a web page, a browser that supports WebGL shows a pop up with WebGL is supported, if no WebGL is available, a pop up is shown with the text WebGL isn't supported. Besides this approach, you can also use the detector object provided by Three.js at https://github.com/mrdoob/three.js/blob/master/examples/js/Detector.js. If you include this file in your JavaScript, you can detect WebGL by checking the webgl attribute of the Detector object.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image