Skip to content

Commit 861fc4b

Browse files
committed
added documentation for HiDPI
- describe why CSS scaling cannot work in this mode
1 parent dd871a9 commit 861fc4b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/browser/test_glfw3_hi_dpi_aware.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,44 @@
1212
#include <stdbool.h>
1313
#include <emscripten/html5.h>
1414

15+
/**
16+
* History:
17+
* - HiDPI awareness is a feature that was added in 3.1.51 / December 2023
18+
* - It allows to render the canvas for modern 4k/retina screens in hi resolution
19+
* - It behaves similarly to the way it is handled on the native desktop platform:
20+
* - `glfwGetWindowSize` returns the size of the GLFW window (as set via `glfwCreateWindow` or `glfwSetWindowSize`)
21+
* - `glfwGetFramebufferSize` returns the actual size of the framebuffer and is usually used for the viewport (ex: `glViewport(0, 0, fbWidth, fbHeight)`)
22+
*
23+
* The feature is enabled via a window hint: `glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE)`
24+
*
25+
* From an implementation point of view, the actual size of the canvas (canvas.width and canvas.height) is defined to be the framebuffer size.
26+
* CSS is used to scale the canvas to the GLFW window size. The factor used for HiDPI scaling is `devicePixelRatio`.
27+
*
28+
* For example, assuming a window size of 640x480 and a devicePixelRatio of 2, the canvas element handled by the library would look like this:
29+
* <canvas width="1280" height="960" style="width: 640; height: 480;">
30+
*
31+
* Important note: when HiDPI awareness is enabled, the library takes over the size of the canvas, in particular its CSS width and height.
32+
* As a result, CSS Scaling (see `test_glfw3_css_scaling.c` for details) is disabled.
33+
* If you need the canvas to be resized dynamically (for example when the browser window is resized),
34+
* you can use a different HTML element with CSS sizing and set a callback to update the size of the canvas appropriately.
35+
*
36+
* Example:
37+
*
38+
* GLFWWindow *window = ...;
39+
* emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, window, false, OnResize);
40+
*
41+
* // assuming HTML like this <div id="canvas-container" style="width: 100%"><canvas id="canvas"></div>
42+
* static EM_BOOL OnResize(int event_type, const EmscriptenUiEvent* event, void* user_data) {
43+
* GLFWWindow *window = (GLFWWindow*) user_data;
44+
* double w, h;
45+
* // use ANOTHER HTML element
46+
* emscripten_get_element_css_size("#canvas-container", &w, &h);
47+
* // set the size of the GLFW window accordingly
48+
* glfwSetWindowSize(window, (int)w, (int)h);
49+
* return true;
50+
* }
51+
*/
52+
1553
// installing mock devicePixelRatio (independent of the browser/screen resolution)
1654
static void installMockDevicePixelRatio() {
1755
printf("installing mock devicePixelRatio...\n");

0 commit comments

Comments
 (0)