Trusted answers to developer questions

How to fill paths in HTML Canvas

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

Overview

We can use the fill method to paint or fill the content area defined by path(s).

For example, we can use fill() to create paths and fill in the content area.

Example

const context = canvas.getContext("2d");
    context.beginPath()
     context.moveTo(0, 50)
     context.lineTo(50, 0)
     context.lineTo(100, 50)
     context.lineTo(0, 50)
     context.fill()
     context.closePath()

This will fill the content with the color black:

Note: Please refer to the following shots for information related to this topic:
-Drawing lines and paths
-Drawing triangles
-Drawing rectangles
-Drawing circles and arcs

RELATED TAGS

javascript
Did you find this helpful?