Number Wheel
Mechanical odometer that rolls an integer up or down to its target, direction inferred from the values.
Source
components/kuchi/number-wheel.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 spring,10 useCurrentFrame,11 useVideoConfig,12} from "remotion";1314const { fontFamily: MONO_FAMILY } = loadMono();1516export interface NumberWheelProps {17 from?: number;18 to?: number;19 fontSize?: number;20 color?: string;21 speed?: number;22}2324export interface OdometerProps {25 current: number;26 fontSize: number;27 color: string;28}2930const WHEEL_ROLL_START = 0.9;31const SPRING_FRAMES = 30;32const SPRING_CONFIG = { damping: 200, stiffness: 100, mass: 1 } as const;33const COUNT_PORTION = 0.8;3435function rollFraction(current: number, place: number): number {36 const v = current / 10 ** place;37 const frac = v - Math.floor(v);38 if (frac <= WHEEL_ROLL_START) return 0;39 return (frac - WHEEL_ROLL_START) / (1 - WHEEL_ROLL_START);40}4142function computeWheel(43 current: number,44 place: number,45 springEased: (t: number) => number,46): number {47 if (current <= 0) return 0;48 if (place === 0) return current % 10;4950 const digit = Math.floor(current / 10 ** place) % 10;51 const t = rollFraction(current, place);52 if (t <= 0) return digit;53 return digit + springEased(t);54}5556function placeOpacity(current: number, place: number): number {57 if (place === 0) return 1;58 const threshold = 10 ** place;59 return interpolate(current, [threshold * 0.9, threshold], [0, 1], {60 extrapolateLeft: "clamp",61 extrapolateRight: "clamp",62 });63}6465function DigitColumn({66 value,67 fontSize,68 opacity,69}: {70 value: number;71 fontSize: number;72 opacity: number;73}) {74 const cellHeight = fontSize * 1.1;75 return (76 <span77 style={{78 display: "inline-block",79 overflow: "hidden",80 height: cellHeight,81 width: "0.62em",82 verticalAlign: "top",83 textAlign: "center",84 opacity,85 }}86 >87 <span88 style={{89 display: "flex",90 flexDirection: "column",91 translate: `0 ${-value * cellHeight}px`,92 }}93 >94 {[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0].map((d, i) => (95 <span96 key={i}97 style={{ height: cellHeight, lineHeight: `${cellHeight}px` }}98 >99 {d}100 </span>101 ))}102 </span>103 </span>104 );105}106107function OdometerColumns({108 current,109 templateValue,110 fontSize,111 color,112 fps,113}: {114 current: number;115 templateValue: number;116 fontSize: number;117 color: string;118 fps: number;119}) {120 const springAtEnd = spring({121 frame: SPRING_FRAMES,122 fps,123 config: SPRING_CONFIG,124 });125 const springEased = (t: number) =>126 spring({ frame: t * SPRING_FRAMES, fps, config: SPRING_CONFIG }) /127 springAtEnd;128129 const template = templateValue.toLocaleString("en-US");130131 const cells: React.ReactNode[] = [];132 let place = 0;133 for (let i = template.length - 1; i >= 0; i--) {134 const ch = template[i];135 if (ch === ",") {136 cells.unshift(137 <span138 key={`c${i}`}139 style={{140 display: "inline-block",141 width: "0.32em",142 color: "currentColor",143 fontSize: "0.85em",144 textAlign: "center",145 opacity: placeOpacity(current, place),146 }}147 >148 ,149 </span>,150 );151 } else {152 const wheel = computeWheel(current, place, springEased);153 cells.unshift(154 <DigitColumn155 key={`d${i}`}156 value={wheel}157 fontSize={fontSize}158 opacity={placeOpacity(current, place)}159 />,160 );161 place++;162 }163 }164165 return (166 <div167 style={{168 display: "flex",169 alignItems: "baseline",170 fontSize,171 fontWeight: 800,172 color,173 fontFamily: MONO_FAMILY,174 fontVariantNumeric: "tabular-nums",175 letterSpacing: "-0.03em",176 lineHeight: 1,177 }}178 >179 {cells}180 </div>181 );182}183184export function Odometer({ current, fontSize, color }: OdometerProps) {185 const { fps } = useVideoConfig();186 const value = Math.max(0, Math.round(current));187 return (188 <OdometerColumns189 current={current}190 templateValue={value}191 fontSize={fontSize}192 color={color}193 fps={fps}194 />195 );196}197198export function NumberWheel({199 from = 0,200 to = 24813,201 fontSize = 120,202 color = "#171717",203 speed = 1,204}: NumberWheelProps) {205 const frame = useCurrentFrame();206 const { durationInFrames, fps } = useVideoConfig();207208 const start = Math.max(0, Math.round(from));209 const end = Math.max(0, Math.round(to));210211 const progress = interpolate(212 frame * speed,213 [0, durationInFrames * COUNT_PORTION],214 [0, 1],215 {216 extrapolateLeft: "clamp",217 extrapolateRight: "clamp",218 easing: Easing.bezier(0.5, 1, 0.5, 1),219 },220 );221 const current = Math.max(0, start + progress * (end - start));222 const maxVal = Math.max(start, end);223224 return (225 <AbsoluteFill226 style={{227 display: "flex",228 alignItems: "center",229 justifyContent: "center",230 }}231 >232 <OdometerColumns233 current={current}234 templateValue={maxVal}235 fontSize={fontSize}236 color={color}237 fps={fps}238 />239 </AbsoluteFill>240 );241}









