Last active
July 26, 2021 19:29
-
-
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
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
// 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