offscreencanvas.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - OffscreenCanvas</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. #noOffscreenCanvas {
  19. display: flex;
  20. width: 100%;
  21. height: 100%;
  22. align-items: center;
  23. justify-content: center;
  24. background: red;
  25. color: white;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <canvas id="c"></canvas>
  31. <div id="noOffscreenCanvas" style="display:none;">
  32. <div>no OffscreenCanvas support</div>
  33. </div>
  34. </body>
  35. <script type="module">
  36. function main() { /* eslint consistent-return: 0 */
  37. const canvas = document.querySelector( '#c' );
  38. if ( ! canvas.transferControlToOffscreen ) {
  39. canvas.style.display = 'none';
  40. document.querySelector( '#noOffscreenCanvas' ).style.display = '';
  41. return;
  42. }
  43. const offscreen = canvas.transferControlToOffscreen();
  44. const worker = new Worker( 'offscreencanvas-cubes.js', { type: 'module' } );
  45. worker.postMessage( { type: 'main', canvas: offscreen }, [ offscreen ] );
  46. function sendSize() {
  47. worker.postMessage( {
  48. type: 'size',
  49. width: canvas.clientWidth,
  50. height: canvas.clientHeight,
  51. } );
  52. }
  53. window.addEventListener( 'resize', sendSize );
  54. sendSize();
  55. }
  56. main();
  57. </script>
  58. </html>