Created
November 22, 2016 03:25
-
-
Save jstncno/55cd2e395864e223a2a4b6b2836ca869 to your computer and use it in GitHub Desktop.
Get data of a remote image. Needs timestamp to get past crossOrigin
This file contains hidden or 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
| // https://davidwalsh.name/convert-image-data-uri-javascript | |
| var getDataUri = function(url, callback) { | |
| var colorThief = new ColorThief(); | |
| var image = new Image(); | |
| image.onload = function () { | |
| var canvas = document.createElement('canvas'); | |
| canvas.id = 'backgroundImg'; | |
| canvas.style.display = 'none'; | |
| canvas.getContext('2d').drawImage(this, 0, 0); | |
| document.body.appendChild(canvas); | |
| var color = colorThief.getColor(canvas); | |
| callback(color); | |
| }; | |
| image.src = url + '?' + new Date().getTime(); | |
| image.setAttribute('crossOrigin', ''); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment