// pages/customized/made/madeFont/madeFont.js import * as util from '../../../../utils/util.js' Page({ /** * 页面的初始数据 */ data: { dataPositionObj: {}, propSize: {}, isActive: '5', // 默认是正面 obverseImgObject: {}, // radarImg:null, fontColorArray: ['#ffffff', '#000000', '#e70012', '#ff8500', '#fff100', '#22ad38', '#00b7f0', '#920784'], fontFamilyArray: ['Nomal', 'Monospace', 'Sans', 'Serif'], // 自带字体样式 addFontStatic: false, // 添加文字 inputFont: null, fontTouch: { isActive: '1', // 表示正面 fontContent: '可定制区域', isColor: '#ffffff', // 默认是白色 x: 10, // 字体的top值 x坐标 y: 10, // 字体的 left值,y坐标 angle: 0, // 旋转度数 fontSize: null, clientX: 0, // 默认初始值 clientY: 0, // 默认初始值 width: 0, height: 0, bold:false, }, imgFont:{}, // 图片对象 animationData: {}, //旋转动画 imgAnimationData: {}, // 图片的旋转 imgTouch:{}, box:{}, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { let that = this; that.animation = wx.createAnimation({ timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end" }) that.imgAnimation = wx.createAnimation({ timingFunction: 'step-start', // "linear","ease","ease-in","ease-in-out","ease-out","step-start","step-end" }) if ('id' in options) { let cupNum = util.changeCup(options.id); let madeTouchFont = wx.getStorageSync('madeCupFontData' + options.id); if (madeTouchFont) { that.animation.rotate(madeTouchFont.angle).step(); that.setData({ fontTouch: madeTouchFont, animationData: that.animation.export(), isActive: options.id, dataPositionObj: cupNum.dataPositionObj, propSize: cupNum.propSize }) } else { that.setData({ 'fontTouch.isActive': options.id, isActive: options.id, dataPositionObj: cupNum.dataPositionObj, propSize: cupNum.propSize }) } } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { let that = this; that.changeCupImg('1'); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.setData({ box: wx.getStorageSync('box'), obverseImgObject: util.changeImg }) this.getDataTouch(this) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { }, changeCupImg(index) { let canvasBg = this.data.obverseImgObject[index], that = this; // wx.getSystemInfo({ // success: function (res) { // let rpx = res.windowWidth / 750; // const ctx = wx.createCanvasContext('madeContent'); // ctx.drawImage(canvasBg, 0, 0, 750 * rpx, 588 * rpx) // ctx.draw(); // setTimeout(()=>{ // that.handleCanvarToImg(that) // },1000) // }, // }) }, addFont() { // 添加文字 let that = this; that.setData({ addFontStatic: true }) }, fontCancel() { this.setData({ addFontStatic: false }) }, bindinput(e) { this.setData({ inputFont: e.detail.value, }) }, fontConfirm(e) { let that = this; if (that.data.inputFont) { this.animation.rotate(0).step() that.setData({ 'fontTouch.fontContent': that.data.inputFont, 'fontTouch.fontSize': 30, 'fontTouch.angle': 0, addFontStatic: false, animationData: this.animation.export() }) // that.getTouchFontInformation(that) that.setData({ inputFont: null, }) } }, handleCanvarToImg(that) { wx.canvasToTempFilePath({ x: 0, y: 0, width: 750, height: 588, canvasId: 'madeContent', success: function (res) { that.setData({ radarImg: res.tempFilePath, }) } }); }, selectColor(e) { // 选择颜色 this.setData({ 'fontTouch.isColor': e.currentTarget.dataset.color }) }, // touch 事件 fontTouchstart(e) { //1.获取鼠标点击下去的 this.setData({ "fontTouch.clientX": e.touches[0].clientX - this.data.fontTouch.x, "fontTouch.clientY": e.touches[0].clientY - this.data.fontTouch.y }); console.log(this.data.fontTouch, "touch") }, fontTouchmove(e) { // 鼠标移动的位置 this.setData({ "fontTouch.x": e.changedTouches[0].clientX - this.data.fontTouch.clientX, "fontTouch.y": e.changedTouches[0].clientY - this.data.fontTouch.clientY }); // console.log(this.data.touch) }, fontTouchend(e) { console.log('移动结束', e) console.log(this.data.fontTouch) }, fontDelete() { // 删除 let touch = { fontContent: '可定制区域', isColor: '#ffffff', // 默认是白色 x: 10, // 字体的top值 x坐标 y: 10, // 字体的 left值,y坐标 rotate: 180, // 旋转度数 fontSize: null, clientX: 0, // 默认初始值 clientY: 0, // 默认初始值 width: 0, height: 0, } this.setData({ fontTouch: touch, }) }, fontEnlarge(e) { // 放大与缩小 console.log(e.detail) this.setData({ 'fontTouch.fontSize': e.detail.value }) }, fontRotate(e) { // 旋转 this.animation.rotate(e.detail.value).step(); this.setData({ animationData: this.animation.export(), 'fontTouch.angle': e.detail.value }); }, fontWeight(e) { this.setData({ 'fontTouch.bold': e.detail.value }) }, madeConfirm() { // 设计完成进行确定 let that = this; if (that.data.fontTouch.fontSize) { that.getTouchFontInformation(that); // wx.redirectTo({ // url: '/customized/pages/made/made?id='+ this.data.isActive, // }) } else { wx.showToast({ title: '您还没有添加文字进行设计', icon: 'none' }) } }, getTouchFontInformation(that) { let query = wx.createSelectorQuery(); query.select('#touchFont').boundingClientRect() query.exec(function (res) { console.log(res, "获取font的宽高"); that.setData({ 'fontTouch.width': res[0].width, 'fontTouch.height': res[0].height }); wx.setStorageSync('madeCupFontData' + that.data.isActive, that.data.fontTouch); wx.navigateBack() }) }, getDataTouch(that) { let madeTouchImg = wx.getStorageSync('madeCupImgData' + that.data.isActive) if (madeTouchImg) { that.imgAnimation.rotate(madeTouchImg.angle).step(); that.setData({ imgAnimationData: that.imgAnimation.export(), imgTouch: madeTouchImg ? madeTouchImg : {}, //存储图片数据 }) } }, weightBtn(){ let that = this; that.setData({ 'fontTouch.bold':!that.data.fontTouch.bold }) } })