diff --git a/src/components/render.js b/src/components/render.js index 0f56e618..5e082ec5 100644 --- a/src/components/render.js +++ b/src/components/render.js @@ -233,37 +233,56 @@ class CanvasRender extends Component { } renderLayout() { - const { viewport, world } = this.viewer - const { currentPage } = this.context.state + const { world } = this.viewer const pos = new OpenSeaDragon.Point(0, 0) const count = world.getItemCount() // Cache tile data let bounds = null - let tiledImage = null + let nextPage = null + let firstPage = null + let nextPageBounds = null let firstPageBounds = null - for (let i = 0; i < count; i++) { - // Get current page - tiledImage = world.getItemAt(i) - - if (tiledImage) { - // Get page bounds - bounds = tiledImage.getBounds() - // Get first page bounds - if (i === 0) firstPageBounds = bounds - // Auto resize pages to fit first page height - else { - tiledImage.setHeight(firstPageBounds.height, true) + if (count > 0) { + // Page view (single page) + firstPage = world.getItemAt(0) + firstPageBounds = firstPage.getBounds() + + // Book view ( two pages ) + if (count > 1) { + nextPage = world.getItemAt(1) + nextPageBounds = nextPage.getBounds() + + // Auto resize page to fit first page height + if (firstPageBounds.height > nextPageBounds.height) { + nextPage.setHeight(firstPageBounds.height, true) + // Recalculate bounds + nextPageBounds = nextPage.getBounds() + } + + // Auto resize page to fit nextPage + if (nextPageBounds.height > firstPageBounds.height) { + firstPage.setHeight(nextPageBounds.height, true) + // Recalculate bounds + firstPageBounds = firstPage.getBounds() } - // Recalculate bounds - bounds = tiledImage.getBounds() - // Position next page - tiledImage.setPosition(pos, true) - pos.x += bounds.width + } + + // Set position for first page + if (firstPage && firstPageBounds) { + firstPage.setPosition(pos, true) + pos.x += firstPageBounds.width + } + + // Set position for next page + if (nextPage && nextPageBounds) { + nextPage.setPosition(pos, true) + pos.x += nextPageBounds.width } } - // Update viewer zoom + + // Fit pages on viewer and apply bounds this.fitPages() }