-3

i want to add image using javascript but it is not working

html code:

<body onload="init()">
    <canvas id="canvas" width="640" height="480">
        <p>your browser doesnot support html5 canvas.</p>
    </canvas>
    <script src="script.js">
    </script>
</body>

and this is javascript:

var canvas;
var context;

function init() {
    canvas = document.getElementById("canvas")
    context = canvas.getContext("2d");
    console.log("loading comepletd");
    var image = new Image();
    image.src = "ball.png";
}
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
suzan
  • 1
  • 5

1 Answers1

0

Try this

var canvas;
var context;

function init() {
    canvas = document.getElementById("canvas");
    context = canvas.getContext("2d");
    console.log("loading comepletd");
    var image = new Image();
    image.src = "ball.png";
    image.onload = function(){
        context.drawImage(image, 100, 100);
    }
}
KANAYO AUGUSTIN UG
  • 2,078
  • 3
  • 17
  • 31