threejs-offscreencanvas-w-fallback.html 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 src="resources/threejs/r114/build/three.min.js"></script>
  23. <script src="shared-cubes.js"></script>
  24. <script>
  25. 'use strict'; // eslint-disable-line
  26. /* globals state, init */
  27. function startWorker(canvas) {
  28. const offscreen = canvas.transferControlToOffscreen();
  29. const worker = new Worker('offscreencanvas-worker-cubes.js');
  30. worker.postMessage({type: 'init', canvas: offscreen}, [offscreen]);
  31. function sendSize() {
  32. worker.postMessage({
  33. type: 'size',
  34. width: canvas.clientWidth,
  35. height: canvas.clientHeight,
  36. });
  37. }
  38. window.addEventListener('resize', sendSize);
  39. sendSize();
  40. console.log('using OffscreenCanvas'); /* eslint-disable-line no-console */
  41. }
  42. function startMainPage(canvas) {
  43. init({canvas});
  44. function sendSize() {
  45. state.width = canvas.clientWidth;
  46. state.height = canvas.clientHeight;
  47. }
  48. window.addEventListener('resize', sendSize);
  49. sendSize();
  50. console.log('using regular canvas'); /* eslint-disable-line no-console */
  51. }
  52. function main() { /* eslint consistent-return: 0 */
  53. const canvas = document.querySelector('#c');
  54. if (canvas.transferControlToOffscreen) {
  55. startWorker(canvas);
  56. } else {
  57. startMainPage(canvas);
  58. }
  59. }
  60. main();
  61. </script>
  62. </html>