-
Notifications
You must be signed in to change notification settings - Fork 7
/
handler.js
260 lines (224 loc) · 7.01 KB
/
handler.js
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import { UI, playMessage } from './gui.js';
class PlayerManager{
constructor( ) {
this.errorMessage = document.getElementById( 'message' );
this.offscreencanvas = document.getElementById( 'offscreencanvas' );
this. width = offscreencanvas.clientWidth;
this.height = offscreencanvas.clientHeight;
this.pixelRatio = window.devicePixelRatio;
// offscreen canvas
if ( 'transferControlToOffscreen' in offscreencanvas ) {
this.offscreen = offscreencanvas.transferControlToOffscreen();
this.worker = new Worker( './js/workers/offscreen.js', { type: 'module' } );
//looks bad, maybe i can create a class to handle some global variables.
this.eventHandlers = {
contextmenu: preventDefaultHandler,
mousedown: mouseEventHandler,
mousemove: mouseEventHandler,
mouseup: mouseEventHandler,
pointerdown: mouseEventHandler,
pointermove: mouseEventHandler,
pointerup: mouseEventHandler,
touchstart: touchEventHandler,
touchmove: touchEventHandler,
touchend: touchEventHandler,
wheel: wheelEventHandler,
keydown: filteredKeydownEventHandler,
};
this.proxy = new ElementProxy(this.offscreencanvas, this.worker, this.eventHandlers);
this.worker.postMessage( {
thisdrawingSurface: this.offscreen,
width: this.offscreencanvas.clientWidth,
height: this.offscreencanvas.clientHeight,
pixelRatio: window.devicePixelRatio,
path: '../../',
type: 'start',
canvas: this.offscreen,
canvasId: this.proxy.id,
}, [ this.offscreen ] );
}
else {
errorMessage.style.display = 'block';
}
}
getWorker(){
return this.worker;
}
}
let nextProxyId = 0;
class ElementProxy {
constructor(element, worker, eventHandlers) {
this.id = nextProxyId++;
this.worker = worker;
const sendEvent = (data) => {
this.worker.postMessage({
type: 'event',
id: this.id,
data,
});
};
// register an id
worker.postMessage({
type: 'makeProxy',
id: this.id,
});
sendSize();
for (const [eventName, handler] of Object.entries(eventHandlers)) {
element.addEventListener(eventName, function(event) {
handler(event, sendEvent);
});
}
function sendSize() {
const rect = element.getBoundingClientRect();
sendEvent({
type: 'size',
left: rect.left,
top: rect.top,
width: element.clientWidth,
height: element.clientHeight,
});
}
// really need to use ResizeObserver
window.addEventListener('resize', sendSize);
}
}
// reference && detailed : https://threejsfundamentals.org/threejs/lessons/threejs-offscreencanvas.html
const mouseEventHandler = makeSendPropertiesHandler([
'ctrlKey',
'metaKey',
'shiftKey',
'button',
'pointerType',
'clientX',
'clientY',
'pageX',
'pageY',]);
const wheelEventHandlerImpl = makeSendPropertiesHandler([
'deltaX',
'deltaY',]);
const keydownEventHandler = makeSendPropertiesHandler([
'ctrlKey',
'metaKey',
'shiftKey',
'keyCode',]);
function wheelEventHandler(event, sendFn) {
event.preventDefault();
wheelEventHandlerImpl(event, sendFn);
}
function preventDefaultHandler(event) {
event.preventDefault();
}
function copyProperties(src, properties, dst) {
for (const name of properties) {
dst[name] = src[name];
}
}
function makeSendPropertiesHandler(properties) {
return function sendProperties(event, sendFn) {
const data = {type: event.type};
copyProperties(event, properties, data);
sendFn(data);
};
}
function touchEventHandler(event, sendFn) {
const touches = [];
const data = {type: event.type, touches};
for (let i = 0; i < event.touches.length; ++i) {
const touch = event.touches[i];
touches.push({
pageX: touch.pageX,
pageY: touch.pageY,
});
}
sendFn(data);
}
// The four arrow keys
const orbitKeys = {
'37': true, // left
'38': true, // up
'39': true, // right
'40': true, // down
};
function filteredKeydownEventHandler(event, sendFn) {
const {keyCode} = event;
if (orbitKeys[keyCode]) {
event.preventDefault();
keydownEventHandler(event, sendFn);
}
}
export function main(){
var progressBarDiv;
progressBarDiv = document.createElement( 'div' );
progressBarDiv.innerText = "Loading...";
progressBarDiv.style.fontSize = "3em";
progressBarDiv.style.color = "#888";
progressBarDiv.style.display = "block";
progressBarDiv.style.position = "absolute";
progressBarDiv.style.top = "50%";
progressBarDiv.style.width = "100%";
progressBarDiv.style.textAlign = "center";
const webPlayer = new PlayerManager();
var progresBar = 0;
const progresBarUI = document.getElementById("progressBar");
var box = document.getElementById( 'playPauseButton' );
box.style.visibility= "hidden";
box.addEventListener('click', (e)=>{
playMessage("Play");
box.style.visibility= "hidden";
})
//to handle received message coming from worker
//increase proggres bar or decode audio
const handlers = {
incProgress,
decodeAudio,
endVideo,
};
//to inc progress bar
function incProgress( ) {
progresBar=progresBar+1;
// bar1.set(progresBar);
if(progresBar==100){
hideProgressBar();
box.style.visibility = "visible";
progresBar=0;
}else if(progresBar<100){
showProgressBar();
updateProgressBar( progresBar );
}else{
//nothing
}
}
function endVideo(){
displayPlaybutton();
}
function displayPlaybutton(){
box.style.visibility = "visible";
}
function updateProgressBar( fraction ) {
progressBarDiv.innerText = 'Loading... ' + fraction;
}
function hideProgressBar() {
document.body.removeChild( progressBarDiv );
}
function showProgressBar() {
document.body.appendChild( progressBarDiv );
}
//not possible to decode audio on workers side.
function decodeAudio( data ) {
var audioData = data.audata;
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var sourceTemp = audioCtx.createBufferSource();
audioCtx.decodeAudioData(audioData, function(buffer) {
allAudio.push(buffer);
});
}
webPlayer.getWorker().onmessage = function ( message ) {
var data = message.data;
var messageType = handlers[data.type];
if(!messageType){
console.log("message handle error!");
}
messageType(data)
};
UI( webPlayer.getWorker() );
}