/

HTML / CSS / JavaScript Tutorial

JavaScript canvas2d object method: fill()

[this page | pdf | back links]

The fill() method of the JavaScript DOM object returned by the getContext("2d") method applied to the HTML <canvas> element fills the current path. If the path is not closed then this method will add a line from the last point to the start point of the path, like closePath().

 

It has the following syntax with no parameters.

 

context.fill()

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
<span id="element"></span>

<script>
var x2 = document.createElement("CANVAS");
x2.setAttribute("width", "200");
x2.setAttribute("height", "100");
x2.setAttribute("style", "border: 1px solid black");
var c2 = x2.getContext("2d");

c2.strokeStyle = "blue";
c2.beginPath();
c2.moveTo(30, 10);
c2.lineTo(100, 90);
c2.lineTo(170, 10);
c2.fillStyle = "orange";
c2.fill();
c2.stroke();

document.getElementById("element").appendChild(x2);
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodCanvas2dFill() {
  var x = document.createElement("CANVAS"); var c = x.getContext("2d"); return !!c.fill;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile