highlights.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import * as PDFJS from 'pdfjs-dist/legacy/build/pdf'
  2. export function buildHighlightElement(highlight, viewer) {
  3. const pageView = viewer.getPageView(highlight.page - 1)
  4. const viewport = pageView.viewport
  5. const height = viewport.viewBox[3]
  6. const rect = viewport.convertToViewportRectangle([
  7. highlight.h, // xMin
  8. height - (highlight.v + highlight.height) + 10, // yMin
  9. highlight.h + highlight.width, // xMax
  10. height - highlight.v + 10, // yMax
  11. ])
  12. const [left, top, right, bottom] = PDFJS.Util.normalizeRect(rect)
  13. const element = document.createElement('div')
  14. element.style.left = Math.floor(left) + 'px'
  15. element.style.top = Math.floor(top) + 'px'
  16. element.style.width = Math.ceil(right - left) + 'px'
  17. element.style.height = Math.ceil(bottom - top) + 'px'
  18. element.style.backgroundColor = 'rgba(255,255,0)'
  19. element.style.position = 'absolute'
  20. element.style.display = 'inline-block'
  21. element.style.scrollMargin = '72px'
  22. element.style.pointerEvents = 'none'
  23. element.style.opacity = '0'
  24. element.style.transition = 'opacity 0.5s'
  25. pageView.div.appendChild(element)
  26. window.setTimeout(() => {
  27. element.style.opacity = '0.3'
  28. window.setTimeout(() => {
  29. element.style.opacity = '0'
  30. }, 1000)
  31. }, 0)
  32. return element
  33. }