Repository navigation
Expand file tree
/
Copy pathWorkerDom.js
More file actions
177 lines (168 loc) · 5.99 KB
/
Copy pathWorkerDom.js
File metadata and controls
177 lines (168 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Minimal DOM stand-ins for the render worker. Input arrives from the main
// thread (InputFrameBridge) and is dispatched on the canvas facade (pointer,
// wheel, drag) or the document (keys). Like a real browser these events then
// propagate: capture listeners on window → document → target, then bubble
// back target → document → window. Tools that end a stroke on a window
// `pointerup` (manual sculpt/texture paint, Paint Mode, spline editing) rely
// on that bubbling.
const NON_BUBBLING_EVENTS = new Set([
'pointerenter',
'pointerleave',
'mouseenter',
'mouseleave',
'focus',
'blur',
'load',
'webglcontextlost',
'webglcontextrestored',
]);
const captureFlag = (options) => (typeof options === 'boolean' ? options : !!options?.capture);
export class TerrainEventTarget {
constructor() {
this.listeners = new Map();
// Next target up the propagation path (canvas → document → window).
this.eventParent = null;
}
addEventListener(type, listener, options) {
if (!listener) return;
const capture = captureFlag(options);
if (!this.listeners.has(type)) this.listeners.set(type, []);
const entries = this.listeners.get(type);
if (entries.some((entry) => entry.listener === listener && entry.capture === capture)) return;
entries.push({ listener, capture });
}
removeEventListener(type, listener, options) {
const entries = this.listeners.get(type);
if (!entries) return;
const capture = captureFlag(options);
const index = entries.findIndex((entry) => entry.listener === listener && entry.capture === capture);
if (index >= 0) entries.splice(index, 1);
}
_invoke(event, phase) {
const entries = this.listeners.get(event.type);
if (!entries?.length) return;
event.currentTarget = this;
event.eventPhase = phase;
// Snapshot: a listener may add/remove listeners while running.
for (const entry of entries.slice()) {
if (event._immediateStopped) return;
if (phase === 1 && !entry.capture) continue;
if (phase === 3 && entry.capture) continue;
if (!entries.includes(entry)) continue;
const { listener } = entry;
if (typeof listener === 'function') listener.call(this, event);
else listener?.handleEvent?.(event);
}
}
dispatchTerrainEvent(type, payload = {}) {
const bubbles = !NON_BUBBLING_EVENTS.has(type);
const event = {
type,
bubbles,
defaultPrevented: false,
preventDefault() { this.defaultPrevented = true; },
button: 0,
buttons: 0,
pointerType: 'mouse',
clientX: 0,
clientY: 0,
deltaX: 0,
deltaY: 0,
deltaMode: 0,
key: '',
code: '',
repeat: false,
...payload,
target: payload.target ?? this,
currentTarget: this,
eventPhase: 2,
_stopped: false,
_immediateStopped: false,
stopPropagation() { this._stopped = true; },
stopImmediatePropagation() { this._stopped = true; this._immediateStopped = true; },
composedPath: () => path.slice(),
};
const path = [this];
if (bubbles) {
for (let node = this.eventParent; node && !path.includes(node); node = node.eventParent) path.push(node);
}
// Capture: outermost ancestor down to (not including) the target.
for (let i = path.length - 1; i >= 1; i--) {
path[i]._invoke(event, 1);
if (event._stopped) return event;
}
// At target: every listener, capture or not, in registration order.
path[0]._invoke(event, 2);
if (!bubbles || event._stopped) return event;
for (let i = 1; i < path.length; i++) {
path[i]._invoke(event, 3);
if (event._stopped) return event;
}
return event;
}
}
export class WorkerCanvasFacade extends TerrainEventTarget {
constructor(canvas, viewport) {
super();
this.canvas = canvas;
this.viewport = { ...viewport };
this.style = {};
this.parentElement = this;
this.tabIndex = 0;
for (const type of ['webglcontextlost', 'webglcontextrestored']) {
canvas.addEventListener?.(type, (event) => {
this.dispatchTerrainEvent(type, {
statusMessage: event?.statusMessage || '',
preventDefault: () => event?.preventDefault?.(),
});
});
}
}
get width() { return this.canvas.width; }
set width(value) { this.canvas.width = value; }
get height() { return this.canvas.height; }
set height(value) { this.canvas.height = value; }
get clientWidth() { return this.viewport.width; }
get clientHeight() { return this.viewport.height; }
getContext(...args) { return this.canvas.getContext(...args); }
getBoundingClientRect() {
return {
left: 0,
top: 0,
right: this.viewport.width,
bottom: this.viewport.height,
width: this.viewport.width,
height: this.viewport.height,
};
}
setTerrainViewport(width, height) { this.viewport = { ...this.viewport, width, height }; }
setPointerCapture() {}
releasePointerCapture() {}
// Only the real browser canvas can acquire pointer lock.
requestPointerLock() {}
focus() {}
}
/**
* Builds the worker's window/document stand-ins and links the propagation
* path canvas → document → window. Returns them without installing globals.
*/
export function createWorkerDom(canvasFacade, { exitPointerLock } = {}) {
const windowTarget = new TerrainEventTarget();
const documentTarget = new TerrainEventTarget();
documentTarget.eventParent = windowTarget;
documentTarget.visibilityState = 'visible';
documentTarget.pointerLockElement = null;
documentTarget.exitPointerLock = () => { exitPointerLock?.(); };
documentTarget.createElement = (tag) => {
if (tag === 'canvas') return new OffscreenCanvas(1, 1);
return new TerrainEventTarget();
};
documentTarget.defaultView = windowTarget;
documentTarget.body = new TerrainEventTarget();
documentTarget.body.eventParent = documentTarget;
if (canvasFacade) {
canvasFacade.eventParent = documentTarget;
canvasFacade.ownerDocument = documentTarget;
}
return { windowTarget, documentTarget };
}