// pages/calibrator/calibrator.js Component({ /** * 组件的属性列表 */ properties: {}, /** * 组件的初始数据 */ data: { pendingStart: 0, success: false, lineStyle: "", }, /** * 组件的方法列表 */ methods: {}, lifetimes: { attached() { const windowInfo = wx.getWindowInfo(); const vh = windowInfo.windowHeight; wx.startDeviceMotionListening({ interval: "ui" }); this.deviceCb = ({ beta, gamma }) => { if (this.data.success) return; const isGammaPass = (gamma > -45 && gamma < 45) || gamma < -135 || gamma > 135; let top = (((Math.abs(gamma) > 90 ? 180 - Math.abs(beta) : Math.abs(beta)) / 90) * vh) / 2; if (!isGammaPass) { if (top < vh / 2) top = vh / 2 - 100; else top = vh / 2 + 100; } const success = top > vh / 2 - 50 && top < vh / 2 + 50; if (!success) { this.setData({ lineStyle: `top: ${top}px;`, pendingStart: 0, }); } else if (!this.data.pendingStart) { this.setData({ lineStyle: `top: ${top}px;`, pendingStart: Date.now(), }); } else if (Date.now() - this.data.pendingStart > 750) { this.setData({ lineStyle: `top: ${top}px;`, success: true, }); setTimeout(() => { this.triggerEvent("success"); }, 500); } else { this.setData({ lineStyle: `top: ${top}px;`, }); } }; wx.onDeviceMotionChange(this.deviceCb); }, detached() { wx.offDeviceMotionChange(this.deviceCb); wx.stopDeviceMotionListening(); }, }, });