Skip to content

Commit

Permalink
Fix flicker when loading image
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrini committed Jul 15, 2024
1 parent aa236d9 commit c730436
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions src/BookReader/PageContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/** @typedef {import('./BookModel.js').PageModel} PageModel */
/** @typedef {import('./ImageCache.js').ImageCache} ImageCache */

import { sleep } from './utils.js';


export class PageContainer {
/**
Expand Down Expand Up @@ -66,37 +68,35 @@ export class PageContainer {

if (!alreadyLoaded) {
this.$container.addClass('BRpageloading');
}

if (!alreadyLoaded && nextBestLoadedReduce) {
// If we have a slightly lower quality image loaded, use that as the background
// while the higher res one loads

if (nextBestLoadedReduce) {
const nextBestUri = this.page.getURI(nextBestLoadedReduce, 0);
if ($oldImg) {
if ($oldImg.data('src') == nextBestUri) {
// Do nothing! It's already showing the right thing
} else {
// We have a different src, need to update the src
this.imageCache.image(this.page.index, nextBestLoadedReduce, $oldImg[0]);
}
// while the higher res one loads

Check failure on line 75 in src/BookReader/PageContainer.js

View workflow job for this annotation

GitHub Actions / tests

Trailing spaces not allowed
const nextBestUri = this.page.getURI(nextBestLoadedReduce, 0);
if ($oldImg) {
if ($oldImg.data('src') == nextBestUri) {
// Do nothing! It's already showing the right thing
} else {
// We don't have an old image, so we need to create a new one
$oldImg = this.imageCache.image(this.page.index, nextBestLoadedReduce);
$oldImg.prependTo(this.$container);
// We have a different src, need to update the src
this.imageCache.image(this.page.index, nextBestLoadedReduce, $oldImg[0]);
}
} else {
$oldImg?.remove();
// We don't have an old <img>, so we need to create a new one
$oldImg = this.imageCache.image(this.page.index, nextBestLoadedReduce);
$oldImg.prependTo(this.$container);
}

this.$img
.one('load', (ev) => {
$oldImg?.remove();
this.$container.removeClass('BRpageloading');
});
} else {
$oldImg?.remove();
}

this.$img
.one('load', async (ev) => {
this.$container.removeClass('BRpageloading');
// `load` can fire a little early, so wait a spell before removing the old image
// to avoid flicker
await sleep(100);
$oldImg?.remove();
});

return this;
}
}
Expand Down

0 comments on commit c730436

Please sign in to comment.