import { useEffect, useRef, useState } from 'react'; import { Viewer, DocumentGate } from '@embedpdf/react/runtime'; import type { OpenInput } from '@embedpdf/react/runtime'; import { Stage, stagePlugin } from '@embedpdf/react/stage'; import { RenderLayer, renderPlugin } from '@embedpdf/react/render'; import { interactionPlugin, useInteraction } from '@embedpdf/react/interaction'; import { AnnotationLayer, annotationPlugin } from '@embedpdf/react/annotation'; import { measurementPlugin, useMeasurement, useMeasurementState, usePageScale, } from '@embedpdf/react/measurement'; import type { LengthUnit } from '@embedpdf/react/measurement'; import { localEngine } from '@embedpdf/engine'; import './calibrate.css'; const engine = localEngine(); const plugins = [ stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin(), measurementPlugin(), ]; const sketch = async (): Promise => { const response = await fetch('https://www.embedpdf.com/pdfs/sketch.pdf'); return { kind: 'bytes', id: 'sketch', bytes: new Uint8Array(await response.arrayBuffer()) }; }; function Calibration() { const measurement = useMeasurement(); const interaction = useInteraction(); const { calibrationRequest, busy } = useMeasurementState(); const scale = usePageScale(0); const [length, setLength] = useState('20'); const [unit, setUnit] = useState('cm'); const started = useRef(false); // On load: calibrating, so the next line you drag is the known length. useEffect(() => { if (!scale.ready || started.current) return; started.current = true; if (measurement.canCalibrate()) measurement.startCalibration(); }, [measurement, scale.ready]); // The plugin asks for the real length of the line you drew. if (calibrationRequest) { const value = Number(length); return (
{ event.preventDefault(); void measurement .calibrate({ ...calibrationRequest, distance: { value, unit } }) .then(() => interaction.activateTool('distance')); }} > That line is setLength(event.target.value)} />
); } return (
Drag along something you know the length of, then measure with the new scale {scale.measure?.subtype === 'rectilinear' ? scale.measure.ratio : '…'}
); } export default function App() { return ( Loading…

}> {() => ( <> )}
); }