-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
187 lines (173 loc) · 5.29 KB
/
Copy pathindex.ts
File metadata and controls
187 lines (173 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
export interface CutConfig {
startX: number;
startY: number;
width: number;
height: number;
}
export interface QualityConfig {
type?: string | undefined;
quality?: number | undefined;
}
export class ImageHelper {
/**
* it can boost the performance if the image to be processed is mutable
*/
private sharedCanvas = document.createElement("canvas");
private image?: ImageBitmapSource;
constructor(image?: ImageBitmapSource, private mutable: boolean = false) {
image && this.setImage(image);
}
static getContextInCPURam(canvas: HTMLCanvasElement | OffscreenCanvas) {
const context = canvas.getContext("2d", {
alpha: false,
willReadFrequently: true,
desynchronized: false,
});
if (!context) throw new Error("get RenderingContext2D failed");
return context;
}
/**
*
* @param image Do Not support Blob
* @param context
*/
static drawOnContext(
image: ImageBitmapSource,
context: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D,
sx: number,
sy: number,
sw: number,
sh: number
) {
if (image instanceof Blob) {
throw new Error("drawOnContext does not support Blob");
} else if (image instanceof ImageData) {
context.putImageData(image, 0, 0, sx, sy, sw, sh);
} else {
context.drawImage(image, sx, sy, sw, sh, 0, 0, sw, sh);
}
}
setImage(image: ImageBitmapSource) {
this.image = image;
return this;
}
async getResolution() {
const imageBitmap = await this.toImageBitmap();
const { width, height } = imageBitmap;
imageBitmap.close();
return { width, height };
}
/**
* create OffscreenCanvas from ImageBitmap. your browser should support OffscreenCanvas!
*/
toOffscreenCanvas(cutConfig: CutConfig) {
if (!window.OffscreenCanvas)
throw new Error("browser does not support OffscreenCanvas");
const { startX: sx, startY: sy, width: sw, height: sh } = cutConfig;
const offscreenCanvas = new OffscreenCanvas(sw, sh);
const context = ImageHelper.getContextInCPURam(offscreenCanvas);
if (!this.image) throw new Error("no avaliable image to process");
ImageHelper.drawOnContext(this.image, context, sx, sy, sw, sh);
return { canvas: offscreenCanvas, context: context };
}
toOnscreenCanvas(cutConfig: CutConfig) {
// create canvas and draw
const canvas = this.mutable
? this.sharedCanvas
: document.createElement("canvas");
const { startX: sx, startY: sy, width: sw, height: sh } = cutConfig;
canvas.width = sw;
canvas.height = sh;
const context = ImageHelper.getContextInCPURam(canvas);
if (!this.image) throw new Error("no avaliable image to process");
ImageHelper.drawOnContext(this.image, context, sx, sy, sw, sh);
return { canvas, context };
}
/**
* @param fileName length <= 255
* @param fileProperty lastModified field
* @param options image format and quality
*/
async toFile(
fileName: string,
fileProperty?: FilePropertyBag,
options?: QualityConfig,
cutConfig?: CutConfig
) {
if (this.image instanceof File) return this.image;
const blob = await this.toBlob(options, cutConfig);
return new File([blob], fileName, fileProperty);
}
/**
* @param options image format and quality
*/
async toBlob(options?: QualityConfig, cutConfig?: CutConfig): Promise<Blob> {
if (this.image instanceof Blob) return this.image;
const { width, height } = await this.getResolution();
cutConfig = cutConfig ?? {
startX: 0,
startY: 0,
height: height,
width: width,
};
const { canvas } = this.toOnscreenCanvas(cutConfig);
return new Promise((resolve, reject) =>
canvas.toBlob(
(blob) => (blob ? resolve(blob) : reject()),
options?.type,
options?.quality
)
);
}
async toImageData(cutConfig?: CutConfig) {
if (this.image instanceof ImageData) return this.image;
const { width, height } = await this.getResolution();
cutConfig = cutConfig ?? {
startX: 0,
startY: 0,
height: height,
width: width,
};
const { startX: sx, startY: sy, width: sw, height: sh } = cutConfig;
const { context } = this.toOnscreenCanvas(cutConfig);
const blob = context.getImageData(sx, sy, sw, sh);
return blob;
}
/**
*
* @param sx
* @param sy
* @param sw
* @param sh
* @returns
*/
async toImageBitmap(sx?: number, sy?: number, sw?: number, sh?: number) {
if (!this.image) throw new Error("no avaliable image to process");
if (
sx !== undefined &&
sy !== undefined &&
sw !== undefined &&
sh !== undefined
) {
return await createImageBitmap(this.image, sx, sy, sw, sh);
} else {
return await createImageBitmap(this.image);
}
}
async toImageElement(options?: QualityConfig, cutConfig?: CutConfig) {
if (this.image instanceof HTMLImageElement) return this.image;
const { width, height } = await this.getResolution();
cutConfig = cutConfig ?? {
startX: 0,
startY: 0,
height: height,
width: width,
};
const { startX: sx, startY: sy, width: sw, height: sh } = cutConfig;
const imageElement = new Image(sw, sh);
imageElement.src = URL.createObjectURL(
await this.toBlob(options, cutConfig)
);
return imageElement;
}
}