Web: improve custom cursor handling and add animated cursors (#3384)

This commit is contained in:
daxpedda 2024-01-12 11:51:19 +01:00 committed by GitHub
parent bdeb2574dc
commit 169cd39f93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 1086 additions and 420 deletions

View file

@ -8,6 +8,15 @@ use winit::{
keyboard::Key,
window::{CursorIcon, CustomCursor, WindowBuilder},
};
#[cfg(wasm_platform)]
use {
std::sync::atomic::{AtomicU64, Ordering},
std::time::Duration,
winit::platform::web::CustomCursorExtWebSys,
};
#[cfg(wasm_platform)]
static COUNTER: AtomicU64 = AtomicU64::new(0);
fn decode_cursor<T>(bytes: &[u8], window_target: &EventLoopWindowTarget<T>) -> CustomCursor {
let img = image::load_from_memory(bytes).unwrap().to_rgba8();
@ -74,6 +83,45 @@ fn main() -> Result<(), impl std::error::Error> {
log::debug!("Setting cursor visibility to {:?}", cursor_visible);
window.set_cursor_visible(cursor_visible);
}
#[cfg(wasm_platform)]
Key::Character("4") => {
log::debug!("Setting cursor to a random image from an URL");
window.set_cursor(
CustomCursor::from_url(
format!(
"https://picsum.photos/128?random={}",
COUNTER.fetch_add(1, Ordering::Relaxed)
),
64,
64,
)
.build(_elwt),
);
}
#[cfg(wasm_platform)]
Key::Character("5") => {
log::debug!("Setting cursor to an animation");
window.set_cursor(
CustomCursor::from_animation(
Duration::from_secs(3),
vec![
custom_cursors[0].clone(),
custom_cursors[1].clone(),
CustomCursor::from_url(
format!(
"https://picsum.photos/128?random={}",
COUNTER.fetch_add(1, Ordering::Relaxed)
),
64,
64,
)
.build(_elwt),
],
)
.unwrap()
.build(_elwt),
);
}
_ => {}
},
WindowEvent::RedrawRequested => {