Canvas How to - Draw line








Question

We would like to know how to draw line.

Answer


<!--   ww w. j  a  v  a  2  s  . c o  m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.moveTo(0,0);
    ctx.lineTo(980,0);
    ctx.stroke();
}//]]>  
</script>
</head>
<body>
  <canvas id="myCanvas" width="980" height="100" ></canvas>
</body>
</html>

The code above is rendered as follows: