From 7aeef34b07dce873c32a607b4b230d548549321c Mon Sep 17 00:00:00 2001 From: saingchildren <80457007+saingchildren@users.noreply.github.com> Date: Sat, 15 Nov 2025 15:27:46 +0800 Subject: [PATCH] write some comment and rewrite --- main.js | 104 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 53 insertions(+), 51 deletions(-) diff --git a/main.js b/main.js index c1cb48e..ea6ef2b 100644 --- a/main.js +++ b/main.js @@ -1,36 +1,14 @@ const canvas = document.getElementById("c"); -const ctx = canvas.getContext("2d"); +const c = canvas.getContext("2d"); + +const imageWidth = 100; +const imageHeight = 100; +const columns = Math.floor(canvas.width / imageWidth); +const totalImageCount = 100; -const img = new Image(); -img.src = "./images/1.jpg"; -const thumbW = 100; -const thumbH = 100; -const gap = 10; -const columns = Math.floor(canvas.width / (thumbW + gap)); -const count = 100; let scrollTop = 0; let selectedIndex = -1; -canvas.addEventListener("wheel", (e) => { - scrollTop += e.deltaY; - const rows = Math.ceil(count / columns); - const contentHeight = rows * (thumbW + gap); - - let maxScroll = contentHeight - canvas.height; - if (maxScroll < 0) maxScroll = 0; - - if (scrollTop < 0) scrollTop = 0; - if (scrollTop > maxScroll) scrollTop = maxScroll + 10; - - render(); -}); - -canvas.addEventListener("click", handleClick); - -img.onload = () => { - render(); -}; - function getMousePos(e) { const rect = canvas.getBoundingClientRect(); return { @@ -39,49 +17,73 @@ function getMousePos(e) { }; } -function handleClick(e) { - const pos = getMousePos(e); - const mouseX = pos.x; - const mouseY = pos.y; +function unselectImage() { + selectedIndex = -1; + canvas.removeEventListener("click", unselectImage); + canvas.addEventListener("click", handleClick); + render(); +} - for (let i = 0; i < count; i++) { +function handleClick(e) { + const { x: mouseX, y: mouseY } = getMousePos(e); + + for (let i = 0; i < totalImageCount; i++) { const row = Math.floor(i / columns); const col = i % columns; - const x = col * (thumbW + gap); - const y = row * (thumbH + gap) - scrollTop; + const x = col * imageWidth; + const y = row * imageHeight - scrollTop; if ( mouseX >= x && - mouseX <= x + thumbW && + mouseX <= x + imageWidth && mouseY >= y && - mouseY <= y + thumbH + mouseY <= y + imageHeight ) { selectedIndex = i; + canvas.removeEventListener("click", handleClick); + canvas.addEventListener("click", unselectImage); render(); return; } } } -function render() { - ctx.clearRect(0, 0, canvas.width, canvas.height); +canvas.addEventListener("click", handleClick); - for (let i = 0; i < count; i++) { - const totalWidth = columns * (thumbW + gap) - gap; - const startX = (canvas.width - totalWidth) / 2; +canvas.addEventListener("wheel", (e) => { + scrollTop += e.deltaY; + + const rows = Math.ceil(totalImageCount / columns); + const contentHeight = rows * imageHeight; + + const maxScroll = Math.max(0, contentHeight - canvas.height); + + scrollTop = Math.min(Math.max(scrollTop, 0), maxScroll); + + render(); +}); + +const img = new Image(); +img.src = "./images/1.jpg"; +img.onload = () => render(); + +function render() { + c.clearRect(0, 0, canvas.width, canvas.height); + + if (selectedIndex >= 0) { + c.drawImage(img, 0, 0, canvas.width, canvas.height); + return; + } + + for (let i = 0; i < totalImageCount; i++) { const row = Math.floor(i / columns); const col = i % columns; - const x = startX + col * (thumbW + gap); - const y = row * (thumbH + gap) - scrollTop; + const x = col * imageWidth; + const y = row * imageHeight - scrollTop; - ctx.drawImage(img, x, y + 10, thumbW, thumbH); - - if (i === selectedIndex) { - ctx.strokeStyle = "red"; - ctx.lineWidth = 4; - ctx.strokeRect(x, y + 10, thumbW, thumbH); - } + c.drawImage(img, x, y, imageWidth, imageHeight); } } +