The Clown Car Technique for Responsive Images
github.com
github.com
Yes, the SVG does "waste" a single HTTP request -- you get two http requests: one for the SVG file and one for the image it downloads that matches the media queries within the SVG. However, you can use a data URI within your HTML to eliminiate the call to the SVG file. An example of this (in CSS) is here: http://jsfiddle.net/estelle/ZHrb2/
The reason I called it Clown Car technique was because we're stuffing a whole bunch of images into a tiny little file.
I am looking into this technique not just to solve the <picture> RWD image issue, but also as a way to manage images. We currently separate content (html) from presentation (css) from behaviour (js). I like the idea of managing my assets separately as well. Thinking this helps along those lines.
Is there a way in SVG to concatenate strings? (I never really used SVG and can't find something on this in Google) .
Using JS I generally use the data attribute in html to set the image name and add _small, _big, _huge depending on the window size. The JS script will automatically set the right image. It's great but doen't work if there's no JS support.
The only solid fix for responsive imagery is a HTML5 tag that allows you to set breakpoints for different sizes. The proposed "picture" tag which is similar to that of the video tag does what we want, but we most likely won't see any action on this tag until 2014, possibly 2015 if this W3 bug ticket is any indication: https://www.w3.org/Bugs/Public/show_bug.cgi?id=18384
This jQuery script adds in the functionality for using the proposed tag now though, I can't speak for how well it works, but it looks promising: http://jquerypicture.com/
http://stackoverflow.com/questions/15674880/
It's a cool hack which you can serve low-res, monochrome jpeg in the first scan, and gradually to full-res, full-color jpeg.
The only thing is I think that is one of the actual pixels, rather than an average. In an ideal world you could send the average color of each 2x2 block, then send the 2nd, 3rd, and 4th pixels and use that data to reconstruct the 1st pixel.
Or is that how it works already?
<g id="logo">
<image id="1x" xlink:href="logo_low_rez.png" />
<image id="2x" xlink:href="logo_high_rez.png" />
</g>
<g id="promo">
<image id="1x" xlink:href="promo_low_rez.png" />
<image id="2x" xlink:href="promo_high_rez.png" />
</g>
Then, you could render the content as such: <img src="images.svg#logo" />
<img src="images.svg#promo" />
I'll have to play with this, but it looks like a promising idea. Here's hoping browser compatibility is decent. For some reason, the first 'foreground' svg example doesn't seem to render an image for me in Chrome or Firefox, despite supposed support for the latter:http://estelle.github.io/clowncar/
This could be a problem; I'd like to see expanded browser support.
> We can work with the browser vendors to get this CSP lifted.
No, let's work with the browser vendors to implement something more sensible than stuffing background image media queries into a god damn svg.
The author neglects to point out that every image will waste a http request downloading the svg file so this is going to give poorer performance than having the logic in your css (perhaps he considered it too obvious).
But, the benefit of this technique (as far as I can tell) is decoupling, and any kind of embedding re-couples the data. Maybe in an acceptable way, if you can do it at a deploy step. It depends on what the use case is.
The method at this link: http://estelle.github.io/clowncar/bgonly.html only does two http requests, the svg and the image used. We can bring that down to 1 by using data URI for the svg logic only.
Serving images according to pixel densities should be handled completely on the server side. This can be done based on HTTP headers and in border cases with a cookie denoting increased pixel density (i.e. N phyiscal pixels are 1 "HTML" pixel in width/height attributes), which can be set using JS. Ideally, we'd have a HTTP request header for this (Accept-*).
For now, you can still get good results with higher resolution images where maximum detail is desired (browsers scale images down nicely nowdays and users actually appreciate being able to save higher-resolution versions) and lower/normal where loading times are important (i.e. everywhere else).
Of course, if you are going to use SVG, by all means provide Vector Graphics where you can!
Seems like you could use this technique to implement rich widgets like video players, social sharing buttons, or ad units.