Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if image is fully loaded #79

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Check if image is fully loaded #79

wants to merge 6 commits into from

Conversation

aderaaij
Copy link

@aderaaij aderaaij commented Dec 16, 2017

This is a quick and dirty pull request to check if images have already loaded. This will not pass your tests yet, and doesn't yet take in account srcset either, but I wanted to get the ball rolling!

A few thoughts.

I've modified the markAsLoaded function to simply implement a waiting for images function, data-loaded is only added after the image is fully loaded. I can imagine that there might be circumstances where you want to do something as soon as the image is added, so maybe it's an idea to add another data-attribute especially for when the image is fully loaded.

I also made a solution for srcset but it isn't very elegant yet. It uses p-wait-for, which only adds a couple of bytes to the total. I didn't include this solution in my pull request yet, I'd like to discuss it first!

function markAsLoaded(element) {
  if (element.dataset.srcset) {
    pWaitFor(() => {
      if (element.currentSrc !== '') {
        return true
      }
      return false
    }).then(() => {
      const imgLoad = new Image()
      imgLoad.onload = () => {
        element.dataset.loaded = true
      }
      imgLoad.src = element.currentSrc
    })
  } else {
    const imgLoad = new Image()
    imgLoad.onload = () => {
      element.dataset.loaded = true
    }
    imgLoad.src = element.dataset.src
  }
}

@aderaaij aderaaij changed the title Check if image for loaded Check if image is fully loaded Dec 16, 2017
@jimblue
Copy link

jimblue commented Dec 16, 2017

@aderaaij this could help don't you think?

https://github.com/marlospomin/turtle/blob/master/src/turtle.js

@aderaaij
Copy link
Author

@jimblue Yeah it's done in a nice way, somewhat similar actually! Only it doesn't have solutions for srcset included.

@jimblue
Copy link

jimblue commented Dec 16, 2017

Indeed @aderaaij... does the actual PR work for background image on your side? I doesn't here

@aderaaij
Copy link
Author

Good question! No it doesn't, I only check the normal src now. Background image shouldn't be too hard though. I'll have a check later on

@jimblue
Copy link

jimblue commented Dec 16, 2017

Allright. I'll work on this too. 😄

@jimblue
Copy link

jimblue commented Dec 16, 2017

I was thinking about a nice way to make the loading feeling really smooth:

  • observe all .lozad elements
  • preload image outside of intersection (for exemple: rootMargin * 2)
  • mark image as loaded when reaching intersection (rootMargin * 1)

The benefit is to preloaded images outside viewport but showing the animation when reaching viewport.

What do you think?

@aderaaij
Copy link
Author

It's a good idea but I think that would be for users themselves to define. You can override the rootmargin and treshold when calling the script, so everyone can figure out what works smooth for themselves, as long as there is a part in there that tells when the image is fully loaded.

@jimblue
Copy link

jimblue commented Dec 16, 2017

That's true

@aderaaij
Copy link
Author

Added support for background images

@jimblue
Copy link

jimblue commented Dec 17, 2017

Thanks @aderaaij!!!! I can't wait @ApoorvSaxena have a look to this PR 😝 !

@jimblue
Copy link

jimblue commented Dec 21, 2017

Hi @aderaaij! Do you now why checks have failed?

@OlehDutchenko
Copy link
Contributor

@jimblue
Copy link

jimblue commented Dec 21, 2017

oh ok just learn somethin! Thanks @dutchenkoOleg ... Just don't know how to fix this ^^

@aderaaij
Copy link
Author

@jimblue in this pull-request I explained I didn't bother passing all the tests as I wanted to discuss this PR first. I don't know too much about tests, but they're failing because they're expecting an image to be returned right away, so they probably also need the 'loading' script included. Oh and there's one about SRC Set too 😅.

But to be honest I think @ApoorvSaxena is pretty preoccupied, and also it's around x-mas time so there might not be happening too much anyway. So you might have to run with the fork for now!

@jimblue
Copy link

jimblue commented Dec 21, 2017

Hey @aderaaij ! No worries about this, I'm already pretty happy with Lozad!
The PR will come after x-mas for sure. It's good time to stay AFK 🎉

@OlehDutchenko
Copy link
Contributor

@aderaaij PR has conflicts. I had the same problem in my pr #80.
You need to update your fork and rebuild dist scripts for resolving conflicts.

Also, could you please consider processing of an picture element
https://github.com/ApoorvSaxena/lozad.js/blob/master/src/lozad.js#L12

@OlehDutchenko
Copy link
Contributor

Also you need to mark the items that have already started to load.
Otherwise, duplicate method calls for image loading are possible.
As long as the source is being loaded, there is no attribute, and when the check is repeated, the code for downloading will be called again.

Pay attention to these lines of code:

Maybe you need just add new data attribute, when image is loaded.
for example:

  • data-loaded="true" - when data-src inserted as src
  • data-full-loaded="true" - when source is fully loaded

@aderaaij
Copy link
Author

@dutchenkoOleg Thanks for the ideas! I'm completely aware that these don't pass the tests as I just wanted to get some opinion of the library maintainers first. As I say in my opening post:

This is a quick and dirty pull request to check if images have already loaded. This will not pass your tests yet, and doesn't yet take in account srcset either, but I wanted to get the ball rolling!

If I ever hear back about this I will definitely take your suggestions into consideration 👌

@jimblue
Copy link

jimblue commented Feb 7, 2018

Hi @ApoorvSaxena!

Can we have your input on this to move on and start fixing #49?

Thanks

@ApoorvSaxena
Copy link
Owner

@jimblue @aderaaij #86 implementation looks better, let's continue with that unless you want to add anything apart from that

@aderaaij
Copy link
Author

aderaaij commented Feb 13, 2018

@ApoorvSaxena Nope I'm totally good with that!

@aderaaij aderaaij closed this Feb 13, 2018
@jimblue
Copy link

jimblue commented Feb 13, 2018

@ApoorvSaxena yep I'm good with that too !

EDIT: after some test it seems that #86 can't replace this PR as it doesn't check if image is fully loaded...

@perosb perosb mentioned this pull request Mar 3, 2018
@ApoorvSaxena ApoorvSaxena reopened this May 3, 2018
@dlecan
Copy link

dlecan commented Jun 12, 2018

This PR is very interested, I need it, how can I help to make it merged?
Thank you.

@danbilokha
Copy link

it's shame it was never merged :c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants