From c8a560e903589236dd0cf0ed65b36302cb0bfb88 Mon Sep 17 00:00:00 2001 From: Billiam Date: Thu, 25 Aug 2022 13:50:12 -0500 Subject: [PATCH] Support looping animations --- src/lib/cli/animate.js | 10 +++++----- src/lib/cli/button.js | 8 ++++++-- src/lib/cli/sleep-screen.js | 12 ++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/lib/cli/animate.js b/src/lib/cli/animate.js index 7cad272..0e847a3 100644 --- a/src/lib/cli/animate.js +++ b/src/lib/cli/animate.js @@ -1,15 +1,15 @@ -export default (duration, fps, callback) => { +export default ({ duration, fps, loop = false, callback }) => { let timeout let startTime const delayTime = 1000 / fps - + const max = loop ? Infinity : 1 const percentCallback = () => { const percent = Math.max( 0, - Math.min(1, (Date.now() - startTime) / duration) + Math.min(max, (Date.now() - startTime) / duration) ) - callback(percent) - if (percent < 1) { + callback(percent % 1) + if (percent < 1 || loop) { run() } } diff --git a/src/lib/cli/button.js b/src/lib/cli/button.js index 35a0c63..e8f9d67 100644 --- a/src/lib/cli/button.js +++ b/src/lib/cli/button.js @@ -103,8 +103,12 @@ export default class CliButton { holdPercent.value = 0 } if (holding) { - holdAnimation = animation(400, 60, (percent) => { - holdPercent.value = percent + holdAnimation = animation({ + duration: 400, + fps: 60, + callback: (percent) => { + holdPercent.value = percent + }, }) holdAnimation.delay(100) } diff --git a/src/lib/cli/sleep-screen.js b/src/lib/cli/sleep-screen.js index bd80501..7831043 100644 --- a/src/lib/cli/sleep-screen.js +++ b/src/lib/cli/sleep-screen.js @@ -13,15 +13,15 @@ export const SleepScreen = (timeout, streamdeck) => { fadeoutAnimation?.cancel() fadeoutAnimation = null if (asleep) { - fadeoutAnimation = animation( - 800 * ui.displayBrightness * 0.01, - 30, - (percent) => { + fadeoutAnimation = animation({ + duration: 800 * ui.displayBrightness * 0.01, + fps: 30, + callback: (percent) => { streamdeck.setBrightness( Math.floor(ui.displayBrightness * (1 - percent)) ) - } - ) + }, + }) fadeoutAnimation.start() } else { streamdeck.setBrightness(ui.displayBrightness)