Web Dev Solutions

Catalin Mititiuc

function isEven(n) { return n % 2 === 0; } function toFixed(n) { return Number.parseFloat(n).toFixed(2); } function radToDeg(radians) { return radians * 180 / Math.PI; } function edgePoint(x1, y1, x2, y2, maxX, maxY) { let pointCoords, xDiff = x2 - x1, yDiff = y2 - y1, xIntercept = y => (y - y1) * xDiff / yDiff + x1, yIntercept = x => (x - x1) * yDiff / xDiff + y1; if (xDiff > 0 && yDiff > 0) { let x = xIntercept(maxY); pointCoords = x <= maxX ? [x, maxY] : [maxX, yIntercept(maxX)]; } else if (xDiff > 0 && yDiff < 0) { let y = yIntercept(maxX); pointCoords = y >= 0 ? [maxX, y] : [xIntercept(0), 0]; } else if (xDiff < 0 && yDiff < 0) { let x = xIntercept(0); pointCoords = x >= 0 ? [x, 0] : [0, yIntercept(0)]; } else { let y = yIntercept(0); pointCoords = y <= maxY ? [0, y] : [xIntercept(maxY), maxY]; } return pointCoords; } let getPointCoords = (x, y) => { let point = document.querySelector(`[data-x="${x}"][data-y="${y}"]`); return [point.x.baseVal.value, point.y.baseVal.value] }; let svgns = "http://www.w3.org/2000/svg", svg = document.querySelector('svg'), rect = document.querySelector('rect#map'); const COLUMN_COUNT = 33, ROW_COUNT = 25, HORZ_POINT_DISTANCE = 1.005, VERT_POINT_DISTANCE = Math.sqrt(3) * HORZ_POINT_DISTANCE / 2, ALTERNATING_OFFSET = HORZ_POINT_DISTANCE / 2, [X_OFFSET, Y_OFFSET] = [0.25, 0.45], [COLUMNS, ROWS] = [COLUMN_COUNT, ROW_COUNT].map(n => [...Array(n).keys()]), POINTS = ROWS.map(y => COLUMNS.map(x => [x, y])); const FIRING_ARC_SIZE = { 'small': Math.atan(HORZ_POINT_DISTANCE / (6 * VERT_POINT_DISTANCE)), 'medium': Math.atan((HORZ_POINT_DISTANCE / 2) / VERT_POINT_DISTANCE), 'large': Math.atan((21 * HORZ_POINT_DISTANCE) / (6 * VERT_POINT_DISTANCE)) } POINTS.forEach((row, index) => row.forEach(([x, y]) => { var cx = x * HORZ_POINT_DISTANCE + X_OFFSET + (isEven(index) ? ALTERNATING_OFFSET : 0), cy = y * HORZ_POINT_DISTANCE * VERT_POINT_DISTANCE + Y_OFFSET, point = document.createElementNS(svgns, 'use'); point.setAttributeNS(null, 'href', `#point`); point.setAttributeNS(null, 'x', `${cx}in`); point.setAttributeNS(null, 'y', `${cy}in`); point.dataset.x = x; point.dataset.y = y; point.addEventListener('click', e => { let selectedSoldier = document.querySelector('.soldier-record.selected'); if (selectedSoldier) { let counter = document.createElementNS(svgns, 'circle'); let text = document.createElementNS(svgns, 'text'); counter.setAttributeNS(null, 'cx', `${cx}in`); counter.setAttributeNS(null, 'cy', `${cy}in`); counter.setAttributeNS(null, 'r', '0.25in'); counter.dataset.troopNumber = selectedSoldier.dataset.troopNumber; counter.dataset.troopAllegiance = selectedSoldier.dataset.troopAllegiance; counter.classList.add('counter'); text.setAttributeNS(null, 'text-anchor', 'middle'); text.setAttributeNS(null, 'x', `${cx}in`); text.setAttributeNS(null, 'y', `${cy + 0.25}in`); text.dataset.troopNumber = selectedSoldier.dataset.troopNumber; text.dataset.troopAllegiance = selectedSoldier.dataset.troopAllegiance; text.textContent = `${selectedSoldier.dataset.troopNumber}`; text.classList.add('counter'); document.querySelectorAll( `.counter[data-troop-number="${selectedSoldier.dataset.troopNumber}"][data-troop-allegiance="${selectedSoldier.dataset.troopAllegiance}"]` ).forEach(el => el.remove()); svg.appendChild(counter); svg.appendChild(text); } }); svg.appendChild(point); })); rect.addEventListener('mousemove', e => { var rect = e.target.getBoundingClientRect(); var pointerX = e.clientX - rect.left; var pointerY = e.clientY - rect.top; let [maxXpx, maxYpx] = [e.target.width, e.target.height].map(v => v.baseVal.value); // console.log('x', `${toFixed(x / rect.width * maxX)}"`, 'y', `${toFixed(y / rect.height * maxY)}"`); let p = document.querySelector('polygon.firing-arc.active'); // TODO: handle exactly horizontal and vertical lines if (p) { let {x: x1px, y: y1px} = p.points[1]; let [x2px, y2px] = [ pointerX / rect.width * maxXpx, pointerY / rect.height * maxYpx ]; let xDiff = x2px - x1px; let yDiff = -(y2px - y1px); let angle = Math.abs(Math.atan(yDiff / xDiff)); if (xDiff < 0 && yDiff > 0) { angle = Math.PI - angle; } else if (xDiff < 0 && yDiff < 0) { angle = Math.PI + angle; } else if (xDiff > 0 && yDiff < 0) { angle = 2 * Math.PI - angle; } console.log('angle:', `${toFixed(radToDeg(angle))}\u00B0`, `${angle}rad`); let arcAngle = FIRING_ARC_SIZE[p.dataset.size]; let distance = Math.sqrt((x2px - x1px)**2 + (y2px - y1px)**2); let yDelta = distance * Math.cos(angle) * Math.tan(arcAngle); let xDelta = distance * Math.sin(angle) * Math.tan(arcAngle); let [newY1, newX1] = [y2px + yDelta, x2px + xDelta]; let [newY2, newX2] = [y2px - yDelta, x2px - xDelta]; [newX1, newY1] = edgePoint(x1px, y1px, newX1, newY1, maxXpx, maxYpx); [newX2, newY2] = edgePoint(x1px, y1px, newX2, newY2, maxXpx, maxYpx); let oppositeEdgeConditions = [ newX1 == 0 && newX2 == maxXpx, newX2 == 0 && newX1 == maxXpx, newY1 == 0 && newY2 == maxYpx, newY2 == 0 && newY1 == maxYpx ] let orthogonalEdgeConditions = [ (newX1 == 0 || newX1 == maxXpx) && (newY2 == 0 || newY2 == maxYpx), (newX2 == 0 || newX2 == maxXpx) && (newY1 == 0 || newY1 == maxYpx), ] let points; xDiff = x2px - x1px; yDiff = y2px - y1px; if (oppositeEdgeConditions.some(e => e)) { let cornerPoints; if (xDiff > 0 && yDiff > 0) { if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { cornerPoints = [[maxXpx, 0], [maxXpx, maxYpx]]; } else { cornerPoints = [[maxXpx, maxYpx], [0, maxYpx]]; } points = `${newX1},${newY1} ${x1px},${y1px} ${newX2},${newY2} ${cornerPoints[0]} ${cornerPoints[1]}`; } else if (xDiff > 0 && yDiff < 0) { if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { cornerPoints = [[maxXpx, 0], [maxXpx, maxYpx]]; } else { cornerPoints = [[0, 0], [maxXpx, 0]]; } points = `${newX1},${newY1} ${x1px},${y1px} ${newX2},${newY2} ${cornerPoints[0]} ${cornerPoints[1]}`; } else if (xDiff < 0 && yDiff > 0) { if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { cornerPoints = [[0, maxYpx], [0, 0]]; } else { cornerPoints = [[maxXpx, maxYpx], [0, maxYpx]]; } points = `${newX1},${newY1} ${x1px},${y1px} ${newX2},${newY2} ${cornerPoints[0]} ${cornerPoints[1]}`; } else { if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { cornerPoints = [[0, maxYpx], [0, 0]]; } else { cornerPoints = [[0, 0], [maxXpx, 0]]; } points = `${newX1},${newY1} ${x1px},${y1px} ${newX2},${newY2} ${cornerPoints[0]} ${cornerPoints[1]}`; } } else if (orthogonalEdgeConditions.some(e => e)) { let cornerPoint; if (newX1 == 0 || newX1 == maxXpx) { cornerPoint = [newX1, newY2]; } else { cornerPoint = [newX2, newY1]; } points = `${newX1},${newY1} ${x1px},${y1px} ${newX2},${newY2} ${cornerPoint}`; } else { points = `${newX1},${newY1} ${x1px},${y1px} ${newX2},${newY2}`; } p.setAttributeNS(null, 'points', points); } }); document.querySelectorAll('.soldier-record').forEach(el => el.addEventListener('click', e => { if (e.target.classList.contains('selected')) { e.target.classList.remove('selected'); } else { document.querySelectorAll('.soldier-record.selected').forEach(el => el.classList.remove('selected') ); e.target.classList.add('selected'); } }) ); document.querySelectorAll('.set-firing-arc').forEach(el => el.addEventListener('click', e => { let selectedSoldier = document.querySelector('.soldier-record.selected'); if (selectedSoldier) { let {troopNumber, troopAllegiance} = selectedSoldier.dataset; let existingArcs = document.querySelectorAll( `.firing-arc[data-troop-number="${troopNumber}"][data-troop-allegiance="${troopAllegiance}"]` ); if (existingArcs.length > 0) { existingArcs.forEach(el => el.remove()); } let counter = document.querySelector( `circle.counter[data-troop-number="${troopNumber}"][data-troop-allegiance="${troopAllegiance}"]` ); if (counter) { let arcLayer = document.getElementById('firing-arcs'); let placeFiringArcListener = e => { document.querySelectorAll('.firing-arc.active').forEach(el => el.classList.remove('active')); document.querySelector('circle#point').style.display = ''; rect.removeEventListener('click', placeFiringArcListener); }; rect.addEventListener('click', placeFiringArcListener); let pivotPoint = [counter.cx.baseVal.value, counter.cy.baseVal.value]; let firingArc = document.createElementNS(svgns, 'polygon'); firingArc.classList.add('firing-arc', 'active'); firingArc.dataset.troopNumber = troopNumber; firingArc.dataset.troopAllegiance = troopAllegiance; firingArc.dataset.size = e.target.dataset.size; firingArc.setAttributeNS(null, 'points', `${pivotPoint} ${pivotPoint} ${pivotPoint}`); arcLayer.prepend(firingArc); document.querySelector('circle#point').style.display = 'none'; } } }));