73 lines
2.1 KiB
JavaScript
73 lines
2.1 KiB
JavaScript
'use strict';
|
|
/* globals $ */
|
|
|
|
function ImageDataHandler(img) {
|
|
|
|
var _cropData;
|
|
|
|
/**
|
|
* Gets information about how the cropped image is dragged
|
|
* @return {Object}
|
|
*/
|
|
this.getCropData = function () {
|
|
return _cropData;
|
|
};
|
|
|
|
this.setCropData = function() {
|
|
var viewportBounds = img.canvas.getViewport().getBounds();
|
|
if (!viewportBounds) {
|
|
return;
|
|
}
|
|
|
|
var dragAxis = img.canvas.getViewport().getData().axis;
|
|
|
|
var leftDiff = viewportBounds.left - img.left;
|
|
var topDiff = viewportBounds.top - img.top;
|
|
|
|
// Robustness measure to handle the extreme precision of shape positioning in canvas.
|
|
var countAsZeroLimit = 1e-5;
|
|
leftDiff = Math.abs(leftDiff) < countAsZeroLimit ? 0 : leftDiff;
|
|
topDiff = Math.abs(topDiff) < countAsZeroLimit ? 0 : topDiff;
|
|
|
|
_cropData = {
|
|
x: dragAxis === 'x' ? (leftDiff) / (img.width * img.scaleX) : 0,
|
|
y: dragAxis === 'y' ? (topDiff) / (img.height * img.scaleY) : 0,
|
|
viewportWidth: viewportBounds.width,
|
|
viewportHeight: viewportBounds.height,
|
|
};
|
|
};
|
|
|
|
this.removeCropData = function() {
|
|
_cropData = null;
|
|
};
|
|
|
|
/**
|
|
* Gets an object containing applied transformations for the Image and also
|
|
* how it is cropped and dragged.
|
|
* @return {Object}
|
|
*/
|
|
this.getData = function () {
|
|
var viewportData = img.canvas.getViewport().getData();
|
|
|
|
var returnValue = $.extend(
|
|
{},
|
|
{
|
|
mirrored: img.canvas.getMirror().getStatus(),
|
|
width: viewportData.dim.width,
|
|
height: viewportData.dim.height,
|
|
},
|
|
this.getCropData()
|
|
);
|
|
|
|
if (img.canvas.__type === 'canvas') {
|
|
var frameHandler = img.canvas.getViewport().getFrameHandler();
|
|
returnValue.edge = frameHandler.getFrameType();
|
|
returnValue.framed = returnValue.edge !== 'none';
|
|
}
|
|
|
|
return returnValue;
|
|
};
|
|
}
|
|
|
|
module.exports = ImageDataHandler;
|