Browse Source

Handle devicePixelRatio for high-DPI screens

master
Fen Dweller 5 years ago
parent
commit
71a67b9854
2 changed files with 11 additions and 4 deletions
  1. +1
    -0
      xray.html
  2. +10
    -4
      xray.js

+ 1
- 0
xray.html View File

@@ -10,6 +10,7 @@
<meta name="description" content="rip Flash lol" />
<meta property="og:title" content="X-ray Viewer" />
<meta property="og:description" content="rip Flash lol" />
<meta name="viewport" content="initial-scale=1.0,width=device-width,height=device-height" />
</head>

<body>


+ 10
- 4
xray.js View File

@@ -522,14 +522,20 @@ function setup() {

width = fitScreen ? Math.floor(availableWidth * scale / scaleW) : baseImg.width;
height = fitScreen ? Math.floor(availableHeight * scale / scaleH) : baseImg.height;
const pixelScale = window.devicePixelRatio;

[baseCtx, baseCtxResized, overlayCtx, overlayCtxResized, shadowCtx].forEach(ctx => {
ctx.canvas.style.width = width + "px";
ctx.canvas.style.height = height + "px";
ctx.canvas.width = width;
ctx.canvas.height = height;
ctx.canvas.style.left = (availableWidth - width) / 2 + "px";
ctx.canvas.style.top = fitScreen ? (availableHeight - height) / 2 + "px" : 0;
ctx.canvas.width = Math.floor(width * pixelScale);
ctx.canvas.height = Math.floor(height * pixelScale);
ctx.scale(pixelScale, pixelScale);
});
baseCtxResized.drawImage(baseImg, 0, 0, width, height);
baseCtx.drawImage(baseResized, 0, 0, width, height);

@@ -572,8 +578,6 @@ function updateOverlay(points, clicked) {

/** @type {CanvasRenderingContext2D} */
const overlayCtx = overlay.getContext("2d");
/** @type {CanvasRenderingContext2D} */
const overlayCtxResized = overlay.getContext("2d");

const w = overlayCtx.canvas.width;
const h = overlayCtx.canvas.height;
@@ -619,7 +623,9 @@ function updateOverlay(points, clicked) {

overlayCtx.globalCompositeOperation = "source-in";

overlayCtx.drawImage(overlayResized, 0, 0);
// the resized canvas was already scaled up, so we have to compensate here

overlayCtx.drawImage(overlayResized, 0, 0, w/window.devicePixelRatio, h/window.devicePixelRatio);

overlayCtx.globalCompositeOperation = "source-over";


Loading…
Cancel
Save