1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
3
< head >
4
- < meta charset ="utf-8 " />
5
4
< meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
6
5
< title > OffscreenCanvas API</ title >
7
6
< link rel ="stylesheet " href ="style.css " />
14
13
< main >
15
14
< h2 > Simulate a heavy process:</ h2 >
16
15
17
- < button onclick ="fibonacci(40 ) "> Canvas A</ button >
18
- < button onclick ="slowdownWorker() "> Canvas B</ button >
16
+ < button id =" main-button " onclick ="slowMainThread( ) "> Canvas A</ button >
17
+ < button id =" worker-button " onclick ="slowdownWorker() "> Canvas B</ button >
19
18
20
- < div >
21
- < button > Test button</ button > < button > Test button</ button
22
- > < button > Test button</ button > < button > Test button</ button
23
- > < button > Test button</ button > < button > Test button</ button >
19
+ < div class ="ui-example ">
20
+ < button > Hover example</ button > < button > Hover example</ button
21
+ > < button > Hover example</ button > < button > Hover example</ button >
24
22
</ div >
25
23
26
- < h2 > Visualization :</ h2 >
24
+ < h2 > Counters :</ h2 >
27
25
< div class ="visualisation ">
28
26
< div >
29
27
< span class ="canvas-title "> Canvas A</ span >
@@ -48,41 +46,33 @@ <h2>Visualization:</h2>
48
46
? "Your browser supports the OffscreenCanvas API "
49
47
: "Your browser does not support OffscreenCanvas" ;
50
48
51
- var requestAnimationFrame =
52
- window . requestAnimationFrame ||
53
- window . mozRequestAnimationFrame ||
54
- window . webkitRequestAnimationFrame ||
55
- window . msRequestAnimationFrame ;
49
+ const requestAnimationFrame = window . requestAnimationFrame ;
56
50
57
51
// Init Canvas A (running on the current page)
58
- var canvasA = document . getElementById ( "canvas main" ) ;
59
- var ctxA = canvasA . getContext ( "2d" ) ;
52
+ const canvasA = document . getElementById ( "canvas main" ) ;
53
+ const indicator = document . getElementById ( "indicator" ) ;
54
+ const ctxA = canvasA . getContext ( "2d" ) ;
60
55
61
- var canvasWidth = ctxA . width ;
62
- var canvasHeight = ctxA . height ;
56
+ const canvasWidth = ctxA . width ;
57
+ const canvasHeight = ctxA . height ;
58
+
59
+ // Setup the counter for Canvas A
60
+ let counter = 0 ;
61
+ setInterval ( function ( ) {
62
+ redrawCanvasA ( ) ;
63
+ counter ++ ;
64
+ } , 100 ) ;
63
65
64
66
// Redraw Canvas A text
65
67
function redrawCanvasA ( ) {
66
- ctxA . clearRect ( 0 , 0 , canvasWidth , canvasHeight ) ;
67
-
68
- // color in the background
69
- ctxA . fillStyle = "#EEEEEE" ;
70
- ctxA . fillRect ( 0 , 0 , canvasWidth , canvasHeight ) ;
71
-
72
- // draw the circle
73
- ctxA . beginPath ( ) ;
74
-
75
- var radius = 25 + 20 * Math . abs ( Math . cos ( angle ) ) ;
76
- ctxA . arc ( 70 , 70 , radius , 0 , Math . PI * 2 , false ) ;
77
- ctxA . closePath ( ) ;
78
-
79
- // color in the circle
80
- ctxA . fillStyle = "#006699" ;
81
- ctxA . fill ( ) ;
82
-
83
- angle += Math . PI / 64 ;
84
-
85
- requestAnimationFrame ( drawCircle ) ;
68
+ ctxA . clearRect ( 0 , 0 , canvasA . width , canvasA . height ) ;
69
+ ctxA . font = "16px Verdana" ;
70
+ ctxA . textAlign = "center" ;
71
+ ctxA . fillText (
72
+ "Counter: " + counter ,
73
+ canvasA . width / 2 ,
74
+ canvasA . height / 2
75
+ ) ;
86
76
}
87
77
88
78
// Using the OffscreenCanvas API and starting the worker
@@ -98,6 +88,10 @@ <h2>Visualization:</h2>
98
88
return fibonacci ( num - 1 ) + fibonacci ( num - 2 ) ;
99
89
}
100
90
91
+ function slowMainThread ( ) {
92
+ fibonacci ( 42 ) ;
93
+ }
94
+
101
95
// Initiate the slowing down of Canvas B
102
96
// By sending a message to the worker
103
97
function slowdownWorker ( ) {
0 commit comments