@hprint/plugins 0.0.1-alpha.1 → 0.0.1-alpha.3

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 (64) hide show
  1. package/dist/index.js +122 -117
  2. package/dist/index.mjs +41257 -21115
  3. package/dist/src/plugins/AlignGuidLinePlugin.d.ts +7 -2
  4. package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -1
  5. package/dist/src/plugins/BarCodePlugin.d.ts +4 -0
  6. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -1
  7. package/dist/src/plugins/CreateElementPlugin.d.ts +3 -9
  8. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
  9. package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -1
  10. package/dist/src/plugins/LockPlugin.d.ts.map +1 -1
  11. package/dist/src/plugins/QrCodePlugin.d.ts +19 -97
  12. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
  13. package/dist/src/plugins/ResizePlugin.d.ts.map +1 -1
  14. package/dist/src/plugins/UnitPlugin.d.ts.map +1 -1
  15. package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -1
  16. package/dist/src/utils/units.d.ts.map +1 -1
  17. package/package.json +5 -4
  18. package/src/assets/style/resizePlugin.css +27 -27
  19. package/src/objects/Arrow.js +47 -47
  20. package/src/objects/ThinTailArrow.js +50 -50
  21. package/src/plugins/AlignGuidLinePlugin.ts +1152 -1141
  22. package/src/plugins/BarCodePlugin.ts +33 -27
  23. package/src/plugins/ControlsPlugin.ts +251 -251
  24. package/src/plugins/ControlsRotatePlugin.ts +111 -111
  25. package/src/plugins/CopyPlugin.ts +255 -255
  26. package/src/plugins/CreateElementPlugin.ts +14 -10
  27. package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
  28. package/src/plugins/DrawLinePlugin.ts +162 -162
  29. package/src/plugins/DrawPolygonPlugin.ts +205 -205
  30. package/src/plugins/DringPlugin.ts +125 -125
  31. package/src/plugins/FlipPlugin.ts +59 -59
  32. package/src/plugins/FontPlugin.ts +165 -165
  33. package/src/plugins/FreeDrawPlugin.ts +49 -49
  34. package/src/plugins/GroupAlignPlugin.ts +365 -365
  35. package/src/plugins/GroupPlugin.ts +82 -82
  36. package/src/plugins/GroupTextEditorPlugin.ts +198 -198
  37. package/src/plugins/HistoryPlugin.ts +181 -181
  38. package/src/plugins/ImageStroke.ts +121 -121
  39. package/src/plugins/LayerPlugin.ts +108 -108
  40. package/src/plugins/LockPlugin.ts +242 -240
  41. package/src/plugins/MaskPlugin.ts +155 -155
  42. package/src/plugins/MaterialPlugin.ts +224 -224
  43. package/src/plugins/MiddleMousePlugin.ts +45 -45
  44. package/src/plugins/MoveHotKeyPlugin.ts +46 -46
  45. package/src/plugins/PathTextPlugin.ts +89 -89
  46. package/src/plugins/PolygonModifyPlugin.ts +224 -224
  47. package/src/plugins/PrintPlugin.ts +81 -81
  48. package/src/plugins/PsdPlugin.ts +52 -52
  49. package/src/plugins/QrCodePlugin.ts +322 -393
  50. package/src/plugins/ResizePlugin.ts +278 -274
  51. package/src/plugins/RulerPlugin.ts +78 -78
  52. package/src/plugins/SimpleClipImagePlugin.ts +244 -244
  53. package/src/plugins/UnitPlugin.ts +326 -327
  54. package/src/plugins/WaterMarkPlugin.ts +257 -257
  55. package/src/plugins/WorkspacePlugin.ts +10 -6
  56. package/src/types/eventType.ts +11 -11
  57. package/src/utils/psd.js +432 -432
  58. package/src/utils/ruler/guideline.ts +145 -145
  59. package/src/utils/ruler/index.ts +91 -91
  60. package/src/utils/ruler/ruler.ts +924 -924
  61. package/src/utils/ruler/utils.ts +162 -162
  62. package/src/utils/units.ts +4 -2
  63. package/tsconfig.json +10 -10
  64. package/vite.config.ts +29 -29
@@ -1,78 +1,78 @@
1
- import { fabric } from '@hprint/core';
2
- import type { IEditor, IPluginTempl } from '@hprint/core';
3
-
4
- type IPlugin = Pick<
5
- RulerPlugin,
6
- 'hideGuideline' | 'showGuideline' | 'rulerEnable' | 'rulerDisable'
7
- >;
8
-
9
- declare module '@hprint/core' {
10
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
11
- interface IEditor extends IPlugin {}
12
- }
13
-
14
- import initRuler from '../utils/ruler';
15
-
16
- class RulerPlugin implements IPluginTempl {
17
- static pluginName = 'RulerPlugin';
18
- // static events = ['sizeChange'];
19
- static apis = [
20
- 'hideGuideline',
21
- 'showGuideline',
22
- 'rulerEnable',
23
- 'rulerDisable',
24
- ];
25
- ruler: any;
26
- constructor(
27
- public canvas: fabric.Canvas,
28
- public editor: IEditor
29
- ) {
30
- this.init();
31
- }
32
-
33
- hookSaveBefore() {
34
- return new Promise((resolve) => {
35
- this.hideGuideline();
36
- resolve(true);
37
- });
38
- }
39
-
40
- hookSaveAfter() {
41
- return new Promise((resolve) => {
42
- this.showGuideline();
43
- resolve(true);
44
- });
45
- }
46
-
47
- init() {
48
- this.ruler = initRuler(this.canvas, this.editor, {
49
- unit: (this.editor.getUnit?.() as 'px' | 'mm') || 'px',
50
- });
51
- // 监听全局单位变化,更新标尺展示
52
- this.editor.on?.('unitChange', (unit: 'px' | 'mm') => {
53
- this.ruler?.setUnit?.(unit);
54
- });
55
- }
56
-
57
- hideGuideline() {
58
- this.ruler.hideGuideline();
59
- }
60
-
61
- showGuideline() {
62
- this.ruler.showGuideline();
63
- }
64
-
65
- rulerEnable() {
66
- this.ruler.enable();
67
- }
68
-
69
- rulerDisable() {
70
- this.ruler.disable();
71
- }
72
-
73
- destroy() {
74
- console.log('pluginDestroy');
75
- }
76
- }
77
-
78
- export default RulerPlugin;
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+
4
+ type IPlugin = Pick<
5
+ RulerPlugin,
6
+ 'hideGuideline' | 'showGuideline' | 'rulerEnable' | 'rulerDisable'
7
+ >;
8
+
9
+ declare module '@hprint/core' {
10
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
11
+ interface IEditor extends IPlugin {}
12
+ }
13
+
14
+ import initRuler from '../utils/ruler';
15
+
16
+ class RulerPlugin implements IPluginTempl {
17
+ static pluginName = 'RulerPlugin';
18
+ // static events = ['sizeChange'];
19
+ static apis = [
20
+ 'hideGuideline',
21
+ 'showGuideline',
22
+ 'rulerEnable',
23
+ 'rulerDisable',
24
+ ];
25
+ ruler: any;
26
+ constructor(
27
+ public canvas: fabric.Canvas,
28
+ public editor: IEditor
29
+ ) {
30
+ this.init();
31
+ }
32
+
33
+ hookSaveBefore() {
34
+ return new Promise((resolve) => {
35
+ this.hideGuideline();
36
+ resolve(true);
37
+ });
38
+ }
39
+
40
+ hookSaveAfter() {
41
+ return new Promise((resolve) => {
42
+ this.showGuideline();
43
+ resolve(true);
44
+ });
45
+ }
46
+
47
+ init() {
48
+ this.ruler = initRuler(this.canvas, this.editor, {
49
+ unit: (this.editor.getUnit?.() as 'px' | 'mm') || 'px',
50
+ });
51
+ // 监听全局单位变化,更新标尺展示
52
+ this.editor.on?.('unitChange', (unit: 'px' | 'mm') => {
53
+ this.ruler?.setUnit?.(unit);
54
+ });
55
+ }
56
+
57
+ hideGuideline() {
58
+ this.ruler.hideGuideline();
59
+ }
60
+
61
+ showGuideline() {
62
+ this.ruler.showGuideline();
63
+ }
64
+
65
+ rulerEnable() {
66
+ this.ruler.enable();
67
+ }
68
+
69
+ rulerDisable() {
70
+ this.ruler.disable();
71
+ }
72
+
73
+ destroy() {
74
+ console.log('pluginDestroy');
75
+ }
76
+ }
77
+
78
+ export default RulerPlugin;
@@ -1,244 +1,244 @@
1
- import { fabric } from '@hprint/core';
2
- import { mathHelper } from '@hprint/shared';
3
- import { get, set } from 'lodash-es';
4
- import type { IEditor, IPluginTempl } from '@hprint/core';
5
-
6
- type IPlugin = Pick<SimpleClipImagePlugin, 'addClipPathToImage' | 'removeClip'>;
7
-
8
- declare module '@hprint/core' {
9
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
- interface IEditor extends IPlugin {}
11
- }
12
-
13
- const getBounds = (activeObject: fabric.Object) => {
14
- const { left = 0, top = 0 } = activeObject;
15
- return {
16
- width: activeObject.getScaledWidth(),
17
- height: activeObject.getScaledHeight(),
18
- left,
19
- top,
20
- };
21
- };
22
- const bindInfo = (shell: fabric.Object, activeObject: fabric.Object) => {
23
- bindFlagToObject(shell);
24
- bindFlagToObject(shell, 'targetId', get(activeObject, 'id'));
25
- bindFlagToObject(shell, 'targetType', get(activeObject, 'type'));
26
- };
27
- const bindFlagToObject = (
28
- activeObject: fabric.Object,
29
- key = 'clip',
30
- value: any = true
31
- ) => {
32
- set(activeObject, key, value);
33
- };
34
- const createRectClip = (activeObject: fabric.Object, inverted: boolean) => {
35
- const {
36
- width = 0,
37
- height = 0,
38
- left = 0,
39
- top = 0,
40
- } = getBounds(activeObject);
41
- const clipW = Math.round(width / 2);
42
- const clipH = Math.round(height / 2);
43
- const shell = new fabric.Rect({
44
- width: clipW,
45
- height: clipH,
46
- fill: 'rgba(0,0,0,0)',
47
- originX: 'center',
48
- originY: 'center',
49
- left: left + width / 2,
50
- top: top + height / 2,
51
- });
52
- bindInfo(shell, activeObject);
53
- const clipPath = new fabric.Rect({
54
- absolutePositioned: true,
55
- width: shell.width,
56
- height: shell.height,
57
- originX: 'center',
58
- originY: 'center',
59
- left: shell.left,
60
- top: shell.top,
61
- inverted: inverted,
62
- });
63
- return { clipPath, shell };
64
- };
65
- const createCircleClip = (activeObject: fabric.Object, inverted: boolean) => {
66
- const point = activeObject.getCenterPoint();
67
- const { width } = getBounds(activeObject);
68
- const shell = new fabric.Ellipse({
69
- fill: 'rgba(0,0,0,0)',
70
- originX: 'center',
71
- originY: 'center',
72
- left: point.x,
73
- top: point.y,
74
- rx: width / 4,
75
- ry: width / 4,
76
- });
77
- bindInfo(shell, activeObject);
78
- const clipPath = new fabric.Ellipse({
79
- absolutePositioned: true,
80
- originX: 'center',
81
- originY: 'center',
82
- left: shell.left,
83
- top: shell.top,
84
- inverted: inverted,
85
- rx: shell.rx,
86
- ry: shell.ry,
87
- });
88
- return { shell, clipPath };
89
- };
90
- const createTriClip = (activeObject: fabric.Object, inverted: boolean) => {
91
- const point = activeObject.getCenterPoint();
92
- const { width = 0, height = 0 } = getBounds(activeObject);
93
- const clipW = Math.round(width / 2);
94
- const clipH = Math.round(height / 2);
95
- const shell = new fabric.Triangle({
96
- fill: 'rgba(0,0,0,0)',
97
- originX: 'center',
98
- originY: 'center',
99
- left: point.x,
100
- top: point.y,
101
- width: clipW,
102
- height: clipH,
103
- });
104
- bindInfo(shell, activeObject);
105
- const clipPath = new fabric.Triangle({
106
- absolutePositioned: true,
107
- originX: 'center',
108
- originY: 'center',
109
- left: shell.left,
110
- top: shell.top,
111
- width: shell.width,
112
- height: shell.height,
113
- inverted: inverted,
114
- });
115
- return { shell, clipPath };
116
- };
117
- const createPolygonClip = (activeObject: fabric.Object, inverted: boolean) => {
118
- const point = activeObject.getCenterPoint();
119
- const points = mathHelper.getPolygonVertices(5, 200);
120
- const shell = new fabric.Polygon(points, {
121
- fill: 'rgba(0,0,0,0)',
122
- originY: 'center',
123
- originX: 'center',
124
- left: point.x,
125
- top: point.y,
126
- });
127
- bindInfo(shell, activeObject);
128
- const clipPath = new fabric.Polygon([...points], {
129
- absolutePositioned: true,
130
- originX: 'center',
131
- originY: 'center',
132
- left: shell.left,
133
- top: shell.top,
134
- inverted: inverted,
135
- });
136
- return { shell, clipPath };
137
- };
138
- export default class SimpleClipImagePlugin implements IPluginTempl {
139
- static pluginName = 'SimpleClipImagePlugin';
140
- // static events = ['sizeChange'];
141
- static apis = ['addClipPathToImage', 'removeClip'];
142
- constructor(
143
- public canvas: fabric.Canvas,
144
- public editor: IEditor
145
- ) {}
146
- addClipPathToImage(value: string) {
147
- const activeObject = this.canvas.getActiveObjects()[0];
148
- if (activeObject && activeObject.type === 'image') {
149
- let clip: { shell: fabric.Object; clipPath: fabric.Object } | null =
150
- null;
151
- const [name, inverted] = value.split('-');
152
- const isInverted = !!inverted;
153
- switch (name) {
154
- case 'polygon':
155
- clip = createPolygonClip(activeObject, isInverted);
156
- break;
157
- case 'rect':
158
- clip = createRectClip(activeObject, isInverted);
159
- break;
160
- case 'circle':
161
- clip = createCircleClip(activeObject, isInverted);
162
- break;
163
- case 'triangle':
164
- clip = createTriClip(activeObject, isInverted);
165
- break;
166
- }
167
- if (clip == null) return;
168
- const { shell, clipPath } = clip;
169
- shell.on('moving', () => {
170
- clipPath.setPositionByOrigin(
171
- shell.getCenterPoint(),
172
- 'center',
173
- 'center'
174
- );
175
- activeObject.set('dirty', true);
176
- });
177
- shell.on('rotating', () => {
178
- clipPath.set({ angle: shell.angle });
179
- activeObject.set('dirty', true);
180
- });
181
- shell.on('scaling', () => {
182
- clipPath.set({ scaleX: shell.scaleX, scaleY: shell.scaleY });
183
- clipPath.setPositionByOrigin(
184
- shell.getCenterPoint(),
185
- 'center',
186
- 'center'
187
- );
188
- activeObject.set('dirty', true);
189
- });
190
- shell.on('deselected', () => {
191
- if (
192
- clipPath instanceof fabric.Ellipse &&
193
- shell instanceof fabric.Ellipse
194
- ) {
195
- clipPath.set({ rx: shell.getRx(), ry: shell.getRy() });
196
- this.correctPosition(activeObject, shell, clipPath);
197
- } else if (shell instanceof fabric.Polygon) {
198
- this.correctPosition(activeObject, shell, clipPath);
199
- const { scaleX: cSx = 1, scaleY: cSy = 1 } = clipPath;
200
- const { scaleX: sSx = 1, scaleY: sSy = 1 } = shell;
201
- clipPath.set('scaleX', cSx * sSx);
202
- clipPath.set('scaleY', cSy * sSy);
203
- } else {
204
- this.correctPosition(activeObject, shell, clipPath);
205
- clipPath.set('width', shell.getScaledWidth());
206
- clipPath.set('height', shell.getScaledHeight());
207
- }
208
- activeObject.set('dirty', true);
209
- this.canvas.remove(shell);
210
- this.canvas.requestRenderAll();
211
- });
212
- activeObject.set({ clipPath: clipPath });
213
- this.canvas.add(shell);
214
- this.canvas.setActiveObject(shell);
215
- }
216
- }
217
- correctPosition(
218
- activeObject: fabric.Object,
219
- shell: fabric.Object,
220
- clipPath: fabric.Object
221
- ) {
222
- const position = activeObject.toLocalPoint(
223
- shell.getCenterPoint(),
224
- 'center',
225
- 'center'
226
- );
227
- const { scaleX = 1, scaleY = 1 } = activeObject;
228
- clipPath.set({
229
- absolutePositioned: false,
230
- left: position.x / scaleX,
231
- top: position.y / scaleY,
232
- scaleX: 1 / scaleX,
233
- scaleY: 1 / scaleY,
234
- });
235
- }
236
- removeClip() {
237
- const activeObject = this.canvas.getActiveObjects()[0];
238
- if (activeObject && activeObject.type === 'image') {
239
- activeObject.set({ clipPath: undefined });
240
- activeObject.set('dirty', true);
241
- this.canvas.requestRenderAll();
242
- }
243
- }
244
- }
1
+ import { fabric } from '@hprint/core';
2
+ import { mathHelper } from '@hprint/shared';
3
+ import { get, set } from 'lodash-es';
4
+ import type { IEditor, IPluginTempl } from '@hprint/core';
5
+
6
+ type IPlugin = Pick<SimpleClipImagePlugin, 'addClipPathToImage' | 'removeClip'>;
7
+
8
+ declare module '@hprint/core' {
9
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
+ interface IEditor extends IPlugin {}
11
+ }
12
+
13
+ const getBounds = (activeObject: fabric.Object) => {
14
+ const { left = 0, top = 0 } = activeObject;
15
+ return {
16
+ width: activeObject.getScaledWidth(),
17
+ height: activeObject.getScaledHeight(),
18
+ left,
19
+ top,
20
+ };
21
+ };
22
+ const bindInfo = (shell: fabric.Object, activeObject: fabric.Object) => {
23
+ bindFlagToObject(shell);
24
+ bindFlagToObject(shell, 'targetId', get(activeObject, 'id'));
25
+ bindFlagToObject(shell, 'targetType', get(activeObject, 'type'));
26
+ };
27
+ const bindFlagToObject = (
28
+ activeObject: fabric.Object,
29
+ key = 'clip',
30
+ value: any = true
31
+ ) => {
32
+ set(activeObject, key, value);
33
+ };
34
+ const createRectClip = (activeObject: fabric.Object, inverted: boolean) => {
35
+ const {
36
+ width = 0,
37
+ height = 0,
38
+ left = 0,
39
+ top = 0,
40
+ } = getBounds(activeObject);
41
+ const clipW = Math.round(width / 2);
42
+ const clipH = Math.round(height / 2);
43
+ const shell = new fabric.Rect({
44
+ width: clipW,
45
+ height: clipH,
46
+ fill: 'rgba(0,0,0,0)',
47
+ originX: 'center',
48
+ originY: 'center',
49
+ left: left + width / 2,
50
+ top: top + height / 2,
51
+ });
52
+ bindInfo(shell, activeObject);
53
+ const clipPath = new fabric.Rect({
54
+ absolutePositioned: true,
55
+ width: shell.width,
56
+ height: shell.height,
57
+ originX: 'center',
58
+ originY: 'center',
59
+ left: shell.left,
60
+ top: shell.top,
61
+ inverted: inverted,
62
+ });
63
+ return { clipPath, shell };
64
+ };
65
+ const createCircleClip = (activeObject: fabric.Object, inverted: boolean) => {
66
+ const point = activeObject.getCenterPoint();
67
+ const { width } = getBounds(activeObject);
68
+ const shell = new fabric.Ellipse({
69
+ fill: 'rgba(0,0,0,0)',
70
+ originX: 'center',
71
+ originY: 'center',
72
+ left: point.x,
73
+ top: point.y,
74
+ rx: width / 4,
75
+ ry: width / 4,
76
+ });
77
+ bindInfo(shell, activeObject);
78
+ const clipPath = new fabric.Ellipse({
79
+ absolutePositioned: true,
80
+ originX: 'center',
81
+ originY: 'center',
82
+ left: shell.left,
83
+ top: shell.top,
84
+ inverted: inverted,
85
+ rx: shell.rx,
86
+ ry: shell.ry,
87
+ });
88
+ return { shell, clipPath };
89
+ };
90
+ const createTriClip = (activeObject: fabric.Object, inverted: boolean) => {
91
+ const point = activeObject.getCenterPoint();
92
+ const { width = 0, height = 0 } = getBounds(activeObject);
93
+ const clipW = Math.round(width / 2);
94
+ const clipH = Math.round(height / 2);
95
+ const shell = new fabric.Triangle({
96
+ fill: 'rgba(0,0,0,0)',
97
+ originX: 'center',
98
+ originY: 'center',
99
+ left: point.x,
100
+ top: point.y,
101
+ width: clipW,
102
+ height: clipH,
103
+ });
104
+ bindInfo(shell, activeObject);
105
+ const clipPath = new fabric.Triangle({
106
+ absolutePositioned: true,
107
+ originX: 'center',
108
+ originY: 'center',
109
+ left: shell.left,
110
+ top: shell.top,
111
+ width: shell.width,
112
+ height: shell.height,
113
+ inverted: inverted,
114
+ });
115
+ return { shell, clipPath };
116
+ };
117
+ const createPolygonClip = (activeObject: fabric.Object, inverted: boolean) => {
118
+ const point = activeObject.getCenterPoint();
119
+ const points = mathHelper.getPolygonVertices(5, 200);
120
+ const shell = new fabric.Polygon(points, {
121
+ fill: 'rgba(0,0,0,0)',
122
+ originY: 'center',
123
+ originX: 'center',
124
+ left: point.x,
125
+ top: point.y,
126
+ });
127
+ bindInfo(shell, activeObject);
128
+ const clipPath = new fabric.Polygon([...points], {
129
+ absolutePositioned: true,
130
+ originX: 'center',
131
+ originY: 'center',
132
+ left: shell.left,
133
+ top: shell.top,
134
+ inverted: inverted,
135
+ });
136
+ return { shell, clipPath };
137
+ };
138
+ export default class SimpleClipImagePlugin implements IPluginTempl {
139
+ static pluginName = 'SimpleClipImagePlugin';
140
+ // static events = ['sizeChange'];
141
+ static apis = ['addClipPathToImage', 'removeClip'];
142
+ constructor(
143
+ public canvas: fabric.Canvas,
144
+ public editor: IEditor
145
+ ) {}
146
+ addClipPathToImage(value: string) {
147
+ const activeObject = this.canvas.getActiveObjects()[0];
148
+ if (activeObject && activeObject.type === 'image') {
149
+ let clip: { shell: fabric.Object; clipPath: fabric.Object } | null =
150
+ null;
151
+ const [name, inverted] = value.split('-');
152
+ const isInverted = !!inverted;
153
+ switch (name) {
154
+ case 'polygon':
155
+ clip = createPolygonClip(activeObject, isInverted);
156
+ break;
157
+ case 'rect':
158
+ clip = createRectClip(activeObject, isInverted);
159
+ break;
160
+ case 'circle':
161
+ clip = createCircleClip(activeObject, isInverted);
162
+ break;
163
+ case 'triangle':
164
+ clip = createTriClip(activeObject, isInverted);
165
+ break;
166
+ }
167
+ if (clip == null) return;
168
+ const { shell, clipPath } = clip;
169
+ shell.on('moving', () => {
170
+ clipPath.setPositionByOrigin(
171
+ shell.getCenterPoint(),
172
+ 'center',
173
+ 'center'
174
+ );
175
+ activeObject.set('dirty', true);
176
+ });
177
+ shell.on('rotating', () => {
178
+ clipPath.set({ angle: shell.angle });
179
+ activeObject.set('dirty', true);
180
+ });
181
+ shell.on('scaling', () => {
182
+ clipPath.set({ scaleX: shell.scaleX, scaleY: shell.scaleY });
183
+ clipPath.setPositionByOrigin(
184
+ shell.getCenterPoint(),
185
+ 'center',
186
+ 'center'
187
+ );
188
+ activeObject.set('dirty', true);
189
+ });
190
+ shell.on('deselected', () => {
191
+ if (
192
+ clipPath instanceof fabric.Ellipse &&
193
+ shell instanceof fabric.Ellipse
194
+ ) {
195
+ clipPath.set({ rx: shell.getRx(), ry: shell.getRy() });
196
+ this.correctPosition(activeObject, shell, clipPath);
197
+ } else if (shell instanceof fabric.Polygon) {
198
+ this.correctPosition(activeObject, shell, clipPath);
199
+ const { scaleX: cSx = 1, scaleY: cSy = 1 } = clipPath;
200
+ const { scaleX: sSx = 1, scaleY: sSy = 1 } = shell;
201
+ clipPath.set('scaleX', cSx * sSx);
202
+ clipPath.set('scaleY', cSy * sSy);
203
+ } else {
204
+ this.correctPosition(activeObject, shell, clipPath);
205
+ clipPath.set('width', shell.getScaledWidth());
206
+ clipPath.set('height', shell.getScaledHeight());
207
+ }
208
+ activeObject.set('dirty', true);
209
+ this.canvas.remove(shell);
210
+ this.canvas.requestRenderAll();
211
+ });
212
+ activeObject.set({ clipPath: clipPath });
213
+ this.canvas.add(shell);
214
+ this.canvas.setActiveObject(shell);
215
+ }
216
+ }
217
+ correctPosition(
218
+ activeObject: fabric.Object,
219
+ shell: fabric.Object,
220
+ clipPath: fabric.Object
221
+ ) {
222
+ const position = activeObject.toLocalPoint(
223
+ shell.getCenterPoint(),
224
+ 'center',
225
+ 'center'
226
+ );
227
+ const { scaleX = 1, scaleY = 1 } = activeObject;
228
+ clipPath.set({
229
+ absolutePositioned: false,
230
+ left: position.x / scaleX,
231
+ top: position.y / scaleY,
232
+ scaleX: 1 / scaleX,
233
+ scaleY: 1 / scaleY,
234
+ });
235
+ }
236
+ removeClip() {
237
+ const activeObject = this.canvas.getActiveObjects()[0];
238
+ if (activeObject && activeObject.type === 'image') {
239
+ activeObject.set({ clipPath: undefined });
240
+ activeObject.set('dirty', true);
241
+ this.canvas.requestRenderAll();
242
+ }
243
+ }
244
+ }