drawLine draws a straight line from one point to another, with smooth antialiased edges.
Use it for rules, arrows, charts and annotations; for a line with several segments, use drawPolygon with closed: false.
Signature
image.drawLine(options: { x1: number; y1: number; x2: number; y2: number; color: Paint; width?: number; cap?: "butt" | "round" | "square";}): thisdrawLine draws on the image in place and returns it, so you can chain more calls after it.
Paint is a color or a gradient; see Colors and gradients. The options type is exported from jimp as DrawLineOptions.
Options
| Option | Type | Default | Description |
|---|---|---|---|
x1 |
number |
required | The x of the start of the line, in pixels from the left. |
y1 |
number |
required | The y of the start of the line, in pixels from the top. |
x2 |
number |
required | The x of the end of the line. |
y2 |
number |
required | The y of the end of the line. |
color |
Paint |
required | The color of the line: a 0xRRGGBBAA number, a CSS color, { r, g, b, a } or a gradient. |
width |
number |
1 |
The width of the line in pixels, centered on the line between the two points. Must be 0 or more. |
cap |
"butt" | "round" | "square" |
"butt" |
How the ends look. "butt" stops at the end points, "round" adds a half circle and "square" adds half the width. |
Example
import { Jimp } from "jimp";
const image = new Jimp({ width: 200, height: 100, color: "white" });
image .drawLine({ x1: 10, y1: 10, x2: 190, y2: 90, color: "#e63946", width: 4 }) .drawLine({ x1: 10, y1: 90, x2: 190, y2: 10, color: 0x1d3557ff, width: 8, cap: "round" });
await image.write("lines.png");Crisp 1-pixel lines
A line is centered on the points you give it. Pixel (x, y) covers the square from (x, y) to (x + 1, y + 1), so a 1-pixel line along y: 50 covers half of row 49 and half of row 50, and both come out half-strength gray.
Add 0.5 to put the line through the middle of a row and cover it fully:
// Two gray rowsimage.drawLine({ x1: 0, y1: 50, x2: 200, y2: 50, color: "black" });
// One black rowimage.drawLine({ x1: 0, y1: 50.5, x2: 200, y2: 50.5, color: "black" });The same goes for any odd width. Even widths, such as 2, are crisp on whole-pixel coordinates.
A gradient line
color can be a gradient. Its coordinates are in image pixels, so give it the same start and end as the line to run from one end to the other:
image.drawLine({ x1: 20, y1: 50, x2: 180, y2: 50, width: 12, cap: "round", color: { type: "linear", x1: 20, y1: 50, x2: 180, y2: 50, stops: [ { offset: 0, color: "#ff4b4b" }, { offset: 1, color: "#3a7bff" }, ], },});Try it
Pick each end of the line on the image, then change the width, cap and color.
image.drawLine({Draw an antialiased line between two points. Pick each end on the image.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- Drawn over the image. The line is blended over the pixels below it with the same source-over maths as
composite, so a semi-transparent color, or the antialiased edge of the line, lets the image show through. See Compositing. - Clipped to the image. Points can be anywhere, including outside the image or at fractional coordinates. Only the part of the line inside the image is drawn, and nothing throws.
- Zero length. A line whose two points are the same draws a dot with
cap: "round"or"square", and nothing with"butt", as in SVG. Awidthof0draws nothing. - Validation. Every coordinate must be a finite number:
NaNorInfinitythrows aZodErrornaming the option, such asx1. A negativewidththrows aZodErrortoo. Acolorstring that isn’t a CSS color throwsInvalid color "notacolor"(with your string in the quotes). - Color spaces. CSS color strings are sRGB and are converted to the image’s color space, so they look the same on a Display P3 image. Numbers and
{ r, g, b, a }objects are pixel values and aren’t converted. - Added in 2.0.