Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect device screen DPI #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions raylib.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class RaylibJs {
#FONT_SCALE_MAGIC = 0.65;

#reset() {
this.height = 0;
this.width = 0;
this.dpi = window.devicePixelRatio || 1;
this.previous = undefined;
this.wasm = undefined;
this.ctx = undefined;
Expand Down Expand Up @@ -107,8 +110,21 @@ class RaylibJs {
}

InitWindow(width, height, title_ptr) {
this.ctx.canvas.width = width;
this.ctx.canvas.height = height;
// Adjust viewport size according to screen DPI for HiDPI screens.
// see: https://web.dev/articles/canvas-hidipi
const { canvas } = this.ctx;
canvas.height = height * this.dpi;
canvas.width = width * this.dpi;
canvas.style.height = `${height}px`;
canvas.style.width = `${width}px`;
this.ctx.scale(this.dpi, this.dpi);

// Store original size for GetScreenWidth and GetScreenHeight calls.
// Necessary as some platforms allow fractional scaling (e.g 1.3)
// and dividing canvas size by DPR can cause rounding issues.
this.height = height;
this.width = width;

const buffer = this.wasm.instance.exports.memory.buffer;
document.title = cstr_by_ptr(buffer, title_ptr);
}
Expand All @@ -123,11 +139,11 @@ class RaylibJs {
}

GetScreenWidth() {
return this.ctx.canvas.width;
return this.width;
}

GetScreenHeight() {
return this.ctx.canvas.height;
return this.height;
}

GetFrameTime() {
Expand Down