Calling getBBox for a SVG text element in a Seed Rust application

144 views Asked by At

I just made my first steps with WASM and Seed which was a very smooth experience so far. I was able to create SVG using svg!, circle!, text!, ... and similar macros. To generate my SVG in the proper way, I have to measure text. My idea is to generate SVG text nodes and call getBBox on the node. I figured out that Seed is using web_sys and that getBBox is implemented there.

My problem is how to get from the Node created by text! to the SvgTextElement. I tried to access the node_ws field, but it seems to be "empty". It might not yet been created, but I don't now enough about the Seed internals.

So how do I create a SVG text node so that I can call getBBox on it before generating the "main" SVG nodes?

1

There are 1 answers

0
glennsl On

You can use el_ref to get a reference to the DOM element. Something like this ought to work:

struct Model {
    element: ElRef<web_sys::SvgTextElement>,
}

fn view(model: &Model) -> Node<Msg> {
    svg!![
        text![
            el_ref(&model.element),
            // ...
        ],
        // ...
    ]
}

fn init(orders: &mut impl Orders<Msg>) -> Model {
    orders.after_next_render(|_| Msg::Rendered);
    // ...
}

fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
    match msg {
        Msg::Rendered => {
            let element = model.element.get().expect("no svg text element");
            let b_box = element.get_b_box();
            // ...
        }
        // ...
    }
}