JavaScript > Browser APIs > Canvas API > Working with paths
Drawing a Simple Path on Canvas
This snippet demonstrates how to draw a simple path (a triangle) on an HTML5 canvas using JavaScript. We'll cover creating the canvas element, obtaining the 2D rendering context, defining the path using moveTo
and lineTo
, and finally, filling the path with a color.
Creating the Canvas Element
First, we create a canvas element in our HTML. The width
and height
attributes define the dimensions of the canvas. The style
attribute adds a border for better visibility. The text inside the canvas
tag is displayed if the browser doesn't support canvas.
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #d3d3d3;">
Your browser does not support the HTML5 canvas tag.
</canvas>
JavaScript to Draw the Triangle
This JavaScript code retrieves the canvas element by its ID. It then obtains the 2D rendering context using canvas.getContext('2d')
. ctx.beginPath()
starts a new path. ctx.moveTo(20, 20)
moves the starting point of the path to coordinates (20, 20). ctx.lineTo(180, 20)
draws a line from the current point to (180, 20). ctx.lineTo(100, 80)
draws another line to (100, 80). ctx.closePath()
closes the path by drawing a line back to the starting point. Finally, ctx.fillStyle = 'red'
sets the fill color to red, and ctx.fill()
fills the path with the specified color.
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(20, 20);
ctx.lineTo(180, 20);
ctx.lineTo(100, 80);
ctx.closePath();
ctx.fillStyle = "red";
ctx.fill();
Concepts Behind the Snippet
The Canvas API provides a way to draw graphics on a webpage using JavaScript. The key concepts are:
moveTo
, lineTo
, arc
, and bezierCurveTo
are used to define paths.fillStyle
and fill
, or stroked (outlined) using strokeStyle
and stroke
.
Real-Life Use Case
Canvas paths are used in many real-life applications such as:
Best Practices
Here are some best practices for working with Canvas paths:
beginPath()
and closePath()
: Always start a new path with beginPath()
and close it with closePath()
to avoid unexpected connections between paths.
Interview Tip
When asked about Canvas paths in an interview, be prepared to discuss the following:
arc
, bezierCurveTo
, etc.).
When to use them
Use Canvas paths when you need to draw complex shapes or graphics that cannot be easily created with standard HTML elements and CSS. Canvas is particularly useful for interactive drawings, animations, and data visualization.
Memory Footprint
Canvas operations can consume significant memory, especially for large canvases and complex drawings. Be mindful of the memory footprint and optimize your code accordingly. Consider using techniques like caching, reusing paths, and reducing the number of redraws.
Alternatives
Alternatives to Canvas include:
Pros
Advantages of using Canvas paths:
Cons
Disadvantages of using Canvas paths:
FAQ
-
What is the difference between
fill()
andstroke()
?
fill()
fills the inside of a closed path with the currentfillStyle
, whilestroke()
draws a line along the path using the currentstrokeStyle
.fill()
creates a solid shape, whilestroke()
creates an outline of the shape. -
How can I draw a circle on the canvas?
You can draw a circle using the
arc()
method. Thearc()
method takes the center coordinates, radius, start angle, end angle, and direction (clockwise or counter-clockwise) as parameters. -
How do I change the color of the line?
You can change the color of the line using the
strokeStyle
property. For example,ctx.strokeStyle = 'blue'
will set the line color to blue.