Skip to content

Commit 637e659

Browse files
poke
1 parent 296d4b2 commit 637e659

File tree

3 files changed

+50
-17
lines changed

3 files changed

+50
-17
lines changed

packages/core/src/index.ts

+17
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ export type JimpInstanceMethods<T> = {
6363
[K in keyof T]: JimpInstanceMethod<T, T[K]>;
6464
};
6565

66+
// type for instantiated class
67+
type Instance<T> = T extends Constructor<infer U> ? U : never;
68+
6669
type JimpSupportedFormats<U> = U extends Format<infer M>[] ? M : never;
6770

6871
export * from "./utils/constants.js";
@@ -127,6 +130,20 @@ export class Jimp<
127130
Constructor<Jimp<SupportedFormats, NewFormats>>;
128131
}
129132

133+
static build<S extends Constructor<any>>(this: S) {
134+
// a generic type that relpaces the return type of the methods in the class
135+
type ReplaceReturnTypeInMethodMap<T> = {
136+
[K in keyof T]: T[K] extends (...args: infer Args) => infer R
137+
? (...args: Args) => FullConstructorWithMethods
138+
: T[K];
139+
};
140+
141+
type FullConstructorWithMethods = Instance<S>;
142+
143+
return class extends this {} as typeof this &
144+
Constructor<ReplaceReturnTypeInMethodMap<FullConstructorWithMethods>>;
145+
}
146+
130147
constructor(options: JimpOptions = {}) {
131148
const classConstructor = this.constructor as typeof Jimp;
132149

packages/jimp/src/index.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,18 @@ import blit from "@jimp/plugin-blit";
55

66
import png from "@jimp/js-png";
77

8-
export const Jimp = JimpCustom.addFormat(png).plugin(blit).plugin(crop);
8+
export const Jimp = JimpCustom
9+
// .addFormat(png)
10+
.plugin(blit)
11+
.plugin(crop)
12+
.build();
13+
14+
const image = new Jimp();
15+
16+
image
17+
.blit({ src: image, x: 0, y: 0 })
18+
.crop(0, 0, image.bitmap.width, image.bitmap.height);
19+
20+
image
21+
.crop(0, 0, image.bitmap.width, image.bitmap.height)
22+
.blit({ src: image, x: 0, y: 0 });

plugins/plugin-blit/src/index.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import { JimpClass } from "@jimp/types";
22
import { limit255, scan } from "@jimp/utils";
33

4+
export interface BlitOptions<I extends JimpClass> {
5+
/** This image to blit on to the current image */
6+
src: I;
7+
/** the x position to blit the image */
8+
x: number;
9+
/** the y position to blit the image */
10+
y: number;
11+
/** the x position from which to crop the source image */
12+
srcX?: number;
13+
/** the y position from which to crop the source image */
14+
srcY?: number;
15+
/** the width to which to crop the source image */
16+
srcW?: number;
17+
/** the height to which to crop the source image */
18+
srcH?: number;
19+
}
20+
421
/**
522
* Blits a source image on to this image
623
*/
@@ -14,22 +31,7 @@ function blit<I extends JimpClass>(
1431
srcY = 0,
1532
srcW = src.bitmap.width,
1633
srcH = src.bitmap.height,
17-
}: {
18-
/** This image to blit on to the current image */
19-
src: I;
20-
/** the x position to blit the image */
21-
x: number;
22-
/** the y position to blit the image */
23-
y: number;
24-
/** the x position from which to crop the source image */
25-
srcX?: number;
26-
/** the y position from which to crop the source image */
27-
srcY?: number;
28-
/** the width to which to crop the source image */
29-
srcW?: number;
30-
/** the height to which to crop the source image */
31-
srcH?: number;
32-
},
34+
}: BlitOptions<I>,
3335
) {
3436
if (!("bitmap" in src)) {
3537
throw new Error("The source must be a Jimp image");

0 commit comments

Comments
 (0)