-
Notifications
You must be signed in to change notification settings - Fork 64
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
Background threshold sizes and resize throttling #672
Comments
Hey @asterikx |
I would class this as a major bug. I came here to open a similar ticket, having just tried the background version of this. Off the top of my head, I'd suggest:
|
@tremby Agreed that the experience can be better here. This is something our team will be looking at in the near future. Also if anyone is feeling up for it, we'd happily accept a PR as well 👍 |
As mentioned in another ticket just now, I actually wrote my own version of this yesterday. I'll ask my client if I can release the code. I'm quite happy with it.
The idea is that by quantizing the aspect ratios a new version will not be requested by the browser unless the aspect or size changes significantly enough to make it worthwhile. I did not yet implement preloading the new version, if it is to switch. |
Hey all 👋 @asterikx @tremby I just wanted to take a moment to talk about some of the work we've done to improve the behavior of this component. This work (already released) revolves around this core idea:
Below I will try to outline the changes we've already made and the changes to come. Biggest IssuesThe biggest issue I found with the pre Main Points
PR #782 Commit Message Better, But Not Best (Yet)Resizing and Debouncing
These issues have been addressed and the behavior should be much better, but it isn't the best it could be (see the next steps section). Next StepsWhile #782 improves things a lot, it doesn't go all the way in my opinion. For one, this is still an issue:
I'd also like to enforce this kind of thresholding:
An iteration of the improved/improving
Ideally, I'd like to connect the concept of providing some “breakpoint dimensions” (a set of values that help constrain image fetching) to the
The above can also be substituted for the If we could do the above I think those changes will make doing something like the below either easier or decrease its appeal.
|
Honestly I think you're significantly overcomplicating it.
Let the browser handle this! Chrome for one already does exactly this. It won't fetch a smaller item from the srcset if it already has a larger one. All you need to give the browser is a srcset of all available resolutions for the current aspect ratio, and a hint about what the render size is via the
This is what my scheme outlined above achieves by quantizing the current aspect ratio. Everywhere you talk about target widths, you should instead be thinking about target aspect ratios. Lean on Under the scheme I outlined in my previous comment, the component only ever changes its I've asked for permission to show my code. I'll post a codepen or something if I'm allowed, so you can see how it works in action. But in the mean time, and in case it's denied, I'll explain the aspect quantization I implemented more fully.
My quantization scheme works by first mapping the aspect ratio (which could be 0 all the way to infinity) to a finite range.
Now we have a number from 0 to 2 which describes all possible aspect ratios (and even some impossible ones, 0 and infinity). What's more, in this mapping portrait and landscape aspects are comparable: in this mapped range 0.1 is the portrait equivalent of 1.9: unmapped these would be aspect ratios 1:10 and 10:1. This means we can easily choose any number of proportionally-equally-spaced aspect ratios over the entire spectrum. So we pick a number of steps. This is an accuracy vs performance question and may be unique to any deployment. It should be an option in the final component. I picked 12 steps, so 13 possible aspects. 0 and infinity are included in this and will never be truly used, so in reality I have 11 aspect ratios which might be asked for. 5 portrait, 5 landscape, and square. So we take our ideal aspect ratio, map it to the 0..2 range as described above, then quantize it linearly within this space to one of the allowable steps, then unmap it again. The output from this is our target aspect ratio. This is the aspect ratio we use to generate the srcset. I'll comment with code and/or a codepen if/when I get permission. |
@tremby When I say:
I mostly mean that the heavy lifting is done by CSS/browser: I see you've mentioned using srcsets a lot; is there an example scenario you could give where a srcset is more desirable over a single src? Maybe I'm just misunderstanding things here. Can you use a srcset for background images? Update: background-image: image-set( "foo.png" 1x,
"foo-2x.png" 2x,
"foo-print.png" 600dpi ); |
I'm closing this issue for now as current releases have largely fixed this problem. That said, the |
There are myriad resources about srcset online; here's an example. The browser knows much more than the developer about the current circumstances, in terms of the user's display, their zoom level, the viewport size, their bandwidth limitations, which assets it has already has downloaded and cached, and so on. The whole point of srcset is to let the browser pick an image size which fits the circumstances. Under the scheme I implemented, the markup and stylesheet do not need to be mutated on resizes, until a aspect ratio threshold has been hit. Additionally, with a srcset and a good guess at the initial aspect ratio we are much more likely to load the correct image at initial page load time (which is the subject of one section of the page I linked above).
That will be lovely in future, but it is only a draft in the spec and browser support is not good enough yet; particularly it's Safari on both desktop and IOS whose support is inadequate. See caniuse but be sure to read the footnotes; the 93% global support metric is misleading. For now, using an Hopefully full image-set support isn't too far in the future. Once it's ready, it'll certainly be the best fit, but the algorithm I'm describing above to choose the set of images via a quantized aspect ratio would still be 100% applicable, and that's what I'm trying to push here. It's a whole lot less code than your old or new versions of this component (the important parts of my implementation are just a few lines, and my whole component is less than 100 including comments), and I'd wager better-behaved and more efficient. Perhaps you're dead set on using |
Before you submit:
Question
I'm using
Background
on a div whose height is set relative to the browser's viewport. This is very inefficient because the image is cropped to the exact pixel height of the div:Pixel-precise images fetched while resizing the window (timeframe is ~2s):
Question
What's the best approach to:
I appreciate any help
The text was updated successfully, but these errors were encountered: