Refactor canvas rendering and DPI handling for improved quality and performance

This commit is contained in:
Oleg Sh
2026-01-01 16:16:46 +01:00
parent ea61466fe0
commit e276350318
10 changed files with 133 additions and 32 deletions

View File

@@ -69,4 +69,30 @@ function InvertColor(hex) {
const toHex = (n) => n.toString(16).padStart(2, '0');
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}
function setDPIForCanvas(canvas, width, height) {
const dpr = window.devicePixelRatio || 1;
canvas.width = Math.round(width * dpr);
canvas.height = Math.round(height * dpr);
if (dpr != 1)
{
canvas.style.width = Math.round(width) + "px";
canvas.style.height = Math.round(height) + "px";
}
const ctx = canvas.getContext("2d");
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
}
function getCanvasLogicWidth(canvas) {
const dpr = window.devicePixelRatio || 1;
return canvas.width / dpr;
}
function getCanvasLogicHeight(canvas) {
const dpr = window.devicePixelRatio || 1;
return canvas.height / dpr;
}