Skip to content

Commit

Permalink
refactor rendering of layout
Browse files Browse the repository at this point in the history
  • Loading branch information
btzr-io committed Nov 15, 2019
1 parent 5ec5531 commit 0fff041
Showing 1 changed file with 40 additions and 21 deletions.
61 changes: 40 additions & 21 deletions src/components/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 0fff041

Please sign in to comment.