Responsive Images: – Coming Soon!



Responsive Images: – Coming Soon!

0 0


respimg-velocity-eu-2014


On Github yoavweiss / respimg-velocity-eu-2014

Responsive Images:

Coming Soon!

Who????

(fragments)

Working on responsive images in my spare time for the last 2 years

A member of the RICG

A Blink & WebKit committer

Prototyped picture in WebKit. Implemented srcset in Blink. Implementing picture in Blink and WebKit.

I also been working on front end optimization server side solutions for the last 15 years, and am on a personal vendetta on image bloat on the Web

What is Responsive images?

Who knows what the responsive images problem is?

Efficiently load properly dimensioned images that fit the page's design

<history>

At the beginning

First - Mobile only Web sites

Mobile Web

Then shock!

And then

RWD

  • Media Queries
  • Fluid grids
  • Flexible images
Then RWD came, a mixture of new browser features and a coherent way to us them

"Flexible images???"

"That's easy"

"Just send the largest possible image"

"And let the browser resize it"

EAZZZZZZY!!!

That strategy was deployed by Web developers, but it may not surprise you that it turned out to be... sub-optimal.

BLOAT!!!

Mobile experience became better, but slower

"a responsive site" became a synonym of "a slow site"

72% Serve same resources

guypo.com

Serving the same resources to mobile and desktop hurts performance

On mobile the images are much smaller and some of them are not even displayed

Which resources?

Images - over 63%

httparchive.org

How much can be saved?

Up to™ 72% image data savings

tkadlec.com A small utility I cooked up and Tim Kadlec wrote about showed 72% data savings

data plan abuse

This has turned out to be an abuse of our users data plans, which may be limited.
And abuse of our users' time.

large images mean long loading times

RWD sites got a reputation of being slow Web sites, mainly because of images.

People demanded a solution

Turned to the mailing lists

Proposals!

Y U NO MEDIA ATTR

Moar proposals!

Y U NO CSS

And MOAR!!!

Y U NO JS

And the RICG was born

<picture> proposal

The RICG discussed the issue for a few months and came up with the picture element proposal

srcset proposal

At the same time, Ted O'Connor from Apple proposed the srcset attribute for simple DPR selection

Hixie added it to the spec over the weekend without much public discussion, then tried to retrofit the other use cases into it

A lot of mailing list flame wars. Things got tense.

Picture vs. srcset!

Picture *and* srcset

Florian Rivoal (from Opera) proposed to join forces. the (original) srcset syntax was adopted into the picture syntax, since they covered different use cases

Browsers weren't convinced

9 months later

I prototyped picture in WebKit. Marcos and I Talked at TPAC. Marcos speced the proposal. Use cases doc. FPWD

But nothing substantial happened

Src-N

Then after a long while of little progress

SrcN was proposed by TabAtkins and John Mellor, after a couple of meetups we had with John.

(John Mellor & Tab Atkins hashed it out over a bottle of wine, the legend says)

It resolved all the use case, in a single element, but got resistence from some browser people.

Moar proposals???

Y U NO *

There was a hecktic time at the end of last year, where proposals were coming in twice a day. But now this is all behind us.

Back to picture

The following an IRC chat I had with Simon Pieters, we figured out a way to gather up all the good pieces

from Src-N and wrap it inside something that looks really like the original picture syntax,

only significantly easier to spec, implement and maintain.

TabAtkins then revived the picture spec (basically rewrote most of it), and we've been working on it with Tab and

Simon ever since.

Mozilla were positive

Browsers liked it way better

Blink tho

Blink still resisted on implementation grounds, basically missing infrastructure.

So I got busy

Basically, I started tackling the infrastructure issues that were the cause for Blink's resistence, in order to defuse it

And realized that if I'd keep doing that in evenings, it's gonna take a looooong while

So, crowdfunding to the rescue

Patches got landing

~80 patches later

Blink work is done

WebKit is half way through

WebKit work still underway

Shipped in Chrome 38

A group effort

XXXXXXX - Talk about Wilto and Geri and stuff regarding the IndieGoGo thing

The RICG & whatwg/blink folks are working together on the spec, to make it as awesome as possible. Mozilla is also heavily involved

The implementation in Blink and Gecko is almost done.

</history>

15:00

<syntax>

Picture 2.0™

Let's take a closer look at each one of the parts combining the latest spec.

The srcset 'x' part

The oldest part of the spec, goes back to the WHAWG 2012 days

Use case

"Retina images"

Load hi-res images

on hi-res devices

Load fixed width high resolution images only on high resolution devices

Note that this is impacting all Websites. Not just responsive ones.

The syntax

<img src="1x.jpg" 
    srcset="2x.jpg 2x, 2.6x.jpg 2.6x" 
     alt="A cat. On the phone.">
You can mix srcset's x descriptor with the overall picture syntax

The <picture> part

Use case

Art direction

The syntax

<picture>
     <source media="(min-width: 45em)" 
               srcset="large.jpg">
     <source media="(min-width: 18em)" 
               srcset="medium.jpg">
     <img src="small.jpg" alt="The president.">
</picture>

Here you can see the syntax parts that make up the "picture" part of the spec, namely the picture element and its source children.

Each one of the source children, as well as the img child define an image that fits into a certain responsive breakpoint.

Use case #2 - MIME type fallback

Today's Web have become fragmented when it comes to image formats.

We may not like it, but that's the truth :(

Picture will enable us to have client-side mime type fallback if we're using browser specific image formats, just like all other resources which they're type may or may not be supported (e.g. video, fonts).

Note that it doesn't mean that you can't do server-side content negotiation using the accept header, if that's your thing

But client-side may be more accessible to Web developers that can't mess around with their backend, and it has caching advantages.

Mime type fallback syntax

<picture>
     <source type="image/webp" 
               srcset="prez.webp">
     <source type="image/vnd.ms-photo" 
               srcset="prez.jpx">
     <img src="prez.jpg" alt="The president.">
</picture>

Here you can see the syntax parts that make up the "picture" part of the spec, namely the picture element and its source children.

Each one of the source children, as well as the img child define an image that fits into a certain responsive breakpoint.

The sizes + srcset 'w' descriptor part

The third part is IMO, the most exciting part.

This is the major innovation that the original src-N proposal brought

both responsive and adaptive designs and even if hi res displays are not involved.

use case

variable width images

The syntax

<img src="otherpic.jpg" 
     sizes="50vw"
     srcset="cat100.jpg 100w, 
             cat200.jpg 200w, cat400.jpg 400w,
             cat800.jpg 800w, cat1600.jpg 1600w"
     alt="A cat. On the phone.">

Didn't add an example,so go over this example in length!

The syntax

<img src="otherpic.jpg" 
     sizes="(max-width: 30em) 100vw, 
          (max-width: 50em) 50vw, 
          calc(33vw - 100px)"
     srcset="cat100.jpg 100w, 
             cat200.jpg 200w, cat400.jpg 400w, 
             cat800.jpg 800w, cat1600.jpg 1600w"
     alt="A cat. On the phone.">

Didn't add an example,so go over this example in length!

`sizes` is an optimization

If you can't be bothered with it, just specifying multiple resources would also give you benefit.

All together now

<picture>
    <source media="(min-width: 80em)"
            srcset="fixed_with_bg.1x.jpg 1x, 
                    fixed_with_bg.2x.jpg 2x">
    <img src="otherpic.jpg" 
         sizes="(max-width: 30em) 100vw, 
                (max-width: 50em) 50vw, 
                calc(33vw - 100px)"
         srcset="cat100.jpg 100w, cat200.jpg 200w, 
                 cat400.jpg 400w, ..."
         alt="A cat. On the phone.">
</picture>

<support>

(fragments)

I started out by implementing srcset's x descriptor. It shipped in Chrome/Opera stable a few months back.

sizes and the 'w' descriptor in srcset are done. I'll probably aim to ship it in M37

picture work has started and is moving along nicely - Christian is helping with stable state

Implemention (almost) done! srcset 'x' descriptor already shipped sizes and srcset - behind a flag picture - almost done Hopefully shipping soon!
Planned to ship in Firefox 33!!! John Schoenick from Mozilla is currently working on it. Likely to hit the dev version soon.
Showing interest

They're showing up on IRC and mailing lists, asking good questions.

Implemented & shipped srcset's 'x' descriptor

sizes and srcset are fully implemented

Some infra is missing for picture, but I'll work on it soon

</support>

<advice>

So, what do we tell our users?

To picturefill or

OTOH, picture has a good fallback mechanism built in. Old browsers get the 1x src image

<picture> or srcset?

I get this question a lot. A simple rule of thumb: If you're doing art-direction, use picture.

Otherwise, you're probably better off with srcset and sizes

Source order matters

<picture>
     <source media="(min-width: 45em)" 
               srcset="large.jpg">
     <source media="(min-width: 18em)" 
               srcset="medium.jpg">
     <img src="small.jpg" alt="The president.">
</picture>

!=

<picture>
     <source media="(min-width: 18em)" 
               srcset="medium.jpg">
     <source media="(min-width: 45em)" 
               srcset="large.jpg">
     <img src="small.jpg" alt="The president.">
</picture>

Source-size order matters

sizes="(max-width: 30em) 100vw, 
     (max-width: 50em) 50vw, 
     calc(33vw - 100px)"

!=

sizes="(max-width: 50em) 50vw,
     (max-width: 30em) 100vw, 
     calc(33vw - 100px)"

'sizes' default value

<img src="otherpic.jpg"
     srcset="pic100.jpg 100w, pic200.jpg 200w">
is identical to
<img src="otherpic.jpg" sizes="100vw"
     srcset="pic100.jpg 100w, pic200.jpg 200w">
When sizes is missing, the default value the browser will use to determine which resource to download and at what size to display it is '100vw'. (So the full width of the viewport)

Intrinsic sizing

Both the 'x' descriptor and the 'w' have an impact on the image's intrinsic size.

That means that when the image dimensions are not otherwise defined, the browser will use these values and the image dimensions from the image data itself to determine the display size of the image.

So, if you set these values incrrectly, they may imapct the way your image is displayed.

Moar intrinsic sizing

Styling

"Picture is a magical span, nothing more"

- Tab Atkins

As far as styling goes, all the style that should impact the image itself, should still go on the img element.

Picture is simply a wrapper around that img, which as far as styling goes, behaves like a span.

Alternative text

alt text stays the same as it always was. On img. Picture has no alt text.

Future compat

Feature detection FTW!

HTMLImageElement.sizes & HTMLPictureElement

The syntax may be extended in the future (e.g. with 'h' descriptors for height-constrained images).

Make sure your polyfill uses feature detection, and backs off when native support is detected.

Which resource was picked?

HTMLImageElement.currentSrc

</advice>

The path not (yet) taken

Client Hints

Content negotiation based

Client sends hints/capabilities Server side logic decides on resource

It's a complementary solution that allows for server side image adaptation, on top or instead of client side one

Future doesn't look bright, since some browser people are opposed to it

But, it may be a thing after all

To sum it up

Responsive images

are coming HERE!

Thanks!

@yoavweiss on Twitter & GitHub

responsiveimages.org

Velocity - NYC, September 2014

yoavweiss.github.io/respimg_velocity_nyc_2014

Questions?