boxShadow()
Add a CSS-style drop shadow, growing the image to fit it.
From @jimp/plugin-border
boxShadow draws a soft shadow under the image, the way CSS box-shadow does for an element.
The image grows to make room for the shadow, and the original pixels are drawn on top unchanged. It’s the quickest way to make a screenshot or a card look lifted off the page.
Signature
image.boxShadow(options?: { x?: number; y?: number; blur?: number; spread?: number; color?: number | string; radius?: number | CornerRadius;}): thisboxShadow changes the image in place, including its size, and returns it, so you can chain more calls after it.
The options type is exported from jimp as BoxShadowOptions.
Options
Every option is optional; image.boxShadow() draws a soft shadow just below the image.
| Option | Type | Default | Description |
|---|---|---|---|
x |
number |
0 |
How far to move the shadow right, in pixels. Negative moves it left. |
y |
number |
4 |
How far to move the shadow down, in pixels. Negative moves it up. |
blur |
number |
12 |
The blur radius in pixels, as in CSS. 0 gives a hard-edged shadow. Must be 0 or more. |
spread |
number |
0 |
How much to grow the shadow on every side, in pixels. Negative shrinks it. |
color |
number | string |
0x00000080 |
The shadow color: an RGBA number such as 0x00000080, or any CSS color such as "rgba(0, 0, 0, 0.3)". The default is 50% black. |
radius |
number | CornerRadius |
0 |
The radius of the shadow’s corners: one number, or { topLeft, topRight, bottomRight, bottomLeft }. Use the radius you rounded the image with. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("screenshot.png");
image.boxShadow({ y: 8, blur: 16 });
await image.write("screenshot-shadow.png");A 360 × 270 image becomes 392 × 302: 16 pixels are added on the left and right for the blur, 8 on top and 24 on the bottom, because the shadow is moved 8 pixels down.
A rounded card
Round the image’s corners with roundCorners first, then give the shadow the same radius so it follows the curve:
image .roundCorners({ radius: 24 }) .boxShadow({ y: 10, blur: 20, radius: 24, color: "rgba(27, 24, 64, 0.45)" });A glow
With no offset and a bright color, the shadow becomes a glow around every edge. spread pushes it further out:
image.boxShadow({ x: 0, y: 0, blur: 24, spread: 4, color: "#ffd83a" });Place the result on a page
Because the image grows, it’s ready to composite onto a background:
const page = new Jimp({ width: 600, height: 400, color: 0xf4f1eaff });const photo = await Jimp.read("photo.png");
photo.boxShadow({ y: 8, blur: 16 });
page.composite(photo, 100, 60);Try it
Move the sliders to change the shadow. Raise radius to round the image’s corners and the shadow’s together.
image.boxShadow({ y: 8, blur: 16 })Draw a CSS-style shadow under the image's box. The image grows to fit the shadow.
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
- How much the image grows. Each side grows by
blur + spread, minus the offset toward that side, and never by less than0. The left side grows byblur + spread - x, the right byblur + spread + x, the top byblur + spread - yand the bottom byblur + spread + y, each rounded up. The original image ends up that far from the new top-left corner. - Blur like CSS. The shadow is blurred with a Gaussian whose standard deviation is half of
blur, which is how browsers drawbox-shadow. - The shadow is a box. It’s the shape of the whole image (with rounded corners if you give
radius), not of its opaque pixels, so it shows through transparent areas. For a shadow shaped like the image’s content, useshadowfrom@jimp/plugin-shadow. - Corner radii. Radii too large for the image are scaled down, as with CSS
border-radius.spreadgrows rounded corners along with the box, but square corners stay square. - Transparent background. The new area outside the shadow is transparent. Save as PNG to keep it, or composite onto an opaque background before saving as JPEG.
- CSS colors and color profiles. A CSS color string is sRGB, as in CSS. On an image with a Display P3 or other color profile, it’s converted to the image’s color space. A number is used as raw pixel values.
- Validation.
blurandradiusmust be0or more; negative values throw a validation error. Acolorstring that isn’t a CSS color throwsInvalid color "nope"(with your string in the quotes). - Added in 2.0.