10
The HTML5 <canvas> element allows you to use colors in various ways to fill shapes, outline paths, and add visual appeal to your graphics. In this tutorial, you’ll learn how to:
- Apply solid colors for fills and strokes.
- Use gradients for dynamic coloring.
- Create patterns using images or existing canvas content.
- Combine color techniques for advanced effects.
1. Setting Colors
The Canvas API uses two primary properties for colors:
- fillStyle: Sets the fill color for shapes.
- strokeStyle: Sets the color of the outlines (strokes).
Example 1: Solid Colors for Fill and Stroke
<canvas id="myCanvas" width="500" height="300" style="border: 1px solid black;"></canvas> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); // Fill with a solid color ctx.fillStyle = "blue"; ctx.fillRect(50, 50, 150, 100); // Outline with a solid color ctx.strokeStyle = "red"; ctx.lineWidth = 5; ctx.strokeRect(250, 50, 150, 100); </script>
2. Using RGBA for Transparency
The RGBA color model adds an alpha channel for transparency.
Example 2: Applying RGBA Colors
// Transparent fill ctx.fillStyle = "rgba(0, 255, 0, 0.5)"; // Green with 50% opacity ctx.fillRect(50, 200, 150, 100); // Transparent stroke ctx.strokeStyle = "rgba(255, 0, 0, 0.8)"; // Red with 80% opacity ctx.lineWidth = 4; ctx.strokeRect(250, 200, 150, 100);
3. Linear Gradients
You can create smooth transitions between colors using linear gradients.
Syntax:
const gradient = ctx.createLinearGradient(x0, y0, x1, y1); gradient.addColorStop(position, color);
Example 3: Linear Gradient Fill
const gradient = ctx.createLinearGradient(0, 0, 500, 0); gradient.addColorStop(0, "blue"); gradient.addColorStop(0.5, "green"); gradient.addColorStop(1, "yellow"); ctx.fillStyle = gradient; ctx.fillRect(50, 50, 400, 200);
Explanation:
- createLinearGradient(x0, y0, x1, y1): Creates a gradient from (x0, y0) to (x1, y1).
- addColorStop(position, color): Adds a color stop at the given position (0 to 1).
4. Radial Gradients
Radial gradients create color transitions radiating from a central point.
Syntax:
const gradient = ctx.createRadialGradient(x0, y0, r0, x1, y1, r1); gradient.addColorStop(position, color);
Example 4: Radial Gradient Fill
const gradient = ctx.createRadialGradient(250, 150, 50, 250, 150, 150); gradient.addColorStop(0, "red"); gradient.addColorStop(1, "purple"); ctx.fillStyle = gradient; ctx.fillRect(50, 50, 400, 200);
Explanation:
- createRadialGradient(x0, y0, r0, x1, y1, r1):
- (x0, y0, r0): Defines the inner circle.
- (x1, y1, r1): Defines the outer circle.
5. Patterns
You can use images or existing canvas content to create repeating patterns.
Syntax:
const pattern = ctx.createPattern(image, repeat); ctx.fillStyle = pattern;
Example 5: Using an Image Pattern
const image = new Image(); image.src = "https://via.placeholder.com/50"; // Use any image URL image.onload = () => { const pattern = ctx.createPattern(image, "repeat"); // Options: repeat, repeat-x, repeat-y, no-repeat ctx.fillStyle = pattern; ctx.fillRect(50, 50, 400, 200); };
6. Combining Colors
You can combine solid colors, gradients, and patterns to create complex effects.
Example 6: Combining Linear Gradient and Stroke
// Linear Gradient Fill const gradient = ctx.createLinearGradient(0, 0, 500, 0); gradient.addColorStop(0, "blue"); gradient.addColorStop(1, "cyan"); ctx.fillStyle = gradient; ctx.fillRect(50, 50, 400, 200); // Stroked Outline ctx.strokeStyle = "red"; ctx.lineWidth = 10; ctx.strokeRect(50, 50, 400, 200);
7. Practical Example: Sunset Scene
Example: Creating a Sunset Scene with Colors
// Sky (Linear Gradient) const skyGradient = ctx.createLinearGradient(0, 0, 0, 300); skyGradient.addColorStop(0, "orange"); skyGradient.addColorStop(1, "blue"); ctx.fillStyle = skyGradient; ctx.fillRect(0, 0, 500, 300); // Sun (Radial Gradient) const sunGradient = ctx.createRadialGradient(250, 150, 20, 250, 150, 100); sunGradient.addColorStop(0, "yellow"); sunGradient.addColorStop(1, "transparent"); ctx.fillStyle = sunGradient; ctx.beginPath(); ctx.arc(250, 150, 100, 0, Math.PI * 2); ctx.fill(); // Ocean ctx.fillStyle = "darkblue"; ctx.fillRect(0, 250, 500, 50);
8. Best Practices
- Reset Colors as Needed:
- fillStyle and strokeStyle persist until changed.
- Use Gradients for Smooth Transitions:
- Avoid overusing solid colors for large areas.
- Combine Techniques:
- Mix solid colors, gradients, and patterns for complex visuals.
- Optimize Patterns:
- Use optimized or small images for patterns to improve performance.
9. Browser Support
Feature | Supported? |
---|---|
Solid Colors | Yes |
RGBA Colors | Yes |
Gradients | Yes |
Patterns | Yes |
10. Conclusion
In this tutorial, you learned:
- How to set colors using fillStyle and strokeStyle.
- How to create and apply linear and radial gradients.
- How to use patterns for fills.
- How to combine multiple color techniques for advanced effects.