Closures and borrowing

Hello. I begin to use web_sys crate, and try t write some simple canvas example with images. Could you help me, with correct way to use image in callback when image was loaded?

pub fn start() {
    let renderer = Rc::new(renderer::Renderer::new(1280, 720));
    let image = web_sys::HtmlImageElement::new().unwrap();

    let closure = Closure::wrap(Box::new(move |_event: web_sys::Event| {
        let renderer = renderer.clone();
        renderer.draw(&image, 10.0, 10.0, 100.0, 100.0);
    }) as Box<dyn FnMut(_)>);



You’ll have to wrap your image in an Rc/Arc to get shared ownership, one for the Closure and one for the main function.

@KrishnaSannasi Thanks.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.