Skip to content

Commit 7d577cc

Browse files
committed
fix: respect screen DPI
1 parent a71e743 commit 7d577cc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

raylib.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,17 @@ class RaylibJs {
107107
}
108108

109109
InitWindow(width, height, title_ptr) {
110-
this.ctx.canvas.width = width;
111-
this.ctx.canvas.height = height;
110+
// Adjust viewport size according to screen DPI for HiDPI screens.
111+
// see: https://web.dev/articles/canvas-hidipi
112+
const dpi = window.devicePixelRatio || 1;
113+
114+
const { canvas } = this.ctx;
115+
canvas.height = height * dpi;
116+
canvas.width = width * dpi;
117+
canvas.style.height = `${height}px`;
118+
canvas.style.width = `${width}px`;
119+
this.ctx.scale(dpi, dpi);
120+
112121
const buffer = this.wasm.instance.exports.memory.buffer;
113122
document.title = cstr_by_ptr(buffer, title_ptr);
114123
}

0 commit comments

Comments
 (0)