Skip to content

Instantly share code, notes, and snippets.

@netgfx
Last active October 12, 2023 17:31
Show Gist options
  • Save netgfx/f1c97c845d771e54520147aff2aace2b to your computer and use it in GitHub Desktop.
Save netgfx/f1c97c845d771e54520147aff2aace2b to your computer and use it in GitHub Desktop.
Framer countdown
import * as React from "react"
import {
Frame,
addPropertyControls,
ControlType,
useMotionValue,
useAnimation,
animate,
Color,
} from "framer"
export function Countdown(props) {
const { startTime, animationDuration, ...rest } = props
var h = 0
var m = 0
var s = 0
var hoursInMs = 3600 * h * 1000
var minutesInMs = m * 60 * 1000
var secondsInMS = s * 1000
let r = useMotionValue(0)
const [value, setValue] = React.useState(props.startTime)
let controls
const invisBG = Color({ r: 255, g: 255, b: 255, a: 0 })
function animateText(start, end, duration) {
controls = animate(start, end, {
duration: duration,
ease: "linear",
onUpdate: (v) => {
// turn v into hours and minutes
var rounded = Math.ceil(v)
var time = parseMillisecondsIntoReadableTime(rounded)
setValue(time)
},
onComplete: (v) => {
//console.log(v)
},
})
}
function parseMillisecondsIntoReadableTime(milliseconds) {
//Get hours from milliseconds
var hours = milliseconds / (1000 * 60 * 60)
var absoluteHours = Math.floor(hours)
var h = absoluteHours > 9 ? absoluteHours : "0" + absoluteHours
//Get remainder from hours and convert to minutes
var minutes = (hours - absoluteHours) * 60
var absoluteMinutes = Math.floor(minutes)
var m = absoluteMinutes > 9 ? absoluteMinutes : "0" + absoluteMinutes
//Get remainder from minutes and convert to seconds
var seconds = (minutes - absoluteMinutes) * 60
var absoluteSeconds = Math.floor(seconds)
var s = absoluteSeconds > 9 ? absoluteSeconds : "0" + absoluteSeconds
return h + ":" + m + ":" + s
}
// use like
const startCountdown = () => {
var time = props.startTime.split(":")
h = time[0]
m = time[1]
s = time[2]
hoursInMs = 3600 * h * 1000
minutesInMs = m * 60 * 1000
secondsInMS = s * 1000
r.set(Number(hoursInMs + minutesInMs + secondsInMS))
animateText(r, 0, props.animationDuration)
}
return (
<Frame
{...rest}
onTap={startCountdown}
background={invisBG}
style={{
color: "#fff",
fontSize: 16,
fontWeight: 600,
}}
>
{value}
</Frame>
)
}
Countdown.defaultProps = {
startTime: "00:00:00",
animationDuration: "5",
}
addPropertyControls(Countdown, {
startTime: {
title: "Time",
type: ControlType.String,
placeholder: "00:00:00",
},
animationDuration: {
title: "Animation Duration (in seconds)",
type: ControlType.Number,
defaultValue: 5, // in seconds
},
})
@netgfx
Copy link
Author

netgfx commented Dec 8, 2020

@thenowguy I have updated the component above to support 00:00:00 timer also I have exposed 2 properties in order to pass the initial time from the Framer web right bar and also to control the total duration of the animation (in seconds)

@thenowguy
Copy link

That is so kind of you to do that, thank you. Works perfectly. For anyone else reading this, simply enter "3600" into the Animation field to get HH:mm:ss

Thanks again netgfx

@a-hyoo
Copy link

a-hyoo commented Mar 31, 2021

Thanks for this work! It is a necessary function for me. Thank you for making it.
But I tapped it, but it's not working.
How can I use it? 😿

@netgfx
Copy link
Author

netgfx commented Mar 31, 2021

@hyunjooo it is a custom component for Framer https://framer.com/ just create a new component (not override) delete the auto-generated code and add this instead.

@a-hyoo
Copy link

a-hyoo commented Apr 1, 2021

Oh! I read the comments late.
Thank you for your comments!
I understand now! Thanks again netgfx!

@FabioFarassat
Copy link

Thank you for the code! Does somebody know if you can make the timer start running by its own?

@netgfx
Copy link
Author

netgfx commented Sep 15, 2021

@FabioFarassat you can do

React.useEffect(() => {
        startCountdown();
    }, [])

just before

return (
        <Frame
        ....

and remove the onTap

@FabioFarassat
Copy link

@netgfx thank you for the quick response - works perfectly!

@FadlanNaufal
Copy link

FadlanNaufal commented Aug 12, 2022

@netgfx hi sir, thanks for this component its so help me as designer to develop a website. but how can I make a counter with days? for example
day : hours : minutes : seconds

@netgfx
Copy link
Author

netgfx commented Aug 12, 2022

@FadlanNaufal To add a day it is slightly more complicated because then we would have to also parse the date and calculate the difference with now. As it is now it is only manipulating numbers and slowly reducing them.
I'll see if I can make something but not so sure when 🤷‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment