Last active
April 4, 2018 04:10
Star
You must be signed in to star a gist
Remove White Space in Image to create Transparency in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
background: lightblue; | |
} | |
#modified { | |
float: left !important; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<img src="" id="original" style="display:none;"> | |
<canvas id="modified" style="display: none"></canvas> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** I chose to delete white space from a logo */ | |
function DisplayLogo() { | |
var logoUrl = "https://cdn.thingiverse.com/renders/2a/83/69/af/59/83c213b7acf3bd593b1211644dc9a07c_preview_featured.jpg"; | |
// Converting URL to Base64 with the use of a proxy to get around the cross domain | |
var getDataUri = function (targetUrl, callback) { | |
var xhr = new XMLHttpRequest(); | |
xhr.onload = function () { | |
var reader = new FileReader(); | |
reader.onloadend = function () { | |
callback(reader.result); | |
}; | |
reader.readAsDataURL(xhr.response); | |
}; | |
var proxyUrl = 'https://cors-anywhere.herokuapp.com/'; | |
xhr.open('GET', proxyUrl + targetUrl); | |
xhr.responseType = 'blob'; | |
xhr.send(); | |
}; | |
// Returns Base 64 of image | |
getDataUri(logoUrl, function (base64) { | |
// console.log('RESULT:', base64); | |
//handling no Logo | |
if (base64 === "data:image/png;base64,Cg==") { | |
// console.log("Reached No Logo"); | |
// Setting logo to not display | |
document.getElementById("modified").style.display = "none"; | |
document.getElementById("original").style.display = "none"; | |
// reducing padding if no logo | |
document.getElementById("CompanyName").style.paddingLeft = "0px"; | |
} | |
//original | |
$("#original").attr("src", base64); | |
$("#original").on("load", function () { | |
var canvas = document.getElementById("modified"), | |
ctx = canvas.getContext("2d"), | |
image = document.getElementById("original"); | |
// This size can change | |
canvas.height = canvas.width = 50; | |
// Updating modified id to display | |
document.getElementById("modified").style.display = "block"; | |
ctx.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height); | |
var imgd = ctx.getImageData(0, 0, 1000, 1000), | |
pix = imgd.data, | |
newColor = {r: 0, g: 0, b: 0, a: 0}; | |
for (var i = 0, n = pix.length; i < n; i += 4) { | |
var r = pix[i], | |
g = pix[i + 1], | |
b = pix[i + 2]; | |
if (r >= 250 && g >= 250 && b >= 250) { | |
// Change the white to the new color. | |
pix[i] = newColor.r; | |
pix[i + 1] = newColor.g; | |
pix[i + 2] = newColor.b; | |
pix[i + 3] = newColor.a; | |
} | |
} | |
ctx.putImageData(imgd, 0, 0); | |
}) | |
}); | |
} | |
DisplayLogo(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment