function chillerPanelAnim() { let num = [] let n = [] for (let i = 0; i < 10; i++) { if (i < 8) { num.push(Math.random() * 2) } else if (i === 8) { n.push(Math.random() * 40 + 60) } else { n.push(Math.random() * 31) } } let oldNumValue1 = chillerPanel.a('l1.l.clipPercentage') let oldNumValue2 = chillerPanel.a('l2.l.clipPercentage') let oldNumValue3 = chillerPanel.a('l3.l.clipPercentage') ht.Default.startAnim({ duration: 2000, easing: (t) => { return t }, action: (v, t) => { chillerPanel.a('l1.l.clipPercentage', oldNumValue1 + (num[0] - oldNumValue1) * v) chillerPanel.a('l2.l.clipPercentage', oldNumValue2 + (num[1] - oldNumValue2) * v) chillerPanel.a('l3.l.clipPercentage', oldNumValue3 + (num[2] - oldNumValue3) * v) }, finishFunc: () => { setTimeout(() => { chillerPanelAnim() }, 2000) } }) }
关于动画的方法大家可以理解为将某些属性由起始值逐渐变到目标值的过程,HT 提供了 ht.Default.startAnim,它支持 Frame-Based 和 Time-Based 两种方式的动画,我使用 Time-Based 方式,优点在于只需要指定 duration 的动画周期的毫秒数即可,HT 将在指定的时间周期内完成动画,也就是说帧数或 action 函数被调用次数取决于系统环境,一般来说系统配置更好的机器,更高效的浏览器则调用帧数越多,动画过程更平滑。避免了由于 js 语言无法精确控制 interval 时间间隔,可能会出现动画周期差异较大的问题。这其中还有个 easing 属性可以通过数学公式计算来配置动画的 缓动效果,感兴趣的朋友可以打开来自己试着玩一玩。
// 流动动画let flowTask = {
interval: 10, action: (data) => { if (data.getDisplayName() === 'flow1') { data.s('shape.dash.offset', data.s('shape.dash.offset') + 1) } if (data.getDisplayName() === 'flow2') { data.s('shape.dash.offset', data.s('shape.dash.offset') - 1) } if (data.getDisplayName() === 'flow3') { data.s('shape.dash.offset', data.s('shape.dash.offset') + 5) } } }
dm.addScheduleTask(flowTask)