template-project/node_modules/@jimp/plugin-displace/dist/esm/index.js
2025-05-30 18:13:30 +08:00

39 lines
1.3 KiB
JavaScript

import { JimpClassSchema } from "@jimp/types";
import { clone } from "@jimp/utils";
import { z } from "zod";
const DisplaceOptionsSchema = z.object({
/** the source Jimp instance */
map: JimpClassSchema,
/** the maximum displacement value */
offset: z.number(),
});
export const methods = {
/**
* Displaces the image based on the provided displacement map
* @param map the source Jimp instance
* @param offset
* @example
* ```ts
* import { Jimp } from "jimp";
*
* const image = await Jimp.read("test/image.png");
* const map = await Jimp.read("test/map.png");
*
* image.displace(map, 10);
* ```
*/
displace(image, options) {
const { map, offset } = DisplaceOptionsSchema.parse(options);
const source = clone(image);
image.scan((x, y, idx) => {
let displacement = (map.bitmap.data[idx] / 256) * offset;
displacement = Math.round(displacement);
const ids = image.getPixelIndex(x + displacement, y);
image.bitmap.data[ids] = source.bitmap.data[idx];
image.bitmap.data[ids + 1] = source.bitmap.data[idx + 1];
image.bitmap.data[ids + 2] = source.bitmap.data[idx + 2];
});
return image;
},
};
//# sourceMappingURL=index.js.map