Skip to content

Instantly share code, notes, and snippets.

@krabs-github
Last active July 26, 2021 19:29
Show Gist options
  • Save krabs-github/68ed724a3d8664947ac411639fb5cd83 to your computer and use it in GitHub Desktop.
Save krabs-github/68ed724a3d8664947ac411639fb5cd83 to your computer and use it in GitHub Desktop.
[Set Image From URL Sample] Set Image from URL Function #Public #Streamdeck
// Variables beginning with "v" are the variables you define prior to calling the function.
// Function to do the conversion to Base64 from a URL
function convertImgToBase64(url, callback, outputFormat){
var canvas = document.createElement('CANVAS');
var ctx = canvas.getContext('2d');
var img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img,0,0);
var dataURL = canvas.toDataURL(outputFormat || 'image/png');
callback.call(this, dataURL);
// Clean up
canvas = null;
};
img.src = url;
}
// Calling the function
// You must also include the setImage function
var imageUrl = yourimageurl; // *** Change the "yourimageurl" to the url of the image you want to use. (ie. https://mywebsite.com/someimage.png ***
convertImgToBase64(imageUrl, function(base64Img){
var vBase64img = base64Img; // *** vBase64img is the variable your will pass to setImage function.
setImage(context, vBase64img);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment