2
* Copyright 2010, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are
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
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.
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.
34
* @fileoverview This file contains functions every webgl program will need
35
* a version of one way or another.
37
* Instead of setting up a context manually it is recommended to
38
* use. This will check for success or failure. On failure it
39
* will attempt to present an approriate message to the user.
41
* gl = WebGLUtils.setupWebGL(canvas);
43
* For animated WebGL apps use of setTimeout or setInterval are
44
* discouraged. It is recommended you structure your rendering
48
* window.requestAnimFrame(render, canvas);
55
* This will call your rendering function up to the refresh rate
56
* of your display but will stop rendering if your app is not
60
WebGLUtils = function() {
63
* Creates the HTLM for a failure message
64
* @param {string} canvasContainerId id of container of th
66
* @return {string} The html.
68
var makeFailHTML = function(msg) {
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>' +
79
* Mesasge for getting a webgl browser
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>';
87
* Mesasge for need better hardware
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>';
95
* Creates a webgl context. If creation fails it will
96
* change the contents of the container of the <canvas>
97
* tag to an error message with the correct links for WebGL.
98
* @param {Element} canvas. The canvas element to create a
100
* @param {WebGLContextCreationAttirbutes} opt_attribs Any
101
* creation attributes you want to pass in.
102
* @param {function:(msg)} opt_onError An function to call
103
* if there is an error during creation.
104
* @return {WebGLRenderingContext} The created context.
106
var setupWebGL = function(canvas, opt_attribs, opt_onError) {
107
function handleCreationError(msg) {
108
var container = canvas.parentNode;
110
var str = window.WebGLRenderingContext ?
114
str += "<br/><br/>Status: " + msg;
116
container.innerHTML = makeFailHTML(str);
120
opt_onError = opt_onError || handleCreationError;
122
if (canvas.addEventListener) {
123
canvas.addEventListener("webglcontextcreationerror", function(event) {
124
opt_onError(event.statusMessage);
127
var context = create3DContext(canvas, opt_attribs);
129
if (!window.WebGLRenderingContext) {
137
* Creates a webgl context.
138
* @param {!Canvas} canvas The canvas tag to get context
139
* from. If one is not passed in one will be created.
140
* @return {!WebGLContext} The created context.
142
var create3DContext = function(canvas, opt_attribs) {
143
var names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
145
for (var ii = 0; ii < names.length; ++ii) {
147
context = canvas.getContext(names[ii], opt_attribs);
157
create3DContext: create3DContext,
158
setupWebGL: setupWebGL
163
* Provides requestAnimationFrame in a cross browser way.
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);
b'\\ No newline at end of file'