import { useRef, useState } from 'react';
import { Viewer, DocumentGate } from '@embedpdf/react/runtime';
import type { OpenInput } from '@embedpdf/react/runtime';
import { Stage, stagePlugin, useStage } from '@embedpdf/react/stage';
import { RenderLayer, renderPlugin } from '@embedpdf/react/render';
import { localEngine } from '@embedpdf/engine';
import './pins.css';
const engine = localEngine();
const plugins = [stagePlugin(), renderPlugin()];
const welcome = async (): Promise => {
const response = await fetch('https://www.embedpdf.com/pdfs/welcome.pdf');
return { kind: 'bytes', id: 'welcome', bytes: new Uint8Array(await response.arrayBuffer()) };
};
// A pin is a page and a point on it, in points from the page's top-left. No pixels.
interface Pin {
pageIndex: number;
point: { x: number; y: number };
}
function Controls({ last, onClear }: { last: Pin | null; onClear: () => void }) {
const stage = useStage();
return (
);
}
export default function App() {
// One pin to start with: an inch in from the first page's top-left corner.
const [pins, setPins] = useState([{ pageIndex: 0, point: { x: 72, y: 72 } }]);
// Where the pointer went down: a press that moved was a drag to scroll, not a click.
const pressed = useRef({ x: 0, y: 0 });
return (
Loading…
}>
setPins([])} />
(
(pressed.current = { x: event.clientX, y: event.clientY })}
onClick={(event) => {
const moved = Math.hypot(
event.clientX - pressed.current.x,
event.clientY - pressed.current.y,
);
if (moved > 4) return;
// A pointer event, to a point on the page.
const point = page.toPagePoint(event.clientX, event.clientY);
setPins((current) => [...current, { pageIndex: page.pageIndex, point }]);
}}
>
{page.pageIndex === 0 ? (
// Many things at once: the browser maps page points with one matrix.
1 inch
) : null}
{pins
.filter((pin) => pin.pageIndex === page.pageIndex)
.map((pin, index) => {
// A point on the page, to pixels on it: at the last moment.
const at = page.transform.pageToView(pin.point);
return ;
})}