webgl_worker_offscreencanvas.html 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - worker - offscreen canvas</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. background:#000000;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px;
  18. width: 100%;
  19. color: #ffffff;
  20. padding: 5px;
  21. font-family:Monospace;
  22. font-size:13px;
  23. text-align:center;
  24. }
  25. #message {
  26. color: #ff0000;
  27. font-size:14px;
  28. display: none;
  29. }
  30. #message > a {
  31. color: #ff0000;
  32. }
  33. a { color: #ffffff; }
  34. </style>
  35. <script src="js/WebGL.js"></script>
  36. </head>
  37. <body>
  38. <div id="info">
  39. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> offscreen canvas<br/><br/>
  40. three.js runs in a worker and produces asynchronously frames for the canvas element in the main thread. <br/>
  41. This is an <a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas" target="_blank" rel="noopener noreferrer"> experimental feature</a>!
  42. <p id="message">Your browser does not support OffscreenCanvas. Check the browser support <a href="https://caniuse.com/#feat=offscreencanvas" target="_blank" rel="noopener noreferrer">here</a></p>
  43. </div>
  44. <canvas id="canvas" style="width: 100%; height: 100%"></canvas>
  45. </body>
  46. <script>
  47. if ( WEBGL.isWebGLAvailable() === false ) {
  48. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  49. }
  50. var canvas = document.getElementById( 'canvas' );
  51. if ( canvas.transferControlToOffscreen !== undefined ) {
  52. var offscreen = canvas.transferControlToOffscreen();
  53. var worker = new Worker( 'js/workers/OffscreenCanvas.js' );
  54. worker.postMessage( {
  55. drawingSurface: offscreen,
  56. width: window.innerWidth,
  57. height: window.innerHeight,
  58. pixelRatio: window.devicePixelRatio
  59. }, [ offscreen ] ); // transfer
  60. } else {
  61. document.getElementById( 'message' ).style.display = 'block';
  62. }
  63. </script>
  64. </html>