This project is an Emscripten port of GLFW written in C++ for the web/wasm platform. The currently supported GLFW API is 3.4.
The main goal of this project is to implement as much of the GLFW API that is possible to implement in the context of a web browser.
Since this project is targeting the web/webassembly platform, which runs in more recent web browsers, it is also trying
to focus on using the most recent features and not use deprecated features (for example, uses keyboardEvent.key
vs keyboardEvent.charcode
). As a result, this implementation will most likely not work in older browsers.
Since the code is written in C++, it is trying to minimize the amount of JavaScript code to remain clean and lean.
Main features:
emscripten::glfw3::SetNextWindowCanvasSelector("#canvas2")
to specify which canvas to use)emscripten::glfw3::MakeCanvasResizable(...)
to make the canvas resizable by user.
Use "window"
as the resize selector for full frame canvas (ex: ImGui))[!NOTE] The Comparison page details the differences between this implementation and the Emscripten built-in one.
Check out the live demo of the example code. Note that you need to use a "modern" browser to see it in action. Currently tested on Google Chrome 120+ and Firefox 121+.
The code for the demo is included in this project.
The demo shows 2 canvases each created via a glfwCreateWindow
and shows how they respond to keyboard and mouse events
(using direct apis, like glfwGetMouseButton
or callback apis like glfwSetMouseButtonCallback
)
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE)
)emscripten::glfw3::MakeCanvasResizable(window2, "#canvas2-container", "#canvas2-handle")
)You can enable/disable each window/canvas independently:
The demo uses webgl to render a triangle (the hellow world of gpu rendering...).
Application | Description |
---|---|
WebGPU Shader Toy (src) | WebGPU Shader Toy is a free and open source tool for experimenting with WebGPU fragment shaders and the WebGPU Shader Language (WGSL) |
Example | Note |
---|---|
Demo (src) | Main test/demo which demonstrates most features of the implementation |
example_asyncify (src) | The purpose of this example is to demonstrate how to use asyncify which allows the code to be written like you would for a normal desktop application |
example_hi_dpi (src) | The purpose of this example is to demonstrate how to make the window Hi DPI aware |
example_minimal (src) | The purpose of this example is to be as minimal as possible: initializes glfw, creates a window, then destroys it and terminates glfw. Uses the default shell that comes with emscripten |
example_resizable_container (src) | The purpose of this example is to demonstrate how to make the canvas resizable with another container (a surrounding div) driving its size. The container width is proportional to the size of the window and so as the window gets resized so does the div, and so does the canvas |
example_resizable_container_with_handle (src) | The purpose of this example is to demonstrate how to make the canvas resizable with a container that has a handle. The handle can be dragged around (left mouse drag), and the div is resized accordingly which in turn resizes the canvas, making the canvas truly resizable like a window |
example_resizable_full_window (src) | The purpose of this example is to demonstrate how to make the canvas resizable and occupy the full window |
Since ImGui v1.91.0, ImGui can be configured to use this port, allowing full gamepad and clipboard support amongst many other advantages.
Since Emscripten 3.1.55, using this library is really easy via the Emscripten use-port
option:
--use-port=contrib.glfw3
(no need to clone this repo at all!).
This is the recommended method to use this project.
Example:
emcc --use-port=contrib.glfw3 main.cpp -o build/index.html
With CMake, you need to provide the option both for compile and link phases:
target_compile_options(${target} PUBLIC "--use-port=contrib.glfw3")
target_link_options(${target} PUBLIC "--use-port=contrib.glfw3")
This is an example from ImGui (examples/example_emscripten_wgpu
)
EMS += -s DISABLE_EXCEPTION_CATCHING=1 --use-port=contrib.glfw3
#LDFLAGS += -s USE_GLFW=3 -s USE_WEBGPU=1
LDFLAGS += -s USE_WEBGPU=1
The port can be configured with the following options:
Option | Description |
---|---|
disableJoystick |
Boolean to disable support for joystick entirely, which can be useful if you don't need it due to polling |
disableWarning |
Boolean to disable warnings emitted by the library (for example when using non supported features) |
disableMultiWindow |
Boolean to disable multi window support which makes the code smaller and faster if you don't need it |
Example using disableWarning
and disableMultiWindow
:
emcc --use-port=contrib.glfw3:disableWarning=true:disableMultiWindow=true main.cpp -o build
Note about availability in Emscripten
Emscripten this port 3.1.69 3.4.0.20241004 3.1.66 3.4.0.20240907 3.1.65 3.4.0.20240817 3.1.63 3.4.0.20240627 3.1.60 3.4.0.20240514 3.1.57 3.4.0.20240318 3.1.56 1.1.0 3.1.55 1.0.5 Due to the release cadence of Emscripten, if you want to be in charge of which version you use, you can simply use the port that is checked-in under
port
:--use-port=port/emscripten-glfw3.py
[!NOTE] Emscripten automatically downloads and builds the library for you and stores it in its cache. If you want to delete the library from the cache, you can use the
embuilder
tool:# remove library from cache embuilder clear contrib.glfw3 # remove library from cache (with options) embuilder clear contrib.glfw3:disableWarning=true:disableMultiWindow=true
Check the documentation for details on how to use this implementation, including clipboard, joystick, resizable canvas, Hi DPI, etc... Note that care has been taken to be backward compatible with the pure JavaScript Emscripten built-in implementation.
glfwCreateCursor
)
canvas.toDataURL()
)cursor: url(xxx) xhot yhot, auto
when calling glfwSetCursor
with a custom cursoremscripten::glfw3::OpenURL
)emscripten::glfw3::IsRuntimePlatformApple
),
mostly used for keyboard shortcuts (Ctrl vs. Cmd). GLFW/emscripten_glfw3_version.h
with EMSCRIPTEN_GLFW_VERSION
define for compilation time version detectionEMSCRIPTEN_USE_PORT_CONTRIB_GLFW3
port define now also contains the version[!WARNING] Breaking changes! The clipboard async API has been removed. Note that due to emscripten release cadence, these changes were never part of the emscripten port, so it is unlikely going to affect your project. Check the Clipboard support section for details on how to deal with the clipboard in your application.
nullptr
issue when clipboard is emptyemscripten_glfw_get_clipboard_string
the C version of emscripten::glfw3::GetClipboardString
to
retrieve the clipboard asynchronouslyemscripten::glfw3::FutureClipboardString
to greatly simplify the more frequent use-casesGetClipboardString::value()
now returns the internal clipboard in case of error, instead of throwing exceptionoptimizationLevel
option to the emscripten portemscripten::glfw3
) included with GLFW3/emscripten_glfw3.h
:
std::string_view
/ std::optional<std::string_view>
vs char const *
which may or may not be nullptr
)std::future
)emscripten::glfw3::GetClipboardString
which provides a way of fetching the global
clipboard in a browser environment (glfwGetClipboardString
is not the right API due to the asynchronous nature
of the underlying platform API).glfwSetWindowPosCallback
GLFW_HOVERED
emscripten_glfw_make_canvas_resizable
does not clean up properly_.EM_BOOL
(PR #5)Super/Meta
key, there is no good solution.
The workaround implemented, releases all keys when Super
is released.
Although not a perfect solution, it guarantees that the state is eventually consistent:
Release
state)Pressed
state)GLFW_PRESS
or GLFW_RELEASE
_glfwGetClipboardString
. Note that due to the async (and restrictive) nature of the
navigator.clipboard.readText
call, this synchronous API returns whatever was set via a previous call
to glfwSetClipboardString
and ignores the external clipboard entirely.glfwSetClipboardString
glfwGetWindowContentScale
with nullptr
glfw3native.h
to GLFW
. Although not used (at this moment) by this implementation, this allows
calling code to include it if necessary since it is part of a normal GLFW installation.GLFW_GAMEPAD_AXIS_LEFT_TRIGGER
GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER
, GLFW_GAMEPAD_BUTTON_LEFT_THUMB
and GLFW_GAMEPAD_BUTTON_RIGHT_THUMB
GLFW_GAMEPAD_AXIS_LEFT_TRIGGER
and GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER
are now properly represented as an analog
value in the range [-1.0, +1.0]glfwGetPlatform
and glfwPlatformSupported
uses the GLFW_PLATFORM_EMSCRIPTEN
constant defined in
emscripten-glfw3.h
(officially reserved value is 0x00060006
)glfwInitHint
supports the new hint GLFW_PLATFORM
glfwGetWindowTitle
GLFW_SCALE_FRAMEBUFFER
to enable (resp. disable) Hi DPI supportGLFW_FEATURE_UNAVAILABLE
failure to report this error instead of a warningGLFW_MOUSE_PASSTHROUGH
is not supportedGLFW_CURSOR_CAPTURED
cursor input mode is not supported (not possible in a browser context)glfwInitAllocator
is implemented as noop (could be supported for the C++ part only if there is demand, not javascript)GLFW_POSITION_X
and GLFW_POSITION_Y
are not supported (same as glfwSetWindowPos
)GLFW_ANGLE_PLATFORM_TYPE
is not supported (no direct access in browser, but implementation is most likely using it anyway) GLFW_SCALE_FRAMEBUFFER
), the prior
constant used by this port (GLFW_SCALE_TO_MONITOR
) is still accepted, but it is now deprecated. In addition,
due to the fact that GLFW_SCALE_FRAMEBUFFER
defaults to GLFW_TRUE
, this port is now Hi DPI aware by default and
needs to be explicitly turned off (glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE)
) if this is the desired
behavior.emscripten_glfw3.h
to work as a C filecreate-archive
targetEMSCRIPTEN_GLFW3_DISABLE_JOYSTICK
as a CMake optionEMSCRIPTEN_GLFW3_DISABLE_JOYSTICK
defineEMSCRIPTEN_GLFW3_DISABLE_MULTI_WINDOW_SUPPORT
as a CMake option and made the multi window code conditional
on EMSCRIPTEN_GLFW3_DISABLE_MULTI_WINDOW_SUPPORT
defineGLFW_EMSCRIPTEN_CANVAS_SELECTOR
window hint in favor of a new api emscripten_glfw_set_next_window_canvas_selector
GLFW_EMSCRIPTEN_CANVAS_RESIZE_SELECTOR
and Module.glfwSetCanvasResizableSelector
in favor of a new
api emscripten_glfw_make_canvas_resizable
getWindowPosition
(canvas position in the browser window)glfwSetTime
, glfwGetTimerValue
and glfwGetTimerFrequency
)glfwExtensionSupported
glfwSetWindowTitle
(changes the browser window title)glfwWindowHintString(GLFW_EMSCRIPTEN_CANVAS_RESIZE_SELECTOR, "#canvas2-container")
from c/c++ code or Module.glfwSetCanvasResizableSelector('#canvas2', '#canvas2-container')
from javascript) glfwShowWindow
and glfwHideWindow
)GLFW_FOCUS_ON_SHOW
window hint/attributeGLFW_SCALE_FRAMEBUFFER
can be used in glfwSetWindowAttrib
)glfwSetWindowSizeLimits
and glfwSetWindowAspectRatio
)Module.glfwOnWindowCreated
)EMSCRIPTEN_GLFW3_DISABLE_JOYSTICK
compilation flagGLFW_CURSOR
(handle all use cases: Normal / Hidden / Locked)glfwCreateStandardCursor
and glfwSetCursor
)glfwGetWindowOpacity
and glfwSetWindowOpacity
)glfwSetScrollCallback
)glfwSetCursorEnterCallback
)This project includes the glfw3.h
header (external/GLFW/glfw3.h
) which uses a ZLib license