gpt4 book ai didi

reactjs - 操作 svg 圆原点以创建围绕中心的旋转动画

转载 作者:行者123 更新时间:2023-12-03 13:42:42 25 4
gpt4 key购买 nike

我通过两个 <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;

不知道如何从我的代码片段中生成实际的工作片段,sry

这是我当前动画的示例:

Here's an example of my current animation

最佳答案

您需要将 transform-origin 设置在 svg 的中心。不过,您可以采取不同的做法。除了为 transform 设置动画之外,您还可以为 lines-dashoffset 设置动画,如下所示:

.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>

关于reactjs - 操作 svg 圆原点以创建围绕中心的旋转动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54136576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com