Files
wx_pbzc/components/progress-ring/progress-ring.js
T
lc 0464eeaba6 feat(timer): 训练页改版 hero 风格 + 进度环刻度 + 醒目姿态提示气泡
- progress-ring: canvas 新增秒刻度层(四分位加粗), 环内加 subText, 移除组件内 statusText
- timer.wxml: 环下方状态词 + 目标信息卡(今日目标·第N天›静态箭头) + 运行提示; 进度环传 ticks/subText
- timer.js: 派生 ringTicks/ringSubText/statusText/goalLine/runningTip/tipType/tipIcon, 各生命周期计算下发
- timer.wxss: 按钮贴底(flex-start+margin-top:auto), 新增 .timer-status/.goal-card/.timer-tip 气泡卡片(主色左边框+小三角+tipPulse 呼吸), 文字放大加粗
- 姿态提示分语义: posture/phase(主色) / done(成功色), 图标用 peopleFill/successFill
- 状态机/计时/循环FSM/呼吸环/彩带/弹窗/语音震动常亮全部保留
- 另含 index 进度地图右上角奖杯图标删除(quest-trophy 样式清理)
2026-08-11 15:38:37 +08:00

173 lines
6.2 KiB
JavaScript

const util = require('../../utils/util')
Component({
properties: {
duration: { type: Number, value: 60 },
remaining: { type: Number, value: 60 },
size: { type: Number, value: 280 },
ringWidth: { type: Number, value: 14 },
status: { type: String, value: 'idle' },
primaryColor: { type: String, value: '#FF6B35' },
primaryLightColor: { type: String, value: '#FF8C5A' },
// Track (background ring) color. Caller passes a darker shade in dark mode
// so the ring isn't an eye-searing light circle on a dark background.
trackColor: { type: String, value: '#EEEEEE' },
// Total elapsed seconds (since start). Shown in the ring center once the
// countdown hits 0 (overtime mode) - replaces the old +Xs badge.
elapsed: { type: Number, value: 0 },
// Tick count. Caller passes the duration (one tick per second, capped at
// 60 to avoid clutter). 0 = no ticks drawn. Quarter ticks are emphasized.
ticks: { type: Number, value: 0 },
// Small secondary label under the big time (e.g. "目标 30 秒"). Empty = hidden.
subText: { type: String, value: '' }
},
data: {
displayTime: '00:00'
},
observers: {
'remaining, status, duration, elapsed'() {
// 倒计时阶段显示剩余;达标后 remaining 归零,改显总训练时长(overtime 模式)
const shown = this.data.remaining > 0 ? this.data.remaining : (this.data.elapsed || 0)
this.setData({
displayTime: util.formatTime(shown)
})
},
'remaining, duration, size, ringWidth, primaryColor, primaryLightColor, trackColor, ticks'() {
this._draw()
}
},
lifetimes: {
ready() {
const query = this.createSelectorQuery()
query.select('#ringCanvas')
.fields({ node: true, size: true })
.exec((res) => {
if (!res || !res[0] || !res[0].node) return
const canvas = res[0].node
const dpr = wx.getWindowInfo().pixelRatio || 2
const displaySize = this.data.size
canvas.width = displaySize * dpr
canvas.height = displaySize * dpr
this._ctx = canvas.getContext('2d')
this._dpr = dpr
this._draw()
})
}
},
methods: {
// '#FF6B35' -> [255, 107, 53]
_hexToRgb(hex) {
const m = /^#?([0-9a-f]{6})$/i.exec(hex || '')
if (!m) return [255, 107, 53]
const n = parseInt(m[1], 16)
return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
},
_draw() {
const ctx = this._ctx
if (!ctx) return
const { size, ringWidth, duration, remaining, primaryColor, primaryLightColor, trackColor, ticks } = this.data
const dpr = this._dpr
const w = size * dpr
const h = size * dpr
ctx.clearRect(0, 0, w, h)
const cx = w / 2
const cy = h / 2
// reserve room for the glowing end dot so its shadow isn't clipped
const glowPad = 6 * dpr
const radius = (size - ringWidth) / 2 * dpr - glowPad
const lw = ringWidth * dpr
// track ring
ctx.beginPath()
ctx.arc(cx, cy, radius, 0, Math.PI * 2)
ctx.strokeStyle = trackColor
ctx.lineWidth = lw
ctx.lineCap = 'round'
ctx.stroke()
// ---- tick marks (inside the band) ----
if (ticks > 0) {
const innerR = radius - lw / 2 - (4 * dpr) // just inside the band
const q = Math.max(1, Math.round(ticks / 4)) // quarter emphasis step
const shortLen = 6 * dpr
const longLen = 12 * dpr
for (let i = 0; i < ticks; i++) {
const ang = -Math.PI / 2 + (i / ticks) * Math.PI * 2
const isLong = (i % q === 0)
const len = isLong ? longLen : shortLen
const cos = Math.cos(ang)
const sin = Math.sin(ang)
ctx.beginPath()
ctx.moveTo(cx + innerR * cos, cy + innerR * sin)
ctx.lineTo(cx + (innerR - len) * cos, cy + (innerR - len) * sin)
ctx.lineWidth = (isLong ? 3 : 1.5) * dpr
ctx.lineCap = 'round'
ctx.strokeStyle = isLong ? 'rgba(0,0,0,0.28)' : 'rgba(0,0,0,0.12)'
ctx.stroke()
}
}
// progress arc (clockwise from 12 o'clock)
const ratio = duration > 0 ? Math.max(0, Math.min(1, remaining / duration)) : 0
if (ratio > 0.001) {
const startAngle = -Math.PI / 2
const sweep = ratio * Math.PI * 2
const [r1, g1, b1] = this._hexToRgb(primaryColor)
const [r2, g2, b2] = this._hexToRgb(primaryLightColor || primaryColor)
// Conic gradient along the arc (light -> primary), drawn as small
// arc segments. Segment count is bounded so per-frame redraws stay cheap.
const segs = Math.max(16, Math.min(120, Math.ceil(sweep / 0.06)))
for (let i = 0; i < segs; i++) {
const a0 = startAngle + (sweep * i) / segs
const a1 = startAngle + (sweep * (i + 1)) / segs + 0.004 // tiny overlap to hide seams
const t = 1 - i / (segs - 1) // head (i=0, oldest remaining) is primary, tail is light
const r = Math.round(r1 + (r2 - r1) * t)
const g = Math.round(g1 + (g2 - g1) * t)
const b = Math.round(b1 + (b2 - b1) * t)
ctx.beginPath()
ctx.arc(cx, cy, radius, a0, a1)
ctx.strokeStyle = `rgb(${r},${g},${b})`
ctx.lineWidth = lw
ctx.lineCap = 'butt'
ctx.stroke()
}
// round cap at the tail (12 o'clock side)
ctx.beginPath()
ctx.arc(cx + radius * Math.cos(startAngle), cy + radius * Math.sin(startAngle), lw / 2, 0, Math.PI * 2)
ctx.fillStyle = `rgb(${r2},${g2},${b2})`
ctx.fill()
// glowing dot at the leading end
const endAngle = startAngle + sweep
const ex = cx + radius * Math.cos(endAngle)
const ey = cy + radius * Math.sin(endAngle)
ctx.save()
ctx.shadowColor = primaryColor
ctx.shadowBlur = 12 * dpr
ctx.beginPath()
ctx.arc(ex, ey, lw / 2, 0, Math.PI * 2)
ctx.fillStyle = primaryColor
ctx.fill()
// white core for a "light bulb" feel
ctx.shadowBlur = 0
ctx.beginPath()
ctx.arc(ex, ey, lw / 4, 0, Math.PI * 2)
ctx.fillStyle = 'rgba(255,255,255,0.9)'
ctx.fill()
ctx.restore()
}
}
}
})