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