Skip to content

Commit

Permalink
poke
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Mar 16, 2024
1 parent 296d4b2 commit 637e659
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
17 changes: 17 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ export type JimpInstanceMethods<T> = {
[K in keyof T]: JimpInstanceMethod<T, T[K]>;
};

// type for instantiated class
type Instance<T> = T extends Constructor<infer U> ? U : never;

type JimpSupportedFormats<U> = U extends Format<infer M>[] ? M : never;

export * from "./utils/constants.js";
Expand Down Expand Up @@ -127,6 +130,20 @@ export class Jimp<
Constructor<Jimp<SupportedFormats, NewFormats>>;
}

static build<S extends Constructor<any>>(this: S) {
// a generic type that relpaces the return type of the methods in the class
type ReplaceReturnTypeInMethodMap<T> = {
[K in keyof T]: T[K] extends (...args: infer Args) => infer R
? (...args: Args) => FullConstructorWithMethods
: T[K];
};

type FullConstructorWithMethods = Instance<S>;

return class extends this {} as typeof this &
Constructor<ReplaceReturnTypeInMethodMap<FullConstructorWithMethods>>;
}

constructor(options: JimpOptions = {}) {
const classConstructor = this.constructor as typeof Jimp;

Expand Down
16 changes: 15 additions & 1 deletion packages/jimp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
34 changes: 18 additions & 16 deletions plugins/plugin-blit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import { JimpClass } from "@jimp/types";
import { limit255, scan } from "@jimp/utils";

export interface BlitOptions<I extends JimpClass> {
/** 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
*/
Expand All @@ -14,22 +31,7 @@ function blit<I extends JimpClass>(
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<I>,
) {
if (!("bitmap" in src)) {
throw new Error("The source must be a Jimp image");
Expand Down

0 comments on commit 637e659

Please sign in to comment.