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

Feature: Add scale prop #434

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ Show [rule of thirds](https://en.wikipedia.org/wiki/Rule_of_thirds) lines in the

Show the crop area as a circle. If your aspect is not 1 (a square) then the circle will be warped into an oval shape. Defaults to `false`.

#### scale (optional)

If a parent element is zoomed or scaled, you can pass in the scale factor in order to correct the cropping bounds. Defaults to `1`.

## FAQ

### What about showing the crop on the client?
Expand Down
47 changes: 25 additions & 22 deletions lib/ReactCrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class ReactCrop extends PureComponent {
};

onComponentPointerDown = e => {
const { crop, disabled, locked, keepSelection, onChange } = this.props;
const { crop, disabled, locked, keepSelection, onChange, scale } = this.props;

const componentEl = this.mediaWrapperRef.firstChild;

Expand All @@ -286,8 +286,9 @@ class ReactCrop extends PureComponent {
this.componentRef.focus({ preventScroll: true }); // All other browsers

const rect = this.mediaWrapperRef.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;

let x = (e.clientX - rect.left) / scale;
let y = (e.clientY - rect.top) / scale;

const nextCrop = {
unit: 'px',
Expand Down Expand Up @@ -325,7 +326,7 @@ class ReactCrop extends PureComponent {
};

onDocPointerMove = e => {
const { crop, disabled, onChange, onDragStart } = this.props;
const { crop, disabled, onChange, onDragStart, scale } = this.props;

if (disabled) {
return;
Expand All @@ -344,8 +345,8 @@ class ReactCrop extends PureComponent {

const { evData } = this;

evData.xDiff = e.clientX - evData.clientStartX;
evData.yDiff = e.clientY - evData.clientStartY;
evData.xDiff = (e.clientX - evData.clientStartX) / scale;
evData.yDiff = (e.clientY - evData.clientStartY) / scale;

let nextCrop;

Expand Down Expand Up @@ -640,22 +641,22 @@ class ReactCrop extends PureComponent {
onPointerDown={this.onCropPointerDown}
>
{!disabled && !locked && (
<div className="ReactCrop__drag-elements">
<div className="ReactCrop__drag-bar ord-n" data-ord="n" />
<div className="ReactCrop__drag-bar ord-e" data-ord="e" />
<div className="ReactCrop__drag-bar ord-s" data-ord="s" />
<div className="ReactCrop__drag-bar ord-w" data-ord="w" />

<div className="ReactCrop__drag-handle ord-nw" data-ord="nw" />
<div className="ReactCrop__drag-handle ord-n" data-ord="n" />
<div className="ReactCrop__drag-handle ord-ne" data-ord="ne" />
<div className="ReactCrop__drag-handle ord-e" data-ord="e" />
<div className="ReactCrop__drag-handle ord-se" data-ord="se" />
<div className="ReactCrop__drag-handle ord-s" data-ord="s" />
<div className="ReactCrop__drag-handle ord-sw" data-ord="sw" />
<div className="ReactCrop__drag-handle ord-w" data-ord="w" />
</div>
)}
<div className="ReactCrop__drag-elements">
<div className="ReactCrop__drag-bar ord-n" data-ord="n" />
<div className="ReactCrop__drag-bar ord-e" data-ord="e" />
<div className="ReactCrop__drag-bar ord-s" data-ord="s" />
<div className="ReactCrop__drag-bar ord-w" data-ord="w" />

<div className="ReactCrop__drag-handle ord-nw" data-ord="nw" />
<div className="ReactCrop__drag-handle ord-n" data-ord="n" />
<div className="ReactCrop__drag-handle ord-ne" data-ord="ne" />
<div className="ReactCrop__drag-handle ord-e" data-ord="e" />
<div className="ReactCrop__drag-handle ord-se" data-ord="se" />
<div className="ReactCrop__drag-handle ord-s" data-ord="s" />
<div className="ReactCrop__drag-handle ord-sw" data-ord="sw" />
<div className="ReactCrop__drag-handle ord-w" data-ord="w" />
</div>
)}
{renderSelectionAddon && isCropValid(crop) && (
<div className="ReactCrop__selection-addon" onMouseDown={e => e.stopPropagation()}>
{renderSelectionAddon(this.state)}
Expand Down Expand Up @@ -833,6 +834,7 @@ ReactCrop.propTypes = {
renderComponent: PropTypes.node,
renderSelectionAddon: PropTypes.func,
ruleOfThirds: PropTypes.bool,
scale: PropTypes.number
};

ReactCrop.defaultProps = {
Expand All @@ -859,6 +861,7 @@ ReactCrop.defaultProps = {
imageStyle: undefined,
renderSelectionAddon: undefined,
ruleOfThirds: false,
scale: 1
};

export { ReactCrop as default, ReactCrop as Component, makeAspectCrop, containCrop };