Skip to content
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

🐛 Fix polygon line not pointed to the cursor during drawing #208

Merged
merged 1 commit into from
Aug 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ import { SharedUndoRedoService } from 'shared/services/shared-undo-redo.service'
import { SegmentationCanvasService } from './segmentation-canvas.service';
import { ImageLabellingActionService } from '../image-labelling-action.service';

interface ExtendedMouseEvent extends MouseEvent {
layerX: number;
layerY: number;
}

@Component({
selector: 'image-labelling-segmentation',
templateUrl: './image-labelling-segmentation.component.html',
Expand Down Expand Up @@ -288,7 +293,7 @@ export class ImageLabellingSegmentationComponent implements OnInit, OnChanges, O
}

@HostListener('dblclick', ['$event'])
canvasDblClickEvent(event: MouseEvent) {
canvasDblClickEvent(event: ExtendedMouseEvent) {
if (this.validateEndDrawPolygon(this.segState, this.isMouseWithinPoint, this.canvasContext)) {
if (this._segCanvasService.isNewPolygon()) {
this.completingPolygon();
Expand Down Expand Up @@ -384,7 +389,7 @@ export class ImageLabellingSegmentationComponent implements OnInit, OnChanges, O
this.canvas.nativeElement
);
this.redrawImage(this._selectMetadata);
this.mouseMoveDrawCanvas(this.mouseEvent as MouseEvent);
this.mouseMoveDrawCanvas(this.mouseEvent as ExtendedMouseEvent);
}

deletePolygon() {
Expand Down Expand Up @@ -481,7 +486,7 @@ export class ImageLabellingSegmentationComponent implements OnInit, OnChanges, O
}

@HostListener('mousedown', ['$event'])
mouseDown(event: MouseEvent) {
mouseDown(event: ExtendedMouseEvent) {
try {
this.isMouseWithinPoint = this._segCanvasService.mouseClickWithinPointPath(this._selectMetadata, event);

Expand Down Expand Up @@ -603,7 +608,7 @@ export class ImageLabellingSegmentationComponent implements OnInit, OnChanges, O
}

@HostListener('mousemove', ['$event'])
mouseMove(event: MouseEvent) {
mouseMove(event: ExtendedMouseEvent) {
try {
// this._selectMetadata as truefy value
// as user can click on image but img not yet loaded onto screen
Expand Down Expand Up @@ -670,7 +675,7 @@ export class ImageLabellingSegmentationComponent implements OnInit, OnChanges, O
}
}

mouseMoveDrawCanvas(event: MouseEvent) {
mouseMoveDrawCanvas(event: ExtendedMouseEvent) {
const mouseWithinShape = this._segCanvasService.mouseMoveDraw(
this._selectMetadata,
this.image,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import {
} from 'shared/types/image-labelling/image-labelling.model';
import { Utils } from 'util/utils';

interface ExtendedMouseEvent extends MouseEvent {
layerX: number;
layerY: number;
}

type ClickPoint = {
polygonIndex: number;
Expand All @@ -45,7 +49,7 @@ export class SegmentationCanvasService {
constructor() {}

mouseDownDraw(
event: MouseEvent,
event: ExtendedMouseEvent,
metadata: PolyMetadata,
canvas: HTMLCanvasElement,
img: HTMLImageElement,
Expand Down Expand Up @@ -92,7 +96,7 @@ export class SegmentationCanvasService {
img: HTMLImageElement,
context: CanvasRenderingContext2D,
canvas: HTMLCanvasElement,
event: MouseEvent,
event: ExtendedMouseEvent,
ctrlDown: boolean,
isMouseDown: boolean,
redrawCallback: (arg: Method) => void,
Expand Down Expand Up @@ -613,14 +617,17 @@ export class SegmentationCanvasService {
}
}

private drawfromPreviousPoint({ offsetX, offsetY }: MouseEvent, context: CanvasRenderingContext2D) {
private drawfromPreviousPoint({ offsetX, offsetY, layerX, layerY }: ExtendedMouseEvent, context: CanvasRenderingContext2D) {
try {
if (this.tmpPolygon?.coorPt) {
const { length } = this.tmpPolygon.coorPt;
const X = offsetX === 0 ? layerX : offsetX;
const Y = offsetY === 0 ? layerY : offsetY;
console.log(X, offsetX, layerX);
const newLength = length - 1;
context.beginPath();
context.moveTo(this.tmpPolygon.coorPt[newLength].x, this.tmpPolygon.coorPt[newLength].y);
context.lineTo(offsetX, offsetY);
context.lineTo(X, Y);
context.stroke();
}
} catch (err) {
Expand Down