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:
- can create as many windows as you want, each one associated to a different canvas (use
emscripten::glfw3::SetNextWindowCanvasSelector("#canvas2")to specify which canvas to use) - resizable window/canvas (use 
emscripten::glfw3::MakeCanvasResizable(...)to make the canvas resizable by user. Use"window"as the resize selector for full frame canvas (ex: ImGui)) - mouse (includes sticky button behavior) & touch
 - keyboard (includes sticky key behavior and Meta key workaround)
 - joystick/gamepad
 - fullscreen
 - Hi DPI
 - all GLFW cursors and custom cursors
 - window opacity
 - size constraints (size limits and aspect ratio)
 - visibility
 - focus
 - clipboard (cut/copy/paste with external clipboard)
 - timer
 - pthread
 
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)
- canvas1 is hi dpi aware (
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_TRUE)) - canvas2 is not hi dpi aware (but can be made so with the "Enable" Hi DPI Aware button)
 - canvas2 is fully resizable (use the square handle to resize) (
emscripten::glfw3::MakeCanvasResizable(window2, "#canvas2-container", "#canvas2-handle")) 
You can enable/disable each window/canvas independently:
- When 2 (or more) canvases are present, the canvas that has focus can receive keyboard events. If no other element on the page has focus, then the last canvas that had the focus will receive these events. Clicking with the left mouse button on a canvas gives it focus.
 - When there is only 1 canvas, the implementation tries to be smart about it and will route keyboard (and other relevant) events to the single canvas if nothing else has focus (the 'Change focus/Text' field is used to test this feature since clicking on the text field grabs the focus).
 
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 | 
| example_pthread (src) | The purpose of this example is to demonstrate how to use pthread. It creates 2 threads that are incrementing a counter to simulate work on separate threads. Note that this test will only work if the proper HTTP headers are set. Github does not set these headers. | 
| example_offscreen_canvas (src) | The purpose of this example is to demonstrate how to use an offscreen canvas. Note that this test will only work if the proper HTTP headers are set. Github does not set these headers. | 
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.htmlWith 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=1The 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 | 
disableWebGL2 | 
Boolean to disable support for WebGL2 (which makes the code smaller) if you don't need it | 
Example using disableWarning and disableMultiWindow:
emcc --use-port=contrib.glfw3:disableWarning=true:disableMultiWindow=true main.cpp -o build
Emscripten this port TBD 3.4.0.20250305 4.0.4 3.4.0.20250209 4.0.2 3.4.0.20250117 4.0.1 3.4.0.20250112 4.0.0 3.4.0.20241230 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 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=trueCheck 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.
Check the Building page for details on how to build this project.
- Fixed missing 
ikey in keyboard mapping. Thanks, @iarwain for PR #18. 
- Added port option 
disableWebGL2to disable support for WebGL2 when not needed (reduces JavaScript code size) 
- Added support for 
GLFW_CONTEXT_VERSION_MAJORandGLFW_CONTEXT_VERSION_MINOR - Re-enable GL extensions by default (regression introduced in 3.4.0.20241230). Fixes #13.
 - Changed port to automatically set the proper Emscripten flags to enable 
GLFW_CONTEXT_VERSION_MAJOR. Fixes #14. 
- Added support for offscreen canvas (check example_offscreen_canvas for a comprehensive example)
 
- Added support for pthread (check example_pthread for a comprehensive example)
 
- Implemented custom cursors (
glfwCreateCursor)- uses a canvas to draw the cursor image into it and convert it into a URL (
canvas.toDataURL()) - uses the CSS property syntax: 
cursor: url(xxx) xhot yhot, autowhen callingglfwSetCursorwith a custom cursor 
 - uses a canvas to draw the cursor image into it and convert it into a URL (
 
- Fixed Meta/Super key workaround to also handle the rare (but possible) use-case of having other keys down before pressing the Meta/Super key
 
- Added a way to tweak the timeouts for the Super + Key workaround (Super is also known as Meta or Cmd)
 - Added a way to set which keys are allowed to be handled by the browser
 - Added a convenient API to open a URL (
emscripten::glfw3::OpenURL) - Added a convenient API to detect if the runtime platform is Apple (
emscripten::glfw3::IsRuntimePlatformApple), mostly used for keyboard shortcuts (Ctrl vs. Cmd). - Added 
GLFW/emscripten_glfw3_version.hwithEMSCRIPTEN_GLFW_VERSIONdefine for compilation time version detection EMSCRIPTEN_USE_PORT_CONTRIB_GLFW3port 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.
- Fixed 
nullptrissue when clipboard is empty - Fixed the internal clipboard being wiped on asynchronous callback error
 
- Added 
emscripten_glfw_get_clipboard_stringthe C version ofemscripten::glfw3::GetClipboardStringto retrieve the clipboard asynchronously - Added a helper class 
emscripten::glfw3::FutureClipboardStringto greatly simplify the more frequent use-cases GetClipboardString::value()now returns the internal clipboard in case of error, instead of throwing exception- Added 
optimizationLeveloption to the emscripten port 
- Introduced C++ API (namespace 
emscripten::glfw3) included withGLFW3/emscripten_glfw3.h:- provides a more correct API with sensible defaults (ex: 
std::string_view/std::optional<std::string_view>vschar const *which may or may not benullptr) - allow for C++ only API (ex: 
std::future) - the C API is still available if you would rather stick to it
 
 - provides a more correct API with sensible defaults (ex: 
 - Implemented  
emscripten::glfw3::GetClipboardStringwhich provides a way of fetching the global clipboard in a browser environment (glfwGetClipboardStringis not the right API due to the asynchronous nature of the underlying platform API). - The cursor position is no longer clamped to the window size, and as a result, can have negative values or values greater than the window size. Note that GLFW implements a similar behavior on the macOS desktop platform.
 - Implemented 
glfwSetWindowPosCallback - Added support for GLFW Window Attribute 
GLFW_HOVERED - Fixed #6: 
emscripten_glfw_make_canvas_resizabledoes not clean up properly. - Fixed an issue with opacity: when using opacity, the handle is not working unless its z-index is higher than the canvas z-index
 
- Fixed internal implementation to use 
EM_BOOL(PR #5) 
- Implemented workaround for #4: Using Super + "Key" on macOS results in "Key" not being released.
Due to the broken state of
JavaScript handling the 
Super/Metakey, there is no good solution. The workaround implemented, releases all keys whenSuperis released. Although not a perfect solution, it guarantees that the state is eventually consistent:- if "Key" was released while "Super" was held, then when "Super" gets released, "Key" is released
(later than when actually released, final state is consistent: "Key" in 
Releasestate) - if "Key" is still held when "Super" is released, "Key" is released when "Super" gets released,
but immediately gets a down event (Up/Down event, final state is consistent": "Key" in 
Pressedstate) 
 - if "Key" was released while "Super" was held, then when "Super" gets released, "Key" is released
(later than when actually released, final state is consistent: "Key" in 
 
- Fixed #3: glfwGetKey must return one of 
GLFW_PRESSorGLFW_RELEASE 
- Implemented 
glfwGetClipboardString. Note that due to the async (and restrictive) nature of thenavigator.clipboard.readTextcall, this synchronous API returns whatever was set via a previous call toglfwSetClipboardStringand ignores the external clipboard entirely. 
- Fixed #2: Dynamically changing HiDPI awareness does not trigger content callback
 
- Implemented 
glfwSetClipboardString 
- Fixed issue when calling 
glfwGetWindowContentScalewithnullptr - Renamed javascript api/impl since these names are not unique to avoid potential conflicts
 - Added 
glfw3native.htoGLFW. 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. 
- Fixed joystick/gamepad code that was improperly mapping 
GLFW_GAMEPAD_AXIS_LEFT_TRIGGERGLFW_GAMEPAD_AXIS_RIGHT_TRIGGER,GLFW_GAMEPAD_BUTTON_LEFT_THUMBandGLFW_GAMEPAD_BUTTON_RIGHT_THUMB GLFW_GAMEPAD_AXIS_LEFT_TRIGGERandGLFW_GAMEPAD_AXIS_RIGHT_TRIGGERare now properly represented as an analog value in the range [-1.0, +1.0]- Please note the change in version numbering, which from now on will be tied to the GLFW version implemented + date of release of this port to avoid confusion
 
- Upgraded to GLFW 3.4
 - GLFW 3.4 features implemented
glfwGetPlatformandglfwPlatformSupporteduses theGLFW_PLATFORM_EMSCRIPTENconstant defined inemscripten-glfw3.h(officially reserved value is0x00060006)glfwInitHintsupports the new hintGLFW_PLATFORM- Supports all 10 cursors
 - Implemented 
glfwGetWindowTitle - Use 
GLFW_SCALE_FRAMEBUFFERto enable (resp. disable) Hi DPI support - Changed the functions that can report 
GLFW_FEATURE_UNAVAILABLEfailure to report this error instead of a warning 
 - GLFW 3.4 features not implemented
GLFW_MOUSE_PASSTHROUGHis not supportedGLFW_CURSOR_CAPTUREDcursor input mode is not supported (not possible in a browser context)glfwInitAllocatoris implemented as noop (could be supported for the C++ part only if there is demand, not javascript)GLFW_POSITION_XandGLFW_POSITION_Yare not supported (same asglfwSetWindowPos)GLFW_ANGLE_PLATFORM_TYPEis not supported (no direct access in browser, but implementation is most likely using it anyway)
 - Since GLFW 3.4 introduces a proper constant to handle Hi DPI Awareness (
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 thatGLFW_SCALE_FRAMEBUFFERdefaults toGLFW_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. 
- Fixed memory corruption with joystick
 
- Fixed version string
 
- Fixed 
emscripten_glfw3.hto work as a C file - Added 
create-archivetarget 
- Added 
EMSCRIPTEN_GLFW3_DISABLE_JOYSTICKas a CMake option - Made joystick code truly conditional on 
EMSCRIPTEN_GLFW3_DISABLE_JOYSTICKdefine - Added 
EMSCRIPTEN_GLFW3_DISABLE_MULTI_WINDOW_SUPPORTas a CMake option and made the multi window code conditional onEMSCRIPTEN_GLFW3_DISABLE_MULTI_WINDOW_SUPPORTdefine - Misc: added GitHub workflow to compile the code/display badge
 
- Fixed import
 
- First 1.0.0 release
 - Added examples
 - Added documentation
 - Fixed some issues
 - Removed 
GLFW_EMSCRIPTEN_CANVAS_SELECTORwindow hint in favor of a new apiemscripten_glfw_set_next_window_canvas_selector - Removed 
GLFW_EMSCRIPTEN_CANVAS_RESIZE_SELECTORandModule.glfwSetCanvasResizableSelectorin favor of a new apiemscripten_glfw_make_canvas_resizable - This new api also offer the ability to deal with the handle automatically
 - Implemented 
getWindowPosition(canvas position in the browser window) - Implemented all timer apis (
glfwSetTime,glfwGetTimerValueandglfwGetTimerFrequency) - Implemented 
glfwExtensionSupported - Implemented 
glfwSetWindowTitle(changes the browser window title) 
- Added support for resizable canvas (
glfwWindowHintString(GLFW_EMSCRIPTEN_CANVAS_RESIZE_SELECTOR, "#canvas2-container")from c/c++ code orModule.glfwSetCanvasResizableSelector('#canvas2', '#canvas2-container')from javascript) - Added support fo visibility (
glfwShowWindowandglfwHideWindow) - Added support for 
GLFW_FOCUS_ON_SHOWwindow hint/attribute - Added support for dynamic Hi DPI Awareness (
GLFW_SCALE_FRAMEBUFFERcan be used inglfwSetWindowAttrib) - Added support for "sticky" mouse button and keyboard
 - Added support for window size constraints (
glfwSetWindowSizeLimitsandglfwSetWindowAspectRatio) - Added support for providing a callback function in javascript to be notified when a window is created (
Module.glfwOnWindowCreated) 
- Added support for joystick/gamepad
- Joystick support can be disabled via 
EMSCRIPTEN_GLFW3_DISABLE_JOYSTICKcompilation flag 
 - Joystick support can be disabled via 
 
- Added support for input mode 
GLFW_CURSOR(handle all use cases: Normal / Hidden / Locked) - Added support for glfw defined cursors (implemented 
glfwCreateStandardCursorandglfwSetCursor) - Added support for window opacity (implemented 
glfwGetWindowOpacityandglfwSetWindowOpacity) 
- remembers the last window that had focus so that some events can be sent to it even if no window has focus (ex: requesting fullscreen)
 - added support for mouse wheel (
glfwSetScrollCallback) - added support for mouse enter/leave (
glfwSetCursorEnterCallback) 
- first public version
 
This project includes the glfw3.h and glfwnative.h headers (under external/GLFW) which use a ZLib license
- Apache 2.0 License. This project can be used according to the terms of the Apache 2.0 license.
 
