silviubogan
silviubogan

Reputation: 3461

Select <svg> root of Fragment with Snap.svg

In Snap.svg I load an external SVG file and after I append it to the document, I must change some of its attributes and contents. I want to set the x and y attributes of the element which is the root element of the Fragment d. d.select("svg") returns null and the following code still doesn't work.

Snap.load("but1.svg", function (d) {
    s.append(d);
    d.attr({
        x: "50",
        y: "50"
    });
});

Update: s is the Paper instance.

Upvotes: 0

Views: 2023

Answers (2)

Alfie Woodland
Alfie Woodland

Reputation: 834

To build on Ian's answer, you can simply select the svg element from the fragment with fragment.select('svg') So the resulting pattern would be:

Snap.load('myGraphic.svg', function (fragment) {
    var element = fragment.select('svg');
    paper.add(element);
    element.attr({
        x: "50",
        y: "50"
    });
});

Upvotes: 1

Ian
Ian

Reputation: 13852

I don't think you can do it quite like that with a fragment. I think you would need to use a select to grab the element, an example (can't test without the svg file)..

Snap.load("but1.svg", function (d) {
    s.append(d);
    var el = s.select("#myElement");
    el.attr({
        x: "50",
        y: "50"
    });
});

Upvotes: 1

Related Questions