Skip to content

Commit

Permalink
Merge pull request #123 from rathodsanjay/bug/31-fix-issues-with-scale
Browse files Browse the repository at this point in the history
Fix for the issues when parent element has a CSS scale transform applied to it
  • Loading branch information
Xie, Ziyu committed Nov 29, 2018
2 parents a3b327c + 4ecbc02 commit 700c271
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions projects/angular2-draggable/src/lib/angular-draggable.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
*/
private _helperBlock: HelperBlock = null;

/**
* Flag to indicate whether the element is dragged once after being initialised
*/
private isDragged: boolean = false;

@Output() started = new EventEmitter<any>();
@Output() stopped = new EventEmitter<any>();
@Output() edge = new EventEmitter<any>();
Expand Down Expand Up @@ -102,7 +107,6 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
let element = this.handle ? this.handle : this.el.nativeElement;
this.renderer.addClass(element, 'ng-draggable');
}

this.resetPosition();
}

Expand Down Expand Up @@ -133,6 +137,13 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
this.needTransform = true;
}
}

if (changes['scale'] && !changes['scale'].isFirstChange()) {
let temp = this.currTrans.value;
temp.x = temp.x * this.scale;
temp.y = temp.y * this.scale;
this.oldTrans.set(new Position(temp.x, temp.y));
}
}

ngAfterViewInit() {
Expand Down Expand Up @@ -178,12 +189,25 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
translateY = Math.round(translateY / this.gridSize) * this.gridSize;
}

let value = `translate(${translateX}px, ${translateY}px)`;
// done to prevent the element from bouncing off when
// the parent element is scaled and element is dragged for first time
if (this.tempTrans.x !== 0 || this.tempTrans.y !== 0) {
if (this.isDragged === false) {
let temp = this.currTrans.value;
temp.x = temp.x * this.scale;
temp.y = temp.y * this.scale;
this.oldTrans.set(new Position(temp.x, temp.y));
}
this.isDragged = true;
}

if (this.scale !== 1) {
value += ` scale(${this.scale})`;
if (this.scale && this.scale !== 0 && this.isDragged) {
translateX = translateX / this.scale;
translateY = translateY / this.scale;
}

let value = `translate(${translateX}px, ${translateY}px)`;

this.renderer.setStyle(this.el.nativeElement, 'transform', value);
this.renderer.setStyle(this.el.nativeElement, '-webkit-transform', value);
this.renderer.setStyle(this.el.nativeElement, '-ms-transform', value);
Expand Down

0 comments on commit 700c271

Please sign in to comment.