How to rotate multiple SVG reused elements around same origin?

假如想象 提交于 2021-02-05 09:44:38

问题


How can I rotate 3 irregular SVG circles around same origin? They are written as a path and reused. I have set transform-origin to center, what else am I missing? They should all be in the same space and overlap in center like in this image

<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


回答1:


You want transform-box: fill-box; i.e.

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



回答2:


When the svg draw is not centered on the canvas, this gets a bit more difficult. I've ajusted the SVG viewBox values, take a look at the following snippet:

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>


来源:https://stackoverflow.com/questions/53144046/how-to-rotate-multiple-svg-reused-elements-around-same-origin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!