WIP: firing arcs: draw line to edge of map
This commit is contained in:
parent
8ebaacfbf6
commit
e6fcfb4ac9
85
index.js
85
index.js
@ -1,5 +1,6 @@
|
|||||||
var rect = document.querySelector('rect#map');
|
var rect = document.querySelector('rect#map');
|
||||||
var toFixed = n => Number.parseFloat(n).toFixed(2);
|
var toFixed = n => Number.parseFloat(n).toFixed(2);
|
||||||
|
var radToDeg = radians => radians * 180 / Math.PI;
|
||||||
|
|
||||||
var svgns = "http://www.w3.org/2000/svg",
|
var svgns = "http://www.w3.org/2000/svg",
|
||||||
svg = document.querySelector('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 x = e.clientX - rect.left; // x position within the element
|
||||||
var y = e.clientY - rect.top; // y position within the element
|
var y = e.clientY - rect.top; // y position within the element
|
||||||
|
|
||||||
console.log(
|
// console.log(
|
||||||
'x: ' + toFixed(x / rect.width * e.target.width.baseVal.valueInSpecifiedUnits) + '"',
|
// 'x: ' + toFixed(x / rect.width * e.target.width.baseVal.valueInSpecifiedUnits) + '"',
|
||||||
'y: ' + toFixed(y / rect.height * e.target.height.baseVal.valueInSpecifiedUnits) + '"'
|
// 'y: ' + toFixed(y / rect.height * e.target.height.baseVal.valueInSpecifiedUnits) + '"'
|
||||||
);
|
// );
|
||||||
|
|
||||||
let aim = document.querySelector('.firing-arc.active');
|
let aim = document.querySelector('.firing-arc.active');
|
||||||
|
|
||||||
if (aim) {
|
if (aim) {
|
||||||
aim.setAttributeNS(null, 'x2', `${x / rect.width * e.target.width.baseVal.valueInSpecifiedUnits}in`);
|
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`);
|
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, 'y1', counter.cy.baseVal.valueAsString);
|
||||||
aim.setAttributeNS(null, 'x2', counter.cx.baseVal.valueAsString);
|
aim.setAttributeNS(null, 'x2', counter.cx.baseVal.valueAsString);
|
||||||
aim.setAttributeNS(null, 'y2', counter.cy.baseVal.valueAsString);
|
aim.setAttributeNS(null, 'y2', counter.cy.baseVal.valueAsString);
|
||||||
|
// aim.style.transformOrigin = `${counter.cx.baseVal.valueAsString} ${counter.cy.baseVal.valueAsString}`;
|
||||||
|
|
||||||
svg.appendChild(aim);
|
svg.appendChild(aim);
|
||||||
|
|
||||||
@ -140,19 +207,27 @@ let getPointCoords = (x, y) => {
|
|||||||
|
|
||||||
|
|
||||||
let smallArc = document.createElementNS(svgns, 'polygon');
|
let smallArc = document.createElementNS(svgns, 'polygon');
|
||||||
|
smallArc.setAttributeNS(null, 'id', 'small-arc');
|
||||||
let smallArcPoints = `${getPointCoords(1, 24)} ${getPointCoords(1, 21)} ${getPointCoords(2, 21)}`;
|
let smallArcPoints = `${getPointCoords(1, 24)} ${getPointCoords(1, 21)} ${getPointCoords(2, 21)}`;
|
||||||
smallArc.setAttributeNS(null, 'points', smallArcPoints);
|
smallArc.setAttributeNS(null, 'points', smallArcPoints);
|
||||||
|
|
||||||
svg.appendChild(smallArc);
|
svg.appendChild(smallArc);
|
||||||
|
|
||||||
let medArc = document.createElementNS(svgns, 'polygon');
|
let medArc = document.createElementNS(svgns, 'polygon');
|
||||||
|
medArc.setAttributeNS(null, 'id', 'med-arc');
|
||||||
let medArcPoints = `${getPointCoords(4, 24)} ${getPointCoords(3, 21)} ${getPointCoords(6, 21)}`;
|
let medArcPoints = `${getPointCoords(4, 24)} ${getPointCoords(3, 21)} ${getPointCoords(6, 21)}`;
|
||||||
medArc.setAttributeNS(null, 'points', medArcPoints);
|
medArc.setAttributeNS(null, 'points', medArcPoints);
|
||||||
|
|
||||||
svg.appendChild(medArc);
|
svg.appendChild(medArc);
|
||||||
|
|
||||||
let lrgArc = document.createElementNS(svgns, 'polygon');
|
let lrgArc = document.createElementNS(svgns, 'polygon');
|
||||||
|
lrgArc.setAttributeNS(null, 'id', 'lrg-arc');
|
||||||
let lrgArcPoints = `${getPointCoords(19, 24)} ${getPointCoords(9, 21)} ${getPointCoords(30, 21)}`;
|
let lrgArcPoints = `${getPointCoords(19, 24)} ${getPointCoords(9, 21)} ${getPointCoords(30, 21)}`;
|
||||||
lrgArc.setAttributeNS(null, 'points', lrgArcPoints);
|
lrgArc.setAttributeNS(null, 'points', lrgArcPoints);
|
||||||
|
|
||||||
svg.appendChild(lrgArc);
|
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)';
|
Loading…
x
Reference in New Issue
Block a user