I noticed that when I was trying to show an image on canvas with the help of HTML tags(i.e input tag and type is file) the codebase wasn’t showing an image and when I was trying to show with the help of p5.js function createFileInput(readImageURL);
then it is going to show.
This is the sample code snippet that demonstrates the problem and their alternative solution.
When I was trying to do like the following…
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/addons/p5.sound.min.js"></script>
<meta charset="utf-8" />
</head>
<body>
Upload image: <input type="file" name="image" onchange="readImageURL(this);" >
<script type="text/javascript" charset="utf-8">
let img;
function setup() {
var canvas = createCanvas(400, 400);
}
function draw() {
background(255);
if (img) {
image(img, 0, 0);
}
}
function readImageURL(file) {
print(file);
if (file.type === 'image') {
img = createImg(file.data, ' ');
img.hide();
} else {
img = null;
}
</script>
</body>
</html>
it wasn’t going to show an image on the canvas.
and then when I was tried to show an image like the following…
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.10.2/addons/p5.sound.min.js"></script>
<meta charset="utf-8" />
</head>
<body>
<script type="text/javascript" charset="utf-8">
let img;
let input;
function setup() {
var canvas = createCanvas(400, 400);
input = createFileInput(readImageURL);
input.position(0,0);
}
function draw() {
background(255);
if (img) {
image(img, 0, 0);
}
}
function readImageURL(file) {
print(file);
if (file.type === 'image') {
img = createImg(file.data, ' ');
img.hide();
} else {
img = null;
}
</script>
</body>
</html>
It was going to show a image on canvas. I’m not understanding why it’s going to happen.