react-pdf-annotations
Version:
Set of React components for PDF annotation
31 lines (24 loc) • 769 B
JavaScript
import { isHTMLCanvasElement } from "../lib/pdfjs-dom";
const getAreaAsPNG = (canvas, position) => {
const {
left,
top,
width,
height
} = position;
const doc = canvas ? canvas.ownerDocument : null; // @TODO: cache this?
const newCanvas = doc && doc.createElement("canvas");
if (!newCanvas || !isHTMLCanvasElement(newCanvas)) {
return "";
}
newCanvas.width = width;
newCanvas.height = height;
const newCanvasContext = newCanvas.getContext("2d");
if (!newCanvasContext || !canvas) {
return "";
}
const dpr = window.devicePixelRatio;
newCanvasContext.drawImage(canvas, left * dpr, top * dpr, width * dpr, height * dpr, 0, 0, width, height);
return newCanvas.toDataURL("image/png");
};
export default getAreaAsPNG;