webgl_worker_offscreencanvas.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. canvas {
  17. display: inline-block;
  18. }
  19. #message {
  20. color: #ff0000;
  21. display: none;
  22. }
  23. #message > a {
  24. color: #ff0000;
  25. }
  26. #container {
  27. position: absolute;
  28. top: 50px;
  29. bottom: 70px;
  30. width: 100%;
  31. }
  32. #ui {
  33. position: absolute;
  34. bottom: 20px;
  35. width: 100%;
  36. text-align: center;
  37. }
  38. #button {
  39. border: 0;
  40. padding: 4px 6px;
  41. background: #dddddd;
  42. outline: none;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id="info">
  48. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - offscreen canvas (<a href="https://developer.mozilla.org/en-US/docs/Web/API/OffscreenCanvas" target="_blank" rel="noopener noreferrer">about</a>)<br/>
  49. <p id="message">Your browser does not support OffscreenCanvas with a WebGL Context. Check the browser support <a href="https://caniuse.com/#feat=offscreencanvas" target="_blank" rel="noopener noreferrer">here</a></p>
  50. </div>
  51. <div id="container">
  52. <canvas id="canvas1" style="width: 50%; height: 100%"></canvas><canvas id="canvas2" style="width: 50%; height: 100%"></canvas>
  53. </div>
  54. <div id="ui">
  55. <button id="button">START JANK</button><br/>
  56. <span id="result"></span>
  57. </div>
  58. <script type="importmap">
  59. {
  60. "imports": {
  61. "three": "../build/three.module.js",
  62. "three/addons/": "./jsm/"
  63. }
  64. }
  65. </script>
  66. <script type="module">
  67. import initJank from 'three/addons/offscreen/jank.js';
  68. import init from 'three/addons/offscreen/scene.js';
  69. // onscreen
  70. const canvas1 = document.getElementById( 'canvas1' );
  71. const canvas2 = document.getElementById( 'canvas2' );
  72. const width = canvas1.clientWidth;
  73. const height = canvas1.clientHeight;
  74. const pixelRatio = window.devicePixelRatio;
  75. init( canvas1, width, height, pixelRatio, './' );
  76. initJank();
  77. // offscreen
  78. const isSafari = /^((?!chrome|android).)*safari/i.test( navigator.userAgent );
  79. let supportOffScreenWebGL = 'transferControlToOffscreen' in canvas2;
  80. // If it's Safari, then check the version because Safari < 17 doesn't support OffscreenCanvas with a WebGL context.
  81. if ( isSafari ) {
  82. var versionMatch = navigator.userAgent.match( /version\/(\d+)/i );
  83. var safariVersion = versionMatch ? parseInt( versionMatch[ 1 ] ) : 0;
  84. supportOffScreenWebGL = safariVersion >= 17;
  85. }
  86. if ( supportOffScreenWebGL ) {
  87. const offscreen = canvas2.transferControlToOffscreen();
  88. const worker = new Worker( 'jsm/offscreen/offscreen.js', { type: 'module' } );
  89. worker.postMessage( {
  90. drawingSurface: offscreen,
  91. width: canvas2.clientWidth,
  92. height: canvas2.clientHeight,
  93. pixelRatio: window.devicePixelRatio,
  94. path: '../../'
  95. }, [ offscreen ] );
  96. } else {
  97. document.getElementById( 'message' ).style.display = 'block';
  98. }
  99. </script>
  100. </body>
  101. </html>