浏览代码

Fit the canvases to the current screen size

This also pre-resizes the images, since doing that is more expensive than
drawing the images normally.
master
Fen Dweller 5 年前
父节点
当前提交
caddcdea45
共有 3 个文件被更改,包括 70 次插入13 次删除
  1. +14
    -1
      xray.css
  2. +6
    -2
      xray.html
  3. +50
    -10
      xray.js

+ 14
- 1
xray.css 查看文件

@@ -1,5 +1,10 @@
body {
html, body {
font-family: sans-serif; font-family: sans-serif;
width: 100vw;
height: 100vh;
margin: 0;
display: flex;
flex-direction: column;
} }


canvas { canvas {
@@ -20,9 +25,12 @@ img {


#menu { #menu {
display: flex; display: flex;
flex: 0 1;
flex-direction: row; flex-direction: row;
transition: 0.5s; transition: 0.5s;
flex-wrap: wrap; flex-wrap: wrap;
justify-content: center;
align-items: center;
} }


#menu.start input, #menu.start input,
@@ -44,3 +52,8 @@ img {
.large > input[type=number] { .large > input[type=number] {
max-width: 72pt; max-width: 72pt;
} }

#fill-div {
position: relative;
flex: 1 0;
}

+ 6
- 2
xray.html 查看文件

@@ -35,8 +35,12 @@
</label> </label>
</div> </div>
<canvas class="hidden" id="overlay"></canvas>
<canvas class="hidden" id="base"></canvas>
<div id="fill-div">
<canvas class="hidden" id="overlay-resized"></canvas>
<canvas class="hidden" id="overlay"></canvas>
<canvas class="hidden" id="base-resized"></canvas>
<canvas class="hidden" id="base"></canvas>
</div>
<img id="overlay-img" src="overlay.png"> <img id="overlay-img" src="overlay.png">
<img id="base-img" src="base.png"> <img id="base-img" src="base.png">
</body> </body>

+ 50
- 10
xray.js 查看文件

@@ -3,9 +3,16 @@
let overlayLoaded = false; let overlayLoaded = false;
let baseLoaded = false; let baseLoaded = false;


let running = false;

let radius = 200; let radius = 200;
let softness = 25; let softness = 25;


let width;
let height;

let scale;

document.addEventListener("DOMContentLoaded", e => { document.addEventListener("DOMContentLoaded", e => {
document.querySelector("#load-button").addEventListener("click", e => { document.querySelector("#load-button").addEventListener("click", e => {
console.log("Trying to load..."); console.log("Trying to load...");
@@ -199,6 +206,12 @@ document.addEventListener("DOMContentLoaded", e => {
console.warn("That was a bogus softness..."); console.warn("That was a bogus softness...");
} }
} }

window.addEventListener("resize", e => {
if (running) {
setup();
}
})
}); });


@@ -230,8 +243,12 @@ function load() {
} }


function setup() { function setup() {
running = true;

const overlay = document.querySelector("#overlay"); const overlay = document.querySelector("#overlay");
const base = document.querySelector("#base"); const base = document.querySelector("#base");
const overlayResized = document.querySelector("#overlay-resized");
const baseResized = document.querySelector("#base-resized");


overlay.classList.remove("hidden"); overlay.classList.remove("hidden");
base.classList.remove("hidden"); base.classList.remove("hidden");
@@ -243,23 +260,46 @@ function setup() {
const overlayCtx = overlay.getContext("2d"); const overlayCtx = overlay.getContext("2d");
/** @type {CanvasRenderingContext2D} */ /** @type {CanvasRenderingContext2D} */
const baseCtx = base.getContext("2d"); const baseCtx = base.getContext("2d");
/** @type {CanvasRenderingContext2D} */
const overlayCtxResized = overlayResized.getContext("2d");
/** @type {CanvasRenderingContext2D} */
const baseCtxResized = baseResized.getContext("2d");


baseCtx.canvas.width = baseImg.width;
baseCtx.canvas.height = baseImg.height;
const availableWidth = document.querySelector("#fill-div").getBoundingClientRect().width;
const availableHeight = document.querySelector("#fill-div").getBoundingClientRect().height;

const scaleW = availableWidth / baseImg.width;
const scaleH = availableHeight / baseImg.height;
scale = Math.min(scaleW, scaleH);

const width = availableWidth * scale / scaleW;
const height = availableHeight * scale / scaleH;

[baseCtx, baseCtxResized, overlayCtx, overlayCtxResized].forEach(ctx => {
console.log(ctx.canvas)
ctx.canvas.width = width;
ctx.canvas.height = height;
ctx.canvas.style.left = (availableWidth - width) / 2 + "px";
ctx.canvas.style.top = (availableHeight - height) / 2 + "px";
});
baseCtx.drawImage(baseImg, 0, 0);
baseCtxResized.drawImage(baseImg, 0, 0, width, height);
baseCtx.drawImage(baseResized, 0, 0, width, height);

overlayCtxResized.drawImage(overlayImg, 0, 0, width, height);


overlayCtx.canvas.width = overlayImg.width;
overlayCtx.canvas.height = overlayImg.height;
console.log("Done"); console.log("Done");
} }


function updateOverlay(points) { function updateOverlay(points) {


const overlay = document.querySelector("#overlay");
const overlayResized = document.querySelector("#overlay-resized");

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

const overlayImg = document.querySelector("#overlay-img");
/** @type {CanvasRenderingContext2D} */
const overlayCtxResized = overlay.getContext("2d");


const w = overlayCtx.canvas.width; const w = overlayCtx.canvas.width;
const h = overlayCtx.canvas.height; const h = overlayCtx.canvas.height;
@@ -273,9 +313,9 @@ function updateOverlay(points) {
points.forEach(point => { points.forEach(point => {
const [x,y] = point; const [x,y] = point;
overlayCtx.beginPath(); overlayCtx.beginPath();
overlayCtx.ellipse(x, y, radius, radius, 0, 0, 2 * Math.PI);
overlayCtx.ellipse(x, y, radius * scale, radius * scale, 0, 0, 2 * Math.PI);
const gradient = overlayCtx.createRadialGradient(x, y, 0, x, y, radius);
const gradient = overlayCtx.createRadialGradient(x, y, 0, x, y, Math.floor(radius * scale));
gradient.addColorStop((100-softness)/100, '#000000FF'); gradient.addColorStop((100-softness)/100, '#000000FF');
gradient.addColorStop(1, '#00000000'); gradient.addColorStop(1, '#00000000');
overlayCtx.fillStyle = gradient; overlayCtx.fillStyle = gradient;
@@ -286,7 +326,7 @@ function updateOverlay(points) {


overlayCtx.globalCompositeOperation = "source-in"; overlayCtx.globalCompositeOperation = "source-in";


overlayCtx.drawImage(overlayImg, 0, 0);
overlayCtx.drawImage(overlayResized, 0, 0);


overlayCtx.restore(); overlayCtx.restore();
} }


正在加载...
取消
保存