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: "已保存到系统相册",
													})
												}
						})
				   }
			})
		}
		
	}
相关推荐
YHHLAI13 小时前
[特殊字符] Agent 智能体开发实战 · 第一课:Tool Use —— 让大模型自动干活
前端·人工智能
nuIl13 小时前
我把 5 个编码 Agent 塞进了一个 npm 包
前端·agent·claude
L-影13 小时前
FastAPI 静态文件:Web 页面的“固定展柜”与“加速引擎”
前端·fastapi
陪我去看海13 小时前
受够了 AI 写完代码留下一堆端口?我做了一个端口管理App!
前端·macos·vibecoding
Xuepoo13 小时前
我抛弃了 DOM,但保留了无障碍访问
前端
李明卫杭州13 小时前
Vue2 vs Vue3 的 h 函数:渲染函数完整迁移指南
前端
月光刺眼13 小时前
用LangChain 打造第一个 Agent:LLM 大脑与 Tool 手脚的完整闭环
javascript·人工智能·全栈
星栈13 小时前
LiveView 的认证系统:从登录到权限,我一开始以为有 `current_user` 就算完事了
前端·前端框架·elixir
在因斯坦13 小时前
摸摸鱼之前端自己研究 Jenkins 自动化部署
前端
西西学代码13 小时前
Flutter---底部导航栏(2)
开发语言·javascript·flutter