如何使用内联 svg 在圆圈内居中绘制数字?
How to draw a number centered inside a circle with inline svg?
我正在尝试编写一个 Django 模板标签,它将在圆圈内生成一个数字作为内联 svg。
数学非常简单(除了 real_fontsize
中的软糖因素——我认为这与数字比最高字符短有关..?)
@register.inclusion_tag('numcircle.html')
def numcircle(n, size=36, border=4, color='white', background="black", **kw):
"""Draw a circle with a number inside.
"""
kw['padding'] = kw.get('padding', 2)
kw['num'] = n
kw['size'] = size
kw['border'] = border
kw['center'] = size / 2
kw['radius'] = (size / 2) - border
kw['color'] = color
kw['background'] = background
kw['fontsize'] = size - (2 * (border + kw['padding']))
real_fontsize = kw['fontsize'] * (0.8 if kw['fontsize'] > 25 else 1)
kw['ypos'] = kw['center'] + real_fontsize / 2 - kw['border'] + kw.get('yadjust', 0)
kw['xpos'] = kw.get('xpos', size / 2)
return kw
模板:
<svg width="{{ size }}" height="{{ size }}" viewBox="0 0 {{ size }} {{ size }}">
<circle cx="{{ center }}"
cy="{{ center }}"
r="{{ radius }}"
stroke="{{ color }}"
stroke-width="{{ border }}"
fill="{{ background }}"/>
<text font-size="{{ fontsize }}"
fill="{{ color }}"
font-family="Verdana"
text-anchor="middle"
alignment-baseline="baseline"
x="{{ xpos }}"
y="{{ ypos }}">{{ num }}</text>
</svg>
它生成的 svg 在我测试过的所有浏览器中呈现得非常好,除了本机 iPad 浏览器,其中数字与圆圈底部齐平(而不是在中心)。
<svg width="44" height="44" viewBox="0 0 44 44">
<circle cx="22"
cy="22"
r="20"
stroke="white"
stroke-width="2"
fill="#21435F"/>
<text font-size="36"
fill="white"
font-family="Verdana"
text-anchor="middle"
alignment-baseline="baseline"
x="22"
y="34.4">1</text>
</svg>
有没有办法以跨浏览器 (IE9+) 的方式解决这个问题?
jsfiddle: http://jsfiddle.net/onLyh6bm/
(使用 alignment-baseline: middle
在任何浏览器中似乎都不会在中间对齐..: http://jsfiddle.net/68oamxdo/1/)
您不能真正依赖 alignment-baseline
属性。对它的支持充其量是参差不齐的。
即使支持,alignment-baseline: middle
也不会真正使字符垂直居中。事实上,没有办法准确可靠地做到这一点。
您可以使用 text-anchor="middle"
水平居中,到处都支持。
IMO,在垂直情况下你能做的最好的就是我在这个答案中提出的解决方案:
我正在尝试编写一个 Django 模板标签,它将在圆圈内生成一个数字作为内联 svg。
数学非常简单(除了 real_fontsize
中的软糖因素——我认为这与数字比最高字符短有关..?)
@register.inclusion_tag('numcircle.html')
def numcircle(n, size=36, border=4, color='white', background="black", **kw):
"""Draw a circle with a number inside.
"""
kw['padding'] = kw.get('padding', 2)
kw['num'] = n
kw['size'] = size
kw['border'] = border
kw['center'] = size / 2
kw['radius'] = (size / 2) - border
kw['color'] = color
kw['background'] = background
kw['fontsize'] = size - (2 * (border + kw['padding']))
real_fontsize = kw['fontsize'] * (0.8 if kw['fontsize'] > 25 else 1)
kw['ypos'] = kw['center'] + real_fontsize / 2 - kw['border'] + kw.get('yadjust', 0)
kw['xpos'] = kw.get('xpos', size / 2)
return kw
模板:
<svg width="{{ size }}" height="{{ size }}" viewBox="0 0 {{ size }} {{ size }}">
<circle cx="{{ center }}"
cy="{{ center }}"
r="{{ radius }}"
stroke="{{ color }}"
stroke-width="{{ border }}"
fill="{{ background }}"/>
<text font-size="{{ fontsize }}"
fill="{{ color }}"
font-family="Verdana"
text-anchor="middle"
alignment-baseline="baseline"
x="{{ xpos }}"
y="{{ ypos }}">{{ num }}</text>
</svg>
它生成的 svg 在我测试过的所有浏览器中呈现得非常好,除了本机 iPad 浏览器,其中数字与圆圈底部齐平(而不是在中心)。
<svg width="44" height="44" viewBox="0 0 44 44">
<circle cx="22"
cy="22"
r="20"
stroke="white"
stroke-width="2"
fill="#21435F"/>
<text font-size="36"
fill="white"
font-family="Verdana"
text-anchor="middle"
alignment-baseline="baseline"
x="22"
y="34.4">1</text>
</svg>
有没有办法以跨浏览器 (IE9+) 的方式解决这个问题?
jsfiddle: http://jsfiddle.net/onLyh6bm/
(使用 alignment-baseline: middle
在任何浏览器中似乎都不会在中间对齐..: http://jsfiddle.net/68oamxdo/1/)
您不能真正依赖 alignment-baseline
属性。对它的支持充其量是参差不齐的。
即使支持,alignment-baseline: middle
也不会真正使字符垂直居中。事实上,没有办法准确可靠地做到这一点。
您可以使用 text-anchor="middle"
水平居中,到处都支持。
IMO,在垂直情况下你能做的最好的就是我在这个答案中提出的解决方案: