combination.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // customized/pages/made/combination/combination.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. dataPositionObj: { //正面数据
  8. top: 156,
  9. left: 286,
  10. width: 270,
  11. height: 270
  12. },
  13. imgTouch: {
  14. isActive: "1",
  15. src: "https://dlz.info666.com//temp/d945bfb0-5cc6-406d-a814-cf63b099041f.jpg",
  16. x: -82,
  17. y: 17,
  18. clientX: 195,
  19. clientY: 121,
  20. width: 517,
  21. height: 208.17866666666666,
  22. scale: 2.4834437086092715,
  23. angle: 27
  24. },
  25. fontTouch:{
  26. isActive: "1",
  27. fontContent: "Gg",
  28. isColor: "#ffffff",
  29. x: 10,
  30. y: 10,
  31. angle: 0,
  32. fontSize: 56,
  33. clientX: 0,
  34. clientY: 0,
  35. width:10,
  36. height:10
  37. },
  38. rpx:1,
  39. obverseImgObject: {
  40. '1': '/customized/images/customized/cup_black_big.png',/*正面 */
  41. '2': '/customized/images/customized/cup_black_side.png',/*侧面面 */
  42. '3': '/customized/images/customized/cup_black_handle.png',/*手柄 */
  43. '4': '/customized/images/customized/cup_black_bottom.png',/*底部 */
  44. },
  45. },
  46. /**
  47. * 生命周期函数--监听页面加载
  48. */
  49. onLoad: function (options) {
  50. if (wx.getStorageSync('madeCupImgData1')){
  51. this.setData({
  52. imgTouch: wx.getStorageSync('madeCupImgData1')
  53. })
  54. }
  55. if (wx.getStorageSync('madeCupFontData1')){
  56. this.setData({
  57. fontTouch: wx.getStorageSync('madeCupFontData1')
  58. })
  59. }
  60. },
  61. /**
  62. * 生命周期函数--监听页面初次渲染完成
  63. */
  64. onReady: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面显示
  68. */
  69. onShow: function () {
  70. this.getCombImg(this);
  71. this.getCombFont(this)
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide: function () {
  77. },
  78. /**
  79. * 生命周期函数--监听页面卸载
  80. */
  81. onUnload: function () {
  82. },
  83. /**
  84. * 页面相关事件处理函数--监听用户下拉动作
  85. */
  86. onPullDownRefresh: function () {
  87. },
  88. /**
  89. * 页面上拉触底事件的处理函数
  90. */
  91. onReachBottom: function () {
  92. },
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {
  97. },
  98. getCombImg(that){
  99. let imgTouch = that.data.imgTouch;
  100. let dataPositionObj = that.data.dataPositionObj;
  101. wx.getSystemInfo({
  102. success: function (res) {
  103. let rpx = res.windowWidth / 750;
  104. that.setData({
  105. rpx: rpx
  106. });
  107. wx.downloadFile({
  108. url: imgTouch.src, // 仅为示例,并非真实的资源
  109. success(res) {
  110. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  111. console.log(res)
  112. if (res.statusCode === 200) {
  113. // canvas截图区域与图片的宽度比例
  114. let dWidth = dataPositionObj.width / imgTouch.width;
  115. // canvas截图区域与图片的高度比例
  116. let dHeight = dataPositionObj.height / imgTouch.height;
  117. const ctx = wx.createCanvasContext('comb-img');
  118. //画布中点坐标转移到图片中心
  119. let movex = imgTouch.x + imgTouch.width * rpx/2;
  120. let movey = imgTouch.y + imgTouch.height * rpx/2;
  121. ctx.translate(movex, movey);
  122. ctx.rotate(imgTouch.angle * Math.PI / 180);
  123. ctx.drawImage(res.tempFilePath, -imgTouch.width * rpx / 2, -imgTouch.height * rpx/2,imgTouch.width * rpx,imgTouch.height * rpx)
  124. ctx.draw(true,function(){
  125. wx.canvasToTempFilePath({
  126. canvasId: 'comb-img',
  127. success:function(res){
  128. console.log(res)
  129. that.getComb(that,res.tempFilePath)
  130. }
  131. }, that)
  132. })
  133. }
  134. }
  135. })
  136. },
  137. })
  138. },
  139. getCombFont(that) {
  140. let fontTouch = that.data.fontTouch;
  141. let dataPositionObj = that.data.dataPositionObj;
  142. const ctx = wx.createCanvasContext('comb-font');
  143. let movex = fontTouch.x + fontTouch.width / 2;
  144. let movey = fontTouch.y + fontTouch.height / 2;
  145. ctx.translate(movex, movey);
  146. ctx.rotate(fontTouch.angle * Math.PI / 180);
  147. ctx.setFontSize(fontTouch.fontSize);
  148. ctx.setFillStyle(fontTouch.isColor);
  149. // ctx.fillText(fontTouch.fontContent, fontTouch.x, fontTouch.y + fontTouch.height / 5 *4);
  150. ctx.fillText(fontTouch.fontContent, -fontTouch.width / 2, fontTouch.height / 5 * 4 - fontTouch.height / 2);
  151. ctx.draw()
  152. },
  153. getComb(that,imgSrc){
  154. let imgTouch = that.data.imgTouch;
  155. let dataPositionObj = that.data.dataPositionObj;
  156. wx.getSystemInfo({
  157. success: function (res) {
  158. let rpx = res.windowWidth / 750;
  159. that.setData({
  160. rpx: rpx
  161. });
  162. let canvasBg = that.data.obverseImgObject[1];
  163. const ctx = wx.createCanvasContext('comb-canvas');
  164. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx);
  165. ctx.draw();
  166. // 设置背景
  167. ctx.drawImage(imgSrc, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  168. ctx.draw(true)
  169. },
  170. })
  171. }
  172. })