-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
173 lines (158 loc) · 5.67 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/vega.min.js"></script>
<script type="text/javascript" id="worker_script">
// The web worker code
self.onmessage = async function timeworker(event) {
const eventArray = event.data;
if (Array.isArray(eventArray)) {
for (const element of eventArray) {
await doEvent(element);
};
};
};
function doEvent(element) {
return new Promise(resolve => {
if (element.type == 'interval') {
index = element.start;
let myInterval = setInterval(() => {
postMessage({ dataindex: index });
index += 1;
if (index > element.end) {
clearInterval(myInterval);
console.log("interval resolved");
resolve();
}
}, element.period);
} else if (element.type == 'delay') {
setTimeout(() => {
postMessage({ event: element.event });
resolve();
console.log("delay resolved");
}, element.duration);
}
});
};
</script>
<script type="text/javascript">
// Wait until the DOM is loaded before trying to attach the vega spec to an element
window.addEventListener("load", () => {
const theview = new vega.View(vega.parse(vegaspec), {
renderer: 'canvas',
container: '#vegaElement',
hover: true
}).run();
// Create the web worker from a Blob formed from the worker_script element
// The web worker would normally be loaded from a regular URL
const myWorker = new Worker(
URL.createObjectURL(
new Blob([worker_script.textContent])
)
);
// Send our array of events to the web worker
myWorker.postMessage(events);
// Process the responses, updating both the HTML element and sending an update to the vega signal
myWorker.onmessage = (e) => {
for (const keys in e.data) {
switch (keys) {
case 'dataindex':
const dataindex = e.data.dataindex;
document.getElementById('dataindex').innerText = dataindex;
const indexevent = new CustomEvent("updateIndex", { detail: dataindex });
window.dispatchEvent(indexevent);
break;
case 'event':
const status = e.data.event;
document.getElementById('status').innerText = status;
const statusevent = new CustomEvent("updateStatus", { detail: status });
window.dispatchEvent(statusevent);
}
}
}
});
const events = [
{
"type": "interval",
"period": 10,
"start": 0,
"end": 316
},
{
"type": "delay",
"duration": 1000,
"event": "Target achieved"
}
];
const vegaspec = {
"$schema": "https://vega.github.io/schema/vega/v3.0.json",
"description": "Web worker test",
"autosize": "pad",
"padding": 5,
"background": "#cec",
"signals": [
{
"name": "width",
"update": "300"
},
{
"name": "height",
"update": "200"
},
{
"name": "dataindex",
"value": "",
"on": [
{
"events": "window:updateIndex",
"update": "event.detail"
}
]
},
{
"name": "status",
"value": "",
"on": [
{
"events": "window:updateStatus",
"update": "event.detail"
}
]
}
],
"marks": [
{
"type": "text",
"encode": {
"enter": {
"x": { "value": 0 },
"y": { "value": 40 }
},
"update": {
"text": { "signal": "dataindex" }
}
}
},
{
"type": "text",
"encode": {
"enter": {
"x": { "value": 0 },
"y": { "value": 80 }
},
"update": {
"text": { "signal": "status" }
}
}
}
]
};
</script>
</head>
<body>
<div id="vegaElement"></div>
<p id="status"></p>
<p id="dataindex"></p>
</body>
</html>