JS Canvas Drawing only works sometimes? - javascript

I want to create a Whiteboard application on which the User can draw (I'm using an HTML <canvas>). I just completed coding a function that imports an PNG image and draws it onto the Canvas:
var canvas = document.getElementById('can');
var ctx = canvas.getContext("2d");
var w = canvas.width;
var h = canvas.height;
function drawImageOntoCanvas (e) {
var input = e.target;
var img = new Image();
var reader = new FileReader();
reader.onload = function(){
var dataURL = reader.result;
img.src = dataURL;
ctx.drawImage(img, 10, 10);
};
reader.readAsDataURL(input.files[0]);
}
<input id="uploadFromPC" type="file" accept='image/png' onchange="drawImageOntoCanvas(event);"></input>
<canvas id="can" width="1000px" height="500px" style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas>
This works, but only sometimes for some reason. It doesn't work at first, then, when I reload the page, it works again. Then, it doesn't work, but it does after reloading and so on.
I tried logging the Data URL in the console, as well as the result of the reader. Both work perfectly fine.
Is there something I'm doing wrong here? I'm not very familiar with JS, so it could be that I'm missing something here.
Thanks in advance

The problem is that, depending on timing, you might be drawing the image before it has finished loading.
You can address that as follows, even though I would suggest using a little more modern JavaScript (addEventListener(), const, let, promises, etc.):
var canvas = document.getElementById('can');
var ctx = canvas.getContext("2d");
var w = canvas.width;
var h = canvas.height;
function drawImageOntoCanvas(e) {
var input = e.target;
var img = new Image();
var reader = new FileReader();
reader.onload = function() {
img.src = reader.result;
img.onload = function() {
ctx.drawImage(img, 10, 10);
}
};
reader.readAsDataURL(input.files[0]);
}
<input id="uploadFromPC" type="file" accept='image/png' onchange="drawImageOntoCanvas(event);"></input>
<canvas id="can" width="1000px" height="500px" style="position:absolute;top:10%;left:10%;border:2px solid;"></canvas>

Related

rendering canvas in firefox not working [duplicate]

I try to build a javascript code, to draw a image on canvas, but I don't know where go wrong.
That is my code:
<body>
<canvas id = "my_canvas"></canvas>
<script>
function setup(){
var canvas = document.getElementById('my_canvas');
var ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
var image = new Image();
image.src = 'a.png';
ctx.drawImage(image,5,5);
};
window.onload = setup;
setup();
</script>
The question is, if I put a line of code setup(); at end then the image is correctly draw, I don't know why.
Thanks.
The problem is that image is loaded asynchronously, so if you set the source and draw it immediately then the image bits are not yet available and nothing is drawn.
You have to wait for the image to load before being able to draw it on a canvas.
Changing the code to
image.onload = function() {
ctx.drawImage(image, 5, 5);
};
image.src = "a.png";
should work as expected.
The image is loading asynchronously. This code will work:
JavaScript:
function setup(){
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
var image = new Image();
image.onload = function () {
ctx.drawImage(image,5,5);
};
image.src = 'a.png';
}
window.onload = setup;

javascript draw a image on canvas

I try to build a javascript code, to draw a image on canvas, but I don't know where go wrong.
That is my code:
<body>
<canvas id = "my_canvas"></canvas>
<script>
function setup(){
var canvas = document.getElementById('my_canvas');
var ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
var image = new Image();
image.src = 'a.png';
ctx.drawImage(image,5,5);
};
window.onload = setup;
setup();
</script>
The question is, if I put a line of code setup(); at end then the image is correctly draw, I don't know why.
Thanks.
The problem is that image is loaded asynchronously, so if you set the source and draw it immediately then the image bits are not yet available and nothing is drawn.
You have to wait for the image to load before being able to draw it on a canvas.
Changing the code to
image.onload = function() {
ctx.drawImage(image, 5, 5);
};
image.src = "a.png";
should work as expected.
The image is loading asynchronously. This code will work:
JavaScript:
function setup(){
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
canvas.width = 800;
canvas.height = 600;
var image = new Image();
image.onload = function () {
ctx.drawImage(image,5,5);
};
image.src = 'a.png';
}
window.onload = setup;

Displaying one canvas to another

I have a problem displaying one canvas to another. I do everything according to this solution
<script>
var source = document.getElementById('hiddenCanvas');
var source_ctx = source.getContext('2d');
var img = new Image();
img.onload = function(){
source.width = img.width;
source.height = img.height;
source_ctx.drawImage(img, 0, 0, img.width, img.height);
}
img.src = 'icon.jpg';
var destination = document.getElementById('visibleCanvas');
var destin_ctx = destination.getContext('2d');
destin_ctx.drawImage(source, 0, 0);
</script>
Well, first canvas element displays picture correctly, but whatever I do, the second canvas does not want to display a picture.
<script>
function init()
{
var source = document.getElementById('hiddenCanvas');
var source_ctx = source.getContext('2d');
var destination = document.getElementById('visibleCanvas');
var destin_ctx = destination.getContext('2d');
var img = new Image();
img.onload = function(){
source.width = img.width;
source.height = img.height;
source_ctx.drawImage(img, 0, 0, img.width, img.height);
destin_ctx.drawImage(source, 0, 0);
}
img.src = 'arun.jpg';
}
</script>
</head>
<body onload="init();">
<canvas id="hiddenCanvas" />
<canvas id="visibleCanvas" />
Your code is not working because you are trying to access canvas element before it is loaded to dom
The way you've currently structured the code, img.onload is executed after the destin_ctx.drawImage line. That means that your program flow currently looks something like this:
Image is told to start loading
Destination canvas is drawn using (currently blank) source canvas
Image finishes loading
Image's onload executes, causing the source canvas to be drawn to. The destination canvas is NOT updated, because the destin_ctx.drawImage operation is a one-time copy.
What you need to do is move the destin_ctw.drawImage call to a place in the execution flow where you know the source canvas will definitely contain the appropriate contents. In this simple case, moving it to inside the image's onload would work.
Here's a full (but simplified) HTML file that works for me in Chromium, with a changed image url:
<script>
function load() {
var source = document.getElementById('hiddenCanvas');
var source_ctx = source.getContext('2d');
var destination = document.getElementById('visibleCanvas');
var destin_ctx = destination.getContext('2d');
var img = new Image();
img.onload = function(){
source.width = img.width;
source.height = img.height;
source_ctx.drawImage(img, 0, 0, img.width, img.height);
destin_ctx.drawImage(source, 0, 0);
}
img.src = 'ship360_32.png';
}
</script>
<body onload="load()">
<canvas id="hiddenCanvas"></canvas>
<canvas id="visibleCanvas"></canvas>
</body>
You are trying to draw the image from source before the image is loaded and the source even have the image.
Move the last draw operation inside the onload handler. Also remember to set the size for destination canvas:
var source = document.getElementById('hiddenCanvas');
var source_ctx = source.getContext('2d');
var destination = document.getElementById('visibleCanvas');
var destin_ctx = destination.getContext('2d');
var img = new Image();
img.onload = function(){
source.width = img.width;
source.height = img.height;
source_ctx.drawImage(img, 0, 0, img.width, img.height);
destination.width = source.width;
destination.height = source.height;
destin_ctx.drawImage(source, 0, 0);
}
img.src = 'icon.jpg';

html5 image is not loaded into canvas

I am trying to load a picture into HTML5 Canvas.
when i use a URL to load the image everything works fine, but if i put the image on the local drive and point to it, nothing happens.
note: when i use a regular tag, everything works fine and the image is loaded.
here is the code:
var canvas = document.getElementById("rightSide");
var context = canvas.getContext("2d");
var imageObj = new Image();
imageObj.src = "cloud.gif";
context.drawImage(imageObj, 650, 55, 93, 104);
<
canvas id="rightSide" width="800px" height="800">
thanks.
Try something like this.
<canvas id="canvas"></canvas>
var can = document.getElementById('canvas');
var ctx = can.getContext('2d');
var img = new Image();
img.onload = function(){
can.width = img.width;
can.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
}
img.src = 'image.jpg';
A local file being loaded into canvas is treated as being from a different source and therefore "tainted". This is why it's not working for your local file, but does for a URL.

Why is the image drawn in the canvas when debugging but not when running?

I'm learning HTML5 and Javascript and I'm trying to draw an image on a canvas. I have the following code which draws the image if I step through the code after breaking on the line marked below. If I don't debug then the image is not drawn at all. What am I doing wrong? Firefox 10 with FireBug 1.9.
Note that although there's a loop to handle multiple images, I've only been selecting one. I figure if one doesn't work, a hundred won't work either. ;-)
<!DOCTYPE html>
<html>
<body>
<input type="file" id="files" name="files[]" multiple />
<canvas id="picCanvas" />
<script>
function handleFileSelect(evt) {
var files = evt.target.files;
// Loop through the FileList and render images
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) {
var img = document.createElement('img'); // <-- BREAK HERE!
img.src = e.target.result;
var canvas = document.getElementById('picCanvas');
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
</html>
You have to wait until the image element is fully loaded by the browser before calling the drawImage method, even if your image element is created from a base64 string and not from an external file. So just make use of the onload event of the image object. A quick fix would be like this:
var img = document.createElement('img');
img.onload = function()
{
var canvas = document.getElementById('picCanvas');
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0);
}
img.src = e.target.result;

Categories

Resources