Skip to content

Commit 63174d2

Browse files
crisbetojelbourn
authored andcommittedDec 3, 2018
fix(drag-drop): dragged elements blurry in some browsers (#14299)
Rounds the `transform` value before they're assigned, in order to avoid blurriness in some browsers. Fixes #14283.
1 parent 700f20f commit 63174d2

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed
 

‎src/cdk/drag-drop/drag.spec.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ describe('CdkDrag', () => {
504504
expect(dragElement.style.touchAction)
505505
.not.toEqual('none', 'should not disable touchAction on when there is a drag handle');
506506
});
507+
507508
it('should be able to reset a freely-dragged item to its initial position', fakeAsync(() => {
508509
const fixture = createComponent(StandaloneDraggable);
509510
fixture.detectChanges();
@@ -556,7 +557,17 @@ describe('CdkDrag', () => {
556557
expect(fixture.componentInstance.endedSpy).toHaveBeenCalledTimes(1);
557558
}));
558559

559-
});
560+
it('should round the transform value', fakeAsync(() => {
561+
const fixture = createComponent(StandaloneDraggable);
562+
fixture.detectChanges();
563+
const dragElement = fixture.componentInstance.dragElement.nativeElement;
564+
565+
expect(dragElement.style.transform).toBeFalsy();
566+
dragElementViaMouse(fixture, dragElement, 13.37, 37);
567+
expect(dragElement.style.transform).toBe('translate3d(13px, 37px, 0px)');
568+
}));
569+
570+
});
560571

561572
describe('draggable with a handle', () => {
562573
it('should not be able to drag the entire element if it has a handle', fakeAsync(() => {

‎src/cdk/drag-drop/drag.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -866,7 +866,9 @@ interface Point {
866866
* @param y Desired position of the element along the Y axis.
867867
*/
868868
function getTransform(x: number, y: number): string {
869-
return `translate3d(${x}px, ${y}px, 0)`;
869+
// Round the transforms since some browsers will
870+
// blur the elements, for sub-pixel transforms.
871+
return `translate3d(${Math.round(x)}px, ${Math.round(y)}px, 0)`;
870872
}
871873

872874
/** Creates a deep clone of an element. */

‎src/cdk/drag-drop/drop-list.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,12 @@ export class CdkDropList<T = any> implements OnInit, OnDestroy {
362362
// Note that we shouldn't use `getBoundingClientRect` here to update the cache, because the
363363
// elements may be mid-animation which will give us a wrong result.
364364
if (isHorizontal) {
365-
elementToOffset.style.transform = `translate3d(${sibling.offset}px, 0, 0)`;
365+
// Round the transforms since some browsers will
366+
// blur the elements, for sub-pixel transforms.
367+
elementToOffset.style.transform = `translate3d(${Math.round(sibling.offset)}px, 0, 0)`;
366368
this._adjustClientRect(sibling.clientRect, 0, offset);
367369
} else {
368-
elementToOffset.style.transform = `translate3d(0, ${sibling.offset}px, 0)`;
370+
elementToOffset.style.transform = `translate3d(0, ${Math.round(sibling.offset)}px, 0)`;
369371
this._adjustClientRect(sibling.clientRect, offset, 0);
370372
}
371373
});

0 commit comments

Comments
 (0)
Please sign in to comment.