当前位置:   article > 正文

微信小程序:使用canvas 生成图片 并分享_小程序canvas生成图片

小程序canvas生成图片

废话不多说直接上代码!!!!

html

 <canvas canvas-id="positionPoster" style="width: 414px; height: 672px; margin: 0; padding: 0;" v-if="isCanvas"></canvas>

js

  1. // 1、canvas对象 2、文本 3、X轴 4、Y轴 5、单行行高 6、文本的宽度
  2. */
  3. toFormateStr(ctx, str, axisX, axisY, titleHeight, maxWidth) {
  4. // 字体
  5. ctx.setFontSize(14 * this.rpx);
  6. // 颜色
  7. ctx.setFillStyle("#353535");
  8. // 文本处理
  9. let strArr = str.split("");
  10. let row = [];
  11. let temp = "";
  12. for (let i = 0; i < strArr.length; i++) {
  13. if (ctx.measureText(temp).width < maxWidth) {
  14. temp += strArr[i];
  15. } else {
  16. i--; //这里添加了i-- 是为了防止字符丢失,效果图中有对比
  17. row.push(temp);
  18. temp = "";
  19. }
  20. }
  21. row.push(temp); // row有多少项则就有多少行
  22. //如果数组长度大于2,现在只需要显示两行则只截取前两项,把第二行结尾设置成'...'
  23. if (row.length > 2) {
  24. let rowCut = row.slice(0, 2);
  25. let rowPart = rowCut[1];
  26. let test = "";
  27. let empty = [];
  28. for (let i = 0; i < rowPart.length; i++) {
  29. if (ctx.measureText(test).width < maxWidth) {
  30. test += rowPart[i];
  31. } else {
  32. break;
  33. }
  34. }
  35. empty.push(test);
  36. let group = empty[0] + "..."; //这里只显示两行,超出的用...表示
  37. rowCut.splice(1, 1, group);
  38. row = rowCut;
  39. }
  40. // 把文本绘制到画布中
  41. for (let i = 0; i < row.length; i++) {
  42. // 一次渲染一行
  43. ctx.fillText(row[i], axisX, axisY + i * titleHeight, maxWidth);
  44. }
  45. // // 保存当前画布状态
  46. // ctx.save();
  47. // // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  48. // ctx.draw();
  49. },
  50. //分享绘制
  51. shareImg() {
  52. this.isCanvas = true;
  53. wx.showLoading({
  54. title: "保存中...",
  55. });
  56. var that = this;
  57. let nickName = wx.getStorageSync("user")["nickName"];
  58. var context = wx.createCanvasContext("positionPoster");
  59. context.beginPath();
  60. context.fillStyle = "white";
  61. context.fillRect(0, 0, that.screenWidth, that.screenHeight);
  62. context.font = "14px sans-serif";
  63. context.fillStyle = "black";
  64. context.fillText(
  65. `${nickName} 推荐给你${that.detail.name}学院`,
  66. 50,
  67. 35,
  68. 250
  69. );
  70. context.font = "14px sans-serif";
  71. context.fillStyle = "black";
  72. context.fillText(
  73. `已经有${that.detail.focusNum}个同学都在关注了`,
  74. 50,
  75. 65,
  76. 250
  77. );
  78. context.font = "bold 16px sans-serif";
  79. context.fillStyle = "black";
  80. context.fillText(`${that.detail.name}`, 50, 100, 250);
  81. //绘制一个圆角矩形
  82. context.setStrokeStyle("black");
  83. context.strokeRect(50, 115, 150, 20);
  84. context.font = "14px sans-serif";
  85. context.fillStyle = "black";
  86. context.fillText(
  87. `${that.detail.stateRun},${that.detail.is211 ? "211" : ""},${
  88. that.detail.is985 ? "985" : ""
  89. },${that.detail.doubleFirstClass ? "双一流" : ""}`,
  90. 55,
  91. 130,
  92. 150
  93. );
  94. context.font = "16px bold";
  95. context.fillText(`简介`, 50, 160, 70);
  96. //多行显示
  97. that.toFormateStr(context, that.detail.introduce, 50, 185, 24, 300);
  98. context.font = "14px sans-serif";
  99. context.fillStyle = "black";
  100. let y = 240;
  101. const y_text = 260;
  102. context.fillText(`成立时间`, 50, y, 100);
  103. context.font = "14px sans-serif";
  104. context.fillStyle = "black";
  105. context.fillText(`关注人数`, 170, y, 100);
  106. context.font = "14px sans-serif";
  107. context.fillStyle = "black";
  108. context.fillText(`报考人数`, 270, y, 130);
  109. context.font = "14px sans-serif";
  110. context.fillStyle = "black";
  111. context.fillText(
  112. `${that.detail.time ? that.detail.time : "--"}`,
  113. 60,
  114. y_text,
  115. 100
  116. );
  117. context.font = "14px sans-serif";
  118. context.fillStyle = "black";
  119. context.fillText(
  120. `${that.detail.focusNum ? that.detail.focusNum : "暂无"}`,
  121. 180,
  122. y_text,
  123. 100
  124. );
  125. context.font = "14px sans-serif";
  126. context.fillStyle = "black";
  127. context.fillText(
  128. `已有${that.detail.examNum ? that.detail.examNum + "人" : "暂无"}`,
  129. `${that.detail.examNum ? y_text : 305}`,
  130. y_text,
  131. 130
  132. );
  133. //画图二维码
  134. context.drawImage(that.drImg, 80, 300, 250, 250);
  135. context.draw(false, function () {
  136. wx.canvasToTempFilePath({
  137. x: 0,
  138. y: 0,
  139. width: that.screenWidth,
  140. height: that.screenHeight,
  141. destWidth: that.screenWidth * that.pixelRatio,
  142. destHeight: that.screenHeight * that.pixelRatio,
  143. canvasId: "positionPoster",
  144. success: function (res) {
  145. that.saveImage = res.tempFilePath;
  146. if (!res.tempFilePath) {
  147. wx.showModal({
  148. title: "提示",
  149. content: "图片绘制中,请稍后重试",
  150. showCancel: false,
  151. });
  152. }
  153. //画板路径保存成功后,调用方法吧图片保存到用户相册
  154. wx.saveImageToPhotosAlbum({
  155. filePath: res.tempFilePath,
  156. //保存成功失败之后,都要隐藏画板,否则影响界面显示。
  157. success: (res) => {
  158. wx.hideLoading();
  159. that.isShow = false;
  160. that.isCanvas = false;
  161. Toast.success({ message: "保存成功!" });
  162. },
  163. fail: (err) => {
  164. console.log(err);
  165. wx.hideLoading();
  166. that.isShow = false;
  167. that.isCanvas = false;
  168. },
  169. });
  170. },
  171. fail: (res) => {
  172. wx.hideLoading();
  173. that.isShow = false;
  174. console.log(res);
  175. },
  176. });
  177. });
  178. },

最终效果图

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/639629
推荐阅读
相关标签
  

闽ICP备14008679号