I tried to bind the x and y position to the svg circle. It works fine with (cdkDragEnded) but it does not work with (cdkDragMoved). On onDragMoved i get really high values for x and y position when i only drag the circle a little bit.
HTML:
<svg height="1000" width="1000">
<circle r="50" stroke="black" stroke-width="3"
cdkDrag
[cdkDragFreeDragPosition]="{x: x + 50, y: y + 50}"
(cdkDragMoved)="onDragMoved($event)"
(cdkDragEnded)="onDragEnded($event)"/>
TypeScript:
interface Position {
x?: number;
y?: number;
}
@Component({
selector: 'app-drag3',
templateUrl: './drag3.component.html',
styleUrls: ['./drag3.component.css']
})
export class Drag3Component {
p: Position = {};
x: number = 100;
y: number = 100;
onDragMoved(event: any){
this.p = event.source.getRootElement().getBoundingClientRect();
this.x = this.p.x!;
this.y = this.p.y!;
console.log({ x: this.x , y: this.y});
}
onDragEnded(event: any){
this.p = event.source.getRootElement().getBoundingClientRect();
this.x = this.p.x!;
this.y = this.p.y!;
console.log({ x: this.x , y: this.y});
}
}
Does anyone have an idea how to fix this?