I have snap svg group of 5 elements like;
let canvas = Snap('#svg');
let myGroup = canvas.g();
myGroup.add(canvas.rect(100, 200, 110, 220).attr({fill: '#000'}));
myGroup.add(canvas.circle(400, 400, 50).attr({fill: '#500'}));
// want to remove this element later
myGroup.add(canvas.rect(100, 200, 50, 60).attr({fill: '#050'}));
myGroup.add(canvas.text(50, 100, 'Some Text').attr({stroke: '#005'}));
myGroup.add(canvas.rect(700, 200, 110, 220).attr({fill: '#055'}));
Then I can access the specific elements of myGroup
using the index, for instance, if I want to change the attributes of text I can use myGroup[3]
because it is at 3rd index in myGroup
.
Now I want to remove the element at 2nd index and then add some other element at same index, so I used remove()
method like;
myGroup[2].remove();
myGroup.add(canvas.path('M 100 200 l 30 35').attr({stroke: '#050'}));
but the problem is; the elements at 3rd and 4th index got shifted up (2nd and 3rd respectively) and new element has been added to 4th index.
Does anyone know how can I add element at the same index?