转换 SVG 对象

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

Transforming svg objects

问题

为什么这个SVG代码能够成功旋转左侧的正方形,但不能旋转右侧的正方形?

<svg id="work_window" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 30">
    <defs>
        <style>
            #rectangle1 {
                fill: blue;
            }
            
            #rectangle2 {
                fill: blue;
                transform: rotate(30,0,10); <!-- 这里进行旋转 -->
            }
        </style>
        <rect id="rectangle1" height="10" width="10" transform="rotate(30,0,10)"/> <!-- 这里进行旋转 -->
        <rect id="rectangle2" height="10" width="10" />
    </defs>
    <use href="#rectangle1" x="20" y="10"/>
    <use href="#rectangle2" x="50" y="10"/>
</svg>

如果尝试使用缩放变换,这个问题就不存在了。

<svg id="work_window" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 30">
    <defs>
        <style>
            #rectangle1 {
                fill: blue;
            }
            
            #rectangle2 {
                fill: blue;
                transform: scale(2); <!-- 这里进行缩放 -->
            }
        </style>
        <rect id="rectangle1" height="10" width="10" transform="scale(2)"/> <!-- 这里进行缩放 -->
        <rect id="rectangle2" height="10" width="10" />
    </defs>
    <use href="#rectangle1" x="20" y="10"/>
    <use href="#rectangle2" x="50" y="10"/>
</svg>

我包含了fill属性作为测试,以查看是否无法以这种方式为use放置的对象设置样式,但在这两种情况下都成功设置了颜色。

英文:

Why does this SVG code successfully rotate the left square but not the right?

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

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

&lt;svg id=&quot;work_window&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 100 30&quot;&gt;
    &lt;defs&gt;
        &lt;style&gt;
            #rectangle1 {
                fill: blue;
            }
            
            #rectangle2 {
                fill: blue;
                transform: rotate(30,0,10); &lt;!-- scale here --&gt;
            }
        &lt;/style&gt;
        &lt;rect id=&quot;rectangle1&quot; height=&quot;10&quot; width=&quot;10&quot; transform=&quot;rotate(30,0,10)&quot;/&gt; &lt;!-- scale here --&gt;
        &lt;rect id=&quot;rectangle2&quot; height=&quot;10&quot; width=&quot;10&quot; /&gt;
    &lt;/defs&gt;
    &lt;use href=&quot;#rectangle1&quot; x=&quot;20&quot; y=&quot;10&quot;/&gt;
    &lt;use href=&quot;#rectangle2&quot; x=&quot;50&quot; y=&quot;10&quot;/&gt;
&lt;/svg&gt;

<!-- end snippet -->

This is not an issue if you try a scale transform instead.

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

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

&lt;svg id=&quot;work_window&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 100 30&quot;&gt;
    &lt;defs&gt;
        &lt;style&gt;
            #rectangle1 {
                fill: blue;
            }
            
            #rectangle2 {
                fill: blue;
                transform: scale(2); &lt;!-- scale here --&gt;
            }
        &lt;/style&gt;
        &lt;rect id=&quot;rectangle1&quot; height=&quot;10&quot; width=&quot;10&quot; transform=&quot;scale(2)&quot;/&gt; &lt;!-- scale here --&gt;
        &lt;rect id=&quot;rectangle2&quot; height=&quot;10&quot; width=&quot;10&quot; /&gt;
    &lt;/defs&gt;
    &lt;use href=&quot;#rectangle1&quot; x=&quot;20&quot; y=&quot;10&quot;/&gt;
    &lt;use href=&quot;#rectangle2&quot; x=&quot;50&quot; y=&quot;10&quot;/&gt;
&lt;/svg&gt;

<!-- end snippet -->

I included the fill attribute as a test to see if objects placed by use couldn't be styled in this way, but the coloring was successful in both cases.

答案1

得分: 4

你在CSS中使用了SVG语法,这样是行不通的。等价的CSS语法

transform: rotate(30deg);
transform-origin: 0 10px;
<svg id="work_window" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 100 30">
    <defs>
        <style>
            #rectangle1 {
                fill: blue;
            }
            
            #rectangle2 {
                fill: blue;
                transform: rotate(30deg);
                transform-origin: 0 10px;
            }
        </style>
        <rect id="rectangle1" height="10" width="10" transform="rotate(30,0,10)"/> <!-- 在此处缩放 -->
        <rect id="rectangle2" height="10" width="10" />
    </defs>
    <use href="#rectangle1" x="20" y="10"/>
    <use href="#rectangle2" x="50" y="10"/>
</svg>
英文:

You're using the SVG syntax in CSS, this won't work. The equivalent CSS syntax is

transform: rotate(30deg);
transform-origin: 0 10px;

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

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

&lt;svg id=&quot;work_window&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 100 30&quot;&gt;
    &lt;defs&gt;
        &lt;style&gt;
            #rectangle1 {
                fill: blue;
            }
            
            #rectangle2 {
                fill: blue;
                transform: rotate(30deg);
                transform-origin: 0 10px;
            }
        &lt;/style&gt;
        &lt;rect id=&quot;rectangle1&quot; height=&quot;10&quot; width=&quot;10&quot; transform=&quot;rotate(30,0,10)&quot;/&gt; &lt;!-- scale here --&gt;
        &lt;rect id=&quot;rectangle2&quot; height=&quot;10&quot; width=&quot;10&quot; /&gt;
    &lt;/defs&gt;
    &lt;use href=&quot;#rectangle1&quot; x=&quot;20&quot; y=&quot;10&quot;/&gt;
    &lt;use href=&quot;#rectangle2&quot; x=&quot;50&quot; y=&quot;10&quot;/&gt;
&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月2日 09:41:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386650.html
匿名

发表评论

匿名网友

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

确定