Skip to content

Commit eab0746

Browse files
bsmthteoli2003
andauthored
Apply suggestions from code review
Reviewer feedback from @teoli2003 to adhere to JS guidelines Co-authored-by: Jean-Yves Perrier <jypenator@gmail.com>
1 parent 921d4cd commit eab0746

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

web-workers/offscreen-canvas-worker/index.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ <h1>OffscreenCanvas and worker threads</h1>
4242
<div class="canvases">
4343
<div>
4444
<span class="canvas-title">Canvas A</span>
45-
<canvas id="canvas main" width="200" height="200"></canvas>
45+
<canvas id="main" width="200" height="200"></canvas>
4646
<div>
4747
<button id="main-button" onclick="slowMainThread()">
4848
Block main thread
@@ -51,7 +51,7 @@ <h1>OffscreenCanvas and worker threads</h1>
5151
</div>
5252
<div>
5353
<span class="canvas-title">Canvas B</span>
54-
<canvas id="canvas worker" width="200" height="200"></canvas>
54+
<canvas id="worker" width="200" height="200"></canvas>
5555

5656
<div>
5757
<button id="worker-button" onclick="slowdownWorker()">
@@ -83,18 +83,16 @@ <h1>OffscreenCanvas and worker threads</h1>
8383
</main>
8484

8585
<script>
86-
const requestAnimationFrame = window.requestAnimationFrame;
87-
88-
const canvasA = document.getElementById("canvas main");
89-
const canvasB = document.getElementById("canvas worker");
86+
const canvasA = document.getElementById("main");
87+
const canvasB = document.getElementById("worker");
9088

9189
const ctxA = canvasA.getContext("2d");
9290
const canvasWidth = ctxA.width;
9391
const canvasHeight = ctxA.height;
9492

9593
// Create a counter for Canvas A
9694
let counter = 0;
97-
setInterval(function () {
95+
setInterval(() => {
9896
redrawCanvasA();
9997
counter++;
10098
}, 100);
@@ -109,7 +107,9 @@ <h1>OffscreenCanvas and worker threads</h1>
109107

110108
// This function creates heavy (blocking) work on a thread
111109
function fibonacci(num) {
112-
if (num <= 1) return 1;
110+
if (num <= 1) {
111+
return 1;
112+
}
113113
return fibonacci(num - 1) + fibonacci(num - 2);
114114
}
115115

web-workers/offscreen-canvas-worker/worker.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@ let canvasB = null;
22
let ctxWorker = null;
33

44
// Waiting to receive the OffScreenCanvas
5-
self.onmessage = function (e) {
6-
if (e.data === "slowDown") {
5+
self.onmessage = (event) => {
6+
if (event.data === "slowDown") {
77
fibonacci(42);
88
} else {
9-
canvasB = e.data.canvas;
9+
canvasB = event.data.canvas;
1010
ctxWorker = canvasB.getContext("2d");
1111
startCounting();
1212
}
1313
};
1414

1515
// Fibonacci function to add some delay to the thread
1616
function fibonacci(num) {
17-
if (num <= 1) return 1;
17+
if (num <= 1) {
18+
return 1;
19+
}
1820
return fibonacci(num - 1) + fibonacci(num - 2);
1921
}
2022

2123
// Start the counter for Canvas B
2224
let counter = 0;
2325
function startCounting() {
24-
setInterval(function () {
26+
setInterval(() => {
2527
redrawCanvasB();
2628
counter++;
2729
}, 100);

0 commit comments

Comments
 (0)