WebGL.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * A utility module with basic WebGL 2 capability testing.
  3. *
  4. * @hideconstructor
  5. */
  6. class WebGL {
  7. /**
  8. * Returns `true` if WebGL 2 is available.
  9. *
  10. * @return {boolean} Whether WebGL 2 is available or not.
  11. */
  12. static isWebGL2Available() {
  13. try {
  14. const canvas = document.createElement( 'canvas' );
  15. return !! ( window.WebGL2RenderingContext && canvas.getContext( 'webgl2' ) );
  16. } catch ( e ) {
  17. return false;
  18. }
  19. }
  20. /**
  21. * Returns `true` if the given color space is available. This method can only be used
  22. * if WebGL 2 is supported.
  23. *
  24. * @param {string} colorSpace - The color space to test.
  25. * @return {boolean} Whether the given color space is available or not.
  26. */
  27. static isColorSpaceAvailable( colorSpace ) {
  28. try {
  29. const canvas = document.createElement( 'canvas' );
  30. const ctx = window.WebGL2RenderingContext && canvas.getContext( 'webgl2' );
  31. ctx.drawingBufferColorSpace = colorSpace;
  32. return ctx.drawingBufferColorSpace === colorSpace; // deepscan-disable-line SAME_OPERAND_VALUE
  33. } catch ( e ) {
  34. return false;
  35. }
  36. }
  37. /**
  38. * Returns a `div` element representing a formatted error message that can be appended in
  39. * web sites if WebGL 2 isn't supported.
  40. *
  41. * @return {HTMLDivElement} A `div` element representing a formatted error message that WebGL 2 isn't supported.
  42. */
  43. static getWebGL2ErrorMessage() {
  44. return this._getErrorMessage( 2 );
  45. }
  46. // private
  47. static _getErrorMessage( version ) {
  48. const names = {
  49. 1: 'WebGL',
  50. 2: 'WebGL 2'
  51. };
  52. const contexts = {
  53. 1: window.WebGLRenderingContext,
  54. 2: window.WebGL2RenderingContext
  55. };
  56. let message = 'Your $0 does not seem to support <a href="http://khronos.org/webgl/wiki/Getting_a_WebGL_Implementation" style="color:#000">$1</a>';
  57. const element = document.createElement( 'div' );
  58. element.id = 'webglmessage';
  59. element.style.fontFamily = 'monospace';
  60. element.style.fontSize = '13px';
  61. element.style.fontWeight = 'normal';
  62. element.style.textAlign = 'center';
  63. element.style.background = '#fff';
  64. element.style.color = '#000';
  65. element.style.padding = '1.5em';
  66. element.style.width = '400px';
  67. element.style.margin = '5em auto 0';
  68. if ( contexts[ version ] ) {
  69. message = message.replace( '$0', 'graphics card' );
  70. } else {
  71. message = message.replace( '$0', 'browser' );
  72. }
  73. message = message.replace( '$1', names[ version ] );
  74. element.innerHTML = message;
  75. return element;
  76. }
  77. // @deprecated, r168
  78. static isWebGLAvailable() {
  79. console.warn( 'isWebGLAvailable() has been deprecated and will be removed in r178. Use isWebGL2Available() instead.' );
  80. try {
  81. const canvas = document.createElement( 'canvas' );
  82. return !! ( window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ) );
  83. } catch ( e ) {
  84. return false;
  85. }
  86. }
  87. static getWebGLErrorMessage() {
  88. console.warn( 'getWebGLErrorMessage() has been deprecated and will be removed in r178. Use getWebGL2ErrorMessage() instead.' );
  89. return this._getErrorMessage( 1 );
  90. }
  91. }
  92. export default WebGL;
粤ICP备19079148号