I'm facing a challenge with allowing users to upload their image file through a DOM file uploader (
<input type="file"></input>
). Once the image is uploaded, I'm unsure of how to transfer it to JavaScript and process it using p5.js. How can I convert an HTML file to a p5.js file or an array of pixels that I can manipulate in JavaScript?
Initially, I set restrictions on the element to only accept .png
and .jpg
files.
I attempted to use the .file[0]
syntax and load the image by its path using the .value
within the loadImage()
function.
Unfortunately, neither method worked for me, and I'm feeling lost about what steps to take next.
<div id="uploadMenu">
<h4>Please select your image file:</h4>
<input accept=".png, .jpg, .jpeg" type="file" id="imageUpload">
<button onclick="fileToGrid()">Done</button>
<script>
</script>
<script>
let thisWindow = document.getElementById("uploadMenu");
let windowWidth = thisWindow.style.width;
console.log(windowWidth);
thisWindow.style.left = `${innerWidth/2 - 100}px`
thisWindow.style.top = `${innerHeight/2 - 50}px`
</script>
</div>
function fileToGrid() {
let uploadFromHTML = document.getElementById("imageUpload");
let uploadedImage = loadImage(uploadFromHTML.value);
let imageW = uploadedImage.width;
let imageH = uploadedImage.height;
console.log(imageW, imageH);
// My ultimate goal is to convert this image into an array of pixel values.
}