Angular CDK Drag & Drop - Snap to Grid
Asked Answered
P

1

4

I am trying to use Angular's CDK Drag & Drop feature and its working great, however I can't find documentation on how to snap to a grid.

I found a GitHub issue that states that this feature has been added.

https://github.com/angular/components/issues/15471

This is what I would like to achieve with the CDK's Drag & Drop.

https://xieziyu.github.io/angular2-draggable/#/advance/snap-grid

Pumping answered 10/2, 2021 at 6:33 Comment(1)
Hi, did you find something ?Tasia
S
4

You can use the cdkDragConstrainPosition input on your cdkDrag element

Example:

<div cdkDrag [cdkDragConstrainPosition]="computeDragRenderPos">
  ...
</div>
export class MyDraggableComponent {

  constructor() {}

  computeDragRenderPos(pos, dragRef) {
    return {x: Math.floor(pos.x / 30) * 30, y: pos.y}; // will render the element every 30 pixels horizontally
  }
}

https://material.angular.io/cdk/drag-drop/api#CdkDrag
https://material.angular.io/cdk/drag-drop/api#DragRef

Swordbill answered 25/9, 2021 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.