Skip to content

Commit ef4ffb3

Browse files
committed
fix: position of elements with zoom
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent a28e8ee commit ef4ffb3

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

src/components/PDFElements.vue

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ SPDX-License-Identifier: AGPL-3.0-or-later
88
<div
99
v-if="pdfDocuments.length"
1010
class="pages-container"
11-
:style="{ transform: `scale(${visualScale / scale})`, transformOrigin: 'top center' }"
1211
>
1312
<div v-for="(pdfDoc, docIndex) in pdfDocuments" :key="docIndex">
1413
<div v-for="(page, pIndex) in pdfDoc.pages" :key="`${docIndex}-${pIndex}`" class="page-slot">
@@ -55,7 +54,7 @@ SPDX-License-Identifier: AGPL-3.0-or-later
5554
:key="object.id"
5655
:ref="`draggable${docIndex}-${pIndex}-${object.id}`"
5756
:object="object"
58-
:pages-scale="getRenderPageScale(docIndex, pIndex)"
57+
:pages-scale="getDisplayedPageScale(docIndex, pIndex)"
5958
:page-width="getPageWidth(docIndex, pIndex)"
6059
:page-height="getPageHeight(docIndex, pIndex)"
6160
:read-only="readOnly"
@@ -136,7 +135,6 @@ SPDX-License-Identifier: AGPL-3.0-or-later
136135
</template>
137136

138137
<script>
139-
import debounce from 'debounce'
140138
import PDFPage from './PDFPage.vue'
141139
import DraggableElement from './DraggableElement.vue'
142140
import { readAsPDF, readAsArrayBuffer } from '../utils/asyncReader.js'
@@ -223,16 +221,15 @@ export default {
223221
viewportRafId: 0,
224222
objectIndexCache: {},
225223
zoomRafId: null,
224+
wheelZoomRafId: null,
226225
boundHandleWheel: null,
227-
debouncedApplyZoom: null,
228226
visualScale: this.initialScale,
229227
autoFitApplied: false,
230228
lastContainerWidth: 0,
231229
}
232230
},
233231
mounted() {
234232
this.boundHandleWheel = this.handleWheel.bind(this)
235-
this.debouncedApplyZoom = debounce(this.commitZoom, 150)
236233
this.init()
237234
document.addEventListener('mousemove', this.handleMouseMove)
238235
document.addEventListener('touchmove', this.handleMouseMove, { passive: true })
@@ -248,7 +245,10 @@ export default {
248245
if (this.zoomRafId) {
249246
cancelAnimationFrame(this.zoomRafId)
250247
}
251-
this.debouncedApplyZoom?.clear?.()
248+
if (this.wheelZoomRafId) {
249+
cancelAnimationFrame(this.wheelZoomRafId)
250+
this.wheelZoomRafId = null
251+
}
252252
if (this.boundHandleWheel) {
253253
this.$el?.removeEventListener('wheel', this.boundHandleWheel)
254254
}
@@ -414,24 +414,35 @@ export default {
414414
},
415415
416416
cachePageBounds() {
417-
this.pagesBoundingRects = {}
417+
const nextRects = {}
418418
for (let docIdx = 0; docIdx < this.pdfDocuments.length; docIdx++) {
419419
for (let pageIdx = 0; pageIdx < this.pdfDocuments[docIdx].pages.length; pageIdx++) {
420420
const canvas = this.getPageCanvasElement(docIdx, pageIdx)
421421
if (!canvas) continue
422422
const rect = canvas.getBoundingClientRect()
423-
this.pagesBoundingRects[`${docIdx}-${pageIdx}`] = {
423+
nextRects[`${docIdx}-${pageIdx}`] = {
424424
docIndex: docIdx,
425425
pageIndex: pageIdx,
426426
rect,
427427
}
428428
}
429429
}
430+
this.pagesBoundingRects = nextRects
430431
},
431432
432433
getDisplayedPageScale(docIndex, pageIndex) {
433434
const doc = this.pdfDocuments[docIndex]
434435
if (!doc) return 1
436+
const baseWidth = doc.pageWidths?.[pageIndex] || 0
437+
const rectWidth = this.pagesBoundingRects[`${docIndex}-${pageIndex}`]?.rect?.width || 0
438+
if (rectWidth && baseWidth) {
439+
return rectWidth / baseWidth
440+
}
441+
const canvas = this.getPageCanvasElement(docIndex, pageIndex)
442+
const fallbackRectWidth = canvas?.getBoundingClientRect?.().width || 0
443+
if (fallbackRectWidth && baseWidth) {
444+
return fallbackRectWidth / baseWidth
445+
}
435446
const base = doc.pagesScale[pageIndex] || 1
436447
const factor = this.visualScale && this.scale ? (this.visualScale / this.scale) : 1
437448
return base * factor
@@ -528,7 +539,11 @@ export default {
528539
const factor = 1 - (event.deltaY * 0.002)
529540
const nextVisual = Math.max(0.5, Math.min(3.0, this.visualScale * factor))
530541
this.visualScale = nextVisual
531-
this.debouncedApplyZoom()
542+
if (this.wheelZoomRafId) return
543+
this.wheelZoomRafId = window.requestAnimationFrame(() => {
544+
this.wheelZoomRafId = null
545+
this.commitZoom()
546+
})
532547
},
533548
534549
commitZoom() {

0 commit comments

Comments
 (0)