Skip to content

Commit cb88605

Browse files
authored
Fix context creation of transferred offscreen canvas (#23518)
After a canvas has been transferred offscreen, it can't be used again to create a webgl context because the result of findCanvasEventTarget is a {canvas:...} map instead of an actual canvas object. This patch grabs the underlying offscreencanvas if it exists.
1 parent 5858c64 commit cb88605

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/lib/libhtml5_webgl.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,10 @@ var LibraryHtml5WebGL = {
109109
#endif
110110

111111
var canvas = findCanvasEventTarget(target);
112-
112+
#if OFFSCREENCANVAS_SUPPORT
113+
// If our canvas from findCanvasEventTarget is actually an offscreen canvas record, we should extract the inner canvas.
114+
if (canvas?.canvas) { canvas = canvas.canvas; }
115+
#endif
113116
#if GL_DEBUG
114117
var targetStr = UTF8ToString(target);
115118
#endif

test/browser/webgl_create_context_swapcontrol.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,13 @@ int main() {
2323
EMSCRIPTEN_RESULT res = emscripten_webgl_make_context_current(context);
2424
assert(res == EMSCRIPTEN_RESULT_SUCCESS);
2525
assert(emscripten_webgl_get_current_context() == context);
26+
emscripten_webgl_destroy_context(context);
27+
// Test that creating a context for a canvas that's already been used once succeeds
28+
context = emscripten_webgl_create_context("#canvas", &attrs);
29+
assert(context > 0); // Must have received a valid context.
30+
res = emscripten_webgl_make_context_current(context);
31+
assert(res == EMSCRIPTEN_RESULT_SUCCESS);
32+
assert(emscripten_webgl_get_current_context() == context);
33+
emscripten_webgl_destroy_context(context);
2634
return 0;
2735
}

0 commit comments

Comments
 (0)