Skip to content

Commit c76ac02

Browse files
committed
optimizations and minor fixes for selections and highlight boxes
1 parent d3c8650 commit c76ac02

File tree

6 files changed

+138
-124
lines changed

6 files changed

+138
-124
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react-use": "^13.27.0",
2424
"react-use-measure": "^2.0.0",
2525
"seamless-immutable": "^7.1.4",
26+
"shallow-equal": "^1.2.1",
2627
"storybook": "^5.3.14",
2728
"transformation-matrix-js": "^2.7.6",
2829
"use-event-callback": "^0.1.0",

src/Annotator/reducers/general-reducer.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,6 @@ export default (state: MainLayoutState, action: Action) => {
370370
break
371371
}
372372

373-
if (newRegion) {
374-
state = unselectRegions(state)
375-
}
376-
377373
if (state.mode) {
378374
switch (state.mode.mode) {
379375
case "DRAW_POLYGON": {
@@ -394,6 +390,7 @@ export default (state: MainLayoutState, action: Action) => {
394390
.map((r) => ({
395391
...r,
396392
editingLabels: false,
393+
highlighted: false,
397394
}))
398395
.concat(newRegion ? [newRegion] : [])
399396

src/ImageCanvas/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ export const ImageCanvas = ({
414414
{showCrosshairs && (
415415
<Crosshairs key="crossHairs" mousePosition={mousePosition} />
416416
)}
417-
{imageLoaded && (
417+
{imageLoaded && !dragging && (
418418
<RegionSelectAndTransformBoxes
419419
key="regionSelectAndTransformBoxes"
420420
regions={regions}
@@ -433,7 +433,7 @@ export const ImageCanvas = ({
433433
showHighlightBox={showHighlightBox}
434434
/>
435435
)}
436-
{imageLoaded && showTags && (
436+
{imageLoaded && showTags && !dragging && (
437437
<PreventScrollToParents key="regionTags">
438438
<RegionTags
439439
regions={regions}

src/RegionSelectAndTransformBoxes/index.js

Lines changed: 129 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// @flow weak
22

3-
import React, { Fragment } from "react"
3+
import React, { Fragment, memo } from "react"
44
import HighlightBox from "../HighlightBox"
55
import { styled } from "@material-ui/core/styles"
66
import PreventScrollToParents from "../PreventScrollToParents"
7+
import { shallowEqualObjects } from "shallow-equal"
78

89
const TransformGrabber = styled("div")({
910
width: 8,
@@ -19,148 +20,160 @@ const boxCursorMap = [
1920
["sw-resize", "s-resize", "se-resize"],
2021
]
2122

22-
export const RegionSelectAndTransformBoxes = ({
23-
regions,
24-
mouseEvents,
25-
projectRegionBox,
26-
dragWithPrimary,
27-
createWithPrimary,
28-
zoomWithPrimary,
29-
onBeginMovePoint,
30-
onSelectRegion,
31-
layoutParams,
32-
mat,
33-
onBeginBoxTransform,
34-
onBeginMovePolygonPoint,
35-
onAddPolygonPoint,
36-
showHighlightBox,
37-
}) => {
38-
return regions
39-
.filter((r) => r.visible || r.visible === undefined)
40-
.filter((r) => !r.locked)
41-
.map((r, i) => {
42-
const pbox = projectRegionBox(r)
43-
const { iw, ih } = layoutParams.current
44-
return (
45-
<Fragment key={r.id}>
46-
<PreventScrollToParents>
47-
{showHighlightBox && (
48-
<HighlightBox
49-
region={r}
50-
mouseEvents={mouseEvents}
51-
dragWithPrimary={dragWithPrimary}
52-
createWithPrimary={createWithPrimary}
53-
zoomWithPrimary={zoomWithPrimary}
54-
onBeginMovePoint={onBeginMovePoint}
55-
onSelectRegion={onSelectRegion}
56-
pbox={pbox}
23+
const arePropsEqual = (prev, next) => {
24+
return (
25+
shallowEqualObjects(prev.region, next.region) &&
26+
prev.dragWithPrimary === next.dragWithPrimary &&
27+
prev.createWithPrimary === next.createWithPrimary &&
28+
prev.zoomWithPrimary === next.zoomWithPrimary &&
29+
prev.layoutParams.current.iw === next.layoutParams.current.iw &&
30+
prev.layoutParams.current.ih === next.layoutParams.current.ih &&
31+
prev.mat === next.mat
32+
)
33+
}
34+
35+
export const RegionSelectAndTransformBox = memo(
36+
({
37+
region: r,
38+
mouseEvents,
39+
projectRegionBox,
40+
dragWithPrimary,
41+
createWithPrimary,
42+
zoomWithPrimary,
43+
onBeginMovePoint,
44+
onSelectRegion,
45+
layoutParams,
46+
mat,
47+
onBeginBoxTransform,
48+
onBeginMovePolygonPoint,
49+
onAddPolygonPoint,
50+
showHighlightBox,
51+
}) => {
52+
const pbox = projectRegionBox(r)
53+
const { iw, ih } = layoutParams.current
54+
return (
55+
<Fragment>
56+
<PreventScrollToParents>
57+
{showHighlightBox && (
58+
<HighlightBox
59+
region={r}
60+
mouseEvents={mouseEvents}
61+
dragWithPrimary={dragWithPrimary}
62+
createWithPrimary={createWithPrimary}
63+
zoomWithPrimary={zoomWithPrimary}
64+
onBeginMovePoint={onBeginMovePoint}
65+
onSelectRegion={onSelectRegion}
66+
pbox={pbox}
67+
/>
68+
)}
69+
{r.type === "box" &&
70+
!dragWithPrimary &&
71+
!zoomWithPrimary &&
72+
!r.locked &&
73+
r.highlighted &&
74+
mat.a < 1.2 &&
75+
[
76+
[0, 0],
77+
[0.5, 0],
78+
[1, 0],
79+
[1, 0.5],
80+
[1, 1],
81+
[0.5, 1],
82+
[0, 1],
83+
[0, 0.5],
84+
[0.5, 0.5],
85+
].map(([px, py], i) => (
86+
<TransformGrabber
87+
key={i}
88+
{...mouseEvents}
89+
onMouseDown={(e) => {
90+
if (e.button === 0)
91+
return onBeginBoxTransform(r, [px * 2 - 1, py * 2 - 1])
92+
mouseEvents.onMouseDown(e)
93+
}}
94+
style={{
95+
left: pbox.x - 4 - 2 + pbox.w * px,
96+
top: pbox.y - 4 - 2 + pbox.h * py,
97+
cursor: boxCursorMap[py * 2][px * 2],
98+
borderRadius: px === 0.5 && py === 0.5 ? 4 : undefined,
99+
}}
57100
/>
58-
)}
59-
{r.type === "box" &&
60-
!dragWithPrimary &&
61-
!zoomWithPrimary &&
62-
!r.locked &&
63-
r.highlighted &&
64-
mat.a < 1.2 &&
65-
[
66-
[0, 0],
67-
[0.5, 0],
68-
[1, 0],
69-
[1, 0.5],
70-
[1, 1],
71-
[0.5, 1],
72-
[0, 1],
73-
[0, 0.5],
74-
[0.5, 0.5],
75-
].map(([px, py], i) => (
101+
))}
102+
{r.type === "polygon" &&
103+
!dragWithPrimary &&
104+
!zoomWithPrimary &&
105+
!r.locked &&
106+
r.highlighted &&
107+
r.points.map(([px, py], i) => {
108+
const proj = mat
109+
.clone()
110+
.inverse()
111+
.applyToPoint(px * iw, py * ih)
112+
return (
76113
<TransformGrabber
77114
key={i}
78115
{...mouseEvents}
79116
onMouseDown={(e) => {
80-
if (e.button === 0)
81-
return onBeginBoxTransform(r, [px * 2 - 1, py * 2 - 1])
117+
if (e.button === 0 && (!r.open || i === 0))
118+
return onBeginMovePolygonPoint(r, i)
82119
mouseEvents.onMouseDown(e)
83120
}}
84121
style={{
85-
left: pbox.x - 4 - 2 + pbox.w * px,
86-
top: pbox.y - 4 - 2 + pbox.h * py,
87-
cursor: boxCursorMap[py * 2][px * 2],
88-
borderRadius: px === 0.5 && py === 0.5 ? 4 : undefined,
122+
cursor: !r.open ? "move" : i === 0 ? "pointer" : undefined,
123+
pointerEvents:
124+
r.open && i === r.points.length - 1 ? "none" : undefined,
125+
left: proj.x - 4,
126+
top: proj.y - 4,
89127
}}
90128
/>
91-
))}
92-
{r.type === "polygon" &&
93-
!dragWithPrimary &&
94-
!zoomWithPrimary &&
95-
!r.locked &&
96-
r.highlighted &&
97-
r.points.map(([px, py], i) => {
129+
)
130+
})}
131+
{r.type === "polygon" &&
132+
r.highlighted &&
133+
!dragWithPrimary &&
134+
!zoomWithPrimary &&
135+
!r.locked &&
136+
!r.open &&
137+
r.points.length > 1 &&
138+
r.points
139+
.map((p1, i) => [p1, r.points[(i + 1) % r.points.length]])
140+
.map(([p1, p2]) => [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2])
141+
.map((pa, i) => {
98142
const proj = mat
99143
.clone()
100144
.inverse()
101-
.applyToPoint(px * iw, py * ih)
145+
.applyToPoint(pa[0] * iw, pa[1] * ih)
102146
return (
103147
<TransformGrabber
104148
key={i}
105149
{...mouseEvents}
106150
onMouseDown={(e) => {
107-
if (e.button === 0 && (!r.open || i === 0))
108-
return onBeginMovePolygonPoint(r, i)
151+
if (e.button === 0) return onAddPolygonPoint(r, pa, i + 1)
109152
mouseEvents.onMouseDown(e)
110153
}}
111154
style={{
112-
cursor: !r.open
113-
? "move"
114-
: i === 0
115-
? "pointer"
116-
: undefined,
117-
pointerEvents:
118-
r.open && i === r.points.length - 1
119-
? "none"
120-
: undefined,
155+
cursor: "copy",
121156
left: proj.x - 4,
122157
top: proj.y - 4,
158+
border: "2px dotted #fff",
159+
opacity: 0.5,
123160
}}
124161
/>
125162
)
126163
})}
127-
{r.type === "polygon" &&
128-
r.highlighted &&
129-
!dragWithPrimary &&
130-
!zoomWithPrimary &&
131-
!r.locked &&
132-
!r.open &&
133-
r.points.length > 1 &&
134-
r.points
135-
.map((p1, i) => [p1, r.points[(i + 1) % r.points.length]])
136-
.map(([p1, p2]) => [(p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2])
137-
.map((pa, i) => {
138-
const proj = mat
139-
.clone()
140-
.inverse()
141-
.applyToPoint(pa[0] * iw, pa[1] * ih)
142-
return (
143-
<TransformGrabber
144-
key={i}
145-
{...mouseEvents}
146-
onMouseDown={(e) => {
147-
if (e.button === 0)
148-
return onAddPolygonPoint(r, pa, i + 1)
149-
mouseEvents.onMouseDown(e)
150-
}}
151-
style={{
152-
cursor: "copy",
153-
left: proj.x - 4,
154-
top: proj.y - 4,
155-
border: "2px dotted #fff",
156-
opacity: 0.5,
157-
}}
158-
/>
159-
)
160-
})}
161-
</PreventScrollToParents>
162-
</Fragment>
163-
)
164+
</PreventScrollToParents>
165+
</Fragment>
166+
)
167+
},
168+
arePropsEqual
169+
)
170+
171+
export const RegionSelectAndTransformBoxes = (props) => {
172+
return props.regions
173+
.filter((r) => r.visible || r.visible === undefined)
174+
.filter((r) => !r.locked)
175+
.map((r, i) => {
176+
return <RegionSelectAndTransformBox key={r.id} {...props} region={r} />
164177
})
165178
}
166179

src/colors.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,4 @@ export const colorInts: Array<number> = colors.map(
2121
(c) => (parseInt(c.substr(1), 16) | 0xff000000) >>> 0
2222
)
2323

24-
console.log(colorInts)
25-
2624
export default colors

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14545,6 +14545,11 @@ shallow-equal@^1.1.0:
1454514545
resolved "https://registry.yarnpkg.com/shallow-equal/-/shallow-equal-1.2.0.tgz#fd828d2029ff4e19569db7e19e535e94e2d1f5cc"
1454614546
integrity sha512-Z21pVxR4cXsfwpMKMhCEIO1PCi5sp7KEp+CmOpBQ+E8GpHwKOw2sEzk7sgblM3d/j4z4gakoWEoPcjK0VJQogA==
1454714547

14548+
shallow-equal@^1.2.1:
14549+
version "1.2.1"
14550+
resolved "https://registry.yarnpkg.com/shallow-equal/-/shallow-equal-1.2.1.tgz#4c16abfa56043aa20d050324efa68940b0da79da"
14551+
integrity sha512-S4vJDjHHMBaiZuT9NPb616CSmLf618jawtv3sufLl6ivK8WocjAo58cXwbRV1cgqxH0Qbv+iUt6m05eqEa2IRA==
14552+
1454814553
shallowequal@1.1.0, shallowequal@^1.1.0:
1454914554
version "1.1.0"
1455014555
resolved "https://registry.yarnpkg.com/shallowequal/-/shallowequal-1.1.0.tgz#188d521de95b9087404fd4dcb68b13df0ae4e7f8"

0 commit comments

Comments
 (0)