Skip to content

Commit 2948487

Browse files
committed
Refactor HintCircle to improve offset calculation and use pixel-based radius for better map rendering
1 parent 8438b80 commit 2948487

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

components/Map.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,27 @@ function HintCircle({ location, gameOptions, round }) {
1919
const [zoom, setZoom] = useState(2);
2020

2121
const map = useMapEvents({
22-
zoomend: () => setZoom(map.getZoom()),
22+
zoom: () => setZoom(map.getZoom()),
2323
});
2424

25-
const radius = hintMul * (gameOptions?.maxDist ?? 20000);
26-
const seed = round ?? 1;
27-
const radiusDeg = radius / 111000;
28-
29-
// Offset center by 5-95% of radius in a random direction
30-
// Use location coords as part of seed for more variation
31-
const locSeed = Math.abs(location.lat * 1000 + location.long * 1000) + seed;
32-
const offsetFraction = 0.05 + seededRandom(locSeed) * 0.9;
33-
const offsetAngle = seededRandom(locSeed * 7.3) * 2 * Math.PI;
34-
const offsetDeg = radiusDeg * offsetFraction;
35-
const centerLat = location.lat + offsetDeg * Math.cos(offsetAngle);
36-
const centerLng = location.long + offsetDeg * Math.sin(offsetAngle);
37-
38-
// Convert meters to pixels at current zoom
39-
// At zoom 0, 1 pixel ≈ 156543 meters at equator
40-
const metersPerPixel = 156543 / Math.pow(2, zoom);
41-
const pixelRadius = radius / metersPerPixel;
25+
// 100px at zoom 1, doubles each zoom level
26+
const ogRadius = 75
27+
const pixelRadius = ogRadius * Math.pow(2, zoom - 1);
28+
// Offset the center by 0 to pixelRadius in a random direction (sqrt for uniform area distribution)
29+
const seed = (round ?? 1) + Math.abs(location.lat * ogRadius + location.long * ogRadius);
30+
const offsetAngle = seededRandom(seed * 3) * 2 * Math.PI;
31+
const offsetAmount = Math.sqrt(seededRandom(seed * 7)) * pixelRadius;
32+
const offsetX = offsetAmount * Math.cos(offsetAngle);
33+
const offsetY = offsetAmount * Math.sin(offsetAngle);
34+
35+
// Convert pixel offset back to lat/lng
36+
const pointC = map.latLngToContainerPoint(L.latLng(location.lat, location.long));
37+
const offsetPoint = L.point(pointC.x + offsetX, pointC.y + offsetY);
38+
const offsetCenter = map.containerPointToLatLng(offsetPoint);
4239

4340
return (
4441
<CircleMarker
45-
center={{ lat: centerLat, lng: centerLng }}
42+
center={offsetCenter}
4643
radius={pixelRadius}
4744
className="hintCircle"
4845
/>

0 commit comments

Comments
 (0)