fix(tilemap-editor): 修复画布高DPI屏幕分辨率适配问题
This commit is contained in:
@@ -81,11 +81,14 @@ export const TilemapCanvas: React.FC<TilemapCanvasProps> = ({
|
|||||||
const ctx = canvas.getContext('2d');
|
const ctx = canvas.getContext('2d');
|
||||||
if (!ctx) return;
|
if (!ctx) return;
|
||||||
|
|
||||||
|
const dpr = window.devicePixelRatio || 1;
|
||||||
|
|
||||||
// Clear
|
// Clear
|
||||||
ctx.fillStyle = '#2d2d2d';
|
ctx.fillStyle = '#2d2d2d';
|
||||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
ctx.save();
|
ctx.save();
|
||||||
|
ctx.scale(dpr, dpr);
|
||||||
ctx.translate(panX, panY);
|
ctx.translate(panX, panY);
|
||||||
ctx.scale(zoom, zoom);
|
ctx.scale(zoom, zoom);
|
||||||
|
|
||||||
@@ -214,11 +217,16 @@ export const TilemapCanvas: React.FC<TilemapCanvasProps> = ({
|
|||||||
cancelAnimationFrame(rafId);
|
cancelAnimationFrame(rafId);
|
||||||
}
|
}
|
||||||
rafId = requestAnimationFrame(() => {
|
rafId = requestAnimationFrame(() => {
|
||||||
|
const dpr = window.devicePixelRatio || 1;
|
||||||
const newWidth = container.clientWidth;
|
const newWidth = container.clientWidth;
|
||||||
const newHeight = container.clientHeight;
|
const newHeight = container.clientHeight;
|
||||||
if (canvas.width !== newWidth || canvas.height !== newHeight) {
|
const scaledWidth = Math.floor(newWidth * dpr);
|
||||||
canvas.width = newWidth;
|
const scaledHeight = Math.floor(newHeight * dpr);
|
||||||
canvas.height = newHeight;
|
if (canvas.width !== scaledWidth || canvas.height !== scaledHeight) {
|
||||||
|
canvas.width = scaledWidth;
|
||||||
|
canvas.height = scaledHeight;
|
||||||
|
canvas.style.width = `${newWidth}px`;
|
||||||
|
canvas.style.height = `${newHeight}px`;
|
||||||
draw();
|
draw();
|
||||||
}
|
}
|
||||||
rafId = null;
|
rafId = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user