1
1
// @flow weak
2
2
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"
4
10
import type { Node } from "react"
5
11
import { Matrix } from "transformation-matrix-js"
6
12
import Crosshairs from "../Crosshairs"
@@ -48,6 +54,7 @@ type Props = {
48
54
allowedArea ?: { x : number , y : number , w : number , h : number } ,
49
55
RegionEditLabel ?: Node ,
50
56
videoPlaying ?: boolean ,
57
+ zoomOnAllowedArea ?: boolean ,
51
58
fullImageSegmentationMode ?: boolean ,
52
59
autoSegmentationOptions ?: Object ,
53
60
@@ -70,7 +77,15 @@ type Props = {
70
77
onChangeVideoPlaying ?: Function ,
71
78
}
72
79
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
+ }
74
89
75
90
export const ImageCanvas = ( {
76
91
regions,
@@ -110,6 +125,7 @@ export const ImageCanvas = ({
110
125
onChangeVideoTime,
111
126
onChangeVideoPlaying,
112
127
onRegionClassAdded,
128
+ zoomOnAllowedArea = true ,
113
129
} : Props ) => {
114
130
const classes = useStyles ( )
115
131
@@ -155,6 +171,7 @@ export const ImageCanvas = ({
155
171
156
172
const [ imageDimensions , changeImageDimensions ] = useState ( )
157
173
const imageLoaded = Boolean ( imageDimensions && imageDimensions . naturalWidth )
174
+
158
175
const onVideoOrImageLoaded = useEventCallback (
159
176
( { naturalWidth, naturalHeight, duration } ) => {
160
177
const dims = { naturalWidth, naturalHeight, duration }
@@ -190,6 +207,17 @@ export const ImageCanvas = ({
190
207
}
191
208
}
192
209
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
+
193
221
useLayoutEffect ( ( ) => {
194
222
if ( ! imageDimensions ) return
195
223
const { clientWidth, clientHeight } = canvas
0 commit comments