I have been trying to implement a load canvas function using the drag and drop method explained in the P5 reference below. However on the first instance of dragging an image to the canvas, the image will not load. The second attempt will load the image fine. This happens with every 'new' image that I try to load to the canvas.
https://p5js.org/examples/dom-drop.html
When commenting out the '.hide' you can see that the image data is successfully loading on the first attempt. I am a little confused as to how I correct this issue.
Thank you to anyone that can point me in the right direction.
index.html
<!DOCTYPE html>
<html>
<head>
<script src="p5.js"></script>
<script src="p5.dom.js"></script>
<script src="sketch.js"></script>
</head>
<body>
</body>
</html>
sketch.js
function setup() {
// create canvas
var c = createCanvas(710, 400);
background(100);
// Add an event for when a file is dropped onto the canvas
c.drop(gotFile);
}
function draw() {
fill(255);
noStroke();
textSize(24);
textAlign(CENTER);
text('Drag an image file onto the canvas.', width/2, height/2);
noLoop();
}
function gotFile(file) {
// If it's an image file
if (file.type === 'image') {
// Create an image DOM element but don't show it
var img = createImg(file.data).hide();
// Draw the image onto the canvas
image(img, 0, 0, width, height);
} else {
println('Not an image file!');
}
}