Skip to content
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
67 changes: 41 additions & 26 deletions lib/js/idcrop/IdCrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class IdCrop {
allowUpload:
typeof config.allowUpload !== "undefined" ? config.allowUpload : true,
closeButtonSelector:
typeof config.closeButtonSelector !== "undefined"
? config.closeButtonSelector
: false,
typeof config.closeButtonSelector !== "undefined"
? config.closeButtonSelector
: false,
containers: {
displayArea: document.querySelector(config.displaySelector),
toolbarArea: config.toolbarSelector
Expand All @@ -29,81 +29,80 @@ class IdCrop {
croppingArea: {
overlayColor:
typeof config.croppingArea !== "undefined" &&
typeof config.croppingArea.overlayColor !== "undefined"
typeof config.croppingArea.overlayColor !== "undefined"
? config.croppingArea.overlayColor
: "rgba(0, 0, 0, 0.7)",
stroke:
typeof config.croppingArea !== "undefined" &&
typeof config.croppingArea.stroke !== "undefined"
typeof config.croppingArea.stroke !== "undefined"
? config.croppingArea.stroke
: true,
strokeColor:
typeof config.croppingArea !== "undefined" &&
typeof config.croppingArea.strokeColor !== "undefined"
typeof config.croppingArea.strokeColor !== "undefined"
? config.croppingArea.strokeColor
: "white",
strokeDashed:
typeof config.croppingArea !== "undefined" &&
typeof config.croppingArea.strokeDashed !== "undefined"
typeof config.croppingArea.strokeDashed !== "undefined"
? config.croppingArea.strokeDashed
: true,
strokeWeight:
typeof config.croppingArea !== "undefined" &&
typeof config.croppingArea.strokeWeight !== "undefined"
typeof config.croppingArea.strokeWeight !== "undefined"
? config.croppingArea.strokeWeight
: 2
},
crop: {
overlayColor:
typeof config.crop !== "undefined" &&
typeof config.crop.overlayColor !== "undefined"
typeof config.crop.overlayColor !== "undefined"
? config.crop.overlayColor
: "rgba(0, 0, 0, 0)",
fillColor:
typeof config.crop !== "undefined" &&
typeof config.crop.fillColor !== "undefined"
typeof config.crop.fillColor !== "undefined"
? config.crop.fillColor
: false,
showImage:
typeof config.crop !== "undefined" &&
typeof config.crop.showImage !== "undefined"
typeof config.crop.showImage !== "undefined"
? config.crop.showImage
: true,
stroke:
typeof config.crop !== "undefined" &&
typeof config.crop.stroke !== "undefined"
typeof config.crop.stroke !== "undefined"
? config.crop.stroke
: true,
strokeColor:
typeof config.crop !== "undefined" &&
typeof config.crop.strokeColor !== "undefined"
typeof config.crop.strokeColor !== "undefined"
? config.crop.strokeColor
: false,
strokeDashed:
typeof config.crop !== "undefined" &&
typeof config.crop.strokeDashed !== "undefined"
typeof config.crop.strokeDashed !== "undefined"
? config.crop.strokeDashed
: false,
strokeWeight:
typeof config.crop !== "undefined" &&
typeof config.crop.strokeWeight !== "undefined"
typeof config.crop.strokeWeight !== "undefined"
? config.crop.strokeWeight
: 0
},
handles: {
class:
typeof config.handles !== "undefined" &&
typeof config.handles.class !== "undefined"
typeof config.handles.class !== "undefined"
? config.handles.class
: "",
defaultStyles:
typeof config.handles !== "undefined" &&
typeof config.handles.defaultStyles !== "undefined"
typeof config.handles.defaultStyles !== "undefined"
? config.handles.defaultStyles
: true
}
};

this.handles = [];
this.points = [];
}
Expand All @@ -127,7 +126,7 @@ class IdCrop {
closeButton.appendChild(closeButtonText);
c.toolbarArea.appendChild(closeButton);
}
closeButton.addEventListener("click", function() {
closeButton.addEventListener("click", function () {
self.startCroppingPolygon();
});
}
Expand Down Expand Up @@ -158,14 +157,19 @@ class IdCrop {

if (!c.toolbarArea || !this.config.allowUpload) {
var src = helpers.getBgSource(c.displayArea);
helpers.dataURIFromSrc(src).then(function(base64) {
helpers.dataURIFromSrc(src).then(function (base64) {
self.startCroppingArea(base64, self);
});
}

window.addEventListener("mouseup", function() {
window.addEventListener("mouseup", function () {
if (self.config.containers.previewArea) self.drawPreview(self);
document.onmousemove = function () { };
});

window.addEventListener("touchend", function () {
if (self.config.containers.previewArea) self.drawPreview(self);
document.onmousemove = function() {};
document.ontouchmove = function () { };
});

var created = new CustomEvent("created", { detail: self.config });
Expand All @@ -176,7 +180,7 @@ class IdCrop {
const displayArea = self.config.containers.displayArea;

self.cropArea = new CropArea(displayArea, base64);
self.cropArea.create().then(function(img) {
self.cropArea.create().then(function (img) {
self.image = img;
displayArea.style.backgroundImage = "url('" + base64 + "')";
});
Expand All @@ -186,7 +190,11 @@ class IdCrop {
}

self.handles = [];
self.cropArea.canvas.addEventListener("mousedown", function(event) {
self.cropArea.canvas.addEventListener("mousedown", function (event) {
self.createHandles(event, self);
});

self.cropArea.canvas.addEventListener("touchcancel", function (event) {
self.createHandles(event, self);
});
}
Expand All @@ -210,6 +218,7 @@ class IdCrop {
self.points.push(point);

if (self.handles.length == self.config.numPoints) {
// when the number of points are placed --> crop the area
self.startCroppingPolygon(self);
}
}
Expand Down Expand Up @@ -237,9 +246,15 @@ class IdCrop {
for (const handle of self.handles) {
const config = self.config.croppingArea;
handle.direction = handle.setDirection(self.points);
handle.node.addEventListener("mousedown", function(event) {

handle.node.addEventListener("mousedown", function (event) {
self.cropPolygon.startResizing(event, self.points, config);
});

handle.node.addEventListener("touchstart", function (event) {
self.cropPolygon.startResizing(event, self.points, config);
});

}

self.drawPreview(self);
Expand All @@ -264,7 +279,7 @@ class IdCrop {
reset() {
this.cropArea = undefined;
this.cropPolygon = undefined;

this.handles = [];
this.points = [];
this.image = "data:image/jpeg;base64,";
Expand Down
13 changes: 12 additions & 1 deletion lib/js/idcrop/Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class Polygon {
const hdim = handle.offsetWidth;
const direction = parseInt(handle.id.substr(10), 10);
const bounds = this.canvas.getBoundingClientRect();

let that = this;
document.onmousemove = function(event) {
let x = event.clientX;
Expand All @@ -78,6 +77,18 @@ class Polygon {

that.resize(handle, direction, points, x, y, config);
};

document.ontouchmove = function(event) {
let x = event.changedTouches[0].clientX;
let y = event.changedTouches[0].clientY;

if (x < bounds.left) x = bounds.left;
if (x > bounds.right - hdim) x = bounds.right - hdim;
if (y < bounds.top) y = bounds.top;
if (y > bounds.bottom - hdim) y = bounds.bottom - hdim;

that.resize(handle, direction, points, x, y, config);
};
}
}

Expand Down
Loading