madeFont.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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: '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. /*正面 */
  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': 100,
  156. 'touch.angle': 0,
  157. addFontStatic: false,
  158. animationData: this.animation.export()
  159. })
  160. that.setData({
  161. inputFont: null,
  162. })
  163. }
  164. },
  165. handleCanvarToImg(that) {
  166. wx.canvasToTempFilePath({
  167. x: 0,
  168. y: 0,
  169. width: 750,
  170. height: 588,
  171. canvasId: 'madeContent',
  172. success: function (res) {
  173. that.setData({
  174. radarImg: res.tempFilePath,
  175. })
  176. }
  177. });
  178. },
  179. selectColor(e) { // 选择颜色
  180. this.setData({
  181. 'touch.isColor': e.currentTarget.dataset.color
  182. })
  183. },
  184. // touch 事件
  185. fontTouchstart(e) {
  186. //1.获取鼠标点击下去的
  187. this.setData({
  188. "touch.clientX": e.touches[0].clientX - this.data.touch.x,
  189. "touch.clientY": e.touches[0].clientY - this.data.touch.y
  190. });
  191. console.log(this.data.touch, "touch")
  192. },
  193. fontTouchmove(e) {
  194. // 鼠标移动的位置
  195. this.setData({
  196. "touch.x": e.changedTouches[0].clientX - this.data.touch.clientX,
  197. "touch.y": e.changedTouches[0].clientY - this.data.touch.clientY
  198. });
  199. // console.log(this.data.touch)
  200. },
  201. fontTouchend(e) {
  202. console.log('移动结束', e)
  203. console.log(this.data.touch)
  204. },
  205. fontDelete() {
  206. // 删除
  207. let touch = {
  208. fontContent: '可定制区域',
  209. isColor: '#ffffff', // 默认是白色
  210. x: 10, // 字体的top值 x坐标
  211. y: 10, // 字体的 left值,y坐标
  212. rotate: 180, // 旋转度数
  213. fontSize: null,
  214. clientX: 0, // 默认初始值
  215. clientY: 0, // 默认初始值
  216. width: 0,
  217. height: 0,
  218. }
  219. this.setData({
  220. touch: touch,
  221. })
  222. },
  223. fontEnlarge(e) { // 放大与缩小
  224. console.log(e.detail)
  225. this.setData({
  226. 'touch.fontSize': e.detail.value
  227. })
  228. },
  229. fontRotate(e) { // 旋转
  230. this.animation.rotate(e.detail.value).step();
  231. this.setData({
  232. animationData: this.animation.export(),
  233. 'touch.angle': e.detail.value
  234. });
  235. },
  236. madeConfirm() { // 设计完成进行确定
  237. let that = this;
  238. if (that.data.touch.fontSize) {
  239. that.getTouchFontInformation(that);
  240. // wx.redirectTo({
  241. // url: '/customized/pages/made/made?id='+ this.data.isActive,
  242. // })
  243. } else {
  244. wx.showToast({
  245. title: '您还没有添加文字进行设计',
  246. icon: 'none'
  247. })
  248. }
  249. },
  250. getTouchFontInformation(that) {
  251. let query = wx.createSelectorQuery();
  252. query.select('#touchFont').boundingClientRect()
  253. query.exec(function (res) {
  254. console.log(res, "获取font的宽高");
  255. that.setData({
  256. 'touch.width': res[0].width,
  257. 'touch.height': res[0].height
  258. });
  259. wx.setStorageSync('madeCupFontData' + that.data.isActive, that.data.touch);
  260. wx.navigateBack()
  261. })
  262. },
  263. })