Merge pull request #2 from Kalekki/main

Crop transparency (what i was calling "smart crop" for some reason)
This commit is contained in:
tim h 2022-11-16 16:43:52 -06:00 committed by GitHub
commit c4ee4752a3

View file

@ -538,6 +538,38 @@ function drawBackground() {
function downloadImage() {
var link = document.createElement('a');
link.download = 'image.png';
link.href = imgCanvas.toDataURL('image/png');
link.href = cropCanvas(imgCanvas).toDataURL('image/png');
link.click();
}
function cropCanvas(sourceCanvas) {
var w = sourceCanvas.width;
var h = sourceCanvas.height;
var pix = {x:[], y:[]};
var imageData = sourceCanvas.getContext('2d').getImageData(0, 0, w, h);
var x, y, index;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
index = (y * w + x) * 4;
if (imageData.data[index+3] > 0) {
pix.x.push(x);
pix.y.push(y);
}
}
}
pix.x.sort(function(a,b){return a-b});
pix.y.sort(function(a,b){return a-b});
var n = pix.x.length-1;
w = pix.x[n] - pix.x[0];
h = pix.y[n] - pix.y[0];
var cut = sourceCanvas.getContext('2d').getImageData(pix.x[0], pix.y[0], w, h);
var cutCanvas = document.createElement('canvas');
cutCanvas.width = w;
cutCanvas.height = h;
cutCanvas.getContext('2d').putImageData(cut, 0, 0);
return cutCanvas;
}