Skip to content

Commit

Permalink
Add pointerrawupdate support
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Jun 4, 2023
1 parent b60863c commit 5ec16a8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ And please only add new entries to the top of this list, right below the `# Unre
- On Web, fix touch input not gaining or loosing focus.
- **Breaking:** On Web, dropped support for Safari versions below 13.
- On Web, fix clicks on the canvas to select text.
- On Web, use high-frequency pointer input events when supported by the browser.

# 0.28.6

Expand Down
30 changes: 28 additions & 2 deletions src/platform_impl/web/web_sys/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use crate::event::{Force, MouseButton};
use crate::keyboard::ModifiersState;

use event::ButtonsState;
use once_cell::unsync::OnceCell;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsCast, JsValue};
use web_sys::PointerEvent;
use web_sys::{HtmlCanvasElement, PointerEvent};

#[allow(dead_code)]
pub(super) struct PointerHandler {
Expand Down Expand Up @@ -186,7 +187,11 @@ impl PointerHandler {
{
let canvas = canvas_common.raw.clone();
self.on_cursor_move = Some(canvas_common.add_event(
"pointermove",
if has_pointer_raw_support(&canvas) {
"pointerrawupdate"
} else {
"pointermove"
},
move |event: PointerEvent| {
// coalesced events are not available on Safari
#[wasm_bindgen]
Expand Down Expand Up @@ -300,3 +305,24 @@ impl PointerHandler {
self.on_touch_cancel = None;
}
}

fn has_pointer_raw_support(canvas: &HtmlCanvasElement) -> bool {
thread_local! {
static POINTER_RAW_SUPPORT: OnceCell<bool> = OnceCell::new();
}

POINTER_RAW_SUPPORT.with(|support| {
*support.get_or_init(|| {
#[wasm_bindgen]
extern "C" {
type HtmlCanvasElementExt;

#[wasm_bindgen(method, getter, js_name = onpointerrawupdate)]
fn has_on_pointerrawupdate(this: &HtmlCanvasElementExt) -> JsValue;
}

let canvas: &HtmlCanvasElementExt = canvas.unchecked_ref();
!canvas.has_on_pointerrawupdate().is_undefined()
})
})
}

0 comments on commit 5ec16a8

Please sign in to comment.