mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 08:13:12 +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();
|
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
|
// Set up keyboard and mouse event listeners
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
const moveSpeed = 50 / viewState.zoomLevel;
|
const moveSpeed = 50 / viewState.zoomLevel;
|
||||||
const zoomFactor = 1.2;
|
|
||||||
|
|
||||||
switch (e.key.toLowerCase()) {
|
switch (e.key.toLowerCase()) {
|
||||||
case 'w':
|
case 'w':
|
||||||
@@ -101,7 +118,7 @@ export default function CanvasMinimapPage() {
|
|||||||
case '=': // Handle both + and = key (since + requires shift)
|
case '=': // Handle both + and = key (since + requires shift)
|
||||||
setViewState(prev => ({
|
setViewState(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
zoomLevel: Math.min(10, prev.zoomLevel * zoomFactor)
|
zoomLevel: getNextZoomLevel(prev.zoomLevel, 'up')
|
||||||
}));
|
}));
|
||||||
e.preventDefault(); // Prevent browser zoom
|
e.preventDefault(); // Prevent browser zoom
|
||||||
break;
|
break;
|
||||||
@@ -109,7 +126,7 @@ export default function CanvasMinimapPage() {
|
|||||||
case '_': // Handle both - and _ key (since _ requires shift)
|
case '_': // Handle both - and _ key (since _ requires shift)
|
||||||
setViewState(prev => ({
|
setViewState(prev => ({
|
||||||
...prev,
|
...prev,
|
||||||
zoomLevel: Math.max(0.1, prev.zoomLevel / zoomFactor)
|
zoomLevel: getNextZoomLevel(prev.zoomLevel, 'down')
|
||||||
}));
|
}));
|
||||||
e.preventDefault(); // Prevent browser zoom
|
e.preventDefault(); // Prevent browser zoom
|
||||||
break;
|
break;
|
||||||
@@ -118,10 +135,8 @@ export default function CanvasMinimapPage() {
|
|||||||
|
|
||||||
const handleWheel = (e: WheelEvent) => {
|
const handleWheel = (e: WheelEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
const zoomFactor = 1.1;
|
const direction = e.deltaY > 0 ? 'down' : 'up';
|
||||||
const newZoom = e.deltaY > 0
|
const newZoom = getNextZoomLevel(viewState.zoomLevel, direction);
|
||||||
? Math.max(0.1, viewState.zoomLevel / zoomFactor)
|
|
||||||
: Math.min(10, viewState.zoomLevel * zoomFactor);
|
|
||||||
|
|
||||||
setViewState(prev => ({ ...prev, zoomLevel: newZoom }));
|
setViewState(prev => ({ ...prev, zoomLevel: newZoom }));
|
||||||
};
|
};
|
||||||
@@ -358,7 +373,8 @@ export default function CanvasMinimapPage() {
|
|||||||
const canvas = canvasRef.current;
|
const canvas = canvasRef.current;
|
||||||
if (!canvas) return;
|
if (!canvas) return;
|
||||||
|
|
||||||
const ctx = canvas.getContext('2d')!;
|
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;
|
||||||
|
ctx.imageSmoothingEnabled = false;
|
||||||
|
|
||||||
// Get canvas dimensions
|
// Get canvas dimensions
|
||||||
const width = canvas.width;
|
const width = canvas.width;
|
||||||
@@ -385,19 +401,36 @@ export default function CanvasMinimapPage() {
|
|||||||
const rightTile = Math.ceil(worldRight / TILE_SIZE) + 1;
|
const rightTile = Math.ceil(worldRight / TILE_SIZE) + 1;
|
||||||
const bottomTile = Math.ceil(worldBottom / 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 tileY = topTile; tileY <= bottomTile; tileY++) {
|
||||||
for (let tileX = leftTile; tileX <= rightTile; tileX++) {
|
for (let tileX = leftTile; tileX <= rightTile; tileX++) {
|
||||||
const tileKey = `${tileX},${tileY}`;
|
const tileKey = `${tileX},${tileY}`;
|
||||||
const virtualTile = virtualTiles.current.get(tileKey);
|
const virtualTile = virtualTiles.current.get(tileKey);
|
||||||
|
|
||||||
// Calculate screen position using consistent scale factor
|
// Get tile position from pre-calculated edges
|
||||||
const worldTileX = tileX * TILE_SIZE;
|
const tileIndexX = tileX - leftTile;
|
||||||
const worldTileY = tileY * TILE_SIZE;
|
const tileIndexY = tileY - topTile;
|
||||||
const screenX = (worldTileX - worldLeft) * scale;
|
const screenX = tileEdgesX[tileIndexX];
|
||||||
const screenY = (worldTileY - worldTop) * scale;
|
const screenY = tileEdgesY[tileIndexY];
|
||||||
const screenWidth = TILE_SIZE * scale;
|
const screenWidth = tileEdgesX[tileIndexX + 1] - screenX;
|
||||||
const screenHeight = TILE_SIZE * scale;
|
const screenHeight = tileEdgesY[tileIndexY + 1] - screenY;
|
||||||
|
|
||||||
// Calculate clipping bounds to ensure we don't draw outside canvas
|
// Calculate clipping bounds to ensure we don't draw outside canvas
|
||||||
const clippedX = Math.max(0, screenX);
|
const clippedX = Math.max(0, screenX);
|
||||||
@@ -412,31 +445,29 @@ export default function CanvasMinimapPage() {
|
|||||||
|
|
||||||
if (virtualTile) {
|
if (virtualTile) {
|
||||||
// Tile is loaded, draw it with clipping
|
// Tile is loaded, draw it with clipping
|
||||||
// Always use nearest neighbor (pixelated) scaling
|
|
||||||
ctx.imageSmoothingEnabled = false;
|
ctx.imageSmoothingEnabled = false;
|
||||||
|
|
||||||
// Calculate source coordinates maintaining 1:1 aspect ratio
|
// Round source coordinates to prevent subpixel sampling
|
||||||
const sourceX = clippedX > screenX ? (clippedX - screenX) / scale : 0;
|
const rawSourceX = clippedX > screenX ? (clippedX - screenX) / scale : 0;
|
||||||
const sourceY = clippedY > screenY ? (clippedY - screenY) / scale : 0;
|
const rawSourceY = clippedY > screenY ? (clippedY - screenY) / scale : 0;
|
||||||
const sourceWidth = clippedWidth / scale;
|
const rawSourceWidth = clippedWidth / scale;
|
||||||
const sourceHeight = clippedHeight / scale;
|
const rawSourceHeight = clippedHeight / scale;
|
||||||
|
|
||||||
// Ensure pixel-perfect alignment by rounding to integers
|
// Use clipped coordinates directly since screen coordinates are already rounded
|
||||||
// This preserves aspect ratio by maintaining the same rounding for both dimensions
|
const destX = clippedX;
|
||||||
const destX = Math.round(clippedX);
|
const destY = clippedY;
|
||||||
const destY = Math.round(clippedY);
|
const destWidth = clippedWidth;
|
||||||
const destWidth = Math.round(clippedWidth);
|
const destHeight = clippedHeight;
|
||||||
const destHeight = Math.round(clippedHeight);
|
|
||||||
|
|
||||||
ctx.drawImage(
|
ctx.drawImage(
|
||||||
virtualTile,
|
virtualTile,
|
||||||
sourceX, sourceY, sourceWidth, sourceHeight,
|
rawSourceX, rawSourceY, rawSourceWidth, rawSourceHeight,
|
||||||
destX, destY, destWidth, destHeight
|
destX, destY, destWidth, destHeight
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Tile not loaded yet, draw black
|
// Tile not loaded yet, draw black
|
||||||
ctx.fillStyle = '#000000';
|
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)
|
// Start loading the tile in the background (but don't wait for it)
|
||||||
getVirtualTile(tileX, tileY).then(() => {
|
getVirtualTile(tileX, tileY).then(() => {
|
||||||
@@ -518,7 +549,7 @@ export default function CanvasMinimapPage() {
|
|||||||
}
|
}
|
||||||
}, [selectedInstance]);
|
}, [selectedInstance]);
|
||||||
|
|
||||||
// Reset view when instance changes
|
// Reset view when instance changes and snap to pixel-perfect zoom
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (selectedInstance) {
|
if (selectedInstance) {
|
||||||
const instanceData = instances.find(inst => inst.instanceId === selectedInstance);
|
const instanceData = instances.find(inst => inst.instanceId === selectedInstance);
|
||||||
@@ -526,7 +557,7 @@ export default function CanvasMinimapPage() {
|
|||||||
setViewState({
|
setViewState({
|
||||||
centerX: (instanceData.bounds.x1 + instanceData.bounds.x2) / 2,
|
centerX: (instanceData.bounds.x1 + instanceData.bounds.x2) / 2,
|
||||||
centerY: (instanceData.bounds.y1 + instanceData.bounds.y2) / 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]);
|
}, [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
|
// Set up live chunk update listener
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!selectedInstance || !selectedSurface || !selectedForce) {
|
if (!selectedInstance || !selectedSurface || !selectedForce) {
|
||||||
@@ -635,7 +674,7 @@ export default function CanvasMinimapPage() {
|
|||||||
{selectedInstance ? (
|
{selectedInstance ? (
|
||||||
<div>
|
<div>
|
||||||
<div style={{ marginBottom: "10px", fontSize: "14px", color: "#666" }}>
|
<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>
|
||||||
<div
|
<div
|
||||||
ref={containerRef}
|
ref={containerRef}
|
||||||
|
|||||||
Reference in new issue
Block a user