@@ -12,22 +12,15 @@ function make_environment(env) {
12
12
}
13
13
14
14
/**
15
- * Setup correct DPI of canvas element for HiDPI screens.
15
+ * Adjust canvas size according to screen DPI.
16
16
*
17
17
* @see : https://web.dev/articles/canvas-hidipi
18
18
*/
19
- function setupCanvas ( canvas ) {
19
+ function adjustCanvasSize ( ctx , height , width ) {
20
20
const dpr = window . devicePixelRatio || 1 ;
21
- const { height, width } = canvas . getBoundingClientRect ( ) ;
22
- canvas . height = height * dpr ;
23
- canvas . width = width * dpr ;
24
-
25
- const ctx = canvas . getContext ( '2d' ) ;
26
- if ( ! ctx ) {
27
- throw new Error ( "Could not create 2d canvas context" )
28
- }
21
+ ctx . canvas . height = height * dpr ;
22
+ ctx . canvas . width = width * dpr ;
29
23
ctx . scale ( dpr , dpr ) ;
30
- return ctx ;
31
24
}
32
25
33
26
let iota = 0 ;
@@ -85,6 +78,13 @@ class RaylibJs {
85
78
}
86
79
87
80
this . ctx = setupCanvas ( canvas ) ;
81
+ if ( ! this . ctx ) {
82
+ throw new Error ( "Could not create 2d canvas context" )
83
+ }
84
+
85
+ const { height, width } = ctx . canvas . getBoundingClientRect ( ) ;
86
+ adjustCanvasSize ( ctx , height , width ) ;
87
+
88
88
this . wasm = await WebAssembly . instantiateStreaming ( fetch ( wasmPath ) , {
89
89
env : make_environment ( this )
90
90
} ) ;
@@ -126,8 +126,7 @@ class RaylibJs {
126
126
}
127
127
128
128
InitWindow ( width , height , title_ptr ) {
129
- this . ctx . canvas . width = width ;
130
- this . ctx . canvas . height = height ;
129
+ adjustCanvasSize ( this . ctx , height , width ) ;
131
130
const buffer = this . wasm . instance . exports . memory . buffer ;
132
131
document . title = cstr_by_ptr ( buffer , title_ptr ) ;
133
132
}
0 commit comments