diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 21b7a63d..d5c85bd6 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -63,6 +63,9 @@ export type JimpInstanceMethods = { [K in keyof T]: JimpInstanceMethod; }; +// type for instantiated class +type Instance = T extends Constructor ? U : never; + type JimpSupportedFormats = U extends Format[] ? M : never; export * from "./utils/constants.js"; @@ -127,6 +130,20 @@ export class Jimp< Constructor>; } + static build>(this: S) { + // a generic type that relpaces the return type of the methods in the class + type ReplaceReturnTypeInMethodMap = { + [K in keyof T]: T[K] extends (...args: infer Args) => infer R + ? (...args: Args) => FullConstructorWithMethods + : T[K]; + }; + + type FullConstructorWithMethods = Instance; + + return class extends this {} as typeof this & + Constructor>; + } + constructor(options: JimpOptions = {}) { const classConstructor = this.constructor as typeof Jimp; diff --git a/packages/jimp/src/index.ts b/packages/jimp/src/index.ts index d597d857..97178b30 100644 --- a/packages/jimp/src/index.ts +++ b/packages/jimp/src/index.ts @@ -5,4 +5,18 @@ import blit from "@jimp/plugin-blit"; import png from "@jimp/js-png"; -export const Jimp = JimpCustom.addFormat(png).plugin(blit).plugin(crop); +export const Jimp = JimpCustom + // .addFormat(png) + .plugin(blit) + .plugin(crop) + .build(); + +const image = new Jimp(); + +image + .blit({ src: image, x: 0, y: 0 }) + .crop(0, 0, image.bitmap.width, image.bitmap.height); + +image + .crop(0, 0, image.bitmap.width, image.bitmap.height) + .blit({ src: image, x: 0, y: 0 }); diff --git a/plugins/plugin-blit/src/index.ts b/plugins/plugin-blit/src/index.ts index 509cb534..fa8d9dd3 100644 --- a/plugins/plugin-blit/src/index.ts +++ b/plugins/plugin-blit/src/index.ts @@ -1,6 +1,23 @@ import { JimpClass } from "@jimp/types"; import { limit255, scan } from "@jimp/utils"; +export interface BlitOptions { + /** This image to blit on to the current image */ + src: I; + /** the x position to blit the image */ + x: number; + /** the y position to blit the image */ + y: number; + /** the x position from which to crop the source image */ + srcX?: number; + /** the y position from which to crop the source image */ + srcY?: number; + /** the width to which to crop the source image */ + srcW?: number; + /** the height to which to crop the source image */ + srcH?: number; +} + /** * Blits a source image on to this image */ @@ -14,22 +31,7 @@ function blit( srcY = 0, srcW = src.bitmap.width, srcH = src.bitmap.height, - }: { - /** This image to blit on to the current image */ - src: I; - /** the x position to blit the image */ - x: number; - /** the y position to blit the image */ - y: number; - /** the x position from which to crop the source image */ - srcX?: number; - /** the y position from which to crop the source image */ - srcY?: number; - /** the width to which to crop the source image */ - srcW?: number; - /** the height to which to crop the source image */ - srcH?: number; - }, + }: BlitOptions, ) { if (!("bitmap" in src)) { throw new Error("The source must be a Jimp image");