offscreencanvas-worker-orbitcontrols.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { init } from './shared-orbitcontrols.js';
  2. import { EventDispatcher } from 'https://cdn.skypack.dev/three@0.136.0/build/three.module.js';
  3. function noop() {
  4. }
  5. class ElementProxyReceiver extends EventDispatcher {
  6. constructor() {
  7. super();
  8. // because OrbitControls try to set style.touchAction;
  9. this.style = {};
  10. }
  11. get clientWidth() {
  12. return this.width;
  13. }
  14. get clientHeight() {
  15. return this.height;
  16. }
  17. // OrbitControls call these as of r132. Maybe we should implement them
  18. setPointerCapture() { }
  19. releasePointerCapture() { }
  20. getBoundingClientRect() {
  21. return {
  22. left: this.left,
  23. top: this.top,
  24. width: this.width,
  25. height: this.height,
  26. right: this.left + this.width,
  27. bottom: this.top + this.height,
  28. };
  29. }
  30. handleEvent( data ) {
  31. if ( data.type === 'size' ) {
  32. this.left = data.left;
  33. this.top = data.top;
  34. this.width = data.width;
  35. this.height = data.height;
  36. return;
  37. }
  38. data.preventDefault = noop;
  39. data.stopPropagation = noop;
  40. this.dispatchEvent( data );
  41. }
  42. focus() {
  43. // no-op
  44. }
  45. }
  46. class ProxyManager {
  47. constructor() {
  48. this.targets = {};
  49. this.handleEvent = this.handleEvent.bind( this );
  50. }
  51. makeProxy( data ) {
  52. const { id } = data;
  53. const proxy = new ElementProxyReceiver();
  54. this.targets[ id ] = proxy;
  55. }
  56. getProxy( id ) {
  57. return this.targets[ id ];
  58. }
  59. handleEvent( data ) {
  60. this.targets[ data.id ].handleEvent( data.data );
  61. }
  62. }
  63. const proxyManager = new ProxyManager();
  64. function start( data ) {
  65. const proxy = proxyManager.getProxy( data.canvasId );
  66. proxy.ownerDocument = proxy; // HACK!
  67. self.document = {}; // HACK!
  68. init( {
  69. canvas: data.canvas,
  70. inputElement: proxy,
  71. } );
  72. }
  73. function makeProxy( data ) {
  74. proxyManager.makeProxy( data );
  75. }
  76. const handlers = {
  77. start,
  78. makeProxy,
  79. event: proxyManager.handleEvent,
  80. };
  81. self.onmessage = function ( e ) {
  82. const fn = handlers[ e.data.type ];
  83. if ( typeof fn !== 'function' ) {
  84. throw new Error( 'no handler for type: ' + e.data.type );
  85. }
  86. fn( e.data );
  87. };