东风启辰小程序端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

142 行
3.7KB

  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:null,//海报图片
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. },
  21. /**
  22. * 生命周期函数--监听页面初次渲染完成
  23. */
  24. onReady: function () {
  25. this.data.canvasContron = wx.createCanvasContext('myCanvas');
  26. wx.getSystemInfo({
  27. success: res => {
  28. this.data.windowScale = res.windowWidth / 750;
  29. this.data.windowW = res.windowWidth;
  30. this.data.windowH = res.windowHeight;
  31. this.posterDrawing();
  32. }
  33. })
  34. },
  35. /**
  36. * 生命周期函数--监听页面显示
  37. */
  38. onShow: function () {
  39. },
  40. /**
  41. * 生命周期函数--监听页面隐藏
  42. */
  43. onHide: function () {
  44. },
  45. /**
  46. * 生命周期函数--监听页面卸载
  47. */
  48. onUnload: function () {
  49. },
  50. /**
  51. * 页面相关事件处理函数--监听用户下拉动作
  52. */
  53. onPullDownRefresh: function () {
  54. },
  55. /**
  56. * 页面上拉触底事件的处理函数
  57. */
  58. onReachBottom: function () {
  59. },
  60. /**
  61. * 用户点击右上角分享
  62. */
  63. onShareAppMessage: function () {
  64. return {
  65. title: '500元购车券',
  66. imageUrl: "",
  67. path: "/pages/coupon/coupon"
  68. }
  69. },
  70. saveImg:function(){//保存到手机
  71. wx.chooseImage({
  72. success: function (res) {
  73. console.log(res);
  74. const tempFilePaths = res.tempFilePaths
  75. wx.saveFile({
  76. tempFilePath: tempFilePaths[0],
  77. success(res) {
  78. console.log(res);
  79. const savedFilePath = res.savedFilePath
  80. }
  81. })
  82. }
  83. })
  84. },
  85. posterDrawing: function () {//海报绘制
  86. var ctx = this.data.canvasContron;
  87. var scale = this.data.windowScale;
  88. console.log(scale);
  89. //背景
  90. ctx.drawImage(this.data.imgUrl + '/images/tempImg1.jpg', 0, 0, scale * 444, scale * 817);//defaultHead
  91. ctx.setFillStyle("#FFFFFF");
  92. ctx.save();
  93. ctx.beginPath(); //开始绘制
  94. //先画个圆 前两个参数确定了圆心 (x,y) 坐标 第三个参数是圆的半径 四参数是绘图方向 默认是false,即顺时针
  95. ctx.arc(scale * 48 / 2 + scale * 49, scale * 48 / 2 + scale * 538, scale * 48 / 2, 0, Math.PI * 2, false);
  96. ctx.clip(); //剪切
  97. ctx.drawImage(this.data.imgUrl + '/images/coupon.png', scale * 49, scale * 538, scale * 48, scale * 48); //头像
  98. ctx.restore(); //恢复之前保存的绘图上下文
  99. //名称
  100. ctx.setFontSize(scale * 28);
  101. ctx.setFillStyle('#FFFFFF');
  102. ctx.setTextAlign('left');
  103. ctx.fillText("啦啦啦", scale * 102, scale *572);
  104. //我是第XXX星探
  105. ctx.drawImage(this.data.imgUrl + '/images/posterNum.png', scale * 50, scale * 598, scale * 333, scale * 74);
  106. //二维码
  107. ctx.drawImage(this.data.qrCodeUrl, scale * 282, scale * 672, scale * 105, scale* 106);
  108. //绘制
  109. ctx.draw();
  110. },
  111. savePoster:function(){//生成海报
  112. wx.canvasToTempFilePath({
  113. width: 443 * this.windowScale,
  114. height: 817 * this.windowScale,
  115. destWidth: 443 * this.windowScale *this.data.windowW*3,
  116. destHeight: 817 * this.windowScale * this.data.windowH*3,
  117. canvasId: 'myCanvas',
  118. success: res => {
  119. wx.previewImage({
  120. urls: [res.tempFilePath] // 需要预览的图片http链接列表
  121. })
  122. // this.data.posterUrl = res.tempFilePath;
  123. // console.log(this.data.posterUrl);
  124. }
  125. })
  126. }
  127. })