Files
faunosteo/apps/app/src/components/editor/annotations/AnnotationOverlay.tsx
T

154 lines
4.0 KiB
TypeScript

import type { Ref } from 'react'
import type { Geometry, Point } from '@faunosteo/core-wasm/types'
import type { Correction } from '@/editor/annotations/correction'
import type { Frame } from '@/editor/annotations/frame'
import { isEditable } from '@/editor/annotations/geometryEdit'
export interface OverlayProps {
svgRef: Ref<SVGSVGElement>
/** The composed root's own attributes: the overlay shares its coordinates. */
viewBox: string
ratio: string
frame: Frame | null
selected: Geometry | null
dragged: Geometry | null
pending: Geometry | null
tracing: Point[] | null
correction: Correction | null
}
const STROKE = { fill: 'none', vectorEffect: 'non-scaling-stroke' } as const
function Outline({
geometry,
dashed,
colour,
}: {
geometry: Geometry
dashed: boolean
colour: string
}) {
const style = {
...STROKE,
stroke: colour,
strokeWidth: 2,
strokeDasharray: dashed ? '6 4' : undefined,
}
if (geometry.type === 'circle') {
return (
<circle cx={geometry.cx} cy={geometry.cy} r={geometry.r} {...style} />
)
}
if (geometry.type === 'polygon') {
return <polygon points={geometry.points.join(' ')} {...style} />
}
return null
}
function Vertices({ geometry, size }: { geometry: Geometry; size: number }) {
if (geometry.type !== 'polygon') return null
return (
<>
{geometry.points.map(([x, y], index) => (
<circle
key={index}
cx={x}
cy={y}
r={size}
fill="white"
stroke="var(--color-ink)"
strokeWidth={1.5}
vectorEffect="non-scaling-stroke"
/>
))}
</>
)
}
/** The hollow cross of the position the user chose, drawn apart from the proposal. */
function HollowCross({
at,
size,
}: {
at: { x: number; y: number }
size: number
}) {
const style = { ...STROKE, stroke: 'var(--color-ink)', strokeWidth: 2 }
return (
<g data-testid="overlay-click-cross">
<path
d={`M ${at.x - size} ${at.y - size} L ${at.x + size} ${at.y + size}`}
{...style}
/>
<path
d={`M ${at.x - size} ${at.y + size} L ${at.x + size} ${at.y - size}`}
{...style}
/>
</g>
)
}
function TracingPath({ points, size }: { points: Point[]; size: number }) {
if (points.length === 0) return null
const shape: Geometry = { type: 'polygon', points }
return (
<>
<polyline
points={points.join(' ')}
{...STROKE}
stroke="var(--color-accent)"
strokeWidth={2}
strokeDasharray="6 4"
/>
<Vertices geometry={shape} size={size} />
</>
)
}
/**
* The page-drawn overlay (docs/ANNOTATION-RENDERING.md « Editor overlay »): numbers only,
* no annotation. Selection outline, vertex circles, the tracing in progress, the pending
* or dragged outline, a corrected position with the click marked apart. It never takes
* the pointer: the canvas box does.
*/
export default function AnnotationOverlay(props: OverlayProps) {
const { frame, selected, dragged, pending, tracing, correction } = props
const size = frame ? Math.max(frame.w, frame.h) / 200 : 1
const shown = dragged ?? selected
return (
<svg
ref={props.svgRef}
aria-hidden="true"
data-testid="annotation-overlay"
viewBox={props.viewBox}
preserveAspectRatio={props.ratio}
className="pointer-events-none absolute inset-0 h-full w-full"
>
{shown && isEditable(shown) && (
<>
<Outline geometry={shown} dashed colour="var(--color-ink)" />
<Vertices geometry={shown} size={size} />
</>
)}
{pending && (
<Outline
geometry={pending}
dashed={false}
colour="var(--color-accent)"
/>
)}
{correction && (
<>
<Outline
geometry={{ type: 'circle', ...correction.circle }}
dashed
colour="var(--color-accent)"
/>
<HollowCross at={correction.click} size={size * 1.5} />
</>
)}
{tracing && <TracingPath points={tracing} size={size} />}
</svg>
)
}