Skip to content

Commit 2ac7e7b

Browse files
added line tool
1 parent 0e1b96e commit 2ac7e7b

File tree

9 files changed

+25498
-1
lines changed

9 files changed

+25498
-1
lines changed

package-lock.json

Lines changed: 25407 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,24 @@ export default (state: MainLayoutState, action: Action) => {
377377
[x, y]
378378
)
379379
}
380+
case "DRAW_LINE": {
381+
const { regionId } = state.mode
382+
const [region, regionIndex] = getRegion(regionId)
383+
if (!region) return setIn(state, ["mode"], null)
384+
return setIn(
385+
state,
386+
[
387+
...pathToActiveImage,
388+
"regions",
389+
regionIndex,
390+
],
391+
{
392+
...region,
393+
x2:x,
394+
y2:y,
395+
}
396+
)
397+
}
380398
case "DRAW_EXPANDING_LINE": {
381399
const { regionId } = state.mode
382400
const [expandingLine, regionIndex] = getRegion(regionId)
@@ -449,6 +467,16 @@ export default (state: MainLayoutState, action: Action) => {
449467
{ ...polygon, points: polygon.points.concat([[x, y]]) }
450468
)
451469
}
470+
case "DRAW_LINE": {
471+
const [line, regionIndex] = getRegion(state.mode.regionId)
472+
if (!line) break
473+
setIn(
474+
state,
475+
[...pathToActiveImage, "regions", regionIndex],
476+
{ ...line, x2:x, y2:y }
477+
)
478+
return setIn(state, ["mode"], null)
479+
}
452480
case "DRAW_EXPANDING_LINE": {
453481
const [expandingLine, regionIndex] = getRegion(state.mode.regionId)
454482
if (!expandingLine) break
@@ -588,6 +616,27 @@ export default (state: MainLayoutState, action: Action) => {
588616
})
589617
break
590618
}
619+
case "create-line": {
620+
if (state.mode && state.mode.mode === "DRAW_LINE") break
621+
state = saveToHistory(state, "Create Line")
622+
newRegion = {
623+
type: "line",
624+
x1: x,
625+
y1: y,
626+
x2: x,
627+
y2: y,
628+
highlighted: true,
629+
editingLabels: false,
630+
color: defaultRegionColor,
631+
cls: defaultRegionCls,
632+
id: getRandomId(),
633+
}
634+
state = setIn(state, ["mode"], {
635+
mode: "DRAW_LINE",
636+
regionId: newRegion.id,
637+
})
638+
break
639+
}
591640
case "create-keypoints": {
592641
state = saveToHistory(state, "Create Keypoints")
593642
const [

src/DemoSite/Editor.js

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

src/ImageCanvas/region-tools.js

Lines changed: 13 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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,14 @@ export const iconDictionary = {
6263
"create-expanding-line": () => (
6364
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faGripLines} />
6465
),
66+
"create-line": () => (
67+
<FontAwesomeIcon
68+
style={faStyle}
69+
size="xs"
70+
fixedWidth
71+
icon={faChartLine}
72+
/>
73+
),
6574
"show-mask": () => (
6675
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faMask} />
6776
),

src/MainLayout/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,10 @@ export const MainLayout = ({
304304
name: "create-polygon",
305305
helperText: "Add Polygon" + getHotkeyHelpText("create_polygon"),
306306
},
307+
{
308+
name: "create-line",
309+
helperText: "Add Line",
310+
},
307311
{
308312
name: "create-expanding-line",
309313
helperText: "Add Expanding Line",

src/RegionShapes/index.js

Lines changed: 13 additions & 0 deletions
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)