Drawing Basic Shapes: Rectangles, Lines, and Paths
Master the fundamental drawing methods for creating rectangles, lines, and custom paths using moveTo, lineTo, and other path-related functions.
JavaScript Canvas Essentials: Drawing Lines
Drawing Basic Shapes: Lines
The HTML5 Canvas element provides a powerful way to draw graphics dynamically using JavaScript. One of the fundamental shapes you can draw is a line. This section will guide you through the basics of drawing lines on the canvas.
Learn How to Draw Lines Using JavaScript Canvas
To draw a line on a canvas, you'll need to use a series of methods on the 2D rendering context. The key steps are:
- Get the Canvas Element: First, you need to get a reference to your HTML canvas element using JavaScript.
- Get the 2D Rendering Context: The 2D rendering context provides the drawing functions you'll use.
- Begin a Path: Use
context.beginPath()
to start a new drawing path. This isolates your line from any previously drawn shapes. - Move to the Starting Point: Use
context.moveTo(x, y)
to specify the starting coordinates of your line.x
andy
represent the horizontal and vertical positions, respectively. - Draw the Line: Use
context.lineTo(x, y)
to specify the ending coordinates of your line. This draws a line from the starting point (defined by `moveTo`) to the new `x` and `y` coordinates. You can call `lineTo` multiple times to create connected line segments. - Stroke the Path: Use
context.stroke()
to actually render the line onto the canvas. This uses the current stroke style (color, width, etc.) to draw the line.
Controlling Line Appearance
You can customize the appearance of your lines by modifying properties of the 2D rendering context:
- Line Width: Use
context.lineWidth = width;
to set the thickness of the line.width
is a number representing the line width in pixels. The default is 1. - Line Color: Use
context.strokeStyle = color;
to set the color of the line.color
can be a CSS color value (e.g., "red", "#FF0000", "rgb(255, 0, 0)"). The default is black. - Line End Points (Line Cap): Use
context.lineCap = type;
to control how the ends of the line are drawn.type
can be:"butt"
(default): The line ends squarely at the endpoint."round"
: The line end is rounded."square"
: The line end is squared off, extending beyond the endpoint by half the line width.
- Line Joins (Line Join): Use
context.lineJoin = type;
to control how lines are joined together. `type` can be:"round"
: the corners of the lines are rounded."bevel"
: the corners of the lines are flattened."miter"
: the corners of the lines are sharp.
Example Code
Here's a basic example demonstrating how to draw a red line with a width of 5 pixels on a canvas:
<canvas id="myCanvas" width="200" height="100"></canvas> <script> const canvas = document.getElementById('myCanvas');
const context = canvas.getContext('2d');
context.beginPath();
context.moveTo(20, 20);
context.lineTo(180, 80);
context.lineWidth = 5;
context.strokeStyle = 'red';
context.stroke();
</script>
This code snippet will draw a red line from the point (20, 20) to the point (180, 80) with a line width of 5 pixels.
Putting It All Together
Let's create a simple canvas element and draw a few lines with different styles: