function startResize(e, handle) {
isResizing = true;
currentHandle = handle;
startX = (e.touches ? e.touches[0].clientX : e.clientX);
startY = (e.touches ? e.touches[0].clientY : e.clientY);
startWidth = parseInt(document.defaultView.getComputedStyle(selectionBox).width, 10);
startHeight = parseInt(document.defaultView.getComputedStyle(selectionBox).height, 10);
startLeft = selectionBox.offsetLeft;
startTop = selectionBox.offsetTop;
e.preventDefault();
}
function resize(e) {
if (!isResizing) return;
let clientX = (e.touches ? e.touches[0].clientX : e.clientX);
let clientY = (e.touches ? e.touches[0].clientY : e.clientY);
let width, height, left, top;
const containerRect = imageContainer.getBoundingClientRect();
if (currentHandle.classList.contains('nw')) {
width = startWidth - (clientX - startX);
height = startHeight - (clientY - startY);
left = startLeft + (clientX - startX);
top = startTop + (clientY - startY);
if (width > 10 && height > 10 && left >= 0 && top >= 0) {
selectionBox.style.width = width + 'px';
selectionBox.style.height = height + 'px';
selectionBox.style.left = left + 'px';
selectionBox.style.top = top + 'px';
}
} else if (currentHandle.classList.contains('ne')) {
width = startWidth + (clientX - startX);
height = startHeight - (clientY - startY);
top = startTop + (clientY - startY);
if (width > 10 && height > 10 && (startLeft + width) <= containerRect.width && top >= 0) {
selectionBox.style.width = width + 'px';
selectionBox.style.height = height + 'px';
selectionBox.style.top = top + 'px';
}
} else if (currentHandle.classList.contains('sw')) {
width = startWidth - (clientX - startX);
height = startHeight + (clientY - startY);
left = startLeft + (clientX - startX);
if (width > 10 && height > 10 && left >= 0 && (startTop + height) <= containerRect.height) {
selectionBox.style.width = width + 'px';
selectionBox.style.height = height + 'px';
selectionBox.style.left = left + 'px';
}
} else if (currentHandle.classList.contains('se')) {
width = startWidth + (clientX - startX);
height = startHeight + (clientY - startY);
if (width > 10 && height > 10 && (startLeft + width) <= containerRect.width && (startTop + height) <= containerRect.height) {
selectionBox.style.width = width + 'px';
selectionBox.style.height = height + 'px';
}
}
e.preventDefault();
}
function stopResize() {
if (isResizing) {
isResizing = false;
}
}
resizeHandles.forEach(handle => {
handle.addEventListener('mousedown', (e) => startResize(e, handle));
handle.addEventListener('touchstart', (e) => startResize(e, handle), { passive: false });
});
window.addEventListener('mousemove', resize);
window.addEventListener('touchmove', resize, { passive: false });
window.addEventListener('mouseup', stopResize);
window.addEventListener('touchend', stopResize);
const screenshotButton = document.getElementById('screenshot-button');
screenshotButton.addEventListener('click', function () {
// This is where you can handle the screenshot logic
console.log('Selected area:', selectionBox.getBoundingClientRect());
});
这个网页需要在手机端上使用,但是对于如何更改选取框的位置和大小没有思路。现在生成的选取框是无法拖动的。