-
-
Save netgfx/f1c97c845d771e54520147aff2aace2b to your computer and use it in GitHub Desktop.
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 | |
}, | |
}) |
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? 😿
@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.
Oh! I read the comments late.
Thank you for your comments!
I understand now! Thanks again netgfx!
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 🤷♂️
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