Question

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.

Was it helpful?

Solution

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"
    });
});

OTHER TIPS

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"
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top