恶补canvas。。。

    1. const ctx = wx.createCanvasContext('myCanvas');
    2. /**/获取设备信息
    3. wx.getSystemInfo({
    4. success: function (res) {
    5. let clientHeight = res.windowHeight;
    6. let clientWidth = res.windowWidth;
    7. //获取屏幕宽度,获取自适应单位
    8. let rpx = res.windowWidth / 375;
    9. that.setData({
    10. clientHeight: clientHeight,
    11. clientWidth: clientWidth,
    12. rpx:rpx
    13. });
    14. }
    15. });
    16. // 生成带参数二维码
    17. this.drawBgPic(ctx,that,function () {
    18. wx.request({
    19. url: 'https://www.xxxxxx.com/xxx/wx_qrcode',
    20. data: {
    21. page: 'pages/xxx/xxx'
    22. },
    23. responseType: 'arraybuffer',
    24. success: function (res) {
    25. //网络图片要下载到本地以后才能绘制
    26. var qrImg = wx.arrayBufferToBase64(res.data);
    27. that.setData({
    28. code: qrImg
    29. })
    30. const fsm = wx.getFileSystemManager(), //文件管理器
    31. FILE_BASE_NAME = 'tmp_base64src', //文件名
    32. format = 'png', //文件后缀
    33. base64Str = that.data.code
    34. var buffer = wx.base64ToArrayBuffer(base64Str), //base 转二进制
    35. filePath = `${wx.env.USER_DATA_PATH}/www.${format}`; //文件名
    36. fsm.writeFile({ //写文件
    37. filePath,
    38. data: buffer,
    39. encoding: 'binary',
    40. success(res) {
    41. wx.getImageInfo({ //读取图片
    42. src: filePath,
    43. success: function (res) {
    44. that.circleImg(ctx, res.path, 200 * that.data.rpx, 250 * that.data.rpx, 55 * that.data.rpx)
    45. that.drawName(ctx,that)
    46. },
    47. })
    48. }
    49. })
    50. },
    51. complete:function(){
    52. wx.hideLoading()
    53. }
    54. })
    55. });
    56. //圆形二维码
    57. circleImg: function (ctx, img, x, y, r) {
    58. ctx.save();
    59. var d = 2 * r;
    60. var cx = x + r;
    61. var cy = y + r;
    62. ctx.beginPath();
    63. ctx.arc(cx, cy, r, 0, 2 * Math.PI);
    64. ctx.clip();
    65. ctx.drawImage(img, x, y, d, d);
    66. },
    67. /**
    68. * 生成分享图
    69. */
    70. /**
    71. * 保存到相册
    72. */
    73. save: function () {
    74. var that = this;
    75. //获取相册授权
    76. wx.getSetting({
    77. success(res) {
    78. if (!res.authSetting['scope.writePhotosAlbum']) {
    79. wx.authorize({
    80. scope: 'scope.writePhotosAlbum',
    81. success() {
    82. that.savaImageToPhoto();
    83. }
    84. })
    85. } else {
    86. that.savaImageToPhoto();
    87. }
    88. }
    89. })
    90. },
    91. drawName: function(ctx,that,callback){
    92. ctx.save();
    93. ctx.setFontSize(20)
    94. ctx.fillText(that.data.nickname, 10, 10)
    95. ctx.draw();
    96. ctx.save();
    97. if(callback){ callback() }
    98. },
    99. drawBgPic: function (ctx,that,callback) {
    100. wx.showLoading({
    101. title: '努力生成中',
    102. mask:true
    103. })
    104. var path = 'https://www.xxxxx.com/img/xxx.png';
    105. wx.getImageInfo({
    106. src: path, //服务器返回的图片地址
    107. success: function (res) {
    108. //res.path是网络图片的本地地址
    109. let Path = res.path;
    110. ctx.drawImage(Path, 0, 0, 325 * that.data.rpx , 450 * that.data.rpx)
    111. ctx.setTextAlign('left')
    112. ctx.setFillStyle('#000')
    113. ctx.setFontSize(18*that.data.rpx)
    114. ctx.fillText(that.data.nickname, (325 / 7) * that.data.rpx, 275*that.data.rpx)
    115. callback()
    116. },
    117. fail: function (res) {
    118. console.log(res)
    119. //失败回调
    120. }
    121. });
    122. },
    123. //保存
    124. saveewm: function () {
    125. let that = this;
    126. wx.showLoading({
    127. title: '努力生成中...'
    128. })
    129. wx.canvasToTempFilePath({
    130. x: 0,
    131. y: 0,
    132. width: that.data.winWidth,
    133. height: that.data.winHeight - 70,
    134. destWidth: that.data.winWidth,
    135. destHeight: that.data.winHeight - 70,
    136. canvasId: 'myCanvas',
    137. success: function (res) {
    138. wx.hideLoading()
    139. wx.saveImageToPhotosAlbum({
    140. filePath: res.tempFilePath,
    141. success(res) {
    142. wx.showModal({
    143. content: '图片已保存到相册了',
    144. showCancel: false,
    145. confirmText: '我知道啦',
    146. confirmColor: '#232323',
    147. success: function (res) {
    148. if (res.confirm) {
    149. console.log('用户点击确定');
    150. that.setData({
    151. hidden: true
    152. })
    153. }
    154. }
    155. })
    156. }
    157. })
    158. },
    159. fail: function (res) {
    160. console.log(res)
    161. }
    162. })
    163. },