Skip to content

Commit bab234a

Browse files
authored
Merge pull request #4945 from Moros1138/master
[rcore][web] Review resizable canvas on `FLAG_WINDOW_RESIZABLE`
2 parents 13e384c + db2abfb commit bab234a

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

src/platforms/rcore_web.c

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,15 @@ int InitPlatform(void)
11321132
if ((CORE.Window.flags & FLAG_WINDOW_UNDECORATED) > 0) glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // Border and buttons on Window
11331133
else glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); // Decorated window
11341134

1135-
if ((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) > 0) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Resizable window
1135+
if ((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) > 0)
1136+
{
1137+
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Resizable window
1138+
1139+
// bypass hidpi code block in libglfw.js
1140+
// https://github.com/raysan5/raylib/pull/4945#issuecomment-2906956170
1141+
//------------------------------------------------------------------------
1142+
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
1143+
}
11361144
else glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // Avoid window being resizable
11371145

11381146
// Disable FLAG_WINDOW_MINIMIZED, not supported on initialization
@@ -1625,15 +1633,67 @@ static void MouseEnterCallback(GLFWwindow *window, int enter)
16251633
static EM_BOOL EmscriptenKeyboardCallback(int eventType, const EmscriptenKeyboardEvent *keyboardEvent, void *userData)
16261634
{
16271635
// WARNING: Keyboard inputs already processed through GLFW callback
1628-
1636+
1637+
// NOTE: 1. Reset the fullscreen flags if the user left fullscreen manually by pressing the Escape key
1638+
// 2. Which is a necessary safeguard because that case will bypass the toggles CORE.Window.flags resets
1639+
if (platform.ourFullscreen) platform.ourFullscreen = false;
1640+
else
1641+
{
1642+
const bool wasFullscreen = EM_ASM_INT( { if (document.fullscreenElement) return 1; }, 0);
1643+
if (!wasFullscreen)
1644+
{
1645+
CORE.Window.fullscreen = false;
1646+
CORE.Window.flags &= ~FLAG_FULLSCREEN_MODE;
1647+
CORE.Window.flags &= ~FLAG_BORDERLESS_WINDOWED_MODE;
1648+
}
1649+
}
1650+
1651+
// Trigger resize event after a brief pause to ensure the canvas exists to resize
1652+
EM_ASM({ setTimeout(function() { window.dispatchEvent(new Event("resize")); }, 50); });
1653+
16291654
return 1; // The event was consumed by the callback handler
16301655
}
16311656
*/
16321657

16331658
// Emscripten: Called on mouse input events
16341659
static EM_BOOL EmscriptenMouseCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
16351660
{
1636-
// This is only for registering mouse click events with emscripten and doesn't need to do anything
1661+
// NOTE: Current code solves mouse position problems with the 3 possible approaches to canvas scaling
1662+
// 1. Canvas not scaled, framebuffer size is fixed
1663+
// 2. Canvas is scaled to 100% browser size, framebuffer size is fixed
1664+
// 3. Canvas is resized to browser size, framebuffer is resized
1665+
1666+
// Don't resize non-resizeable windows
1667+
if ((CORE.Window.flags & FLAG_WINDOW_RESIZABLE) == 0) return 1;
1668+
1669+
// This event is called whenever the window changes sizes,
1670+
// so the size of the canvas object is explicitly retrieved below
1671+
int width = EM_ASM_INT( return window.innerWidth; );
1672+
int height = EM_ASM_INT( return window.innerHeight; );
1673+
1674+
if (width < (int)CORE.Window.screenMin.width) width = CORE.Window.screenMin.width;
1675+
else if ((width > (int)CORE.Window.screenMax.width) && (CORE.Window.screenMax.width > 0)) width = CORE.Window.screenMax.width;
1676+
1677+
if (height < (int)CORE.Window.screenMin.height) height = CORE.Window.screenMin.height;
1678+
else if ((height > (int)CORE.Window.screenMax.height) && (CORE.Window.screenMax.height > 0)) height = CORE.Window.screenMax.height;
1679+
1680+
emscripten_set_canvas_element_size(GetCanvasId(), width, height);
1681+
1682+
glfwSetWindowSize(platform.handle, width, height); // Inform glfw of the new size
1683+
1684+
SetupViewport(width, height); // Reset viewport and projection matrix for new size
1685+
1686+
CORE.Window.currentFbo.width = width;
1687+
CORE.Window.currentFbo.height = height;
1688+
CORE.Window.resizedLastFrame = true;
1689+
1690+
if (IsWindowFullscreen()) return 1;
1691+
1692+
// Set current screen size
1693+
CORE.Window.screen.width = width;
1694+
CORE.Window.screen.height = height;
1695+
1696+
// WARNING: RenderTextures are not scaled to new size
16371697

16381698
return 1; // The event was consumed by the callback handler
16391699
}

0 commit comments

Comments
 (0)