blit()
Copy pixels from another image, or part of it, onto this one.
From @jimp/plugin-blit
blit copies pixels from a source image onto this one at a position. Its name is short for “bit-block transfer”.
Unlike composite, it can copy just a rectangle of the source, which makes it the tool for sprite sheets, tiles and copying one part of an image to another.
Signature
image.blit(src: Jimp): thisimage.blit(options: { src: Jimp; x?: number; y?: number; srcX?: number; srcY?: number; srcW?: number; srcH?: number;}): thisblit changes this image in place and returns it, so you can chain more calls after it. The source image isn’t changed.
Options
Pass an image on its own to copy all of it to the top-left corner, or an object to choose where it goes and which part to copy.
| Option | Type | Default | Description |
|---|---|---|---|
src |
Jimp |
required | The image to copy from. |
x |
number |
0 |
Where the copied pixels’ left edge goes. |
y |
number |
0 |
Where the copied pixels’ top edge goes. |
srcX |
number |
0 |
The left edge of the area to copy from src. |
srcY |
number |
0 |
The top edge of the area to copy from src. |
srcW |
number |
src.width |
The width of the area to copy. |
srcH |
number |
src.height |
The height of the area to copy. |
All positions and sizes are rounded to whole pixels.
Example
Draw one image onto another at a position:
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");const badge = await Jimp.read("badge.png");
image.blit({ src: badge, x: 20, y: 20 });
await image.write("photo-badge.png");Copy part of a sprite sheet
Use srcX, srcY, srcW and srcH to pick a rectangle of the source. Here the second 32 × 32 sprite in the top row is copied onto a blank canvas:
const sheet = await Jimp.read("sprites.png");const sprite = new Jimp({ width: 32, height: 32 });
sprite.blit({ src: sheet, srcX: 32, srcY: 0, srcW: 32, srcH: 32 });Copy within the same image
To copy one area of an image to another place in it, blit from a clone:
image.blit({ src: image.clone(), x: 10, y: 10, srcX: 120, srcY: 80, srcW: 100, srcH: 60 });Passing the image itself as src reads pixels that the copy has already overwritten, which smears the result when the two areas overlap.
Try it
Drag on the image to pick the source region, and click to choose where it goes. Switch the source to sticker.png to blit a second image.
image.blit({ src: image.clone(), x: 14, y: 14, srcX: 126, srcY: 95, srcW: 108, srcH: 81 })Copy pixels onto the image at x, y. Copy a region of the image itself (drag to pick it, click to place it), or sticker.png.
import { Jimp } from "jimp"; const image = await Jimp.read("dice-on-paper.png"); // Change an option to run it await image.write("output.png");
Notes
- Transparency is respected. Source pixels are drawn over the image with the same “over” blending as
composite’s default mode. Fully transparent source pixels leave the image unchanged, and partly transparent ones mix with it. - Out-of-bounds parts are skipped. Pixels that would land outside the image are ignored, so the image never grows.
- The source area is clamped to the source. The part of
srcX,srcY,srcW,srcHoutside the source image is skipped. The pixels that are copied keep their position, so withsrcX: -1the first column atxis left as it was. - Color profiles. A source in a different color space from the image, for example an sRGB sprite on a Display P3 image, is converted to the image’s color space first. See Color spaces.
- Any Jimp image works.
blitaccepts any object with abitmapofdata,widthandheight, so it doesn’t matter which Jimp class made the source. - Validation. The options are checked with zod. Calling
blit()without an image, or with an object withoutsrc, throws a validation error. - No blend modes or opacity. For those, use
composite. To copy part of a source withcomposite,cropa clone of it first. - Changed in 2.0: semi-transparent source pixels were mixed as if the image were opaque, so blitting onto a transparent image darkened them. A source area that ran past the source’s right edge used to pick up pixels from the start of its next rows. Sources in a different color space were copied without conversion.