CHAPTER 11

image

The Truth About SVG: The Flash Challenger That Was, Wasn’t, and Now…

We touched on the idea of Flash-challenging technology when we looked at Canvas in Chapter 9. But perhaps the most serious challenger to Flash in the past decade has been Scalable Vector Graphics (SVG), an XML format for 2D graphics and animation, which is making yet another mini-comeback. Let’s take a very brief look at the dead-again, less-dead-again technology that is SVG.

SVG, SVG…

Oh, SVG (Scalable Vector Graphics)…what can we say about you? You’re a separate W3C spec that’s been in development since 1998 (on target to be finalized in August 2014!). You’re not part of the HTML5 spec, but HTML5 will let you appear inline with other markup. You’re all about vector shapes, which makes you the Illustrator to Canvas’s Photoshop. You’ve promised so much, for so long—in 2002 people were writing 1,000+ page books about you, such as SVG Unleashed (Sams, 2002). Yet you’ve never made it to the big leagues. What happened?

SVG is both a relic of past web standards and a technology that’s finally kinda-sorta-almost arrived (just in time for the development of SVG 2.0). It’s an XML format for vector graphics (think of SVG being to graphics as HTML is to text), which means it looks like a bunch of angle-bracketed tags and attributes. Remember when we looked at the history of HTML and how it was going to be all XML? Well, SVG is part of that vision—the vision that didn’t work out.

Here’s what basic SVG looks like:

<svg id="mysvgexample" height="200" width="300" xmlns="http://www.w3.org/2000/svg">
   <rect id="myrectangle" width="200" height="100" fill="red" x="20" y="20" />
</svg>

This renders what you see in Figure 11-1.

9781430264156_Fig11-01.jpg

Figure 11-1. Our exciting SVG demo

Yep, a red rectangle. I’ll give you a moment to recover.

Here’s a quick explanation of what’s going on here: we’ve set the <svg> element to be 200px high and 300px wide, which acts as a transparent container for the shapes or lines we want to render. (I’ve given it a border with CSS so you can see its boundaries.) We then create a rectangle with the <rect /> element, give it a height and width, and give it an x and y offset from the container <svg> element. Add a fill (I’ve used the keyword red, but you could use a hex value too), and we’re done.

In HTML5 you can drop this code right into your HTML document, and supporting browsers (IE9+, FF4+, Safari 5.1+, Chrome 7+, Opera 11.6+, iOS 5+, Android 3+) will render it as appropriate.

SVG: Browser Support Arrives at Last

SVG is interesting again because browser support is finally arriving. Currently, all modern and not-so-modern browsers, including IE9 (but excluding, you guessed it, IE6–8) and Android browser 3.0+ (but not 2.x or older), support SVG embedded using the <embed> or <object> element. The IE situation isn’t quite as bad as it sounds, though, as we’ll see next.

Browsers are also starting to support SVG images in the <img> tag and even as CSS backgrounds (specifically IE9+, FF4+, and all recent Safari, iOS, Chrome, and Opera releases).

And there’s even great support for applying advanced SVG features such as filters (for example, Gaussian blur), clipping, and transforms to non-SVG objects. Amazingly, at the time of this writing, all of the major browsers on both desktop and mobile now support this; that’s a huge improvement from just a year or two ago when support was varied and spotty.

Things get a little less rosy on the more advanced features. At the time of this writing, SVG SMIL animation isn’t supported in IE on the desktop or mobile; SVG transform and transition effects aren’t supported on any of the mainstream mobile browsers; SVG fonts aren’t supported in IE, Firefox, or mobile Opera; CSS filters for SVG are available only in WebKit browsers (including Opera); and SVG Fragment filters are available only in non-WebKitbrowsers (IE and Firefox). For up-to-date information on what is and isn’t supported in SVG, check out http://caniuse.com/#search=svg.

Support for basic SVG including filters and inline elements has come a long way in the past few years. But in order for SVG to become a major contender and move beyond just drawing static images, it needs full support for advanced animation, CSS transitions, and fragment shaders. Adding to the dilemma, most web designers and developers can’t afford to abandon everyone who isn’t in the latest browser. A huge portion of the web-browsing population still uses older browsers, and if we rely on dynamic SVG effects out of the box, we’ll be abandoning those people to a graphics-less world.

So, what do we do? Is there any way to start using SVG now without having to mess with all this cross- and legacy-browser nonsense? Luckily, there is.

Yes, There Is Real-World SVG We Can Use Right Now

Dmitry Baranovskiy is SVG’s hero and savior. He has authored two JavaScript libraries that let designers take advantage of everything SVG can do without having to manage all of the complexities themselves. Both are sponsored by Adobe, who is Baranovskiy’s employer at the time of this writing and who has been pushing SVG features to spec through the W3C with a lot of success over the past decade or so.

Baranovskiy’s first library is called Raphaël, and it provides SVG compatibility all the way back to IE6. Raphaël lets us do some simple, cool, Flash-like things with SVG. Under active development for several years, it offers browser support right down to IE6 (thanks to VML translation in IE). You can check it out here: http://raphaeljs.com/. (And make sure you check out the sister graphing library, gRaphaël, as well: http://g.raphaeljs.com.)

His second library is called Snap.svg, and it trades legacy support for more advanced features that work in all modern browsers. Snap was written entirely from scratch and works on SVG elements loaded from anywhere (in the page, downloaded to the page through a file, loaded from text input…whatever). It’s free and open source; check it out here: http://snapsvg.io/.

We’ll look at what SVG can do both by itself and through these libraries in a moment, but for now it’s worth keeping in mind that, thanks to Dmitry, simple SVG (including animation) can be used with broad browser support.

Imagine if, all of a sudden, we had to support devices with an extremely wide array of resolution sizes and densities, some of which didn’t support Flash. Wouldn’t vector graphics—crisp at any resolution and scalable to any size—make life a lot easier? Hold that thought....

The Many Faces of SVG

Let’s look at the different faces of SVG.

  • SVG, the gigantic spec that’s been kicking around (and growing) for a decade
  • Advanced SVG as it’s being implemented in cutting-edge browsers
  • SVG as we can use it today in real-world situations with tools such as Raphaël and jQuery SVG (http://keith-wood.name/svg.html)

SVG can be (and is being) used right now for a variety of situations with solid cross-support, including iOS devices, so its time may have finally arrived. And it’s just in time, considering Adobe’s abandonment of Flash in mobile browsers and its promotion of Snap.svg. (For more on the death of Flash, see the discussion in Chapter 9 where we looked at Canvas and Flash.) And that’s not to mention the pressing need for interface elements that can scale up from a phone to an iPad Retina display to a 27" or 30" screen.

SVG in the ’00s: The Great Hope That Wasn’t

SVG has been on quite a journey. In its full pomp, it’s extremely impressive and currently does as much (if not more) than current CSS3 implementations. Full SVG support does everything from animation to the Photoshop-like filter effects we touched on earlier (see this example in Chrome, Opera, or Firefox: http://svgwow.org/filterEffects/chiseled.svg) to custom fonts, masking, video, and of course drawing vector shapes. It’s all there (in the spec at least—browser support has lagged badly). So, in a way, it’s a lot like Flash.

Well, basic SVG is like Flash a decade ago but without the browser support or developer tools. In fact, before Adobe bought Macromedia (which developed Flash), Adobe was championing SVG as an open alternative. And in 2002 some 160 million people were using their SVG viewer plug-in (since discontinued; see www.xml.com/pub/a/2002/07/03/adobesvg.html).

To give you a taste of the hype around SVG in the early ’00s, here’s a quote from a 2002 article on Digital Web titled “SVG: The New Flash” (www.digital-web.com/articles/svg_the_new_flash/):

SVG should soon be widespr ead, and its non-proprietary nature will help to hasten the progress. Flash will continue to be the dominant standard for quite some time because of its large client base. However, SVG is rising quickly. The distribution of the SVG plug-in via browser manufacturers will quickly increase the installed user base, just as it did for Flash. Future versions of various browsers will include SVG viewers as standard, and some already do.

But SVG never really took off. (Let it be said its “nonproprietary nature” hasn’t counted for much in the past decade.) It couldn’t touch Flash’s installed base and never had a development tool as designer-friendly as Flash. And when Adobe bought Macromedia in 2005, as far as vector graphics on the Web went, it was Flash or bust.

Still, SVG never really died either. And with browser support now improving rapidly and the writing on the wall for Flash, perhaps SVG is due for yet another comeback.

SVG Browser Support: Android, What the Hell? Oh, and IE…

One sticking point at the moment is that Android 2.x doesn’t offer even basic SVG browser support, despite Google pushing SVG elsewhere (see http://googlecode.blogspot.com/2009/10/svg-at-google-and-in-internet-explorer.html). That might seem like old news considering that at the time of this writing Android is on version 4 and heading toward 5, but thus is the plight of Android: somewhere around 24 percent of the Android world is still using Android 2 as I type this (for up-to-date stats, see the Android developer dashboard: http://developer.android.com/about/dashboards/index.html). Basic SVG support has been in WebKit, the engine behind Android’s browser, for ages; it was just intentionally left out of Android 2.x “to save space” (see the comment and discussion here: http://code.google.com/p/android/issues/detail?id=1376#c4). Go figure. Android browsers in 3.x and 4.x support basic SVG just fine, and most of the advanced features are finally available in 4.4. (aka “KitKat.”)

Fortunately for us, there are several libraries available that allow us to translate modern SVG into something IE6-8 (and any other browser) can understand.

Raphaël, the JavaScript library for working with SVG we touched on earlier, falls back to IE’s old Vector Markup Language (VML) for compatibility. (It’s similar to the Canvas emulation we looked at in Chapter 9 and has similar limitations; in other words, VML is slow.)

There’s also SVG Web (http://code.google.com/p/svgweb/), which translates SVG to Flash for older browsers that don’t support SVG, including IE. (Sadly, this still doesn’t help us out with Android 2.x. See http://groups.google.com/group/svgweb/browse_thread/thread/77fb6970f5f01e97.)

There’s also canvg (http://code.google.com/p/canvg/), which renders SVG in Canvas and provides some SVG support for Android as a stopgap measure until Android 4.0 becomes widespread. This approach is discussed here: www.kendoui.com/blogs/teamblog/posts/12-02-17/using_svg_on_android_2_x_and_kendo_ui_dataviz.aspx. (Fabric.js may also help: http://fabricjs.com/.)

While these tools make basic SVG a reality for just about any browser, remember this is only basic SVG, not the crazy Photoshop-like filters that have become part of the SVG spec.

SVG Demos: What Is It Good For?

Vector graphics are useful in lots of instances, such as maps, charts, illustrations, logos, visualizations, resolution-independent interfaces, and so on. And the success of Flash has certainly demonstrated that the need for animated vector graphics is there. What may otherwise be delivered in Flash can potentially be done in SVG and therefore be available to iOS users.

Let’s take a look at some SVG demos to see what it is capable of. We’ll look at a few general examples and then a few real-world Snap.SVG and Raphaël examples.

SVG Girl

SVG Girl (Figure 11-2) is an “SVG animated video” that Microsoft commissioned to show off its hardware-accelerated SVG support in IE9 (though it works in any modern browser). It’s a brief but incredibly complex and impressive anime clip done with SVG. See it here: http://jsdo.it/event/svggirl/.

9781430264156_Fig11-02.jpg

Figure 11-2. The animated SVG Girl short is brief but impressive

SVG performance for this sort of complex animation used to be lackluster, but hardware acceleration makes the world of difference. Hats off to the IE team for hardware-accelerated SVG. (You can read more about it here: http://blogs.msdn.com/b/ie/archive/2011/03/08/comparing-hardware-accelerated-svg-across-browsers-with-santa-s-workshop.aspx.)

SVG Edit

SVG Edit (Figure 11-3) shows what can be done with client-side web technologies alone. It’s an SVG- and JavaScript-based app for editing SVG. Download it at http://code.google.com/p/svg-edit/ or try it live at http://svg-edit.googlecode.com/svn/branches/2.5.1/editor/svg-editor.html.

9781430264156_Fig11-03.jpg

Figure 11-3. SVG Edit is an SVG-powered drawing program that outputs…SVG

Google Docs

Google Docs’ drawing program (Figure 11-4) uses SVG with a VML fallback. (Google also started using SVG for the graphs in Google Analytics in early 2012.)

9781430264156_Fig11-04.jpg

Figure 11-4. SVG is used in some very high-profile situations, such as the Google Docs drawing program

SVG Games

You can create games in SVG with JavaScript, but SVG for games hasn’t caught on in the same way Canvas has. As part of its IE9 tech demos, Microsoft released a couple of very simple retro game examples:

9781430264156_Fig11-05.jpg

Figure 11-5. SVG-oids may be simple, but it demonstrates the interactive possibilities of SVG

D3.js

D3.js (Figure 11-6 and online at http://mbostock.github.com/d3/) is a “small, free JavaScript library for manipulating documents based on data” that uses SVG for some extremely impressive data visualizations. Check out the examples for more: http://mbostock.github.com/d3/ex/. Also see the 150+ annotated slides from a D3 workshop by D3 creator Mike Bostock: http://bost.ocks.org/mike/d3/workshop/. It starts with a simple introduction of D3 and SVG and finishes with some impressive examples.

9781430264156_Fig11-06.jpg

Figure 11-6. It’s worth exploring the impressive D3.js examples online, such as this “streamgraph.”

Charts with Highcharts

For a fully featured SVG charting library, it’s hard to go past Highcharts (Figure 11-7 and online at www.highcharts.com/), which is an SVG (and VML for legacy IE support) JavaScript-driven chart library. (See why SVG was used: www.highcharts.com/component/content/article/2-news/12-highcharts-goes-svg. Highcharts also gets a lot of love from developers: http://news.ycombinator.com/item?id=1847569.)

9781430264156_Fig11-07.jpg

Figure 11-7. Highcharts has a lot of fans, and its flexible, well-documented API makes it easy to use

Snap.svg-Powered Demos

Much of the current, real work for advanced browsers is done with the library Snap.svg, which, as we saw earlier, offers a simple and easy way to generate, manipulate, and animate SVG elements.

PBS Kids

PBS Kids uses Snap.svg to power all of the interactive and animated elements on the page, and its home page is full of them: cars carrying major characters drive onto the screen, there’s a large interactive dial for selecting a show, and everything clickable has richly animated hover states. The reliance on SVG lets the home page weigh in at just 9.2KB and lets the site focus on images of its characters rather than images for all of the interactive elements. See it in Figure 11-8 and online at http://pbskids.org/.

9781430264156_Fig11-08.jpg

Figure 11-8. PBS Kids relies on Snap.svg to provide the bold, animated, colorful experience on its home page

Codrops Animated SVG Icons with Snap.svg

This rich icon set (shown in Figure 11-9) is built entirely with SVG. Each icon has an animation that plays when clicked. The animations are smooth and subtle in modern browsers. View it online here: http://tympanus.net/Development/AnimatedSVGIcons/.

9781430264156_Fig11-09.jpg

Figure 11-9. Animated SVG icons built in Snap.svg are a great example of how SVG can provide the same subtle animations that used to be available only through Adobe Flash

Snap.svg’s Demo Page

It shouldn’t be hard for a framework to demo itself well, but many provide examples that feel contrived and not really great at showing real-world examples. Snap.svg’s demo page isn’t anything like that. Shown in Figure 11-10, the animated game example shines particularly well, demonstrating the power of SVG in delivering an animated spinning die and game board. The trees on the game board sway constantly, as if in the wind. It’s hard to believe this is all happening natively in the browser, without a plug-in.

9781430264156_Fig11-10.jpg

Figure 11-10. Snap.svg’s demos page features an interactive, animated game board that does a great job of showing the framework’s full potential

Raphael.js-Powered Demos

Snap.js is amazingly powerful, and many sites are moving in that direction for animation. Yet legacy browsers are still out there, and many of us need to support them. To get the most out of SVG without leaving legacy users behind, Raphaël is the library of choice. Let’s look at how it’s being used.

thirteen23

thirteen23 (shown in Figure 11-11 and online at http://thirteen23.com) is a design consultancy in Austin, Texas, that has done a great job using modern web technologies to pull off an impressive studio site. Its curved, smoothly animated navigation is built using Raphaël. Click around to see the navigation in action (and watch the backgrounds change). Also note the lack of full-page refreshes despite URL changes and without using the /#/ pattern. That’s the HTML5 History API in action, which we’ll touch on in the next chapter.

9781430264156_Fig11-11.jpg

Figure 11-11. The animated, circular navigation for thirteen23 shows what a nice sprinkling of SVG can do

Markup.io

Markup.io (Figure 11-12 and online at http://markup.io/) lets you draw vector lines on (and annotate) any web page with a simple bookmarklet. You can also publish and share your annotated pages. The drawing tools are SVG-powered by Raphaël.

9781430264156_Fig11-12.jpg

Figure 11-12. Scribble all over your pages with SVG thanks to Markup.io

DrawAStickman.com

The http://DrawAStickman.com agency promo for Hitcents is, in the words of the creator, an “interactive web site where visitors draw a stickman and take part in his animated adventure” that “became a viral success overnight, attracting millions of visitors from every part of the globe and winning numerous awards” (see Figure 11-13 and online at www.hitcents.com/blog/post/making-drawastickmancom-part-1-birth-idea). This brilliantly executed idea has had more than 20 million visits and uses Raphaël for the graphics.

9781430264156_Fig11-13.jpg

Figure 11-13. My stickman remained unflappably cheery in the face of a fire-breathing dragon

Working with SVG

Given browsers now support SVG files in <img> tags and even as CSS backgrounds, we can now use SVG files for background gradients, tab backgrounds, and other image elements where a single file can be reused and scaled as needed (see this demo: http://css-tricks.com/using-svg/). Combine SVG’s flexibility with CSS3’s multiple backgrounds, and there are interesting things afoot when it comes to styling visual elements. For example, SVG would be great for styling the controls of a media player for <video> and <audio> elements.

This isn’t just theoretical—designers now use SVG seriously and provide tutorials to get us up to speed. For example, in “A Farewell to CSS3 Gradients,” Alex Walker looks at the patchy support for CSS3 gradients, suggests we consider SVG as an alternative, and provides a handy tutorial to do just that: http://designfestival.com/a-farewell-to-css3-gradients/.

Responsive Web Design and SVG

Vector graphics may also prove quite helpful in responsive web design situations where we want to display crisp, lightweight interface elements on everything from the mobile to the desktop (and especially on super-high-resolution iOS and Android [3.0+] screens). Again, this isn’t theoretical; designers are getting their hands dirty with this right now. In Smashing Magazine’s January 2012 article “Resolution Independence With SVG,” David Bushell looks at using SVG for interface elements: http://coding.smashingmagazine.com/2012/01/16/resolution-independence-with-svg/.

Vector UI elements are not a free responsive lunch, however. While it’s tempting to think they can scale up and down effortlessly (they’re vectors!), this isn’t necessarily true. Large artwork scaled down to very small sizes can become a blurry mess, and small artwork scaled up can look spartan and devoid of detail, as this lengthy article on icons and SVG demonstrates: www.pushing-pixels.org/2011/11/04/about-those-vector-icons.html.

(Of course, CSS3’s support for gradients, rounded corners, transformations, and animation may place the proverbial pillow on SVG’s face just as it comes off of life support for the third time. In fact, SVG filter effects are being ported to CSS and are already in WebKit, as this article explains: www.html5rocks.com/en/tutorials/filters/understanding-css/. Plus, check out these crazy demos of SVG-inspired CSS shaders: www.adobe.com/devnet/html5/articles/css-shaders.html.)

Despite that SVG has been kicking around for more than a decade, the web design community hasn’t really given it a thorough workout to see what’s possible until recently. So, with the renewed interest in all things web standards, the decline of Flash, the rise of responsive web design, and a reasonable baseline of browser support, it’s definitely time to get experimenting again.

SVG Gotchas

There are two key problems, though. The first is performance: complex SVG is slow. Browser makers generally haven’t paid much attention to SVG performance because it was always the red-headed stepchild of web standards. Things are starting to change, however. For one, hardware acceleration helps dramatically, as Microsoft has demonstrated with IE9–11. (Yes, Microsoft has not only caught up but is now leading the way in some areas of web standards implementation. You’re telling me!)

The other problem is tools. No one wants to sit around writing SVG markup by hand. Some drawing tools are available, however, such as the following:

You can also use JavaScript libraries such as Snap.svg, Raphaël, or jQuery SVG to draw SVG with JavaScript. Nevertheless, most vector drawing and animation for the Web is done in the Flash IDE. What if Flash could export SVG?

SVG: Heir to Flash?

Adobe’s dedication to pushing SVG forward, both in the standards community and through support of frameworks such as Snap.svg and Raphaël, is admirable and speaks to its desire for a post-Flash Web. For many, many, many years Flash was the only and preferred solution for bringing animated, interactive elements to the Web. Those elements were locked inside a tiny plug-in element, though, and communicating with them or manipulating them was difficult. Want a dynamic and animated navigation for your site? Well, you might as well build the entire thing in Flash because using it for just the navigation frame was a royal pain.

Yet Flash was (and continues to languish as) much more than simple animations. It was a fully fledged solution for multibitrate-encrypted video streams, a high-powered gaming engine, a 3D engine, and so on and so forth. As we saw in previous chapters, there are many different technologies hiding under banner of “HTML5,” and most of them will take a piece of Flash’s territory. SVG has been coexisting with Flash for a long time, and now it’s finally coming into its own.

The question for SVG isn’t whether it can (or should) replace Flash as a solution for constructing interactive and animated page elements but whether it can carve out a niche truly of its own. Snap.svg and Raphaël have certainly helped it carve out that niche, and in the next few years as the browsers mature we’ll be able to do more with SVG natively.

Can SVG generate enough interest in the design community after crying wolf in the early ’00s? Ironically, it may not be Flash that distracts the web community from SVG but other web standards. With so much activity around web standards development these days, it has a fight for attention on its hands.

Is SVG destined to always be the bridesmaid and never the bride? Or will the folks who wrote the huge tomes on SVG all those years ago get to dust off their work for a belated second edition? Time will tell.

In the meantime, it’s easy to get started with Snap.svg or Raphaël, so why not give them a try? Check them out at http://snapsvg.io and http://raphaeljs.com.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181