uniapp小程序使用canvas画圆

<view class="container">
    <canvas canvas-id="arcCanvas" id="arcCanvas" class="arc-canvas"                                     
            width="300" height="300">
    </canvas>
</view>

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

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')方法获取

        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()

 

uniapp小程序中,使用canvas涂鸦实现撤销和恢复上一步的功能,可以采用类似于原生小程序的方式进行操作。以下是示例代码: ```vue <template> <canvas class="canvas" canvas-id="myCanvas" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas> <view class="tools"> <button class="btn" @tap="undo">撤销</button> <button class="btn" @tap="redo">恢复</button> </view> </template> <script> export default { data() { return { ctx: null, // canvas绘图上下文 startX: 0, // 起始x坐标 startY: 0, // 起始y坐标 drawData: [], // 绘制数据 index: 0 // 当前绘制步骤 } }, mounted() { // 获取canvas绘图上下文 let query = uni.createSelectorQuery().in(this) query.select('.canvas').fields({node: true, size: true}).exec((res) => { let canvas = res[0].node this.ctx = canvas.getContext('2d') }) }, methods: { // 监听touchstart事件 touchStart(e) { this.startX = e.touches[0].clientX this.startY = e.touches[0].clientY // 绘制起始点 this.ctx.beginPath() this.ctx.moveTo(this.startX, this.startY) // 将绘制数据添加到数组中 this.drawData.splice(this.index, this.drawData.length - this.index, { type: 'start', x: this.startX, y: this.startY }) this.index++ }, // 监听touchmove事件 touchMove(e) { let currentX = e.touches[0].clientX let currentY = e.touches[0].clientY // 绘制线条 this.ctx.lineTo(currentX, currentY) this.ctx.stroke() // 将绘制数据添加到数组中 this.drawData.splice(this.index, this.drawData.length - this.index, { type: 'move', x: currentX, y: currentY }) this.index++ }, // 监听touchend事件 touchEnd(e) { // 将绘制数据添加到数组中 this.drawData.splice(this.index, this.drawData.length - this.index, { type: 'end' }) this.index++ }, // 撤销操作 undo() { // 判断是否有可撤销的绘制步骤 if(this.index > 0) { this.index-- // 判断绘制数据类型 while(this.index > 0 && this.drawData[this.index - 1].type !== 'start') { this.index-- } // 重新绘制整个canvas this.redraw() } }, // 恢复上一步操作 redo() { // 判断是否有可恢复的绘制步骤 if(this.index < this.drawData.length) { // 判断绘制数据类型 while(this.index < this.drawData.length && this.drawData[this.index].type !== 'end') { this.index++ } this.index++ // 重新绘制整个canvas this.redraw() } }, // 重新绘制整个canvas redraw() { // 清空canvas this.ctx.clearRect(0, 0, uni.upx2px(750), uni.upx2px(1000)) // 循环绘制数据,重新绘制整个canvas for(let i = 0; i < this.index; i++) { let data = this.drawData[i] if(data.type === 'start') { // 绘制起始点 this.ctx.beginPath() this.ctx.moveTo(data.x, data.y) } else if(data.type === 'move') { // 绘制线条 this.ctx.lineTo(data.x, data.y) this.ctx.stroke() } } } } } </script> <style> .canvas { width: 750upx; height: 1000upx; background-color: #fff; } .tools { display: flex; justify-content: space-around; margin-top: 20upx; } .btn { width: 200upx; height: 80upx; line-height: 80upx; text-align: center; font-size: 32upx; color: #fff; background-color: #f00; border-radius: 40upx; } </style> ``` 以上代码中,使用canvas的touchstart、touchmove和touchend事件来监听用户的绘制操作,并将绘制数据存储到数组中。同时,还定义了撤销和恢复上一步操作的方法,并通过重新绘制整个canvas来实现撤销和恢复上一步操作的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值