东风启辰小程序端
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.

poster.js 6.4KB

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