circle makes every pixel outside a circle fully transparent, with a smooth one-pixel edge.
With no options the circle is centered and as large as fits, which turns a square image into a round avatar.
Signature
image.circle(options?: { radius?: number; x?: number; y?: number }): thiscircle changes the image in place and returns it, so you can chain more calls after it.
Options
| Option | Type | Default | Description |
|---|---|---|---|
radius |
number |
half the shorter side | The radius of the circle in pixels. Must be 0 or more. 0 makes the whole image transparent. |
x |
number |
image.width / 2 |
The x position of the center. |
y |
number |
image.height / 2 |
The y position of the center. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("profile.jpg");
// Square it up first, so the circle fills the whole imageimage.cover({ w: 256, h: 256 }).circle();
// Save as PNG to keep the transparencyawait image.write("avatar.png");A smaller circle
image.circle({ radius: 50, x: 80, y: 60 });The image keeps its size; only the pixels outside the circle become transparent. Crop afterwards to remove the empty space.
Try it
Click the image to move the center and drag radius to resize the circle.
image.circle({ radius: 135, x: 180, y: 135 })Make every pixel outside a circle transparent. Click the image to move the center.
x, y
Loading Jimp…Drop an image to load it
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- The size doesn’t change. On a non-square image the default circle touches the top and bottom (or the sides), and the rest is transparent. Use
coverorcropfirst to get a square. - Changed in 2.0:
radius: 0makes the whole image transparent. Before, it was treated as “not set” and gave the largest circle that fits. Leaveradiusout to get the default. - Only alpha changes. Pixels outside the circle keep their color values with an alpha of
0. - Rounded rectangles. For rounded corners instead of a full circle, use
roundCorners.