Skip to content

Commit

Permalink
Support looping animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Billiam committed Aug 25, 2022
1 parent 1bf77e7 commit c8a560e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/lib/cli/animate.js
Original file line number Diff line number Diff line change
@@ -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()
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/lib/cli/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib/cli/sleep-screen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c8a560e

Please sign in to comment.