admin管理员组文章数量:1326011
The documentation for Snap.svg's Snap()
function lists three possible ways of creating a Snap object.
Snap(width, height)
- Creates a new, blank canvas of given dimensions.Snap(svg element)
- Create a Snap canvas from an existing, inline SVG elementSnap(css selector)
- Same as above, but with a selector rather than a direct reference
Is it possible to create a Snap object from either an SVG embedded as either an<object>
element or a <img>
?
The documentation for Snap.svg's Snap()
function lists three possible ways of creating a Snap object.
Snap(width, height)
- Creates a new, blank canvas of given dimensions.Snap(svg element)
- Create a Snap canvas from an existing, inline SVG elementSnap(css selector)
- Same as above, but with a selector rather than a direct reference
Is it possible to create a Snap object from either an SVG embedded as either an<object>
element or a <img>
?
- Not sure of a way, but is loading the svg in via Snaps load function not a possibility ? A bit more context of the limitations may give some possible ideas. – Ian Commented Feb 28, 2014 at 12:51
-
You're correct, creating a blank canvas with
Snap()
and then loading the external SVGs withsnap.load()
would probably work. This would mean loading the SVG twice though, once embedded in the page (because the SVG still needs to be visible without Snap.svg) and then again withsnap.load()
. – MartinAnsty Commented Feb 28, 2014 at 14:47 - Couldn't you place the svg inside the other markup and use it then ? Like jsfiddle/f8Sh8/1 its tricky as I mentioned without knowing what you actually need to do and why (ie why you need to use Snap). – Ian Commented Feb 28, 2014 at 15:17
- Technically yes, the svgs could be embedded. This is tricky though, our templating system doesn't have access to the frontend image files where the svgs are stored. So whilst that is a potential solution I'd rather avoid it because it would involve something like duplicating the svgs in the template directory. – MartinAnsty Commented Feb 28, 2014 at 15:47
4 Answers
Reset to default 2By peering at the source-code, I think just doing a Snap('#object-id')
would give you the SVG, instead of doing .node.contentDocument
. This may be a recent improvement, but as of today, this is officially there in the code.
Here's the supporting source code: https://github./adobe-webplatform/Snap.svg/blob/5b17fca57cbc4f8a8fd9ddd55cea2511da619ecf/dist/snap.svg.js#L3182-L3184
Only thing I can e up with that may sort of work, is using something like the object tag, with contentDocument (may need to check support, but Snap isn't really aimed at old browsers anyway).
I think the svg image will have to be local to the file though, so remote calls to images I don't think would work (or maybe with some amended server settings), so I couldn't get it working on a fiddle to show, just with a test url below, so the code would be something like...
in html...
<object id="tux" data="Dreaming_Tux.svg" width="600" height="600" type="image/svg+xml"></object>
then js....
var tux = Snap("#tux");
var tuxContent = tux.node.contentDocument; /// grab the referenced content
var sTux = Snap( tuxContent.firstChild ); /// snapify it
var tuxPaths = sTux.selectAll('path'); /// use snaps selector to grab elements
tuxPaths.forEach( function( el ) { el.attr({ opacity: 0.2 }) });
testing example here
Probably the best way is to use Snap's Element.node
with an <object>
tag if you are serving the svg from the same domain. E.g. you can't use it from a file system, you have to set up a local server (same-origin policy).
If you have
<object id="graph" data="somevectors.svg" type="image/svg+xml"></object>
Then you can just use this
var s = Snap(Snap("#graph").node); //wrap the element
Then select the svg elements with CSS selectors and mess around with them
var circle = s.select("#circle")
.attr({
opacity: .3
});
@kumar_harsh is right. That's being said, I couldn't get it to work due to page loading problems. Even inside a window.onload callback, Snap('#object-id')
came back null or returned the object tag with nothing inside.
To use a simple Snap('#object-id')
, I had to use the method demonstrated in the documentation here:
1) substitute the object tag with an empty svg tag (#object-id
-> #svg-id
)
2) var s = Snap(#svg-id)
3) Load in the svg elements using Snap.load('/path/to/my/svg.svg')
4) In the load callback function s.append(resultData)
;
5) Use Snap
If your code works in your browsers console, but not live, then you might be encountering the same problem, and your solution is above.
本文标签: javascriptCreating a Snapsvg object from an ltobjectgt elementStack Overflow
版权声明:本文标题:javascript - Creating a Snap.svg object from an <object> element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742136816a2422406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论