cloning svg group

Go To StackoverFlow.com

0

I want to copy an svg group but I really dont know what I missed.Here the code : For the group:

<g id="exCar" x="50" y="500"  transform="" >
   <path ..
   <path ... 
   <image
   <text ..
</g>

And function to copy:

function cloning(){
     var newCar = document.getElementById("exCar").cloneNode(true);
     document.getElementById("newCar").setAttribute("x",250);
     document.getElementById("newCar").setAttribute("y",600);
     document.getElementById("exCar").appendChild(newCar);
     alert("!!!!");
    };

Please help me to understand what is wrong..

2012-04-03 21:25
by Ecrin


0

Instead of

document.getElementById("newCar").setAttribute("x",250);

It should be:

newCar.setAttribute("x", 250)
2012-04-03 22:02
by mihai
Of just that .. Thank you.Can I ask you something more? I transformed 'excar' to scale.And I want to use 'newCar' same shape with the ex but not transformed.Is it possible?Does the new carry the features of ex - Ecrin 2012-04-03 22:06
You have to clone exCar before transforming it, so that newCar will not carry the features. cloneNode creates a deep copy of the object, so once you clone it it will have all it's properties up to that point - mihai 2012-04-03 22:20
Ok.I only need the shape of the exCar after scaled.I will use newCar(with the shape of ex) to move.Is there any way to get its shape by loading image or anything else?Because I cant use exCar for movement .Transformed matrix confused everythin - Ecrin 2012-04-03 22:23
Although what you're saying is a bit confusing, I think the mistake you are making is that you append newCar within the same group as exCar, so they share the same transformation matrix. You should be appending newCar to exCar.parentNod - mihai 2012-04-03 22:34
I thought this and append the newCar to svg document instead of exCar.But still it is using exCar's matrix.Because I am changing newCar's x,y coordinates but it is using the exCar's matrix and locating over the exCar.Sorry I know it is looking confusing.But i want to only use a new car looking like the scaled one: - Ecrin 2012-04-03 22:38
With this code :document.getElementById("svgDoc").appendChild(newCar) - Ecrin 2012-04-03 22:41
Perhaps if you post another question with more code I can be more helpfull, otherwise I'm confused : - mihai 2012-04-03 22:54
Ads