Function freya::hooks::use_canvas_with_deps

source ·
pub fn use_canvas_with_deps<D>(
    dependencies: D,
    renderer_cb: impl Fn(<D as Dependency>::Out) -> Box<dyn Fn(&mut CanvasRunnerContext<'_>) + Sync + Send> + 'static,
) -> UseCanvas
where D: Dependency, <D as Dependency>::Out: 'static,
Expand description

Register a rendering hook to gain access to the Canvas. Reactivity managed with manual dependencies.

§Usage

fn app() -> Element {
    let (reference, size) = use_node_signal();
    let mut value = use_signal(|| 0);
    let platform = use_platform();

    let canvas = use_canvas_with_deps(&value(), move |curr| {
        platform.invalidate_drawing_area(size.peek().area);
        platform.request_animation_frame();
        Box::new(move |ctx| {
            // Draw using the canvas !
            // use `curr`
        })
    });

    rsx!(rect {
        onclick: move |_| {
            value += 1;
        },
        canvas_reference: canvas.attribute(),
        reference,
        width: "fill",
        height: "fill",
    })
}