-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
  • 135,913
  • 89
  • 401
  • 325
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
  • 1,877
  • 1
  • 14
  • 27