offscreencanvas-worker-orbitcontrols.js 1.6 KB

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