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: "已保存到系统相册",
													})
												}
						})
				   }
			})
		}
		
	}
相关推荐
郑州光合科技余经理1 小时前
代驾系统架构拆解:订单链路、权限组织与私有化源码交付
开发语言·后端·算法·架构·系统架构·uni-app·php
程序员黑豆2 小时前
鸿蒙应用开发:一次开发多端部署之响应式布局完全指南
前端·harmonyos
小月土星3 小时前
2.0 React Router 前端路由深度解析:从传统后端路由到现代 SPA 的完整演进
前端
To_OC4 小时前
LC 35 搜索插入位置:二分查找谁都会,边界条件谁写谁懵
javascript·算法·leetcode
码哥DFS4 小时前
二叉树的直径
开发语言·javascript·算法
badhope3386 小时前
当所有AI都在抄同一份作业:前端模板的趋同、困局与破局
前端·ai·ai编程·前端开发·tailwind·ui设计
光影少年6 小时前
RN的Fabric 渲染流程
运维·前端·javascript·react native·react.js·fabric
__zRainy__7 小时前
React开始:直接在网站中使用React.js
前端·react.js·前端框架
用户059540174468 小时前
大模型长记忆评测踩坑实录:2000条错位记忆,让我排查了整整3小时
前端·css
码林鼠8 小时前
webpack的基本配置
前端·webpack·node.js