最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - svg viewbox should not resize the text fontSize? - Stack Overflow

programmeradmin0浏览0评论

I'm using svg with viewBox for fit to the container its working fine ,when i resize the container the svg circle and text are resizing and fit to container but i don't want to resize the text fontSize when i resize the container.I searched a lot but didn't find any valuable suggestions.

I need to resize div and svg circle should resize but text should not resize the font size and also text should move to along with the circle.

Any suggestions should be appreciated.

The following is the SVG i'm using in my application

<div id="container">
    <svg version="1.1" xmlns="" viewBox="0 0 920 620" preserveAspectRatio="none" style="overflow: hidden; position: relative;">
        <circle cx="100" cy="100" r="100" fill="green"></circle>
        <text x="100" y="100" text-anchor="middle" font="18px &quot;Arial&quot;" stroke="none" fill="#000000" font-size="20px" font-style="italic" font-weight="800" font-family="Times New Roman" opacity="1.0" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: middle; font-style: italic; font-variant: normal; font-weight: 800; font-size: 18px; line-height: normal; font-family: 'Times New Roman'; opacity: 1;">
            <tspan dy="5.828125" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Circle</tspan>
        </text>
        </svg>
</div>

Here is the Demo

Note: Resize the jsFiddle

I'm using svg with viewBox for fit to the container its working fine ,when i resize the container the svg circle and text are resizing and fit to container but i don't want to resize the text fontSize when i resize the container.I searched a lot but didn't find any valuable suggestions.

I need to resize div and svg circle should resize but text should not resize the font size and also text should move to along with the circle.

Any suggestions should be appreciated.

The following is the SVG i'm using in my application

<div id="container">
    <svg version="1.1" xmlns="http://www.w3/2000/svg" viewBox="0 0 920 620" preserveAspectRatio="none" style="overflow: hidden; position: relative;">
        <circle cx="100" cy="100" r="100" fill="green"></circle>
        <text x="100" y="100" text-anchor="middle" font="18px &quot;Arial&quot;" stroke="none" fill="#000000" font-size="20px" font-style="italic" font-weight="800" font-family="Times New Roman" opacity="1.0" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: middle; font-style: italic; font-variant: normal; font-weight: 800; font-size: 18px; line-height: normal; font-family: 'Times New Roman'; opacity: 1;">
            <tspan dy="5.828125" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Circle</tspan>
        </text>
        </svg>
</div>

Here is the Demo

Note: Resize the jsFiddle

Share Improve this question edited Aug 14, 2013 at 4:37 Karthi Keyan asked Aug 13, 2013 at 11:40 Karthi KeyanKarthi Keyan 4,3935 gold badges26 silver badges47 bronze badges 1
  • In this demo I supply some JavaScript that can dynamically modify the transforms on selected elements to keep them scale independent as the page zooms. – Phrogz Commented Aug 13, 2013 at 23:25
Add a ment  | 

2 Answers 2

Reset to default 3

Move the viewbox out of the root svg tag and into a nested svg tag. Put the text outside the nested svg tag and the viewbox will not affect the text tag

<div id="container">
    <svg version="1.1" xmlns="http://www.w3/2000/svg" style="overflow: hidden; position: relative;">
        <svg viewBox="0 0 920 620" preserveAspectRatio="none">
            <circle cx="100" cy="100" r="100" fill="green"></circle>
        </svg>
        <text x="100" y="100" text-anchor="middle" font="18px &quot;Arial&quot;" stroke="none" fill="#000000" font-size="20px" font-style="italic" font-weight="800" font-family="Times New Roman" opacity="1.0" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0); text-anchor: middle; font-style: italic; font-variant: normal; font-weight: 800; font-size: 18px; line-height: normal; font-family: 'Times New Roman'; opacity: 1;">
            <tspan dy="5.828125" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);">Circle</tspan>
        </text>
</svg>

Example

Sorry. There isn't a way to do what you want. There is a feature of SVG 1.2 called TransformRef (http://www.w3/TR/SVGTiny12/coords.html#transform-ref) which could be useful in theis situation, but sadly it is not supported by any of the browsers AFAIK.

发布评论

评论列表(0)

  1. 暂无评论