Skip to content

Commit 5e05674

Browse files
authored
feat: Merge pull request #135 from sreevardhanreddi/master
added line tool
2 parents 03e0734 + 140d8fc commit 5e05674

File tree

8 files changed

+78
-1
lines changed

8 files changed

+78
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"seamless-immutable": "^7.1.4",
3030
"shallow-equal": "^1.2.1",
3131
"storybook": "^5.3.14",
32+
"styled-components": "^5.2.1",
3233
"transformation-matrix-js": "^2.7.6",
3334
"use-event-callback": "^0.1.0",
3435
"use-key-hook": "^1.3.0"

src/Annotator/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export const Annotator = ({
6666
"create-point",
6767
"create-box",
6868
"create-polygon",
69+
"create-line",
6970
"create-expanding-line",
7071
"show-mask",
7172
],

src/Annotator/reducers/general-reducer.js

+41
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,16 @@ export default (state: MainLayoutState, action: Action) => {
380380
[x, y]
381381
)
382382
}
383+
case "DRAW_LINE": {
384+
const { regionId } = state.mode
385+
const [region, regionIndex] = getRegion(regionId)
386+
if (!region) return setIn(state, ["mode"], null)
387+
return setIn(state, [...pathToActiveImage, "regions", regionIndex], {
388+
...region,
389+
x2: x,
390+
y2: y,
391+
})
392+
}
383393
case "DRAW_EXPANDING_LINE": {
384394
const { regionId } = state.mode
385395
const [expandingLine, regionIndex] = getRegion(regionId)
@@ -452,6 +462,16 @@ export default (state: MainLayoutState, action: Action) => {
452462
{ ...polygon, points: polygon.points.concat([[x, y]]) }
453463
)
454464
}
465+
case "DRAW_LINE": {
466+
const [line, regionIndex] = getRegion(state.mode.regionId)
467+
if (!line) break
468+
setIn(state, [...pathToActiveImage, "regions", regionIndex], {
469+
...line,
470+
x2: x,
471+
y2: y,
472+
})
473+
return setIn(state, ["mode"], null)
474+
}
455475
case "DRAW_EXPANDING_LINE": {
456476
const [expandingLine, regionIndex] = getRegion(state.mode.regionId)
457477
if (!expandingLine) break
@@ -591,6 +611,27 @@ export default (state: MainLayoutState, action: Action) => {
591611
})
592612
break
593613
}
614+
case "create-line": {
615+
if (state.mode && state.mode.mode === "DRAW_LINE") break
616+
state = saveToHistory(state, "Create Line")
617+
newRegion = {
618+
type: "line",
619+
x1: x,
620+
y1: y,
621+
x2: x,
622+
y2: y,
623+
highlighted: true,
624+
editingLabels: false,
625+
color: defaultRegionColor,
626+
cls: defaultRegionCls,
627+
id: getRandomId(),
628+
}
629+
state = setIn(state, ["mode"], {
630+
mode: "DRAW_LINE",
631+
regionId: newRegion.id,
632+
})
633+
break
634+
}
594635
case "create-keypoints": {
595636
state = saveToHistory(state, "Create Keypoints")
596637
const [

src/DemoSite/Editor.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const Editor = ({ onOpenAnnotator, lastOutput }: any) => {
180180
imageTagList?: Array<string>,
181181
imageClsList?: Array<string>,
182182
// all tools are enabled by default
183-
enabledTools?: Array< "select" | "create-point" | "create-box" | "create-polygon">,
183+
enabledTools?: Array< "select" | "create-point" | "create-box" | "create-polygon" | "create-line">,
184184
selectedImage?: string, // initial selected image
185185
images: Array<{
186186
src: string,

src/ImageCanvas/region-tools.js

+13
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ export type Polygon = {|
4848
open?: boolean,
4949
points: Array<[number, number]>,
5050
|}
51+
52+
export type Line = {|
53+
...$Exact<BaseRegion>,
54+
type: "line",
55+
x1: number,
56+
y1: number,
57+
x2: number,
58+
y2: number,
59+
|}
60+
5161
export type ExpandingLine = {|
5262
...$Exact<BaseRegion>,
5363
type: "expanding-line",
@@ -132,6 +142,9 @@ export const getEnclosingBox = (region: Region) => {
132142
box.h = Math.max(...region.points.map(({ x, y }) => y)) - box.y
133143
return box
134144
}
145+
case "line": {
146+
return { x: region.x1, y: region.y1, w: 0, h: 0 }
147+
}
135148
case "box": {
136149
return { x: region.x, y: region.y, w: region.w, h: region.h }
137150
}

src/MainLayout/icon-dictionary.js

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
faSearch,
1717
faMask,
1818
faEdit,
19+
faChartLine,
1920
} from "@fortawesome/free-solid-svg-icons"
2021
import FullscreenIcon from "@material-ui/icons/Fullscreen"
2122
import AccessibilityNewIcon from "@material-ui/icons/AccessibilityNew"
@@ -62,6 +63,9 @@ export const iconDictionary = {
6263
"create-expanding-line": () => (
6364
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faGripLines} />
6465
),
66+
"create-line": () => (
67+
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faChartLine} />
68+
),
6569
"show-mask": () => (
6670
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faMask} />
6771
),

src/MainLayout/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ export const MainLayout = ({
305305
name: "create-polygon",
306306
helperText: "Add Polygon" + getHotkeyHelpText("create_polygon"),
307307
},
308+
{
309+
name: "create-line",
310+
helperText: "Add Line",
311+
},
308312
{
309313
name: "create-expanding-line",
310314
helperText: "Add Expanding Line",

src/RegionShapes/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ const RegionComponents = {
1818
/>
1919
</g>
2020
)),
21+
line: memo(({ region, iw, ih }) => (
22+
<g transform={`translate(${region.x1 * iw} ${region.y1 * ih})`}>
23+
<line
24+
strokeWidth={2}
25+
x1={0}
26+
y1={0}
27+
x2={(region.x2 - region.x1) * iw}
28+
y2={(region.y2 - region.y1) * ih}
29+
stroke={colorAlpha(region.color, 0.75)}
30+
fill={colorAlpha(region.color, 0.25)}
31+
/>
32+
</g>
33+
)),
2134
box: memo(({ region, iw, ih }) => (
2235
<g transform={`translate(${region.x * iw} ${region.y * ih})`}>
2336
<rect

0 commit comments

Comments
 (0)