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: "已保存到系统相册",
													})
												}
						})
				   }
			})
		}
		
	}
相关推荐
学以智用9 分钟前
wangEditor V5 精简完整参考手册
javascript
AiMan37 分钟前
Poe还是不可以导出左侧所有对话,只能单条内容导出,方法不对吗
前端
用户298698530141 小时前
React 前端实现 Excel 与 CSV 互转:基于 WebAssembly 的浏览器端方案
javascript·react.js·excel
AiMan1 小时前
文心生成的内容能否下载吗?
前端
前端双越老师2 小时前
程序员 1v1 访谈系列第十四期:接受焦虑 拥抱不确定
前端·ai编程·全栈
deli0072 小时前
MCP 协议 6 条消息拆开看:AI 为什么能自己读文件调接口
前端
风骏时光牛马3 小时前
请告诉我标题对应的内容、想要的风格,我马上给你生成。
前端
deli0073 小时前
OAuth 2.0 授权码流程实测:4 次跳转,少个 state 就被劫持
前端
deli0075 小时前
手机20%突然关机?电量百分比是估的,SOC曲线4组实测
前端