0
Hello, I have a function that captures coordinates of elements that I move on the screen with the mouse. But this function of mine, it 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 I could handle the array so that I only get the last coordinate relative to the id?
Function that picks up 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 coordinate array
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, I thank you already.
The last coordinate is always the one with the
id
greater ?– Isac
No, each element has a different id. I need to get only the last x, y coordinate of each of these id.
– Maicon Costa
Why not modify the method
popularListaCoordenadas
to use another dictionary only with the last coordinates of each id ?– Isac
That may be, but how do I do it? I still haven’t been able to extract the last coordinates from each id in isolation.
– Maicon Costa