Download web/src/components/RegionPicker.tsx from loveaa/OneAlign: direct link, hf CLI and curl.
- Browser
- Download file 2.87 kB
-
https://huggingface.co/spaces/loveaa/OneAlign/resolve/main/web/src/components/RegionPicker.tsx
- Command line
-
hf download hf://spaces/loveaa/OneAlign/web/src/components/RegionPicker.tsx
-
curl -L -o RegionPicker.tsx https://huggingface.co/spaces/loveaa/OneAlign/resolve/main/web/src/components/RegionPicker.tsx
2.87 kB
| import {useRef,useState} from 'react' | |
| import type {Box} from '@/lib/grounded' | |
| export default function RegionPicker({image,target,region,onChange}:{image:string;target:Box;region:Box;onChange:(target:Box,region:Box)=>void}){ | |
| const [active,setActive]=useState<'target'|'region'>('target'),[ratio,setRatio]=useState(16/9) | |
| const start=useRef<{x:number;y:number}|null>(null),stage=useRef<HTMLDivElement>(null) | |
| const point=(e:React.PointerEvent)=>{const r=stage.current!.getBoundingClientRect();return {x:Math.max(0,Math.min(1,(e.clientX-r.left)/r.width)),y:Math.max(0,Math.min(1,(e.clientY-r.top)/r.height))}} | |
| const current=active==='target'?target:region | |
| const update=(box:Box)=>onChange(active==='target'?box:target,active==='region'?box:region) | |
| const move=(e:React.PointerEvent)=>{if(!start.current)return;const p=point(e),a=start.current;const x=Math.min(a.x,p.x),y=Math.min(a.y,p.y);update({x:Math.min(x,.98),y:Math.min(y,.98),w:Math.min(1-x,Math.max(.02,Math.abs(p.x-a.x))),h:Math.min(1-y,Math.max(.02,Math.abs(p.y-a.y)))})} | |
| return <div className="region-picker"><div className="selection-tabs"><button type="button" className={active==='target'?'active target':''} onClick={()=>setActive('target')}><i/>圈出目标</button><button type="button" className={active==='region'?'active region':''} onClick={()=>setActive('region')}><i/>圈出观察范围</button><span>在画面上拖动;也可调整下方数值</span></div><div className="selection-stage" ref={stage} style={{aspectRatio:ratio}} onPointerDown={e=>{if(e.button!==0)return;start.current=point(e);e.currentTarget.setPointerCapture(e.pointerId)}} onPointerMove={move} onPointerUp={e=>{move(e);start.current=null}} onPointerCancel={()=>{start.current=null}}><img src={image} alt="目标与观察范围确认画面" draggable={false} onLoad={e=>setRatio(e.currentTarget.naturalWidth/e.currentTarget.naturalHeight)}/><svg viewBox="0 0 1000 1000" preserveAspectRatio="none" aria-hidden="true">{[[region,'region'],[target,'target']].map(([b,n])=>{const r=b as Box;return <rect key={n as string} className={n as string} x={r.x*1000} y={r.y*1000} width={r.w*1000} height={r.h*1000}/>})}</svg><span className="selection-hint">{active==='target'?'红框 · 关注的对象':'绿框 · 观察的位置与边界'}</span></div><div className="box-fields">{([['x','左侧'],['y','顶部'],['w','宽度'],['h','高度']] as const).map(([key,label])=><label key={key}>{label}<input aria-label={(active==='target'?'目标':'范围')+label+'百分比'} type="number" min={key==='w'||key==='h'?2:0} max="100" step="1" value={Math.round(current[key]*100)} onChange={e=>{const b={...current,[key]:Number(e.target.value)/100};b.x=Math.min(.98,Math.max(0,b.x));b.y=Math.min(.98,Math.max(0,b.y));b.w=Math.min(1-b.x,Math.max(.02,b.w));b.h=Math.min(1-b.y,Math.max(.02,b.h));update(b)}}/><span>%</span></label>)}</div></div> | |
| } | |