Stoke style

In this chapter you will learn:

  1. How to change the stroke style

Setting the Fill and Stroke Styles

When we set a style using the fillStyle or strokeStyle properties, we can specify a color using the CSS color values, using either a name or a color model.

<!DOCTYPE HTML><!--  j av a  2  s.c o  m-->
<html>
<head>
<style>
canvas {
      border: thin solid black;
      margin: 4px
}
</style>
</head>
<body>
      <canvas id="canvas" width="500" height="500"> 
      Your browser doesn't support the <code>canvas</code> element 
      </canvas>
      <script>
            var ctx = document.getElementById("canvas").getContext("2d");
            var offset = 10;
            var size = 50;
            var count = 3;
            ctx.lineWidth = 3;
            var fillColors = [ "black",  "red", "blue" ];
            var strokeColors = [ "rgb(20,20,20)", "rgb(255, 20, 20)", "rgb(0, 0, 255)" ];
            for ( var i = 0; i < count; i++) {
                  ctx.fillStyle = fillColors[i];
                  ctx.strokeStyle = strokeColors[i];
                  ctx.fillRect(i * (offset + size) + offset, offset, size, size);
                  ctx.strokeRect(i * (offset + size) + offset, (2 * offset) + size,size, size);
            }
      </script>
</body>
</html>

Click to view the demo

Home » Javascript Tutorial » Canvas
Canvas tag
Canvas Context
Rectangle
Rectangle clear
Drawing State
Drawing state restore
Line width
Line Join Style
Line cap settings
Gradients
Linear Gradient
Radial Gradient
Path
Arcs
Two types of curve
Quadratic Bezier Curves
Cubic Bezier Curves
Clip
Text
Shadow
Transparency
Stoke style