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: "已保存到系统相册",
													})
												}
						})
				   }
			})
		}
		
	}
相关推荐
昔人'5 分钟前
css 高度从 0 到 auto 的动画效果 `interpolate-size: allow-keywords`
前端·css
百思可瑞教育18 分钟前
Vue 生命周期详解:从初始化到销毁的全过程剖析
前端·javascript·vue.js·前端框架·uni-app·北京百思可瑞教育·百思可瑞教育
IT_陈寒39 分钟前
Python 3.12 新特性实战:10个性能优化技巧让你的代码快如闪电⚡
前端·人工智能·后端
Wiktok1 小时前
前后端开发Mock作用说明,mock.ts
前端·mock·vue3
冲!!1 小时前
SCSS 中的Mixins 和 Includes,%是什么意思
前端·css·scss
星语卿2 小时前
Vuetify:构建优雅Vue应用的Material Design组件库
前端·javascript·vue.js
wangbing11252 小时前
界面规范11-对话框
javascript·vue.js·elementui
roman_日积跬步-终至千里2 小时前
【系统架构设计(25)】Web应用服务器与现代架构
前端·架构·系统架构