We have a few issues that need to implement SVG images in HTML.
We already defined a good process for CSS in #1963886: Use HiDPI icons in the toolbar but we still haven't agreed a good way to do this inline.
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | svg-degrades-2142655-21.patch | 2.58 KB | lewisnyman |
Comments
Comment #1
jessebeach commentedI have no experience with inline SVGs. Let's cast a wide net on our networks and try to solicit input from folks who have experience with such implementations.
Comment #2
yannickooHow should I understand that? You just can use an
<img>tag and pass the path to the SVG file as source or you use data URIs and you are done.Comment #3
lewisnymanI think the main problem is falling back to PNG gracefully, see #2083949: filter.module: Update use of icons to new standards
Comment #4
yannickooOkay, so we could have to versions of files, one .svg and one .png file and with following code we could check whether SVG is supported and otherwise we replace the file extensions with png:
Comment #5
nod_This doesn't work if you svg is inline.
We could add a data- attribute with the src of the png file and replace all svg tags with img tags and src tag.
And of course, in the final script, no jquery involved and only a dependency on domready.
Comment #6
yannickooAre we using inline SVG or are we using image tags? Your solution sounds cool.
Comment #7
ruplWhere SVG is supported, browsers are almost always capable of inline SVG too (your main loser in this battle is Opera Mini).
http://caniuse.com/#feat=svg (basic)
http://caniuse.com/#feat=svg-html5 (inline)
When I use inline SVGs it's normally in a stylesheet and in most of my real-world experience we did this using Compass to inline the SVG into the stylesheet. This has the benefit of reducing HTTP requests because the payload for an unused SVG is normally pretty small. So you end up with something like this:
Edit: this can be done by hand as well. There's absolutely no dependency on Compass for this technique.
Comment #8
rainbowarrayYou may want to look at http://grumpicon.com.
Helps to set up best practice code with PNG fallback. Not sure the exact technique used but some smart people I trust developed this. Plus it has an ASCII Unicorn, so it has that going for it.
Comment #9
lewisnymanPerhaps the title was too vague, I don't mean encoding an SVG. Just embedding it in HTML:
Comment #10
nod_#4 without jquery involved is ok with me.
Comment #11
yannickooSo we have sth. like following code?
Comment #12
nod_I'd either use .getAttribute() and .setAttributes() but not mixing the two. Here I'd probably just go with .src in both cases.
Also if we're looking at performance I'd be better to do two loops, on where we read all src values and a second one where we change the attribute.
Comment #13
yannickooI just wanted do post an example how we could replace the SVG files. Forgot that we just can write to
.src.Is the technique already decided or are we waiting for more suggestions? I don't think that it is good to use libraries like Raphaël because we just could replace the SVG with a PNG, do you agree?
Comment #14
rainbowarrayI asked Scott Jehl and Mat Marquis for their suggestions for markup (they developed the Grumpicon tool I mentioned above at Filament Group) for inline SVGs. Here's what they suggested:
<img src="foo.svg" onerror="this.src='foo.png'; this.onerror=null">That seems pretty simple and straightforward.
Comment #15
nod_yes but no. No inline js please, it's not the 90' anymore.
http://dbushell.com/2013/02/04/a-primer-to-front-end-svg-hacking/
Comment #16
rainbowarrayOne of my Twitter peeps ran a performance test on one loop vs. two loops. Looks to me like two loops is indeed the winner: http://jsperf.com/one-loop-to-rule-them-all
Comment #17
rainbowarraySo I misread that graph. Higher ops per sec is better, so this appears to show that one loop would be better than two loops. Entirely possible I misunderstood how to set up these loops.
Comment #18
nod_Made a update, the first test was not really relevant, a loop over 1 result is kinda not very useful. Also things were not reseted properly making the test inaccurate.
http://jsperf.com/one-loop-to-rule-them-all/2
Comment #19
rainbowarrayLooks like two loops is generally the fastest option?
Comment #20
lewisnymanI think it would be easier to create the patch here, there's no need to bundle it into one of the related issues.
Comment #21
lewisnymanWe could probably just add this? http://benhowdle.im/svgeezy/
Comment #22
lewisnymanNo implementation yet but I thought it was worth bringing in SVGeezy in patch form for review.
Comment #24
lewisnyman22: svg-degrades-2142655-21.patch queued for re-testing.
Comment #25
sunThat library looks stunningly large compared to the code proposed by @nod_...? Are we missing something? Shall we contribute upstream?
I actually like both ideas that were proposed here — i.e., both the simple file extension swapping, but also the data- attribute approach for inlined/embedded data resources. Perhaps we should combine both into a single library?
FWIW, I ran http://jsperf.com/one-loop-to-rule-them-all/4 and the "non-src" approaches are significantly faster than the "src" variants in latest Firefox.
Comment #26
lewisnymanI had a wee chat with nod_ in irc regarding the suggestion he put forth in #2083949: filter.module: Update use of icons to new standards.
Looks like we have a consensus around that method. It's implemented on a case by case basis so no need for a patch here.
Comment #28
actualgabe commentedHave a look at Iconic. They have a script that converts
<img>tags with a .svg srouce to inline SVG code. They also offer a PNG fallback option, though that is still being developed. I have been in contact with them, and they gave me a copy of iconic.js with .png fallback support enabled, and in my initial tests it worked perfectly, automatically switching to a .png source for IE8 and Android 2.3. I don't know how this would integrate with Drupal, but their system seems to be a good one.There is also SVG Injector, which is open source and appears to be the basis for inconic.js.
Comment #29
rainbowarrayThe new picture element allows for image types on source elements. So that means you can provide an SVG as a source with type="svg" and have a png as a fallback. Since picture support is being implemented in browsers now, that means there will be actual browser support for this within the next few months. The revised version of Picturefill will have type support so this could work in older browsers too.
Entirely possible that the solution in the other issue might work well too. Just thought I'd mention this as Scott Jehl brought this up on Twitter the other day.
Comment #30
lewisnymanRelated: #2286601: [policy] Drop support for browsers that don't support SVG
Comment #31
Sumit kumar commentedif(!abc.svg) {
var i=document.getElementsByTagName("img"),j,y;
for(j = i.length ; j-- ; ){
y = i[j].src
// If filenames ends with SVG
if( y.match(/svg$/) ){
i[j].src = y.slice(0,-3) + 'png'
}
}
}
I think this will help to u
Comment #32
lewisnyman#2286601: [policy] Drop support for browsers that don't support SVG