如何更新Motion.svg的大小?

发布于 2025-02-13 20:41:02 字数 1436 浏览 1 评论 0原文

如何更新Motion.svg的大小?

我有一个常规的Div(在商店中的“ MAKESELEMENT”),其中包含一个Motion.svg元素,该元素又包含SVG G容器中包裹的一些RECT元素。 我需要更新我的SVG元素才能查看所有RECT元素。我将状态设置在回调钩中,但是即使我看到自己的状态变化,我的SVG元素的高度仍然是100。 我正在使用MOBX-LITE的UseObserver挂钩

export interface SVGContainerProps {
    store: myStore;
}

export default function SVGContainer(props: SVGContainerProps): ReactElement {
    const {
        store,
    } = props;

    const [height, setHeight] = useState<number>(100);

    const useSVGRef = useCallback((svg: SVGSVGElement) => {
        const bbox = svg.getBBox();

        console.log('height:', bbox.height);

        setHeight(bbox.height);
    }, [])

    return useObserver(() => (
        <motion.svg
            ref={useSVGRef}
            className={cssSvgContainer}
            dragConstraints={{ current: store.maskElement }}
            dragMomentum={false}
            drag={true}
            whileTap={{ cursor: 'grabbing' }}
            style={{ height: height}}
        >
            {store.items.map((item, index) => (
                <SVGRect
                    key={item.id}
                    item={item}
                    x={0}
                    y={index * 10}
                />
            ))}
        </motion.svg>
    ));
}

const cssSvgContainer = cx(
    cssPointerEventsAll,
    cssAbsolute,
    cssBottom(0),
    cssRight(0),
    cssBorder,
    cssBorderWidth1,
);

How to update the size of motion.svg?

I have a regular DIV ("maskElement" in my store) that contains a motion.svg element, that in turn contains some rect elements wrapped in SVG g Container.
I need to update my svg element to see all the rect elements. I set the state in a callback hook but the height of my svg element is still 100 even though I see my state change.
I'm using useObserver hook from mobx-lite

export interface SVGContainerProps {
    store: myStore;
}

export default function SVGContainer(props: SVGContainerProps): ReactElement {
    const {
        store,
    } = props;

    const [height, setHeight] = useState<number>(100);

    const useSVGRef = useCallback((svg: SVGSVGElement) => {
        const bbox = svg.getBBox();

        console.log('height:', bbox.height);

        setHeight(bbox.height);
    }, [])

    return useObserver(() => (
        <motion.svg
            ref={useSVGRef}
            className={cssSvgContainer}
            dragConstraints={{ current: store.maskElement }}
            dragMomentum={false}
            drag={true}
            whileTap={{ cursor: 'grabbing' }}
            style={{ height: height}}
        >
            {store.items.map((item, index) => (
                <SVGRect
                    key={item.id}
                    item={item}
                    x={0}
                    y={index * 10}
                />
            ))}
        </motion.svg>
    ));
}

const cssSvgContainer = cx(
    cssPointerEventsAll,
    cssAbsolute,
    cssBottom(0),
    cssRight(0),
    cssBorder,
    cssBorderWidth1,
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文