How to select fragments of an existing SVG using Snap.svg

我只是一个虾纸丫 提交于 2019-12-12 10:46:08

问题


I'm trying to run Snap.svg locally, but the Snap.load() function makes an AJAX request, which isn't allowed locally (in Chrome, anyways). Below is my code:

window.onload = function () {

    var s = Snap("#headDiv");
    Snap.load("emotions.svg", function(f) {

        eyes = f.select("#eyes");
        lids = f.select("#lids");
        head = f.select("#head");

        s.append(f);
    });
};

So while this works fine from a server, I'd like to get this to run locally. What would be my best option to include my emotions.svg file without making an AJAX request?

I know it's easy to just throw the SVG in the DIV, but I wasn't able to access the fragments that way with my current script. Any ideas?


回答1:


Changing:

window.onload = function () {

var s = Snap("#headDiv");
Snap.load("emotions.svg", function(f) {

    eyes = f.select("#eyes");
    lids = f.select("#lids");
    head = f.select("#head");

    s.append(f);
});
};

To simply:

window.onload = function () {

    eyes = Snap.select("#eyes");
    lids = Snap.select("#lids");
    head = Snap.select("#head");
};

And then placing the actual SVG script in the target DIV worked great.



来源:https://stackoverflow.com/questions/30130808/how-to-select-fragments-of-an-existing-svg-using-snap-svg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!