Skip to content

Commit dd871a9

Browse files
committed
implemented proper behavior with HiDPI
1 parent 1dbf09c commit dd871a9

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/library_glfw.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,7 +1249,7 @@ var LibraryGLFW = {
12491249
if (canvas.width != wNativeScaled) canvas.width = wNativeScaled;
12501250
if (canvas.height != hNativeScaled) canvas.height = hNativeScaled;
12511251
if (typeof canvas.style != 'undefined') {
1252-
if (wNativeScaled != wNative || hNativeScaled != hNative) {
1252+
if (!GLFW.isCSSScalingEnabled()) {
12531253
canvas.style.setProperty( "width", wNative + "px", "important");
12541254
canvas.style.setProperty("height", hNative + "px", "important");
12551255
} else {
@@ -1259,18 +1259,11 @@ var LibraryGLFW = {
12591259
}
12601260
},
12611261

1262-
// Overrides Browser.calculateMouseCoords to account for hi dpi scaling
1262+
// Overrides Browser.calculateMouseCoords to account for HiDPI scaling and CSS scaling
12631263
calculateMouseCoords(pageX, pageY) {
12641264
// Calculate the movement based on the changes
12651265
// in the coordinates.
1266-
var rect = Module["canvas"].getBoundingClientRect();
1267-
var cw = Module["canvas"].clientWidth;
1268-
var ch = Module["canvas"].clientHeight;
1269-
1270-
if (GLFW.isCSSScalingEnabled()) {
1271-
cw = GLFW.active.width;
1272-
ch = GLFW.active.height;
1273-
}
1266+
const rect = Module["canvas"].getBoundingClientRect();
12741267

12751268
// Neither .scrollX or .pageXOffset are defined in a spec, but
12761269
// we prefer .scrollX because it is currently in a spec draft.
@@ -1285,11 +1278,14 @@ var LibraryGLFW = {
12851278
var adjustedX = pageX - (scrollX + rect.left);
12861279
var adjustedY = pageY - (scrollY + rect.top);
12871280

1288-
// the canvas might be CSS-scaled compared to its backbuffer;
1289-
// SDL-using content will want mouse coordinates in terms
1290-
// of backbuffer units.
1291-
adjustedX = adjustedX * (cw / rect.width);
1292-
adjustedY = adjustedY * (ch / rect.height);
1281+
// getBoundingClientRect() returns dimension affected by CSS, so as a result:
1282+
// - when CSS scaling is enabled, this will fix the mouse coordinates to match the width/height of the window
1283+
// - otherwise the CSS width/height are forced to the width/height of the GLFW window (see updateCanvasDimensions),
1284+
// so there is no need to adjust the position
1285+
if (GLFW.isCSSScalingEnabled() && GLFW.active) {
1286+
adjustedX = adjustedX * (GLFW.active.width / rect.width);
1287+
adjustedY = adjustedY * (GLFW.active.height / rect.height);
1288+
}
12931289

12941290
return { x: adjustedX, y: adjustedY };
12951291
},

0 commit comments

Comments
 (0)