A world-map component from Aceternity UI.
From Aceternity UI
1"use client";
2
3import { useRef } from "react";
4import { motion } from "motion/react";
5import DottedMap from "dotted-map";
6
7import { useTheme } from "next-themes";
8
9interface MapProps {
10 dots?: Array<{
11 start: { lat: number; lng: number; label?: string };
12 end: { lat: number; lng: number; label?: string };
13 }>;
14 lineColor?: string;
15}
16
17export default function WorldMap({
18 dots = [],
19 lineColor = "#0ea5e9",
20}: MapProps) {
21 const svgRef = useRef<SVGSVGElement>(null);
22 const map = new DottedMap({ height: 100, grid: "diagonal" });
23
24 const { theme } = useTheme();
25
26 const svgMap = map.getSVG({
27 radius: 0.22,
28 color: theme === "dark" ? "#FFFFFF40" : "#00000040",
29 shape: "circle",
30 backgroundColor: theme === "dark" ? "black" : "white",
31 });
32
33 const projectPoint = (lat: number, lng: number) => {
34 const x = (lng + 180) * (800 / 360);
35 const y = (90 - lat) * (400 / 180);
36 return { x, y };
37 };
38
39 const createCurvedPath = (
40 start: { x: number; y: number },
41 end: { x: number; y: number }
42 ) => {
43 const midX = (start.x + end.x) / 2;
44 const midY = Math.min(start.y, end.y) - 50;
45 return `M ${start.x} ${start.y} Q ${midX} ${midY} ${end.x} ${end.y}`;
46 };
47
48 return (
49 <div className="w-full aspect-[2/1] dark:bg-black bg-white rounded-lg relative font-sans">
50 <img
51 src={`data:image/svg+xml;utf8,${encodeURIComponent(svgMap)}`}
52 className="h-full w-full [mask-image:linear-gradient(to_bottom,transparent,white_10%,white_90%,transparent)] pointer-events-none select-none"
53 alt="world map"
54 height="495"
55 width="1056"
56 draggable={false}
57 />
58 <svg
59 ref={svgRef}
60 viewBox="0 0 800 400"
61 className="w-full h-full absolute inset-0 pointer-events-none select-none"
62 >
63 {dots.map((dot, i) => {
64 const startPoint = projectPoint(dot.start.lat, dot.start.lng);
65 const endPoint = projectPoint(dot.end.lat, dot.end.lng);
66 return (
67 <g key={`path-group-${i}`}>
68 <motion.path
69 d={createCurvedPath(startPoint, endPoint)}
70 fill="none"
71 stroke="url(#path-gradient)"
72 strokeWidth="1"
73 initial={{
74 pathLength: 0,
75 }}
76 animate={{
77 pathLength: 1,
78 }}
79 transition={{
80 duration: 1,
81 delay: 0.5 * i,
82 ease: "easeOut",
83 }}
84 key={`start-upper-${i}`}
85 ></motion.path>
86 </g>
87 );
88 })}
89
90 <defs>
91 <linearGradient id="path-gradient" x1="0%" y1="0%" x2="100%" y2="0%">
92 <stop offset="0%" stopColor="white" stopOpacity="0" />
93 <stop offset="5%" stopColor={lineColor} stopOpacity="1" />
94 <stop offset="95%" stopColor={lineColor} stopOpacity="1" />
95 <stop offset="100%" stopColor="white" stopOpacity="0" />
96 </linearGradient>
97 </defs>
98
99 {dots.map((dot, i) => (
100 <g key={`points-group-${i}`}>
101 <g key={`start-${i}`}>
102 <circle
103 cx={projectPoint(dot.start.lat, dot.start.lng).x}
104 cy={projectPoint(dot.start.lat, dot.start.lng).y}
105 r="2"
106 fill={lineColor}
107 />
108 <circle
109 cx={projectPoint(dot.start.lat, dot.start.lng).x}
110 cy={projectPoint(dot.start.lat, dot.start.lng).y}
111 r="2"
112 fill={lineColor}
113 opacity="0.5"
114 >
115 <animate
116 attributeName="r"
117 from="2"
118 to="8"
119 dur="1.5s"
120 begin="0s"
121 repeatCount="indefinite"
122 />
123 <animate
124 attributeName="opacity"
125 from="0.5"
126 to="0"
127 dur="1.5s"
128 begin="0s"
129 repeatCount="indefinite"
130 />
131 </circle>
132 </g>
133 <g key={`end-${i}`}>
134 <circle
135 cx={projectPoint(dot.end.lat, dot.end.lng).x}
136 cy={projectPoint(dot.end.lat, dot.end.lng).y}
137 r="2"
138 fill={lineColor}
139 />
140 <circle
141 cx={projectPoint(dot.end.lat, dot.end.lng).x}
142 cy={projectPoint(dot.end.lat, dot.end.lng).y}
143 r="2"
144 fill={lineColor}
145 opacity="0.5"
146 >
147 <animate
148 attributeName="r"
149 from="2"
150 to="8"
151 dur="1.5s"
152 begin="0s"
153 repeatCount="indefinite"
154 />
155 <animate
156 attributeName="opacity"
157 from="0.5"
158 to="0"
159 dur="1.5s"
160 begin="0s"
161 repeatCount="indefinite"
162 />
163 </circle>
164 </g>
165 </g>
166 ))}
167 </svg>
168 </div>
169 );
170}
171