Canvas How to - Set stoke style with hex color value








Question

We would like to know how to set stoke style with hex color value.

Answer


<!--   www  .  j a va2s .  co m-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
    var context = canvas.getContext("2d");
    context.strokeStyle = '#000000';
    context.fillStyle = '#000000';
    context.moveTo(10, 10);
    context.lineTo(50, 10);
    context.save();
    context.lineWidth = 15;
    context.moveTo(10, 30);
    context.lineTo(50, 30);
    context.restore();
    context.moveTo(10, 50);
    context.lineTo(50, 50);
    context.stroke();
}//]]>  
</script>
</head>
<body>
      <canvas id="canvas" height="400" width="600">
      </canvas>
</body>
</html>

The code above is rendered as follows: