File tree 3 files changed +50
-17
lines changed
3 files changed +50
-17
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,9 @@ export type JimpInstanceMethods<T> = {
63
63
[ K in keyof T ] : JimpInstanceMethod < T , T [ K ] > ;
64
64
} ;
65
65
66
+ // type for instantiated class
67
+ type Instance < T > = T extends Constructor < infer U > ? U : never ;
68
+
66
69
type JimpSupportedFormats < U > = U extends Format < infer M > [ ] ? M : never ;
67
70
68
71
export * from "./utils/constants.js" ;
@@ -127,6 +130,20 @@ export class Jimp<
127
130
Constructor < Jimp < SupportedFormats , NewFormats > > ;
128
131
}
129
132
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
+
130
147
constructor ( options : JimpOptions = { } ) {
131
148
const classConstructor = this . constructor as typeof Jimp ;
132
149
Original file line number Diff line number Diff line change @@ -5,4 +5,18 @@ import blit from "@jimp/plugin-blit";
5
5
6
6
import png from "@jimp/js-png" ;
7
7
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 } ) ;
Original file line number Diff line number Diff line change 1
1
import { JimpClass } from "@jimp/types" ;
2
2
import { limit255 , scan } from "@jimp/utils" ;
3
3
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
+
4
21
/**
5
22
* Blits a source image on to this image
6
23
*/
@@ -14,22 +31,7 @@ function blit<I extends JimpClass>(
14
31
srcY = 0 ,
15
32
srcW = src . bitmap . width ,
16
33
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 > ,
33
35
) {
34
36
if ( ! ( "bitmap" in src ) ) {
35
37
throw new Error ( "The source must be a Jimp image" ) ;
You can’t perform that action at this time.
0 commit comments