@anu3ev/fabric-image-editor 0.1.12 → 0.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/worker.js DELETED
@@ -1,68 +0,0 @@
1
- /* eslint-disable no-restricted-globals */
2
-
3
- self.onmessage = async(e) => {
4
- const { action, payload, requestId } = e.data
5
-
6
- try {
7
- switch (action) {
8
- case 'resizeImage': {
9
- const { dataURL, maxWidth, maxHeight, sizeType } = payload
10
- const imgBitmap = await createImageBitmap(await (await fetch(dataURL)).blob())
11
-
12
- // вычисляем новый размер
13
- let { width, height } = imgBitmap
14
- let ratio = Math.min(maxWidth / width, maxHeight / height)
15
-
16
- if (sizeType === 'min') {
17
- ratio = Math.max(maxWidth / width, maxHeight / height)
18
- }
19
-
20
- width = Math.floor(width * ratio)
21
- height = Math.floor(height * ratio)
22
-
23
- // рисуем изображение в offscreen
24
- const offscreen = new OffscreenCanvas(width, height)
25
- const ctx = offscreen.getContext('2d')
26
- ctx.drawImage(imgBitmap, 0, 0, width, height)
27
-
28
- // конвертим обратно в dataURL
29
- const resizedBlob = await offscreen.convertToBlob()
30
-
31
- self.postMessage({ requestId, action, success: true, data: resizedBlob })
32
- break
33
- }
34
-
35
- case 'toDataURL': {
36
- const { bitmap, format, quality, returnBlob } = payload
37
- const { width, height } = bitmap
38
-
39
- // рисуем изображение в offscreen
40
- const off = new OffscreenCanvas(bitmap.width, bitmap.height)
41
- const ctx = off.getContext('2d')
42
- ctx.drawImage(bitmap, 0, 0, width, height)
43
-
44
- // конвертируем в blob, а затем в dataURL
45
- const blob = await off.convertToBlob({ type: format, quality })
46
-
47
- if (returnBlob) {
48
- self.postMessage({ requestId, action, success: true, data: blob })
49
- break
50
- }
51
-
52
- const dataURL = await new Promise((res) => {
53
- const r = new FileReader()
54
- r.onload = () => res(r.result)
55
- r.readAsDataURL(blob)
56
- })
57
-
58
- self.postMessage({ requestId, action, success: true, data: dataURL })
59
- break
60
- }
61
-
62
- default:
63
- throw new Error(`Unknown action ${action}`)
64
- }
65
- } catch (err) {
66
- self.postMessage({ requestId, action, success: false, error: err.message })
67
- }
68
- }