comb.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // customized/pages/made/combination/combination.js
  2. import * as util from '../../../../utils/util.js'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. dataPositionObj: { //正面数据
  9. top: 156,
  10. left: 286,
  11. width: 270,
  12. height: 270
  13. },
  14. imgTouch: {
  15. isActive: "1",
  16. src: "https://dlz.info666.com//temp/d945bfb0-5cc6-406d-a814-cf63b099041f.jpg",
  17. x: -82,
  18. y: 17,
  19. clientX: 195,
  20. clientY: 121,
  21. width: 517,
  22. height: 208.17866666666666,
  23. scale: 2.4834437086092715,
  24. angle: 27
  25. },
  26. fontTouch: {
  27. isActive: "1",
  28. fontContent: "Gg",
  29. isColor: "#ffffff",
  30. x: 10,
  31. y: 10,
  32. angle: 0,
  33. fontSize: 100,
  34. clientX: 0,
  35. clientY: 0,
  36. width: 138.34375,
  37. height: 132
  38. },
  39. rpx: 1,
  40. obverseImgObject:['', '/customized/images/customized/cup_black_big.png', '/customized/images/customized/cup_black_side.png', '/customized/images/customized/cup_black_handle.png', '/customized/images/customized/cup_black_bottom.png'],
  41. // obverseImgObject: {
  42. // '1': ,
  43. // /*正面 */
  44. // '2': '/customized/images/customized/cup_black_side.png',
  45. // /*侧面面 */
  46. // '3': '/customized/images/customized/cup_black_handle.png',
  47. // /*手柄 */
  48. // '4': '/customized/images/customized/cup_black_bottom.png',
  49. // /*底部 */
  50. // },
  51. imgSrcArray: [],
  52. buildSrcArrayText:{
  53. '1':'正面',
  54. '2':'侧面',
  55. '3':'手柄',
  56. '4':'底部',
  57. },
  58. buildSrcArray: [],
  59. },
  60. /**
  61. * 生命周期函数--监听页面加载
  62. */
  63. onLoad: function (options) {
  64. // if (wx.getStorageSync('madeCupImgData1')) {
  65. // this.setData({
  66. // imgTouch: wx.getStorageSync('madeCupImgData1')
  67. // })
  68. // }
  69. // if (wx.getStorageSync('madeCupFontData1')) {
  70. // this.setData({
  71. // fontTouch: wx.getStorageSync('madeCupFontData1')
  72. // })
  73. // }
  74. },
  75. /**
  76. * 生命周期函数--监听页面初次渲染完成
  77. */
  78. onReady: function () {
  79. },
  80. /**
  81. * 生命周期函数--监听页面显示
  82. */
  83. onShow: function () {
  84. // this.getCombImg(this);
  85. // if (this.buildSrcArray.length === 0){
  86. wx.showLoading({
  87. title: '图片合成中...'
  88. })
  89. let that = this;
  90. // for (let i = 1; i <= 4; i++) {
  91. // if (wx.getStorageSync('madeCupImgData' + i) || wx.getStorageSync('madeCupFontData' + i)) {
  92. // console.log(i, "iiiii")
  93. // that.getCombImg(that, i);
  94. // }
  95. // }
  96. if (wx.getStorageSync('madeCupImgData1') || wx.getStorageSync('madeCupFontData1')) {
  97. that.getCombImg(that, 1);
  98. } else if (wx.getStorageSync('madeCupImgData2') || wx.getStorageSync('madeCupFontData2')) {
  99. that.getCombImg(that, 2);
  100. } else if (wx.getStorageSync('madeCupImgData3') || wx.getStorageSync('madeCupFontData3')) {
  101. that.getCombImg(that, 3);
  102. } else if (wx.getStorageSync('madeCupImgData4') || wx.getStorageSync('madeCupFontData4')) {
  103. that.getCombImg(that, 4);
  104. } else {
  105. wx.hideLoading();
  106. }
  107. // }
  108. },
  109. /**
  110. * 生命周期函数--监听页面隐藏
  111. */
  112. onHide: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面卸载
  116. */
  117. onUnload: function () {
  118. },
  119. /**
  120. * 页面相关事件处理函数--监听用户下拉动作
  121. */
  122. onPullDownRefresh: function () {
  123. },
  124. /**
  125. * 页面上拉触底事件的处理函数
  126. */
  127. onReachBottom: function () {
  128. },
  129. /**
  130. * 用户点击右上角分享
  131. */
  132. onShareAppMessage: function () {
  133. },
  134. getCombImg(that,i) {
  135. let imgTouch = null, dataPositionObj=null;
  136. that.setData({ // imgSrcArray 初始化
  137. imgSrcArray:[],
  138. })
  139. console.log(wx.getStorageSync('madeCupImgData' + i),"wx.getStorageSync('madeCupImgData' + i)")
  140. if(wx.getStorageSync('madeCupImgData' + i)){
  141. imgTouch = wx.getStorageSync('madeCupImgData'+i);
  142. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  143. console.log(dataPositionObj,"dataPositionObj")
  144. wx.getSystemInfo({
  145. success: function (res) {
  146. let rpx = res.windowWidth / 750;
  147. that.setData({
  148. rpx: rpx
  149. });
  150. wx.downloadFile({
  151. url: imgTouch.src, // 仅为示例,并非真实的资源
  152. success(res) {
  153. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  154. console.log(res)
  155. if (res.statusCode === 200) {
  156. // canvas截图区域与图片的宽度比例
  157. let dWidth = dataPositionObj.width / imgTouch.width;
  158. // canvas截图区域与图片的高度比例
  159. let dHeight = dataPositionObj.height / imgTouch.height;
  160. const ctx = wx.createCanvasContext('comb-img'+i);
  161. //画布中点坐标转移到图片中心
  162. let movex = imgTouch.x + imgTouch.width * rpx / 2;
  163. let movey = imgTouch.y + imgTouch.height * rpx / 2;
  164. ctx.translate(movex, movey);
  165. ctx.rotate(imgTouch.angle * Math.PI / 180);
  166. ctx.drawImage(res.tempFilePath, -imgTouch.width * rpx / 2, -imgTouch.height * rpx / 2, imgTouch.width * rpx, imgTouch.height * rpx)
  167. ctx.draw(false,function () {
  168. wx.canvasToTempFilePath({
  169. canvasId: 'comb-img'+i,
  170. success: function (res) {
  171. console.log(res)
  172. let imgArray = that.data.imgSrcArray;
  173. imgArray.push(res.tempFilePath)
  174. that.setData({
  175. imgSrcArray: imgArray
  176. })
  177. that.getCombFont(that,i,true);
  178. }
  179. }, that)
  180. })
  181. }
  182. }
  183. })
  184. },
  185. })
  186. } else {
  187. that.getCombFont(that,i,false);
  188. }
  189. },
  190. getCombFont(that,i,_static) {
  191. let fontTouch = null, dataPositionObj = null, simulationFont=null;
  192. if (wx.getStorageSync('madeCupFontData' + i)) {
  193. fontTouch = wx.getStorageSync('madeCupFontData' + i);
  194. simulationFont = wx.getStorageSync('simulationFont' + i);
  195. dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  196. const ctx = wx.createCanvasContext('comb-font'+i);
  197. // ctx.textBaseline = 'top';
  198. ctx.setFontSize(fontTouch.fontSize);
  199. ctx.setFillStyle(fontTouch.isColor);
  200. const metrics = ctx.measureText(fontTouch.fontContent)
  201. let movex = fontTouch.x + metrics.width / 2;
  202. let movey = fontTouch.y + simulationFont.height / 2;
  203. ctx.translate(movex, movey);
  204. ctx.rotate(fontTouch.angle * Math.PI / 180);
  205. ctx.fillText(fontTouch.fontContent, -metrics.width / 2, -simulationFont.height / 2 + simulationFont.height / 5 * 4);
  206. console.log(metrics,"文本的宽度")
  207. ctx.draw(false,function () {
  208. wx.canvasToTempFilePath({
  209. canvasId: 'comb-font'+i,
  210. success: function (res) {
  211. console.log(res)
  212. let imgArray = that.data.imgSrcArray;
  213. imgArray.push(res.tempFilePath)
  214. that.setData({
  215. imgSrcArray: imgArray
  216. })
  217. console.log(that.data.imgSrcArray, "that.data.imgSrcArray")
  218. that.getComb(that, that.data.imgSrcArray,i,true)
  219. }
  220. }, that)
  221. })
  222. } else {
  223. that.getComb(that, that.data.imgSrcArray, i, _static)
  224. }
  225. },
  226. getComb(that, imgSrcArray,i,_static) {
  227. if(_static){
  228. let imgTouch = wx.getStorageSync('madeCupImgData' + i);
  229. let dataPositionObj = util.changeCup(i.toString()).dataPositionObj;
  230. let canvasBg = that.data.obverseImgObject[i];
  231. console.log(canvasBg)
  232. wx.getSystemInfo({
  233. success: function (res) {
  234. let rpx = res.windowWidth / 750;
  235. that.setData({
  236. rpx: rpx
  237. });
  238. const ctx = wx.createCanvasContext('comb-canvas' + i);
  239. ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx);
  240. // 设置背景
  241. ctx.draw(true)
  242. imgSrcArray.forEach((res) => {
  243. ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  244. })
  245. ctx.draw(true, function () {
  246. wx.canvasToTempFilePath({
  247. canvasId: 'comb-canvas' + i,
  248. success: function (res) {
  249. console.log(res, "合成成功");
  250. let buildSrcArray = that.data.buildSrcArray;
  251. buildSrcArray.push({
  252. src:res.tempFilePath,
  253. isActive:i,
  254. });
  255. console.log(buildSrcArray, "buildSrcArray")
  256. that.setData({
  257. buildSrcArray: buildSrcArray
  258. })
  259. console.log(that.data.buildSrcArray, "buildSrcArray")
  260. if (i < 4) {
  261. i = i + 1
  262. that.getCombImg(that, i);
  263. } else {
  264. wx.hideLoading()
  265. }
  266. },
  267. fail: function (err) {
  268. console.log(err, "合成失败")
  269. wx.hideLoading();
  270. }
  271. }, that)
  272. })
  273. },
  274. })
  275. } else {
  276. wx.hideLoading()
  277. }
  278. }
  279. })