We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am trying to find the colour underneath my mouse pointer. I want to be able to load an image from a local folder selected by a user (ie through a file window open dialogue box) then scroll over any part of the image and capture the RGB value below my pointer.
Currently the html code loads an image to the screen but then p5 cannot read the value of the pixel of the loaded image (I believe because they are being drawn to separate frames/canvases ?) It is correctly reading colour of pixels of things drawn in the sketch.js file (below this is shown with a test shape - a red circle)
Thanks for the help. Code below...
index.html
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render image.
var span = document.createElement('span');
span.innerHTML = ['img src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
body {padding: 0; margin: 0;}
sketch.js
function setup(){ colorMode(RGB, height, height, height); createCanvas(1024, 768); } function draw() { // put drawing code here fill("red"); ellipse(50, 50, 80, 80); // putting something on the sketch canvas } function mouseMoved(){ var color_under_mouse = get(mouseX, mouseY); println(mouseX); println(mouseY); println(color_under_mouse); }
Answers
First things first
:)
the pre code always fails me when just trying to write < img so the '<' is missing on line 19 of the code above
You don't need the
pre
tag, only four spaces before line start and a 2 blank lines before and after the code… I guess… Let's see:I think you need to display the image using p5.js with an
image(ing, x, y);
function, indraw()
. Not using HTML.<canvas>
.<canvas>
only.<canvas>
.BtW,
<
in HTML code is<
. ;)how would I "stamp' it into a shared canvas? thx