EN
JavaScript - fillStyle vs strokeStyle
0 points
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,
2 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
andfillStyle
colors are set to black.
In this section, we present a practical example that shows the difference between strokeStyle
and fillStyle
properties.
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
#my-canvas { border: 1px solid gray; }
7
8
</style>
9
</head>
10
<body>
11
<canvas id="my-canvas" width="200" height="200"></canvas>
12
<script>
13
14
var canvas = document.querySelector('#my-canvas');
15
var context = canvas.getContext('2d');
16
17
context.lineWidth = 10; // sets shape outline width (in px)
18
19
// strokeStyle
20
context.strokeStyle = 'green'; // sets shape outline color
21
22
// fillStyle
23
context.fillStyle = 'red'; // sets shape fill color
24
25
// drawing
26
context.beginPath();
27
context.rect(50, 50, 100, 100); // square shape
28
context.fill(); // fills the shape with fillStyle color
29
context.stroke();
30
31
</script>
32
</body>
33
</html>