我正在尝试通过内联 SVG 在图像上覆盖弯曲的文本。它在 Chrome 中运行良好,但 iOS 和桌面上的 Safari 无法呈现文本。

<svg viewBox="0 0 580 472" width="580" height="472" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
    <defs>
        <path id="curvedPath" d="M123.9,309.87s55.27,20.4,148.06,23.54c99.49,3.37,153.33-16.68,153.33-16.68"></path>
        <style type="text/css">
            #text {
                font-size: 24px;
                text-align: center;
                text-anchor: middle;
                fill: rgba(0,0,0,0.8);
            }
        </style>
    </defs>

    <image x="0" y="0" width="580" height="472" xlink:href="https://www.silvity.de/out/pictures/master/product/1/gravur-armband-swarovski-kristall-rosegold-schwarz-111106601.jpg"></image>

    <text id="text" class="Philosopher">
        <textPath href="#curvedPath" startOffset="50%" id="textcontent">Foobar StackOverflow</textPath>
    </text>
</svg>


此代码段在 Chrome 中显示文本“Foobar StackOverflow”,但在 Safari 中不显示。

路径的曲线无关紧要,直线( M10.100,L100.100 )也不起作用。我可以让文本显示的唯一方法是将它直接嵌入到 text 标签中,例如 <text id="...">Foobar StackOverflow</text> ,这显然不是我想要的。

在 Safari SVG 中使用 textPath 有什么限制吗?我怎样才能让它在两个浏览器中正确呈现?

最佳答案

Safari 仅支持 xlink:href 而不是较新的裸 href。

<svg viewBox="0 0 580 472" width="580" height="472" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:cc="http://creativecommons.org/ns#" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
    <defs>
        <path id="curvedPath" d="M123.9,309.87s55.27,20.4,148.06,23.54c99.49,3.37,153.33-16.68,153.33-16.68"></path>
        <style type="text/css">
            #text {
                font-size: 24px;
                text-align: center;
                text-anchor: middle;
                fill: rgba(0,0,0,0.8);
            }
        </style>
    </defs>

    <image x="0" y="0" width="580" height="472" xlink:href="https://www.silvity.de/out/pictures/master/product/1/gravur-armband-swarovski-kristall-rosegold-schwarz-111106601.jpg"></image>

    <text id="text" class="Philosopher">
        <textPath xlink:href="#curvedPath" startOffset="50%" id="textcontent">Foobar StackOverflow</textPath>
    </text>
</svg>

关于html - SVG textPath 在 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51404939/

10-12 12:31
查看更多