poster.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="content" v-if="isShow" >
  3. <view style="position: relative;">
  4. <image :src="img" style="height: 1000rpx" mode="heightFix" @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://music.nanyue6688.com/obsfile/b894874df7184a5d9caa961aab70cac8-poster.jpg'
  21. },
  22. avatar: {
  23. type: String,
  24. default: 'https://music.nanyue6688.com/obsfile/4620dd0fe1634138b73e0e12d2f02131-bc7e8021890d422d330851995fb8aae.jpg'
  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. let avatar = await this.getImageInfo(this.avatar)
  87. let avatarW = 290
  88. let aW = uni.upx2px(avatarW);
  89. this.drawRoundImg(this.ctx, avatar.path, (hW - aW) / 2, uni.upx2px(222), aW, aW, uni.upx2px(avatarW /
  90. 2))
  91. //绘制标题
  92. let titleSize = 19
  93. let titleParams = {
  94. color: '#fff',
  95. x: (hW / 2) - (this.title.length * titleSize / 2) + 10,
  96. y: (hH / 2) + uni.upx2px(110)
  97. }
  98. this.ctx.font = "19px 华文楷体"
  99. this.ctx.setFontSize(titleSize); //设置标题字体大小
  100. this.ctx.setFillStyle('#fff'); //设置标题文本颜色
  101. this.ctx.fillText(this.title, titleParams.x, titleParams.y)
  102. //绘制副标题
  103. let subTitleParams = {
  104. color: '#fff',
  105. y: (hH / 2) + uni.upx2px(170)
  106. }
  107. this.ctx.setFontSize(titleSize); //设置标题字体大小
  108. this.ctx.setFillStyle('#fff'); //设置标题文本颜色
  109. this.ctx.fillText(this.subTitle, titleParams.x, subTitleParams.y)
  110. //小程序码
  111. let qrSize = uni.upx2px(150)
  112. let qrcodeImg = await this.getImageInfo(this.qrcode)
  113. this.ctx.drawImage(qrcodeImg.path, hW / 2 + uni.upx2px(116), hH - uni.upx2px(344), qrSize, qrSize)
  114. //绘制描述
  115. let descSize = 14
  116. let descParams = {
  117. color: '#fff',
  118. x: hW / 2 + uni.upx2px(70),
  119. y: hH - uni.upx2px(308) + qrSize
  120. }
  121. this.ctx.setFontSize(descSize); //设置标题字体大小
  122. this.ctx.setFillStyle('#fff'); //设置标题文本颜色
  123. this.ctx.fillText(this.desc, descParams.x, descParams.y)
  124. //渲染
  125. this.ctx.draw(true, (res) => {
  126. uni.hideLoading()
  127. this.canvasToTempFilePath()
  128. })
  129. },
  130. //绘制实心圆
  131. drawEmptyRound(ctx, x, y, radius) {
  132. ctx.save()
  133. ctx.beginPath();
  134. ctx.arc(x, y, radius, 0, 2 * Math.PI, true);
  135. ctx.setFillStyle('rgba(0, 0, 0, .4)')
  136. ctx.fill();
  137. ctx.restore()
  138. ctx.closePath()
  139. },
  140. //绘制虚线
  141. drawDashLine(ctx, x1, y1, x2, y2, dashLength) {
  142. ctx.setStrokeStyle("#cccccc") //设置线条的颜色
  143. ctx.setLineWidth(1) //设置线条宽度
  144. var dashLen = dashLength,
  145. xpos = x2 - x1, //得到横向的宽度;
  146. ypos = y2 - y1, //得到纵向的高度;
  147. numDashes = Math.floor(Math.sqrt(xpos * xpos + ypos * ypos) / dashLen);
  148. //利用正切获取斜边的长度除以虚线长度,得到要分为多少段;
  149. for (var i = 0; i < numDashes; i++) {
  150. if (i % 2 === 0) {
  151. ctx.moveTo(x1 + (xpos / numDashes) * i, y1 + (ypos / numDashes) * i);
  152. //有了横向宽度和多少段,得出每一段是多长,起点 + 每段长度 * i = 要绘制的起点;
  153. } else {
  154. ctx.lineTo(x1 + (xpos / numDashes) * i, y1 + (ypos / numDashes) * i);
  155. }
  156. }
  157. ctx.stroke();
  158. },
  159. //带圆角图片
  160. drawRoundImg(ctx, img, x, y, width, height, radius) {
  161. ctx.beginPath()
  162. ctx.save()
  163. // 左上角
  164. ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5)
  165. // 右上角
  166. ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2)
  167. // 右下角
  168. ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5)
  169. // 左下角
  170. ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI)
  171. ctx.stroke()
  172. ctx.clip()
  173. ctx.drawImage(img, x, y, width, height);
  174. ctx.restore()
  175. ctx.closePath()
  176. },
  177. //圆角矩形
  178. drawRoundRect(ctx, x, y, width, height, radius, color) {
  179. ctx.save();
  180. ctx.beginPath();
  181. ctx.setFillStyle(color);
  182. ctx.setStrokeStyle(color)
  183. ctx.setLineJoin('round'); //交点设置成圆角
  184. ctx.setLineWidth(radius);
  185. ctx.strokeRect(x + radius / 2, y + radius / 2, width - radius, height - radius);
  186. ctx.fillRect(x + radius, y + radius, width - radius * 2, height - radius * 2);
  187. ctx.stroke();
  188. ctx.closePath();
  189. },
  190. //获取图片
  191. getImageInfo(imgSrc) {
  192. return new Promise((resolve, reject) => {
  193. uni.getImageInfo({
  194. src: imgSrc,
  195. success: (image) => {
  196. resolve(image);
  197. console.log('获取图片成功', image)
  198. },
  199. fail: (err) => {
  200. reject(err);
  201. console.log('获取图片失败', err)
  202. }
  203. });
  204. });
  205. },
  206. canvasToTempFilePath() {
  207. var that = this
  208. uni.canvasToTempFilePath({
  209. canvasId: 'my-canvas',
  210. quality: 1,
  211. complete: (res) => {
  212. this.img = res.tempFilePath
  213. }
  214. }, this);
  215. },
  216. //保存图片到相册
  217. saveImage() {
  218. //判断用户授权
  219. uni.getSetting({
  220. success(res) {
  221. console.log('获取用户权限', res.authSetting)
  222. if (Object.keys(res.authSetting).length > 0) {
  223. //判断是否有相册权限
  224. if (res.authSetting['scope.writePhotosAlbum'] == undefined) {
  225. //打开设置权限
  226. uni.openSetting({
  227. success(res) {
  228. console.log('设置权限', res.authSetting)
  229. }
  230. })
  231. } else {
  232. if (!res.authSetting['scope.writePhotosAlbum']) {
  233. //打开设置权限
  234. uni.openSetting({
  235. success(res) {
  236. console.log('设置权限', res.authSetting)
  237. }
  238. })
  239. }
  240. }
  241. } else {
  242. return
  243. }
  244. }
  245. })
  246. var that = this
  247. uni.saveImageToPhotosAlbum({
  248. filePath: that.img,
  249. success(res) {
  250. uni.showToast({
  251. title: '已保存到相册',
  252. icon: 'none',
  253. duration: 2000
  254. })
  255. setTimeout(() => {
  256. that.isShow = false
  257. }, 2000)
  258. }
  259. })
  260. }
  261. }
  262. }
  263. </script>
  264. <style scoped lang="scss">
  265. .content {
  266. position: fixed;
  267. top: 0;
  268. left: 0;
  269. right: 0;
  270. bottom: 0;
  271. background: rgba(0, 0, 0, .5);
  272. display: flex;
  273. flex-direction: column;
  274. justify-content: center;
  275. align-items: center;
  276. .save-btn {
  277. margin-top: 50rpx;
  278. color: #FFFFFF;
  279. background-color: #E72226;
  280. width: 200rpx;
  281. height: 60rpx;
  282. line-height: 60rpx;
  283. text-align: center;
  284. border-radius: 50rpx;
  285. margin: 30rpx;
  286. }
  287. .cancel-btn {
  288. background-color: #FFFFFF;
  289. color: #E72226;
  290. }
  291. }
  292. .btn {
  293. width: 100%;
  294. display: flex;
  295. justify-content: center;
  296. }
  297. </style>