combination.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. rpx:1,
  26. obverseImgObject: {
  27. '1': '/customized/images/customized/cup_black_big.png',/*正面 */
  28. '2': '/customized/images/customized/cup_black_side.png',/*侧面面 */
  29. '3': '/customized/images/customized/cup_black_handle.png',/*手柄 */
  30. '4': '/customized/images/customized/cup_black_bottom.png',/*底部 */
  31. },
  32. },
  33. /**
  34. * 生命周期函数--监听页面加载
  35. */
  36. onLoad: function (options) {
  37. if (wx.getStorageSync('madeCupImgData1')){
  38. this.setData({
  39. imgTouch: wx.getStorageSync('madeCupImgData1')
  40. })
  41. }
  42. },
  43. /**
  44. * 生命周期函数--监听页面初次渲染完成
  45. */
  46. onReady: function () {
  47. },
  48. /**
  49. * 生命周期函数--监听页面显示
  50. */
  51. onShow: function () {
  52. this.getCombImg(this)
  53. },
  54. /**
  55. * 生命周期函数--监听页面隐藏
  56. */
  57. onHide: function () {
  58. },
  59. /**
  60. * 生命周期函数--监听页面卸载
  61. */
  62. onUnload: function () {
  63. },
  64. /**
  65. * 页面相关事件处理函数--监听用户下拉动作
  66. */
  67. onPullDownRefresh: function () {
  68. },
  69. /**
  70. * 页面上拉触底事件的处理函数
  71. */
  72. onReachBottom: function () {
  73. },
  74. /**
  75. * 用户点击右上角分享
  76. */
  77. onShareAppMessage: function () {
  78. },
  79. getCombImg(that){
  80. let imgTouch = that.data.imgTouch;
  81. let dataPositionObj = that.data.dataPositionObj;
  82. wx.getSystemInfo({
  83. success: function (res) {
  84. let rpx = res.windowWidth / 750;
  85. that.setData({
  86. rpx: rpx
  87. });
  88. wx.downloadFile({
  89. url: imgTouch.src, // 仅为示例,并非真实的资源
  90. success(res) {
  91. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  92. console.log(res)
  93. if (res.statusCode === 200) {
  94. // canvas截图区域与图片的宽度比例
  95. let dWidth = dataPositionObj.width / imgTouch.width;
  96. // canvas截图区域与图片的高度比例
  97. let dHeight = dataPositionObj.height / imgTouch.height;
  98. const ctx = wx.createCanvasContext('comb-img');
  99. // 设置背景
  100. // ctx.setFillStyle('red')
  101. // ctx.fillRect(0, 0, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  102. ctx.drawImage(res.tempFilePath,imgTouch.x,imgTouch.y,imgTouch.width * rpx,imgTouch.height * rpx)
  103. ctx.draw(true,function(){
  104. wx.canvasToTempFilePath({
  105. canvasId: 'comb-img',
  106. success:function(res){
  107. console.log(res)
  108. that.getComb(that,res.tempFilePath)
  109. }
  110. }, that)
  111. })
  112. }
  113. }
  114. })
  115. },
  116. })
  117. },
  118. getComb(that,imgSrc){
  119. let imgTouch = that.data.imgTouch;
  120. let dataPositionObj = that.data.dataPositionObj;
  121. wx.getSystemInfo({
  122. success: function (res) {
  123. let rpx = res.windowWidth / 750;
  124. that.setData({
  125. rpx: rpx
  126. });
  127. let canvasBg = that.data.obverseImgObject[1];
  128. wx.downloadFile({
  129. url: imgSrc, // 仅为示例,并非真实的资源
  130. success(res) {
  131. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  132. console.log(res)
  133. if(res.statusCode === 200){
  134. console.log(res)
  135. const ctx = wx.createCanvasContext('comb-canvas');
  136. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx);
  137. ctx.draw();
  138. // 设置背景
  139. ctx.drawImage(res.tempFilePath, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height *rpx);
  140. ctx.draw(true)
  141. }
  142. }
  143. })
  144. },
  145. })
  146. }
  147. })