boxMadeFont.js 7.5 KB

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