skew()
Slant the image along x and/or y by an angle, like CSS skew().
From @jimp/plugin-rotate
skew slants the image by an angle in degrees, like CSS skew(x, y).
Use x to lean vertical lines over, as for italic or a sense of speed, and y to tilt horizontal lines, as for a slanted banner.
Signature
image.skew(options: { x?: number; y?: number; mode?: ResizeStrategy; fit?: boolean;}): thisskew changes the image in place and returns it, so you can chain more calls after it. The options type is exported as SkewOptions.
Options
| Option | Type | Default | Description |
|---|---|---|---|
x |
number |
0 |
The angle in degrees along the x axis, like CSS skewX(). Positive values slant vertical lines like \, negative values like /. |
y |
number |
0 |
The angle in degrees along the y axis, like CSS skewY(). Positive values move the right side down. |
mode |
ResizeStrategy |
nearest pixel | How to sample the pixels. Leave it out to copy the nearest pixel, which keeps hard edges. Pass a strategy to interpolate. |
fit |
boolean |
true |
true grows the canvas to fit the skewed image. false keeps the width and height and crops what falls outside. |
Example
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// 800 x 600 becomes 1019 x 600image.skew({ x: 20 });
await image.write("photo-skewed.png");The canvas grows by height * tan(x) in width (and width * tan(y) in height), so nothing is cut off.
The new space is filled with image.background, which is transparent black for images loaded with Jimp.read. Save as PNG to keep it transparent, or set image.background first.
Smooth edges
The default copies the nearest pixel, so the slanted edges look stepped. Pass a ResizeStrategy to interpolate:
import { Jimp, ResizeStrategy } from "jimp";
const image = await Jimp.read("photo.jpg");
image.skew({ x: 10, y: -15, mode: ResizeStrategy.BILINEAR });Keep the size
image.skew({ x: 20, fit: false });The image is skewed about its center and keeps its width and height. The corners that slide outside are cropped, and the areas the image no longer covers are filled with image.background.
Try it
Drag x and y to change the angles, pick a mode to smooth the edges, and turn off fit to keep the size.
image.skew({ x: 20 })Slant the image by an angle along x and/or y, like CSS skew(). The canvas grows to fit unless fit is off.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");
Notes
- Added in 2.0.
- A shortcut for
affine.skew({ x, y })isaffinewith the matrix[1, tan(y), tan(x), 1, 0, 0]. Useaffineto combine a skew with scaling, flipping or rotating in one step. - Same as CSS. The angles have the same meaning as in CSS
skew(), soskew({ x: 20, y: -10 })matchestransform: skew(20deg, -10deg)with the defaulttransform-originof the center. - Exact where possible. If every output pixel lands on the center of a source pixel, as at 45°, the nearest pixel is taken even when
modeis set, so the result stays sharp. - Transparency. With a
mode, colors are weighted by alpha, so edges against a transparent background don’t get a dark fringe. Withfiton, the image is drawn overimage.backgroundfirst, asrotatedoes. - Errors. An angle of 90° (or 90° plus a multiple of 180°) throws
Skew angles can't be 90 degrees (or 90 plus a multiple of 180), it would make the image infinitely long. Skewing both axes by 45° flattens the image and throwsThe matrix can't be inverted (a * d - b * c is 0), so it would flatten the image into a line or a point.NaNfails validation withInvalid input: expected number, received NaN. These arrive as a zod error; see Errors and validation. - Options object required.
skew(20)fails withInvalid input: expected object, received number. Passskew({ x: 20 }).