操纵 svg circle origin 以围绕中心创建旋转动画
Manipulate svg circle origin to create rotation animation around center
我通过两个 <circle />
在页面上绘制了一个加载器(微调器)。需要以原点为中心沿不同方向旋转两条路径,因此,圆圈围绕 SVG 的中心旋转并且不平移,据说。
正在尝试制作动画 transform: rotate(360deg)
。路径变得混乱并起源于其他地方。尝试管理 viewBox
以获得预期结果但没有成功。
import React, { PureComponent } from 'react';
import styled from 'styled-components';
import { prop } from 'styled-tools';
class Loader extends PureComponent {
render() {
return (
<Spinner
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
preserveAspectRatio="xMidYMid"
viewBox="0 0 100 100"
>
<circle
className='outer'
cx="50"
cy="50"
r="40"
fill="none"
stroke="#374a67"
stroke-dasharray="63 63"
stroke-linecap="round"
stroke-width="4"
/>
<circle
className='inner'
cx="50"
cy="50"
r="35"
fill="none"
stroke="#d50000"
stroke-dasharray="55 55"
stroke-dashoffset="55"
stroke-linecap="round"
stroke-width="4"
/>
</Spinner>
)
}
}
const Spinner = styled.svg`
& .outer {
animation: rotate 2s linear infinite;
}
& .inner {
animation: reverseRotate 2s linear infinite;
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}
@keyframes reverseRotate {
100% {
transform: rotate(-360deg);
}
}
`;
export default Loader;
不知道如何从我的代码中制作出实际工作的片段,对不起
这是我当前的动画示例:
欢迎来到 Stack。
您需要进行一些小调整才能使其正常工作。
- 只需使用一个从 0% 到 100% 的动画。
动画从 0deg
到 360deg
@keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
对于反转动画,可以使用
反转方向
animation-direction: alternate;
在你的 CSS
您需要在 svg 的中心设置 transform-origin
。但是你可以做不同的事情。您可以像这样为 stroke-dashoffset
设置动画,而不是设置 transform
的动画:
.outer {
stroke-dashoffset:0;
animation: rotate 2s linear infinite;
}
.inner {
animation: reverseRotate 2s linear infinite;
}
@keyframes rotate {
100% {
stroke-dashoffset:126px;
}
}
@keyframes reverseRotate {
100% {
stroke-dashoffset:-55px;
}
}
svg{border:1px solid}
<svg xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
preserveAspectRatio="xMidYMid"
viewBox="0 0 100 100"
>
<circle
class='outer'
cx="50"
cy="50"
r="40"
fill="none"
stroke="#374a67"
stroke-dasharray="63"
stroke-linecap="round"
stroke-width="4"
/>
<circle
class='inner'
cx="50"
cy="50"
r="35"
fill="none"
stroke="#d50000"
stroke-dasharray="55"
stroke-dashoffset="55"
stroke-linecap="round"
stroke-width="4"
/>
</svg>
我通过两个 <circle />
在页面上绘制了一个加载器(微调器)。需要以原点为中心沿不同方向旋转两条路径,因此,圆圈围绕 SVG 的中心旋转并且不平移,据说。
正在尝试制作动画 transform: rotate(360deg)
。路径变得混乱并起源于其他地方。尝试管理 viewBox
以获得预期结果但没有成功。
import React, { PureComponent } from 'react';
import styled from 'styled-components';
import { prop } from 'styled-tools';
class Loader extends PureComponent {
render() {
return (
<Spinner
xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
preserveAspectRatio="xMidYMid"
viewBox="0 0 100 100"
>
<circle
className='outer'
cx="50"
cy="50"
r="40"
fill="none"
stroke="#374a67"
stroke-dasharray="63 63"
stroke-linecap="round"
stroke-width="4"
/>
<circle
className='inner'
cx="50"
cy="50"
r="35"
fill="none"
stroke="#d50000"
stroke-dasharray="55 55"
stroke-dashoffset="55"
stroke-linecap="round"
stroke-width="4"
/>
</Spinner>
)
}
}
const Spinner = styled.svg`
& .outer {
animation: rotate 2s linear infinite;
}
& .inner {
animation: reverseRotate 2s linear infinite;
}
@keyframes rotate {
100% {
transform: rotate(360deg);
}
}
@keyframes reverseRotate {
100% {
transform: rotate(-360deg);
}
}
`;
export default Loader;
不知道如何从我的代码中制作出实际工作的片段,对不起
这是我当前的动画示例:
欢迎来到 Stack。
您需要进行一些小调整才能使其正常工作。
- 只需使用一个从 0% 到 100% 的动画。
动画从
0deg
到360deg
@keyframes rotate { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
对于反转动画,可以使用
反转方向animation-direction: alternate;
在你的 CSS
您需要在 svg 的中心设置 transform-origin
。但是你可以做不同的事情。您可以像这样为 stroke-dashoffset
设置动画,而不是设置 transform
的动画:
.outer {
stroke-dashoffset:0;
animation: rotate 2s linear infinite;
}
.inner {
animation: reverseRotate 2s linear infinite;
}
@keyframes rotate {
100% {
stroke-dashoffset:126px;
}
}
@keyframes reverseRotate {
100% {
stroke-dashoffset:-55px;
}
}
svg{border:1px solid}
<svg xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
preserveAspectRatio="xMidYMid"
viewBox="0 0 100 100"
>
<circle
class='outer'
cx="50"
cy="50"
r="40"
fill="none"
stroke="#374a67"
stroke-dasharray="63"
stroke-linecap="round"
stroke-width="4"
/>
<circle
class='inner'
cx="50"
cy="50"
r="35"
fill="none"
stroke="#d50000"
stroke-dasharray="55"
stroke-dashoffset="55"
stroke-linecap="round"
stroke-width="4"
/>
</svg>