I have an image (imgt
) and a resizable and movable crop div (ram
) and want to crop imgt
according to size and position of ram
.
Result - dimensions are ok, but there is no image, just a black rectangle.
var img = document.getElementById("imgt");
c1 = document.createElement("canvas");
var ctx = c1.getContext("2d");
var a = $('#imgt').width();
var b = $('#imgt').height();
c1.width = a;
c1.height = b;
ctx.drawImage(img, 0, 0, a, b);
ax = $('#ram').position().left;
ay = $('#ram').position().top;
aw = $('#ram').width();
ah = $('#ram').height();
var img = new Image();
img.src = c1.toDataURL('image/jpeg');
c2 = document.createElement("canvas");
c2.width = aw;
c2.height = ah;
var ctx = c2.getContext("2d");
ctx.drawImage(img, ax, ay, aw, ah, 0, 0, aw, ah);
var dl = document.createElement("a");
dl.href = c2.toDataURL("image/jpeg");
dl.download = true;
document.body.appendChild(dl);
dl.click();