made.js 11 KB

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