Skip to content

Commit 700c271

Browse files
author
Xie, Ziyu
authored
Merge pull request #123 from rathodsanjay/bug/31-fix-issues-with-scale
Fix for the issues when parent element has a CSS scale transform applied to it
2 parents a3b327c + 4ecbc02 commit 700c271

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

projects/angular2-draggable/src/lib/angular-draggable.directive.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
2828
*/
2929
private _helperBlock: HelperBlock = null;
3030

31+
/**
32+
* Flag to indicate whether the element is dragged once after being initialised
33+
*/
34+
private isDragged: boolean = false;
35+
3136
@Output() started = new EventEmitter<any>();
3237
@Output() stopped = new EventEmitter<any>();
3338
@Output() edge = new EventEmitter<any>();
@@ -102,7 +107,6 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
102107
let element = this.handle ? this.handle : this.el.nativeElement;
103108
this.renderer.addClass(element, 'ng-draggable');
104109
}
105-
106110
this.resetPosition();
107111
}
108112

@@ -133,6 +137,13 @@ export class AngularDraggableDirective implements OnInit, OnDestroy, OnChanges,
133137
this.needTransform = true;
134138
}
135139
}
140+
141+
if (changes['scale'] && !changes['scale'].isFirstChange()) {
142+
let temp = this.currTrans.value;
143+
temp.x = temp.x * this.scale;
144+
temp.y = temp.y * this.scale;
145+
this.oldTrans.set(new Position(temp.x, temp.y));
146+
}
136147
}
137148

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

181-
let value = `translate(${translateX}px, ${translateY}px)`;
192+
// done to prevent the element from bouncing off when
193+
// the parent element is scaled and element is dragged for first time
194+
if (this.tempTrans.x !== 0 || this.tempTrans.y !== 0) {
195+
if (this.isDragged === false) {
196+
let temp = this.currTrans.value;
197+
temp.x = temp.x * this.scale;
198+
temp.y = temp.y * this.scale;
199+
this.oldTrans.set(new Position(temp.x, temp.y));
200+
}
201+
this.isDragged = true;
202+
}
182203

183-
if (this.scale !== 1) {
184-
value += ` scale(${this.scale})`;
204+
if (this.scale && this.scale !== 0 && this.isDragged) {
205+
translateX = translateX / this.scale;
206+
translateY = translateY / this.scale;
185207
}
186208

209+
let value = `translate(${translateX}px, ${translateY}px)`;
210+
187211
this.renderer.setStyle(this.el.nativeElement, 'transform', value);
188212
this.renderer.setStyle(this.el.nativeElement, '-webkit-transform', value);
189213
this.renderer.setStyle(this.el.nativeElement, '-ms-transform', value);

0 commit comments

Comments
 (0)