环形进度条组件

import { CSSProperties, FC, ReactNode, useEffect, useState } from 'react';
import classNames from 'classnames';
import './style.scss';

// strokeDasharray 的第一个值(填充部分长度)表示进度环的“实线”部分,应该有多长,而第二个值(圆周长)表示整个进度环的周长。
const getRingPercent = (percent: number, r: number) => {
    const perimeter = Math.PI * 2 * r;
    return (percent / 100) * perimeter + ' ' + perimeter;
};

export interface ProgressCircleProps {
    className?: string;
    style?: CSSProperties;
    children?: ReactNode;
    percent?: number;
    // 圆环颜色
    color?: string;
    // 圆环底色
    trackColor?: string;
    // 圆环尺寸
    size?: string | number;
    // 圆环厚度
    thickness?: number;
    // 动画持续时间
    dur?: number;
}

export const CircleProgress: FC<ProgressCircleProps> = (props) => {
    const {
        dur,
        className,
        style,
        children,
        percent = 0,
        color,
        trackColor,
        size,
        thickness = 4,
        ...restProps
    } = props;
    const [finalDashArray, setFinalDashArray] = useState('');
    const radius = 50 - thickness / 2;
    const perimeter = Math.PI * 2 * radius;

    const progressClass = classNames('progress-circle', className);
    const progressStyle = {
        width: size,
        height: size,
        ...style
    };

    const trackStyle = {
        stroke: trackColor,
        strokeWidth: thickness,
        r: radius
    };

    const trailStyle = {
        stroke: color,
        strokeDasharray: finalDashArray,
        strokeWidth: thickness,
        r: radius
    };

    const animateFrom = `0 ${perimeter}`;
    const animateTo = `${(percent / 100) * perimeter} ${perimeter}`;

    useEffect(() => {
        const finalDash = getRingPercent(percent, radius);
        setFinalDashArray(finalDash);
    }, [percent, radius]);

    return (
        <div {...restProps} className={progressClass} style={progressStyle}>
            <svg viewBox='0 0 100 100' className='progress-circle-graph'>
                <circle cx='50' cy='50' fill='none' className='progress-circle-track' style={trackStyle} />
                <circle cx='50' cy='50' fill='none' className='progress-circle-trail' style={trailStyle}>
                    <animate
                        attributeName='stroke-dasharray'
                        begin='0s'
                        dur={dur + 's'} // 动画持续时间,例如1秒
                        from={animateFrom}
                        to={animateTo}
                        fill='freeze'
                    />
                </circle>
            </svg>
            {children}
        </div>
    );
};

export default CircleProgress;

样式:

.progress-circle {
  position: relative;
  z-index: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.progress-circle-graph {
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
}
.progress-circle-trail {
  stroke-linecap: round;
  transform: rotate(-90deg);
  transform-origin: center center;
  transition: stroke-dasharray 3s ease,stroke .3s;
}

     

组件使用方法:

  <CircleProgress 
   percent={40} 
   color='#4CAF50' 
   trackColor='#EEEEEE' 
   size='120px' 
   thickness={8} 
   dur={0.01}>
   <h1>80分</h1>
  </CircleProgress>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杜的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值