Updated image editor with not having to crop again after resizing

This commit is contained in:
Erik Tiekstra
2017-05-23 09:03:29 +02:00
parent 30e49c95c8
commit 5f9169ef51
7 changed files with 50 additions and 31 deletions
+1 -1
View File
@@ -29,7 +29,7 @@
</form> </form>
<br /><br /> <br /><br />
<div id="wrapper"> <div id="wrapper">
<canvas id="canvas-editor" data-image-id="45809"></canvas> <canvas id="canvas-editor" data-image-id="45810"></canvas>
</div> </div>
<br /><br /><br /> <br /><br /><br />
</div> </div>
+2 -2
View File
@@ -23,7 +23,7 @@ var Canvas = fabric.util.createClass(fabric.Canvas, {
}); });
this.on('mouse:up', function () { this.on('mouse:up', function () {
if (this.getImage().__moved) { if (this.getImage().__cropped) {
this.getApertures().setTransparent(false); this.getApertures().setTransparent(false);
// Trigger event to tell the image has been dragged. // Trigger event to tell the image has been dragged.
@@ -37,7 +37,7 @@ var Canvas = fabric.util.createClass(fabric.Canvas, {
this.setHeight(args.height); this.setHeight(args.height);
this.setWidth(args.width); this.setWidth(args.width);
this.getButtonMenu().render(); this.getButtonMenu().render();
this.setImage(this.getImage()); this.getViewport().reset();
this.sendToBack(this.getImage()); this.sendToBack(this.getImage());
this.getDragbars().resetPosition(); this.getDragbars().resetPosition();
this.renderAll(); this.renderAll();
+13 -8
View File
@@ -4,32 +4,37 @@ var config = require('../config');
var DragHandle = function (handler) { var DragHandle = function (handler) {
var _currentAxis; var _currentAxis;
var _dragging; var _dragHandle;
var _moved; var _movedHandle;
// Event declarations // Event declarations
var events = { var events = {
mouseDown: function () { mouseDown: function () {
_dragging = true; _dragHandle = true;
_currentAxis = this.axis; _currentAxis = this.axis;
handler.canvas.getImage().trigger('mousedown'); handler.canvas.getImage().trigger('mousedown');
}, },
mouseUp: function () { mouseUp: function () {
if (!_dragHandle) {
return;
}
if (!_moved && _currentAxis) { if (!_movedHandle) {
$(document).trigger('PIE:dragbar-click', { axis: _currentAxis }); $(document).trigger('PIE:dragbar-click', { axis: _currentAxis });
} }
_dragging = false; _dragHandle = false;
_moved = false; _movedHandle = false;
_currentAxis = null; _currentAxis = null;
handler.canvas.getImage().trigger('mouseup'); handler.canvas.getImage().trigger('mouseup');
}, },
mouseMove: function (e) { mouseMove: function (e) {
if (!_dragging) { if (!_dragHandle) {
return; return;
} }
_moved = true;
_movedHandle = true;
if (_currentAxis === 'h') { if (_currentAxis === 'h') {
handler.canvas.getImage().left -= e.e.movementX; handler.canvas.getImage().left -= e.e.movementX;
+2 -1
View File
@@ -13,7 +13,8 @@ function Draggable(img, boundingRect) {
* Triggers when object is moved * Triggers when object is moved
*/ */
function onMove() { function onMove() {
this.__moved = true; this.__cropped = true;
util.setPositionInside(this, _bounds); util.setPositionInside(this, _bounds);
} }
@@ -67,6 +67,7 @@ function ImageEditor(canvasId, args) {
* @return {ImageEditor} * @return {ImageEditor}
*/ */
this.crop = function (width, height) { this.crop = function (width, height) {
_canvas.getImage().__cropped = false;
_canvas.getViewport().set(width, height); _canvas.getViewport().set(width, height);
return this; return this;
}; };
+26 -8
View File
@@ -16,6 +16,7 @@ function Viewport(canvas) {
* Applies viewport to page * Applies viewport to page
*/ */
function apply() { function apply() {
var croppedData = getCroppedData();
canvas.remove(_rect); canvas.remove(_rect);
_rect = new fabric.Rect({ _rect = new fabric.Rect({
@@ -31,9 +32,18 @@ function Viewport(canvas) {
_rect.height += _borderWidth; _rect.height += _borderWidth;
_rect.addTo(canvas).center().setCoords(); _rect.addTo(canvas).center().setCoords();
canvas.getImage() if (croppedData) {
.center() // Calculate the new position after screenresize and when the
.drag.enable(calcMinMaxBoundsForRect(_rect.getInsideBoundingRect(), canvas.getImage())); // image already has been cropped to a certain position.
var left = _rect.left + _borderWidth - (canvas.getImage().width * croppedData.x);
var top = _rect.top + _borderWidth - (canvas.getImage().height * croppedData.y);
canvas.getImage().set({ left: left, top: top }).setCoords();
} else {
canvas.getImage().center().setCoords();
}
canvas.getImage().drag.enable(calcMinMaxBoundsForRect(_rect.getInsideBoundingRect(), canvas.getImage()))
canvas.getApertures().apply(_data.axis, _rect.getBoundingRect()); canvas.getApertures().apply(_data.axis, _rect.getBoundingRect());
if (canvas.__type !== 'canvas') { if (canvas.__type !== 'canvas') {
@@ -49,6 +59,9 @@ function Viewport(canvas) {
canvas.refresh3dView(); canvas.refresh3dView();
} }
function getCroppedData() {
return canvas.getImage().__cropped ? canvas.getImage().transformation.getCropData() : null;
}
/** /**
* Render the diagonal lines from viewport to edge of the canvas * Render the diagonal lines from viewport to edge of the canvas
@@ -126,12 +139,17 @@ function Viewport(canvas) {
apply(); apply();
this.getGores().reset(); this.getGores().reset();
this.getRulers().reset(); this.getRulers().reset();
canvas.getApertures().setTransparent(true);
canvas.getImage().__moved = false;
canvas.getButtonMenu().bringToFront();
// Trigger event to tell the image needs to be dragged. if (canvas.getImage().__cropped) {
$(document).trigger('PIE:needs-dragging', { axis: _data.axis });
} else {
canvas.getApertures().setTransparent(true);
// Trigger event to tell the image needs to be dragged.
$(document).trigger('PIE:needs-dragging', { axis: _data.axis });
}
canvas.getButtonMenu().bringToFront();
}; };
+5 -11
View File
@@ -23,20 +23,14 @@
Event.handleResize(); Event.handleResize();
}); });
function getOptimalDimension(currentDimension, ratio) { function getOptimalDimension(values, currentDimension, ratio) {
var width = $input.w.val();
var height = $input.h.val();
if (currentDimension === 'h') { if (currentDimension === 'h') {
width = Math.round(height / ratio); values.width = Math.round(values.height * ratio);
} else { } else {
height = Math.round(width / ratio); values.height = Math.round(values.width / ratio);
} }
return { return values;
width: width,
height: height
};
} }
var Event = { var Event = {
@@ -70,7 +64,7 @@
handleResize: function() { handleResize: function() {
var isFullScreen = document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement; var isFullScreen = document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement;
var width = $wrapper.width(); var width = $wrapper.width();
var height = isFullScreen ? $window.height() : $wrapper.height(); var height = isFullScreen ? ($window.height() < 800 ? $window.height() : 800) : $wrapper.height();
editor.canvas.resize({width: width, height: height}); editor.canvas.resize({width: width, height: height});
} }
}; };