如何围绕同一原点旋转多个SVG重用元素?

时间:2018-11-04 18:30:23

标签: css svg

如何围绕相同的原点旋转3个不规则的SVG圆?它们被编写为路径并被重用。我已将transform-origin设置为center,还缺少什么?它们都应该在同一空间中,并在中心重叠,就像这张图片enter image description here

<svg width="900" height="500" viewBox="0 0 900 500">
  <defs>
     <path id="circle" d="m 43.467262,110.65774 c -2.81733,25.95379 9.663408,54.24201 33.479611,66.3315 12.768318,6.52194 29.493997,6.26667 40.854417,-3.07058 12.79824,-9.29014 25.48168,-19.76411 33.26937,-33.78136 4.54432,-8.49226 5.12542,-19.52979 -0.73083,-27.56368 C 142.27364,100.40343 128.56364,93.579328 115.25185,88.674523 98.350761,82.775856 78.939082,80.223234 62.116925,87.733369 52.940099,92.163321 45.975566,100.79364 43.467262,110.65774 Z" />
  </defs>

  <g class="group">
      <use class="circle circle--1" xlink:href="#circle" /> 
      <use class="circle circle--2" xlink:href="#circle" />
      <use class="circle circle--3" xlink:href="#circle" />
  </g>
</svg>


svg {
  width: 900px;
  path {
    stroke: #333;
    stroke-width: 3px;
    fill: transparent;
  }
}
g {
    position: relative;
    transform-origin: center center;
}
.circle {
  transform-origin: 50% 50%;
  perspective: 500px;
  &--1 {
    transform: rotateZ(60deg);
  }
  &--2 {
    transform: rotateZ(120deg);
  }
  &--3 {
    transform: rotateZ(180deg);
  }
}

https://codepen.io/anon/pen/mQdLvX

2 个答案:

答案 0 :(得分:4)

您想要transform-box: fill-box;,即

.circle {
  transform-box: fill-box;
  transform-origin: 50% 50%;
  perspective: 500px;
  &--1 {
    transform: rotateZ(60deg);
  }
  &--2 {
    transform: rotateZ(120deg);
  }
  &--3 {
    transform: rotateZ(180deg);
  }
}

答案 1 :(得分:1)

当svg绘制不在画布上居中时,这会变得更加困难。我已经调整了SVG viewBox的值,请看以下代码段:

svg {
  border: 1px solid red;
}

svg path {
  stroke: #333;
  stroke-width: 3px;
  fill: transparent;
}

.circle {
  transform-origin: 50% 50%;
}

.circle--1 {
  transform: rotateZ(90deg);
}

.circle--2 {
  transform: rotateZ(160deg);
}

.circle--3 {
  transform: rotateZ(270deg);
}
<svg width="900" height="500" viewBox="0 0 250 250">
  <defs>
     <path id="circle" d="m 43.467262,110.65774 c -2.81733,25.95379 9.663408,54.24201 33.479611,66.3315 12.768318,6.52194 29.493997,6.26667 40.854417,-3.07058 12.79824,-9.29014 25.48168,-19.76411 33.26937,-33.78136 4.54432,-8.49226 5.12542,-19.52979 -0.73083,-27.56368 C 142.27364,100.40343 128.56364,93.579328 115.25185,88.674523 98.350761,82.775856 78.939082,80.223234 62.116925,87.733369 52.940099,92.163321 45.975566,100.79364 43.467262,110.65774 Z" />
  </defs>
  <g class="group">
    <use class="circle circle--1" xlink:href="#circle" /> 
    <use class="circle circle--2" xlink:href="#circle" />
    <use class="circle circle--3" xlink:href="#circle" />
  </g>
</svg>