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
},
})
@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