Rolling Number
Odometer where every place rolls at its own speed, new places rising into view, settling exactly on the target value.
Source
components/kuchi/rolling-number.tsx
1"use client";23import { loadFont as loadMono } from "@remotion/google-fonts/JetBrainsMono";4import type React from "react";5import {6 AbsoluteFill,7 Easing,8 interpolate,9 useCurrentFrame,10 useVideoConfig,11} from "remotion";1213const { fontFamily: MONO_FAMILY } = loadMono();1415export interface RollingNumberProps {16 from?: number;17 to?: number;18 fontSize?: number;19 color?: string;20 speed?: number;21}2223const COUNT_PORTION = 0.8;24const COUNT_EASING = Easing.out(Easing.poly(4));25const REVEAL_BAND = 0.4;26const REVEAL_EASING = Easing.inOut(Easing.sin);27const REVEAL_RISE_EM = 0.32;2829function easedProgress(30 frame: number,31 speed: number,32 durationInFrames: number,33): number {34 return interpolate(35 frame * speed,36 [0, durationInFrames * COUNT_PORTION],37 [0, 1],38 {39 extrapolateLeft: "clamp",40 extrapolateRight: "clamp",41 easing: COUNT_EASING,42 },43 );44}4546function placeTravel(start: number, end: number, place: number): number {47 const pow = 10 ** place;48 const up = end >= start;49 const diff = Math.abs(end - start);50 const startDigit = Math.floor(start / pow) % 10;51 const finalDigit = Math.floor(end / pow) % 10;52 const fullTurns = Math.floor(diff / (pow * 10));53 const stepDelta = up54 ? (finalDigit - startDigit + 10) % 1055 : (startDigit - finalDigit + 10) % 10;56 const magnitude = fullTurns * 10 + stepDelta;57 return up ? magnitude : -magnitude;58}5960function placeReveal(current: number, place: number): number {61 if (place === 0) return 1;62 const threshold = 10 ** place;63 return interpolate(current, [threshold * REVEAL_BAND, threshold], [0, 1], {64 extrapolateLeft: "clamp",65 extrapolateRight: "clamp",66 easing: REVEAL_EASING,67 });68}6970function wrap10(value: number): number {71 return ((value % 10) + 10) % 10;72}7374function DigitColumn({75 value,76 fontSize,77 reveal,78}: {79 value: number;80 fontSize: number;81 reveal: number;82}) {83 const cellHeight = fontSize * 1.1;84 return (85 <span86 style={{87 display: "inline-block",88 overflow: "hidden",89 height: cellHeight,90 width: "0.62em",91 verticalAlign: "top",92 textAlign: "center",93 opacity: reveal,94 translate: `0 ${(1 - reveal) * fontSize * REVEAL_RISE_EM}px`,95 }}96 >97 <span98 style={{99 display: "flex",100 flexDirection: "column",101 translate: `0 ${-value * cellHeight}px`,102 }}103 >104 {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].map((d, i) => (105 <span106 key={i}107 style={{ height: cellHeight, lineHeight: `${cellHeight}px` }}108 >109 {d}110 </span>111 ))}112 </span>113 </span>114 );115}116117export function RollingNumber({118 from = 0,119 to = 24813,120 fontSize = 120,121 color = "#171717",122 speed = 1,123}: RollingNumberProps) {124 const frame = useCurrentFrame();125 const { durationInFrames } = useVideoConfig();126127 const start = Math.max(0, Math.round(from));128 const end = Math.max(0, Math.round(to));129130 const e = easedProgress(frame, speed, durationInFrames);131 const current = start + e * (end - start);132133 const maxVal = Math.max(start, end);134 const digits = String(maxVal);135 const cellHeight = fontSize * 1.1;136137 const cells: React.ReactNode[] = [];138 for (let place = 0; place < digits.length; place++) {139 if (place > 0 && place % 3 === 0) {140 cells.unshift(141 <span142 key={`s${place}`}143 style={{144 display: "inline-block",145 width: "0.34em",146 height: cellHeight,147 }}148 />,149 );150 }151 const pow = 10 ** place;152 const startDigit = Math.floor(start / pow) % 10;153 const travel = placeTravel(start, end, place);154 const pos = startDigit + e * travel;155 cells.unshift(156 <DigitColumn157 key={`d${place}`}158 value={wrap10(pos)}159 fontSize={fontSize}160 reveal={placeReveal(current, place)}161 />,162 );163 }164165 return (166 <AbsoluteFill167 style={{168 display: "flex",169 alignItems: "center",170 justifyContent: "center",171 }}172 >173 <div174 style={{175 display: "flex",176 alignItems: "flex-start",177 fontSize,178 fontWeight: 800,179 color,180 fontFamily: MONO_FAMILY,181 fontVariantNumeric: "tabular-nums",182 letterSpacing: "-0.03em",183 lineHeight: 1,184 }}185 >186 {cells}187 </div>188 </AbsoluteFill>189 );190}









