Posted in Uncategorized How do I get the pixel color of a jpg? Fantashit January 6, 2021 1 Comment on How do I get the pixel color of a jpg? How do I get the pixel color of a jpg? For example: var imgTexture = THREE.ImageUtils.loadTexture( "environment/floor.jpg" ); function getPixel( x, y ){ //what is the code to do this? return colorPixel; } Hope someone can help. Thanks! Author: Fantashit
I think should be something like this: function getImageData( image ) { var canvas = document.createElement( 'canvas' ); canvas.width = image.width; canvas.height = image.height; var context = canvas.getContext( '2d' ); context.drawImage( image, 0, 0 ); return context.getImageData( 0, 0, image.width, image.height ); } function getPixel( imagedata, x, y ) { var position = ( x + imagedata.width * y ) * 4, data = imagedata.data; return { r: data[ position ], g: data[ position + 1 ], b: data[ position + 2 ], a: data[ position + 3 ] }; } var imagedata = getImageData( imgTexture.image ); var color = getPixel( imagedata, 10, 10 );
I think should be something like this: