Skip to content

Commit 7ef5bd0

Browse files
authored
fix: Merge pull request #83 from UniversalDataTool/remove-image-selector
better interactive allowedArea limits
2 parents 87a4584 + 6e6a7fe commit 7ef5bd0

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@material-ui/icons": "^4.9.1",
1010
"@semantic-release/git": "^9.0.0",
1111
"autoseg": "^0.0.8",
12+
"clamp": "^1.0.1",
1213
"color-alpha": "^1.0.4",
1314
"get-image-data": "^3.0.1",
1415
"material-survey": "^1.0.34",

src/Annotator/reducers/general-reducer.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { saveToHistory } from "./history-handler.js"
99
import colors from "../../colors"
1010
import fixTwisted from "./fix-twisted"
1111
import convertExpandingLineToPolygon from "./convert-expanding-line-to-polygon"
12+
import clamp from "clamp"
1213

1314
const getRandomId = () => Math.random().toString().split(".")[1]
1415

@@ -207,7 +208,13 @@ export default (state: MainLayoutState, action: Action) => {
207208
})
208209
}
209210
case "MOUSE_MOVE": {
210-
const { x, y } = action
211+
let { x, y } = action
212+
if (state.allowedArea) {
213+
const aa = state.allowedArea
214+
x = clamp(x, aa.x, aa.x + aa.w)
215+
y = clamp(y, aa.x, aa.x + aa.w)
216+
}
217+
211218
if (!state.mode) return state
212219
if (!activeImage) return state
213220
const { mouseDownAt } = state
@@ -352,18 +359,16 @@ export default (state: MainLayoutState, action: Action) => {
352359
}
353360
case "MOUSE_DOWN": {
354361
if (!activeImage) return state
355-
const { x, y } = action
356-
state = setIn(state, ["mouseDownAt"], { x, y })
362+
let { x, y } = action
357363

358364
if (state.allowedArea) {
359-
// TODO clamp x/y instead of giving up
360-
// TODO or image bounds
361365
const aa = state.allowedArea
362-
if (x < aa.x || x > aa.x + aa.w || y < aa.y || y > aa.y + aa.h) {
363-
return state
364-
}
366+
x = clamp(x, aa.x, aa.x + aa.w)
367+
y = clamp(y, aa.x, aa.x + aa.w)
365368
}
366369

370+
state = setIn(state, ["mouseDownAt"], { x, y })
371+
367372
if (state.mode) {
368373
switch (state.mode.mode) {
369374
case "DRAW_POLYGON": {
@@ -529,7 +534,13 @@ export default (state: MainLayoutState, action: Action) => {
529534
return setIn(state, [...pathToActiveImage, "regions"], regions)
530535
}
531536
case "MOUSE_UP": {
532-
const { x, y } = action
537+
let { x, y } = action
538+
if (state.allowedArea) {
539+
const aa = state.allowedArea
540+
x = clamp(x, aa.x, aa.x + aa.w)
541+
y = clamp(y, aa.x, aa.x + aa.w)
542+
}
543+
533544
const { mouseDownAt = { x, y } } = state
534545
if (!state.mode) return state
535546
state = setIn(state, ["mouseDownAt"], null)

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5719,6 +5719,11 @@ cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3:
57195719
inherits "^2.0.1"
57205720
safe-buffer "^5.0.1"
57215721

5722+
clamp@^1.0.1:
5723+
version "1.0.1"
5724+
resolved "https://registry.yarnpkg.com/clamp/-/clamp-1.0.1.tgz#66a0e64011816e37196828fdc8c8c147312c8634"
5725+
integrity sha1-ZqDmQBGBbjcZaCj9yMjBRzEshjQ=
5726+
57225727
class-utils@^0.3.5:
57235728
version "0.3.6"
57245729
resolved "https://registry.yarnpkg.com/class-utils/-/class-utils-0.3.6.tgz#f93369ae8b9a7ce02fd41faad0ca83033190c463"

0 commit comments

Comments
 (0)