made.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // pages/customized/made/made.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. propSize: {
  15. top: '8cm',
  16. right: '8cm',
  17. bottom: '8cm',
  18. left: '8cm',
  19. },
  20. isActive: '1', // 默认是正面
  21. obverseImgObject: {
  22. '1': '/customized/images/customized/cup_black_big.png',
  23. /*正面 */
  24. '2': '/customized/images/customized/cup_black_side.png',
  25. /*侧面面 */
  26. '3': '/customized/images/customized/cup_black_handle.png',
  27. /*手柄 */
  28. '4': '/customized/images/customized/cup_black_bottom.png',
  29. /*底部 */
  30. },
  31. fontColor: ['#000', '#fff', '#e70012', '#ff8500', '#fff100', '#22ad38', '#00b7f0', '#920784'],
  32. // obverseImg: '/images/customized/cup_black_big.png',/*正面 */
  33. imgTouch: {}, // 图片的对象,分为正面,侧面,手柄,底部
  34. fontTouch: {}, //文字的对象,分为正面,侧面,手柄,底部
  35. imgAnimationData: {}, // 图片的旋转
  36. fontAnimationData: {}, // 文字的旋转
  37. touchStatic: true,
  38. fontContent: '可定制区域',
  39. rpx: 1, // 获取页面的rpx
  40. imgSrcArray: [],
  41. imgBuildArray: [], // 生成后图片的数组
  42. simulation:{ // 模拟字体,获取字体的宽高
  43. fontContent:'',
  44. fontSize:'',
  45. height:'',
  46. }
  47. },
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. onLoad: function (options) {
  52. let that = this;
  53. wx.getSystemInfo({
  54. success: function (res) {
  55. let rpx = res.windowWidth / 750;
  56. that.setData({
  57. rpx: rpx
  58. });
  59. console.log(rpx, 'rpx')
  60. },
  61. })
  62. if ('id' in options) {
  63. this.setData({
  64. isActive: options.id
  65. })
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面初次渲染完成
  70. */
  71. onReady: function () {
  72. let that = this;
  73. // console.log('页面开始准备中'
  74. that.imgAnimation = wx.createAnimation({
  75. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  76. })
  77. that.fontAnimation = wx.createAnimation({
  78. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  79. })
  80. },
  81. /**
  82. * 生命周期函数--监听页面显示
  83. */
  84. onShow: function () {
  85. console.log('onshow')
  86. let that = this;
  87. let cupNum = util.changeCup(that.data.isActive);
  88. that.setData({
  89. dataPositionObj: cupNum.dataPositionObj,
  90. propSize: cupNum.propSize
  91. })
  92. that.getDataTouch(that)
  93. },
  94. /**
  95. * 生命周期函数--监听页面隐藏
  96. */
  97. onHide: function () {
  98. },
  99. /**
  100. * 生命周期函数--监听页面卸载
  101. */
  102. onUnload: function () {
  103. },
  104. /**
  105. * 页面相关事件处理函数--监听用户下拉动作
  106. */
  107. onPullDownRefresh: function () {
  108. },
  109. /**
  110. * 页面上拉触底事件的处理函数
  111. */
  112. onReachBottom: function () {
  113. },
  114. /**
  115. * 用户点击右上角分享
  116. */
  117. onShareAppMessage: function () {
  118. },
  119. // changeCupImg(index){
  120. // console.log(this.data.imgTouch,"this.data.imgTouch")
  121. // let rpx = this.data.rpx;
  122. // let canvasBg = this.data.obverseImgObject[index];
  123. // const ctx = wx.createCanvasContext('madeContent');
  124. // ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx)
  125. // ctx.draw()
  126. // },
  127. changeCup(e) {
  128. // data-id== 1正面,2背面,3手柄,4杯底
  129. // this.changeCupImg(e.currentTarget.dataset.id);
  130. let that = this;
  131. let cupNum = util.changeCup(e.currentTarget.dataset.id)
  132. that.setData({
  133. isActive: e.currentTarget.dataset.id,
  134. dataPositionObj: cupNum.dataPositionObj,
  135. propSize: cupNum.propSize
  136. })
  137. that.getDataTouch(that)
  138. },
  139. addImgRouter() {
  140. console.log('添加图片');
  141. wx.navigateTo({
  142. url: '/customized/pages/made/madeImg/madeImg?id=' + this.data.isActive,
  143. })
  144. },
  145. addFontRouter() {
  146. console.log('添加文字');
  147. wx.navigateTo({
  148. url: '/customized/pages/made/madeFont/madeFont?id=' + this.data.isActive,
  149. })
  150. },
  151. getDataTouch(that) {
  152. let madeTouchImg = wx.getStorageSync('madeCupImgData' + that.data.isActive)
  153. let madeTouchFont = wx.getStorageSync('madeCupFontData' + that.data.isActive)
  154. console.log(madeTouchImg, madeTouchFont, "madeTouchFontmadeTouchFont")
  155. if (madeTouchImg || madeTouchFont) {
  156. that.setData({
  157. touchStatic: false
  158. })
  159. if (madeTouchImg) {
  160. that.imgAnimation.rotate(madeTouchImg.angle).step();
  161. that.setData({
  162. imgAnimationData: that.imgAnimation.export(),
  163. })
  164. }
  165. if (madeTouchFont) {
  166. that.fontAnimation.rotate(madeTouchFont.angle).step();
  167. that.setData({
  168. fontAnimationData: that.fontAnimation.export(),
  169. })
  170. that.setData({
  171. 'simulation.fontContent': madeTouchFont.fontContent,
  172. 'simulation.fontSize': madeTouchFont.fontSize
  173. })
  174. that.getTouchFontInformation(that)
  175. }
  176. } else {
  177. that.setData({
  178. touchStatic: true
  179. })
  180. }
  181. that.setData({
  182. imgTouch: madeTouchImg ? madeTouchImg : {}, //存储图片数据
  183. fontTouch: madeTouchFont ? madeTouchFont : {},
  184. })
  185. },
  186. finishDesign() { // 完成设计
  187. let that = this;
  188. let designObj = {
  189. '1': '正面',
  190. '2': '背面',
  191. '3': '手柄',
  192. '4': '杯底',
  193. },
  194. designVal = '',
  195. madeStatic = false;
  196. for (let i = 1; i < 5; i++) {
  197. if (!wx.getStorageSync('madeCupImgData' + i) && !wx.getStorageSync('madeCupFontData' + i)) {
  198. designVal += designObj[i] + ','
  199. } else {
  200. madeStatic = true; // 表示已经有设计过的页面
  201. }
  202. }
  203. if (madeStatic) {
  204. if (!designVal) {
  205. console.log('可以完成')
  206. wx.navigateTo({
  207. // url: '/customized/pages/made/combination/combination',
  208. url: '/customized/pages/made/comb/comb'
  209. })
  210. } else {
  211. wx.showModal({
  212. title: '温馨提示',
  213. content: '您的' + designVal.substring(0, designVal.length - 1) + '还没有设计,是否确定不设计了?',
  214. success: function (res) {
  215. if (res.confirm) {
  216. console.log('用户点击确定,不设计了,直接完成')
  217. // wx.showToast({
  218. // title: '图片合成中...',
  219. // icon: 'loading'
  220. // })
  221. // that.getBuildImg(that)
  222. wx.navigateTo({
  223. // url: '/customized/pages/made/combination/combination',
  224. url:'/customized/pages/made/comb/comb'
  225. })
  226. } else {
  227. console.log('用户点击取消')
  228. }
  229. }
  230. })
  231. }
  232. } else {
  233. wx.showToast({
  234. title: '您还没有设计',
  235. icon: 'none'
  236. })
  237. }
  238. },
  239. getTouchFontInformation(that) {
  240. let query = wx.createSelectorQuery();
  241. query.select('#simulation').boundingClientRect()
  242. query.exec(function (res) {
  243. console.log(res, "获取font的宽高");
  244. that.setData({
  245. 'simulation.height': res[0].height
  246. });
  247. wx.setStorageSync('simulationFont' + that.data.isActive, that.data.simulation);
  248. })
  249. },
  250. // getBuildImg(that) {
  251. // for (let i = 1; i <= 1; i++) {
  252. // if (wx.getStorageSync('madeCupImgData' + i) || wx.getStorageSync('madeCupFontData' + i)) {
  253. // if (wx.getStorageSync('madeCupImgData' + i)) {
  254. // this.setData({
  255. // imgTouch: wx.getStorageSync('madeCupImgData' + i)
  256. // })
  257. // }
  258. // if (wx.getStorageSync('madeCupFontData' + i)) {
  259. // this.setData({
  260. // fontTouch: wx.getStorageSync('madeCupFontData' + i)
  261. // })
  262. // }
  263. // that.getCombImg(that);
  264. // }
  265. // console.log(i)
  266. // }
  267. // },
  268. // 合成canvas的js
  269. // getCombImg(that) {
  270. // let imgTouch = that.data.imgTouch;
  271. // let dataPositionObj = that.data.dataPositionObj;
  272. // wx.getSystemInfo({
  273. // success: function (res) {
  274. // let rpx = res.windowWidth / 750;
  275. // that.setData({
  276. // rpx: rpx
  277. // });
  278. // wx.downloadFile({
  279. // url: imgTouch.src, // 仅为示例,并非真实的资源
  280. // success(res) {
  281. // // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  282. // console.log(res)
  283. // if (res.statusCode === 200) {
  284. // // canvas截图区域与图片的宽度比例
  285. // let dWidth = dataPositionObj.width / imgTouch.width;
  286. // // canvas截图区域与图片的高度比例
  287. // let dHeight = dataPositionObj.height / imgTouch.height;
  288. // const ctx = wx.createCanvasContext('comb-img');
  289. // //画布中点坐标转移到图片中心
  290. // let movex = imgTouch.x + imgTouch.width * rpx / 2;
  291. // let movey = imgTouch.y + imgTouch.height * rpx / 2;
  292. // ctx.translate(movex, movey);
  293. // ctx.rotate(imgTouch.angle * Math.PI / 180);
  294. // ctx.drawImage(res.tempFilePath, -imgTouch.width * rpx / 2, -imgTouch.height * rpx / 2, imgTouch.width * rpx, imgTouch.height * rpx)
  295. // ctx.draw(true, function () {
  296. // wx.canvasToTempFilePath({
  297. // canvasId: 'comb-img',
  298. // success: function (res) {
  299. // console.log(res)
  300. // let imgArray = that.data.imgSrcArray;
  301. // imgArray.push(res.tempFilePath)
  302. // that.setData({
  303. // imgSrcArray: imgArray
  304. // })
  305. // that.getCombFont(that);
  306. // }
  307. // }, that)
  308. // })
  309. // }
  310. // }
  311. // })
  312. // },
  313. // })
  314. // },
  315. // getCombFont(that) {
  316. // let fontTouch = that.data.fontTouch;
  317. // let dataPositionObj = that.data.dataPositionObj;
  318. // const ctx = wx.createCanvasContext('comb-font');
  319. // let movex = fontTouch.x + fontTouch.width / 2;
  320. // let movey = fontTouch.y + fontTouch.height / 2;
  321. // // ctx.translate(movex, movey);
  322. // // ctx.rotate(fontTouch.angle * Math.PI / 180);
  323. // ctx.setFontSize(fontTouch.fontSize);
  324. // ctx.setFillStyle(fontTouch.isColor);
  325. // ctx.fillText(fontTouch.fontContent, fontTouch.x, fontTouch.y + fontTouch.height / 5 * 4);
  326. // // ctx.fillText(fontTouch.fontContent, -fontTouch.width / 2, fontTouch.height / 10 * 3);
  327. // ctx.draw(true, function () {
  328. // wx.canvasToTempFilePath({
  329. // canvasId: 'comb-font',
  330. // success: function (res) {
  331. // console.log(res)
  332. // let imgArray = that.data.imgSrcArray;
  333. // imgArray.push(res.tempFilePath)
  334. // that.setData({
  335. // imgSrcArray: imgArray
  336. // })
  337. // console.log(that.data.imgSrcArray, "that.data.imgSrcArray")
  338. // that.getComb(that, that.data.imgSrcArray)
  339. // }
  340. // }, that)
  341. // })
  342. // },
  343. // getComb(that, imgSrcArray) {
  344. // console.log('开始最后的合成')
  345. // let imgTouch = that.data.imgTouch;
  346. // let dataPositionObj = that.data.dataPositionObj;
  347. // wx.getSystemInfo({
  348. // success: function (res) {
  349. // let rpx = res.windowWidth / 750;
  350. // that.setData({
  351. // rpx: rpx
  352. // });
  353. // let canvasBg = that.data.obverseImgObject[1];
  354. // const ctx = wx.createCanvasContext('comb-canvas');
  355. // ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx);
  356. // ctx.draw(true);
  357. // // 设置背景
  358. // imgSrcArray.forEach((res) => {
  359. // ctx.drawImage(res, dataPositionObj.left * rpx, dataPositionObj.top * rpx, dataPositionObj.width * rpx, dataPositionObj.height * rpx);
  360. // })
  361. // console.log('渲染完成');
  362. // ctx.draw(true);
  363. // // ctx.draw(true, function () {
  364. // // wx.canvasToTempFilePath({
  365. // // canvasId: 'comb-canvas',
  366. // // success: function (res) {
  367. // // console.log(res, "最后的合成")
  368. // // let imgBuildArray = that.data.imgBuildArray;
  369. // // imgBuildArray.push(res.tempFilePath)
  370. // // that.setData({
  371. // // imgSrcArray: [], // 重置
  372. // // imgBuildArray: imgBuildArray
  373. // // })
  374. // // console.log(that.data.imgBuildArray, "that.data.imgBuildArray")
  375. // // }
  376. // // }, that)
  377. // // })
  378. // },
  379. // })
  380. // }
  381. })