Search icon CANCEL
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
WebGL Beginner's Guide

You're reading from   WebGL Beginner's Guide If you're a JavaScript developer who wants to take the plunge into 3D web development, this is the perfect primer. From a basic understanding of WebGL structure to creating realistic 3D scenes, everything you need is here.

Arrow left icon
Product type Paperback
Published in Jun 2012
Publisher Packt
ISBN-13 9781849691727
Length 376 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Toc

Table of Contents (18) Chapters Close

WebGL Beginner's Guide
Credits
About the Authors
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started with WebGL FREE CHAPTER 2. Rendering Geometry 3. Lights! 4. Camera 5. Action 6. Colors, Depth Testing, and Alpha Blending 7. Textures 8. Picking 9. Putting It All Together 10. Advanced Techniques Index

Time for action – accessing the WebGL context


We are going to modify the previous example to add a JavaScript function that is going to check the WebGL availability in your browser (trying to get a handle). This function is going to be called when the page is loaded. For this, we will use the standard DOM onLoad event.

  1. Open the file ch1_Canvas.html in your favorite text editor (a text editor that highlight HTML/JavaScript syntax is ideal).

  2. Add the following code right below the </style> tag:

    <script>
    var gl = null;
    function getGLContext(){
    var canvas = document.getElementById("canvas-element-id");
       if (canvas == null){
          alert("there is no canvas on this page");
          return;
       }
    
    var names = ["webgl", 
                 "experimental-webgl", 
                 "webkit-3d", 
                 "moz-webgl"];
    
       for (var i = 0; i < names.length; ++i) {
           try {
              gl = canvas.getContext(names[i]);
           } 
           catch(e) {}
           if (gl) break;
       }
    
       if (gl == null){
          alert("WebGL is not available");
       }
       else{
          alert("Hooray! You got a WebGL context");
       }
    }
       </script>
  3. We need to call this function on the onLoad event. Modify your body tag so it looks like the following:

    <body onLoad ="getGLContext()">
  4. Save the file as ch1_GL_Context.html.

  5. Open the file ch1_GL_Context.html using one of the WebGL supported browsers.

  6. If you can run WebGL you will see a dialog similar to the following:

What just happened?

Using a JavaScript variable (gl), we obtained a reference to a WebGL context. Let's go back and check the code that allows accessing WebGL:

var names = ["webgl", 
             "experimental-webgl", 
             "webkit-3d", 
             "moz-webgl"];
   
for (var i = 0; i < names.length; ++i) {
       try {
          gl = canvas.getContext(names[i]);
       } 
       catch(e) {}
       if (gl) break;
}

The canvas getContext method gives us access to WebGL. All we need to specify a context name that currently can vary from vendor to vendor. Therefore we have grouped them in the possible context names in the names array. It is imperative to check on the WebGL specification (you will find it online) for any updates regarding the naming convention.

getContext also provides access to the HTML5 2D graphics library when using 2d as the context name. Unlike WebGL, this naming convention is standard. The HTML5 2D graphics API is completely independent from WebGL and is beyond the scope of this book.

You have been reading a chapter from
WebGL Beginner's Guide
Published in: Jun 2012
Publisher: Packt
ISBN-13: 9781849691727
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