Created
October 17, 2012 23:46
-
-
Save peterssonjesper/3909044 to your computer and use it in GitHub Desktop.
Creates a boxshadow based on an image
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
// Create an empty canvas | |
var canvas = document.createElement('canvas'); | |
// Create an image | |
var img = new Image(); | |
img.src = "http://path/to/image.png"; | |
// Wait for image to be downloaded | |
img.onload = function() { | |
// Draw the image on the canvas | |
var context = canvas.getContext("2d"); | |
context.drawImage(img, 0, 0); | |
// Result variable | |
var css = ""; | |
// Loop through every pixel | |
for(var x=0; x < img.width; ++x) { | |
for(var y=0; y < img.height; ++y) { | |
// Get the colors of this specific pixel and append to css | |
var colors = context.getImageData(x, y, 1, 1).data; | |
css += x + "px " + y + "px rgba(" + | |
colors[0] + ", " + | |
colors[1] + ", " + | |
colors[2] + ", " + | |
Math.round((colors[3]/255)*100)/100 + | |
"),"; | |
} | |
} | |
// Replace last , with ; | |
css = css.substring(0, css.length-1); | |
// Do something useful | |
console.log(css); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment