Spaces:
Running
Running
liewchooichin
commited on
Commit
•
4840bff
1
Parent(s):
b884914
Upload 4 files
Browse files- draw-web-worker/index.html +63 -0
- draw-web-worker/main.js +56 -0
- draw-web-worker/style.css +40 -0
- draw-web-worker/worker.js +56 -0
draw-web-worker/index.html
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
<!DOCTYPE html>
|
3 |
+
|
4 |
+
<html lang="en">
|
5 |
+
<head>
|
6 |
+
<meta charset="utf-8">
|
7 |
+
<meta name="viewport" content="width=device-width">
|
8 |
+
<meta name="viewport" content="initial-scale=1">
|
9 |
+
<link href="style.css" rel="stylesheet" type="text/css">
|
10 |
+
<link href="../favicon.svg" rel="icon" type="image/svg+sml">
|
11 |
+
<script defer src="main.js"></script>
|
12 |
+
<title>Draw simple shapes</title>
|
13 |
+
</head>
|
14 |
+
|
15 |
+
<body>
|
16 |
+
|
17 |
+
<header>
|
18 |
+
</header>
|
19 |
+
|
20 |
+
<main>
|
21 |
+
<h1>Draw some shapes with simple Web Workers API</h1>
|
22 |
+
<p>
|
23 |
+
To view the message between the main and the Worker,
|
24 |
+
view the message using the Web Developer Tools of your
|
25 |
+
browser.
|
26 |
+
</p>
|
27 |
+
<p>
|
28 |
+
The main script will first transfer the canvas as an
|
29 |
+
<code>offscreenCanvas</code> using
|
30 |
+
<code>canvas.transferControlToOffscreen();</code>.
|
31 |
+
After that the Worker will draw on the canvas.
|
32 |
+
</p>
|
33 |
+
<!-- Choose a shape and color from a selection list -->
|
34 |
+
<form>
|
35 |
+
<label for="shape">Choose a shape:</label>
|
36 |
+
<select id="shape" name="shape">
|
37 |
+
<option value="">--Choose a shape--</option>
|
38 |
+
<option value="rect">Rectangle</option>
|
39 |
+
<option value="square">Square</option>
|
40 |
+
<option value="circle">Circle</option>
|
41 |
+
</select>
|
42 |
+
<br>
|
43 |
+
<label for="color">Choose a color:</label>
|
44 |
+
<select id="color" name="color">
|
45 |
+
<option value="">--Choose a color</option>
|
46 |
+
<option value="salmon">Pink</option>
|
47 |
+
<option value="blue">Blue</option>
|
48 |
+
<option value="green">Green</option>
|
49 |
+
</select>
|
50 |
+
<br>
|
51 |
+
</form>
|
52 |
+
|
53 |
+
<!-- Drawing canvas -->
|
54 |
+
<canvas id="drawingArea"></canvas>
|
55 |
+
|
56 |
+
</main>
|
57 |
+
|
58 |
+
<footer>
|
59 |
+
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="http://url link to work">Title of Work</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="http://url link to my profile">Chooi Chin Liew</a> is marked with <a href="https://creativecommons.org/publicdomain/zero/1.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC0 1.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1" alt=""><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/zero.svg?ref=chooser-v1" alt=""></a></p>
|
60 |
+
</footer>
|
61 |
+
|
62 |
+
</body>
|
63 |
+
</html>
|
draw-web-worker/main.js
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* Main script to call the worker.
|
3 |
+
* The canvas will be transferred to the Worker.
|
4 |
+
* The main script will pass the color and shape to the worker.
|
5 |
+
* The worker will then draw the shape with the color.
|
6 |
+
*/
|
7 |
+
const color = document.querySelector("#color");
|
8 |
+
const shape = document.querySelector("#shape");
|
9 |
+
|
10 |
+
// Use getElementById: No need to append the # in front of the id
|
11 |
+
const drawingArea = document.getElementById("drawingArea");
|
12 |
+
// offscreen canvas for worker
|
13 |
+
const offscreen = drawingArea.transferControlToOffscreen();
|
14 |
+
|
15 |
+
// Check if the window has Worker.
|
16 |
+
if (window.Worker) {
|
17 |
+
const worker = new Worker("./worker.js");
|
18 |
+
// transfer the offscreen to the worker
|
19 |
+
let messageToWorker = {
|
20 |
+
command: "getOffscreen",
|
21 |
+
canvas: offscreen
|
22 |
+
}
|
23 |
+
// The [] array buffer must be transferred to the Worker.
|
24 |
+
worker.postMessage(messageToWorker, [offscreen]);
|
25 |
+
worker.addEventListener("message", messageFromWorker);
|
26 |
+
|
27 |
+
// get the color and shape to draw
|
28 |
+
color.addEventListener("change", drawShape);
|
29 |
+
shape.addEventListener("change", drawShape);
|
30 |
+
|
31 |
+
// process message from worker
|
32 |
+
function messageFromWorker(message) {
|
33 |
+
console.log("Main: Message received from worker");
|
34 |
+
// get the status of work
|
35 |
+
if(message.data.status) {
|
36 |
+
console.log(`Main: Message from worker: ${message.data.result}`);
|
37 |
+
}
|
38 |
+
else {
|
39 |
+
console.log(`Main: Worker cannot complete the status for some reason.`)
|
40 |
+
}
|
41 |
+
}
|
42 |
+
|
43 |
+
// event listener to draw a shape
|
44 |
+
function drawShape(event) {
|
45 |
+
const drawData = {
|
46 |
+
command: "draw",
|
47 |
+
selectedColor: color.value.toLowerCase(),
|
48 |
+
selectedShape: shape.value.toLowerCase(),
|
49 |
+
}
|
50 |
+
worker.postMessage(drawData);
|
51 |
+
worker.addEventListener("message", messageFromWorker);
|
52 |
+
}
|
53 |
+
} else {
|
54 |
+
console.log('Your browser doesn\'t support web workers.');
|
55 |
+
}
|
56 |
+
|
draw-web-worker/style.css
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* Basic stylesheet */
|
2 |
+
* {
|
3 |
+
box-sizing: border-box;
|
4 |
+
}
|
5 |
+
html {
|
6 |
+
font-family: Verdana, sans-serif;
|
7 |
+
font-size: 12pt;
|
8 |
+
}
|
9 |
+
footer {
|
10 |
+
border-top: 1px solid lightgrey;
|
11 |
+
font-size: 10pt;
|
12 |
+
}
|
13 |
+
body {
|
14 |
+
width: 100%;
|
15 |
+
}
|
16 |
+
/* Slightly highlight the code */
|
17 |
+
code {
|
18 |
+
background-color: lightgrey;
|
19 |
+
}
|
20 |
+
/* choose all the children from form */
|
21 |
+
form > * {
|
22 |
+
margin: 0.5em 0;
|
23 |
+
}
|
24 |
+
/* canvas */
|
25 |
+
canvas#drawingArea {
|
26 |
+
margin: 0.5em 1.5em;
|
27 |
+
width: 80%;
|
28 |
+
height: 60%;
|
29 |
+
/*border: 1px solid black;*/
|
30 |
+
}
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
draw-web-worker/worker.js
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/**
|
2 |
+
* First, the Worker will get the offscreenCanvas.
|
3 |
+
* On changes to selection, Worker will take the color and shape
|
4 |
+
* and draw on the canvas.
|
5 |
+
*/
|
6 |
+
let offscreenCanvas;
|
7 |
+
let ctx;
|
8 |
+
let workStatus = {
|
9 |
+
status: false,
|
10 |
+
result: ""
|
11 |
+
};
|
12 |
+
// The message will be handled in this function.
|
13 |
+
onmessage = drawShape;
|
14 |
+
|
15 |
+
function drawShape(message) {
|
16 |
+
console.log('Worker: Message received from main script');
|
17 |
+
console.log(`Worker: command ${message.data.command}`)
|
18 |
+
// get the offscreen canvas
|
19 |
+
if(message.data.command==="getOffscreen") {
|
20 |
+
offscreenCanvas = message.data.canvas;
|
21 |
+
ctx = offscreenCanvas.getContext("2d");
|
22 |
+
console.log(`Worker: canvas (w x h): (${offscreenCanvas.width}, ${offscreenCanvas.height}).`);
|
23 |
+
// return a message to indicate the offscreen is successful
|
24 |
+
workStatus.status = true;
|
25 |
+
workStatus.result = "Worker: Got the offscreen canvas.";
|
26 |
+
postMessage(workStatus);
|
27 |
+
}
|
28 |
+
// get the color and shape
|
29 |
+
if(message.data.command==="draw") {
|
30 |
+
const color = message.data.selectedColor;
|
31 |
+
const shape = message.data.selectedShape;
|
32 |
+
console.log(`Worker: Receive order to draw a ${color} ${shape}.`);
|
33 |
+
// draw now
|
34 |
+
// clear the canvas first
|
35 |
+
ctx.clearRect(0, 0, offscreenCanvas.width, offscreenCanvas.height);
|
36 |
+
ctx.fillStyle = color;
|
37 |
+
switch(shape) {
|
38 |
+
case "square":
|
39 |
+
ctx.fillRect(x=0, y=0, w=100, h=100);
|
40 |
+
break;
|
41 |
+
case "rect":
|
42 |
+
ctx.fillRect(x=0, y=0, w=100, h=60);
|
43 |
+
break;
|
44 |
+
case "circle":
|
45 |
+
ctx.beginPath();
|
46 |
+
ctx.arc(x=60, y=60, radius=50,
|
47 |
+
startAngle=0, endAngle=2*Math.PI);
|
48 |
+
ctx.fill();
|
49 |
+
break;
|
50 |
+
}
|
51 |
+
// return a message to indicate it has finished drawing
|
52 |
+
workStatus.status = true;
|
53 |
+
workStatus.result = "Worker: Finish drawing the shape.";
|
54 |
+
postMessage(workStatus);
|
55 |
+
}
|
56 |
+
}
|