Size & shape
autocropRect()
Measure the rectangle autocrop would keep, without cropping.
From @jimp/plugin-crop
autocropRect works out the rectangle that autocrop would keep and returns it, without changing the image.
Use it when you need the offsets, for example to remember where a trimmed sprite sat in its frame, or to check the crop before applying it.
Signature
image.autocropRect(options?: AutocropOptions): { x: number; y: number; w: number; h: number }image.autocropRect(tolerance: number): { x: number; y: number; w: number; h: number }autocropRect doesn’t change the image. It returns a rectangle in the same shape as the options of crop, so you can pass it straight to crop.
Options
The options are the same as autocrop’s; see autocrop options for the details.
| Option | Type | Default | Description |
|---|---|---|---|
tolerance |
number |
0.0002 |
How different from the top-left pixel a pixel can be and still count as border, from 0 to 1. |
cropOnlyFrames |
boolean |
true |
Only crop if every side that isn’t in ignoreSides has a border. |
cropSymmetric |
boolean |
false |
Trim the same amount from opposite sides. |
leaveBorder |
number |
0 |
Pixels of border to keep on every side. |
ignoreSides |
object |
none | Sides to leave alone: { north, south, east, west }. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("sprite.png");
const rect = image.autocropRect();console.log(rect); // { x: 12, y: 4, w: 40, h: 56 }
image.crop(rect);await image.write("sprite-trimmed.png");Keep the offsets for packing
When you trim sprites for a sprite sheet, store where each one sat in its original frame so it can be drawn in the same place later:
const { x, y, w, h } = image.autocropRect();
const entry = { frame: { w, h }, offset: { x, y }, sourceSize: { w: image.width, h: image.height },};
image.crop({ x, y, w, h });Notes
- Nothing to trim. If
autocropwouldn’t crop, the result is the whole image:{ x: 0, y: 0, w: image.width, h: image.height }. This includes the case wherecropOnlyFramesistrueand a side that isn’t ignored has no border. - Same rules as
autocrop. Everything on theautocroppage applies, including howtoleranceis measured and the changes in 2.0 toignoreSides,leaveBorderand transparent borders. - Added in 2.0.