web_sys::HtmlImageElement::set_onload .. passing it a Rust function?

Relevant function: https://rustwasm.github.io/wasm-bindgen/api/web_sys/struct.HtmlImageElement.html#method.onload

This function takes as an argument an object of type Function, namely https://rustwasm.github.io/wasm-bindgen/api/js_sys/struct.Function.html

Problem is that both constructors look like they take &str representing JS code for the callback.

I have an existing Rust function I would like to use as the callback. Is there anyway to transform it into a Function ?

It is a bit tricky:

1 Like

Thanks, there is also rustwasm/gloo : https://github.com/rustwasm/gloo/blob/master/crates/events/src/lib.rs which appears to do the same thing but provides slightly nicer wrapper functions.