webgl-utils.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * @fileoverview This file contains functions every webgl program will need
  33. * a version of one way or another.
  34. *
  35. * Instead of setting up a context manually it is recommended to
  36. * use. This will check for success or failure. On failure it
  37. * will attempt to present an approriate message to the user.
  38. *
  39. * gl = WebGLUtils.setupWebGL(canvas);
  40. *
  41. * For animated WebGL apps use of setTimeout or setInterval are
  42. * discouraged. It is recommended you structure your rendering
  43. * loop like this.
  44. *
  45. * function render() {
  46. * window.requestAnimFrame(render, canvas);
  47. *
  48. * // do rendering
  49. * ...
  50. * }
  51. * render();
  52. *
  53. * This will call your rendering function up to the refresh rate
  54. * of your display but will stop rendering if your app is not
  55. * visible.
  56. */
  57. WebGLUtils = function() {
  58. /**
  59. * Creates the HTLM for a failure message
  60. * @param {string} canvasContainerId id of container of th
  61. * canvas.
  62. * @return {string} The html.
  63. */
  64. var makeFailHTML = function(msg) {
  65. return '' +
  66. '<table style="background-color: #8CE; width: 100%; height: 100%;"><tr>' +
  67. '<td align="center">' +
  68. '<div style="display: table-cell; vertical-align: middle;">' +
  69. '<div style="">' + msg + '</div>' +
  70. '</div>' +
  71. '</td></tr></table>';
  72. };
  73. /**
  74. * Mesasge for getting a webgl browser
  75. * @type {string}
  76. */
  77. var GET_A_WEBGL_BROWSER = '' +
  78. 'This page requires a browser that supports WebGL.<br/>' +
  79. '<a href="http://get.webgl.org">Click here to upgrade your browser.</a>';
  80. /**
  81. * Mesasge for need better hardware
  82. * @type {string}
  83. */
  84. var OTHER_PROBLEM = '' +
  85. "It doesn't appear your computer can support WebGL.<br/>" +
  86. '<a href="http://get.webgl.org/troubleshooting/">Click here for more information.</a>';
  87. /**
  88. * Creates a webgl context. If creation fails it will
  89. * change the contents of the container of the <canvas>
  90. * tag to an error message with the correct links for WebGL.
  91. * @param {Element} canvas. The canvas element to create a
  92. * context from.
  93. * @param {WebGLContextCreationAttirbutes} opt_attribs Any
  94. * creation attributes you want to pass in.
  95. * @return {WebGLRenderingContext} The created context.
  96. */
  97. var setupWebGL = function(canvas, opt_attribs) {
  98. function showLink(str) {
  99. var container = canvas.parentNode;
  100. if (container) {
  101. container.innerHTML = makeFailHTML(str);
  102. }
  103. };
  104. if (!window.WebGLRenderingContext) {
  105. showLink(GET_A_WEBGL_BROWSER);
  106. return null;
  107. }
  108. var context = create3DContext(canvas, opt_attribs);
  109. if (!context) {
  110. showLink(OTHER_PROBLEM);
  111. }
  112. return context;
  113. };
  114. /**
  115. * Creates a webgl context.
  116. * @param {!Canvas} canvas The canvas tag to get context
  117. * from. If one is not passed in one will be created.
  118. * @return {!WebGLContext} The created context.
  119. */
  120. var create3DContext = function(canvas, opt_attribs) {
  121. var names = ["webgl", "experimental-webgl", "webkit-3d", "moz-webgl"];
  122. var context = null;
  123. for (var ii = 0; ii < names.length; ++ii) {
  124. try {
  125. context = canvas.getContext(names[ii], opt_attribs);
  126. } catch(e) {}
  127. if (context) {
  128. break;
  129. }
  130. }
  131. return context;
  132. }
  133. return {
  134. create3DContext: create3DContext,
  135. setupWebGL: setupWebGL
  136. };
  137. }();
  138. /**
  139. * Provides requestAnimationFrame in a cross browser way.
  140. */
  141. window.requestAnimFrame = (function() {
  142. return window.requestAnimationFrame ||
  143. window.webkitRequestAnimationFrame ||
  144. window.mozRequestAnimationFrame ||
  145. window.oRequestAnimationFrame ||
  146. window.msRequestAnimationFrame ||
  147. function(/* function FrameRequestCallback */ callback, /* DOMElement Element */ element) {
  148. window.setTimeout(callback, 1000/60);
  149. };
  150. })();