Created
November 7, 2014 02:15
-
-
Save doublejosh/f1a0b5302b9a141af75b to your computer and use it in GitHub Desktop.
Cross origin colorTheif
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
| // Get image colors (cross origin fun). | |
| $(window).load(function() { | |
| var canvas = document.createElement('canvas'), | |
| ctx = canvas.getContext("2d"); | |
| bgColor, | |
| $image; | |
| canvas.width = 50; | |
| canvas.height= 50; | |
| $('#card-deck .card img').each(function() { | |
| var $finalImage, | |
| image = new Image(), | |
| imgData; | |
| image.onload = function() { | |
| ctx.drawImage(image, 0, 0); | |
| imgData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
| ctx.putImageData(imgData, 0, 0); | |
| $finalImage = $('<img/>', { | |
| src: canvas.toDataURL(), | |
| width: 50, | |
| height: 50 | |
| }); | |
| bgColor = colorThief.getColor($finalImage[0]); | |
| $card.css('background-color', 'rgb(' + bgColor[0] + ',' + bgColor[1] + ',' + bgColor[2] + ')'); | |
| } | |
| image.crossOrigin = "anonymous"; | |
| image.src = $(this).src; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment