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/resize-with-preserving-ratio #690

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 25 additions & 3 deletions croppie.css
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@
}

.croppie-container .cr-resizer-vertical,
.croppie-container .cr-resizer-horisontal {
.croppie-container .cr-resizer-horisontal,
.croppie-container .cr-resizer-top-right,
.croppie-container .cr-resizer-bottom-right {
position: absolute;
pointer-events: all;
}

.croppie-container .cr-resizer-vertical::after,
.croppie-container .cr-resizer-horisontal::after {
.croppie-container .cr-resizer-horisontal::after,
.croppie-container .cr-resizer-top-right::after,
.croppie-container .cr-resizer-bottom-right::after {
display: block;
position: absolute;
box-sizing: border-box;
Expand All @@ -66,11 +70,29 @@
height: 10px;
}

.croppie-container .cr-resizer-vertical::after {
.croppie-container .cr-resizer-top-right,
.croppie-container .cr-resizer-bottom-right {
width: 10px;
height: 10px;
right: -5px;
}

.croppie-container .cr-resizer-vertical {
cursor: row-resize;
left: 50%;
margin-left: -5px;
}

.croppie-container .cr-resizer-top-right {
cursor: nesw-resize;
top: -5px;
}

.croppie-container .cr-resizer-bottom-right {
cursor: nwse-resize;
bottom: -5px;
}

.croppie-container .cr-resizer-horisontal {
right: -5px;
cursor: col-resize;
Expand Down
136 changes: 136 additions & 0 deletions croppie.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@
if (self.options.enableResize) {
_initializeResize.call(self);
}
if (self.options.enableResizeWithRatio) {
_initializeRatioResize.call(self);
}
}

// function _initRotationControls () {
Expand Down Expand Up @@ -460,6 +463,139 @@
return this.options.enableExif && window.EXIF;
}

function _initializeRatioResize() {
var self = this;
var wrap = document.createElement('div');
var isDragging = false;
var direction;
var originalY;
var minSize = 50;
var maxWidth;
var maxHeight;
var ratio = self.options.viewport.width / self.options.viewport.height;
var tr;
var br;

addClass(wrap, 'cr-resizer');
css(wrap, {
width: this.options.viewport.width + 'px',
height: this.options.viewport.height + 'px'
});

tr = document.createElement('div');
addClass(tr, 'cr-resizer-top-right');
wrap.appendChild(tr);

br = document.createElement('div');
addClass(br, 'cr-resizer-bottom-right');
wrap.appendChild(br);

function getDirection(className) {
if(className.indexOf('top-right') >= 0) {
return 'tr';
}
if(className.indexOf('bottom-right') >= 0) {
return 'br';
}
}

function mouseDown(ev) {
if (ev.button !== undefined && ev.button !== 0) return;

ev.preventDefault();
if (isDragging) {
return;
}

var overlayRect = self.elements.overlay.getBoundingClientRect();

isDragging = true;
originalX = ev.pageX;
originalY = ev.pageY;
direction = getDirection(ev.currentTarget.className);
maxWidth = overlayRect.width;
maxHeight = overlayRect.height;
if (ev.touches) {
var touches = ev.touches[0];
originalX = touches.pageX;
originalY = touches.pageY;
}
window.addEventListener('mousemove', mouseMove);
window.addEventListener('touchmove', mouseMove);
window.addEventListener('mouseup', mouseUp);
window.addEventListener('touchend', mouseUp);
document.body.style[CSS_USERSELECT] = 'none';
}

function mouseMove(ev) {
var pageX = ev.pageX;
var pageY = ev.pageY;
var newVHeight;
var newVWidth;
ev.preventDefault();

if (ev.touches) {
var touches = ev.touches[0];
pageX = touches.pageX;
pageY = touches.pageY;
}

var deltaY = pageY - originalY;
var deltaX = deltaY * ratio;

if(direction === 'tr') {
newVHeight = self.options.viewport.height - deltaY;
newVWidth = self.options.viewport.width - deltaX;
}
if(direction === 'br') {
newVHeight = self.options.viewport.height + deltaY;
newVWidth = self.options.viewport.width + deltaX;
}
if(
(newVHeight && newVHeight >= minSize && newVHeight <= maxHeight && newVHeight <= self.options.boundary.height)
&&
(newVWidth && newVWidth >= minSize && newVWidth <= maxWidth && newVWidth <= self.options.boundary.width)
) {
css(wrap, {
height: newVHeight + 'px',
width: newVWidth + 'px'
});
self.options.viewport.height = newVHeight;
self.options.viewport.width = newVWidth;

css(self.elements.viewport, {
height: self.options.viewport.height + 'px',
width: self.options.viewport.width + 'px'
});
}

_updateOverlay.call(self);
_updateZoomLimits.call(self);
_updateCenterPoint.call(self);
_triggerUpdate.call(self);
originalY = pageY;
originalX = pageX;
}

function mouseUp() {
isDragging = false;
window.removeEventListener('mousemove', mouseMove);
window.removeEventListener('touchmove', mouseMove);
window.removeEventListener('mouseup', mouseUp);
window.removeEventListener('touchend', mouseUp);
document.body.style[CSS_USERSELECT] = '';
}

tr.addEventListener('mousedown', mouseDown);
tr.addEventListener('touchstart', mouseDown);

br.addEventListener('mousedown', mouseDown);
br.addEventListener('touchstart', mouseDown);


this.elements.boundary.appendChild(wrap);
}

function _initializeResize () {
var self = this;
var wrap = document.createElement('div');
Expand Down
34 changes: 29 additions & 5 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,40 @@ var Demo = (function() {
viewport: { width: 100, height: 100 },
boundary: { width: 300, height: 300 },
showZoomer: false,
enableResize: true,
enableOrientation: true,
mouseWheelZoom: 'ctrl'
enableResize: true,
enableOrientation: true,
mouseWheelZoom: 'ctrl'
});
resize.bind({
url: 'demo/demo-2.jpg',
zoom: 0
});
vEl.addEventListener('update', function (ev) {
});
document.querySelector('.resizer-result').addEventListener('click', function (ev) {
resize.result({
type: 'blob'
}).then(function (blob) {
popupResult({
src: window.URL.createObjectURL(blob)
});
});
});
}
function demoRatioResizer() {
var vEl = document.getElementById('resizer-ratio-demo'),
resize = new Croppie(vEl, {
viewport: { width: 250, height: 100 },
boundary: { width: 300, height: 300 },
showZoomer: false,
enableResizeWithRatio: true,
enableOrientation: true,
});
resize.bind({
url: 'demo/demo-2.jpg',
zoom: 0
});
vEl.addEventListener('update', function (ev) {
console.log('resize update', ev);
});
document.querySelector('.resizer-result').addEventListener('click', function (ev) {
resize.result({
Expand All @@ -178,7 +202,6 @@ var Demo = (function() {
$uploadCrop.croppie('bind', {
url: e.target.result
}).then(function(){
console.log('jQuery bind complete');
});

}
Expand Down Expand Up @@ -251,6 +274,7 @@ var Demo = (function() {
demoBasic();
demoVanilla();
demoResizer();
demoRatioResizer();
demoUpload();
demoHidden();
}
Expand Down
32 changes: 32 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,38 @@ <h2>Demos</h2>
</div>
</div>
</div>
<div class="demo-wrap demo-ratio-resize">
<div class="container">
<div class="grid">
<div class="col-1-2">
<strong>Resize With Preserving Ratio Example</strong>
<pre class="language-javascript"><code class="language-javascript">
var el = document.getElementById('resizer-ratio-demo');
var resize = new Croppie(el, {
viewport: { width: 100, height: 100 },
boundary: { width: 300, height: 100 },
showZoomer: false,
enableResizeWithRatio: true,
enableOrientation: true,
mouseWheelZoom: 'ctrl'
});
resize.bind({
url: 'demo/demo-2.jpg',
});
//on button click
resize.result('blob').then(function(blob) {
// do something with cropped blob
});</code></pre>
<div class="actions">
<button class="resizer-result">Result</button>
</div>
</div>
<div class="col-1-2">
<div id="resizer-ratio-demo"></div>
</div>
</div>
</div>
</div>
<div class="demo-wrap upload-demo">
<div class="container">
<div class="grid">
Expand Down