madeFont.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. '2': '/customized/images/customized/cup_black_side.png',/*侧面面 */
  24. '3': '/customized/images/customized/cup_black_handle.png',/*手柄 */
  25. '4': '/customized/images/customized/cup_black_bottom.png',/*底部 */
  26. },
  27. // radarImg:null,
  28. fontColorArray: ['#ffffff','#000000', '#e70012', '#ff8500', '#fff100', '#22ad38', '#00b7f0', '#920784'],
  29. fontFamilyArray: ['Nomal','Monospace','Sans','Serif'],// 自带字体样式
  30. addFontStatic: false, // 添加文字
  31. inputFont:null,
  32. touch:{
  33. isActive:'1', // 表示正面
  34. fontContent: '可定制区域',
  35. isColor: '#ffffff', // 默认是白色
  36. x:10, // 字体的top值 x坐标
  37. y:10, // 字体的 left值,y坐标
  38. angle:0, // 旋转度数
  39. fontSize:null,
  40. clientX:0, // 默认初始值
  41. clientY:0, // 默认初始值
  42. width:0,
  43. height:0,
  44. },
  45. animationData:{},//旋转动画
  46. },
  47. /**
  48. * 生命周期函数--监听页面加载
  49. */
  50. onLoad: function (options) {
  51. let that = this;
  52. that.animation = wx.createAnimation({
  53. timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end"
  54. })
  55. if('id' in options){
  56. let cupNum = util.changeCup(options.id);
  57. let madeTouchFont = wx.getStorageSync('madeCupFontData' + options.id);
  58. if (madeTouchFont){
  59. that.animation.rotate(madeTouchFont.angle).step();
  60. that.setData({
  61. touch: madeTouchFont,
  62. animationData: that.animation.export(),
  63. isActive: options.id,
  64. dataPositionObj: cupNum.dataPositionObj,
  65. propSize: cupNum.propSize
  66. })
  67. }else {
  68. that.setData({
  69. 'touch.isActive': options.id,
  70. isActive: options.id,
  71. dataPositionObj: cupNum.dataPositionObj,
  72. propSize: cupNum.propSize
  73. })
  74. }
  75. }
  76. },
  77. /**
  78. * 生命周期函数--监听页面初次渲染完成
  79. */
  80. onReady: function () {
  81. let that = this;
  82. that.changeCupImg('1');
  83. },
  84. /**
  85. * 生命周期函数--监听页面显示
  86. */
  87. onShow: function () {
  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],that = this;
  116. // wx.getSystemInfo({
  117. // success: function (res) {
  118. // let rpx = res.windowWidth / 750;
  119. // const ctx = wx.createCanvasContext('madeContent');
  120. // ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx)
  121. // ctx.draw();
  122. // setTimeout(()=>{
  123. // that.handleCanvarToImg(that)
  124. // },1000)
  125. // },
  126. // })
  127. },
  128. addFont(){ // 添加文字
  129. let that = this;
  130. that.setData({
  131. addFontStatic: true
  132. })
  133. },
  134. fontCancel(){
  135. this.setData({
  136. addFontStatic: false
  137. })
  138. },
  139. bindinput(e){
  140. this.setData({
  141. inputFont: e.detail.value,
  142. })
  143. },
  144. fontConfirm(e){
  145. let that = this;
  146. if (that.data.inputFont){
  147. this.animation.rotate(0).step()
  148. that.setData({
  149. 'touch.fontContent': that.data.inputFont,
  150. 'touch.fontSize':100,
  151. 'touch.angle': 0,
  152. addFontStatic: false,
  153. animationData: this.animation.export()
  154. })
  155. that.setData({
  156. inputFont: null,
  157. })
  158. }
  159. },
  160. handleCanvarToImg(that){
  161. wx.canvasToTempFilePath({
  162. x: 0,
  163. y: 0,
  164. width: 750,
  165. height: 588,
  166. canvasId: 'madeContent',
  167. success: function (res) {
  168. that.setData({
  169. radarImg: res.tempFilePath,
  170. })
  171. }
  172. });
  173. },
  174. selectColor(e){ // 选择颜色
  175. this.setData({
  176. 'touch.isColor': e.currentTarget.dataset.color
  177. })
  178. },
  179. // touch 事件
  180. fontTouchstart(e){
  181. //1.获取鼠标点击下去的
  182. this.setData({
  183. "touch.clientX": e.touches[0].clientX - this.data.touch.x,
  184. "touch.clientY": e.touches[0].clientY - this.data.touch.y
  185. });
  186. console.log(this.data.touch,"touch")
  187. },
  188. fontTouchmove(e){
  189. // 鼠标移动的位置
  190. this.setData({
  191. "touch.x": e.changedTouches[0].clientX - this.data.touch.clientX,
  192. "touch.y": e.changedTouches[0].clientY - this.data.touch.clientY
  193. });
  194. // console.log(this.data.touch)
  195. },
  196. fontTouchend(e){
  197. console.log('移动结束',e)
  198. console.log(this.data.touch)
  199. },
  200. fontDelete(){
  201. // 删除
  202. let touch= {
  203. fontContent: '可定制区域',
  204. isColor: '#ffffff', // 默认是白色
  205. x: 10, // 字体的top值 x坐标
  206. y: 10, // 字体的 left值,y坐标
  207. rotate: 180, // 旋转度数
  208. fontSize: null,
  209. clientX: 0, // 默认初始值
  210. clientY: 0, // 默认初始值
  211. width:0,
  212. height:0,
  213. }
  214. this.setData({
  215. touch: touch,
  216. })
  217. },
  218. fontEnlarge(e){ // 放大与缩小
  219. console.log(e.detail)
  220. this.setData({
  221. 'touch.fontSize':e.detail.value
  222. })
  223. },
  224. fontRotate(e){ // 旋转
  225. this.animation.rotate(e.detail.value).step();
  226. this.setData({
  227. animationData:this.animation.export(),
  228. 'touch.angle': e.detail.value
  229. });
  230. },
  231. madeConfirm(){ // 设计完成进行确定
  232. let that = this;
  233. if(that.data.touch.fontSize){
  234. that.getTouchFontInformation(that);
  235. // wx.redirectTo({
  236. // url: '/customized/pages/made/made?id='+ this.data.isActive,
  237. // })
  238. } else {
  239. wx.showToast({
  240. title: '您还没有添加文字进行设计',
  241. icon: 'none'
  242. })
  243. }
  244. },
  245. getTouchFontInformation(that){
  246. let query = wx.createSelectorQuery();
  247. query.select('#touchFont').boundingClientRect()
  248. query.exec(function (res) {
  249. console.log(res,"获取font的宽高");
  250. that.setData({
  251. 'touch.width':res[0].width,
  252. 'touch.height':res[0].height
  253. });
  254. wx.setStorageSync('madeCupFontData' + that.data.isActive, that.data.touch);
  255. wx.navigateBack()
  256. })
  257. },
  258. })