NoobOnStack
NoobOnStack

Reputation: 105

Clone a SVG Rectangle with SVG.js

Hello i am trying to clone this SVG rectangle with the svg.js library. I tried it like this but it does not seem to work or do i have to do something else so it actually appears/gets drawn?

 var rect = draw.rect(1650, 850).move(100, 20).attr ({ 
    fill: 'white',
    stroke: '#000',
    'stroke-width': 5
    })

    var clone = rect.clone(rect);

Looking forward to your answers!

Upvotes: 1

Views: 617

Answers (1)

Ian
Ian

Reputation: 13852

I think the problem here, is that you are passing a parameter to the clone method. If you pass a parameter to it, it thinks that's the parent.

So the code is trying to clone the rect, and add the clone to the rect element.

Problem is, a rect is not a container element, so doesn't have children, and can't be added.

So you need to pass in a different parent if you want it (like a g or svg element), or not include the parameter at all, and just call clone(), which should place it after the existing rect element.

Upvotes: 1

Related Questions