Forked from georgebyte/background-color-picker.js
Last active
August 29, 2015 14:22
-
-
Save JPKCom/2377e7b0d06c770e2c52 to your computer and use it in GitHub Desktop.
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
var image = new Image(); | |
image.src = "sample.jpg"; | |
$(image).load(function() { | |
ctx.drawImage(image, 0, 0); | |
}); | |
$(canvas).click(function(e) { | |
var canvasOffset = $(canvas).offset(); | |
var canvasX = Math.floor(e.pageX-canvasOffset.left); | |
var canvasY = Math.floor(e.pageY-canvasOffset.top); | |
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
var pixels = imageData.data; | |
var pixelRedIndex = ((canvasY - 1) * (imageData.width * 4)) + ((canvasX - 1) * 4); | |
var pixelcolor = "rgba("+pixels[pixelRedIndex]+", "+pixels[pixelRedIndex+1]+", "+pixels[pixelRedIndex+2]+", "+pixels[pixelRedIndex+3]+")"; | |
$("body").css("backgroundColor", pixelcolor); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment