scroll、layout、click ok
This commit is contained in:
parent
cddc95ecc4
commit
a6e45246de
BIN
images/1.jpg
Normal file
BIN
images/1.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.1 KiB |
BIN
images/2.jpg
Normal file
BIN
images/2.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.9 KiB |
98
main.js
98
main.js
@ -1,31 +1,87 @@
|
||||
console.log("load main.js success!");
|
||||
const canvas = document.getElementById("c");
|
||||
const ctx = canvas.getContext("2d");
|
||||
|
||||
function render() {
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
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;
|
||||
|
||||
ctx.fillStyle = "#eee";
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
canvas.addEventListener("wheel", (e) => {
|
||||
scrollTop += e.deltaY;
|
||||
const rows = Math.ceil(count / columns);
|
||||
const contentHeight = rows * (thumbW + gap);
|
||||
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(20, 20);
|
||||
ctx.lineTo(200, 50);
|
||||
ctx.strokeStyle = 'blue';
|
||||
ctx.lineWidth = 4;
|
||||
ctx.stroke();
|
||||
let maxScroll = contentHeight - canvas.height;
|
||||
if (maxScroll < 0) maxScroll = 0;
|
||||
|
||||
ctx.font = "20px Arial";
|
||||
ctx.fillStyle = "black";
|
||||
ctx.fillText("Canvas", 20, 100);
|
||||
if (scrollTop < 0) scrollTop = 0;
|
||||
if (scrollTop > maxScroll) scrollTop = maxScroll + 10;
|
||||
|
||||
requestAnimationFrame(render);
|
||||
render();
|
||||
});
|
||||
|
||||
canvas.addEventListener("click", handleClick);
|
||||
|
||||
img.onload = () => {
|
||||
render();
|
||||
};
|
||||
|
||||
function getMousePos(e) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
return {
|
||||
x: e.clientX - rect.left,
|
||||
y: e.clientY - rect.top,
|
||||
};
|
||||
}
|
||||
|
||||
render();
|
||||
function handleClick(e) {
|
||||
const pos = getMousePos(e);
|
||||
const mouseX = pos.x;
|
||||
const mouseY = pos.y;
|
||||
|
||||
canvas.addEventListener("click", (e) => {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const x = e.clientX - rect.left;
|
||||
const y = e.clientY - rect.top;
|
||||
});
|
||||
for (let i = 0; i < count; i++) {
|
||||
const row = Math.floor(i / columns);
|
||||
const col = i % columns;
|
||||
|
||||
const x = col * (thumbW + gap);
|
||||
const y = row * (thumbH + gap) - scrollTop;
|
||||
|
||||
if (
|
||||
mouseX >= x &&
|
||||
mouseX <= x + thumbW &&
|
||||
mouseY >= y &&
|
||||
mouseY <= y + thumbH
|
||||
) {
|
||||
selectedIndex = i;
|
||||
render();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function render() {
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
const totalWidth = columns * (thumbW + gap) - gap;
|
||||
const startX = (canvas.width - totalWidth) / 2;
|
||||
const row = Math.floor(i / columns);
|
||||
const col = i % columns;
|
||||
|
||||
const x = startX + col * (thumbW + gap);
|
||||
const y = row * (thumbH + gap) - 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user