Web Dev Solutions

Catalin Mititiuc

From e6fcfb4ac90df3e3d9f6079c9db69808e78fbcd6 Mon Sep 17 00:00:00 2001 From: Catalin Mititiuc Date: Wed, 20 Mar 2024 14:54:19 -0700 Subject: WIP: firing arcs: draw line to edge of map --- index.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- style.css | 1 + 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index ecfba46..f233dbf 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var rect = document.querySelector('rect#map'); var toFixed = n => Number.parseFloat(n).toFixed(2); +var radToDeg = radians => radians * 180 / Math.PI; var svgns = "http://www.w3.org/2000/svg", svg = document.querySelector('svg'); @@ -9,16 +10,81 @@ rect.addEventListener('mousemove', e => { var x = e.clientX - rect.left; // x position within the element var y = e.clientY - rect.top; // y position within the element - console.log( - 'x: ' + toFixed(x / rect.width * e.target.width.baseVal.valueInSpecifiedUnits) + '"', - 'y: ' + toFixed(y / rect.height * e.target.height.baseVal.valueInSpecifiedUnits) + '"' - ); + // console.log( + // 'x: ' + toFixed(x / rect.width * e.target.width.baseVal.valueInSpecifiedUnits) + '"', + // 'y: ' + toFixed(y / rect.height * e.target.height.baseVal.valueInSpecifiedUnits) + '"' + // ); let aim = document.querySelector('.firing-arc.active'); if (aim) { aim.setAttributeNS(null, 'x2', `${x / rect.width * e.target.width.baseVal.valueInSpecifiedUnits}in`); aim.setAttributeNS(null, 'y2', `${y / rect.height * e.target.height.baseVal.valueInSpecifiedUnits}in`); + + let w = aim.x2.baseVal.value - aim.x1.baseVal.value; + let h = -(aim.y2.baseVal.value - aim.y1.baseVal.value); + let angle = radToDeg(Math.atan(h / w)); + + if (w < 0 && h > 0) { + angle = 180 - Math.abs(angle); + } else if (w < 0 && h < 0) { + angle = 180 + Math.abs(angle); + } else if (w > 0 && h < 0) { + angle = 360 - Math.abs(angle); + } + + console.log(`${toFixed(angle)}\u00B0`); + + let [x1, y1, x2, y2] = [aim.x1, aim.y1, aim.x2, aim.y2].map(v => v.baseVal.valueInSpecifiedUnits); + let [width, height] = [e.target.width, e.target.height].map(v => v.baseVal.valueInSpecifiedUnits); + + console.log(`(${x2 - x1}, ${y2 - y1})`); + + if (x2 - x1 > 0 && y2 - y1 > 0) { + let yWhenXisMax = (width - x1) * (y2 - y1) / (x2 - x1) + y1; + let xWhenYisMax = (height - y1) * (x2 - x1) / (y2 - y1) + x1; + + if (xWhenYisMax <= width) { + aim.setAttributeNS(null, 'x2', `${xWhenYisMax}in`); + aim.setAttributeNS(null, 'y2', `${height}in`); + } else { + aim.setAttributeNS(null, 'x2', `${width}in`); + aim.setAttributeNS(null, 'y2', `${yWhenXisMax}in`); + } + } else if (x2 - x1 > 0 && y2 - y1 < 0) { + let yWhenXisMax = (width - x1) * (y2 - y1) / (x2 - x1) + y1; + let xWhenYisZero = (0 - y1) * (x2 - x1) / (y2 - y1) + x1; + + if (xWhenYisZero <= width) { + aim.setAttributeNS(null, 'x2', `${xWhenYisZero}in`); + aim.setAttributeNS(null, 'y2', `0`); + } else { + aim.setAttributeNS(null, 'x2', `${width}in`); + aim.setAttributeNS(null, 'y2', `${yWhenXisMax}in`); + } + } else if (x2 - x1 < 0 && y2 - y1 < 0) { + let yWhenXisZero = (0 - x1) * (y2 - y1) / (x2 - x1) + y1; + let xWhenYisZero = (0 - y1) * (x2 - x1) / (y2 - y1) + x1; + + if (yWhenXisZero >= 0) { + aim.setAttributeNS(null, 'x2', `0`); + aim.setAttributeNS(null, 'y2', `${yWhenXisZero}in`); + } else { + aim.setAttributeNS(null, 'x2', `${xWhenYisZero}in`); + aim.setAttributeNS(null, 'y2', `0`); + } + } else { + let yWhenXisZero = (0 - x1) * (y2 - y1) / (x2 - x1) + y1; + let xWhenYisMax = (height - y1) * (x2 - x1) / (y2 - y1) + x1; + + if (yWhenXisZero <= height) { + aim.setAttributeNS(null, 'x2', `0`); + aim.setAttributeNS(null, 'y2', `${yWhenXisZero}in`); + } else { + aim.setAttributeNS(null, 'x2', `${xWhenYisMax}in`); + aim.setAttributeNS(null, 'y2', `${height}in`); + } + } } }); @@ -60,6 +126,7 @@ document.querySelector('#set-firing-arc').addEventListener('click', e => { aim.setAttributeNS(null, 'y1', counter.cy.baseVal.valueAsString); aim.setAttributeNS(null, 'x2', counter.cx.baseVal.valueAsString); aim.setAttributeNS(null, 'y2', counter.cy.baseVal.valueAsString); + // aim.style.transformOrigin = `${counter.cx.baseVal.valueAsString} ${counter.cy.baseVal.valueAsString}`; svg.appendChild(aim); @@ -140,19 +207,27 @@ let getPointCoords = (x, y) => { let smallArc = document.createElementNS(svgns, 'polygon'); +smallArc.setAttributeNS(null, 'id', 'small-arc'); let smallArcPoints = `${getPointCoords(1, 24)} ${getPointCoords(1, 21)} ${getPointCoords(2, 21)}`; smallArc.setAttributeNS(null, 'points', smallArcPoints); svg.appendChild(smallArc); let medArc = document.createElementNS(svgns, 'polygon'); +medArc.setAttributeNS(null, 'id', 'med-arc'); let medArcPoints = `${getPointCoords(4, 24)} ${getPointCoords(3, 21)} ${getPointCoords(6, 21)}`; medArc.setAttributeNS(null, 'points', medArcPoints); svg.appendChild(medArc); let lrgArc = document.createElementNS(svgns, 'polygon'); +lrgArc.setAttributeNS(null, 'id', 'lrg-arc'); let lrgArcPoints = `${getPointCoords(19, 24)} ${getPointCoords(9, 21)} ${getPointCoords(30, 21)}`; lrgArc.setAttributeNS(null, 'points', lrgArcPoints); -svg.appendChild(lrgArc); \ No newline at end of file +svg.appendChild(lrgArc); + +arc = document.querySelector('#med-arc'); +let [ox, oy] = getPointCoords(4, 24); +arc.style.transformOrigin = `${ox}px ${oy}px`; +arc.style.transform = 'rotate(90deg)'; \ No newline at end of file diff --git a/style.css b/style.css index c2d0db8..5d3598c 100644 --- a/style.css +++ b/style.css @@ -39,6 +39,7 @@ polygon { line.firing-arc { stroke: black; + /* transform: scale(2); */ } image#img1 { -- cgit v1.2.3