11// @flow weak
22
3- import React , { useRef , useState , useLayoutEffect , useMemo } from "react"
3+ import React , {
4+ useRef ,
5+ useState ,
6+ useLayoutEffect ,
7+ useEffect ,
8+ useMemo ,
9+ } from "react"
410import type { Node } from "react"
511import { Matrix } from "transformation-matrix-js"
612import Crosshairs from "../Crosshairs"
@@ -48,6 +54,7 @@ type Props = {
4854 allowedArea ?: { x : number , y : number , w : number , h : number } ,
4955 RegionEditLabel ?: Node ,
5056 videoPlaying ?: boolean ,
57+ zoomOnAllowedArea ?: boolean ,
5158 fullImageSegmentationMode ?: boolean ,
5259 autoSegmentationOptions ?: Object ,
5360
@@ -70,7 +77,15 @@ type Props = {
7077 onChangeVideoPlaying ?: Function ,
7178}
7279
73- const getDefaultMat = ( ) => Matrix . from ( 1 , 0 , 0 , 1 , - 10 , - 10 )
80+ const getDefaultMat = ( allowedArea = null , { iw, ih } = { } ) => {
81+ let mat = Matrix . from ( 1 , 0 , 0 , 1 , - 10 , - 10 )
82+ if ( allowedArea && iw ) {
83+ mat = mat
84+ . translate ( allowedArea . x * iw , allowedArea . y * ih )
85+ . scaleU ( allowedArea . w + 0.05 )
86+ }
87+ return mat
88+ }
7489
7590export const ImageCanvas = ( {
7691 regions,
@@ -110,6 +125,7 @@ export const ImageCanvas = ({
110125 onChangeVideoTime,
111126 onChangeVideoPlaying,
112127 onRegionClassAdded,
128+ zoomOnAllowedArea = true ,
113129} : Props ) => {
114130 const classes = useStyles ( )
115131
@@ -155,6 +171,7 @@ export const ImageCanvas = ({
155171
156172 const [ imageDimensions , changeImageDimensions ] = useState ( )
157173 const imageLoaded = Boolean ( imageDimensions && imageDimensions . naturalWidth )
174+
158175 const onVideoOrImageLoaded = useEventCallback (
159176 ( { naturalWidth, naturalHeight, duration } ) => {
160177 const dims = { naturalWidth, naturalHeight, duration }
@@ -190,6 +207,17 @@ export const ImageCanvas = ({
190207 }
191208 }
192209
210+ useEffect ( ( ) => {
211+ if ( ! imageLoaded ) return
212+ changeMat (
213+ getDefaultMat (
214+ zoomOnAllowedArea ? allowedArea : null ,
215+ layoutParams . current
216+ )
217+ )
218+ // eslint-disable-next-line
219+ } , [ imageLoaded ] )
220+
193221 useLayoutEffect ( ( ) => {
194222 if ( ! imageDimensions ) return
195223 const { clientWidth, clientHeight } = canvas
0 commit comments