Skip to content

added line tool #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"seamless-immutable": "^7.1.4",
"shallow-equal": "^1.2.1",
"storybook": "^5.3.14",
"styled-components": "^5.2.1",
"transformation-matrix-js": "^2.7.6",
"use-event-callback": "^0.1.0",
"use-key-hook": "^1.3.0"
Expand Down
1 change: 1 addition & 0 deletions src/Annotator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const Annotator = ({
"create-point",
"create-box",
"create-polygon",
"create-line",
"create-expanding-line",
"show-mask",
],
Expand Down
41 changes: 41 additions & 0 deletions src/Annotator/reducers/general-reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ export default (state: MainLayoutState, action: Action) => {
[x, y]
)
}
case "DRAW_LINE": {
const { regionId } = state.mode
const [region, regionIndex] = getRegion(regionId)
if (!region) return setIn(state, ["mode"], null)
return setIn(state, [...pathToActiveImage, "regions", regionIndex], {
...region,
x2: x,
y2: y,
})
}
case "DRAW_EXPANDING_LINE": {
const { regionId } = state.mode
const [expandingLine, regionIndex] = getRegion(regionId)
Expand Down Expand Up @@ -449,6 +459,16 @@ export default (state: MainLayoutState, action: Action) => {
{ ...polygon, points: polygon.points.concat([[x, y]]) }
)
}
case "DRAW_LINE": {
const [line, regionIndex] = getRegion(state.mode.regionId)
if (!line) break
setIn(state, [...pathToActiveImage, "regions", regionIndex], {
...line,
x2: x,
y2: y,
})
return setIn(state, ["mode"], null)
}
case "DRAW_EXPANDING_LINE": {
const [expandingLine, regionIndex] = getRegion(state.mode.regionId)
if (!expandingLine) break
Expand Down Expand Up @@ -588,6 +608,27 @@ export default (state: MainLayoutState, action: Action) => {
})
break
}
case "create-line": {
if (state.mode && state.mode.mode === "DRAW_LINE") break
state = saveToHistory(state, "Create Line")
newRegion = {
type: "line",
x1: x,
y1: y,
x2: x,
y2: y,
highlighted: true,
editingLabels: false,
color: defaultRegionColor,
cls: defaultRegionCls,
id: getRandomId(),
}
state = setIn(state, ["mode"], {
mode: "DRAW_LINE",
regionId: newRegion.id,
})
break
}
case "create-keypoints": {
state = saveToHistory(state, "Create Keypoints")
const [
Expand Down
2 changes: 1 addition & 1 deletion src/DemoSite/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const Editor = ({ onOpenAnnotator, lastOutput }: any) => {
imageTagList?: Array<string>,
imageClsList?: Array<string>,
// all tools are enabled by default
enabledTools?: Array< "select" | "create-point" | "create-box" | "create-polygon">,
enabledTools?: Array< "select" | "create-point" | "create-box" | "create-polygon" | "create-line">,
selectedImage?: string, // initial selected image
images: Array<{
src: string,
Expand Down
13 changes: 13 additions & 0 deletions src/ImageCanvas/region-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ export type Polygon = {|
open?: boolean,
points: Array<[number, number]>,
|}

export type Line = {|
...$Exact<BaseRegion>,
type: "line",
x1: number,
y1: number,
x2: number,
y2: number,
|}

export type ExpandingLine = {|
...$Exact<BaseRegion>,
type: "expanding-line",
Expand Down Expand Up @@ -132,6 +142,9 @@ export const getEnclosingBox = (region: Region) => {
box.h = Math.max(...region.points.map(({ x, y }) => y)) - box.y
return box
}
case "line": {
return { x: region.x1, y: region.y1, w: 0, h: 0 }
}
case "box": {
return { x: region.x, y: region.y, w: region.w, h: region.h }
}
Expand Down
4 changes: 4 additions & 0 deletions src/MainLayout/icon-dictionary.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
faSearch,
faMask,
faEdit,
faChartLine,
} from "@fortawesome/free-solid-svg-icons"
import FullscreenIcon from "@material-ui/icons/Fullscreen"
import AccessibilityNewIcon from "@material-ui/icons/AccessibilityNew"
Expand Down Expand Up @@ -62,6 +63,9 @@ export const iconDictionary = {
"create-expanding-line": () => (
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faGripLines} />
),
"create-line": () => (
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faChartLine} />
),
"show-mask": () => (
<FontAwesomeIcon style={faStyle} size="xs" fixedWidth icon={faMask} />
),
Expand Down
4 changes: 4 additions & 0 deletions src/MainLayout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ export const MainLayout = ({
name: "create-polygon",
helperText: "Add Polygon" + getHotkeyHelpText("create_polygon"),
},
{
name: "create-line",
helperText: "Add Line",
},
{
name: "create-expanding-line",
helperText: "Add Expanding Line",
Expand Down
13 changes: 13 additions & 0 deletions src/RegionShapes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ const RegionComponents = {
/>
</g>
)),
line: memo(({ region, iw, ih }) => (
<g transform={`translate(${region.x1 * iw} ${region.y1 * ih})`}>
<line
strokeWidth={2}
x1={0}
y1={0}
x2={(region.x2 - region.x1) * iw}
y2={(region.y2 - region.y1) * ih}
stroke={colorAlpha(region.color, 0.75)}
fill={colorAlpha(region.color, 0.25)}
/>
</g>
)),
box: memo(({ region, iw, ih }) => (
<g transform={`translate(${region.x * iw} ${region.y * ih})`}>
<rect
Expand Down