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

Optimize lots of previews rendering #920

Open
skjnldsv opened this issue Sep 1, 2023 · 0 comments
Open

Optimize lots of previews rendering #920

skjnldsv opened this issue Sep 1, 2023 · 0 comments
Labels
performances Performances issues and optimisations

Comments

@skjnldsv
Copy link
Contributor

skjnldsv commented Sep 1, 2023

We could try to resize on demand, but that means we would have to generate the preview as we scroll.

async resizeImageWithCanvas(image: HTMLImageElement): Promise<string|null> {
return new Promise((resolve) => {
const width = image.width
const height = image.height
// Calc scale up factor
const f = height < width ? PREVIEW_SIZE / height : PREVIEW_SIZE / width
try {
// Create your canvas
const canvas = document.createElement('canvas')
canvas.height = canvas.width = PREVIEW_SIZE
const ctx = canvas.getContext('2d')
const posX = (width * f - PREVIEW_SIZE) / 2 * -1
const posY = (height * f - PREVIEW_SIZE) / 2 * -1
ctx.drawImage(image, posX, posY, width * f, height * f)
canvas.toBlob((blob) => {
const url = URL.createObjectURL(blob)
resolve(url)
})
} catch (e) {
resolve(null)
}
})

Maybe using onload to fetchThePreview so that it gets replaced as we scroll with the lazy loading?
Or use an intersectObserver and trigger the preview Fetch 🤷

@skjnldsv skjnldsv added the performances Performances issues and optimisations label Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performances Performances issues and optimisations
Projects
None yet
Development

No branches or pull requests

1 participant