uni-app使用canvas层级过高解决方案

  • 效果图

在这里插入图片描述

  • 解决方法

    在canvas画完图后,使用uni.canvasToTempFilePath生成本地图进行展示
    注意:生成图片的时候用setTimeout延迟一下,否则生成的图片会为空
  • 代码

<template v-if="!cpbarCanvasImg && !cpbgCanvasImg">
	<canvas class="progress_bg" canvas-id="cpbg"></canvas>
	<canvas class="progress_bar" canvas-id="cpbar"></canvas>
</template>
<template v-if="cpbarCanvasImg && cpbgCanvasImg">
	<image class="progress_bg" :src="cpbgCanvasImg" mode=""></image>
	<image class="progress_bar" :src="cpbarCanvasImg" mode=""></image>
</template>

第一个圆

drawProgressbg: function() {
// 自定义组件实例 this ,表示在这个自定义组件下查找拥有 canvas-id 的 <canvas/>
	var ctx = uni.createCanvasContext('cpbg', this);
	ctx.setLineWidth(12); // 设置圆环的宽度
	ctx.setStrokeStyle('#F5F5F5'); // 设置圆环的颜色
	ctx.setLineCap('round'); // 设置圆环端点的形状
	ctx.beginPath(); //开始一个新的路径
	ctx.arc(56, 60, 50, Math.PI, 2 * Math.PI, false);
	//设置一个原点(110,110),半径为100的圆的路径到当前路径
	ctx.stroke(); //对当前路径进行描边
	ctx.draw();
	
	let that = this
	setTimeout(() => {
		uni.canvasToTempFilePath({
			canvasId: 'cpbg',
			success: function(res) {
				console.log('res1', res)
				that.cpbgCanvasImg = res.tempFilePath
			}
		},that)
	},500)
},

第二个圆

drawCircle: function(step, total) {
	var ctx = uni.createCanvasContext('cpbar', this);
	ctx.setStrokeStyle('#4C77F4');
	ctx.setLineWidth(12);
	ctx.setLineCap('round');
	ctx.beginPath();
	// 参数step 为绘制的百分比
	step = (step / total) + 1;
	if (step >= 2) {
		step = step % 2;
	}
	
	ctx.arc(56, 60, 50, Math.PI, step * Math.PI, false);
	ctx.stroke();
	ctx.draw();
	
	let that = this 
	setTimeout(() => {
		uni.canvasToTempFilePath({
			canvasId: 'cpbar',
			success: function(res) {
				console.log('res2', res)
				that.cpbarCanvasImg = res.tempFilePath
			}
		},that)
	},500)
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值