Define a red fill-color for the rectangle:
<!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.fillStyle = "#FF0000"; ctx.fillRect(20, 20, 150, 100);//from w w w. j a v a2s.c o m </script> </body> </html>
The fillStyle property sets or gets the color, gradient, or pattern used to fill the drawing.
Default value: #000000
context.fillStyle = color|gradient|pattern;
Property Values
Value | Description |
---|---|
color | A CSS color value. Default value is #000000 |
gradient | gradient object, linear or radial, used to fill the drawing |
pattern | pattern object to use to fill the drawing |