poster.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="content" v-if="isShow" >
  3. <view style="position: relative;">
  4. <image :src="img" style="height: 950rpx;width: 550rpx;" @load="load"></image>
  5. <image @click.stop="isShow=false" style="position: absolute;right: -50rpx;top: -60rpx;width: 50rpx;height: 50rpx;" src="@/static/icon/close.png" mode=""></image>
  6. </view>
  7. <view class="btn" v-if="imgLoad">
  8. <view class="save-btn" @click.stop="saveImage">保存图片</view>
  9. <view class="save-btn" @click.stop="goLottery"> 去抽奖 </view>
  10. </view>
  11. <canvas style="position: fixed;top: 999999999rpx;" @click.stop=""
  12. :style="{ width: canvasW + 'px', height: canvasH + 'px' }" canvas-id="my-canvas"></canvas>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. props: {
  18. headerImg: {
  19. type: String,
  20. default: 'https://gx.vote.activity.obs.cn-south-1.myhuaweicloud.com:443/a9d4b65ba1cd4bc88ef9da08cb8fcd26-poster.jpg'
  21. },
  22. avatar: {
  23. type: String,
  24. default: ''
  25. },
  26. title: {
  27. type: String,
  28. default: '我是第888位大赛代言人!'
  29. },
  30. subTitle: {
  31. type: String,
  32. default: '邀请您为888号作品投票!'
  33. },
  34. desc: {
  35. type: String,
  36. default: '关注公众号参与投票!'
  37. },
  38. },
  39. data() {
  40. return {
  41. imgLoad: false,
  42. img: '',
  43. canvasW: 0,
  44. canvasH: 0,
  45. ctx: null,
  46. isShow: false,
  47. qrcode: ''
  48. }
  49. },
  50. methods: {
  51. saveImage(){
  52. },
  53. goLottery(){
  54. },
  55. load() {
  56. this.imgLoad = true
  57. },
  58. //显示
  59. showCanvas(qrcode) {
  60. this.isShow = true
  61. this.qrcode = qrcode
  62. this.__init()
  63. },
  64. //初始化画布
  65. async __init() {
  66. uni.showLoading({
  67. title: '加载中...',
  68. mask: true
  69. })
  70. this.ctx = uni.createCanvasContext('my-canvas', this)
  71. this.canvasW = uni.upx2px(750);
  72. this.canvasH = uni.upx2px(1333);
  73. //设置画布背景透明
  74. this.ctx.setFillStyle('rgba(255, 255, 255, 0)')
  75. //设置画布大小
  76. this.ctx.fillRect(0, 0, this.canvasW, this.canvasH)
  77. //绘制圆角背景
  78. this.drawRoundRect(this.ctx, 0, 0, this.canvasW, this.canvasH, 0, '#f82001')
  79. //绘制标题图
  80. let headerImg = await this.getImageInfo(this.headerImg)
  81. let hW = uni.upx2px(750);
  82. let hH = uni.upx2px(1333);
  83. this.drawRoundImg(this.ctx, headerImg.path, ((this.canvasW - hW) / 2), ((this.canvasW - hW) / 2), hW,
  84. hH, 0)
  85. //绘制头像
  86. const userInfo = uni.getStorageSync("userInfo");
  87. let avatar = await this.getImageInfo( userInfo.avatar ||this.avatar)
  88. let avatarW = 290
  89. let aW = uni.upx2px(avatarW);
  90. this.drawRoundImg(this.ctx, avatar.path, (hW - aW) / 2, uni.upx2px(222), aW, aW, uni.upx2px(avatarW /
  91. 2))
  92. //绘制标题
  93. let titleSize = 19
  94. let titleParams = {
  95. color: '#fff',
  96. x: (hW / 2) - (this.title.length * titleSize / 2) - 10,
  97. y: (hH / 2) + uni.upx2px(110)
  98. }
  99. this.ctx.font = "19px 华文楷体"
  100. this.ctx.setFontSize(titleSize); //设置标题字体大小
  101. this.ctx.setFillStyle('#fff'); //设置标题文本颜色
  102. this.ctx.fillText(this.title, titleParams.x, titleParams.y)
  103. //绘制副标题
  104. let subTitleParams = {
  105. color: '#fff',
  106. y: (hH / 2) + uni.upx2px(170)
  107. }
  108. this.ctx.setFontSize(titleSize); //设置标题字体大小
  109. this.ctx.setFillStyle('#fff'); //设置标题文本颜色
  110. this.ctx.fillText(this.subTitle, titleParams.x, subTitleParams.y)
  111. //小程序码
  112. let qrSize = uni.upx2px(150)
  113. let qrcodeImg = await this.getImageInfo(this.qrcode)
  114. this.ctx.drawImage(qrcodeImg.path, hW / 2 + uni.upx2px(116), hH - uni.upx2px(344), qrSize, qrSize)
  115. //绘制描述
  116. let descSize = 14
  117. let descParams = {
  118. color: '#fff',
  119. x: hW / 2 + uni.upx2px(70),
  120. y: hH - uni.upx2px(308) + qrSize
  121. }
  122. this.ctx.setFontSize(descSize); //设置标题字体大小
  123. this.ctx.setFillStyle('#fff'); //设置标题文本颜色
  124. this.ctx.fillText(this.desc, descParams.x, descParams.y)
  125. //渲染
  126. this.ctx.draw(true, (res) => {
  127. uni.hideLoading()
  128. this.canvasToTempFilePath()
  129. })
  130. },
  131. //绘制实心圆
  132. drawEmptyRound(ctx, x, y, radius) {
  133. ctx.save()
  134. ctx.beginPath();
  135. ctx.arc(x, y, radius, 0, 2 * Math.PI, true);
  136. ctx.setFillStyle('rgba(0, 0, 0, .4)')
  137. ctx.fill();
  138. ctx.restore()
  139. ctx.closePath()
  140. },
  141. //绘制虚线
  142. drawDashLine(ctx, x1, y1, x2, y2, dashLength) {
  143. ctx.setStrokeStyle("#cccccc") //设置线条的颜色
  144. ctx.setLineWidth(1) //设置线条宽度
  145. var dashLen = dashLength,
  146. xpos = x2 - x1, //得到横向的宽度;
  147. ypos = y2 - y1, //得到纵向的高度;
  148. numDashes = Math.floor(Math.sqrt(xpos * xpos + ypos * ypos) / dashLen);
  149. //利用正切获取斜边的长度除以虚线长度,得到要分为多少段;
  150. for (var i = 0; i < numDashes; i++) {
  151. if (i % 2 === 0) {
  152. ctx.moveTo(x1 + (xpos / numDashes) * i, y1 + (ypos / numDashes) * i);
  153. //有了横向宽度和多少段,得出每一段是多长,起点 + 每段长度 * i = 要绘制的起点;
  154. } else {
  155. ctx.lineTo(x1 + (xpos / numDashes) * i, y1 + (ypos / numDashes) * i);
  156. }
  157. }
  158. ctx.stroke();
  159. },
  160. //带圆角图片
  161. drawRoundImg(ctx, img, x, y, width, height, radius) {
  162. ctx.beginPath()
  163. ctx.save()
  164. // 左上角
  165. ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5)
  166. // 右上角
  167. ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2)
  168. // 右下角
  169. ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5)
  170. // 左下角
  171. ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI)
  172. ctx.stroke()
  173. ctx.clip()
  174. ctx.drawImage(img, x, y, width, height);
  175. ctx.restore()
  176. ctx.closePath()
  177. },
  178. //圆角矩形
  179. drawRoundRect(ctx, x, y, width, height, radius, color) {
  180. ctx.save();
  181. ctx.beginPath();
  182. ctx.setFillStyle(color);
  183. ctx.setStrokeStyle(color)
  184. ctx.setLineJoin('round'); //交点设置成圆角
  185. ctx.setLineWidth(radius);
  186. ctx.strokeRect(x + radius / 2, y + radius / 2, width - radius, height - radius);
  187. ctx.fillRect(x + radius, y + radius, width - radius * 2, height - radius * 2);
  188. ctx.stroke();
  189. ctx.closePath();
  190. },
  191. //获取图片
  192. getImageInfo(imgSrc) {
  193. return new Promise((resolve, reject) => {
  194. uni.getImageInfo({
  195. src: imgSrc,
  196. success: (image) => {
  197. resolve(image);
  198. console.log('获取图片成功', image)
  199. },
  200. fail: (err) => {
  201. reject(err);
  202. console.log('获取图片失败', err)
  203. }
  204. });
  205. });
  206. },
  207. canvasToTempFilePath() {
  208. var that = this
  209. uni.canvasToTempFilePath({
  210. canvasId: 'my-canvas',
  211. quality: 1,
  212. complete: (res) => {
  213. this.img = res.tempFilePath
  214. }
  215. }, this);
  216. },
  217. //保存图片到相册
  218. saveImage() {
  219. //判断用户授权
  220. var that = this
  221. uni.saveImageToPhotosAlbum({
  222. filePath: that.img,
  223. success(res) {
  224. uni.showToast({
  225. title: '已保存到相册',
  226. icon: 'none',
  227. duration: 2000
  228. })
  229. setTimeout(() => {
  230. that.isShow = false
  231. }, 2000)
  232. }
  233. })
  234. }
  235. }
  236. }
  237. </script>
  238. <style scoped lang="scss">
  239. .content {
  240. position: fixed;
  241. top: 0;
  242. left: 0;
  243. right: 0;
  244. bottom: 0;
  245. background: rgba(0, 0, 0, .5);
  246. display: flex;
  247. flex-direction: column;
  248. justify-content: center;
  249. align-items: center;
  250. z-index: 999;
  251. padding-top: 60rpx;
  252. .save-btn {
  253. margin-top: 50rpx;
  254. color: #FFFFFF;
  255. background-color: #E72226;
  256. width: 200rpx;
  257. height: 60rpx;
  258. line-height: 60rpx;
  259. text-align: center;
  260. border-radius: 50rpx;
  261. margin: 30rpx;
  262. }
  263. .cancel-btn {
  264. background-color: #FFFFFF;
  265. color: #E72226;
  266. }
  267. }
  268. .btn {
  269. width: 100%;
  270. display: flex;
  271. justify-content: center;
  272. }
  273. </style>