oneMadeFont.js 7.4 KB

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