Skip to content

Commit

Permalink
chore(dragging): normalize coordinates to full pixels
Browse files Browse the repository at this point in the history
This ensures dragging emits events with full pixel coordinates only, as
we don't support sub-pixel positioning
anywhere anyway.

Closes #271
  • Loading branch information
nikku committed Aug 22, 2018
1 parent d5c497c commit 559e6f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/features/dragging/Dragging.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ export default function Dragging(eventBus, canvas, selection) {
var clientRect = canvas._container.getBoundingClientRect();

return {
x: viewbox.x + round((globalPosition.x - clientRect.left) / viewbox.scale),
y: viewbox.y + round((globalPosition.y - clientRect.top) / viewbox.scale)
x: viewbox.x + (globalPosition.x - clientRect.left) / viewbox.scale,
y: viewbox.y + (globalPosition.y - clientRect.top) / viewbox.scale
};
}

Expand Down Expand Up @@ -184,15 +184,16 @@ export default function Dragging(eventBus, canvas, selection) {
localCurrent = toLocalPoint(globalCurrent),
localDelta = deltaPos(localCurrent, localStart);


// activate context explicitly or once threshold is reached
if (!context.active && (activate || getLength(globalDelta) > context.threshold)) {

// fire start event with original
// starting coordinates

assign(payload, {
x: localStart.x + displacement.x,
y: localStart.y + displacement.y,
x: round(localStart.x + displacement.x),
y: round(localStart.y + displacement.y),
dx: 0,
dy: 0
}, { originalEvent: event });
Expand Down Expand Up @@ -226,10 +227,10 @@ export default function Dragging(eventBus, canvas, selection) {

// update payload with actual coordinates
assign(payload, {
x: localCurrent.x + displacement.x,
y: localCurrent.y + displacement.y,
dx: localDelta.x,
dy: localDelta.y
x: round(localCurrent.x + displacement.x),
y: round(localCurrent.y + displacement.y),
dx: round(localDelta.x),
dy: round(localDelta.y)
}, { originalEvent: event });

// emit move event
Expand Down
31 changes: 31 additions & 0 deletions test/spec/features/dragging/DraggingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,37 @@ describe('features/dragging - Dragging', function() {
]);
}));


it('should normalize to full pixel coordinates', inject(
function(dragging, canvas) {

// given
canvas.zoom(0.37);

// assume viewbox has sub-pixel x coordinate
expect(canvas.viewbox().x).not.to.eql(Math.round(canvas.viewbox().x));
expect(canvas.viewbox().y).not.to.eql(Math.round(canvas.viewbox().y));

var events = recordEvents('foo');

// when
dragging.init(canvasEvent({ x: 0, y: 0 }), { x: 10.12321312, y: 9.9123821 }, 'foo');
dragging.move(canvasEvent({ x: 20, y: 10 }));

dragging.cancel();

// then
expect(events.map(raw)).to.eql([
{ type: 'foo.init' },
{ x: 10, y: 10, dx: 0, dy: 0, type: 'foo.start' },
{ x: 64, y: 37, dx: 54, dy: 27, type: 'foo.move' },
{ x: 64, y: 37, dx: 54, dy: 27, type: 'foo.cancel' },
{ x: 64, y: 37, dx: 54, dy: 27, type: 'foo.cleanup' },
{ x: 64, y: 37, dx: 54, dy: 27, type: 'foo.canceled' }
]);
}
));

});

});
Expand Down

0 comments on commit 559e6f9

Please sign in to comment.