|
|
@@ -44,12 +44,13 @@
|
|
|
<text class="tp-col-icon">🧭</text>
|
|
|
<text class="tp-col-label">今日吉位</text>
|
|
|
<view class="tp-compass-wrap">
|
|
|
- <canvas
|
|
|
- canvas-id="tianpanCompass"
|
|
|
- id="tianpanCompass"
|
|
|
- type="2d"
|
|
|
- class="tp-compass-canvas"
|
|
|
- ></canvas>
|
|
|
+ <canvas
|
|
|
+ canvas-id="tianpanCompass"
|
|
|
+ id="tianpanCompass"
|
|
|
+ type="2d"
|
|
|
+ class="tp-compass-canvas"
|
|
|
+ @touchstart="handleTouchStart"
|
|
|
+ ></canvas>
|
|
|
</view>
|
|
|
<text class="tp-direction-value">{{ luckyDirection }}</text>
|
|
|
</view>
|
|
|
@@ -123,6 +124,11 @@ export default {
|
|
|
return {
|
|
|
canvasCtx: null,
|
|
|
canvasReady: false,
|
|
|
+ _compassW: 0,
|
|
|
+ _compassH: 0,
|
|
|
+ themeColor: '#FF6B9D', // 默认火维度颜色
|
|
|
+ _animFrameId: null,
|
|
|
+ _clickableAreas: [],
|
|
|
// 五行 → 方位映射
|
|
|
elementDirMap: {
|
|
|
wood: '东',
|
|
|
@@ -256,25 +262,89 @@ export default {
|
|
|
var self = this
|
|
|
var query = uni.createSelectorQuery().in(self)
|
|
|
query.select('#tianpanCompass')
|
|
|
- .fields({ node: true, size: true })
|
|
|
- .exec(function(res) {
|
|
|
- if (!res || !res[0] || !res[0].node) return
|
|
|
- var ctx = res[0].node.getContext('2d')
|
|
|
- var dpr = uni.getSystemInfoSync().pixelRatio || 2
|
|
|
- var w = res[0].width
|
|
|
- var h = res[0].height
|
|
|
- res[0].node.width = w * dpr
|
|
|
- res[0].node.height = h * dpr
|
|
|
- ctx.scale(dpr, dpr)
|
|
|
- self.canvasCtx = ctx
|
|
|
- self.canvasReady = true
|
|
|
- self._compassW = w
|
|
|
- self._compassH = h
|
|
|
- self.drawCompass()
|
|
|
- })
|
|
|
+ .fields({ node: true, size: true })
|
|
|
+ .exec(function(res) {
|
|
|
+ if (!res || !res[0] || !res[0].node) return
|
|
|
+ var ctx = res[0].node.getContext('2d')
|
|
|
+ var dpr = uni.getSystemInfoSync().pixelRatio || 2
|
|
|
+ var w = res[0].width
|
|
|
+ var h = res[0].height
|
|
|
+
|
|
|
+ // 高分辨率适配
|
|
|
+ res[0].node.width = w * dpr
|
|
|
+ res[0].node.height = h * dpr
|
|
|
+ ctx.scale(dpr, dpr)
|
|
|
+
|
|
|
+ self.canvasCtx = ctx
|
|
|
+ self.canvasReady = true
|
|
|
+ self._compassW = w
|
|
|
+ self._compassH = h
|
|
|
+
|
|
|
+ // 设置初始主题色(火维度)
|
|
|
+ self.themeColor = '#FF6B9D'
|
|
|
+ if (self.fortune && self.fortune.dominantElement) {
|
|
|
+ var elementColors = {
|
|
|
+ 木: '#10B981', 火: '#FF6B9D', 土: '#FF8C42', 金: '#6366F1', 水: '#3B82F6'
|
|
|
+ }
|
|
|
+ self.themeColor = elementColors[self.fortune.dominantElement]
|
|
|
+ }
|
|
|
+
|
|
|
+ // 启动动画
|
|
|
+ self.startAnimation()
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ handleTouchStart: function(e) {
|
|
|
+ // 儿童模式交互
|
|
|
+ if (this.$store.state.role === 'child') {
|
|
|
+ this.handleCompassClick(e)
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ clearCanvas: function() {
|
|
|
+ if (!this.canvasReady || !this.canvasCtx) return
|
|
|
+ var ctx = this.canvasCtx
|
|
|
+ var width = this._compassW
|
|
|
+ var height = this._compassH
|
|
|
+ ctx.clearRect(0, 0, width, height)
|
|
|
+ },
|
|
|
+
|
|
|
+ startAnimation: function() {
|
|
|
+ var self = this
|
|
|
+ if (this._animFrameId) {
|
|
|
+ cancelAnimationFrame(this._animFrameId)
|
|
|
+ }
|
|
|
+
|
|
|
+ var animFrameId = null
|
|
|
+ var lastTime = 0
|
|
|
+
|
|
|
+ function animationLoop(timestamp) {
|
|
|
+ if (!self.canvasCtx || !self.canvasReady) {
|
|
|
+ cancelAnimationFrame(animFrameId)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 控制帧率
|
|
|
+ if (timestamp - lastTime < 16) {
|
|
|
+ animFrameId = requestAnimationFrame(animationLoop)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ lastTime = timestamp
|
|
|
+
|
|
|
+ self.clearCanvas()
|
|
|
+ self.drawCompassWithAnimation(timestamp)
|
|
|
+ animFrameId = requestAnimationFrame(animationLoop)
|
|
|
+ }
|
|
|
+
|
|
|
+ this._animFrameId = requestAnimationFrame(animationLoop)
|
|
|
},
|
|
|
|
|
|
drawCompass: function() {
|
|
|
+ // 兼容旧调用
|
|
|
+ this.drawCompassWithAnimation(0)
|
|
|
+ },
|
|
|
+
|
|
|
+ drawCompassWithAnimation: function(timestamp) {
|
|
|
var ctx = this.canvasCtx
|
|
|
if (!ctx) return
|
|
|
|
|
|
@@ -363,7 +433,7 @@ export default {
|
|
|
ctx.lineWidth = 1.5
|
|
|
ctx.stroke()
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
}
|
|
|
</script>
|
|
|
|