madeFont.js 7.7 KB

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