Skip to content
Merged
38 changes: 34 additions & 4 deletions src/library_glfw.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,15 @@ var LibraryGLFW = {

// Get non alive id
const canvas = Module['canvas'];
var win = new GLFW_Window(id, canvas.clientWidth, canvas.clientHeight, canvas.width, canvas.height, title, monitor, share);

var clientWidth = canvas.clientWidth;
var clientHeight = canvas.clientHeight;
if (GLFW.isCSSScalingEnabled()) {
clientWidth = width;
clientHeight = height;
}

var win = new GLFW_Window(id, clientWidth, clientHeight, canvas.width, canvas.height, title, monitor, share);

// Set window to array
if (id - 1 == GLFW.windows.length) {
Expand Down Expand Up @@ -1266,6 +1274,11 @@ var LibraryGLFW = {
var cw = Module["canvas"].clientWidth;
var ch = Module["canvas"].clientHeight;

if (GLFW.isCSSScalingEnabled()) {
cw = GLFW.active.width;
ch = GLFW.active.height;
}

// Neither .scrollX or .pageXOffset are defined in a spec, but
// we prefer .scrollX because it is currently in a spec draft.
// (see: http://www.w3.org/TR/2013/WD-cssom-view-20131217/)
Expand Down Expand Up @@ -1308,10 +1321,27 @@ var LibraryGLFW = {
return false;
},

/**
* CSS Scaling is a feature that is NOT part of the GLFW API, but for historical reasons, it is available
* in Emscripten.
* It is enabled by default but can be disabled by setting Module['EMSCRIPTEN_GLFW_DISABLE_CSS_SCALING'] to true.
* It is automatically disabled when using Hi DPI (the library overrides CSS sizes). */
isCSSScalingEnabled() {
return Module['EMSCRIPTEN_GLFW_DISABLE_CSS_SCALING'] !== true && !GLFW.isHiDPIAware();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm somewhat loath to add more incoming module API elements like this. Can we perhaps wait until somebody asks for this setting?

If we do end up adding it it would need to be added to INCOMING_MODULE_JS_API and then this line should be wrapped in #if expectToReceiveOnModule('INCOMING_MODULE_JS_API').

I simpler more pay-as-you-go option could be to add some kind of new API for this either via glfwSetWindowParam or some new C API.

I wonder if we can add a test for this behaviour? Can we perhaps make a simple reftest that shows that the GLFW windows is scaled to the size of the canvas? I'm not sure how hard that would be.

},

adjustCanvasDimensions() {
const canvas = Module['canvas'];
Browser.updateCanvasDimensions(canvas, canvas.clientWidth, canvas.clientHeight);
Browser.updateResizeListeners();
if (GLFW.active) {
const canvas = Module['canvas'];
var clientWidth = canvas.clientWidth;
var clientHeight = canvas.clientHeight;
if (GLFW.isCSSScalingEnabled()) {
clientWidth = GLFW.active.width;
clientHeight = GLFW.active.height;
}
Browser.updateCanvasDimensions(canvas, clientWidth, clientHeight);
Browser.updateResizeListeners();
}
},

getHiDPIScale() {
Expand Down
Loading