otsdaq_utilities  v2_05_02_indev
webgl-utils.js
1 /*
2  * Copyright 2010, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
60 WebGLUtils = function() {
61 
68 var makeFailHTML = function(msg) {
69  return '' +
70  '<table style="background-color: #8CE; width: 100%; height: 100%;"><tr>' +
71  '<td align="center">' +
72  '<div style="display: table-cell; vertical-align: middle;">' +
73  '<div style="">' + msg + '</div>' +
74  '</div>' +
75  '</td></tr></table>';
76 };
77 
82 var GET_A_WEBGL_BROWSER = '' +
83  'This page requires a browser that supports WebGL.<br/>' +
84  '<a href="http://get.webgl.org">Click here to upgrade your browser.</a>';
85 
90 var OTHER_PROBLEM = '' +
91  "It doesn't appear your computer can support WebGL.<br/>" +
92  '<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>';
93 
106 var setupWebGL = function(canvas, opt_attribs, opt_onError) {
107  function handleCreationError(msg) {
108  var container = canvas.parentNode;
109  if (container) {
110  var str = window.WebGLRenderingContext ?
111  OTHER_PROBLEM :
112  GET_A_WEBGL_BROWSER;
113  if (msg) {
114  str += "<br/><br/>Status: " + msg;
115  }
116  container.innerHTML = makeFailHTML(str);
117  }
118  };
119 
120  opt_onError = opt_onError || handleCreationError;
121 
122  if (canvas.addEventListener) {
123  canvas.addEventListener("webglcontextcreationerror", function(event) {
124  opt_onError(event.statusMessage);
125  }, false);
126  }
127  var context = create3DContext(canvas, opt_attribs);
128  if (!context) {
129  if (!window.WebGLRenderingContext) {
130  opt_onError("");
131  }
132  }
133  return context;
134 };
135 
142 var create3DContext = function(canvas, opt_attribs) {
143  var names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
144  var context = null;
145  for (var ii = 0; ii < names.length; ++ii) {
146  try {
147  context = canvas.getContext(names[ii], opt_attribs);
148  } catch(e) {}
149  if (context) {
150  break;
151  }
152  }
153  return context;
154 }
155 
156 return {
157  create3DContext: create3DContext,
158  setupWebGL: setupWebGL
159 };
160 }();
161 
165 window.requestAnimFrame = (function() {
166  return window.requestAnimationFrame ||
167  window.webkitRequestAnimationFrame ||
168  window.mozRequestAnimationFrame ||
169  window.oRequestAnimationFrame ||
170  window.msRequestAnimationFrame ||
171  function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
172  window.setTimeout(callback, 1000/60);
173  };
174 })();
175