-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"; } 
15
  • 2
    What exactly are you expecting to happen? You're creating an instance of an image but you aren't doing anything with it. Commented Aug 29, 2016 at 14:41
  • you forgot a semi-colon after the getElementById() And you can you the JS console to debug your code ;) (F12) Commented Aug 29, 2016 at 14:42
  • i want to show this image in my browser. That's all i want to do! Commented Aug 29, 2016 at 14:43
  • 2
    The phrase: "Not working" is going to invite downvotes. We are not mind-readers. Tell us what you expected to happen and what actually happened. Commented Aug 29, 2016 at 14:43
  • 1
    @Pierre Technically the semicolon isn't required but it's definitely a good idea to be consistent. Commented Aug 29, 2016 at 14:43

1 Answer 1

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); } } 
Sign up to request clarification or add additional context in comments.

9 Comments

Please delete your answer instead of copying the code from the link I posted
@kanayo austin kane this worked for me!
@suzan. Am happy it worked. Please mark it as answered and don't forget to upvote
i am new at stackoverflow so i don't know so much about marking etc,etc
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.