-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Amr Wagdy
committed
Apr 10, 2023
1 parent
65f08fd
commit 912846d
Showing
6 changed files
with
57 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,9 @@ | ||
export const loadImageAsPromise = async (src, index, cb) => { | ||
export const loadImageAsPromise = (src, cb) => { | ||
const image = new Image(); | ||
|
||
image.src = src; | ||
|
||
return new Promise((reslove) => { | ||
image.onload = () => { | ||
reslove(image); | ||
|
||
if (cb) { | ||
cb(image, index); | ||
} | ||
}; | ||
|
||
image.onerror = () => { | ||
reslove(image); | ||
const onImageLoad = () => cb(image); | ||
|
||
if (cb) { | ||
cb(image, index); | ||
} | ||
}; | ||
}); | ||
image.onload = onImageLoad | ||
image.onerror = onImageLoad | ||
}; |
19 changes: 15 additions & 4 deletions
19
src/utils/load-images/load-images-relative-to-container-size.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
import { loadImageAsPromise } from './load-image-as-promise'; | ||
|
||
export const loadImagesRelativeToContainerSize = async (imagesSrcs, cb) => { | ||
await Promise.all(imagesSrcs.map(async (src, index) => { | ||
await loadImageAsPromise(src, index, cb); | ||
})); | ||
|
||
export const loadImagesRelativeToContainerSize = (imagesSrcs, cb, index = 0) => { | ||
const imageSrc = imagesSrcs[index]; | ||
|
||
if (index > (imagesSrcs.length - 1)) return; | ||
|
||
const imageLoadCallback = (image) => { | ||
const _index = index + 1; | ||
|
||
|
||
cb(image, index); | ||
loadImagesRelativeToContainerSize(imagesSrcs, cb, _index); | ||
} | ||
|
||
loadImageAsPromise(imageSrc, imageLoadCallback); | ||
}; |