在 safari 中更新后,svg 无法正确渲染

发布于 2025-01-11 06:32:11 字数 715 浏览 0 评论 0原文

我正在研究 React 项目,并实现了用于图标的渐变生成器。问题是,在 chrome 上一切正常,但 safari 通常只会在第一次渲染图标,例如,如果我执行一些更新操作,例如将视图更改为移动设备或更改组件的状态,它会变成黑色。当我在图标之间切换时,它会重新生成,直到我重复该场景。

我读到 safari 的常见问题是 url 路径没有正确解析,但正如你所看到的,我已经这样做了,即使路径是硬编码的,它也不起作用。还尝试在 safari 中禁用画布渲染。

<svg
    width={72}
    height={26}
>
    <defs>
        <linearGradient id={object.id}>
            {this.generateStops(object.depthColors ? object.depthColors : [])}
        </linearGradient>
    </defs>
    <rect width={72} height={26} fill={`url(${window.location.href}#${object.id})`}/>
</svg>

我想我不会添加更多代码,因为问题不在于停止生成,它总是返回正确生成的停止。我还尝试向 svg 添加各种参数,例如 idviewbox,但没有帮助。

I'm working on React project and I implemented gradient generator that is used for icons. The problem is that on chrome everything works fine, but safari renders the icon normally only first time, for example, if I do some update action like change view to mobile or change state of component it turns black. When I switch between icons it regenerates until I repeat the scenario.

I read that the common problem with safari is that url path is not resolved right, but as you can see I already done that, it's not working even when the path is hardcoded. Also tried to disable canvas rendering in safari.

<svg
    width={72}
    height={26}
>
    <defs>
        <linearGradient id={object.id}>
            {this.generateStops(object.depthColors ? object.depthColors : [])}
        </linearGradient>
    </defs>
    <rect width={72} height={26} fill={`url(${window.location.href}#${object.id})`}/>
</svg>

I think I won't add more code since the problem is not with stops generation, it always returns correctly generated stops. Also I tried to add various parameters to svg like id, viewbox, but it didn't help.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2025-01-18 06:32:11

我最终做的是这样的:

const random = Math.random().toString();

return (
    <svg
        width={72}
        height={26}
    >
        <defs>
            <linearGradient id={random}>
                {this.generateStops(colorPalette.depthColors ? colorPalette.depthColors : [])}
            </linearGradient>
        </defs>
        <rect width={72} height={26} fill={`url(#${random})`}/>
    </svg>
);

以前的版本也使用唯一的id来生成渐变,但每次选择相同的渐变时它不会刷新。

What I ended up doing is this:

const random = Math.random().toString();

return (
    <svg
        width={72}
        height={26}
    >
        <defs>
            <linearGradient id={random}>
                {this.generateStops(colorPalette.depthColors ? colorPalette.depthColors : [])}
            </linearGradient>
        </defs>
        <rect width={72} height={26} fill={`url(#${random})`}/>
    </svg>
);

Previous version also used unique id's for gradient generation, but it didn't refresh every time when the same gradient is selected.

念三年u 2025-01-18 06:32:11

我相信我刚刚遇到了和你完全相同的问题!
我有带有线性渐变的 SVG,只有当发生一些渲染/取消渲染时(在我的例子中是模态窗口),它才会在 iOS 上变黑。 Chrome/Firefox 都很好。

详细的文章可以在此处和代码沙箱示例这里

核心原因是 webkit 的 bug。我使用的是运行 iOS 15.4(2022 年 3 月 14 日发布)的 iPad,与提出此问题的时间相似。当我将 iPad 更新到最新版本(并更新 webkit)后,问题就消失了。

我花了 30 多个小时调试这个问题,但我不敢相信我没有考虑尝试任何其他 iOS 设备(尤其是那些完全更新到最新版本的设备)。

I believe I just ran into the exact same issue as you!
I had SVGs with linear gradient that would turn black on iOS only when some render / unrenders happened (in my case a modal window). Chrome/Firefox were fine.

A detailed write up is available here and a code sandbox example here.

The core reason was a webkit bug. I was using an iPad on iOS 15.4 (released March 14, 2022), similar time to when this question was asked. Once I updated the iPad to latest version (and thus updating webkit), the problem went away.

I spend 30+ hours debugging this issue though, I can't believe I didn't consider trying any other iOS device (especially ones that were fully updated to the latest version).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文