css锥形渐变结合SVG实现环形进度条

css锥形渐变结合SVG实现环形进度条

准备:

<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%">
      <circle
        id="circle"
        cx="160"
        cy="160"
        r="150"
        stroke="green"
        stroke-dasharray="942"
        stroke-linecap="round"
        stroke-dashoffset="80"
        stroke-width="20"
        fill="transparent"
        transform="rotate(-90, 160, 160)"
      ></circle>
</svg>

demo

JS:

import { useEffect, useRef, useState } from 'react';
import styles from './index.less';
interface RingGradientChartParams {
  barWidth?: number; // 圆弧宽度
  percent?: number; // 百分比占比 注意范围(0-100)
  colorSteps?: string[]; // 柱条渐变颜色
  backgroundColor?: string; // 背景色
}

const IndexPage: React.FC<RingGradientChartParams> = (
  props: RingGradientChartParams,
) => {
  const {
    barWidth = 10,
    percent = 0,
    colorSteps = ['rgba(0, 216, 249, 0)', 'rgba(0, 216, 249, 1)'],
    backgroundColor = 'rgba(0, 216, 249, 0.3)',
  } = props;
  
  const chartRef = useRef<HTMLDivElement>(null);
  const [svgUrl, setSvgUrl] = useState<string>(); // 转为base64
  const [svgString, setSvgString] = useState<string>('');  // svg字符串
  const [boxSize, setBoxSize] = useState<number>(200); // 盒子尺寸

  useEffect(() => {
    const resizeObserver = new ResizeObserver((entries) => {
      for (const entry of entries) {
        if (entry.contentBoxSize) {
          // Firefox implements `contentBoxSize` as a single content rect, rather than an array
          const contentBoxSize = Array.isArray(entry.contentBoxSize)
            ? entry.contentBoxSize[0]
            : entry.contentBoxSize;
          const { inlineSize, blockSize } = contentBoxSize;
          // 取最小值
          setBoxSize(Math.min(inlineSize, blockSize));
        }
      }
    });
    if (chartRef.current) {
      resizeObserver.observe(chartRef.current);
    }
  }, []);

  useEffect(() => {
    // 实际占比
    const proportion = percent < 0 ? 0 : percent > 100 ? 100 : percent;

    // 半径
    const radius = boxSize / 2 - barWidth / 2;

    // 旋转角度
    // const rotate =
    //   Math.asin(barWidth / 2 / (2 * (radius - barWidth / 2))) * (180 / Math.PI);

    const svgStr = `
      <svg xmlns="http://www.w3.org/2000/svg" width="${boxSize}" height="${boxSize}">
        <circle
            id="circle"
            cx="${boxSize / 2}"
            cy="${boxSize / 2}"
            r="${radius}"
            stroke="green"
            stroke-dasharray="${2 * Math.PI * radius}"
            stroke-linecap="round"
            stroke-dashoffset="${2 * Math.PI * radius * (1 - proportion / 100)}"
            stroke-width="${barWidth}"
            fill="transparent"
            transform="rotate(${-90}, ${boxSize / 2}, ${boxSize / 2})"
        >
        <animate 
            attributeType="XML"
            attributeName="stroke-dashoffset"
            from="${2 * Math.PI * radius}" 
            to="${2 * Math.PI * radius * (1 - proportion / 100)}"
            dur="1s"
        />
        </circle>
      </svg>
    `;
    setSvgString(svgStr);
  }, [boxSize, barWidth, percent]);

  useEffect(() => {
    const base64 = window.btoa(svgString);
    setSvgUrl(`data:image/svg+xml;base64,${base64}`);
  }, [svgString]);
  return (
    <div
      className={styles.ringGradient}
      ref={chartRef}
      style={
        {
          '--mask': `url(${svgUrl})`,
          '--gradient': `conic-gradient(
        ${colorSteps[0]} 0%,
        ${colorSteps[1]} ${percent + 2}%,
        transparent ${percent + 2}%
      )`,
          '--bgColor': backgroundColor,
          '--barWidth': `${barWidth}px`,
        } as React.CSSProperties
      }
    >
      <div className={styles.gradientBar}></div>
    </div>
  );
};
export default IndexPage;

css:

.ringGradient {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  box-shadow: inset 0 0 0 var(--barWidth) var(--bgColor);
}
.gradientBar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: var(--gradient);
  -webkit-mask-image: var(--mask);
  mask-image: var(--mask);
}

最后效果图:
在这里插入图片描述
最后:有问题欢迎评论指正!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现SVG CSS动画的流光线效果,可以结合使用stroke-dasharray和stroke-dashoffset这两个属性。首先,为折线元素添加动画效果的class,设置stroke-dasharray的值为折线的总长度,然后通过改变stroke-dashoffset的值来实现流动效果。具体的步骤如下: 1. 在SVG中创建折线元素,并为其设置class属性以便添加动画效果。 2. 使用CSS样式为折线元素设置stroke颜色、宽度等属性,并添加animation属性来指定动画的名称、持续时间和循环方式。 3. 使用stroke-dasharray属性设置折线的总长度,可以根据实际需求调整该值。 4. 使用stroke-dashoffset属性设置折线的偏移量,初始值为折线的总长度,这样折线会完全隐藏起来。 5. 创建一个@keyframes规则,定义动画的关键帧。在关键帧中使用stroke-dashoffset属性来逐渐改变折线的偏移量,从而实现流动效果。 6. 将@keyframes规则与折线元素的动画属性关联起来,使动画生效。 下面是一个示例的代码片段,演示了如何实现SVG CSS动画的流光线效果: ```html <svg> <polyline class="flowing-line" points="50,50 200,50 200,200 50,200" /> </svg> <style> .flowing-line { stroke: #E5DA40; fill: transparent; stroke-width: 2; stroke-linecap: round; animation: flow 3s linear infinite; } @keyframes flow { 0% { stroke-dashoffset: 0; } 100% { stroke-dashoffset: -800; } } </style> ``` 希望这个回答能够帮助到您!如果您有其他相关问题,请随时提出。 相关问题: 1. 如何改变流光线的颜色和宽度? 2. 怎样调整流光线的速度和流动方向? 3. 能否在同一个SVG中同时应用多个流光线效果?

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值