question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Curtains with different image sources based on the viewport size

See original GitHub issue

Hey Martin,

Do you have any examples of CurtainsJS working with different image sources depending on the viewport size? It could or could not use the <picture> element. I haven’t seen any material or demos on the subject. Looking at the texture docs I don’t see a straightforward way of doing this sort of thing but I very well could be missing something. Perhaps this sort of thing is what a texture loader could be used for? Any direction would be appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ZachSauciercommented, Dec 4, 2020

In my actual project I used data attributes to specify the image sources at different sizes and used a loop to keep the code more DRY:

const planeTexture = plane.createTexture({
  sampler: "planeTexture"
});

const images = {
  small: {
    src: planeEl.dataset.smallimg,
    img: null,
    minWinWidth: 992
  },
  medium: {
    src: planeEl.dataset.mediumimg,
    img: null,
    minWinWidth: 768
  },
  large: {
    src: planeEl.dataset.largeimg,
    img: null,
    minWinWidth: -1
  },
};

// set the texture source dynamically based on viewport size
const setResponsiveTexture = () => {
  // be careful it takes curtains pixelRatio into account
  const curtainsBBox = curtains.getBoundingRect();

  for(const size in images) {
    const imgInfo = images[size];

    if(curtainsBBox.width >= imgInfo.minWinWidth && plane.userData.planeTexture !== size) {
      plane.userData.planeTexture = size;
      
      if(imgInfo.img) {
        planeTexture.setSource(imgInfo.img);
      }
      else {
        const image = new Image();
        image.addEventListener('load', () => {
          imgInfo.img = image;
          planeTexture.setSource(imgInfo.img);
        });
        image.crossOrigin = "anonymous";
        image.src = imgInfo.src;
      }

      break;
    }
  }
};

setResponsiveTexture();

plane.onAfterResize(() => {
  setResponsiveTexture();
});

Thanks again for the help!

1reaction
martinlaxenairecommented, Dec 2, 2020

My first answer may not have been clear, sorry about that. The thing is, you cannot upload a texture to the GPU if the image source itself is not already loaded (this is not a curtainsjs limitation, it’s just how WebGL works). That being said, this is up to you to load the right image and use it as your texture source whenever you want.

I’ve made this quick codepen to demonstrate how to achieve what I think you are trying to do: https://codepen.io/martinlaxenaire/pen/abmvyNG

Was this helpful?

Cheers,

Read more comments on GitHub >

github_iconTop Results From Across the Web

curtains.js change texture source based on viewport size
... we'll cache the image\\n },\\n };\\n \\n // set the texture source dynamically based on viewport size\\n const setResponsiveTexture = () =>...
Read more >
Responsive images - Learn web development | MDN
In this article, we'll learn about the concept of responsive images — images that work well on devices with widely differing screen sizes, ......
Read more >
Load different images with HTML or CSS based on img size ...
It loads image based on viewport. In the question title it clearly says based on image size not screen size .
Read more >
Responsive Images in Practice - A List Apart
The first problem we'll tackle: getting our images to scale efficiently across varying viewport widths and screen resolutions.
Read more >
How to use Resolution Switching for Viewport based Image ...
The srcset attribute provides the browser with a set of image sources to choose from, including the size of each. It accepts a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found