Skip to content
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