poster.vue 7.8 KB

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