refactor: separate scripts

This commit is contained in:
2026-06-23 22:02:59 +05:00
parent 630a33ee7c
commit 37204f0de4
10 changed files with 480 additions and 454 deletions
+45
View File
@@ -0,0 +1,45 @@
let dragIndex = -1;
function svgPoint(svg, clientX, clientY) {
const pt = svg.createSVGPoint();
pt.x = clientX;
pt.y = clientY;
return pt.matrixTransform(svg.getScreenCTM().inverse());
}
export function initDrag(svg, state, onDragEnd) {
function updateDragPosition(e) {
if (dragIndex < 0) return;
e.preventDefault();
const pt = svgPoint(svg, e.clientX, e.clientY);
state.corners[dragIndex].x = Math.round(Math.max(0, Math.min(state.fieldW, pt.x)));
state.corners[dragIndex].y = Math.round(Math.max(0, Math.min(state.fieldH, pt.y)));
const circles = svg.querySelectorAll('.corner-circle');
const c = state.corners[dragIndex];
circles[dragIndex].setAttribute('cx', c.x);
circles[dragIndex].setAttribute('cy', c.y);
const poly = svg.querySelector('polygon');
const pts = state.corners.map(p => `${p.x},${p.y}`).join(' ');
poly.setAttribute('points', pts);
}
function endDrag() {
dragIndex = -1;
svg.classList.remove('dragging');
document.removeEventListener('pointermove', updateDragPosition);
document.removeEventListener('pointerup', endDrag);
onDragEnd();
}
svg.addEventListener('pointerdown', (e) => {
const target = e.target.closest('.corner-circle');
if (!target) return;
e.preventDefault();
dragIndex = parseInt(target.dataset.index);
svg.classList.add('dragging');
document.addEventListener('pointermove', updateDragPosition);
document.addEventListener('pointerup', endDrag);
});
}