Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scale parameter #352

Merged
merged 5 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Expand Up @@ -97,6 +97,7 @@ class App extends React.Element {
defaultPosition={{x: 0, y: 0}}
position={null}
grid={[25, 25]}
scale={1}
onStart={this.handleStart}
onDrag={this.handleDrag}
onStop={this.handleStop}>
Expand Down Expand Up @@ -217,6 +218,11 @@ onStop: DraggableEventHandler,
// becomes 'controlled' and is not responsive to user input. Use `position`
// if you need to have direct control of the element.
position: {x: number, y: number}

// Specifies the scale of the canvas your are dragging this element on. This allows
// you to, for example, get the correct drag deltas while you are zoomed in or out via
// a transform or matrix in the parent of this element.
scale: number
}
```

Expand Down Expand Up @@ -272,7 +278,8 @@ on itself and thus must have callbacks attached to be useful.
onStart: DraggableEventHandler,
onDrag: DraggableEventHandler,
onStop: DraggableEventHandler,
onMouseDown: (e: MouseEvent) => void
onMouseDown: (e: MouseEvent) => void,
scale: number
}
```

Expand Down
4 changes: 3 additions & 1 deletion lib/Draggable.js
Expand Up @@ -29,6 +29,7 @@ export type DraggableProps = {
defaultClassNameDragged: string,
defaultPosition: ControlPosition,
position: ControlPosition,
scale: number
};

//
Expand Down Expand Up @@ -162,7 +163,8 @@ export default class Draggable extends React.Component<DraggableProps, Draggable
defaultClassNameDragging: 'react-draggable-dragging',
defaultClassNameDragged: 'react-draggable-dragged',
defaultPosition: {x: 0, y: 0},
position: null
position: null,
scale: 1
};

constructor(props: DraggableProps) {
Expand Down
6 changes: 6 additions & 0 deletions lib/DraggableCore.js
Expand Up @@ -115,6 +115,12 @@ export default class DraggableCore extends React.Component<DraggableCoreProps, D
* `grid` specifies the x and y that dragging should snap to.
*/
grid: PropTypes.arrayOf(PropTypes.number),

/**
* `scale` specifies the scale of the area you are dragging inside of. It allows
* the drag deltas to scale correctly with how far zoomed in/out you are.
*/
scale: PropTypes.number,

/**
* `handle` specifies a selector to be used as the handle that initiates drag.
Expand Down
9 changes: 5 additions & 4 deletions lib/utils/positionFns.js
Expand Up @@ -103,12 +103,13 @@ export function createCoreData(draggable: DraggableCore, x: number, y: number):

// Create an data exposed by <Draggable>'s events
export function createDraggableData(draggable: Draggable, coreData: DraggableData): DraggableData {
const scale = draggable.props.scale;
return {
node: coreData.node,
x: draggable.state.x + coreData.deltaX,
y: draggable.state.y + coreData.deltaY,
deltaX: coreData.deltaX,
deltaY: coreData.deltaY,
x: draggable.state.x + (coreData.deltaX / scale),
y: draggable.state.y + (coreData.deltaY / scale),
deltaX: (coreData.deltaX / scale),
deltaY: (coreData.deltaY / scale),
lastX: draggable.state.x,
lastY: draggable.state.y
};
Expand Down
18 changes: 18 additions & 0 deletions specs/draggable.spec.jsx
Expand Up @@ -134,6 +134,24 @@ describe('react-draggable', function () {
assert(drag.props.onStop === handleStop);
});

it('should adjust draggable data output when `scale` prop supplied', function () {
function onDrag(event, data) {
assert(data.x === 200);
assert(data.y === 200);
assert(data.deltaX === 200);
assert(data.deltaY === 200);
}
drag = TestUtils.renderIntoDocument(
<Draggable
scale={0.5}
onDrag={onDrag}>
<div />
</Draggable>
);

simulateMovementFromTo(drag, 0, 0, 100, 100);
});

it('should throw when setting className', function () {
drag = (<Draggable className="foo"><span /></Draggable>);

Expand Down