@anu3ev/fabric-image-editor 0.1.0 → 0.1.1

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.
Files changed (3) hide show
  1. package/dist/main.js +474 -473
  2. package/dist/worker.js +68 -0
  3. package/package.json +2 -2
package/dist/worker.js ADDED
@@ -0,0 +1,68 @@
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anu3ev/fabric-image-editor",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "JavaScript image editor built on FabricJS, allowing you to create instances with an integrated montage area and providing an API to modify and manage state.",
5
5
  "module": "dist/main.js",
6
6
  "files": [
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "repository": {
42
42
  "type": "git",
43
- "url": "https://github.com/Anu3ev/image-editor.git"
43
+ "url": "git+https://github.com/Anu3ev/image-editor.git"
44
44
  },
45
45
  "license": "MIT"
46
46
  }