Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

HTML Canvas Fill and Stroke


Canvas Fill and Stroke

To define fill-color and outline-color for shapes/objects in canvas, we use the following properties:

Property Description
fillStyle Defines the fill-color of the object/shape
strokeStyle Defines the color of the outline of the object/shape

The fillStyle Property

The fillStyle property defines the fill-color of the object.

The fillStyle property value can be a color (colorname, RGB, HEX, HSL), a gradient or a pattern.

Example

Sorry, your browser does not support canvas.

Set the fill-color to "green" and draw a filled rectangle with the fillRect() method:

<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.fillStyle = "green";
ctx.fillRect(10,10, 100,100);
</script>
Try it Yourself »


The strokeStyle Property

The strokeStyle property defines the color of the outline.

The strokeStyle property value can be a color (colorname, RGB, HEX, HSL), a gradient or a pattern.

Example

Sorry, your browser does not support canvas.

Set the outline-color to "blue" and draw an outlined rectangle with the strokeRect() method:

<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

ctx.strokeStyle = "blue";
ctx.lineWidth = 5;
ctx.strokeRect(10,10, 100,100);
</script>
Try it Yourself »

Combining fillStyle and strokeStyle

It is perfectly legal to combine the two rectangles above.

Example

Sorry, your browser does not support canvas.
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// the filled rectangle
ctx.fillStyle = "green";
ctx.fillRect(10,10, 100,100);

// the outline rectangle
ctx.strokeStyle = "blue";
ctx.lineWidth = 5;
ctx.strokeRect(10,10, 100,100);
</script>
Try it Yourself »

fillStyle and strokeStyle with Alpha Channel

You can also add an alpha channel to both the fillStyle and the strokeStyle properties to create opacity.

Example

Sorry, your browser does not support canvas.

Set the opacity to 50% for both the fillStyle and strokeStyle properties:

<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");

// the filled rectangle
ctx.fillStyle = "rgb(0 255 0 / 50%)";
ctx.fillRect(10,10, 100,100);

// the outline rectangle
ctx.strokeStyle = "rgb(0 0 255 / 50%)";
ctx.lineWidth = 5;
ctx.strokeRect(10,10, 100,100);
</script>
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.