madeFont.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // pages/customized/made/madeFont/madeFont.js
  2. import * as util from '../../../../utils/util.js'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. dataPositionObj: { //正面数据
  9. top: 184,
  10. left: 305,
  11. width: 218,
  12. height: 218,
  13. },
  14. propSize: {
  15. top: '5cm',
  16. right: '5cm',
  17. bottom: '5cm',
  18. left: '5cm',
  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. // radarImg:null,
  32. fontColorArray: ['#ffffff', '#000000', '#e70012', '#ff8500', '#fff100', '#22ad38', '#00b7f0', '#920784'],
  33. fontFamilyArray: ['Nomal', 'Monospace', 'Sans', 'Serif'], // 自带字体样式
  34. addFontStatic: false, // 添加文字
  35. inputFont: null,
  36. fontTouch: {
  37. isActive: '1', // 表示正面
  38. fontContent: '可定制区域',
  39. isColor: '#ffffff', // 默认是白色
  40. x: 10, // 字体的top值 x坐标
  41. y: 10, // 字体的 left值,y坐标
  42. angle: 0, // 旋转度数
  43. fontSize: null,
  44. clientX: 0, // 默认初始值
  45. clientY: 0, // 默认初始值
  46. width: 0,
  47. height: 0,
  48. },
  49. imgFont:{}, // 图片对象
  50. animationData: {}, //旋转动画
  51. imgAnimationData: {}, // 图片的旋转
  52. imgTouch:{},
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad: function (options) {
  58. let that = this;
  59. that.animation = wx.createAnimation({
  60. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  61. })
  62. that.imgAnimation = wx.createAnimation({
  63. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  64. })
  65. if ('id' in options) {
  66. let cupNum = util.changeCup(options.id);
  67. let madeTouchFont = wx.getStorageSync('madeCupFontData' + options.id);
  68. if (madeTouchFont) {
  69. that.animation.rotate(madeTouchFont.angle).step();
  70. that.setData({
  71. fontTouch: madeTouchFont,
  72. animationData: that.animation.export(),
  73. isActive: options.id,
  74. dataPositionObj: cupNum.dataPositionObj,
  75. propSize: cupNum.propSize
  76. })
  77. } else {
  78. that.setData({
  79. 'fontTouch.isActive': options.id,
  80. isActive: options.id,
  81. dataPositionObj: cupNum.dataPositionObj,
  82. propSize: cupNum.propSize
  83. })
  84. }
  85. }
  86. },
  87. /**
  88. * 生命周期函数--监听页面初次渲染完成
  89. */
  90. onReady: function () {
  91. let that = this;
  92. that.changeCupImg('1');
  93. },
  94. /**
  95. * 生命周期函数--监听页面显示
  96. */
  97. onShow: function () {
  98. this.getDataTouch(this)
  99. },
  100. /**
  101. * 生命周期函数--监听页面隐藏
  102. */
  103. onHide: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面卸载
  107. */
  108. onUnload: function () {
  109. },
  110. /**
  111. * 页面相关事件处理函数--监听用户下拉动作
  112. */
  113. onPullDownRefresh: function () {
  114. },
  115. /**
  116. * 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. },
  120. /**
  121. * 用户点击右上角分享
  122. */
  123. onShareAppMessage: function () {
  124. },
  125. changeCupImg(index) {
  126. let canvasBg = this.data.obverseImgObject[index],
  127. that = this;
  128. // wx.getSystemInfo({
  129. // success: function (res) {
  130. // let rpx = res.windowWidth / 750;
  131. // const ctx = wx.createCanvasContext('madeContent');
  132. // ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx)
  133. // ctx.draw();
  134. // setTimeout(()=>{
  135. // that.handleCanvarToImg(that)
  136. // },1000)
  137. // },
  138. // })
  139. },
  140. addFont() { // 添加文字
  141. let that = this;
  142. that.setData({
  143. addFontStatic: true
  144. })
  145. },
  146. fontCancel() {
  147. this.setData({
  148. addFontStatic: false
  149. })
  150. },
  151. bindinput(e) {
  152. this.setData({
  153. inputFont: e.detail.value,
  154. })
  155. },
  156. fontConfirm(e) {
  157. let that = this;
  158. if (that.data.inputFont) {
  159. this.animation.rotate(0).step()
  160. that.setData({
  161. 'fontTouch.fontContent': that.data.inputFont,
  162. 'fontTouch.fontSize': 30,
  163. 'fontTouch.angle': 0,
  164. addFontStatic: false,
  165. animationData: this.animation.export()
  166. })
  167. // that.getTouchFontInformation(that)
  168. that.setData({
  169. inputFont: null,
  170. })
  171. }
  172. },
  173. handleCanvarToImg(that) {
  174. wx.canvasToTempFilePath({
  175. x: 0,
  176. y: 0,
  177. width: 750,
  178. height: 588,
  179. canvasId: 'madeContent',
  180. success: function (res) {
  181. that.setData({
  182. radarImg: res.tempFilePath,
  183. })
  184. }
  185. });
  186. },
  187. selectColor(e) { // 选择颜色
  188. this.setData({
  189. 'fontTouch.isColor': e.currentTarget.dataset.color
  190. })
  191. },
  192. // touch 事件
  193. fontTouchstart(e) {
  194. //1.获取鼠标点击下去的
  195. this.setData({
  196. "fontTouch.clientX": e.touches[0].clientX - this.data.fontTouch.x,
  197. "fontTouch.clientY": e.touches[0].clientY - this.data.fontTouch.y
  198. });
  199. console.log(this.data.fontTouch, "touch")
  200. },
  201. fontTouchmove(e) {
  202. // 鼠标移动的位置
  203. this.setData({
  204. "fontTouch.x": e.changedTouches[0].clientX - this.data.fontTouch.clientX,
  205. "fontTouch.y": e.changedTouches[0].clientY - this.data.fontTouch.clientY
  206. });
  207. // console.log(this.data.touch)
  208. },
  209. fontTouchend(e) {
  210. console.log('移动结束', e)
  211. console.log(this.data.fontTouch)
  212. },
  213. fontDelete() {
  214. // 删除
  215. let touch = {
  216. fontContent: '可定制区域',
  217. isColor: '#ffffff', // 默认是白色
  218. x: 10, // 字体的top值 x坐标
  219. y: 10, // 字体的 left值,y坐标
  220. rotate: 180, // 旋转度数
  221. fontSize: null,
  222. clientX: 0, // 默认初始值
  223. clientY: 0, // 默认初始值
  224. width: 0,
  225. height: 0,
  226. }
  227. this.setData({
  228. fontTouch: touch,
  229. })
  230. },
  231. fontEnlarge(e) { // 放大与缩小
  232. console.log(e.detail)
  233. this.setData({
  234. 'fontTouch.fontSize': e.detail.value
  235. })
  236. },
  237. fontRotate(e) { // 旋转
  238. this.animation.rotate(e.detail.value).step();
  239. this.setData({
  240. animationData: this.animation.export(),
  241. 'fontTouch.angle': e.detail.value
  242. });
  243. },
  244. madeConfirm() { // 设计完成进行确定
  245. let that = this;
  246. if (that.data.fontTouch.fontSize) {
  247. that.getTouchFontInformation(that);
  248. // wx.redirectTo({
  249. // url: '/customized/pages/made/made?id='+ this.data.isActive,
  250. // })
  251. } else {
  252. wx.showToast({
  253. title: '您还没有添加文字进行设计',
  254. icon: 'none'
  255. })
  256. }
  257. },
  258. getTouchFontInformation(that) {
  259. let query = wx.createSelectorQuery();
  260. query.select('#touchFont').boundingClientRect()
  261. query.exec(function (res) {
  262. console.log(res, "获取font的宽高");
  263. that.setData({
  264. 'fontTouch.width': res[0].width,
  265. 'fontTouch.height': res[0].height
  266. });
  267. wx.setStorageSync('madeCupFontData' + that.data.isActive, that.data.fontTouch);
  268. wx.navigateBack()
  269. })
  270. },
  271. getDataTouch(that) {
  272. let madeTouchImg = wx.getStorageSync('madeCupImgData' + that.data.isActive)
  273. if (madeTouchImg) {
  274. that.imgAnimation.rotate(madeTouchImg.angle).step();
  275. that.setData({
  276. imgAnimationData: that.imgAnimation.export(),
  277. imgTouch: madeTouchImg ? madeTouchImg : {}, //存储图片数据
  278. })
  279. }
  280. },
  281. })