HTML canvas strokeStyle Property

HTMLWeb DevelopmentFront End Technology

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 −


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 −


 Live Demo

<!DOCTYPE html>
<canvas id="newCanvas" width="550" height="400" style="border −2px solid orange;"></canvas>
   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);


Updated on 30-Jul-2019 22:30:26