webgl_worker_offscreencanvas.html 3.5 KB

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