1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import {init} from './shared-orbitcontrols.js';
- import {EventDispatcher} from 'https://cdn.skypack.dev/[email protected]/build/three.module.js';
- function noop() {
- }
- class ElementProxyReceiver extends EventDispatcher {
- constructor() {
- super();
- // because OrbitControls try to set style.touchAction;
- this.style = {};
- }
- get clientWidth() {
- return this.width;
- }
- get clientHeight() {
- return this.height;
- }
- // OrbitControls call these as of r132. Maybe we should implement them
- setPointerCapture() { }
- releasePointerCapture() { }
- getBoundingClientRect() {
- return {
- left: this.left,
- top: this.top,
- width: this.width,
- height: this.height,
- right: this.left + this.width,
- bottom: this.top + this.height,
- };
- }
- handleEvent(data) {
- if (data.type === 'size') {
- this.left = data.left;
- this.top = data.top;
- this.width = data.width;
- this.height = data.height;
- return;
- }
- data.preventDefault = noop;
- data.stopPropagation = noop;
- this.dispatchEvent(data);
- }
- focus() {
- // no-op
- }
- }
- class ProxyManager {
- constructor() {
- this.targets = {};
- this.handleEvent = this.handleEvent.bind(this);
- }
- makeProxy(data) {
- const {id} = data;
- const proxy = new ElementProxyReceiver();
- this.targets[id] = proxy;
- }
- getProxy(id) {
- return this.targets[id];
- }
- handleEvent(data) {
- this.targets[data.id].handleEvent(data.data);
- }
- }
- const proxyManager = new ProxyManager();
- function start(data) {
- const proxy = proxyManager.getProxy(data.canvasId);
- proxy.ownerDocument = proxy; // HACK!
- self.document = {}; // HACK!
- init({
- canvas: data.canvas,
- inputElement: proxy,
- });
- }
- function makeProxy(data) {
- proxyManager.makeProxy(data);
- }
- const handlers = {
- start,
- makeProxy,
- event: proxyManager.handleEvent,
- };
- self.onmessage = function(e) {
- const fn = handlers[e.data.type];
- if (typeof fn !== 'function') {
- throw new Error('no handler for type: ' + e.data.type);
- }
- fn(e.data);
- };
|