offscreencanvas-worker-orbitcontrols.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. 'use strict';
  2. /* global importScripts, init, THREE */
  3. importScripts('resources/threejs/r115/build/three.min.js');
  4. importScripts('resources/threejs/r115/examples/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. getBoundingClientRect() {
  19. return {
  20. left: this.left,
  21. top: this.top,
  22. width: this.width,
  23. height: this.height,
  24. right: this.left + this.width,
  25. bottom: this.top + this.height,
  26. };
  27. }
  28. handleEvent(data) {
  29. if (data.type === 'size') {
  30. this.left = data.left;
  31. this.top = data.top;
  32. this.width = data.width;
  33. this.height = data.height;
  34. return;
  35. }
  36. data.preventDefault = noop;
  37. data.stopPropagation = noop;
  38. this.dispatchEvent(data);
  39. }
  40. focus() {
  41. // no-op
  42. }
  43. }
  44. class ProxyManager {
  45. constructor() {
  46. this.targets = {};
  47. this.handleEvent = this.handleEvent.bind(this);
  48. }
  49. makeProxy(data) {
  50. const {id} = data;
  51. const proxy = new ElementProxyReceiver();
  52. this.targets[id] = proxy;
  53. }
  54. getProxy(id) {
  55. return this.targets[id];
  56. }
  57. handleEvent(data) {
  58. this.targets[data.id].handleEvent(data.data);
  59. }
  60. }
  61. const proxyManager = new ProxyManager();
  62. function start(data) {
  63. const proxy = proxyManager.getProxy(data.canvasId);
  64. proxy.body = proxy; // HACK!
  65. self.window = proxy;
  66. self.document = {
  67. addEventListener: proxy.addEventListener.bind(proxy),
  68. removeEventListener: proxy.removeEventListener.bind(proxy),
  69. };
  70. init({
  71. canvas: data.canvas,
  72. inputElement: proxy,
  73. });
  74. }
  75. function makeProxy(data) {
  76. proxyManager.makeProxy(data);
  77. }
  78. const handlers = {
  79. start,
  80. makeProxy,
  81. event: proxyManager.handleEvent,
  82. };
  83. self.onmessage = function(e) {
  84. const fn = handlers[e.data.type];
  85. if (!fn) {
  86. throw new Error('no handler for type: ' + e.data.type);
  87. }
  88. fn(e.data);
  89. };