uniapp小程序使用canvas画圆

javascript 复制代码
<view class="container">
    <canvas canvas-id="arcCanvas" id="arcCanvas" class="arc-canvas"                                     
            width="300" height="300">
    </canvas>
</view>

最开始我使用的**uni.createSelectorQuery()**来获取元素

javascript 复制代码
onMounted(() => {
  const query = uni.createSelectorQuery();
  query.select('#progressCanvas').node().exec((res) => {
    if (res[0] && res[0].node) {
      const canvas = res[0].node;
      const context = canvas.getContext('2d');
      const percentage = 75;  // 进度百分比,可以调整
      drawProgressBar(context, percentage);
    } else {
      console.error('Canvas node not found');
    }
  });
});

网上找了很多都是用这种方法,但是我放到小程序中,不起作用。

于是就换成了uni.createCanvasContext('arcCanvas')方法获取

javascript 复制代码
        const ctx = uni.createCanvasContext('arcCanvas');
		let canvas={width:250,height:250}
		// 外圈圆
		const outerRadius = 110;//半径
		const outerX = canvas.width / 2;//x轴
		const outerY = canvas.height / 2;//y轴
		ctx.beginPath();//开始画路径
		ctx.arc(outerX, outerY, outerRadius, 0, 2 * Math.PI);//画圆
		// 描边
		ctx.strokeStyle = 'white';//边框设置为白色
		ctx.stroke();//画出当前边框
		// 填充
		ctx.fillStyle='white';//填充为白色
		ctx.fill();//为当前圆填色
		
		 
		// 中间圆
		const middleRadius = 90;//半径
		const middleX = canvas.width / 2;//x轴
		const middleY = canvas.height / 2;//y轴
		ctx.beginPath();//开始画路径
		// 灰色
		ctx.arc(middleX, middleY, middleRadius, 1.5, 1.5 * Math.PI);//画圆
		// 设置边框宽
		ctx.lineWidth = 10;
		// 描边框
		ctx.strokeStyle = '#F5F5F7';
		ctx.stroke();
		// 蓝色填充实
		ctx.beginPath();
		ctx.arc(middleX, middleY, middleRadius, 1.5, 1.2 * Math.PI);
		ctx.lineWidth = 10;//设置边框宽度
		ctx.strokeStyle = '#00C3CE';
		ctx.stroke();
		  // 设置阴影的颜色
		 ctx.setShadow(0, 10, 10, '#E6F9FA');
		 ctx.stroke()
		// 保存状态,以便在中间圆内部绘制文字
		ctx.save();
		 
		// 内圈圆
		const innerRadius = 60;
		const innerX = canvas.width / 2;
		const innerY = canvas.height / 2;
		ctx.beginPath();
		ctx.arc(innerX, innerY, innerRadius, 0, 2 * Math.PI);
		ctx.lineWidth = 4;
		ctx.setShadow(0, 2,30, '#E6F9FA');
		ctx.stroke()
		ctx.strokeStyle = '#f3fafa';
		ctx.stroke();
		ctx.fillStyle = '#fff';
		ctx.fill();
		 
		// 在中间圆内居中加上文字
		const text = '80%';
		const text2 = '已修课程';
		ctx.font = 'bold 40px Arial';
		ctx.textAlign = 'center';
		ctx.fillStyle = 'black';
		ctx.fillText(text, innerX,  innerY+10); // 10是调整文字位置的偏移量,可以根据实际情况调整
		ctx.font = 'bold 14px Arial';
		ctx.fillText(text2, innerX, innerY + 30); // 10是调整文字位置的偏移量,可以根据实际情况调整
		// 恢复状态
		ctx.restore();
		ctx.draw()
相关推荐
慢热型网友.26 分钟前
【安装教程】Windows环境下Apache Jena Fuseki的安装与配置
windows·apache
hong16168828 分钟前
Apache Cordova和PhoneGap
apache·html5·phonegap
2401_8441394441 分钟前
PHP智慧教育新篇章优校管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
前端小凯1 小时前
小程序电量
小程序
YMZN913 小时前
基于SSM+小程序的医院挂号登录管理系统(医院4)(源码+sql脚本+视频导入教程+文档)
小程序
guanpinkeji3 小时前
线上搭子小程序:随时随地找搭子!
小程序·团队开发·小程序开发·搭子小程序·搭子·找搭子app
过期の秋刀鱼3 小时前
Nest.js实现一个简单的聊天室
javascript·后端·uni-app
qq_203527134 小时前
微信小程序配置prettier+eslint
微信小程序·小程序
谢尔登6 小时前
周家庄智慧旅游小程序
小程序·旅游
春天的菠菜6 小时前
【小程序】微信小程序课程 -2 快速上手
微信小程序·小程序