【问题标题】:SVG circled text on textPath (center align)textPath 上的 SVG 圆形文本(居中对齐)
【发布时间】:2013-03-19 09:44:24
【问题描述】:

我遇到了与 SVG 相关的带圆圈文本的问题。我的目标是创建一个路径,让我可以在上面写字,同时让文本居中,仍然与我的路径保持一致 - 从圆圈的顶部开始。

示例

That's what it looks like(内有图片)

问题

目前我正在使用 textPath + path 组合来生成路径并在上面写。

<svg>
<defs>
<path id="textPath" d="M 200 175 A 25 25 0 0 0 182.322 217.678" />
</defs>
<text x="25" y="0"><textPath xlink:href="#textPath" startOffset="0" >here goes my text</textPath></text>
</svg>

我也尝试过 Raphael 库来管理它,但严重的是我不能做我想做的事。这里有人真正设法使它工作吗?或者有什么办法可以做到吗?

【问题讨论】:

    标签: javascript svg raphael geometry


    【解决方案1】:

    将您的文本路径定义为您要绘制的椭圆弧的完整上半球:

    <path id="textPath" d="M 250 500 A 250,150 0 1 1 750,500" />
    

    并将您的textPathstartOffset 设置为50%。请注意,您的 svg 文件格式不正确,因为它缺少 xlink 的命名空间定义。以下是一个独立的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- SO: http://stackoverflow.com/questions/15495978/svg-circled-text-on-textpath-center-align  -->
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <svg xmlns="http://www.w3.org/2000/svg"
       xmlns:xhtml="http://www.w3.org/1999/xhtml"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       version="1.1"
       width="20cm" height="20cm"
       viewBox="0 0 1000 1000"
       preserveAspectRatio="xMinYMin"
       style="background-color:white; border: solid 1px black;"
    >
    <defs>
    <path id="textPath" d="M 250 500 A 250,150 0 1 1 750,500"/>
    </defs>
    <text x="0" y="0" text-anchor="middle" style="font-size:30pt;"><textPath xlink:href="#textPath" startOffset="50%" >here goes my text</textPath></text>
    </svg>
    

    re: 评论一个绕圈子的解决方案: 要点是定义沿整个圆周延伸的文本路径,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <!-- SO: http://stackoverflow.com/questions/15495978/svg-circled-text-on-textpath-center-align  -->
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <svg xmlns="http://www.w3.org/2000/svg"
       xmlns:xhtml="http://www.w3.org/1999/xhtml"
       xmlns:xlink="http://www.w3.org/1999/xlink"
       version="1.1"
       width="20cm" height="20cm"
       viewBox="0 0 1000 1000"
       preserveAspectRatio="xMinYMin"
       style="background-color:white; border: solid 1px black;"
    >
    <defs>
    <path id="textPath" d="M 250 500 A 250,250 0 1 1 250 500.0001"/>
    </defs>
    <text x="0" y="0" text-anchor="middle" style="font-size:30pt;"><textPath xlink:href="#textPath" startOffset="50%" >this is a merry-go-round of all my text wrapped around a circle, a vbery big one</textPath></text>
    </svg>
    

    【讨论】:

    • 将您的文本路径定义为您要绘制的椭圆弧的完整上半球: 如果我需要完整的圆圈来书写怎么办?然后呢?
    猜你喜欢
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    相关资源
    最近更新 更多