Skip to content

Commit

Permalink
fix: Allow polygon to move properly. #1597
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Sep 30, 2022
1 parent a779e08 commit 480bcb6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions ui/src/image_annotator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,49 @@ describe('ImageAnnotator.tsx', () => {
{ shape: { polygon: { items: [{ x: 100, y: 200 }, { x: 240, y: 100 }, { x: 240, y: 220 },] } }, tag: 'person' },
])
})

it('Moves polygon by a single point correctly', async () => {
const { container } = render(<XImageAnnotator model={model} />)
await waitForImgLoad()
const canvasEl = container.querySelectorAll('canvas')[1]

fireEvent.click(canvasEl, { clientX: 180, clientY: 120 })
fireEvent.mouseDown(canvasEl, { clientX: 105, clientY: 100 })
fireEvent.mouseMove(canvasEl, { clientX: 105, clientY: 100, buttons: 1 })
fireEvent.mouseMove(canvasEl, { clientX: 100, clientY: 200, buttons: 1 })
fireEvent.click(canvasEl, { clientX: 100, clientY: 200 })

expect(wave.args[name]).toMatchObject([
rect,
{ shape: { polygon: { items: [{ x: 100, y: 200 }, { x: 240, y: 100 }, { x: 240, y: 220 },] } }, tag: 'person' },
])
})

it.only('Moves polygon by a single point, then moves it whole', async () => {
const { container } = render(<XImageAnnotator model={model} />)
await waitForImgLoad()
const canvasEl = container.querySelectorAll('canvas')[1]

fireEvent.click(canvasEl, { clientX: 180, clientY: 120 })
fireEvent.mouseDown(canvasEl, { clientX: 105, clientY: 100 })
fireEvent.mouseMove(canvasEl, { clientX: 105, clientY: 100, buttons: 1 })
fireEvent.mouseMove(canvasEl, { clientX: 100, clientY: 200, buttons: 1 })
fireEvent.click(canvasEl, { clientX: 100, clientY: 200 })

expect(wave.args[name]).toMatchObject([
rect,
{ shape: { polygon: { items: [{ x: 100, y: 200 }, { x: 240, y: 100 }, { x: 240, y: 220 },] } }, tag: 'person' },
])

fireEvent.click(canvasEl, { clientX: 180, clientY: 150 })
fireEvent.mouseDown(canvasEl, { clientX: 180, clientY: 150 })
fireEvent.mouseMove(canvasEl, { clientX: 190, clientY: 160, buttons: 1 })
fireEvent.click(canvasEl, { clientX: 190, clientY: 160 })

expect(wave.args[name]).toMatchObject([
rect,
{ shape: { polygon: { items: [{ x: 110, y: 210 }, { x: 250, y: 110 }, { x: 250, y: 230 },] } }, tag: 'person' },
])
})
})
})
1 change: 1 addition & 0 deletions ui/src/image_annotator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ export const XImageAnnotator = ({ model }: { model: ImageAnnotator }) => {
}
case 'select': {
if (intersected) setActiveTag(intersected.tag)
polygonRef.current?.resetDragging()
setDrawnShapes(drawnShapes => drawnShapes.map(s => { s.isFocused = s === intersected; return s }))
redrawExistingShapes()
break
Expand Down
5 changes: 5 additions & 0 deletions ui/src/image_annotator_polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export class PolygonAnnotator {

constructor(private canvas: HTMLCanvasElement) { this.ctx = canvas.getContext('2d') }

resetDragging() {
this.draggedPoint = null
this.draggedShape = null
}

onClick(cursor_x: U, cursor_y: U, color: S, tag: S): DrawnShape | undefined {
if (!this.ctx) return

Expand Down

0 comments on commit 480bcb6

Please sign in to comment.