-
Notifications
You must be signed in to change notification settings - Fork 142
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
Support webp background images #322
Comments
This could be supported by passing a custom function, something like: This is not implemented yet @Component({
selector: 'image',
template: `
<div [lazyLoad]="image" [setBackgroudImage]="setBackgroudImage"></div>
`
})
class ImageComponent {
image = 'https://image.jpg';
setBackgroudImage = (element, imagePath) => {
if (window.CSS.supports(`background-image`, `-webkit-image-set(url('${imagePath}') 1x)`) {
element.style.backgroundImage = `-webkit-image-set(url('${imagePath}') 1x)`;
} else {
element.style.backgroundImage = `url('${imagePath}')`;
}
}
} I guess however that you want to set a default function instead of setting this for every image. In order to fix that we need to fix #323, #304 first. |
@DominikTrenz Declare your custom function setLoadedImage({ element, imagePath, useSrcset }) {
if (window.CSS.supports(`background-image`, `-webkit-image-set(url('${imagePath}') 1x)`) {
element.style.backgroundImage = `-webkit-image-set(url('${imagePath}') 1x)`;
} else {
element.style.backgroundImage = `url('${imagePath}')`;
}
} And then give it to @NgModule({
declarations: [ AppComponent ],
imports: [
BrowserModule,
LazyLoadImageModule.forRoot({ setLoadedImage })
],
bootstrap: [ AppComponent ]
}) Let me know if you run into any problems. |
Hi! Do you have any updates? Does ng-lazyload-image support using image-set for background images? |
Hi @Kolotovkina, So we can define our own function setLoadedImage({ element, imagePath, useSrcset }) {
if (window.CSS.supports(`background-image`, `image-set(url('${imagePath}') 1x)`) {
element.style.backgroundImage = `image-set(url('${imagePath}') 1x)`;
} else {
element.style.backgroundImage = `url('${imagePath}')`;
}
} But what if we want to use function loadImage({ imagePath }: { imagePath: string }) {
// imagePath is a string. And can be: '"cat.png" 1x, "cat-2x.png" 2x, "cat-3x.png" 3x'
// get devicePixelRatio (window.devicePixelRatio)
// create a regex to pick the right image
// load it:
return new Promise((resolve, reject) => {
img = new Image();
img.src = theRightImage;
img.onload = () => resolve(imagePath);
img.onerror = (error: Error) => reject(error);
});
} And then we can use the image: function setLoadedImage({ element, imagePath, useSrcset }) {
if (window.CSS.supports(`background-image`, `image-set(${imagePath})`) {
element.style.backgroundImage = `image-set(${imagePath})`;
} else {
const deafultImage = pick1xImage(imagePath); // Create this function
element.style.backgroundImage = `url('${deafultImage}')`;
}
} And then pass to to ng-lazyload: @NgModule({
declarations: [ AppComponent ],
imports: [
BrowserModule,
LazyLoadImageModule.forRoot({ loadImage, setLoadedImage })
],
bootstrap: [ AppComponent ]
}) and in the template: <img [defaultImage]="defaultImage" [lazyLoad]="`"cat.png" 1x, "cat-2x.png" 2x, "cat-3x.png" 3x'`"> So it should be possible but you have to do some work. |
I'm using webp format images as background images, which needs a fallback for browsers that doesn't support it.
The fallback looks like this:
Can you support this?
The text was updated successfully, but these errors were encountered: