offscreencanvas-worker-orbitcontrols.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import {init} from './shared-orbitcontrols.js';
  2. import {EventDispatcher} from 'https://cdn.skypack.dev/[email protected]/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. };