threejs-offscreencanvas-w-fallback.html 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 w/Fallback</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script type="module">
  23. import {init, state} from './shared-cubes.js';
  24. function startWorker(canvas) {
  25. const offscreen = canvas.transferControlToOffscreen();
  26. const worker = new Worker('offscreencanvas-worker-cubes.js', {type: 'module'});
  27. worker.postMessage({type: 'init', canvas: offscreen}, [offscreen]);
  28. function sendSize() {
  29. worker.postMessage({
  30. type: 'size',
  31. width: canvas.clientWidth,
  32. height: canvas.clientHeight,
  33. });
  34. }
  35. window.addEventListener('resize', sendSize);
  36. sendSize();
  37. console.log('using OffscreenCanvas'); /* eslint-disable-line no-console */
  38. }
  39. function startMainPage(canvas) {
  40. init({canvas});
  41. function sendSize() {
  42. state.width = canvas.clientWidth;
  43. state.height = canvas.clientHeight;
  44. }
  45. window.addEventListener('resize', sendSize);
  46. sendSize();
  47. console.log('using regular canvas'); /* eslint-disable-line no-console */
  48. }
  49. function main() { /* eslint consistent-return: 0 */
  50. const canvas = document.querySelector('#c');
  51. if (canvas.transferControlToOffscreen) {
  52. startWorker(canvas);
  53. } else {
  54. startMainPage(canvas);
  55. }
  56. }
  57. main();
  58. </script>
  59. </html>