@@ -17,6 +17,7 @@ import classnames from "classnames"
1717import RegionLabel from "../RegionLabel"
1818import LockIcon from "@material-ui/icons/Lock"
1919import Paper from "@material-ui/core/Paper"
20+ import HighlightBox from "../HighlightBox"
2021// import excludePatternSrc from "./xpattern.png"
2122import excludePatternSrc from "./xpattern.js"
2223
@@ -38,7 +39,10 @@ type Props = {
3839 zoomWithPrimary ?: boolean ,
3940 createWithPrimary ?: boolean ,
4041 showTags ?: boolean ,
42+ realSize ?: { width : number , height : number , unitName : string } ,
4143 showCrosshairs ?: boolean ,
44+ showPointDistances ?: boolean ,
45+ pointDistancePrecision ?: number ,
4246 regionClsList ?: Array < string > ,
4347 regionTagList ?: Array < string > ,
4448 allowedArea ?: { x : number , y : number , w : number , h : number } ,
@@ -51,26 +55,31 @@ type Props = {
5155 onBeginMovePolygonPoint : ( Polygon , index : number ) => any ,
5256 onAddPolygonPoint : ( Polygon , point : [ number , number ] , index : number ) => any ,
5357 onSelectRegion : Region => any ,
54- onBeginMovePoint : Point => any
58+ onBeginMovePoint : Point => any ,
59+ onImageLoaded : ( { width : number , height : number } ) => any
5560}
5661
5762const getDefaultMat = ( ) => Matrix . from ( 1 , 0 , 0 , 1 , - 10 , - 10 )
5863
5964export default ( {
6065 regions,
6166 imageSrc,
67+ realSize,
6268 showTags,
6369 onMouseMove = p => null ,
6470 onMouseDown = p => null ,
6571 onMouseUp = p => null ,
6672 dragWithPrimary = false ,
6773 zoomWithPrimary = false ,
6874 createWithPrimary = false ,
75+ pointDistancePrecision = 0 ,
6976 regionClsList,
7077 regionTagList,
7178 showCrosshairs,
79+ showPointDistances,
7280 allowedArea,
7381
82+ onImageLoaded,
7483 onChangeRegion,
7584 onBeginRegionEdit,
7685 onCloseRegionEdit,
@@ -128,6 +137,10 @@ export default ({
128137 image . current = new Image ( )
129138 image . current . onload = ( ) => {
130139 changeImageLoaded ( true )
140+ onImageLoaded ( {
141+ width : image . current . naturalWidth ,
142+ height : image . current . naturalHeight
143+ } )
131144 }
132145 image . current . src = imageSrc
133146 }
@@ -504,62 +517,16 @@ export default ({
504517 const { iw, ih } = layoutParams . current
505518 return (
506519 < Fragment >
507- < svg
508- key = { r . id }
509- className = { classnames ( classes . highlightBox , {
510- highlighted : r . highlighted
511- } ) }
512- { ...mouseEvents }
513- { ...( ! zoomWithPrimary && ! dragWithPrimary
514- ? {
515- onMouseDown : e => {
516- if (
517- ! r . locked &&
518- r . type === "point" &&
519- r . highlighted &&
520- e . button === 0
521- ) {
522- return onBeginMovePoint ( r )
523- }
524- if ( e . button === 0 && ! createWithPrimary )
525- return onSelectRegion ( r )
526- mouseEvents . onMouseDown ( e )
527- }
528- }
529- : { } ) }
530- style = { {
531- ...( r . highlighted
532- ? {
533- pointerEvents : r . type !== "point" ? "none" : undefined ,
534- cursor : "grab"
535- }
536- : {
537- cursor : ! (
538- zoomWithPrimary ||
539- dragWithPrimary ||
540- createWithPrimary
541- )
542- ? "pointer"
543- : undefined ,
544- pointerEvents :
545- zoomWithPrimary ||
546- dragWithPrimary ||
547- ( createWithPrimary && ! r . highlighted )
548- ? "none"
549- : undefined
550- } ) ,
551- position : "absolute" ,
552- left : pbox . x - 5 ,
553- top : pbox . y - 5 ,
554- width : pbox . w + 10 ,
555- height : pbox . h + 10
556- } }
557- >
558- < path
559- d = { `M5,5 L${ pbox . w + 5 } ,5 L${ pbox . w + 5 } ,${ pbox . h +
560- 5 } L5,${ pbox . h + 5 } Z`}
561- />
562- </ svg >
520+ < HighlightBox
521+ region = { r }
522+ mouseEvents = { mouseEvents }
523+ dragWithPrimary = { dragWithPrimary }
524+ createWithPrimary = { createWithPrimary }
525+ zoomWithPrimary = { zoomWithPrimary }
526+ onBeginMovePoint = { onBeginMovePoint }
527+ onSelectRegion = { onSelectRegion }
528+ pbox = { pbox }
529+ />
563530 { r . type === "box" &&
564531 ! dragWithPrimary &&
565532 ! zoomWithPrimary &&
@@ -749,6 +716,60 @@ export default ({
749716 } }
750717 />
751718 ) }
719+ { showPointDistances && (
720+ < svg
721+ className = { classes . pointDistanceIndicator }
722+ style = { {
723+ pointerEvents : "none" ,
724+ position : "absolute" ,
725+ left : 0 ,
726+ top : 0 ,
727+ width : "100%" ,
728+ height : "100%"
729+ } }
730+ >
731+ { regions . filter ( r1 => r1 . type === "point" ) . flatMap ( ( r1 , i1 ) =>
732+ regions
733+ . filter ( ( r2 , i2 ) => i2 > i1 )
734+ . filter ( r2 => r2 . type === "point" )
735+ . map ( r2 => {
736+ const pr1 = projectRegionBox ( r1 )
737+ const pr2 = projectRegionBox ( r2 )
738+ const prm = {
739+ x : ( pr1 . x + pr1 . w / 2 + pr2 . x + pr2 . w / 2 ) / 2 ,
740+ y : ( pr1 . y + pr1 . h / 2 + pr2 . y + pr2 . h / 2 ) / 2
741+ }
742+ let displayDistance
743+ if ( realSize ) {
744+ const { w, h, unitName } = realSize
745+ displayDistance =
746+ Math . sqrt (
747+ Math . pow ( r1 . x * w - r2 . x * w , 2 ) +
748+ Math . pow ( r1 . y * h - r2 . y * h , 2 )
749+ ) . toFixed ( pointDistancePrecision ) + unitName
750+ } else {
751+ displayDistance =
752+ (
753+ Math . sqrt (
754+ Math . pow ( r1 . x - r2 . x , 2 ) + Math . pow ( r1 . y - r2 . y , 2 )
755+ ) * 100
756+ ) . toFixed ( pointDistancePrecision ) + "%"
757+ }
758+ return (
759+ < Fragment >
760+ < path
761+ d = { `M${ pr1 . x + pr1 . w / 2 } ,${ pr1 . y + pr1 . h / 2 } L${ pr2 . x +
762+ pr2 . w / 2 } ,${ pr2 . y + pr2 . h / 2 } `}
763+ />
764+ < text x = { prm . x } y = { prm . y } >
765+ { displayDistance }
766+ </ text >
767+ </ Fragment >
768+ )
769+ } )
770+ ) }
771+ </ svg >
772+ ) }
752773 < canvas { ...mouseEvents } className = { classes . canvas } ref = { canvasEl } />
753774 < div className = { classes . zoomIndicator } >
754775 { ( ( 1 / mat . a ) * 100 ) . toFixed ( 0 ) } %
0 commit comments