@hprint/plugins 0.0.1-alpha.0

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 (153) hide show
  1. package/dist/index.css +1 -0
  2. package/dist/index.js +478 -0
  3. package/dist/index.mjs +41731 -0
  4. package/dist/src/index.d.ts +8 -0
  5. package/dist/src/index.d.ts.map +1 -0
  6. package/dist/src/objects/Arrow.d.ts +2 -0
  7. package/dist/src/objects/Arrow.d.ts.map +1 -0
  8. package/dist/src/objects/ThinTailArrow.d.ts +2 -0
  9. package/dist/src/objects/ThinTailArrow.d.ts.map +1 -0
  10. package/dist/src/plugins/AddBaseTypePlugin.d.ts +26 -0
  11. package/dist/src/plugins/AddBaseTypePlugin.d.ts.map +1 -0
  12. package/dist/src/plugins/AlignGuidLinePlugin.d.ts +16 -0
  13. package/dist/src/plugins/AlignGuidLinePlugin.d.ts.map +1 -0
  14. package/dist/src/plugins/BarCodePlugin.d.ts +68 -0
  15. package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -0
  16. package/dist/src/plugins/CenterAlignPlugin.d.ts +29 -0
  17. package/dist/src/plugins/CenterAlignPlugin.d.ts.map +1 -0
  18. package/dist/src/plugins/ControlsPlugin.d.ts +11 -0
  19. package/dist/src/plugins/ControlsPlugin.d.ts.map +1 -0
  20. package/dist/src/plugins/ControlsRotatePlugin.d.ts +11 -0
  21. package/dist/src/plugins/ControlsRotatePlugin.d.ts.map +1 -0
  22. package/dist/src/plugins/CopyPlugin.d.ts +30 -0
  23. package/dist/src/plugins/CopyPlugin.d.ts.map +1 -0
  24. package/dist/src/plugins/CreateElementPlugin.d.ts +121 -0
  25. package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -0
  26. package/dist/src/plugins/DeleteHotKeyPlugin.d.ts +25 -0
  27. package/dist/src/plugins/DeleteHotKeyPlugin.d.ts.map +1 -0
  28. package/dist/src/plugins/DrawLinePlugin.d.ts +26 -0
  29. package/dist/src/plugins/DrawLinePlugin.d.ts.map +1 -0
  30. package/dist/src/plugins/DrawPolygonPlugin.d.ts +41 -0
  31. package/dist/src/plugins/DrawPolygonPlugin.d.ts.map +1 -0
  32. package/dist/src/plugins/DringPlugin.d.ts +33 -0
  33. package/dist/src/plugins/DringPlugin.d.ts.map +1 -0
  34. package/dist/src/plugins/FlipPlugin.d.ts +26 -0
  35. package/dist/src/plugins/FlipPlugin.d.ts.map +1 -0
  36. package/dist/src/plugins/FontPlugin.d.ts +33 -0
  37. package/dist/src/plugins/FontPlugin.d.ts.map +1 -0
  38. package/dist/src/plugins/FreeDrawPlugin.d.ts +23 -0
  39. package/dist/src/plugins/FreeDrawPlugin.d.ts.map +1 -0
  40. package/dist/src/plugins/GroupAlignPlugin.d.ts +24 -0
  41. package/dist/src/plugins/GroupAlignPlugin.d.ts.map +1 -0
  42. package/dist/src/plugins/GroupPlugin.d.ts +24 -0
  43. package/dist/src/plugins/GroupPlugin.d.ts.map +1 -0
  44. package/dist/src/plugins/GroupTextEditorPlugin.d.ts +18 -0
  45. package/dist/src/plugins/GroupTextEditorPlugin.d.ts.map +1 -0
  46. package/dist/src/plugins/HistoryPlugin.d.ts +30 -0
  47. package/dist/src/plugins/HistoryPlugin.d.ts.map +1 -0
  48. package/dist/src/plugins/ImageStroke.d.ts +18 -0
  49. package/dist/src/plugins/ImageStroke.d.ts.map +1 -0
  50. package/dist/src/plugins/LayerPlugin.d.ts +31 -0
  51. package/dist/src/plugins/LayerPlugin.d.ts.map +1 -0
  52. package/dist/src/plugins/LockPlugin.d.ts +27 -0
  53. package/dist/src/plugins/LockPlugin.d.ts.map +1 -0
  54. package/dist/src/plugins/MaskPlugin.d.ts +38 -0
  55. package/dist/src/plugins/MaskPlugin.d.ts.map +1 -0
  56. package/dist/src/plugins/MaterialPlugin.d.ts +45 -0
  57. package/dist/src/plugins/MaterialPlugin.d.ts.map +1 -0
  58. package/dist/src/plugins/MiddleMousePlugin.d.ts +18 -0
  59. package/dist/src/plugins/MiddleMousePlugin.d.ts.map +1 -0
  60. package/dist/src/plugins/MoveHotKeyPlugin.d.ts +12 -0
  61. package/dist/src/plugins/MoveHotKeyPlugin.d.ts.map +1 -0
  62. package/dist/src/plugins/PathTextPlugin.d.ts +30 -0
  63. package/dist/src/plugins/PathTextPlugin.d.ts.map +1 -0
  64. package/dist/src/plugins/PolygonModifyPlugin.d.ts +28 -0
  65. package/dist/src/plugins/PolygonModifyPlugin.d.ts.map +1 -0
  66. package/dist/src/plugins/PrintPlugin.d.ts +39 -0
  67. package/dist/src/plugins/PrintPlugin.d.ts.map +1 -0
  68. package/dist/src/plugins/PsdPlugin.d.ts +17 -0
  69. package/dist/src/plugins/PsdPlugin.d.ts.map +1 -0
  70. package/dist/src/plugins/QrCodePlugin.d.ts +137 -0
  71. package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -0
  72. package/dist/src/plugins/ResizePlugin.d.ts +44 -0
  73. package/dist/src/plugins/ResizePlugin.d.ts.map +1 -0
  74. package/dist/src/plugins/RulerPlugin.d.ts +24 -0
  75. package/dist/src/plugins/RulerPlugin.d.ts.map +1 -0
  76. package/dist/src/plugins/SimpleClipImagePlugin.d.ts +18 -0
  77. package/dist/src/plugins/SimpleClipImagePlugin.d.ts.map +1 -0
  78. package/dist/src/plugins/UnitPlugin.d.ts +84 -0
  79. package/dist/src/plugins/UnitPlugin.d.ts.map +1 -0
  80. package/dist/src/plugins/WaterMarkPlugin.d.ts +40 -0
  81. package/dist/src/plugins/WaterMarkPlugin.d.ts.map +1 -0
  82. package/dist/src/plugins/WorkspacePlugin.d.ts +57 -0
  83. package/dist/src/plugins/WorkspacePlugin.d.ts.map +1 -0
  84. package/dist/src/types/eventType.d.ts +11 -0
  85. package/dist/src/types/eventType.d.ts.map +1 -0
  86. package/dist/src/utils/psd.d.ts +3 -0
  87. package/dist/src/utils/psd.d.ts.map +1 -0
  88. package/dist/src/utils/ruler/guideline.d.ts +4 -0
  89. package/dist/src/utils/ruler/guideline.d.ts.map +1 -0
  90. package/dist/src/utils/ruler/index.d.ts +5 -0
  91. package/dist/src/utils/ruler/index.d.ts.map +1 -0
  92. package/dist/src/utils/ruler/ruler.d.ts +147 -0
  93. package/dist/src/utils/ruler/ruler.d.ts.map +1 -0
  94. package/dist/src/utils/ruler/utils.d.ts +50 -0
  95. package/dist/src/utils/ruler/utils.d.ts.map +1 -0
  96. package/dist/src/utils/units.d.ts +22 -0
  97. package/dist/src/utils/units.d.ts.map +1 -0
  98. package/package.json +51 -0
  99. package/src/assets/edgecontrol.svg +17 -0
  100. package/src/assets/lock.svg +7 -0
  101. package/src/assets/middlecontrol.svg +17 -0
  102. package/src/assets/middlecontrolhoz.svg +17 -0
  103. package/src/assets/rotateicon.svg +20 -0
  104. package/src/assets/style/resizePlugin.css +27 -0
  105. package/src/index.ts +121 -0
  106. package/src/objects/Arrow.js +47 -0
  107. package/src/objects/ThinTailArrow.js +50 -0
  108. package/src/plugins/AddBaseTypePlugin.ts +107 -0
  109. package/src/plugins/AlignGuidLinePlugin.ts +1141 -0
  110. package/src/plugins/BarCodePlugin.ts +860 -0
  111. package/src/plugins/CenterAlignPlugin.ts +133 -0
  112. package/src/plugins/ControlsPlugin.ts +251 -0
  113. package/src/plugins/ControlsRotatePlugin.ts +111 -0
  114. package/src/plugins/CopyPlugin.ts +255 -0
  115. package/src/plugins/CreateElementPlugin.ts +548 -0
  116. package/src/plugins/DeleteHotKeyPlugin.ts +57 -0
  117. package/src/plugins/DrawLinePlugin.ts +162 -0
  118. package/src/plugins/DrawPolygonPlugin.ts +205 -0
  119. package/src/plugins/DringPlugin.ts +125 -0
  120. package/src/plugins/FlipPlugin.ts +59 -0
  121. package/src/plugins/FontPlugin.ts +165 -0
  122. package/src/plugins/FreeDrawPlugin.ts +49 -0
  123. package/src/plugins/GroupAlignPlugin.ts +365 -0
  124. package/src/plugins/GroupPlugin.ts +82 -0
  125. package/src/plugins/GroupTextEditorPlugin.ts +198 -0
  126. package/src/plugins/HistoryPlugin.ts +181 -0
  127. package/src/plugins/ImageStroke.ts +121 -0
  128. package/src/plugins/LayerPlugin.ts +108 -0
  129. package/src/plugins/LockPlugin.ts +240 -0
  130. package/src/plugins/MaskPlugin.ts +155 -0
  131. package/src/plugins/MaterialPlugin.ts +224 -0
  132. package/src/plugins/MiddleMousePlugin.ts +45 -0
  133. package/src/plugins/MoveHotKeyPlugin.ts +46 -0
  134. package/src/plugins/PathTextPlugin.ts +89 -0
  135. package/src/plugins/PolygonModifyPlugin.ts +224 -0
  136. package/src/plugins/PrintPlugin.ts +81 -0
  137. package/src/plugins/PsdPlugin.ts +52 -0
  138. package/src/plugins/QrCodePlugin.ts +393 -0
  139. package/src/plugins/ResizePlugin.ts +274 -0
  140. package/src/plugins/RulerPlugin.ts +78 -0
  141. package/src/plugins/SimpleClipImagePlugin.ts +244 -0
  142. package/src/plugins/UnitPlugin.ts +327 -0
  143. package/src/plugins/WaterMarkPlugin.ts +257 -0
  144. package/src/plugins/WorkspacePlugin.ts +307 -0
  145. package/src/types/eventType.ts +11 -0
  146. package/src/utils/psd.js +432 -0
  147. package/src/utils/ruler/guideline.ts +145 -0
  148. package/src/utils/ruler/index.ts +91 -0
  149. package/src/utils/ruler/ruler.ts +924 -0
  150. package/src/utils/ruler/utils.ts +162 -0
  151. package/src/utils/units.ts +133 -0
  152. package/tsconfig.json +10 -0
  153. package/vite.config.ts +29 -0
@@ -0,0 +1,133 @@
1
+ import { fabric, IEditor, IPluginTempl } from '@hprint/core';
2
+
3
+ type IPlugin = Pick<
4
+ CenterAlignPlugin,
5
+ 'centerH' | 'center' | 'position' | 'centerV' | 'positionV2'
6
+ >;
7
+
8
+ declare module '@hprint/core' {
9
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
10
+ interface IEditor extends IPlugin { }
11
+ }
12
+
13
+ class CenterAlignPlugin implements IPluginTempl {
14
+ static pluginName = 'CenterAlignPlugin';
15
+ static apis = ['centerH', 'center', 'position', 'centerV', 'positionV2'];
16
+ static anignType = ['centerH', 'center', 'centerV'];
17
+ static alignTypeV2 = ['centerH', 'center', 'centerV', 'leftH', 'topV', 'rightH', 'bottomV'];
18
+ // public hotkeys: string[] = ['space'];
19
+ constructor(
20
+ public canvas: fabric.Canvas,
21
+ public editor: IEditor
22
+ ) { }
23
+
24
+ center(workspace: fabric.Rect, object: fabric.Object) {
25
+ const center = workspace.getCenterPoint();
26
+ return this.canvas._centerObject(object, center);
27
+ }
28
+
29
+ centerV(workspace: fabric.Rect, object: fabric.Object) {
30
+ return this.canvas._centerObject(
31
+ object,
32
+ new fabric.Point(
33
+ object.getCenterPoint().x,
34
+ workspace.getCenterPoint().y
35
+ )
36
+ );
37
+ }
38
+
39
+ centerH(workspace: fabric.Rect, object: fabric.Object) {
40
+ return this.canvas._centerObject(
41
+ object,
42
+ new fabric.Point(
43
+ workspace.getCenterPoint().x,
44
+ object.getCenterPoint().y
45
+ )
46
+ );
47
+ }
48
+
49
+ position(name: 'centerH' | 'center' | 'centerV') {
50
+ const activeObject = this.canvas.getActiveObject();
51
+ if (CenterAlignPlugin.anignType.includes(name) && activeObject) {
52
+ const defaultWorkspace = this.canvas
53
+ .getObjects()
54
+ .find((item) => item.id === 'workspace');
55
+ if (defaultWorkspace) {
56
+ this[name](defaultWorkspace, activeObject);
57
+ }
58
+ this.canvas.renderAll();
59
+ }
60
+ }
61
+
62
+ contextMenu() {
63
+ const activeObject = this.canvas.getActiveObject();
64
+ if (activeObject) {
65
+ return [
66
+ {
67
+ text: '水平垂直居中',
68
+ hotkey: '',
69
+ disabled: false,
70
+ onclick: () => this.position('center'),
71
+ },
72
+ ];
73
+ }
74
+ }
75
+
76
+ positionV2(element: fabric.Object, position: 'centerH' | 'center' | 'centerV' | 'leftH' | 'topV' | 'rightH' | 'bottomV') {
77
+ if (CenterAlignPlugin.alignTypeV2.includes(position) && element) {
78
+ const defaultWorkspace = this.canvas
79
+ .getObjects()
80
+ .find((item) => item.id === 'workspace');
81
+ if (defaultWorkspace) {
82
+ const center = defaultWorkspace.getCenterPoint();
83
+ switch (position) {
84
+ case 'centerH':
85
+ this.canvas._centerObject(
86
+ element,
87
+ new fabric.Point(
88
+ defaultWorkspace.getCenterPoint().x,
89
+ element.getCenterPoint().y
90
+ )
91
+ );
92
+ break;
93
+ case 'centerV':
94
+ this.canvas._centerObject(
95
+ element,
96
+ new fabric.Point(
97
+ element.getCenterPoint().x,
98
+ defaultWorkspace.getCenterPoint().y,
99
+ )
100
+ );
101
+ break;
102
+ case 'leftH':
103
+ element.set('left', 0)
104
+ break;
105
+ case 'topV':
106
+ element.set('top', 0)
107
+ break;
108
+ case 'rightH':
109
+ element.set('left', defaultWorkspace.get('left') + defaultWorkspace.get('width') - element.get('width'))
110
+ break;
111
+ case 'bottomV':
112
+ element.set('top', defaultWorkspace.get('top') + defaultWorkspace.get('height') - element.get('height'))
113
+ break;
114
+ case 'center':
115
+ this.canvas._centerObject(element, center);
116
+ break;
117
+ default:
118
+ break;
119
+ }
120
+ }
121
+ element.syncOriginSizeByUnit()
122
+ this.canvas.fire('object:modified', element);
123
+ this.canvas.renderAll();
124
+ }
125
+
126
+ }
127
+
128
+ destroy() {
129
+ console.log('pluginDestroy');
130
+ }
131
+ }
132
+
133
+ export default CenterAlignPlugin;
@@ -0,0 +1,251 @@
1
+ import { fabric } from '@hprint/core';
2
+ import verticalImg from '../assets/middlecontrol.svg?url';
3
+ // import verticalImg from './middlecontrol.svg';
4
+ import horizontalImg from '../assets/middlecontrolhoz.svg?url';
5
+ import edgeImg from '../assets/edgecontrol.svg?url';
6
+ import rotateImg from '../assets/rotateicon.svg?url';
7
+ import type { IEditor, IPluginTempl } from '@hprint/core';
8
+
9
+ /**
10
+ * 实际场景: 在进行某个对象缩放的时候,由于fabricjs默认精度使用的是toFixed(2)。
11
+ * 此处为了缩放的精度更准确一些,因此将NUM_FRACTION_DIGITS默认值改为4,即toFixed(4).
12
+ */
13
+ fabric.Object.NUM_FRACTION_DIGITS = 4;
14
+
15
+ function drawImg(
16
+ ctx: CanvasRenderingContext2D,
17
+ left: number,
18
+ top: number,
19
+ img: HTMLImageElement,
20
+ wSize: number,
21
+ hSize: number,
22
+ angle: number | undefined
23
+ ) {
24
+ if (angle === undefined) return;
25
+ ctx.save();
26
+ ctx.translate(left, top);
27
+ ctx.rotate(fabric.util.degreesToRadians(angle));
28
+ ctx.drawImage(img, -wSize / 2, -hSize / 2, wSize, hSize);
29
+ ctx.restore();
30
+ }
31
+
32
+ // 中间横杠
33
+ function intervalControl() {
34
+ const verticalImgIcon = document.createElement('img');
35
+ verticalImgIcon.src = verticalImg;
36
+
37
+ const horizontalImgIcon = document.createElement('img');
38
+ horizontalImgIcon.src = horizontalImg;
39
+
40
+ function renderIcon(
41
+ ctx: CanvasRenderingContext2D,
42
+ left: number,
43
+ top: number,
44
+ styleOverride: any,
45
+ fabricObject: fabric.Object
46
+ ) {
47
+ drawImg(ctx, left, top, verticalImgIcon, 20, 25, fabricObject.angle);
48
+ }
49
+
50
+ function renderIconHoz(
51
+ ctx: CanvasRenderingContext2D,
52
+ left: number,
53
+ top: number,
54
+ styleOverride: any,
55
+ fabricObject: fabric.Object
56
+ ) {
57
+ drawImg(ctx, left, top, horizontalImgIcon, 25, 20, fabricObject.angle);
58
+ }
59
+ // 中间横杠
60
+ fabric.Object.prototype.controls.ml = new fabric.Control({
61
+ x: -0.5,
62
+ y: 0,
63
+ offsetX: -1,
64
+ cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
65
+ actionHandler: fabric.controlsUtils.scalingXOrSkewingY,
66
+ getActionName: fabric.controlsUtils.scaleOrSkewActionName,
67
+ render: renderIcon,
68
+ });
69
+
70
+ fabric.Object.prototype.controls.mr = new fabric.Control({
71
+ x: 0.5,
72
+ y: 0,
73
+ offsetX: 1,
74
+ cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
75
+ actionHandler: fabric.controlsUtils.scalingXOrSkewingY,
76
+ getActionName: fabric.controlsUtils.scaleOrSkewActionName,
77
+ render: renderIcon,
78
+ });
79
+
80
+ fabric.Object.prototype.controls.mb = new fabric.Control({
81
+ x: 0,
82
+ y: 0.5,
83
+ offsetY: 1,
84
+ cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
85
+ actionHandler: fabric.controlsUtils.scalingYOrSkewingX,
86
+ getActionName: fabric.controlsUtils.scaleOrSkewActionName,
87
+ render: renderIconHoz,
88
+ });
89
+
90
+ fabric.Object.prototype.controls.mt = new fabric.Control({
91
+ x: 0,
92
+ y: -0.5,
93
+ offsetY: -1,
94
+ cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
95
+ actionHandler: fabric.controlsUtils.scalingYOrSkewingX,
96
+ getActionName: fabric.controlsUtils.scaleOrSkewActionName,
97
+ render: renderIconHoz,
98
+ });
99
+ }
100
+
101
+ // 顶点
102
+ function peakControl() {
103
+ const img = document.createElement('img');
104
+ img.src = edgeImg;
105
+
106
+ function renderIconEdge(
107
+ ctx: CanvasRenderingContext2D,
108
+ left: number,
109
+ top: number,
110
+ styleOverride: any,
111
+ fabricObject: fabric.Object
112
+ ) {
113
+ drawImg(ctx, left, top, img, 25, 25, fabricObject.angle);
114
+ }
115
+ // 四角图标
116
+ fabric.Object.prototype.controls.tl = new fabric.Control({
117
+ x: -0.5,
118
+ y: -0.5,
119
+ cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
120
+ actionHandler: fabric.controlsUtils.scalingEqually,
121
+ render: renderIconEdge,
122
+ });
123
+ fabric.Object.prototype.controls.bl = new fabric.Control({
124
+ x: -0.5,
125
+ y: 0.5,
126
+ cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
127
+ actionHandler: fabric.controlsUtils.scalingEqually,
128
+ render: renderIconEdge,
129
+ });
130
+ fabric.Object.prototype.controls.tr = new fabric.Control({
131
+ x: 0.5,
132
+ y: -0.5,
133
+ cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
134
+ actionHandler: fabric.controlsUtils.scalingEqually,
135
+ render: renderIconEdge,
136
+ });
137
+ fabric.Object.prototype.controls.br = new fabric.Control({
138
+ x: 0.5,
139
+ y: 0.5,
140
+ cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
141
+ actionHandler: fabric.controlsUtils.scalingEqually,
142
+ render: renderIconEdge,
143
+ });
144
+ }
145
+
146
+ // 删除
147
+ /*function deleteControl(canvas: fabric.Canvas) {
148
+ const deleteIcon =
149
+ "data:image/svg+xml,%3C%3Fxml version='1.0' encoding='utf-8'%3F%3E%3C!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'%3E%3Csvg version='1.1' id='Ebene_1' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' x='0px' y='0px' width='595.275px' height='595.275px' viewBox='200 215 230 470' xml:space='preserve'%3E%3Ccircle style='fill:%23F44336;' cx='299.76' cy='439.067' r='218.516'/%3E%3Cg%3E%3Crect x='267.162' y='307.978' transform='matrix(0.7071 -0.7071 0.7071 0.7071 -222.6202 340.6915)' style='fill:white;' width='65.545' height='262.18'/%3E%3Crect x='266.988' y='308.153' transform='matrix(0.7071 0.7071 -0.7071 0.7071 398.3889 -83.3116)' style='fill:white;' width='65.544' height='262.179'/%3E%3C/g%3E%3C/svg%3E";
150
+ const delImg = document.createElement('img');
151
+ delImg.src = deleteIcon;
152
+
153
+ function renderDelIcon(
154
+ ctx: CanvasRenderingContext2D,
155
+ left: number,
156
+ top: number,
157
+ styleOverride: any,
158
+ fabricObject: fabric.Object
159
+ ) {
160
+ drawImg(ctx, left, top, delImg, 24, 24, fabricObject.angle);
161
+ }
162
+
163
+ // 删除选中元素
164
+ function deleteObject(mouseEvent: MouseEvent, target: fabric.Transform) {
165
+ if (target.action === 'rotate') return true;
166
+ const activeObject = canvas.getActiveObjects();
167
+ if (activeObject) {
168
+ activeObject.map((item) => canvas.remove(item));
169
+ canvas.requestRenderAll();
170
+ canvas.discardActiveObject();
171
+ }
172
+ return true;
173
+ }
174
+
175
+ // 删除图标
176
+ fabric.Object.prototype.controls.deleteControl = new fabric.Control({
177
+ x: 0.5,
178
+ y: -0.5,
179
+ offsetY: -16,
180
+ offsetX: 16,
181
+ cursorStyle: 'pointer',
182
+ mouseUpHandler: deleteObject,
183
+ render: renderDelIcon,
184
+ // cornerSize: 24,
185
+ });
186
+ }*/
187
+
188
+ // 旋转
189
+ function rotationControl() {
190
+ const img = document.createElement('img');
191
+ img.src = rotateImg;
192
+ function renderIconRotate(
193
+ ctx: CanvasRenderingContext2D,
194
+ left: number,
195
+ top: number,
196
+ styleOverride: any,
197
+ fabricObject: fabric.Object
198
+ ) {
199
+ drawImg(ctx, left, top, img, 40, 40, fabricObject.angle);
200
+ }
201
+ // 旋转图标
202
+ fabric.Object.prototype.controls.mtr = new fabric.Control({
203
+ x: 0,
204
+ y: 0.5,
205
+ cursorStyleHandler: fabric.controlsUtils.rotationStyleHandler,
206
+ actionHandler: fabric.controlsUtils.rotationWithSnapping,
207
+ offsetY: 30,
208
+ // withConnecton: false,
209
+ actionName: 'rotate',
210
+ render: renderIconRotate,
211
+ });
212
+ }
213
+
214
+ class ControlsPlugin implements IPluginTempl {
215
+ static pluginName = 'ControlsPlugin';
216
+ constructor(
217
+ public canvas: fabric.Canvas,
218
+ public editor: IEditor
219
+ ) {
220
+ this.init();
221
+ }
222
+ init() {
223
+ // 删除图标
224
+ // deleteControl(this.canvas);
225
+ // 顶点图标
226
+ peakControl();
227
+ // 中间横杠图标
228
+ intervalControl();
229
+ // 旋转图标
230
+ rotationControl();
231
+
232
+ // 选中样式
233
+ fabric.Object.prototype.set({
234
+ transparentCorners: false,
235
+ borderColor: '#51B9F9',
236
+ cornerColor: '#FFF',
237
+ borderScaleFactor: 2.5,
238
+ cornerStyle: 'circle',
239
+ cornerStrokeColor: '#0E98FC',
240
+ borderOpacityWhenMoving: 1,
241
+ });
242
+ // textbox保持一致
243
+ // fabric.Textbox.prototype.controls = fabric.Object.prototype.controls;
244
+ }
245
+
246
+ destroy() {
247
+ console.log('pluginDestroy');
248
+ }
249
+ }
250
+
251
+ export default ControlsPlugin;
@@ -0,0 +1,111 @@
1
+ import { fabric } from '@hprint/core';
2
+ import type { IEditor, IPluginTempl } from '@hprint/core';
3
+
4
+ // 定义旋转光标样式,根据转动角度设定光标旋转
5
+ function rotateIcon(angle: number) {
6
+ return `url("data:image/svg+xml,%3Csvg height='18' width='18' viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg' style='color: black;'%3E%3Cg fill='none' transform='rotate(${angle} 16 16)'%3E%3Cpath d='M22.4484 0L32 9.57891L22.4484 19.1478V13.1032C17.6121 13.8563 13.7935 17.6618 13.0479 22.4914H19.2141L9.60201 32.01L0 22.4813H6.54912C7.36524 14.1073 14.0453 7.44023 22.4484 6.61688V0Z' fill='white'/%3E%3Cpath d='M24.0605 3.89587L29.7229 9.57896L24.0605 15.252V11.3562C17.0479 11.4365 11.3753 17.0895 11.3048 24.0879H15.3048L9.60201 29.7308L3.90932 24.0879H8.0806C8.14106 15.3223 15.2645 8.22345 24.0605 8.14313V3.89587Z' fill='black'/%3E%3C/g%3E%3C/svg%3E ") 12 12,crosshair`;
7
+ }
8
+
9
+ class ControlsRotatePlugin implements IPluginTempl {
10
+ static pluginName = 'ControlsRotatePlugin';
11
+ constructor(
12
+ public canvas: fabric.Canvas,
13
+ public editor: IEditor
14
+ ) {
15
+ this.init();
16
+ }
17
+ init() {
18
+ const { canvas } = this;
19
+ // 添加旋转控制响应区域
20
+ fabric.Object.prototype.controls.mtr = new fabric.Control({
21
+ x: -0.5,
22
+ y: -0.5,
23
+ offsetY: -10,
24
+ offsetX: -10,
25
+ rotate: 20,
26
+ actionName: 'rotate',
27
+ actionHandler: fabric.controlsUtils.rotationWithSnapping,
28
+ render: () => '',
29
+ });
30
+ // ↖左上
31
+ fabric.Object.prototype.controls.mtr2 = new fabric.Control({
32
+ x: 0.5,
33
+ y: -0.5,
34
+ offsetY: -10,
35
+ offsetX: 10,
36
+ rotate: 20,
37
+ actionName: 'rotate',
38
+ actionHandler: fabric.controlsUtils.rotationWithSnapping,
39
+ render: () => '',
40
+ }); // ↗右上
41
+ fabric.Object.prototype.controls.mtr3 = new fabric.Control({
42
+ x: 0.5,
43
+ y: 0.5,
44
+ offsetY: 10,
45
+ offsetX: 10,
46
+ rotate: 20,
47
+ actionName: 'rotate',
48
+ actionHandler: fabric.controlsUtils.rotationWithSnapping,
49
+ render: () => '',
50
+ }); // ↘右下
51
+ fabric.Object.prototype.controls.mtr4 = new fabric.Control({
52
+ x: -0.5,
53
+ y: 0.5,
54
+ offsetY: 10,
55
+ offsetX: -10,
56
+ rotate: 20,
57
+ actionName: 'rotate',
58
+ actionHandler: fabric.controlsUtils.rotationWithSnapping,
59
+ render: () => '',
60
+ }); // ↙左下
61
+
62
+ // 渲染时,执行
63
+ canvas.on('after:render', () => {
64
+ const activeObj = canvas.getActiveObject();
65
+ const angle = activeObj?.angle?.toFixed(2);
66
+ if (angle !== undefined) {
67
+ fabric.Object.prototype.controls.mtr.cursorStyle = rotateIcon(
68
+ Number(angle)
69
+ );
70
+ fabric.Object.prototype.controls.mtr2.cursorStyle = rotateIcon(
71
+ Number(angle) + 90
72
+ );
73
+ fabric.Object.prototype.controls.mtr3.cursorStyle = rotateIcon(
74
+ Number(angle) + 180
75
+ );
76
+ fabric.Object.prototype.controls.mtr4.cursorStyle = rotateIcon(
77
+ Number(angle) + 270
78
+ );
79
+ }
80
+ });
81
+
82
+ // 旋转时,实时更新旋转控制图标
83
+ canvas.on('object:rotating', (event) => {
84
+ const body = canvas.lowerCanvasEl.nextSibling as HTMLElement;
85
+ const angle = canvas.getActiveObject()?.angle?.toFixed(2);
86
+ if (angle === undefined) return;
87
+ switch (event.transform?.corner) {
88
+ case 'mtr':
89
+ body.style.cursor = rotateIcon(Number(angle));
90
+ break;
91
+ case 'mtr2':
92
+ body.style.cursor = rotateIcon(Number(angle) + 90);
93
+ break;
94
+ case 'mtr3':
95
+ body.style.cursor = rotateIcon(Number(angle) + 180);
96
+ break;
97
+ case 'mtr4':
98
+ body.style.cursor = rotateIcon(Number(angle) + 270);
99
+ break;
100
+ default:
101
+ break;
102
+ } // 设置四角旋转光标
103
+ });
104
+ }
105
+
106
+ destroy() {
107
+ console.log('pluginDestroy');
108
+ }
109
+ }
110
+
111
+ export default ControlsRotatePlugin;