微信小程序/uni-app生成海报功能@令狐张豪

10 篇文章 1 订阅
8 篇文章 0 订阅
本文详细介绍了如何在uni-app中利用createCanvasContext API生成海报,包括下载网络图片、绘制到canvas、保存图片到本地以及分享到相册的过程。涉及到的技术点包括小程序的下载方法、canvas绘图、uni.canvasToTempFilePath及保存图片到相册的实现。
摘要由CSDN通过智能技术生成

使用技术栈:uni-app
使用官方文档提供的API:createCanvasContext(uni-app) /CanvasContext(原生)

*注意:海报里用到的图片都得先使用小程序提供的下载方法把网络图片下载成本地文件才能使用,否则会出现跨域问题!

html代码:

<canvas id="imgCanvas" canvas-id="imgCanvas" style="width: 260px;height:414px;background: #FFFFFF;"></canvas>

js代码:

data(){
	return {
	    imgUrl: '',//生成的海报路径
		ewm: '', //小程序码
		bg_img: 'https://s3.cn-north-1.amazonaws.com.cn/huivo-miyamibao/png/c3962d3ae80e9847316e0c8d00ebb00f.png', //背景图片
		system_info: [], //系统信息(用到头像)
	}
}

methods:{
	share(){
	   //分享(生成海报)
	   //绘图
	   var that = this;

	   //这里等promise执行,避免拿不到图片临时路径
	   //先把我们用到的素材图片下载成本地图片
	   const promise1 = new Promise((resolve, reject) => {
		   uni.downloadFile({
			   url: that.bg_img, //背景图
			   success: (res) => {
				   resolve(res)
			   }
		   })
	   });
	   const promise2 = new Promise((resolve, reject) => {
		   uni.downloadFile({
			   url: that.ewm,  //小程序码
			   success: (res) => {
				   resolve(res)
			   }
		   })
	   });
	   const promise3 = new Promise((resolve, reject) => {
		   uni.downloadFile({
			   url: that.userInfo.avatarUrl, //微信用户头像
			   success: (res) => {
				   resolve(res)
			   }
		   })
	   });
	   Promise.all([promise1, promise2, promise3]).then((values) => {
		this.make_canvas(values[0], values[1], values[2]);
	   });
	},
	make_canvas(img01, img02, header) {
		var ctx = uni.createCanvasContext('imgCanvas') //绑定画布 
		//drawImage 参数说明:drawImage(图片地址,图片w,图片h,x轴,y轴)
		ctx.setFillStyle("#ffffff");
		ctx.drawImage(img01.tempFilePath, 0, 0, 260, 414); //填充进图片
		ctx.drawImage(img02.tempFilePath, 17, 337, 61, 61); //填充进图片
		ctx.drawImage(header.tempFilePath, 92, 280, 23, 23, );
		
		//有几处内容就写几个
		ctx.setFillStyle('#FFFFFF') //设置内容1的文字样式
		ctx.setFontSize(8);
		ctx.fillText(this.userInfo.nickName, 123, 295) //用户微信名称

		ctx.setFillStyle('#FFFFFF') //设置内容2的文字样式
		ctx.setFontSize(8);
		ctx.fillText('为您推荐', 190, 295)

		ctx.draw(); //输出到画布中
		uni.showLoading({ //增加loading等待效果
			mask: true
		})
		setTimeout(() => { //不加延迟的话,base64有时候会赋予undefined
			uni.canvasToTempFilePath({
				canvasId: 'imgCanvas',
				success: (res) => {
					//生成的海报地址
					this.imgUrl = res.tempFilePath
				}
			})
			uni.hideLoading();
		}, 1200)
	},
	//保存到相册方法:
   downloadFile() {
	uni.saveImageToPhotosAlbum({
		filePath: this.imgUrl,
		success: function() {
			console.log('save success');
		}
	});
  },
}

end~~~

如有错误或观点不一致的请评论留言共同讨论,本人前端小白一枚,根据自己实际项目遇到的问题进行总结分享,谢谢大家的阅读!
文章对您有所帮助请给作者点个赞支持下,谢谢~

PS D:\pythonProject> pip install matplotlib Collecting matplotlib Obtaining dependency information for matplotlib from https://files.pythonhosted.org/packages/d0/39/4c4fb38ec2356bcbc9017a5421623aec69aacde110e4e76d34d0a43702f0/matplotlib-3.7.2-cp310-cp310-win32.whl.metadata Using cached matplotlib-3.7.2-cp310-cp310-win32.whl.metadata (5.8 kB) Collecting contourpy>=1.0.1 (from matplotlib) Using cached contourpy-1.1.0.tar.gz (13.4 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [25 lines of output] + meson setup C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322 C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1 b9965df3941b882963f3350114322\.mesonpy-qrhmsqrm\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322\.mesonpy-qrhmsqrm\build\meson-python-native-file.ini The Meson build system Version: 1.2.0 Source dir: C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322 Build dir: C:\Users\张豪\AppData\Local\Temp\pip-install-fuirjkwl\contourpy_ab1b9965df3941b882963f3350114322\.mesonpy-qrhmsqrm\build Build type: native build Project name: contourpy Project version: 1.1.0 Activating VS 17.6.5 note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
07-23
C:\Users\张豪>pip install matplotlib-3.7.2-cp310-cp310-win32.whl Processing c:\users\张豪\matplotlib-3.7.2-cp310-cp310-win32.whl Collecting contourpy>=1.0.1 (from matplotlib==3.7.2) Using cached contourpy-1.1.0.tar.gz (13.4 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... error error: subprocess-exited-with-error × Preparing metadata (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [12 lines of output] + meson setup C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957 C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build -Dbuildtype=release -Db_ndebug=if-release -Db_vscrt=md --vsenv --native-file=C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build\meson-python-native-file.ini The Meson build system Version: 1.2.0 Source dir: C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957 Build dir: C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build Build type: native build Project name: contourpy Project version: 1.1.0 ..\..\meson.build:1:0: ERROR: Could not find C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe A full log can be found at C:\Users\张豪\AppData\Local\Temp\pip-install-9aonnup5\contourpy_cf94fc4320674b1ab97a527e0a04f957\.mesonpy-k9vixjo_\build\meson-logs\meson-log.txt [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
07-23
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值