Skip to content

[core] FLAG_WINDOW_RESIZABLE has no effect on PLATFORM_WEB #3231

@Peter0x44

Description

@Peter0x44

Issue description

FLAG_WINDOW_RESIZABLE has no effect on PLATFORM_WEB

Issue Screenshot

Screenshot_20230808_012136

Code Example

#include "raylib.h"

#ifdef __EMSCRIPTEN__
    #include <emscripten/emscripten.h>
#endif

const int screenWidth = 800;
const int screenHeight = 450;

void UpdateDrawFrame(void);


int main(void)
{
    SetConfigFlags(FLAG_WINDOW_RESIZABLE);
    InitWindow(screenWidth, screenHeight, "web resizing example");

#ifdef __EMSCRIPTEN__
    emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
    SetTargetFPS(60);

    while (!WindowShouldClose())
    {
        UpdateDrawFrame();
    }
#endif

    CloseWindow();

    return 0;
}

void UpdateDrawFrame(void)
{
    static Vector2 ballPosition = (Vector2) { screenWidth/2.0f, screenHeight/2.0f };
    static Vector2 ballSpeed = (Vector2) { 6.0f, 4.0f };
    const float ballRadius = 10.0f;

    ballPosition.x += ballSpeed.x;
    ballPosition.y += ballSpeed.y;

    if (((ballPosition.x + ballRadius) > GetScreenWidth()) || ((ballPosition.x - ballRadius) < 0)) ballSpeed.x *= -1.0f;
    if (((ballPosition.y + ballRadius) > GetScreenHeight()) || ((ballPosition.y - ballRadius) < 0)) ballSpeed.y *= -1.0f;

    BeginDrawing();

        ClearBackground(GREEN);

        DrawCircleV(ballPosition, ballRadius, RED);

    EndDrawing();
}

compile with:
emcc resize_example.c -I path/to/raylib/src ./libraylib.a -s USE_GLFW=3 -s WASM=1 --shell-file minshell.html -o resize_example.html

The following 3 lines seem to do nothing:

raylib/src/rcore.c

Lines 6176 to 6178 in db55bed

int width = GetCanvasWidth();
int height = GetCanvasHeight();
emscripten_set_canvas_element_size("#canvas",width,height);

They set the height of the canvas... to the existing height/width of the canvas, so they are unchanged
The callback they are in is unused, anyway

the following patch seems to fix the issue:

diff --git a/src/rcore.c b/src/rcore.c
index 45495bb1..5b29a5bf 100644
--- a/src/rcore.c
+++ b/src/rcore.c
@@ -925,9 +925,9 @@ void InitWindow(int width, int height, const char *title)
     // Check fullscreen change events(note this is done on the window since most browsers don't support this on #canvas)
     //emscripten_set_fullscreenchange_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenResizeCallback);
     // Check Resize event (note this is done on the window since most browsers don't support this on #canvas)
-    //emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenResizeCallback);
+    emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, EmscriptenResizeCallback);
     // Trigger this once to get initial window sizing
-    //EmscriptenResizeCallback(EMSCRIPTEN_EVENT_RESIZE, NULL, NULL);
+    EmscriptenResizeCallback(EMSCRIPTEN_EVENT_RESIZE, NULL, NULL);
 
     // Support keyboard events -> Not used, GLFW.JS takes care of that
     //emscripten_set_keypress_callback("#canvas", NULL, 1, EmscriptenKeyboardCallback);
@@ -6042,8 +6042,8 @@ static EM_BOOL EmscriptenResizeCallback(int eventType, const EmscriptenUiEvent *
 
     // This event is called whenever the window changes sizes,
     // so the size of the canvas object is explicitly retrieved below
-    int width = GetCanvasWidth();
-    int height = GetCanvasHeight();
+    int width = event->windowInnerWidth;
+    int height = event->windowInnerHeight;
     emscripten_set_canvas_element_size("#canvas",width,height);
 
     SetupViewport(width, height);    // Reset viewport and projection matrix for new size

It uncomments the lines that set the callback, and sets the canvas width/height to the windowInnerWidth and windowInnerHeight

The result:

resizer.mp4

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions