Uniapp 制作海报功能

<template>
	<view>
		<canvas class="can" canvas-id="mycan"></canvas>
		<button @click="but">保存海报</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				backurl: '../../static/2bcd944a40e85a34f2e904f4461e163f.jpg' //本地背景图
			}
		},
		onShow: function(option) {
			var that = this
			// 邀请码接口请求
			uni.request({
				url: '******',
				data: {
					invite_code: *
				},
				header: {
					'content-type': '******'
				},
				method: 'POST',
				success: (e) => {
					// 获取当前系统的信息
					uni.getSystemInfo({
						success(res) {
							// 在画布绘制图像
							const ctx = uni.createCanvasContext('mycan')
							ctx.drawImage(that.backurl, 0, 0, res.windowWidth, res.windowHeight)
							ctx.drawImage(e.data, +res.windowWidth / 2 - 100, +res.windowHeight / 2 - 100, 200, 200)
							ctx.draw() //重绘页面
						}
					})
				}
			})
		},
		methods: {
			but() {
				var that = this
				// 获取用户的当前设置
				wx.getSetting({
					success(res) {
						if (!res.authSetting['scope.writePhotosAlbum']) {
							uni.showModal({
								title: '您还未授权相册权限',
								content: '点击确认授权',
								success(res) {
									if (res.confirm == true) {
										wx.authorize({
											scope: 'scope.writePhotosAlbum',
										})
									}
								}
							})
						} else {
							// 把当前画布指定区域的内容导出生成指定大小的图片,并返回文件路径
							uni.canvasToTempFilePath({
								x: 0,
								y: 0,
								width: that.windowWidth * 1.22, // 截取的画布的宽
								height: that.windowHeight * 1.22, // 截取的画布的高
								destWidth: that.windowWidth * 1.22 * 3, // 保存成的画布宽度
								destHeight: that.windowHeight * 1.22 * 3, // 保存成的画布高度
								fileType: 'png', // 保存成的文件类型
								quality: 1, // 图片质量
								canvasId: 'mycan', // 画布ID
								success(res) {
									// 保存图片至相册
									uni.saveImageToPhotosAlbum({
										filePath: res.tempFilePath,
										success() {
											uni.showToast({
												title: '图片保存成功',
												duration: 2000
											})
										}
									})
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.can {
		width: 100vw;
		height: 100vh;
	}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值