|
12 | 12 | #include <stdbool.h> |
13 | 13 | #include <emscripten/html5.h> |
14 | 14 |
|
| 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 | + |
15 | 53 | // installing mock devicePixelRatio (independent of the browser/screen resolution) |
16 | 54 | static void installMockDevicePixelRatio() { |
17 | 55 | printf("installing mock devicePixelRatio...\n"); |
|
0 commit comments