Text in a SVG: 在原地将文本围绕圆旋转

huangapple go评论57阅读模式
英文:

Text in a SVG: Rotate a text within a circle on place

问题

我有一个圆圈中的文本(这就是为什么我使用来定义圆圈一次)。我想要在这个圆圈中使文本绕自身旋转。我的问题是:当我对文本进行旋转时,文本不再位于圆圈内。如何在应用旋转时保持文本在圆圈内?如何使文本围绕其中心旋转?

这是我目前的代码:

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <svg xmlns="http://www.w3.org/2000/svg" 
      xmlns:xlink="http://www.w3.org/1999/xlink" 
      width="200" 
      height="200" 
      viewBox="0 0 200 200" 
      preserveAspectRatio="xMinYMin">
      <style>
        circle{
          fill: #dcdcc6;
        }
        text{
          font-size: 11px;
          text-anchor: middle;
          fill: rgb(51, 65, 74); 
          font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
        }
      </style>
      <symbol id="circle" viewBox="0 0 16 16">
        <circle cx="8" cy="8" r="7"></circle>
      </symbol>
      <g>
        <use href="#circle" x="100" y="100" width="40" height="40"></use>
        <svg x="100" y="100" height="40" width="40">
          <g>
            <text x="50%" y="60%" text-anchor="end">TEXT</text>
          </g>
        </svg>
      </g>
    </svg>
    <script>
      var angle = 45;
      var text = document.querySelector('text');
      text.addEventListener('click', function(event) {
        angle += 45;
        var bbox = this.getBBox();
        var width = bbox.width;
        var height = bbox.height;
        this.setAttribute('transform', 'translate(' + (height / 2) + ',' + (height / 2) + ') rotate(' + angle + ')');
      });
    </script>
  </body>
</html>
英文:

I have several text in circle (that's why I use <use> to define circle once). I want to rotate the text on itself in that circle. My problem is: when I perform a rotation of the text, the text in no longer within the circle. How can I keep the text in the circle when applying a rotation? How can I make the text rotate at its center?

Here is the code I have for now:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; 
xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; 
width=&quot;200&quot; 
height=&quot;200&quot; 
viewBox=&quot;0 0 200 200&quot; 
preserveAspectRatio=&quot;xMinYMin&quot;&gt;
&lt;style&gt;
circle{
fill: #dcdcc6;
}
text{
font-size: 11px;
text-anchor: middle;
fill: rgb(51, 65, 74); 
font-family: &quot;Helvetica Neue&quot;,Helvetica,Arial,sans-serif;
}
&lt;/style&gt;
&lt;symbol id=&quot;circle&quot; viewBox=&quot;0 0 16 16&quot;&gt;
&lt;circle cx=&quot;8&quot; cy=&quot;8&quot; r=&quot;7&quot;&gt;&lt;/circle&gt;
&lt;/symbol&gt;
&lt;g&gt;
&lt;use href=&quot;#circle&quot; x=&quot;100&quot; y=&quot;100&quot; width=&quot;40&quot; height=&quot;40&quot;&gt;&lt;/use&gt;
&lt;svg x=&quot;100&quot; y=&quot;100&quot; height=&quot;40&quot; width=&quot;40&quot;&gt;
&lt;g&gt;
&lt;text x=&quot;50%&quot; y=&quot;60%&quot; text-anchor=&quot;end&quot;&gt;TEXT&lt;/text&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;script&gt;
var angle = 45;
var text = document.querySelector(&#39;text&#39;);
text.addEventListener(&#39;click&#39;, function(event) {
angle += 45;
var bbox = this.getBBox();
var width = bbox.width;
var height = bbox.height;
this.setAttribute(&#39;transform&#39;, &#39;translate(&#39; + (height / 2) + &#39;,&#39; + (height / 2) + &#39;) rotate(&#39; + angle + &#39;)&#39;);
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 3

如果将文本和圆放在0,0位置,然后使用transform/translate将它们移动到中心,你的代码将变得更加直观。

<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200" preserveAspectRatio="xMinYMin">
  <symbol id="circle" viewBox="0 0 16 16">
    <circle cx="8" cy="8" r="7"></circle>
  </symbol>
  <g transform="translate(100 100)">
    <use href="#circle" transform="translate(-20 -20)" width="40" height="40"/>
    <text text-anchor="middle" dominant-baseline="middle">文本</text>
  </g>
</svg>
circle {
  fill: #dcdcc6;
}

text {
  font-size: 11px;
  fill: rgb(51, 65, 74);
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}
var angle = 0;
var text = document.querySelector('text');
text.addEventListener('click', function(event) {
  angle += 45;
  this.setAttribute('transform', 'rotate(' + angle + ')');
});
英文:

If you place the text and the circle in 0,0 and then move them to the center using transform/translate you code becomes more straight-forward.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var angle = 0;
var text = document.querySelector(&#39;text&#39;);
text.addEventListener(&#39;click&#39;, function(event) {
angle += 45;
this.setAttribute(&#39;transform&#39;, &#39;rotate(&#39; + angle + &#39;)&#39;);
});

<!-- language: lang-css -->

circle {
fill: #dcdcc6;
}
text {
font-size: 11px;
fill: rgb(51, 65, 74);
font-family: &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;
}

<!-- language: lang-html -->

&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;200&quot; height=&quot;200&quot; viewBox=&quot;0 0 200 200&quot; preserveAspectRatio=&quot;xMinYMin&quot;&gt;
&lt;symbol id=&quot;circle&quot; viewBox=&quot;0 0 16 16&quot;&gt;
&lt;circle cx=&quot;8&quot; cy=&quot;8&quot; r=&quot;7&quot;&gt;&lt;/circle&gt;
&lt;/symbol&gt;
&lt;g transform=&quot;translate(100 100)&quot;&gt;
&lt;use href=&quot;#circle&quot; transform=&quot;translate(-20 -20)&quot; width=&quot;40&quot; height=&quot;40&quot;/&gt;
&lt;text text-anchor=&quot;middle&quot; dominant-baseline=&quot;middle&quot;&gt;TEXT&lt;/text&gt;
&lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->

答案2

得分: 0

感谢AndTheGodsMadeLove
transform-origin CSS属性起了作用。
更改为:

this.setAttribute('transform', 'rotate(' + angle + ')');
this.setAttribute('transform-origin', 'center');

英文:

thanks to AndTheGodsMadeLove
The transform-origin CSS property made the trick.
Changed to:

this.setAttribute(&#39;transform&#39;, &#39;rotate(&#39; + angle + &#39;)&#39;);
this.setAttribute(&#39;transform-origin&#39;, &#39;center&#39;);

huangapple
  • 本文由 发表于 2023年3月8日 17:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定