Emscripten comes with a built-in implementation of GLFW (3) written in 100% JavaScript. This implementation is an alternate and more complete implementation of the GLFW API (currently 3.4.0). This document explains the differences.
From the git history and the current source code, it is possible to gather the facts that the built-in GLFW
implementation was implemented back in 2013 in pure JavaScript.
It currently states that it implements the 3.2.1
version of the API released in 2016
(as returned by glfwGetVersion
).
There have been 2 major releases since then (3.3.0
released in 2019 and 3.4.0
released in 2024).
It does not comply with the error handling paradigm of GLFW (glfwSetErrorCallback
stores the function pointer but
never uses it and some APIs throw an exception when called (like glfwGetTimerFrequency
)).
This library currently implements the latest GLFW API (3.4.0) in C++ (and JavaScript when necessary), using more modern browser technologies and addresses some issues from the JavaScript implementation. It tries to do its best to implement as much of the GLFW API that is possible to implement in the context of a web browser. When it can't, it handles the case gracefully and in compliance with error handling in GLFW.
Note
This implementation has taken great care to be backward compatible with the built-in implementation,
for example, supporting Module['canvas']
, so that it is a drop-in replacement.
When compiling using the Emscripten compiler (emcc
/ em++
), it is equally trivial to pick which flavor you want
to use:
- If you want to use the built-in implementation, you use the syntax
-sUSE_GLFW=3
- If you want to use this implementation, you use the syntax
--use-port=contrib.glfw3
Tip
When using CMake
, you need to define it as both a compile and link option:
target_compile_options(${target} PUBLIC "--use-port=contrib.glfw3")
target_link_options(${target} PUBLIC "--use-port=contrib.glfw3")
This section describes the main features implemented by both libraries.
Feature | emscripten-glfw (this implementation) |
library_glfw.js (built-in implementation) |
---|---|---|
Single Window | ||
Multiple Windows | ||
Mouse | (includes sticky button behavior) | (NO sticky button behavior) |
Keyboard | (includes sticky key behavior) | (NO sticky key behavior) |
Keyboard / Meta Key | (implements workaround) | |
Joystick | ||
Gamepad | 3.3.x feature | |
Fullscreen | ||
Hi DPI | ||
Clipboard (copy/paste) | ||
Cursors (Standard + Custom) | ||
Window Size constraints (size limits and aspect ratio) | ||
Window Opacity | ||
Window Status (Focused / Hovered / Position) | ||
Timer | (throws exception if called!) | |
Error Handing |
Please refer to the exhaustive list of functions for further details.
Note
This table is built with data from Emscripten 3.1.65 (2024/08/22)
Warning
Emscripten embeds glfw3.h
version 3.3.8 yet implements version 3.2.1.