Skip to content

Commit 4ecb306

Browse files
authored
Round down any canvas-relative mouse event coordinates (fixes SAFE_HEAP asserts) (#21429)
When using a custom target the coordinates may be doubles, which causes SAFE_HEAP to assert. These are truncated to avoid this (which is what happens anyway, only now it's explicit).
1 parent ccad71b commit 4ecb306

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

site/source/docs/api_reference/html5.h.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,13 @@ Struct
419419
.. c:member:: long targetX
420420
long targetY
421421

422-
These fields give the mouse coordinates mapped relative to the coordinate space of the target DOM element receiving the input events (Emscripten-specific extension).
422+
These fields give the mouse coordinates mapped relative to the coordinate space of the target DOM element receiving the input events (Emscripten-specific extension; coordinates are rounded down to the nearest integer).
423423

424424

425425
.. c:member:: long canvasX
426426
long canvasY
427427

428-
These fields give the mouse coordinates mapped to the Emscripten canvas client area (Emscripten-specific extension).
428+
These fields give the mouse coordinates mapped to the Emscripten canvas client area (Emscripten-specific extension; coordinates are rounded down the nearest integer).
429429

430430

431431
.. c:member:: long padding

src/library_html5.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -481,16 +481,17 @@ var LibraryHTML5 = {
481481
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
482482
if (Module['canvas']) {
483483
var rect = getBoundingClientRect(Module['canvas']);
484-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasX / 4 }}}] = e.clientX - rect.left;
485-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasY / 4 }}}] = e.clientY - rect.top;
484+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasX / 4 }}}] = e.clientX - (rect.left | 0);
485+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasY / 4 }}}] = e.clientY - (rect.top | 0);
486486
} else { // Canvas is not initialized, return 0.
487487
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasX / 4 }}}] = 0;
488488
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.canvasY / 4 }}}] = 0;
489489
}
490490
#endif
491+
// Note: rect contains doubles (truncated to placate SAFE_HEAP, which is the same behaviour when writing to HEAP32 anyway)
491492
var rect = getBoundingClientRect(target);
492-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.targetX / 4 }}}] = e.clientX - rect.left;
493-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.targetY / 4 }}}] = e.clientY - rect.top;
493+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.targetX / 4 }}}] = e.clientX - (rect.left | 0);
494+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenMouseEvent.targetY / 4 }}}] = e.clientY - (rect.top | 0);
494495

495496
#if MIN_SAFARI_VERSION <= 80000 || MIN_CHROME_VERSION <= 21 // https://caniuse.com/#search=movementX
496497
#if MIN_CHROME_VERSION <= 76
@@ -1945,11 +1946,11 @@ var LibraryHTML5 = {
19451946
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.pageY / 4}}}] = t.pageY;
19461947
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.isChanged / 4}}}] = t.isChanged;
19471948
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.onTarget / 4}}}] = t.onTarget;
1948-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.targetX / 4}}}] = t.clientX - targetRect.left;
1949-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.targetY / 4}}}] = t.clientY - targetRect.top;
1949+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.targetX / 4}}}] = t.clientX - (targetRect.left | 0);
1950+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.targetY / 4}}}] = t.clientY - (targetRect.top | 0);
19501951
#if !DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR
1951-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.canvasX / 4}}}] = canvasRect ? t.clientX - canvasRect.left : 0;
1952-
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.canvasY / 4}}}] = canvasRect ? t.clientY - canvasRect.top : 0;
1952+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.canvasX / 4}}}] = canvasRect ? t.clientX - (canvasRect.left | 0) : 0;
1953+
HEAP32[idx + {{{ C_STRUCTS.EmscriptenTouchPoint.canvasY / 4}}}] = canvasRect ? t.clientY - (canvasRect.top | 0) : 0;
19531954
#endif
19541955

19551956
idx += {{{ C_STRUCTS.EmscriptenTouchPoint.__size__ / 4 }}};

0 commit comments

Comments
 (0)