Skip to content

Commit 8212a5c

Browse files
committed
🎨 #9068
1 parent cf5948d commit 8212a5c

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

app/electron/main.js

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ try {
5050
}
5151

5252
const hotKey2Electron = (key) => {
53-
console.log(key)
5453
if (!key) {
5554
return key;
5655
}

app/src/asset/anno.ts

+16-42
Original file line numberDiff line numberDiff line change
@@ -636,62 +636,36 @@ const copyAnno = (idPath: string, fileName: string, pdf: any) => {
636636
}, Constants.TIMEOUT_DBLCLICK);
637637
};
638638

639-
640-
const getPage = (element: Element): Element | null => {
641-
if (!element) {
642-
return null;
643-
} else if (element.classList.contains("page")) {
644-
return element;
645-
}
646-
return getPage(element.parentElement);
647-
};
648-
649-
const getCaptureCanvas = async (page: any, captureScale: number) => {
650-
const viewport = page.getViewport({scale: captureScale * window.pdfjsLib.PixelsPerInch.PDF_TO_CSS_UNITS});
651-
// Support HiDPI-screens.
652-
const outputScale = window.devicePixelRatio || 1;
653-
639+
const getCaptureCanvas = async (pdfObj: any, pageNumber: number) => {
640+
const pdfPage = await pdfObj.pdfDocument.getPage(pageNumber);
641+
const viewport = pdfPage.getViewport({scale: window.devicePixelRatio || 1});
654642
const canvas = document.createElement("canvas");
655-
canvas.width = Math.floor(viewport.width * outputScale);
656-
canvas.height = Math.floor(viewport.height * outputScale);
657-
canvas.style.width = Math.floor(viewport.width) + "px";
658-
canvas.style.height = Math.floor(viewport.height) + "px";
643+
canvas.width = Math.floor(viewport.width);
644+
canvas.height = Math.floor(viewport.height);
659645

660-
await page.render({
646+
await pdfPage.render({
661647
canvasContext: canvas.getContext("2d"),
662-
transform: outputScale !== 1
663-
? [outputScale, 0, 0, outputScale, 0, 0]
664-
: null,
665648
viewport: viewport
666649
}).promise;
667650

668651
return canvas;
669652
};
670653

671654
async function getRectImgData(pdfObj: any) {
672-
const pageElement = getPage(rectElement.firstElementChild);
655+
const pageElement = hasClosestByClassName(rectElement, "page");
673656
if (!pageElement) {
674657
return;
675658
}
676-
const cavasElement = pageElement.querySelector(".canvasWrapper canvas") as HTMLCanvasElement;
677-
let scale;
678-
const trueWith = cavasElement.getBoundingClientRect().width;
679-
if (trueWith <= 0) {
680-
scale = window.devicePixelRatio || 1;
681-
} else {
682-
scale = cavasElement.width / trueWith;
683-
}
659+
660+
const captureCanvas = await getCaptureCanvas(pdfObj, parseInt(pageElement.getAttribute("data-page-number")));
661+
684662
const rectStyle = (rectElement.firstElementChild as HTMLElement).style;
685-
const captureLocation = {
686-
width: scale * parseFloat(rectStyle.width),
687-
height: scale * parseFloat(rectStyle.height),
688-
top: scale * parseFloat(rectStyle.top),
689-
left: scale * parseFloat(rectStyle.left),
690-
};
691-
692-
const pdfPage = await pdfObj.pdfDocument.getPage(parseInt(pageElement.getAttribute("data-page-number")));
693-
const captureCanvas = await getCaptureCanvas(pdfPage, pdfObj.pdfViewer.currentScale);
694-
const captureImageData = captureCanvas.getContext("2d").getImageData(captureLocation.left, captureLocation.top, captureLocation.width, captureLocation.height);
663+
const scale = (window.devicePixelRatio || 1) / pdfObj.pdfViewer.currentScale / window.pdfjsLib.PixelsPerInch.PDF_TO_CSS_UNITS;
664+
const captureImageData = captureCanvas.getContext("2d").getImageData(
665+
scale * parseFloat(rectStyle.left),
666+
scale * parseFloat(rectStyle.top),
667+
scale * parseFloat(rectStyle.width),
668+
scale * parseFloat(rectStyle.height));
695669

696670
const tempCanvas = document.createElement("canvas");
697671
tempCanvas.width = captureImageData.width;

0 commit comments

Comments
 (0)