Change the stroke style in JavaScript

Description

The following code shows how to change the stroke style.

Example


<!-- ww w. j  av a  2s .  c o  m-->
<!DOCTYPE HTML>
<html>
<head>
</head>
<body>
<canvas id="canvas" width="500" height="500"></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

The code above generates the following result.

Change the stroke style in JavaScript
Home »
  Javascript Tutorial »
    Canvas »
      Canvas Draw
Javascript Tutorial Canvas Draw
Change the stroke style in JavaScript
Clip a region in JavaScript
Control line width in JavaScript
Create Radial Gradient on HTML5 canvas in J...
Create a rainbow radial gradient in JavaScr...
Create linear gradient in JavaScript
Draw an image on HTML5 Canvas in JavaScript
Draw transparent shapes on HTML5 Canvas in ...
Get canvas context in JavaScript
Restore a drawing state in JavaScript
Save and restore Canvas states with button ...
Set color to transparency when drawing on c...
Set shadow color and blur in JavaScript
Use image as the fill pattern in JavaScript