Colors and Transparency

By default, all shapes drawn on the canvas are solid black. You can use the fillStyle, strokeStyle, and globalAlpha properties to customize the appearance of your drawings.

Colors

To set the color for shapes or strokes, use the fillStyle and strokeStyle properties:

Property Description
fillStyle Sets the color (or gradient, or pattern) used for filling shapes.
strokeStyle Sets the color (or gradient, or pattern) used for shape outlines.

These properties accept any valid CSS color value, including:

Transparency

There are two ways to draw transparent shapes on the canvas:

Color and Transparency Example

This example demonstrates how to apply various colors and transparency levels to rectangles:

View Output

The next lesson explains how to implement gradients.