Skip to content
docs
GitHub 4.5M /week v2.2.0
Size & shape

circle()

Make everything outside a circle transparent.

From @jimp/plugin-circle

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

circle.d.ts
image.circle(options?: { radius?: number; x?: number; y?: number }): this

circle 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

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("profile.jpg");
// Square it up first, so the circle fills the whole image
image.cover({ w: 256, h: 256 }).circle();
// Save as PNG to keep the transparency
await image.write("avatar.png");

A smaller circle

example.ts
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.

Try 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
135px
Loading Jimp…Drop an image to load it
PNG
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 cover or crop first to get a square.
  • Changed in 2.0: radius: 0 makes the whole image transparent. Before, it was treated as “not set” and gave the largest circle that fits. Leave radius out 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.