offscreencanvas-worker-orbitcontrols.js 1.8 KB

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