-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
151 lines (138 loc) · 4.08 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>pixi.js for worker</title>
<style>
canvas {
border: 1px solid black;
}
</style>
</head>
<body>
<canvas width="800" height="600"></canvas>
<script>
function cloneEvent(e) {
const exclude = [
"path",
"view",
"target",
"srcElement",
"currentTarget",
"sourceCapabilities",
"toElement",
"fromElement",
"relatedTarget"
];
if (e === undefined || e === null) return undefined;
const clone = {};
for (const p in e) {
if (exclude.find((el) => el === p)) {
continue;
}
if (typeof e[p] === "function") {
continue;
}
clone[p] = e[p];
}
return clone;
}
var pixiWorker = new Worker("worker.js");
const eventListeners = {
mousemove: (e) => {
pixiWorker.postMessage({
type: "event",
event: { type: "mousemove", data: cloneEvent(e) },
messageType: "notify",
eventId: -1,
});
},
mousedown: (e) => {
pixiWorker.postMessage({
type: "event",
event: { type: "mousedown", data: cloneEvent(e) },
messageType: "notify",
eventId: -1,
});
},
mouseup: (e) => {
pixiWorker.postMessage({
type: "event",
event: { type: "mouseup", data: cloneEvent(e) },
messageType: "notify",
eventId: -1,
});
},
mouseover: (e) => {
pixiWorker.postMessage({
type: "event",
event: { type: "mouseover", data: cloneEvent(e) },
messageType: "notify",
eventId: -1,
});
},
mouseout: (e) => {
pixiWorker.postMessage({
type: "event",
event: { type: "mouseout", data: cloneEvent(e) },
messageType: "notify",
eventId: -1,
});
},
// 只允许了按键tab事件,暂不处理
// 用于辅助功能管理器重新创建了选项卡和屏幕阅读器阅读内容的能力,可以帮助残障人士访问 PixiJS 内容.
// keydown: (e) => {
// pixiWorker.postMessage({
// type: "event",
// event: { type: "keydown", data: cloneEvent(e) },
// messageType: "notify",
// eventId: -1,
// });
// },
wheel: (e) => {
pixiWorker.postMessage({
type: "event",
event: { type: "wheel", data: cloneEvent(e) },
messageType: "notify",
eventId: -1,
});
},
};
const canvas = document.querySelector("canvas");
const offscreen = canvas.transferControlToOffscreen();
// document.body.addEventListener("keydown", eventListeners.keydown, false);
document.body.addEventListener("mousemove", eventListeners.mousemove, true);
document.body.addEventListener("mouseup", eventListeners.mouseup, true);
canvas.addEventListener("mousedown", eventListeners.mousedown, true);
canvas.addEventListener("mouseover", eventListeners.mouseover, true);
canvas.addEventListener("mouseout", eventListeners.mouseout, true);
document.body.addEventListener("wheel", eventListeners.wheel);
let boundingClientRect = canvas.getBoundingClientRect();
pixiWorker.postMessage({
type: "start",
hostname: window.location.hostname,
width: 500,
height: 500,
boundingClientRect,
canvas: offscreen,
}, [offscreen]);
</script>
<script>
(function () {
var script = document.createElement('script');
script.onload = function () {
var stats = new Stats();
document.body.appendChild(stats.dom);
requestAnimationFrame(function loop() {
stats.update();
requestAnimationFrame(loop);
});
};
script.src = './library/stats.js';
document.head.appendChild(script);
})()
</script>
</body>
</html>