首页 > 其他分享 >Vue canvas绘制圆形进度条动画加载

Vue canvas绘制圆形进度条动画加载

时间:2024-07-03 11:21:14浏览次数:1  
标签:canvas Vue 进度条 cirX cirY context title 绘制

父组件代码:

<template>   <!-- 创建 -->   <div class="resource-warp-box">     <div class="center-box">       <div class="used-source">         <pageTitle title="已用资源" />         <div class="progress-box">           <div             v-for="(item, index) in quotaList"             :key="index"             class="progress-content"           >             <circle-progress :percent="item.proportionNum" :strokeStyle="item.color" :id="index" />             <div class="title">{{ item.title }}</div>           </div>         </div>       </div>     </div>   </div> </template> <script> import CircleProgress from './components/circleProgress.vue'; export default {   components: {     CircleProgress   },   data() {     return {       quotaList: [         {           title: "CPU",           color: "#1161E4",           proportionNum: 33         },         {           title: "GPU",           color: "#5860F0",           proportionNum: 50         },         {           title: "内存",           color: "#09D99F",           proportionNum: 80         }       ],     };   },   mounted() {       },   methods: {       } }; </script> <style lang="scss" scoped> .center-box {     display: flex;     margin-top: 12px;     .used-source {       background-color: #ffffff;       width: 50%;       height: 294px;       border-radius: 6px;       .progress-box {         display: flex;         margin-top: 45px;         .progress-content {           width: 33.3333%;           display: flex;           flex-direction: column;           justify-content: center;           align-items: center;           .title {             margin-top: 28px;             font-weight: 400;             font-size: 16px;             color: #333333;           }         }       }     } } </style>   子组件CircleProgress 代码:   <template>   <div class="canvas">     <canvas :id="id" width="220" height="220" style="width:110px;height:110px;"></canvas>   </div> </template>
<script> export default {   data () {     return {       num:0,       canvas: '',       context: '',       cirX: '',       cirY: '',       rad: '',       n: 0,       speed: 450,       r: 49     }   },   props:[     'percent', // 进度条值     'strokeStyle', // 进度条值背景颜色     'id'   ],   mounted () {     this.canvas = document.getElementById(this.id)     this.context = this.canvas.getContext("2d")     this.context.scale(2,2);     this.cirX = 55,//this.canvas.width/ 2     this.cirY = 55,//this.canvas.height/ 2     this.rad = Math.PI * 2 / 100     this.DreamLoading()   },   created () {   },   methods:{     //绘制最外层细圈     writeCircle(){         this.context.save(); //save和restore可以保证样式属性只运用于该段canvas元素         this.context.beginPath(); //开始路径         this.context.strokeStyle = '#f5f8fc'; //设置边线的颜色         this.context.lineWidth = 12;         this.context.arc(this.cirX, this.cirY, this.r, 0, Math.PI * 2, false); //画一个圆的路径         this.context.stroke(); //绘制边线         this.context.closePath();     },     //绘制文本     writeText(n){         this.context.save();         this.context.font = "14px MicrosoftYaHei";         this.context.fillStyle="#333333"; //字体颜色         this.context.fillText(n.toFixed(0) + "%", this.cirX - 13 , this.cirY + 6); //绘制实心         //context.strokeStyle = "#49f";         // context.strokeText(n.toFixed(0)+"%",cirX - 30 ,cirY +10); //绘制空心         this.context.stroke();         this.context.restore();     },     //绘制蓝色外圈     writeBlue(n){         this.context.save();         this.context.strokeStyle = this.strokeStyle;         this.context.lineWidth = 12;         this.context.lineCap = 'round';         this.context.beginPath();         this.context.arc(this.cirX, this.cirY, this.r, -Math.PI/2,-Math.PI/2+ this.rad * n, false);         this.context.stroke();         this.context.restore();     },     DreamLoading(){         //清除所有,重新绘制         this.context.clearRect(0,0,this.canvas.width,this.canvas.height)         this.writeCircle();         this.writeText(this.n);         this.writeBlue(this.n)         if(this.n <= this.percent){             this.n = this.n + 0.4;         }else {           return this.n = 0;         }         requestAnimationFrame(this.DreamLoading);     }   } } </script> <style scoped> .canvas {     width: 110px;     height: 110px; } </style>

标签:canvas,Vue,进度条,cirX,cirY,context,title,绘制
From: https://www.cnblogs.com/xiaofang234/p/18281244

相关文章

  • VuePress 的更多配置
    08.更多配置现在,读者应该对VuePress、主题和插件等有了基本的认识,除了插件,VuePress自身也有很多有用的配置,这里简单说明下。‍‍VuePress的介绍在介绍了VuePress的基本使用、主题和插件的概念之后,我们再来看看官网对于VuePress的介绍,就很好理解了:VuePress由两部分......
  • 掉了两根头发后,我悟了!vue3的scoped原来是这样避免样式污染(下)
    前言在上一篇掉了两根头发后,我悟了!vue3的scoped原来是这样避免样式污染(上)文章中我们讲了使用scoped后,vue是如何给CSS选择器添加对应的属性选择器[data-v-x]。这篇文章我们来接着讲使用了scoped后,vue是如何给html增加自定义属性data-v-x。注:本文中使用的vue版本为3.4.19,@vitejs/......
  • vue 监视属性
    <divid="root"><h2>今天天气很{{info}}</h2><button@click="changeWeather">切换天气</button><hr/><h3>a的值是{{numbers.a}}</h3><button@click="add">点我让a+1</bu......
  • 基于SpringBoot+Vue+uniapp的论文管理系统的详细设计和实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示具体实现截图技术栈后端框架SpringBoot前端框架Vue持久层框架MyBaitsPlus系统测试系统测试目的系统功能测试系统测试结论为什么选择我代码参考数据库参考源码获取前言......
  • vue2项目的打包以及部署
        打包          当我们写好vue2的项目后,可以通过npmbuild来对项目进行打包npmbuild        打包完成后我们可以看到在当面目录下生成了dis目录,src下的文件都会被打包进这个目录里,当然打包后的文件我们不能直接在浏览器打开,需要进行部署......
  • java基于ssm+vue 大学生兼职信息系统
    1管理员登录管理员输入个人的用户名、密码和角色登录系统,这时候系统的数据库就会在进行查找相关的信息,如果我们输入的用户名、密码和角色不正确,数据库就会提示出错误的信息提示,同时会提示管理员重新输入自己的用户名、密码、角色,直到账号密码输入成功后,会提登录成功的信息。......
  • 前端vue3项目dagre-d3基础配置项及流程图组件示例(包括安装依赖)
    目录引言d3是什么?dagre是什么?dagre-d3是什么?dagre-d3配置项流程图示例依赖安装组件示例总结引言因为很多文档都是英文,刚开始调研的时候比较费劲,文档里的配置像示例又比较分散,就自己整理了一下,附上测试时写的示例d3是什么?d3.js 是一个强大的JavaScript库,用于在......
  • 【electron-vite+live2d+vue3+element-plus】实现桌面模型宠物+桌面管理系统应用(踩坑)
    脚手架项目使用electron-vite脚手架搭建ps:还有一个框架是electron-vite,这个框架我发现与pixi库有冲突,无法使用,如果不用pixi也可以用这个脚手架。node版本建议18+----------------------------------------------------------------------------------------运行live2D......
  • Vuex
    Vuex什么是Vuex?概念:专门在Vue中实现集中式状态(数据)管理的一个Vue插件,对应用中多个组件的共享状态进行集中式管理(读/写),也是组件间通信的方式,且适用于任意组件间通信之前想要传递数据,可以使用全局事件总线/消息订阅去实现,但是如果有很多组件都想要去读和写某个组件中的数据,那......
  • 多个vue项目nginx部署流程
    nginx部署流程#在nginx.conf中配置#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}http{incl......