Clone of an image

2

I have the code that moves images in SVG with the mouse. When I move the image, as soon as it is released, I want your clone to appear in the position the first one started and that it is also possible to move it. I have an example here, with the clone code that does not work for me:

var newimage = document.getElementById('img').cloneNode(true);

Example: link

    
asked by anonymous 21.03.2015 / 16:42

2 answers

2

You can clone directly when you "grab" an element, in mousedown .

In this case all you need is a new line:

ddData.element.parentNode.appendChild(ddData.element.cloneNode(true));

Example: link

Ps: I added one more check here because of errors I was having when the click was not in an image:

if (!ddData.element || ddData.element.tagName == 'svg') return ddData.element = null;
    
21.03.2015 / 19:08
2

You almost got it right, it was just wrong to give appendChild, you are trying to give appendChild in the body, when append should append in svg root, try the following:

HTML :

<svg width="90%" height=500px id="svgRoot">
    <image xlink:href=/favicon.png id="img" x=0 y=0 height=20 width=20 />
<svg>

Javascript :

function clone() {
    var root = document.getElementById('svgRoot');
    var newimage = document.getElementById('img').cloneNode(true);

    newimage.setAttribute("x", ddData.initialX);
    newimage.setAttribute("y", ddData.initialY);
    newimage.style.position = 'absolute';
    newimage.id = 'novoSVG';

    root.appendChild(newimage);
}

@Edit: Functional JSFiddle: link

    
21.03.2015 / 19:05