Progressive image rendering
Render Conf 2017
Everybody loves fast sites
Why images are important
What we can do to reduce their impact
Tradeoffs
Myself
Images on the web
Engage in discussions on JS/CSS
Images represent 2/3 of the bytes of pages
To use or not to use images
Try not to request an image
Also one less file to keep around (or many if responsive), and one less source file (eg PSD).
Flat design and minimalistic
Rounded corners, gradients, shadows.
Embrace it?
Fewer images in repo
Using CSS to hide an image won't prevent the browser from requesting the image. The browser requests any image referenced by the ` ` tag regardless of the stylesheet.
Using CSS to hide an image won't prevent the browser from requesting the image. The browser requests any image referenced by the ` ` tag regardless of the stylesheet.
Optimise your images
really
Jpegoptim, Imageoptim
We have budget for other things.
Structural Similarity
Picture element and srcset
1 column
2 columns
3 columns
Illustration from WHATWG HTML spec
It is difficult to maintain HTML and CSS in sync. Imagine we are using LESS/SASS variables to define the breakpoints. How do we import and use them in the markup?
Say you make a change in CSS, you need to make sure the HTML is also updated.
I feel like browsers don't do enough.
Most users don’t scroll through the whole page, and it makes sense to delay the request of images below the fold until they are needed.
The good thing about doing it yourself is that it gives you fine tuning on when to load images. You can decide when to trigger the request, and what image you want to load. You can implement your own “responsive images” approach, choosing an image according to, for instance, available space.
Combine regular images and lazy-loaded ones
Checking the dimensions of elements trigger relayouts
Binding blindly on scroll can produce stuttering
Cache dimensions and positions of some elements
and use throttling and debouncing to execute the scroll callback fewer times.
There is an experimental API called IntersectionObserver that suits very well this use case. Instead of binding to the scroll event, and then go through the lazy-loaded images, IntersectionObserver allows us to subscribe to an event triggered when the image enters the rendered area (viewport).
IntersectionObserver
IntersectionObserver can also be used to lazy-load other elements on the page. In Spotify we have used it to implement an infinite list React component, so we subscribe to either rows or pages of rows.
What to show while the image is loading
Hero images and Text on images
Examples of Progressive Image Loading
Take a flight with Norwegian and use their Wi-Fi. Or go to a place where good internet connection is a luxury.
Medium
- Also known as blur-up technique
- Also on some other sites like http://qz.com/
- Noticeable in slower networks
How it's done
Use <div> to render the image
Request small thumbnail
Draw thumbnail to <canvas/> and apply blur effect
Request large image
Render large image and hide <canvas/>
How do users perceive it?
Getting creative with SVGs
How it works
This can be applied directly to SVGs. However, the whole talk is focused on bitmap images, for which we don't have any SVG line unless we vectorize it.
But there is hope.
Drawing bitmap images
Canny Edge Detector
- No need to vectorize an image
- We can tweak how many lines we want to generate. Eg prune short ones. Can be improved a lot.
Should we do this?
Just because you can it doesn't mean you should
It might be enough with using progressive JPEGs...
or not using anything at all.
Don't couple. Make it revertible.
Summary
Reduce requests
Choose the right format and optimise
Embrace responsive images
Try to lazy load
Innovate!
The Web is fun .
Open
I learned from other sites
Never stop exploring and experimenting.