getBorder = (img) => {
const context = DOM.context2d(img.width, img.height);
context.drawImage(img, 0, 0);
const w = img.width * 4,
h = img.height * 4;
const imgd = context.getImageData(0, 0, w, h);
let pix = imgd.data;
let newimgd = context.createImageData(imgd);
let newPix = newimgd.data;
let newimgd2 = context.createImageData(imgd);
let newPix2 = newimgd2.data;
const blackpixel = 0;
let prevTransparent = true;
function processPixel(i, newPix) {
function setBlack(i) {
newPix[i] = blackpixel;
newPix[i + 1] = blackpixel;
newPix[i + 2] = blackpixel;
newPix[i + 3] = 255;
}
function setTransparent(i) {
newPix[i] = 0;
newPix[i + 1] = 0;
newPix[i + 2] = 0;
newPix[i + 3] = 0;
}
if (i % (w * 4) === 0) {
prevTransparent = true;
}
if (pix[i + 3] !== 0) {
if (prevTransparent) {
setBlack(i);
} else {
setTransparent(i);
}
prevTransparent = false;
} else {
if (!prevTransparent && i > 3) {
setBlack(i - 4);
}
prevTransparent = true;
}
}
for (var i = 0, n = pix.length; i < n; i += 4) {
processPixel(i, newPix);
}
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
let i = (y * w + x) * 4;
processPixel(i, newPix2);
}
}
context.putImageData(newimgd, 0, 0);
context.putImageData(newimgd2, 0, 0);
return context.canvas;
}