带百分比的虚线圆条

2024-03-28

我正在尝试制作一个带有虚线的圆形进度条。我以编程方式创建stroke-dasharray and stroke-dashoffset用百分比画一个圆。

我需要绘制虚线圆,而不是实心圆,如下所示:

我无法将实心圆更改为虚线圆。我是否遗漏了一些东西,或者我是否需要改变我的逻辑来绘制虚线圆圈?

https://jsfiddle.net/6mu97jyL/ https://jsfiddle.net/6mu97jyL/

class CircularProgressBar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    // Size of the enclosing square
    const sqSize = this.props.sqSize;
    // SVG centers the stroke width on the radius, subtract out so circle fits in square
    const radius = (this.props.sqSize - this.props.strokeWidth) / 2;
    // Enclose cicle in a circumscribing square
    const viewBox = `0 0 ${sqSize} ${sqSize}`;
    // Arc length at 100% coverage is the circle circumference
    const dashArray = radius * Math.PI * 2;
    // Scale 100% coverage overlay with the actual percent
    const dashOffset = dashArray - dashArray * this.props.percentage / 100;

    return (
      <svg
          width={this.props.sqSize}
          height={this.props.sqSize}
          viewBox={viewBox}>
          <circle
            className="circle-background"
            cx={this.props.sqSize / 2}
            cy={this.props.sqSize / 2}
            r={radius}
            strokeWidth={`${this.props.strokeWidth}px`} />
          <circle
            className="circle-progress"
            cx={this.props.sqSize / 2}
            cy={this.props.sqSize / 2}
            r={radius}
            strokeWidth={`${this.props.strokeWidth}px`}
            transform={`rotate(-90 ${this.props.sqSize / 2} ${this.props.sqSize / 2})`}
            style={{
              strokeDasharray: dashArray,
              strokeDashoffset: dashOffset
            }} />
      </svg>
    );
  }
}

CircularProgressBar.defaultProps = {
  sqSize: 200,
  percentage: 25,
  strokeWidth: 10
};

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      percentage: 25
    };

    this.handleChangeEvent = this.handleChangeEvent.bind(this);
  }

  handleChangeEvent(event) {
    this.setState({
      percentage: event.target.value
    });
  }

  render() {
    return (
      <div>
          <CircularProgressBar
            strokeWidth="10"
            sqSize="200"
            percentage={this.state.percentage}/>
          <div>
            <input 
              id="progressInput" 
              type="range" 
              min="0" 
              max="100" 
              step="1"
              value={this.state.percentage}
              onChange={this.handleChangeEvent}/>
          </div>
        </div>
    );
  }
}

ReactDOM.render(<App/>, document.getElementById('app'));
#app {
  margin-top: 40px;
  margin-left: 50px;
}

#progressInput {
  margin: 20px auto;
  width: 30%;
}

.circle-background,
.circle-progress {
  fill: none;
}

.circle-background {
  stroke: #ddd;
}

.circle-progress {
  stroke: #F99123;
  stroke-linecap: round;
  stroke-linejoin: round;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.0/umd/react-dom.production.min.js"></script>

<div class="container">
  <div class="text-center" id="app">
  </div>

</div>

不确定这是否在您正在寻找的范围内。 (下面是完整的片段/演示)

我不是这个主题的专家,所以可能还有另一种选择(比如两个具有不同样式的半圆) - 但这里基本上所做的是在实心圆的顶部放置另一个圆,并确保它具有相同的笔画颜色与页面相同。然后,这将掩盖后面圆圈的间隙(基本上隐藏圆圈的一部分)。

          <circle
            className="circle-dashes"
            cx={this.props.sqSize / 2}
            cy={this.props.sqSize / 2}
            r={radius}
            strokeWidth={`${this.props.strokeWidth}px`}
            style={{
              strokeDasharray: "5 10" // Adjust the spacing here
            }} />

css:

.circle-dashes {
  stroke: #FFF;
  fill: none;
}

并删除

  stroke-linecap: round;
  stroke-linejoin: round;

一些小的调整可以满足您的需求,希望您能得到它!

如果您使用其他背景颜色查看应用程序,变化可能会更明显。

class CircularProgressBar extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    // Size of the enclosing square
    const sqSize = this.props.sqSize;
    // SVG centers the stroke width on the radius, subtract out so circle fits in square
    const radius = (this.props.sqSize - this.props.strokeWidth) / 2;
    // Enclose cicle in a circumscribing square
    const viewBox = `0 0 ${sqSize} ${sqSize}`;
    // Arc length at 100% coverage is the circle circumference
    const dashArray = radius * Math.PI * 2;
    // Scale 100% coverage overlay with the actual percent
    const dashOffset = dashArray - dashArray * this.props.percentage / 100;

    return (
      <svg
          width={this.props.sqSize}
          height={this.props.sqSize}
          viewBox={viewBox}>
          <circle
            className="circle-background"
            cx={this.props.sqSize / 2}
            cy={this.props.sqSize / 2}
            r={radius}
            strokeWidth={`${this.props.strokeWidth}px`} />
          <circle
            className="circle-progress"
            cx={this.props.sqSize / 2}
            cy={this.props.sqSize / 2}
            r={radius}
            strokeWidth={`${this.props.strokeWidth}px`}
            // Start progress marker at 12 O'Clock
            transform={`rotate(-90 ${this.props.sqSize / 2} ${this.props.sqSize / 2})`}
            style={{
              strokeDasharray: dashArray,
              strokeDashoffset: dashOffset
            }} />
            
            <circle
            className="circle-dashes"
            cx={this.props.sqSize / 2}
            cy={this.props.sqSize / 2}
            r={radius}
            strokeWidth={`${this.props.strokeWidth}px`}
            style={{
              strokeDasharray: "5 10"
            }} />
      </svg>
    );
  }
}

CircularProgressBar.defaultProps = {
  sqSize: 200,
  percentage: 25,
  strokeWidth: 10
};

class App extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      percentage: 25
    };

    this.handleChangeEvent = this.handleChangeEvent.bind(this);
  }

  handleChangeEvent(event) {
    this.setState({
      percentage: event.target.value
    });
  }

  render() {
    return (
      <div>
          <CircularProgressBar
            strokeWidth="10"
            sqSize="200"
            percentage={this.state.percentage}/>
          <div>
            <input 
              id="progressInput" 
              type="range" 
              min="0" 
              max="100" 
              step="1"
              value={this.state.percentage}
              onChange={this.handleChangeEvent}/>
          </div>
        </div>
    );
  }
}

ReactDOM.render(
  <App/>,
  document.getElementById('app')
);
#app {
  margin-top: 40px;
  margin-left: 50px;
}

#progressInput {
  margin: 20px auto;
  width: 30%;
}

.circle-background,
.circle-progress {
  fill: none;
}

.circle-background {
  stroke: #ddd;
}

.circle-dashes {
  stroke: #fff;
  fill: none;
}

.circle-progress {
  stroke: #F99123;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div class="container">
  <div class="text-center" id="app">
  </div>
		
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带百分比的虚线圆条 的相关文章

  • 需要一个正则表达式将 css 类添加到第一个和最后一个列表项

    更新 谢谢大家的意见 一些附加信息 它实际上只是我正在使用的一小部分标记 20 行 目的是利用正则表达式来完成工作 我还能够修改脚本 电子商务脚本 以在构建导航时插入类 我想限制我所采用的黑客数量 以便在更新到软件的最新版本时让事情变得更容
  • 如何将 vue3-openlayers 插件添加到 nuxt

    我有以下 main ts 文件Vue3 https v3 vuejs org import createApp from vue import App from App vue How to do this in nuxt3 import
  • linkedin js 如何是有效的 javascript

    LinkedIn Javascript 集成是通过以下方式完成的 我不明白 这怎么是一个有效的javascript 为什么 api key 没有被引用 脚本标签的主体带有src永远不会被执行 但是 加载的脚本可以像访问任何其他元素的内容一样
  • 2 个 SVG 路径的交集

    我需要检查两个 SVG Path 元素是否相交 检查边界框与 getBBox 太不准确了 我目前正在做的是迭代两条路径 getTotalLength 然后检查是否有两个点 getPointAtLength 是平等的 下面是一个片段 但正如您
  • 如何将值从孩子的孩子传递给父母?

    我有一个父组件 有一个子组件 它也有一个子组件 Parent Child One child of parent Child Two child of child 当在子二中定义一个值时 我使用回调将该值传递给子一 但我也想将相同的值传递回
  • 如何使用css网格制作一个垄断板?

    I want to create a monopoly board like There are following features in the board 角是方形的 比其他盒子大 每行的文本都面向特定的角度 我的基本 html 结构
  • 复合组件和 CSS

    I have newcss css formdiv width 30 margin 150 auto 和复合组件
  • React setState回调返回值

    我是 React 新手 我希望实现这种流程 set the state execute a function f an async one which returns a promise set the state again return
  • 如何创建显示/隐藏 Docusaurus 项目中所有详细标签状态的按钮?

    根据讨论here https stackoverflow com questions 58579048 how to add or remove the open attribute from all details tags in a r
  • Lodash _.hasIntersection?

    我想知道两个或多个数组是否有共同的项目 但我不在乎这些项目是什么 我知道 lodash 有一个 intersection方法 但我不需要它来遍历每个数组的每个项目 相反 我需要类似的东西 hasIntersection一旦找到第一个常见的出
  • 如何将类组件中的 props 发送到功能组件?

    我是 ReactJS 的初学者 需要知道如何将一个页面中的 props 值发送到另一个页面 道具位于第一页上我可以获取类组件值如何获取另一页中的值 提前致谢 墙色 jsx import React Component from react
  • 使标签充当输入按钮

    我怎样才能做一个 a href http test com tag test Test a 就像表单按钮一样 通过充当表单按钮 我的意思是 当单击链接执行操作时method get 或 post 以便能够通过 get 或 post 捕获它
  • 无法验证 CSRF 令牌的真实性 Rails/React

    我的 Rails 应用程序中有一个 React 组件 我正在尝试使用它fetch 发送一个POST对于我在本地主机上托管的 Rails 应用程序 这给了我错误 ActionController InvalidAuthenticityToke
  • 鼠标输入时反应显示按钮

    我有一个反应组件 它包含如下方法 mouseEnter console log this is mouse enter render var album list const albums this props if albums user
  • 如何从代码隐藏中向我的 div 添加点击事件?

    如何从代码隐藏中向我的 div 添加点击事件 当我点击 div 时 会出现一个消息框 其中显示 您想删除它吗 并在框中显示 是 或 否 全部来自后面的代码 while reader Read System Web UI HtmlContro
  • 如何从 Visual Studio Code API 打开浏览器

    我只是在探索一种从用于开发扩展的 Visual Studio Code API 打开默认浏览器的方法 以下是我的代码 var disposable vscode commands registerCommand extension brow
  • 如何在 getStaticPaths 内添加 params 值数组

    我有一个页面 其结构如下 read slug number 我想要得到slug每个对应的值number in the getStaticPaths这是代码 export async function getStaticPaths const
  • React-Redux:绑定按键操作以启动减速器序列的规范方法是什么?

    这是一个关于react redux的新手问题 我花了几个小时四处搜寻才发现 所以我发布了这个问题 然后为后代回答 也可能是代码审查 我正在使用 React Redux 创建一个游戏 我想使用 WASD 键在小地图上移动角色 这只是更大努力的
  • 如何使用node.js获取屏幕分辨率

    我需要使用 node js 获取屏幕分辨率 但以下代码不起作用 var w screen width var h screen height 这也行不通 var w window screen width var h window scre
  • 如何向 SvelteKit/Vite 应用添加版本号?

    我正在尝试在我的 SvelteKit 应用程序中创建一个系统 它会在某个页面上向您显示有关当前应用程序版本的信息 最好是 Git 提交哈希和描述 我尝试使用Vite的定义功能 https vitejs dev config define在构

随机推荐

  • Flutter 将两个 Firestore 流合并为一个流

    我只想执行 OR 运算并将两个查询的结果放入一个流中 这是我的带有单个流的代码 StreamBuilder stream Firestore instance collection list where id isEqualTo false
  • Python:判断字符串是否包含数学?

    给定这些字符串 1 2 apple pear 如何使用 Python 3 5 确定第一个字符串包含数学问题并且没有其他的而第二根弦没有 这是一种方法 import ast UNARY OPS ast UAdd ast USub BINARY
  • Amazon S3 托管流媒体视频 [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 如果我公开提供 Amazon s3 MP4 资源 然后在该资源的 URL 周围添加 Html5 视频标
  • 如果所有变量都是 float16 而不是 float32,如何运行定义 Tensorflow 图

    默认情况下 Tensorflow 变量采用 float32 格式 为了节省内存 我尝试以 float16 运行 在我的图表中 我在每个可以将数据类型定义为 float16 的地方都这样做了 但是 当我运行代码时出现错误 下面是我的代码 im
  • 在过程触发器内动态插入记录

    我们正在寻求将我们的数据库转换为 Postgres 9 3 5 我对此没有经验 并且我正在尝试让我们的审计表启动并运行 我知道每个表都需要自己的触发器 但所有触发器都可以调用单个函数 表上的触发器正在传递需要审核的列的列表 因为我们的某些列
  • WFFM 8.0 - 缺少保存到数据库操作

    我刚刚安装了 Web Forms For Marketers 8 0 并做了一个测试表单 尝试选择Save to Database从操作列表中但它不存在 另外 我已经检查过 sitecore system Modules Web Forms
  • 哪里有指示不应写入“程序文件”区域的指南?

    许多关于SO的问题都说 Windows开发人员指南 或 Windows设计指南 说你不应该将临时数据或程序数据写入程序文件区域 但据我所知 它们都没有真正链接到文档这就是说 搜索 MSDN 没有得到任何结果 Windows 将使该区域变为只
  • clojure(带超时...宏)

    我正在寻找一个宏 如果表达式完成时间超过 X 秒 它将引发异常 这个问题在这里有更好的答案 执行具有超时功能的函数 https stackoverflow com questions 6694530 executing a function
  • Visual Studio 将项目发布到一个简单的安装程序中

    我有一个相当大的项目 包含多个类 500 多个图像以及与该项目关联的 20 多个文本文件 我一直通过右键单击来发布我的项目project gt properties 然后单击 发布 选项卡 我已将文本文件和图像包含为resources已经
  • 如何使用 CSS 使 div 上的滚动条变粗?

    如果我使用 CSS 溢出属性 overflow scroll 默认情况下我得到一个细滚动条 我如何设计它以获得宽 且平坦 的滚动条 您可以在这里找到有关如何在多个浏览器上更改滚动条设计的答案 https stackoverflow com
  • Rails activesupport 通知 - 错误的数据库运行时值

    我正在尝试记录 REST API 应用程序的请求 我为此使用 Rails 通知 如下所示http railscasts com episodes 249 notifications in rails 3 http railscasts co
  • 更改 TortoiseGit 中的存储库 url

    我们刚刚将 git 存储库更新到了新位置 我正在使用 TortoiseGit 进行一些未提交的更改 我可以在任何地方更改文件夹引用吗 我在上下文菜单中没有看到该选项 如果可以避免的话 我宁愿不重新创建和合并 因为总共大约有 14 个存储库
  • 使用现有的 Rails 应用程序添加 twitter-bootstrap-rails

    我尝试将 twitter bootstrap rails 与现有的 Rails 应用程序一起使用 并在刷新页面时收到以下错误 没有要加载的文件 less 在 app assets stylesheets bootstrap and over
  • 如何将UIScrollView的触摸事件发送到其后面的视图?

    我在另一个视图之上有一个透明的 UIScrollView 滚动视图有内容 文本和图像 用于显示信息 它后面的视图有一些用户应该能够点击的图像 并且它们上面的内容可以使用提到的滚动视图进行滚动 我希望能够正常使用滚动视图 尽管没有缩放 但是当
  • 节点检查器无法连接到节点

    我运行节点 node debug app OR node debug brk app 它回应 debugger listening on port 5858 Express server listening on port 1338 我现在
  • 将 varchar 值转换为 int,如果输入错误,不会引发异常

    有没有办法调用 Sql Server 函数 Convert Cast 而不让它们抛出异常 基本上 我有一列包含字母数字数据 我正在从字段中提取值 并且我想将数据显示为整数值 有时提取的数据不是数字 在这些情况下我希望 Sql Server
  • 为什么我嵌入的 JointJS 元素重叠?

    我正在研究 JointJS 图 使用 DirectedGraph 来处理布局 我试图实现类似于下图的效果 我需要将节点 A B C D E F G H I J 概述 或包含在单独的节点 Foo Bar Hmm 中 当我将所有元素添加到图表中
  • Android 插件将使用哪个 cmake?

    在 Android Studio 3 3 中 我使用本机 C 库 它是用 CMake 构建的 Android 插件 v 3 2 1 将根据配置选择 内置 或 外部 cmakeexternalNativeBuild 如记录于开发者 andro
  • 证书被苹果拒绝

    While configuring certificate for Push Notification on my ios build of cordova project using OneSignal the certificate i
  • 带百分比的虚线圆条

    我正在尝试制作一个带有虚线的圆形进度条 我以编程方式创建stroke dasharray and stroke dashoffset用百分比画一个圆 我需要绘制虚线圆 而不是实心圆 如下所示 我无法将实心圆更改为虚线圆 我是否遗漏了一些东西