Wednesday, April 27, 2016

Why JPEGs are like McDonalds Apple Pies (and SVGs are not)

Last week we looked at a simple, free SVG editor called Boxy SVG ( it's here in case you missed it).

Let's see where it takes us today. But first, here's an idea…

SVG isn't an image format – it's more of a recipe

[caption id="attachment_129311" align="alignright" width="291"]McDonald's hot apple pie Baking pies[/caption]

That makes it very different from all the pixel-based formats we've used in the past. Let me explain.

You could think of our classic pixel-based image formats -JPEGs, GIFs and PNGs - as being like those McDonald's Apple Pies. Each comes packaged in precisely fixed amounts. You can't ask the McDonalds serving staff to whip you up a 'family-sized' pie or to rustle up a 'half-slice' to have with your coffee. Well, technically, you can ask but you'll get a strange look.

It's a reasonable serving for one person, but if you need to share with 2, 3 or more people, you need to stretch your pie further – i.e. everyone gets less.

But SVG is like having a good apple pie recipe.

[caption id="attachment_129313" align="alignright" width="409"]Baking pies Baking pies[/caption]

You hold the instructions to make as much or as little pie as you want.

Sure, there are numerical amounts written into the recipe – 8 apples, 4 cups of flour and so on. But if you need to feed twice as many people, you just double the measurements. If you want to feed half as many – just halve the numbers. It's designed to scale.

And because you control how the pie is made, you can even 'change things up' a little and customize the basic pie recipe for different people's needs. Nanna likes a handful of black currants in her pie. Your brother loves custard. Small, thoughtful changes to the original recipe are easy and valuable.

Of course, the 'S' part in 'SVG' stands for 'scalable', and it's part of what makes SVG so useful for the web. But 'scalability' – by itself – doesn't always mean 'more usable'. Sometimes we need to change how our layouts are displayed. SVG can help us.

Making Charts with SVG

Here's an SVG chart that I created using Boxy SVG. It shows the number of mentions of the phrase 'Star Trek' and 'Star Wars' in books since 1960 (data via Google NGRAM).

[caption id="attachment_129310" align="aligncenter" width="800"]A simple chart made in Boxy SVG A simple chart made in Boxy SVG[/caption]

You might typically see this type of chart online as a JPEG or PNG - in fact, you can export it from Boxy SVG as a PNG if you wanted to.

Ok, so, why not make it a JPG and be done with it?

Reason #1: The File Size

  • The JPEG version is 47kb. (JPEG link)
  • The raw SVG version from Boxy SVG is 14kb (SVG link).
  • With a little tidying, I got it down to 10kb (SVG link).

The Boxy SVG chart isn't perfect out of the box. View the source and you'll see:

  • some extra empty TSPANs in the text
  • the extreme number precision makes numbers longer and more unwieldy than they need to be (tools like SVGO can fix this for you)
  • elements moved inside the editor tend to get transforms added to their original X & Y positions. Ideally any repositioning would be made to that original X & Y co-ordinate.

A little bit of find-and-replace resolves these issues in five minutes. Even with these small issues, the Boxy SVG is clearer and easy-to-read than anything else I've worked with.

Of course, either of the SVG files above will still be laser crisp at 2000px (if required) with no increase in file size. The JPEG certainly wouldn't.

Still, internet speeds are fast – we're not going to fight over 30kb between friends, are we?

Reason #2: SVG has Non-Scaling Strokes

This is an SVG trick that I only became aware of yesterday.

While it's always handy to be able to squish down our images to fit it into a small screen, there's a point where our linework legibility simply breaks down. Similarly, highly scaled-up image lines can become fat and oafish.

[caption id="attachment_129308" align="alignright" width="400"]Vector-effect: Non-scaling stroke in action. Vector-effect: Non-scaling stroke in action.[/caption]

Unfortunately, that's an unsolvable problem in our PNGs and JPGs. We just have to live with it.

Non-scaling Strokes

Continue reading %Why JPEGs are like McDonalds Apple Pies (and SVGs are not)%


by Alex Walker via SitePoint

No comments:

Post a Comment