Best way to move a group of SVG elements
NickName:Arnaud Ask DateTime:2013-05-05T20:29:56

Best way to move a group of SVG elements

I'm looking for the best way (the faster, the cleaner...) to move a group of SVG elements. I have three ways in mind :

  • do a loop on all elements and, for each of us, change the x and y attributes
  • group all elements in a svg element and change its x and y attributes
  • group all elemnts in a g element and apply the method described here : https://stackoverflow.com/a/14036803/2019761

Do you have an idea please ?

Copyright Notice:Content Author:「Arnaud」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16384541/best-way-to-move-a-group-of-svg-elements

Answers
Rik Del Mar 2013-05-08T11:04:48

You can move svg groups or elements with javascript\n\n// translate svg element\nfunction translate( _element , _x , _y )\n{\n var transform = _element.transform.baseVal.getItem(0); \n var mat = transform.matrix; \n\n mat = mat.translate( _x, _y ); \n transform.setMatrix( mat );\n\n}\n\n\nsee it in action:\n\nhttp://www.janvas.com/illustrators_designers_developers/projects/janvas2D_web/examples_EN.php?exampleName=ufo_animation_EN",


Rik Del Mar 2013-05-08T12:43:55

I think that the better way is to move a group of elements.\n\nIf you look the example you can see that the ufo are translated\nand the inner motor rotate inside of it.\n(all moved elements are groups)\n\n<g xmlns=\"http://www.w3.org/2000/svg\" transform=\"matrix(1 0 0 1 -12.5067 69.4101)\" id=\"ufo\">\n <g transform=\"matrix(1 0 0 1 0 -2.842170943040401e-14)\">\n <path transform=\"matrix(1 0 0 1 21.6 2.8)\" width=\"92.34371368613222\" height=\"91.4899957511011\" stroke-width=\"0.83\" stroke-miterlimit=\"3\" stroke=\"none\" fill=\"url(#_1_)\" d=\"M46.1,0 C71.67,0 92… \"/> \n </g>\n <g transform=\"matrix(0.5 0.86 -0.86 0.5 74.6 24.1)\" id=\"motor\">\n <path transform=\"matrix(1 0 0 1 9.7 -2.2)\" width=\"13.11\" height=\"13.5849\" stroke-width=\"0.88\" stroke-miterlimit=\"3\" stroke=\"none\" fill=\"url(#_4_)\" d=\"M6.55,2.8… \"/> \n </g>\n</g>\n",


More about “Best way to move a group of SVG elements” related questions

Best way to move a group of SVG elements

I'm looking for the best way (the faster, the cleaner...) to move a group of SVG elements. I have three ways in mind : do a loop on all elements and, for each of us, change the x and y attributes ...

Show Detail

SVG.js move group containing symbol use element

I'm using the SVG.js library in my project and I'm trying to move a group containing some rect, text and one use element in x direction: // create symbol, symbol can be anything const symbol = svg.

Show Detail

SVG Move group to other group

Want to move a group into another group, but on some way it ends up in a huge execution time, and the script is never ending. the problem is in the foreach loop rest function ok. I don't see any mi...

Show Detail

SVG: border outline for group of elements

I'd like to add a border to a number of elements which are grouped by &lt;g&gt;. As an example: &lt;g id="group"&gt; &lt;circle cx="125" cy="125" r="65" fill="orange"

Show Detail

How to move a polygon (or any other none basic shape) in svg.js?

Let's assume I have created two SVG elements: rect and polygon. I can simply move the rect around via rect.move(x, y) or rect.dmove(x, y) What is the best way to move the polygon? Do I have to cr...

Show Detail

Move all SVG Elements to the upper left corner

I got a SVG-Element and I want to fit it to the content by something similar to this way: var svg = document.getElementsByTagName("svg")[0]; var bb=svg.getBBox(); var bbx=bb.x; var bby=bb.y; var b...

Show Detail

SVG chart move elements which do not fit on the screen

I use svg.js to draw my chart. The chart is very specific, so I can't use some plugin to do it. I have elements, rectangles, on my chart that are too long. The problem is that I can't resize all

Show Detail

Grouping SVG elements into a group while moving

It is necessary to make it possible to move svg elements in the svg editor. The first thought is to change the selected elements transform translate using js and that's it. But with a large number of

Show Detail

Link SVG elements so they move at once?

I would like to "group" SVG points that belong to elements. What I want is very simple, but I am not sure that SVG supports this: &lt;polyline stroke-width="7" stroke="#f4c76b" points="70,0 120,...

Show Detail

How to drag drop elements into svg group

I have few circles on left and a svg group on the right. I want to drag the circles in to svg group on drag drop. Once I drop the element into svg group, it should be appended to the group. So far I

Show Detail