Tutorialspoint

1 Answer
George John

The strokeStyle property in HTML canvas is used to set the color, gradient or pattern for the stroke. The <canvas> element allows you to draw graphics on a web page using JavaScript. Every canvas has two elements that describes the height and width of the canvas i.e. height and width respectively.

Following is the syntax −

context.strokeStyle=color|gradient|pattern;

Above, the values, include −

  • color − The stroke color of the drawing.
  • gradient − Linear or radial gradient object to create gradient stroke
  • pattern − The pattern object to create pattern stroke.

Let us now see an example to implement the strokeStyle property of canvas −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<canvas id="newCanvas" width="550" height="400" style="border −2px solid orange;"></canvas>
<script>
   var c = document.getElementById("newCanvas");
   var ctx = c.getContext("2d");
   var grad = ctx.createLinearGradient(150, 0, 280, 0);
   grad.addColorStop("0", "orange");
   grad.addColorStop("0.5", "blue");
   grad.addColorStop("1.0", "green");
   ctx.strokeStyle = grad;
   ctx.lineWidth = 20;
   ctx.strokeRect(150, 120, 240, 100);
</script>
</body>
</html>

Output

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.