File tree Expand file tree Collapse file tree 1 file changed +22
-3
lines changed Expand file tree Collapse file tree 1 file changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,25 @@ function make_environment(env) {
11
11
} ) ;
12
12
}
13
13
14
+ /**
15
+ * Setup correct DPI of canvas element for HiDPI screens.
16
+ *
17
+ * @see : https://web.dev/articles/canvas-hidipi
18
+ */
19
+ function setupCanvas ( canvas ) {
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
+ }
29
+ ctx . scale ( dpr , dpr ) ;
30
+ return ctx ;
31
+ }
32
+
14
33
let iota = 0 ;
15
34
const LOG_ALL = iota ++ ; // Display all logs
16
35
const LOG_TRACE = iota ++ ; // Trace logging, intended for internal use only
@@ -61,11 +80,11 @@ class RaylibJs {
61
80
}
62
81
63
82
const canvas = document . getElementById ( canvasId ) ;
64
- this . ctx = canvas . getContext ( "2d" ) ;
65
- if ( this . ctx === null ) {
66
- throw new Error ( "Could not create 2d canvas context" ) ;
83
+ if ( ! canvas ) {
84
+ throw new Error ( 'Canvas element not found' )
67
85
}
68
86
87
+ this . ctx = setupCanvas ( canvas ) ;
69
88
this . wasm = await WebAssembly . instantiateStreaming ( fetch ( wasmPath ) , {
70
89
env : make_environment ( this )
71
90
} ) ;
You can’t perform that action at this time.
0 commit comments