highlights.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. export function buildHighlightElement(highlight, wrapper) {
  2. const pageView = wrapper.viewer.getPageView(highlight.page - 1)
  3. const viewport = pageView.viewport
  4. const height = viewport.viewBox[3]
  5. const rect = viewport.convertToViewportRectangle([
  6. highlight.h, // xMin
  7. height - (highlight.v + highlight.height) + 10, // yMin
  8. highlight.h + highlight.width, // xMax
  9. height - highlight.v + 10, // yMax
  10. ])
  11. const [left, top, right, bottom] = wrapper.PDFJS.Util.normalizeRect(rect)
  12. const element = document.createElement('div')
  13. element.style.left = Math.floor(pageView.div.offsetLeft + left) + 'px'
  14. element.style.top = Math.floor(pageView.div.offsetTop + top + 10) + 'px'
  15. element.style.width = Math.ceil(right - left) + 'px'
  16. element.style.height = Math.ceil(bottom - top) + 'px'
  17. element.style.backgroundColor = 'rgba(255,255,0)'
  18. element.style.position = 'absolute'
  19. element.style.display = 'inline-block'
  20. element.style.scrollMargin = '72px'
  21. element.style.pointerEvents = 'none'
  22. element.style.opacity = '0'
  23. element.style.transition = 'opacity 1s'
  24. wrapper.viewer.viewer.append(element)
  25. return element
  26. }