Hello, I have a function that captures coordinates of elements that I move on the screen with the mouse. But this function of mine takes the trace of the coordinates of the elements that I drag, I would like to take only the last coordinate of each one, to save in the bank. How could I treat the array, so that I just get the last coordinate in relation to the id?
Function that takes the coordinates:
moveElem(elemID) {
let self = this;
function parseNumber(num) {
return parseFloat(num.replace(/[^\d]/)) || 0;
}
let moveElemento = (function () {
let coords = [];
let startX;
let startY;
let currentElemento = null;
let currentWidth = 0;
let currentHeight = 0;
let currentLeft = 0;
let currentTop = 0;
let callMoveOnElemento = null;
let callMoveStopElemento = null;
let contentMove = '.circle' + elemID;
let move = false;
let marginStop = 30;
let maxWidth = window.innerWidth - marginStop;
let maxHeight = window.innerHeight - marginStop;
$(contentMove).on('mousedown', function (e) {
currentElemento = this.parentNode;
currentLeft = parseNumber(currentElemento.style.left);
currentTop = parseNumber(currentElemento.style.top);
startX = e.clientX;
startY = e.clientY;
if (typeof (callMoveOnElemento) == 'function')
callMoveOnElemento(currentElemento);
move = true;
});
$(document).on('mouseup', function () {
if (currentElemento == null) return;
if (typeof (callMoveStopElemento) == 'function')
callMoveStopElemento(currentElemento);
currentElemento = null;
move = false;
})
$(document).on('mousemove', function (e) {
if (move == true) {
let newX = currentLeft + e.clientX - startX;
let newY = currentTop + e.clientY - startY;
if (marginStop > e.clientX) return;
if (marginStop > e.clientY) return;
if (maxWidth < e.clientX) return;
if (maxHeight < e.clientY) return;
$(currentElemento).css({
'left': newX,
'top': newY
});
coords = [{ "id": elemID, "latitude": newY, "longitude": newX }];
self.trabalharRespostaFuncao(coords);
}
});
return function (func1, func2) {
callMoveOnElemento = func1;
callMoveStopElemento = func2;
}
})();
}
Function to prepare to fill the array of coordinates
private trabalharRespostaFuncao(coords) {
for (var coord of coords) {
this.popularListaCoordenadas(coord);
}
}
Function to feed coordinate array
private popularListaCoordenadas(coord) {
this.coordenadas.push({
id: coord.id, latitude: coord.latitude, longitude: coord.longitude
});
}
I am open to suggestions, thank you in advance.