uniapp vue3 使用canvas绘制图片

需要注意如果使用popup这种控制显示隐藏的话,canvas只有第一次才会显示,第二次popup显示后canvas不显示了解决方案使用v-if控制canvas显示

简单实例

 <canvas @longpress="onLongPress" v-if="model" canvas-id="posterCanvas" :style="{ width: config1.canvasWidth+'rpx', height: config1.canvasHeight+'rpx' }" class="canvas"></canvas>
			 <view class="colse">


	const sercanvas=()=>{
	
		// 绘制背景
		// 开始绘制
		uni.showLoading({
			title:"绘制中..."
		})
		ctx.value=uni.createCanvasContext('posterCanvas')
		const context=ctx.value
		// context.setStrokeStyle("#00ff00")  
		// 绘制背景色
		// 开始绘制
		context.beginPath()
		
		context.setFillStyle('#fff')
		context.fillRect(0, 0, 690, 1180)
		context.fill() 
		 context.beginPath()对当前路径中的内容进行填充
		context.drawImage(s2.value,15,10,300,320)
		context.beginPath()
		context.fill() 
		context.beginPath()
		context.drawImage(s1.value,110,340,100,100)
		context.fill() 
		// 设置文字
		context.beginPath()
		context.fillStyle = "#000";	
		context.font = "20px sans-serif";	
		context.fillText("长按图片保存相册", 80, 470);	
		// context.setFontSize("20px")
		context.fill() 
		context.draw()
		context.save()
		uni.hideLoading()
		// ctx.rect(0, 0, config1.value.canvasWidth/config1.value.ratio, config1.value.canvasHeight/config1.value.ratio)
		// ctx.setFillStyle('#fff')
		// ctx.fill()
		// ctx.draw()
	}
	// 保存
	const onLongPress=async ()=>{
	// baocu保存图片先检测权限是否打开
		 let result = await permision.showAuthTipModal("android.permission.READ_EXTERNAL_STORAGE")  
		if(result){
			uni.canvasToTempFilePath({
				 canvasId: 'posterCanvas',
				  success: function(res) {
				     // 在H5平台下,tempFilePath 为 base64
				     console.log(res.tempFilePath)
						uni.saveImageToPhotosAlbum({
							filePath:res.tempFilePath,
							success: function() {
													uni.showToast({
														icon: 'none',
														position: 'bottom',
														title: "已保存到系统相册",
													})
												}
						})
				   }
			})
		}
		
	}
相关推荐
yinxiangzhongqing4 分钟前
loadash知识整理
前端·javascript·chrome
德莱厄斯23 分钟前
三行代码完成国际化适配,妙~啊~
前端·javascript·babel
2301_7891695426 分钟前
JSON.parse(JSON.stringify())深拷贝不会复制函数
开发语言·前端·javascript
程序员XC29 分钟前
前端性能优化的思考过程
前端·javascript·面试
皮皮虾我们跑1 小时前
web—HTML
前端·html
JosieBook1 小时前
【前端】如何在HTML中调用CSS和JavaScript(完整指南)
前端·css·html
IT、木易1 小时前
React 学习全阶段总结
javascript·学习·react.js
唐诗1 小时前
这位同学来说一说 vue3 的组件通信
前端