offscreencanvas-w-picking.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Picking</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 { state, init, pickPosition } from './shared-picking.js';
  25. let sendMouse;
  26. function startWorker( canvas ) {
  27. const offscreen = canvas.transferControlToOffscreen();
  28. const worker = new Worker( 'offscreencanvas-worker-picking.js', { type: 'module' } );
  29. worker.postMessage( { type: 'init', canvas: offscreen }, [ offscreen ] );
  30. sendMouse = ( x, y ) => {
  31. worker.postMessage( {
  32. type: 'mouse',
  33. x,
  34. y,
  35. } );
  36. };
  37. function sendSize() {
  38. worker.postMessage( {
  39. type: 'size',
  40. width: canvas.clientWidth,
  41. height: canvas.clientHeight,
  42. } );
  43. }
  44. window.addEventListener( 'resize', sendSize );
  45. sendSize();
  46. console.log( 'using OffscreenCanvas' ); /* eslint-disable-line no-console */
  47. }
  48. function startMainPage( canvas ) {
  49. init( { canvas } );
  50. sendMouse = ( x, y ) => {
  51. pickPosition.x = x;
  52. pickPosition.y = y;
  53. };
  54. function sendSize() {
  55. state.width = canvas.clientWidth;
  56. state.height = canvas.clientHeight;
  57. }
  58. window.addEventListener( 'resize', sendSize );
  59. sendSize();
  60. console.log( 'using regular canvas' ); /* eslint-disable-line no-console */
  61. }
  62. function main() { /* eslint consistent-return: 0 */
  63. const canvas = document.querySelector( '#c' );
  64. if ( canvas.transferControlToOffscreen ) {
  65. startWorker( canvas );
  66. } else {
  67. startMainPage( canvas );
  68. }
  69. function getCanvasRelativePosition( event ) {
  70. const rect = canvas.getBoundingClientRect();
  71. return {
  72. x: ( event.clientX - rect.left ) * canvas.width / rect.width,
  73. y: ( event.clientY - rect.top ) * canvas.height / rect.height,
  74. };
  75. }
  76. function setPickPosition( event ) {
  77. const pos = getCanvasRelativePosition( event );
  78. sendMouse(
  79. ( pos.x / canvas.width ) * 2 - 1,
  80. ( pos.y / canvas.height ) * - 2 + 1 ); // note we flip Y
  81. }
  82. function clearPickPosition() {
  83. // unlike the mouse which always has a position
  84. // if the user stops touching the screen we want
  85. // to stop picking. For now we just pick a value
  86. // unlikely to pick something
  87. sendMouse( - 100000, - 100000 );
  88. }
  89. window.addEventListener( 'mousemove', setPickPosition );
  90. window.addEventListener( 'mouseout', clearPickPosition );
  91. window.addEventListener( 'mouseleave', clearPickPosition );
  92. window.addEventListener( 'touchstart', ( event ) => {
  93. // prevent the window from scrolling
  94. event.preventDefault();
  95. setPickPosition( event.touches[ 0 ] );
  96. }, { passive: false } );
  97. window.addEventListener( 'touchmove', ( event ) => {
  98. setPickPosition( event.touches[ 0 ] );
  99. } );
  100. window.addEventListener( 'touchend', clearPickPosition );
  101. }
  102. main();
  103. </script>
  104. </html>