-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
125 lines (108 loc) · 3.29 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>canvas-context by Damien Seguin (https://github.com/dmnsgn)</title>
<style>
:root {
--color-dark: #404040;
--color-light: #f2f2f2;
--color-accent: #fd5e62;
}
body {
margin: 0;
overscroll-behavior: none;
font-family: sans-serif;
color: var(--color-dark);
background-color: var(--color-light);
}
main {
padding: 0 20px;
}
</style>
</head>
<body>
<main>
<h1>canvas-context</h1>
<h2>Window context</h2>
<canvas id="canvas-window" width="100" height="100"></canvas>
<h2>Worker context</h2>
<canvas id="canvas-worker" width="100" height="100"></canvas>
<br />
<button id="button">Block the main thread</button>
</main>
<script type="module">
import createCanvasContext from "./index.js";
// Store an animation function to evaluate later in both the window and worker
const TEST_FUNCTION = /* js */ `
const loop = () => {
const now = Date.now() * 0.005;
// Clear
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
// Draw
context.save();
context.translate(50, 50);
context.beginPath();
context.arc(0, 0, 25 - Math.cos(now) * 10, 0, 2 * Math.PI);
context.fillStyle = "${window
.getComputedStyle(document.documentElement)
.getPropertyValue("--color-accent")}";
context.fill();
context.restore();
requestAnimationFrame(loop);
};
loop();
`;
// Create a usual context in Window
const { context } = createCanvasContext("2d", {
canvas: document.querySelector("#canvas-window"),
offscreen: true,
});
new Function(["context"], TEST_FUNCTION)(context);
// Use a Worker
const packageUrl = new URL("./index.js", import.meta.url).href;
const blob = new Blob(
[
/* js */ `
import createCanvasContext from "${packageUrl}";
addEventListener("message", ({ data }) => {
if (data.msg == "canvas-context-init") {
const { context } = createCanvasContext("2d", {
canvas: data.canvas,
});
${TEST_FUNCTION}
}
});
`,
],
{ type: "text/javascript" }
);
const worker = new Worker(URL.createObjectURL(blob), { type: "module" });
// Worker can't transfer a canvas context so passing worker will only return an OffscreenCanvas
const { canvas: offscreenCanvas } = createCanvasContext(null, {
canvas: document.querySelector("#canvas-worker"),
worker: true,
});
worker.postMessage(
{ msg: "canvas-context-init", canvas: offscreenCanvas },
[offscreenCanvas]
);
// Simulate a thread blocking action
function blockThread(milliseconds) {
const start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if (new Date().getTime() - start > milliseconds) {
break;
}
}
}
document.querySelector("#button").addEventListener("click", () => {
requestAnimationFrame(() => {
blockThread(2500);
});
});
</script>
</body>
</html>