Identify the positions of objects

1

I have two images that I move, and that I store its position x and y in an array. I create a clone of each image, and when I parse to move (onmouseup) I keep its position. I give the same id to each generated image so I can know which one. Without creating two arrays, I wanted to know how to identify them in the array, what is the position of the object.

Example: link

    
asked by anonymous 25.03.2015 / 11:50

1 answer

1

I suggest an adjustment to the function where you create the clones. Something like this:

function mousedown(e) {
    e.preventDefault();
    var evt = e || window.event;
    var el = ddData.element = evt.target || evt.srcElement;
    var id = ddData.element.id
    if (!el || el.tagName == 'svg' || ddData.movidos[id]) return ddData.element = null;
    var clone = el.cloneNode(true);
    el.id = id + '_' + (ddData.contadorElementos++);
    el.parentNode.appendChild(clone);

In this way each new clone will have the original ID, and the element that drags ( ddData.element ) will have a unique ID formed by the ID of the original element ( img1 or img2 in the example you have) plus one part _x which is the number of ddData.contadorElementos that is always increasing each element generated.

In this way you always have unique IDs and you can go get the element when you need it and whose ID is registered in the keys of your ddData.movidos object.

    
25.03.2015 / 13:29