东风启辰小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.8KB

  1. // pages/poster/poster.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. imgUrl: app.globalData.urlStatic,//图片路径
  9. canvasContron:null,//canvas
  10. qrCodeUrl:"https://www.jiyou-tech.com/2020/496_qichen/static/images/testQrCode.png",
  11. windowScale:0,//屏幕缩放比
  12. windowW: 0,//屏幕宽度
  13. windowH: 0,//屏幕高度
  14. posterUrl:[],//海报图片
  15. canvasShow: true,//是否渲染canvas
  16. swiperCurrent: 0,//swiper选中的元素下标
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: function (options) {
  22. },
  23. /**
  24. * 生命周期函数--监听页面初次渲染完成
  25. */
  26. onReady: function () {
  27. this.data.canvasContron = wx.createCanvasContext('myCanvas');
  28. wx.getSystemInfo({
  29. success: res => {
  30. this.data.windowScale = res.windowWidth / 750;
  31. this.data.windowW = res.windowWidth;
  32. this.data.windowH = res.windowHeight;
  33. this.posterDrawing(1);
  34. }
  35. })
  36. },
  37. /**
  38. * 生命周期函数--监听页面显示
  39. */
  40. onShow: function () {
  41. },
  42. /**
  43. * 生命周期函数--监听页面隐藏
  44. */
  45. onHide: function () {
  46. },
  47. /**
  48. * 生命周期函数--监听页面卸载
  49. */
  50. onUnload: function () {
  51. },
  52. /**
  53. * 页面相关事件处理函数--监听用户下拉动作
  54. */
  55. onPullDownRefresh: function () {
  56. },
  57. /**
  58. * 页面上拉触底事件的处理函数
  59. */
  60. onReachBottom: function () {
  61. },
  62. /**
  63. * 用户点击右上角分享
  64. */
  65. onShareAppMessage: function () {
  66. return {
  67. title: '500元购车券',
  68. imageUrl: "",
  69. path: "/pages/coupon/coupon"
  70. }
  71. },
  72. saveImg:function(){//保存到手机
  73. wx.saveFile({
  74. tempFilePath: this.data.posterUrl[this.data.swiperCurrent],
  75. success(res) {
  76. if (res.errMsg== "saveFile:ok"){
  77. wx.showToast({
  78. title: '保存成功'
  79. })
  80. }
  81. }
  82. })
  83. },
  84. posterDrawing: function (e) {//海报绘制
  85. var ctx = this.data.canvasContron;//canvas对象
  86. var scale = this.data.windowScale;//屏幕缩放比
  87. //背景
  88. ctx.drawImage(this.data.imgUrl + '/images/tempImg'+e+'.jpg', 0, 0, scale * 444, scale * 817);//defaultHead
  89. ctx.setFillStyle("#FFFFFF");
  90. ctx.save();
  91. ctx.beginPath(); //开始绘制
  92. //先画个圆 前两个参数确定了圆心 (x,y) 坐标 第三个参数是圆的半径 四参数是绘图方向 默认是false,即顺时针
  93. ctx.arc(scale * 48 / 2 + scale * 49, scale * 48 / 2 + scale * 538, scale * 48 / 2, 0, Math.PI * 2, false);
  94. ctx.clip(); //剪切
  95. ctx.drawImage(this.data.imgUrl + '/images/coupon.png', scale * 49, scale * 538, scale * 48, scale * 48); //头像
  96. ctx.restore(); //恢复之前保存的绘图上下文
  97. //名称
  98. ctx.setFontSize(scale * 28);
  99. ctx.setFillStyle('#FFFFFF');
  100. ctx.setTextAlign('left');
  101. ctx.fillText("啦啦啦", scale * 102, scale *572);
  102. //我是第XXX星探
  103. ctx.drawImage(this.data.imgUrl + '/images/posterNum.png', scale * 50, scale * 598, scale * 333, scale * 74);
  104. //排名数字
  105. ctx.drawImage(this.data.imgUrl + '/images/nums/8.png', scale * 168, scale * 602, scale * 32, scale * 40);
  106. ctx.drawImage(this.data.imgUrl + '/images/nums/9.png', scale * 202, scale * 602, scale * 32, scale * 40);
  107. ctx.drawImage(this.data.imgUrl + '/images/nums/0.png', scale * 236, scale * 602, scale * 32, scale * 40);
  108. ctx.drawImage(this.data.imgUrl + '/images/nums/8.png', scale * 270, scale * 602, scale * 32, scale * 40);
  109. //二维码
  110. ctx.drawImage(this.data.qrCodeUrl, scale * 282, scale * 672, scale * 105, scale* 106);
  111. //绘制
  112. ctx.draw(false, setTimeout(() => {
  113. wx.canvasToTempFilePath({
  114. canvasId: 'myCanvas',
  115. success: res => {
  116. this.data.posterUrl.push(res.tempFilePath);
  117. this.setData({
  118. posterUrl: this.data.posterUrl
  119. })
  120. if (e < 3) {
  121. this.posterDrawing(e+1);
  122. }else{
  123. this.setData({
  124. canvasShow:false
  125. })
  126. }
  127. }
  128. })
  129. }, 300));
  130. },
  131. savePoster:function(){//预览海报
  132. wx.previewImage({
  133. current: this.data.posterUrl[this.data.swiperCurrent],
  134. urls: this.data.posterUrl // 需要预览的图片http链接列表
  135. })
  136. },
  137. prevImg() {//上一张图片
  138. if (this.data.swiperCurrent > 0) {
  139. this.setData({
  140. swiperCurrent: this.data.swiperCurrent -= 1,
  141. })
  142. }
  143. },
  144. nextImg() {//下一张图片
  145. if (this.data.swiperCurrent < this.data.posterUrl.length - 1) {
  146. this.setData({
  147. swiperCurrent: this.data.swiperCurrent += 1,
  148. })
  149. }
  150. },
  151. swiperChange(e) {//通过鼠标滑动改变swiper时
  152. if (e.detail.source == "touch") {
  153. this.setData({
  154. swiperCurrent: e.detail.current,
  155. })
  156. }
  157. }
  158. })