webgl_worker_offscreencanvas.html 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 {
  34. color: #ffffff;
  35. }
  36. </style>
  37. <script src="js/Detector.js"></script>
  38. </head>
  39. <body>
  40. <div id="info">
  41. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> offscreen canvas<br/><br/>
  42. three.js runs in a worker and produces asynchronously frames for the canvas element in the main thread. <br/>
  43. This is an <a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas" target="_blank" rel="noopener noreferrer"> experimental feature</a>!
  44. <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>
  45. </div>
  46. <canvas id="canvas" style="width: 100%; height: 100%"></canvas>
  47. </body>
  48. <script>
  49. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  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>