HTML canvas fill() Method

Introduction

Draw a 150*100 pixels rectangle, and fill it with the color red:

View in separate window

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="300" height="180" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.</canvas>

<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.rect(20, 20, 150, 100);//from www . j a  va2s. c o m
ctx.fillStyle = "red";
ctx.fill();
</script>

</body>
</html>

The fill() method fills the current drawing (path). The default color is black.

Use the fillStyle property to fill with another color/gradient.

If the path is not closed, the fill() method will add a line from the last point to the starting point of the path to close the path, and then fill the path.

context.fill();



PreviousNext

Related