分享海报的操作

<template>
	<Layout :activeIndex="3">

		<!-- 准备一个画布 -->
		<canvas canvas-id="poster" id="poster"></canvas>
		<button @click="draw">绘制</button>

		<!-- <uqrcode v-show="false" ref="uqrcode" canvas-id="qrcode" value="https://uqrcode.cn/doc" :options="{ margin: 10 }"></uqrcode>
  <image style="width: 360px; height: 438px;" :src="cimgurl" show-menu-by-longpress></image> -->
	</Layout>
</template>

<script>
	export default {
		data() {
			return {
				bg_url: 'https://cdn.img.up678.com/u/11225972/2020/07/08/1594200688478467431.png',
				cimgurl: ''

			}
		},
		methods: {
			async draw() {
				// 得到画笔
				const ctx = uni.createCanvasContext('poster', this)

				// 绘制背景图片(把网络图片下载下来)
				const bgImg = await this.downloadImg(this.bg_url)
				ctx.drawImage(bgImg, 0, 0, 360, 438)

				ctx.save()

				// 画头像
				// 1. 获取头像
				const {
					headImg,
					nickName
				} = await this.getUserInfo()
				console.log(headImg, nickName)
				ctx.beginPath();
				ctx.arc(50, 50, 20, 0, 2 * Math.PI);
				ctx.clip();
				ctx.drawImage(headImg, 30, 30, 40, 40);


				// 绘制昵称
				ctx.restore()

				ctx.setFillStyle('red')
				ctx.setFontSize(18)
				ctx.setShadow(0, 5, 2, '#ff6900')

				ctx.fillText(nickName, 100, 100)

				ctx.save()
				ctx.draw()

				// 绘制二维码
				// 1. 生成二维码
				// 2. 将二维码保存到本地(本地存储的地址)
				// 3. 将二维码绘制到画布上
				//   const { tempFilePath } = await this.getQrcodeTemFilePath()

				//   ctx.drawImage(tempFilePath, 150, 150, 100, 100)


				//   ctx.draw()

				//   setTimeout(() => {
				//    uni.canvasToTempFilePath({
				//     canvasId: 'poster',
				//     success: res => {
				//      console.log(res)
				//      this.cimgurl = res.tempFilePath
				//     }
				//    })
				//   }, 50)

			},

			// getQrcodeTemFilePath() {
			//  return new Promise((resolve, reject) => {
			//   this.$refs.uqrcode.toTempFilePath({
			//     success: res => {
			//       resolve(res)
			//     }
			//   });
			//  })
			// },

			async getUserInfo() {
				const info = await uni.getUserProfile({
					desc: 'xxx'
				})
				// 下载头像得到本地地址
				const headImg = await this.downloadImg(info.userInfo.avatarUrl)
				return {
					headImg,
					nickName: info.userInfo.nickName
				}
			},

			async downloadImg(url) {
				const {
					tempFilePath
				} = await uni.downloadFile({
					url
				})
				// console.log(res)
				return tempFilePath
			}
		}
	}
</script>

<style>
	#poster {
		box-sizing: border-box;
		width: 360px;
		height: 438px;
		border: 1px solid red;
	}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值