Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 canvas - drawImage not always drawing the image

I am drawing on the canvas each time a user presses a button, however sometimes the image is not getting drawn on the canvas. I think this could be that the image isn't loaded in time before the context.drawimage function runs, as some of the smaller files sometimes get drawn. I've used the console and checked resources and so this is the only problem I can think of.

How do I avoid this problem?

This is my Javascript code.

var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");

var questionbg = new Image();
var answerbg = new Image();

//this code is inside a function that is called each time a user presses a button
if(questiontype == "text"){
    questionbg.src = "./resources/textquestionbg.png";
    context.drawImage(questionbg, 0, 0);
    }
//if image question
else if(questiontype == "image"){
questionbg.src = "./resources/imageaudiovideoquestionbg.png";
context.drawImage(questionbg, 0, 0);
}
//if audio question
else if(questiontype == "audio"){
questionbg.src = "./resources/imageaudiovideoquestionbg.png";
context.drawImage(questionbg, 0, 0);
}
//else it is a video question
else{
questionbg.src = "./resources/imageaudiovideoquestionbg.png";
context.drawImage(questionbg, 0, 0);
}
like image 662
user2177629 Avatar asked Mar 19 '13 15:03

user2177629


People also ask

What is the use of drawimage in HTML5?

The HTML5 drawImage () method is used to draw an image, canvas or video on the canvas. It also draws parts of an image. You can also use it to increase or decrease image size. To specify the image, canvas, or video.

Why is context drawimage not displaying the image on the canvas?

html5-canvas Images Is "context.drawImage" not displaying the image on the Canvas? Make sure your image object is fully loaded before you try to draw it on the canvas with context.drawImage. Otherwise the image will silently fail to display. In JavaScript, images are not loaded immediately.

How to draw an image on a canvas in JavaScript?

Position the image on the canvas: JavaScript syntax: context .drawImage ( img,x,y ); Position the image on the canvas, and specify width and height of the image: JavaScript syntax: context .drawImage ( img,x,y,width,height ); Clip the image and position the clipped part on the canvas: JavaScript syntax:

What does the method drawimage () do in Java?

The drawImage () method draws an image, canvas, or video onto the canvas. The drawImage () method can also draw parts of an image, and/or increase/reduce the image size.


1 Answers

You should check if the image is loaded. If not then listen to the load event.

questionbg.src = "./resources/imageaudiovideoquestionbg.png";
if (questionbg.complete) {
    context.drawImage(questionbg, 0, 0);
} else {
    questionbg.onload = function () {
        context.drawImage(questionbg, 0, 0);    
    };
}


MDN (Mozilla Doc, great source btw) suggests:
function draw() {
  var ctx = document.getElementById('canvas').getContext('2d');
  var img = new Image();
  img.onload = function(){
    ctx.drawImage(img,0,0);
    ctx.beginPath();
    ctx.moveTo(30,96);
    ctx.lineTo(70,66);
    ctx.lineTo(103,76);
    ctx.lineTo(170,15);
    ctx.stroke();
  };
  img.src = '/files/4531/backdrop.png';
}

Obviously, you are not wanting to apply the stroke or fill. However, the idea is the same.

like image 121
Joe Avatar answered Oct 07 '22 18:10

Joe