mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-27 00:37:14 +09:00
Fix issues with black seams from incorrect scaling math
This commit is contained in:
1 parent
b70c7bd84f
commit
285254e3bc
1 file changed
+72
-33
@@ -78,11 +78,28 @@ export default function CanvasMinimapPage() {
|
||||
loadSurfaceForceData();
|
||||
}, []);
|
||||
|
||||
// Define pixel-perfect zoom levels to eliminate seaming issues
|
||||
const ZOOM_LEVELS = [0.125, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 2.5, 3, 4, 5, 6, 8, 10];
|
||||
|
||||
const getClosestZoomLevel = (targetZoom: number): number => {
|
||||
return ZOOM_LEVELS.reduce((prev, curr) =>
|
||||
Math.abs(curr - targetZoom) < Math.abs(prev - targetZoom) ? curr : prev
|
||||
);
|
||||
};
|
||||
|
||||
const getNextZoomLevel = (currentZoom: number, direction: 'up' | 'down'): number => {
|
||||
const currentIndex = ZOOM_LEVELS.findIndex(zoom => Math.abs(zoom - currentZoom) < 0.001);
|
||||
if (direction === 'up') {
|
||||
return ZOOM_LEVELS[Math.min(currentIndex + 1, ZOOM_LEVELS.length - 1)];
|
||||
} else {
|
||||
return ZOOM_LEVELS[Math.max(currentIndex - 1, 0)];
|
||||
}
|
||||
};
|
||||
|
||||
// Set up keyboard and mouse event listeners
|
||||
useEffect(() => {
|
||||
const handleKeyDown = (e: KeyboardEvent) => {
|
||||
const moveSpeed = 50 / viewState.zoomLevel;
|
||||
const zoomFactor = 1.2;
|
||||
|
||||
switch (e.key.toLowerCase()) {
|
||||
case 'w':
|
||||
@@ -101,7 +118,7 @@ export default function CanvasMinimapPage() {
|
||||
case '=': // Handle both + and = key (since + requires shift)
|
||||
setViewState(prev => ({
|
||||
...prev,
|
||||
zoomLevel: Math.min(10, prev.zoomLevel * zoomFactor)
|
||||
zoomLevel: getNextZoomLevel(prev.zoomLevel, 'up')
|
||||
}));
|
||||
e.preventDefault(); // Prevent browser zoom
|
||||
break;
|
||||
@@ -109,7 +126,7 @@ export default function CanvasMinimapPage() {
|
||||
case '_': // Handle both - and _ key (since _ requires shift)
|
||||
setViewState(prev => ({
|
||||
...prev,
|
||||
zoomLevel: Math.max(0.1, prev.zoomLevel / zoomFactor)
|
||||
zoomLevel: getNextZoomLevel(prev.zoomLevel, 'down')
|
||||
}));
|
||||
e.preventDefault(); // Prevent browser zoom
|
||||
break;
|
||||
@@ -118,10 +135,8 @@ export default function CanvasMinimapPage() {
|
||||
|
||||
const handleWheel = (e: WheelEvent) => {
|
||||
e.preventDefault();
|
||||
const zoomFactor = 1.1;
|
||||
const newZoom = e.deltaY > 0
|
||||
? Math.max(0.1, viewState.zoomLevel / zoomFactor)
|
||||
: Math.min(10, viewState.zoomLevel * zoomFactor);
|
||||
const direction = e.deltaY > 0 ? 'down' : 'up';
|
||||
const newZoom = getNextZoomLevel(viewState.zoomLevel, direction);
|
||||
|
||||
setViewState(prev => ({ ...prev, zoomLevel: newZoom }));
|
||||
};
|
||||
@@ -358,7 +373,8 @@ export default function CanvasMinimapPage() {
|
||||
const canvas = canvasRef.current;
|
||||
if (!canvas) return;
|
||||
|
||||
const ctx = canvas.getContext('2d')!;
|
||||
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
|
||||
ctx.imageSmoothingEnabled = false;
|
||||
|
||||
// Get canvas dimensions
|
||||
const width = canvas.width;
|
||||
@@ -385,19 +401,36 @@ export default function CanvasMinimapPage() {
|
||||
const rightTile = Math.ceil(worldRight / TILE_SIZE) + 1;
|
||||
const bottomTile = Math.ceil(worldBottom / TILE_SIZE) + 1;
|
||||
|
||||
// Render visible tiles (only those that are already loaded)
|
||||
const scaledTileSize = TILE_SIZE * scale;
|
||||
const firstTileScreenX = Math.round((leftTile * TILE_SIZE - worldLeft) * scale);
|
||||
const firstTileScreenY = Math.round((topTile * TILE_SIZE - worldTop) * scale);
|
||||
|
||||
// Pre-calculate all tile edge positions using exact integer steps
|
||||
const tileEdgesX: number[] = [];
|
||||
const tileEdgesY: number[] = [];
|
||||
|
||||
// Build tile edges using clean integer arithmetic from the first tile position
|
||||
for (let i = 0; i <= (rightTile - leftTile + 1); i++) {
|
||||
tileEdgesX.push(firstTileScreenX + Math.round(i * scaledTileSize));
|
||||
}
|
||||
|
||||
for (let i = 0; i <= (bottomTile - topTile + 1); i++) {
|
||||
tileEdgesY.push(firstTileScreenY + Math.round(i * scaledTileSize));
|
||||
}
|
||||
|
||||
// Render visible tiles using the pre-calculated edge grid
|
||||
for (let tileY = topTile; tileY <= bottomTile; tileY++) {
|
||||
for (let tileX = leftTile; tileX <= rightTile; tileX++) {
|
||||
const tileKey = `${tileX},${tileY}`;
|
||||
const virtualTile = virtualTiles.current.get(tileKey);
|
||||
|
||||
// Calculate screen position using consistent scale factor
|
||||
const worldTileX = tileX * TILE_SIZE;
|
||||
const worldTileY = tileY * TILE_SIZE;
|
||||
const screenX = (worldTileX - worldLeft) * scale;
|
||||
const screenY = (worldTileY - worldTop) * scale;
|
||||
const screenWidth = TILE_SIZE * scale;
|
||||
const screenHeight = TILE_SIZE * scale;
|
||||
// Get tile position from pre-calculated edges
|
||||
const tileIndexX = tileX - leftTile;
|
||||
const tileIndexY = tileY - topTile;
|
||||
const screenX = tileEdgesX[tileIndexX];
|
||||
const screenY = tileEdgesY[tileIndexY];
|
||||
const screenWidth = tileEdgesX[tileIndexX + 1] - screenX;
|
||||
const screenHeight = tileEdgesY[tileIndexY + 1] - screenY;
|
||||
|
||||
// Calculate clipping bounds to ensure we don't draw outside canvas
|
||||
const clippedX = Math.max(0, screenX);
|
||||
@@ -412,31 +445,29 @@ export default function CanvasMinimapPage() {
|
||||
|
||||
if (virtualTile) {
|
||||
// Tile is loaded, draw it with clipping
|
||||
// Always use nearest neighbor (pixelated) scaling
|
||||
ctx.imageSmoothingEnabled = false;
|
||||
|
||||
// Calculate source coordinates maintaining 1:1 aspect ratio
|
||||
const sourceX = clippedX > screenX ? (clippedX - screenX) / scale : 0;
|
||||
const sourceY = clippedY > screenY ? (clippedY - screenY) / scale : 0;
|
||||
const sourceWidth = clippedWidth / scale;
|
||||
const sourceHeight = clippedHeight / scale;
|
||||
// Round source coordinates to prevent subpixel sampling
|
||||
const rawSourceX = clippedX > screenX ? (clippedX - screenX) / scale : 0;
|
||||
const rawSourceY = clippedY > screenY ? (clippedY - screenY) / scale : 0;
|
||||
const rawSourceWidth = clippedWidth / scale;
|
||||
const rawSourceHeight = clippedHeight / scale;
|
||||
|
||||
// Ensure pixel-perfect alignment by rounding to integers
|
||||
// This preserves aspect ratio by maintaining the same rounding for both dimensions
|
||||
const destX = Math.round(clippedX);
|
||||
const destY = Math.round(clippedY);
|
||||
const destWidth = Math.round(clippedWidth);
|
||||
const destHeight = Math.round(clippedHeight);
|
||||
// Use clipped coordinates directly since screen coordinates are already rounded
|
||||
const destX = clippedX;
|
||||
const destY = clippedY;
|
||||
const destWidth = clippedWidth;
|
||||
const destHeight = clippedHeight;
|
||||
|
||||
ctx.drawImage(
|
||||
virtualTile,
|
||||
sourceX, sourceY, sourceWidth, sourceHeight,
|
||||
rawSourceX, rawSourceY, rawSourceWidth, rawSourceHeight,
|
||||
destX, destY, destWidth, destHeight
|
||||
);
|
||||
} else {
|
||||
// Tile not loaded yet, draw black
|
||||
ctx.fillStyle = '#000000';
|
||||
ctx.fillRect(Math.round(clippedX), Math.round(clippedY), Math.round(clippedWidth), Math.round(clippedHeight));
|
||||
ctx.fillRect(clippedX, clippedY, clippedWidth, clippedHeight);
|
||||
|
||||
// Start loading the tile in the background (but don't wait for it)
|
||||
getVirtualTile(tileX, tileY).then(() => {
|
||||
@@ -518,7 +549,7 @@ export default function CanvasMinimapPage() {
|
||||
}
|
||||
}, [selectedInstance]);
|
||||
|
||||
// Reset view when instance changes
|
||||
// Reset view when instance changes and snap to pixel-perfect zoom
|
||||
useEffect(() => {
|
||||
if (selectedInstance) {
|
||||
const instanceData = instances.find(inst => inst.instanceId === selectedInstance);
|
||||
@@ -526,7 +557,7 @@ export default function CanvasMinimapPage() {
|
||||
setViewState({
|
||||
centerX: (instanceData.bounds.x1 + instanceData.bounds.x2) / 2,
|
||||
centerY: (instanceData.bounds.y1 + instanceData.bounds.y2) / 2,
|
||||
zoomLevel: 1,
|
||||
zoomLevel: getClosestZoomLevel(1), // Snap to nearest pixel-perfect zoom
|
||||
});
|
||||
}
|
||||
|
||||
@@ -538,6 +569,14 @@ export default function CanvasMinimapPage() {
|
||||
}
|
||||
}, [selectedInstance, instances]);
|
||||
|
||||
// Snap current zoom level to nearest pixel-perfect level on mount
|
||||
useEffect(() => {
|
||||
setViewState(prev => ({
|
||||
...prev,
|
||||
zoomLevel: getClosestZoomLevel(prev.zoomLevel)
|
||||
}));
|
||||
}, []);
|
||||
|
||||
// Set up live chunk update listener
|
||||
useEffect(() => {
|
||||
if (!selectedInstance || !selectedSurface || !selectedForce) {
|
||||
@@ -635,7 +674,7 @@ export default function CanvasMinimapPage() {
|
||||
{selectedInstance ? (
|
||||
<div>
|
||||
<div style={{ marginBottom: "10px", fontSize: "14px", color: "#666" }}>
|
||||
Use WASD to move, +/- or scroll wheel to zoom. Current zoom: {viewState.zoomLevel.toFixed(2)}x
|
||||
Use WASD to move, +/- or scroll wheel to zoom. Current zoom: {viewState.zoomLevel}x
|
||||
</div>
|
||||
<div
|
||||
ref={containerRef}
|
||||
|
||||
Reference in new issue
Block a user