Lozad.js: Performant Lazy Loading of Images

Post pobrano z: Lozad.js: Performant Lazy Loading of Images

There are a few different „traditional” ways of lazy loading of images. They all require JavaScript needing to figure out if an image is currently visible within the browser’s viewport or not. Traditional approaches might be:

  • Listening to scroll and resize events on the window
  • Using a timer like setInterval

Both of these have performance problems.

Why traditional approaches are not performant?

Both of those approaches listed above are problematic because they work repeatedly and their function triggers **forced layout while calculating the position of the element with respect to the viewport, to check if the element is inside the viewport or not.

To combat these performance problems, some libraries throttle the function calls that do these things, limiting the number of times they are done.

Even then, repeated layout/reflow triggering operations consume precious time while a user interacts with the site and induces „junk” (that sluggish feeling when interacting with a site that nobody likes).

There is another approach we could use, that makes use of a new browser API designed specifically to help us with things like lazy loading: the Intersection Observer API.

That’s exactly what my own library, Lozad.js, uses.

What makes Lozad.js performant?

Intersection Observers are the main ingredient. They allow registration of callback functions which get called when a monitored element enters or exits another element (or the viewport itself).

While Intersection Observers don’t provide the exact pixels which overlap, they allow listening to events that allow us to watch if elements enter other elements by X% (configurable), then the callback gets fired. That is exactly our use case when using Intersection Observers for lazy loading.

Quick facts about Lozad.js

  • Light-weight: just 535 bytes minified & gzipped
  • No dependencies
  • Uses the IntersectionObserver API
  • Allows lazy loading of dynamically added elements as well (not just images), though a custom load function

Usage

Install from npm:

yarn add lozad

or via CDN:

<script src="https://cdn.jsdelivr.net/npm/lozad"></script>

In your HTML, add a class to any image you wish to lazy load. The class can be changed via configuration, but „lozad” is the default.

<img class="lozad" data-src="image.png">

Also note we’ve removed the src attribute of the image and replaced it with data-src. This prevents the image from being loaded before the JavaScript executes and determines it should be. It’s up to you to consider the implications there. With this HTML, images won’t be shown at all until JavaScript executes. Nor will they be shown in contexts like RSS or other syndication. You may want to filter your HTML to only use this markup pattern when shown on your own website, and not elsewhere.

In JavaScript, initialize Lozad library with the options:

const observer = lozad(); // lazy loads elements with default selector as ".lozad"
observer.observe();

Read here about the complete list of options available in Lozad.js API.

Demo

See the Pen oGgxJr by Apoorv Saxena (@ApoorvSaxena) on CodePen.

Browser support

Browser support is limited, as the feature is relatively new. Use the official IntersectionObserver polyfill to overcome the limited support of this API.


Lozad.js: Performant Lazy Loading of Images is a post from CSS-Tricks

How to Draw a Stack of Books and an E-Book Reader in Adobe Illustrator

Post pobrano z: How to Draw a Stack of Books and an E-Book Reader in Adobe Illustrator

Final product image
What You’ll Be Creating

In this simple tutorial, you will learn how to use the Mesh Tool in Adobe Illustrator to create a stack of books and an e-reader!

If you want to skip the tutorial and just use these books in your work, you can purchase the Stack of books and e-book from GraphicRiver!

Stack of Books and E-Book

1. How to Draw a Book

Step 1

We will begin by drawing the top cover of the red book using the Mesh Tool (U).

First, simply draw a rectangle and fill it with the #B9201F color. Next, move around its corner nodes so it looks like a cover of a book in perspective.

Now, grab the Mesh Tool (U) and continue by creating a Mesh grid on the cover by clicking in places you want nodes to be in. Follow the screenshot for reference.

Smooth out the bottom left corner, turning it from sharp to round.

There must be three Mesh lines close to each other on the sides and the bottom of the shape. Color the nodes on the middle lines with #EA4242 (nodes that are selected in the screenshot below).

draw top cover with mesh

Step 2

Continue coloring the selected nodes in each part of the screenshot! You will need first #A91716, and then #D13130.

color cover

Step 3

Draw a side for the cover by creating a narrow, #AB1817 rectangle, smoothing out the corners, and coloring its middle row of Mesh with #DC3838.

draw side of cover

Step 4

Attach the side to the cover as indicated in the screenshot below.

assemble top cover

Step 5

Start drawing the spine of the book!

Draw a narrow, #4E4142 rectangle. Then move to Effect > Warp > Arc and apply a 26% Bend to it.

Go to Object > Expand Appearance to turn the result into a regular path.

bend line with warp

Step 6

Create a copy of the shape we just made.

Then simply modify the original shape on its right side, dragging its nodes to create a curve.

draw spine

Step 7

Grab the copy we just made and color it with #CA2F2E.

Next, modify it with Mesh by simply dragging the nodes around and color its outer edges with #980605.

draw spine

Step 8

Move these two shapes next to each other, creating the spine of the book.

put together spine

Step 9

Let’s draw the paper part! Modify and color the rectangle as indicated by the nodes selected in the screenshot below.

You will need these colors:

  1. #FEF7DE
  2. #A69980
  3. #F8ECD2
draw mesh base

Step 10

Now, to create an illusion of separate pages, draw a straight line, coloring its Stroke with #CEB789.

Proceed to create multiple copies of it with some intervals in between (I suggest moving the first line a bit down manually, pressing Control-D to create more copies automatically, and deleting the ones you don’t need).

After that, select all the lines and in the Stroke panel, apply Profile1 to them. Flip it if needed—the lines should be thicker on the left side.

create lines

Step 11

Expand Appearance of the lines.

Next, grab the Warp Tool (Shift-R) and double-click on the canvas to bring up the options. You can use the options I did in the screenshot below or your own.

Next, bend the lines slightly to add a bit of realism to our „pages”.

bend pages

Step 12

Copy the outline of the Mesh shape we drew in Step 9.

Place it on top of the pages, select both and right-click, choosing Make Clipping Mask in the drop-down menu.

make clipping mask

Step 13

Change the Opacity of the result to 30% and its Transparency mode to Multiply.

change transparency

Step 14

Overlay the pages over the Mesh.

overlay the pages

Step 15

Draw the left side of the book using Mesh. You will need these colors:

  1. #EEE3CA
  2. #AB9F86
  3. #CDC1A8
  4. #EADAC5
draw the left side

Step 16

Draw the bottom part of the cover using Mesh. Use these colors:

  1. #980605
  2. #CC2F2F
draw the bottom cover

Step 17

Assemble the book out of the parts we made!

assemble the book

2. How to Recolor the Book

Step 1

Now that the most difficult part of this tutorial is done, we will be creating the stack of books by making a few color variants!

Create a copy of the red book and recolor it (only the red parts!) by choosing Edit > Edit Colors > Adjust Colors. Use these options to get a green book:

  • Red: -11%
  • Green: 57%
  • Blue: -31%

Of course, you can play with the percentages however you want to create your own color variants!

recolor the book to green

Step 2

Create three copies of the light green book (aside from the original).

Recolor the first copy using these settings:

  • Red: -54%
  • Green: -25%
  • Blue: -9%
recolor the book to dark green

Step 3

Grab the second copy and recolor it using these:

  • Red: 23%
  • Green: -21%
  • Blue: 0%
recolor the book to orange

Step 4

Create the final, cyan book out of the third copy with these settings:

  • Red: -35%
  • Green: -10%
  • Blue: 58%
recolor the book to cyan

Step 5

Create an uneven stack out of the color variants!

create a stack

3. How to Draw an E-Book Reader

Step 1

We are going to revisit Mesh for a bit in the final parts of this tutorial.

Fill a rectangle with rounded edges with #000002, and then stretch it around a bit. Create a Mesh grid and color the bottom right part of the shape with #3F4246.

draw base of e-book

Step 2

Draw a screen for the e-book by coloring the left top corner of a #EDEFEE rectangle with #999B9C.

draw screen with mesh

Step 3

Draw the sides of the e-book reader, coloring them with #313335, #383B3C, and #000002.

create parts of frame

Step 4

Put the sides together to create a frame.

assemble frame

Step 5

Move the screen into the frame. The left side should be lined up perfectly, and the right side can be left with a gap.

assemble screen

Step 6

Draw the outer frame by applying a thick, #3F4246 stroke to a line, and then Expanding Appearance and sharpening the edges.

draw side with stroke

Step 7

Draw a button by creating a #0B0E10 ellipse, and adding three #3E4145 nodes in the middle of it.

draw button with mesh

Step 8

Assemble the e-book reader: the black base should be on the bottom, with everything else on top of it.

assemble e-book

Step 9

Finally, draw shadows for the reader and the stack of books. Use #B3ADA5.

color shadows

Step 10

Change the Blending Mode of both shadows to Multiply.

change transparency

Step 11

Put everything together!

Result

Awesome Work, You’re Now Done!

Thank you for following along, and please
feel welcome to post your result in the comments. I’ll be looking
forward to seeing it!

In this tutorial, you learned how to
create a stack of colorful books and an e-book reader using the Mesh Tool.

I
hope you’ve enjoyed this tutorial and learned how to use some useful
tools for the future! Check out some of my other vector tutorials on my instructor profile.

You can purchase Stack of books and e-book as well as other similar designs in my portfolio on GraphicRiver.

Stack of Books and E-Book
Stack of Books and E-Book

Agregator najlepszych postów o designie, webdesignie, cssie i Internecie