-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
388 lines (349 loc) · 14 KB
/
script.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
export class Cropo {
// zoom range input
private slider: HTMLInputElement | undefined;
// canvas related variables
private canvas: HTMLCanvasElement;
private canvasContext: CanvasRenderingContext2D;
private canvasWidth: number = 0;
private canvasHeight: number = 0;
// image related variables
private fit: boolean = true;
private img: HTMLImageElement;
private imgHeight: number;
private imgWidth: number;
private scale: number;
private baseScale: number = 1;
private maxScale: number = 5;
private minScale: number = 1;
private originalWidth: number;
private originalHeight: number;
private ratio: number;
// Pointer drag related variables
private isDown: boolean;
private pointerX: number
private pointerY: number;
// the accumulated horizontal(X) & vertical(Y) panning the user has done in total
private netPanningX: number;
private netPanningY: number;
// zoom and pinch related variables
private originX: number
private originY: number;
private eventCache: PointerEvent[];
private prevDiff: number;
// TODO: remove optional from version 1.0.0
constructor (options: {
imageUrl?: string;
onImageLoad?: () => void;
canvas?: HTMLCanvasElement,
rangeInput?: HTMLInputElement,
height?: number,
width?: number,
x?: number,
y?: number,
fit?: boolean,
baseScale?: number;
maxScale?: number;
minScale?: number;
}) {
this.baseScale = options?.baseScale || this.baseScale
this.maxScale = options?.maxScale || this.maxScale
this.minScale = options?.minScale || this.minScale
this.loadCanvas(options?.canvas || document.createElement('canvas'), options?.width, options?.height)
options?.rangeInput && this.loadSlider(options.rangeInput)
if (options?.imageUrl) {
this.loadImageFromUrl(options?.imageUrl, options?.fit, () => {
this.move(options?.x || 0, options?.y || 0)
options?.onImageLoad?.()
})
}
}
// define function to clamp number
private clamp (num: number, from: number, to: number) {
return Math.max(from, Math.min(num, to))
}
// define debounce function
private debounce<Params extends any[]> (func: (...args: Params) => any, timeout: number): (...args: Params) => void {
let timer
return (...args: Params) => {
clearTimeout(timer)
timer = setTimeout(() => {
func(...args)
}, timeout)
}
}
// initialize pointer related variables
private initPointerAndZoom () {
this.isDown = false
this.netPanningX = 0
this.netPanningY = 0
this.eventCache = []
this.prevDiff = -1
if (this.slider) this.slider.value = String(this.baseScale)
}
// draw image
private draw () {
if (!this.img) return
this.canvasContext?.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
this.canvasContext?.drawImage(this.img, this.netPanningX, this.netPanningY, this.imgWidth, this.imgHeight)
}
// fix range
private fixScale () {
if (this.fit) {
this.scale = Math.min(this.imgWidth / this.canvasWidth, this.imgHeight / this.canvasHeight) || this.baseScale
} else {
this.scale = Math.min(this.imgWidth / this.originalWidth, this.imgHeight / this.originalHeight) || this.baseScale
}
if (this.slider) this.slider.value = String(this.scale)
}
// recalculate images related variables
private onImageLoad () {
if (this.fit) {
this.scale = Math.max(this.canvasHeight / this.imgHeight, this.canvasWidth / this.imgWidth)
this.imgHeight *= this.scale
this.imgWidth *= this.scale
}
this.pointerX = this.pointerY = 0
this.originalWidth = this.imgWidth
this.originalHeight = this.imgHeight
this.ratio = this.originalHeight / this.originalWidth
this.draw()
}
// get the median point of pointers
private getPointerAverage () {
let x = 0; let y = 0
for (let i = 0; i < this.eventCache.length; i++) {
x += this.eventCache[i].offsetX
y += this.eventCache[i].offsetY
}
x = x / this.eventCache.length
y = y / this.eventCache.length
return [x, y]
}
// calc origin for zoom and pinch
private calcOrigin (x: number, y: number) {
this.originX = (-this.netPanningX + x) / this.imgWidth
this.originY = (-this.netPanningY + y) / this.imgHeight
}
public move (x: number, y: number) {
// the last mousemove event
const dx = x - this.pointerX
const dy = y - this.pointerY
// reset the vars for next mousemove
this.pointerX = x
this.pointerY = y
// accumulate the net panning done
this.netPanningX = this.fit ? this.clamp(this.netPanningX + dx, this.canvasWidth - this.imgWidth, 0) : this.netPanningX + dx
this.netPanningY = this.fit ? this.clamp(this.netPanningY + dy, this.canvasHeight - this.imgHeight, 0) : this.netPanningY + dy
}
private drawZoom (deltaX: number, deltaY: number) {
this.netPanningX = this.fit ? this.clamp(this.netPanningX - deltaX * this.originX, this.canvasWidth - this.imgWidth, 0) : this.netPanningX - deltaX * this.originX
this.netPanningY = this.fit ? this.clamp(this.netPanningY - deltaY * this.originY, this.canvasHeight - this.imgHeight, 0) : this.netPanningY - deltaY * this.originY
}
private zoomDelta (deltaX: number, deltaY: number) {
const newWidth = this.imgWidth + deltaX
if (newWidth < this.originalWidth || this.imgHeight + deltaY < this.originalHeight) return
if (newWidth / this.originalWidth > this.maxScale || newWidth / this.originalWidth < this.minScale) return
if (this.slider) this.slider.value = String(this.scale = newWidth / this.originalWidth) // TODO: check
// calc new size
this.imgWidth = newWidth
this.imgHeight += deltaY
// accumulate the net panning done
this.drawZoom(deltaX, deltaY)
}
private zoomScale (scale: number) {
if (scale > this.maxScale || scale < this.minScale) return
this.prevDiff = -1
let deltaX = this.imgWidth
let deltaY = this.imgHeight
// calc new size
this.imgWidth = this.originalWidth * scale
this.imgHeight = this.originalHeight * scale
// calc diff
deltaX -= this.imgWidth
deltaY -= this.imgHeight
//
this.calcOrigin(this.canvasWidth / 2, this.canvasHeight / 2)
this.drawZoom(-deltaX, -deltaY)
}
private pinch () {
if (this.eventCache.length === 2) {
// Calculate the distance between the two pointers
const curDiff = Math.hypot(this.eventCache[0].offsetX - this.eventCache[1].offsetX, this.eventCache[0].offsetY - this.eventCache[1].offsetY)
// zoom into image
if (this.prevDiff > 0) {
const delta = curDiff - this.prevDiff
this.zoomDelta(delta, delta * this.ratio)
}
this.prevDiff = curDiff
}
}
/* -------------------------------------------------------------------------- */
/* Event Handlers */
/* -------------------------------------------------------------------------- */
private onSliderMove (e: Event) {
const value = (e.target as HTMLInputElement).value
this.scale = +value
this.zoomScale(this.scale)
this.draw()
};
private onPointerdown (e: PointerEvent) {
if (!this.img) return
// This event is cached to support 2-finger gestures
this.eventCache.push(e);
// refresh move origin
[this.pointerX, this.pointerY] = this.getPointerAverage()
this.isDown = true
};
private onPointerUp (e: PointerEvent) {
if (!this.isDown) return
// If the number of pointers down is less than two then reset diff tracker
this.eventCache = this.eventCache.filter(ev => ev.pointerId !== e.pointerId)
if (this.eventCache.length < 2) {
this.prevDiff = -1
}
[this.pointerX, this.pointerY] = this.getPointerAverage()
if (this.eventCache.length === 0) this.isDown = false
};
private onPointermove (e: PointerEvent) {
if (!this.isDown) return
// Find this event in the cache and update its record with this event
for (let i = 0; i < this.eventCache.length; i++) {
if (e.pointerId === this.eventCache[i].pointerId) {
this.eventCache[i] = e; break
}
}
// calc x,y and
const [x, y] = this.getPointerAverage()
this.move(x, y)
this.pinch()
this.calcOrigin(this.canvasWidth / 2, this.canvasHeight / 2)
this.draw()
};
private onResize = this.debounce<[]>(() => {
if (!this.img) return
const deltaX = this.canvas.offsetWidth - this.canvasWidth
const deltaY = this.canvas.offsetHeight - this.canvasHeight
this.canvasWidth = this.canvas.width = this.canvas.offsetWidth
this.canvasHeight = this.canvas.height = this.canvas.offsetHeight
if (this.fit && this.imgWidth < this.canvasWidth) {
this.netPanningX = 0
this.onImageLoad()
} else if (this.fit && this.imgHeight < this.canvasHeight) {
this.netPanningY = 0
this.onImageLoad()
} else {
this.netPanningX += deltaX / 2
this.netPanningY += deltaY / 2
this.originalWidth = this.canvasWidth
this.originalHeight = this.canvasWidth * this.ratio
this.draw()
}
this.fixScale()
}, 300)
private prevent (e: Event) {
e.preventDefault()
e.stopPropagation()
}
private leadListeners () {
this.canvas.addEventListener('pointerdown', (e) => { this.prevent(e); this.onPointerdown(e) })
this.canvas.addEventListener('pointermove', (e) => { this.prevent(e); this.onPointermove(e) })
this.canvas.addEventListener('pointerout', (e) => { this.prevent(e); this.onPointerUp(e) })
this.canvas.addEventListener('pointerup', (e) => { this.prevent(e); this.onPointerUp(e) })
this.canvas.addEventListener('pointercancel', (e) => { this.prevent(e); this.onPointerUp(e) })
this.canvas.addEventListener('pointerleave', (e) => { this.prevent(e); this.onPointerUp(e) })
new ResizeObserver(this.onResize).observe(this.canvas)
}
/* -------------------------------------------------------------------------- */
/* Loading */
/* -------------------------------------------------------------------------- */
public loadSlider (el: HTMLInputElement) {
this.slider = el
this.slider.value = String(this.scale || this.baseScale)
this.slider.addEventListener('input', (e) => { this.prevent(e); this.onSliderMove(e) })
}
public loadCanvas (el: HTMLCanvasElement, width?: number, height?: number) {
this.canvas = el
this.canvasContext = this.canvas.getContext('2d')
this.canvasWidth = this.canvas.width = width || this.canvas.offsetWidth
this.canvasHeight = this.canvas.height = height || this.canvas.offsetHeight
this.leadListeners()
}
public loadImageFromUrl (url: string, fitImage: boolean = true, onload?: () => void) {
if (!this.canvas) throw Error('first call loadCanvas')
this.fit = fitImage
this.img = new Image()
this.img.onload = () => {
this.initPointerAndZoom()
this.imgHeight = this.img.naturalHeight
this.imgWidth = this.img.naturalWidth
this.onImageLoad()
onload?.()
}
this.img.src = url
}
/* -------------------------------------------------------------------------- */
/* Export */
/* -------------------------------------------------------------------------- */
private getCanvas (scale:number) {
if (!this.img) throw Error('please set an image')
const canvas = document.createElement('canvas')
canvas.width = this.canvasWidth * scale
canvas.height = this.canvasHeight * scale
const context = canvas.getContext('2d')
context.drawImage(this.img, this.netPanningX * scale, this.netPanningY * scale, this.imgWidth * scale, this.imgHeight * scale)
return canvas
}
public getBlob (scale: number = 1) {
return new Promise<Blob>(resolve => {
this.getCanvas(scale).toBlob((blob) => { resolve(blob) })
})
}
public getDataUrl (scale: number = 1) {
return this.getCanvas(scale).toDataURL()
}
public download (scale: number = 1) {
const link = document.createElement('a')
link.download = 'canvas.png'
link.href = this.getDataUrl(scale)
link.click()
}
public getCropInfo () {
return ({
originalWidth: this.img.naturalWidth,
originalHeight: this.img.naturalHeight,
imgWidth: this.imgWidth,
imgHeight: this.imgHeight,
x: this.netPanningX,
y: this.netPanningY
})
}
}
const cr = new Cropo({})
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead. (example: cont cr= new Cropo(config); cr.download();)
*/
export function download (...arg: Parameters<typeof cr.download>) { cr.download(...arg) };
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead. (example: new Cropo(config))
*/
export function loadCanvas (...arg: Parameters<typeof cr.loadCanvas>) { cr.loadCanvas(...arg) };
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead. (example: cont cr= new Cropo(config); cr.loadImageFromUrl();)
*/
export function loadImageFromUrl (...arg: Parameters<typeof cr.loadImageFromUrl>) { cr.loadImageFromUrl(...arg) };
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead. (example: new Cropo(config))
*/
export function loadSlider (...arg: Parameters<typeof cr.loadSlider>) { cr.loadSlider(...arg) };
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead. (example: cont cr= new Cropo(config); cr.move();)
*/
export function move (...arg: Parameters<typeof cr.move>) { cr.move(...arg) };
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead.(example: cont cr= new Cropo(config); cr.getCropInfo();)
*/
export function getCropInfo (...arg: Parameters<typeof cr.getCropInfo>) { cr.getCropInfo(...arg) };
/**
* @deprecated Since version 0.6. Will be deleted in version 1.0. Use Cropo instance instead. (example: cont cr= new Cropo(config); cr.getDataUrl();)
*/
export function getDataUrl (...arg: Parameters<typeof cr.getDataUrl>) { cr.getDataUrl(...arg) };