madeFont.js 6.8 KB

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