Languages
[Edit]
EN

JavaScript - fillStyle vs strokeStyle

0 points
Created by:
Kevin
797

In this article, we would like to show you the difference between fillStyle and strokeStyle in JavaScript.

Quick solution:

1. strokeStyle is used for setting the shape outline color,

fillStyle is used for setting the fill color.

Note:

The properties can be set to a string representing a CSS color value, a gradient or a pattern. By default, the strokeStyle and fillStyle colors are set to black.

 

Practical example

In this section, we present a practical example that shows the difference between strokeStyle and fillStyle properties.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    #my-canvas { border: 1px solid gray; }

  </style>
</head>
<body>
  <canvas id="my-canvas" width="200" height="200"></canvas>
  <script>

    var canvas = document.querySelector('#my-canvas');
    var context = canvas.getContext('2d');

    context.lineWidth = 10;           // sets shape outline width (in px)

    // strokeStyle
    context.strokeStyle = 'green';    // sets shape outline color

    // fillStyle
    context.fillStyle = 'red';    	  // sets shape fill color

    // drawing
    context.beginPath();
    context.rect(50, 50, 100, 100);   // square shape
    context.fill();					  // fills the shape with fillStyle color
    context.stroke();

  </script>
</body>
</html>

See also

  1. JavaScript - draw square on canvas element

  2. JavaScript - set line width (thickness) on canvas element

References

  1. fillStyle - Web APIs | MDN
  2. strokeStyle - Web APIs | MDN

Alternative titles

  1. JavaScript - fillStyle vs strokeStyle (HTML canvas element)
  2. JavaScript - fillStyle and strokeStyle differences
  3. JavaScript - difference between fillStyle and strokeStyle
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - HTML5 canvas tutorial

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join