shadow()
Add a shadow shaped like the image. Not included in the jimp package.
From @jimp/plugin-shadow
shadow draws a soft, offset shadow in the shape of the image’s opaque pixels, such as a logo or a cut-out product photo, and puts the image back on top.
It comes from @jimp/plugin-shadow, which isn’t part of the jimp package, so you add it to a custom Jimp first.
Setup
Install the plugin and @jimp/core:
npm install jimp @jimp/core @jimp/plugin-shadow Then build a Jimp class with the default formats and plugins plus the shadow plugin, and use it everywhere you’d use Jimp:
import { createJimp } from "@jimp/core";import { defaultFormats, defaultPlugins } from "jimp";import * as shadow from "@jimp/plugin-shadow";
export const Jimp = createJimp({ formats: defaultFormats, plugins: [...defaultPlugins, shadow.methods],});See Custom Jimp for more on createJimp.
Signature
image.shadow(options?: { opacity?: number; size?: number; blur?: number; x?: number; y?: number;}): thisshadow changes the image in place and returns it, so you can chain more calls after it. The image keeps its size.
The options type is exported from @jimp/plugin-shadow as ShadowOptions.
Options
| Option | Type | Default | Description |
|---|---|---|---|
opacity |
number |
0.7 |
How dark the shadow is, from 0 (invisible) to 1. |
size |
number |
1.1 |
How much bigger than the image the shadow is drawn, scaled from the image’s center. 1 is the same size. |
blur |
number |
5 |
The blur radius in pixels, passed to blur. Rounded; 0 doesn’t blur. |
x |
number |
-25 |
How far to move the shadow right, in pixels. Negative moves it left. |
y |
number |
25 |
How far to move the shadow down, in pixels. Negative moves it up. |
Example
The shadow can only appear where the image is transparent, so give the subject some transparent room around it first. Here a logo is placed on a larger transparent canvas, then given a shadow down and to the right:
import { Jimp } from "./jimp";
const logo = await Jimp.read("logo.png");
const canvas = new Jimp({ width: logo.width + 60, height: logo.height + 60 });
canvas .composite(logo, 20, 20) .shadow({ x: 12, y: 12, blur: 6, size: 1 });
await canvas.write("logo-shadow.png");With no options, image.shadow() uses the defaults: a shadow 10% larger than the image, 25 pixels left and 25 pixels down, at 70% opacity.
Notes
- How it’s drawn. Jimp copies the image, turns every pixel black while keeping its alpha, resizes the copy by
size, blurs it and sets its opacity. It then centers the copy on a transparent canvas the size of the image, moves it byx,y, and draws the original image on top. - Opaque images hide it. Because the image keeps its size and goes back on top, a fully opaque image, such as a photo, covers its whole shadow and nothing changes. Use
shadowon images with transparency, or useboxShadowfor rectangles. - Clipped at the edges. Any part of the shadow that falls outside the image’s bounds is cut off. Add transparent space first, as in the example.
sizegrows from the center. The enlarged shadow is centered on the image beforexandymove it, so withsize: 1.2it reaches 10% past each side.- Blur is rounded.
bluris rounded to a whole number before it’s passed toblur, and a value that rounds to0leaves the shadow sharp. - Mixing Jimp classes. Images from the
jimppackage’sJimpand from your custom class can be composited onto each other, but only images made by your class have theshadowmethod. - Color profiles. The canvas the shadow is drawn on gets the image’s ICC profile, so images in Display P3 or another color space keep their colors.
- Validation.
opacitymust be between0and1; other values throw a validation error. - Changed in 2.0: the shadow is a dark silhouette drawn under the image. It used to be a blurred copy of the image drawn over it.
sizeused to grow the shadow from the top-left corner, so it reached further right and down than the image; it’s now centered. A fractionalblurused to make the shadow disappear andblur: 0threwr must be greater than 0;bluris now rounded and0means no blur. Compositing images from different Jimp classes used to throwThe source must be a Jimp image.