Last active
October 12, 2023 17:31
-
-
Save netgfx/f1c97c845d771e54520147aff2aace2b to your computer and use it in GitHub Desktop.
Framer countdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}, | |
}) |
Thank you for the code! Does somebody know if you can make the timer start running by its own?
@FabioFarassat you can do
React.useEffect(() => {
startCountdown();
}, [])
just before
return (
<Frame
....
and remove the onTap
@netgfx thank you for the quick response - works perfectly!
@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
@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
Oh! I read the comments late.
Thank you for your comments!
I understand now! Thanks again netgfx!