Setting the Fill & Stroke Styles
When we set a style using the fillStyle or strokeStyle properties, we can specify a color using the CSS color values, using either a name or a color model.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
canvas {
border: thin solid black;
margin: 4px
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500">
Your browser doesn't support the <code>canvas</code> element
</canvas>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
var offset = 10;
var size = 50;
var count = 3;
ctx.lineWidth = 3;
var fillColors = [ "black", "red", "blue" ];
var strokeColors = [ "rgb(20,20,20)", "rgb(255, 20, 20)", "rgb(0, 0, 255)" ];
for ( var i = 0; i < count; i++) {
ctx.fillStyle = fillColors[i];
ctx.strokeStyle = strokeColors[i];
ctx.fillRect(i * (offset + size) + offset, offset, size, size);
ctx.strokeRect(i * (offset + size) + offset, (2 * offset) + size,size, size);
}
</script>
</body>
</html>
Home
HTML CSS Book
HTML
HTML CSS Book
HTML
canvas:
- Getting Started with the Canvas Element
- Getting a Canvas Context
- Drawing Rectangles
- Canvas Drawing State
- Setting the Line Join Style
- Using Gradients
- Using Patterns
- Using smaller shapes with an image pattern
- Drawing Images
- Using Video Images
- Using Canvas Images
- Setting the Fill & Stroke Styles
- Saving and Restoring Drawing State
- Drawing Using Paths
- Drawing Arcs
- Drawing Bezier Curves
- Drawing Text
- Using Shadows
- Using Transparency
Related: