How to draw a number centered inside a circle with inline svg?

thebjorn

I'm trying to write a Django template tag that will generate a number inside a circle as an inline svg.

The math is pretty straight-forward (except for the fudge-factor in real_fontsize -- I'm thinking it has to do with numerals being shorter than the tallest character..?)

@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

template:

<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>

it produces an svg that renders very nicely in all browsers I have tested, except the native iPad browser, where the number is flush with the bottom of the circle (and not in the center)..

<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>

Is there any way to work around this issue in a cross-browser (IE9+) fashion?

jsfiddle: http://jsfiddle.net/onLyh6bm/

(using alignment-baseline: middle doesn't seem to align in the middle in any browser..: http://jsfiddle.net/68oamxdo/1/)

Paul LeBeau

You can't really rely on the alignment-baseline property. Support for it is pretty spotty at best.

Even when it is supported, alignment-baseline: middle doesn't really vertically centre the characters. There is, in fact, no way to do it accurately and reliably.

You can centre horizontally with text-anchor="middle", which is supported everywhere.

IMO, the best you can do in the vertical case, is the solution I propose in this answer: https://stackoverflow.com/a/19273331/1292848

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Draw a filled circle with number inside

From Dev

How to draw a number inside a circle on Google Map with JavaScript

From Dev

How to draw a number inside a circle on Google Map with JavaScript

From Dev

svg draw circle with curved text inside

From Dev

svg draw circle with curved text inside

From Dev

Draw centered circle

From Dev

How to draw stroke inside a circle?

From Java

How to set correct Inline SVG Circle coordinates

From Dev

How to draw circle centered at mouse location when a key is pressed

From Dev

How to draw a circle inside a path on canvas in Android

From Dev

How to draw an inline svg (in DOM) to a canvas?

From Dev

Draw segment of circle on SVG

From Dev

Draw text inside a circle

From Dev

How to add a link inside an svg circle

From Dev

How to draw an image inside Rounded Rectangle or inside a shape like circle?

From Dev

How to draw a rectangle inside a rectangle using svg?

From Dev

SVG image inside circle

From Dev

SVG path inside circle

From Dev

SVG Draw a circle with 4 sectors

From Dev

Draw half circle with CSS or SVG

From Dev

How to draw multiple vertical lines inside a semi-circle?

From Dev

How to draw a circle in UILabel?

From Dev

How to draw a circle css

From Dev

How to draw a circle css

From Dev

How to draw a (value)% Circle

From Dev

How do I perfectly center a single character inside an SVG circle?

From Dev

How can I place text inside an SVG circle with React JS?

From Dev

How to include angular JS expressions inside an SVG circle

From Dev

Google Chart Calendar how to mark date with circle inside -svg