On button click I just want to put element in the canvas, but it's not working. Here is the code I have written:
<script language="javascript" type="text/javascript"> function image() { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.clear(); ctx.setTransform(-1, 0, 0, 1, 200, 200); context.clear(true) var imgobj=new Image(); imgobj.onload=function() { ctx.drawImage(imgobj,69,50); } imgobj.src='images.jpg'; } function circle() { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.clear(); // do some more drawing ctx.setTransform(-1, 0, 0, 1, 200, 200); // do some drawing with the new transform context.clear(true); ctx.beginPath(); ctx.arc(95,50,40,0,2*Math.PI); ctx.stroke(); } function line() { var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.clear(); // do some more drawing ctx.setTransform(-1, 0, 0, 1, 200, 200); // do some drawing with the new transform context.clear(true); ctx.moveTo(0,0); ctx.lineTo(200,100); ctx.stroke(); </script> </head> <body> <h1>Demo of HTM5</h1> <div class="canvas1" align="left"> <canvas id="myCanvas" width="300" height="300" style="border:1px solid #000000;"> </canvas> <p><button onclick="circle()"> Draw Circle</button></p> <p><button onclick="line()"> Draw Line</button></p> <p><button onclick="image()"> Display Image</button></p> </div> </body> </html>
onclick=lines. You're looking for something more like:onclick=function() { circle(); }.