@hprint/plugins 0.0.6 → 0.0.8
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/index.js +15 -15
- package/dist/index.mjs +1864 -1862
- package/dist/src/plugins/BarCodePlugin.d.ts +2 -2
- package/dist/src/plugins/BarCodePlugin.d.ts.map +1 -1
- package/dist/src/plugins/CopyPlugin.d.ts.map +1 -1
- package/dist/src/plugins/CreateElementPlugin.d.ts +2 -2
- package/dist/src/plugins/CreateElementPlugin.d.ts.map +1 -1
- package/dist/src/plugins/QrCodePlugin.d.ts +2 -2
- package/dist/src/plugins/QrCodePlugin.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/assets/style/resizePlugin.css +27 -27
- package/src/objects/Arrow.js +47 -47
- package/src/objects/ThinTailArrow.js +50 -50
- package/src/plugins/BarCodePlugin.ts +7 -7
- package/src/plugins/ControlsPlugin.ts +413 -413
- package/src/plugins/ControlsRotatePlugin.ts +111 -111
- package/src/plugins/CopyPlugin.ts +261 -255
- package/src/plugins/CreateElementPlugin.ts +3 -1
- package/src/plugins/DeleteHotKeyPlugin.ts +57 -57
- package/src/plugins/DrawLinePlugin.ts +162 -162
- package/src/plugins/DrawPolygonPlugin.ts +205 -205
- package/src/plugins/DringPlugin.ts +125 -125
- package/src/plugins/FlipPlugin.ts +59 -59
- package/src/plugins/FontPlugin.ts +165 -165
- package/src/plugins/FreeDrawPlugin.ts +49 -49
- package/src/plugins/GroupPlugin.ts +82 -82
- package/src/plugins/GroupTextEditorPlugin.ts +198 -198
- package/src/plugins/HistoryPlugin.ts +181 -181
- package/src/plugins/ImageStroke.ts +121 -121
- package/src/plugins/LayerPlugin.ts +108 -108
- package/src/plugins/MaskPlugin.ts +155 -155
- package/src/plugins/MaterialPlugin.ts +224 -224
- package/src/plugins/MiddleMousePlugin.ts +45 -45
- package/src/plugins/MoveHotKeyPlugin.ts +46 -46
- package/src/plugins/PathTextPlugin.ts +89 -89
- package/src/plugins/PolygonModifyPlugin.ts +224 -224
- package/src/plugins/PrintPlugin.ts +81 -81
- package/src/plugins/PsdPlugin.ts +52 -52
- package/src/plugins/QrCodePlugin.ts +6 -6
- package/src/plugins/SimpleClipImagePlugin.ts +244 -244
- package/src/types/eventType.ts +11 -11
- package/src/utils/psd.js +432 -432
- package/src/utils/ruler/guideline.ts +145 -145
- package/src/utils/ruler/index.ts +91 -91
- package/src/utils/ruler/utils.ts +162 -162
- package/tsconfig.json +10 -10
- package/vite.config.ts +29 -29
|
@@ -1,413 +1,413 @@
|
|
|
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
|
-
/**
|
|
16
|
-
* 统一的控制点样式配置
|
|
17
|
-
*/
|
|
18
|
-
const CONTROL_STYLES = {
|
|
19
|
-
transparentCorners: false,
|
|
20
|
-
borderColor: '#51B9F9',
|
|
21
|
-
cornerColor: '#FFF',
|
|
22
|
-
borderScaleFactor: 2.5,
|
|
23
|
-
cornerStyle: 'circle' as const,
|
|
24
|
-
cornerStrokeColor: 'rgba(0,0,0,0.25)', // 更淡的灰色边框
|
|
25
|
-
borderOpacityWhenMoving: 1,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
function drawImg(
|
|
29
|
-
ctx: CanvasRenderingContext2D,
|
|
30
|
-
left: number,
|
|
31
|
-
top: number,
|
|
32
|
-
img: HTMLImageElement,
|
|
33
|
-
wSize: number,
|
|
34
|
-
hSize: number,
|
|
35
|
-
angle: number | undefined
|
|
36
|
-
) {
|
|
37
|
-
if (angle === undefined) return;
|
|
38
|
-
ctx.save();
|
|
39
|
-
ctx.translate(left, top);
|
|
40
|
-
ctx.rotate(fabric.util.degreesToRadians(angle));
|
|
41
|
-
ctx.drawImage(img, -wSize / 2, -hSize / 2, wSize, hSize);
|
|
42
|
-
ctx.restore();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// 中间横杠
|
|
46
|
-
function intervalControl() {
|
|
47
|
-
const verticalImgIcon = document.createElement('img');
|
|
48
|
-
verticalImgIcon.src = verticalImg;
|
|
49
|
-
|
|
50
|
-
const horizontalImgIcon = document.createElement('img');
|
|
51
|
-
horizontalImgIcon.src = horizontalImg;
|
|
52
|
-
|
|
53
|
-
function renderIcon(
|
|
54
|
-
ctx: CanvasRenderingContext2D,
|
|
55
|
-
left: number,
|
|
56
|
-
top: number,
|
|
57
|
-
styleOverride: any,
|
|
58
|
-
fabricObject: fabric.Object
|
|
59
|
-
) {
|
|
60
|
-
drawImg(ctx, left, top, verticalImgIcon, 20, 25, fabricObject.angle);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function renderIconHoz(
|
|
64
|
-
ctx: CanvasRenderingContext2D,
|
|
65
|
-
left: number,
|
|
66
|
-
top: number,
|
|
67
|
-
styleOverride: any,
|
|
68
|
-
fabricObject: fabric.Object
|
|
69
|
-
) {
|
|
70
|
-
drawImg(ctx, left, top, horizontalImgIcon, 25, 20, fabricObject.angle);
|
|
71
|
-
}
|
|
72
|
-
// 中间横杠
|
|
73
|
-
fabric.Object.prototype.controls.ml = new fabric.Control({
|
|
74
|
-
x: -0.5,
|
|
75
|
-
y: 0,
|
|
76
|
-
offsetX: -1,
|
|
77
|
-
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
78
|
-
actionHandler: fabric.controlsUtils.scalingXOrSkewingY,
|
|
79
|
-
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
80
|
-
render: renderIcon,
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
fabric.Object.prototype.controls.mr = new fabric.Control({
|
|
84
|
-
x: 0.5,
|
|
85
|
-
y: 0,
|
|
86
|
-
offsetX: 1,
|
|
87
|
-
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
88
|
-
actionHandler: fabric.controlsUtils.scalingXOrSkewingY,
|
|
89
|
-
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
90
|
-
render: renderIcon,
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
fabric.Object.prototype.controls.mb = new fabric.Control({
|
|
94
|
-
x: 0,
|
|
95
|
-
y: 0.5,
|
|
96
|
-
offsetY: 1,
|
|
97
|
-
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
98
|
-
actionHandler: fabric.controlsUtils.scalingYOrSkewingX,
|
|
99
|
-
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
100
|
-
render: renderIconHoz,
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
fabric.Object.prototype.controls.mt = new fabric.Control({
|
|
104
|
-
x: 0,
|
|
105
|
-
y: -0.5,
|
|
106
|
-
offsetY: -1,
|
|
107
|
-
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
108
|
-
actionHandler: fabric.controlsUtils.scalingYOrSkewingX,
|
|
109
|
-
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
110
|
-
render: renderIconHoz,
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// 顶点
|
|
115
|
-
function peakControl() {
|
|
116
|
-
const img = document.createElement('img');
|
|
117
|
-
img.src = edgeImg;
|
|
118
|
-
|
|
119
|
-
function renderIconEdge(
|
|
120
|
-
ctx: CanvasRenderingContext2D,
|
|
121
|
-
left: number,
|
|
122
|
-
top: number,
|
|
123
|
-
styleOverride: any,
|
|
124
|
-
fabricObject: fabric.Object
|
|
125
|
-
) {
|
|
126
|
-
drawImg(ctx, left, top, img, 25, 25, fabricObject.angle);
|
|
127
|
-
}
|
|
128
|
-
// 四角图标
|
|
129
|
-
fabric.Object.prototype.controls.tl = new fabric.Control({
|
|
130
|
-
x: -0.5,
|
|
131
|
-
y: -0.5,
|
|
132
|
-
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
133
|
-
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
134
|
-
render: renderIconEdge,
|
|
135
|
-
});
|
|
136
|
-
fabric.Object.prototype.controls.bl = new fabric.Control({
|
|
137
|
-
x: -0.5,
|
|
138
|
-
y: 0.5,
|
|
139
|
-
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
140
|
-
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
141
|
-
render: renderIconEdge,
|
|
142
|
-
});
|
|
143
|
-
fabric.Object.prototype.controls.tr = new fabric.Control({
|
|
144
|
-
x: 0.5,
|
|
145
|
-
y: -0.5,
|
|
146
|
-
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
147
|
-
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
148
|
-
render: renderIconEdge,
|
|
149
|
-
});
|
|
150
|
-
fabric.Object.prototype.controls.br = new fabric.Control({
|
|
151
|
-
x: 0.5,
|
|
152
|
-
y: 0.5,
|
|
153
|
-
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
154
|
-
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
155
|
-
render: renderIconEdge,
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// 删除
|
|
160
|
-
/*function deleteControl(canvas: fabric.Canvas) {
|
|
161
|
-
const deleteIcon =
|
|
162
|
-
"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";
|
|
163
|
-
const delImg = document.createElement('img');
|
|
164
|
-
delImg.src = deleteIcon;
|
|
165
|
-
|
|
166
|
-
function renderDelIcon(
|
|
167
|
-
ctx: CanvasRenderingContext2D,
|
|
168
|
-
left: number,
|
|
169
|
-
top: number,
|
|
170
|
-
styleOverride: any,
|
|
171
|
-
fabricObject: fabric.Object
|
|
172
|
-
) {
|
|
173
|
-
drawImg(ctx, left, top, delImg, 24, 24, fabricObject.angle);
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// 删除选中元素
|
|
177
|
-
function deleteObject(mouseEvent: MouseEvent, target: fabric.Transform) {
|
|
178
|
-
if (target.action === 'rotate') return true;
|
|
179
|
-
const activeObject = canvas.getActiveObjects();
|
|
180
|
-
if (activeObject) {
|
|
181
|
-
activeObject.map((item) => canvas.remove(item));
|
|
182
|
-
canvas.requestRenderAll();
|
|
183
|
-
canvas.discardActiveObject();
|
|
184
|
-
}
|
|
185
|
-
return true;
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// 删除图标
|
|
189
|
-
fabric.Object.prototype.controls.deleteControl = new fabric.Control({
|
|
190
|
-
x: 0.5,
|
|
191
|
-
y: -0.5,
|
|
192
|
-
offsetY: -16,
|
|
193
|
-
offsetX: 16,
|
|
194
|
-
cursorStyle: 'pointer',
|
|
195
|
-
mouseUpHandler: deleteObject,
|
|
196
|
-
render: renderDelIcon,
|
|
197
|
-
// cornerSize: 24,
|
|
198
|
-
});
|
|
199
|
-
}*/
|
|
200
|
-
|
|
201
|
-
// 旋转
|
|
202
|
-
function rotationControl() {
|
|
203
|
-
const img = document.createElement('img');
|
|
204
|
-
img.src = rotateImg;
|
|
205
|
-
function renderIconRotate(
|
|
206
|
-
ctx: CanvasRenderingContext2D,
|
|
207
|
-
left: number,
|
|
208
|
-
top: number,
|
|
209
|
-
styleOverride: any,
|
|
210
|
-
fabricObject: fabric.Object
|
|
211
|
-
) {
|
|
212
|
-
drawImg(ctx, left, top, img, 40, 40, fabricObject.angle);
|
|
213
|
-
}
|
|
214
|
-
// 旋转图标
|
|
215
|
-
fabric.Object.prototype.controls.mtr = new fabric.Control({
|
|
216
|
-
x: 0,
|
|
217
|
-
y: 0.5,
|
|
218
|
-
cursorStyleHandler: fabric.controlsUtils.rotationStyleHandler,
|
|
219
|
-
actionHandler: fabric.controlsUtils.rotationWithSnapping,
|
|
220
|
-
offsetY: 30,
|
|
221
|
-
// withConnecton: false,
|
|
222
|
-
actionName: 'rotate',
|
|
223
|
-
render: renderIconRotate,
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
class ControlsPlugin implements IPluginTempl {
|
|
228
|
-
static pluginName = 'ControlsPlugin';
|
|
229
|
-
constructor(
|
|
230
|
-
public canvas: fabric.Canvas,
|
|
231
|
-
public editor: IEditor
|
|
232
|
-
) {
|
|
233
|
-
this.init();
|
|
234
|
-
}
|
|
235
|
-
init() {
|
|
236
|
-
// 删除图标
|
|
237
|
-
// deleteControl(this.canvas);
|
|
238
|
-
// 顶点图标
|
|
239
|
-
peakControl();
|
|
240
|
-
// 中间横杠图标
|
|
241
|
-
intervalControl();
|
|
242
|
-
// 旋转图标
|
|
243
|
-
rotationControl();
|
|
244
|
-
|
|
245
|
-
// 选中样式
|
|
246
|
-
fabric.Object.prototype.set(CONTROL_STYLES);
|
|
247
|
-
// textbox保持一致
|
|
248
|
-
// fabric.Textbox.prototype.controls = fabric.Object.prototype.controls;
|
|
249
|
-
|
|
250
|
-
// 自定义多选控制点渲染
|
|
251
|
-
this.customizeActiveSelection();
|
|
252
|
-
|
|
253
|
-
// 监听 loadJson 事件,重新应用控制点样式
|
|
254
|
-
this.editor.on('loadJson', () => {
|
|
255
|
-
this.applyControlStyles();
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* 为画布上所有对象应用统一的控制点样式
|
|
261
|
-
*/
|
|
262
|
-
applyControlStyles() {
|
|
263
|
-
const objects = this.canvas.getObjects();
|
|
264
|
-
objects.forEach((obj) => {
|
|
265
|
-
// 跳过 workspace 等特殊对象
|
|
266
|
-
if (obj.id === 'workspace' || obj.id === 'coverMask') {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
obj.set(CONTROL_STYLES);
|
|
270
|
-
});
|
|
271
|
-
this.canvas.renderAll();
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/**
|
|
275
|
-
* 自定义多选控制点,添加白色填充并确保在边框上方
|
|
276
|
-
*/
|
|
277
|
-
customizeActiveSelection() {
|
|
278
|
-
// 自定义控制点渲染函数
|
|
279
|
-
const renderCircleControl = (
|
|
280
|
-
ctx: CanvasRenderingContext2D,
|
|
281
|
-
left: number,
|
|
282
|
-
top: number,
|
|
283
|
-
styleOverride: any,
|
|
284
|
-
fabricObject: fabric.Object
|
|
285
|
-
) => {
|
|
286
|
-
const size = fabricObject.cornerSize || 10;
|
|
287
|
-
ctx.save();
|
|
288
|
-
ctx.translate(left, top);
|
|
289
|
-
ctx.rotate(fabric.util.degreesToRadians(fabricObject.angle || 0));
|
|
290
|
-
|
|
291
|
-
// 绘制阴影
|
|
292
|
-
ctx.shadowColor = 'rgba(0,0,0,0.15)';
|
|
293
|
-
ctx.shadowBlur = 3;
|
|
294
|
-
ctx.shadowOffsetX = 0;
|
|
295
|
-
ctx.shadowOffsetY = 1;
|
|
296
|
-
|
|
297
|
-
// 绘制白色填充的圆形
|
|
298
|
-
ctx.beginPath();
|
|
299
|
-
ctx.arc(0, 0, size / 2, 0, 2 * Math.PI, false);
|
|
300
|
-
ctx.fillStyle = '#FFF';
|
|
301
|
-
ctx.fill();
|
|
302
|
-
|
|
303
|
-
// 清除阴影,避免边框也有阴影
|
|
304
|
-
ctx.shadowColor = 'transparent';
|
|
305
|
-
ctx.shadowBlur = 0;
|
|
306
|
-
ctx.shadowOffsetX = 0;
|
|
307
|
-
ctx.shadowOffsetY = 0;
|
|
308
|
-
|
|
309
|
-
// 绘制更淡的灰色边框
|
|
310
|
-
ctx.strokeStyle = 'rgba(0,0,0,0.25)';
|
|
311
|
-
ctx.lineWidth = 1.5;
|
|
312
|
-
ctx.stroke();
|
|
313
|
-
|
|
314
|
-
ctx.restore();
|
|
315
|
-
};
|
|
316
|
-
|
|
317
|
-
// 为 ActiveSelection 设置自定义控制点
|
|
318
|
-
const controlKeys = ['tl', 'tr', 'bl', 'br', 'ml', 'mr', 'mt', 'mb', 'mtr'];
|
|
319
|
-
controlKeys.forEach((key) => {
|
|
320
|
-
if (fabric.ActiveSelection.prototype.controls[key]) {
|
|
321
|
-
const originalControl = fabric.ActiveSelection.prototype.controls[key];
|
|
322
|
-
fabric.ActiveSelection.prototype.controls[key] = new fabric.Control({
|
|
323
|
-
...originalControl,
|
|
324
|
-
render: renderCircleControl,
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
// 应用样式配置
|
|
330
|
-
fabric.ActiveSelection.prototype.set(CONTROL_STYLES);
|
|
331
|
-
|
|
332
|
-
// 跟踪是否正在移动或缩放对象
|
|
333
|
-
let isTransforming = false;
|
|
334
|
-
|
|
335
|
-
// 监听对象移动开始
|
|
336
|
-
this.canvas.on('object:moving', (e: any) => {
|
|
337
|
-
if (e.target && e.target.type === 'activeSelection') {
|
|
338
|
-
isTransforming = true;
|
|
339
|
-
}
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
// 监听对象缩放开始
|
|
343
|
-
this.canvas.on('object:scaling', (e: any) => {
|
|
344
|
-
if (e.target && e.target.type === 'activeSelection') {
|
|
345
|
-
isTransforming = true;
|
|
346
|
-
}
|
|
347
|
-
});
|
|
348
|
-
|
|
349
|
-
// 监听对象旋转开始
|
|
350
|
-
this.canvas.on('object:rotating', (e: any) => {
|
|
351
|
-
if (e.target && e.target.type === 'activeSelection') {
|
|
352
|
-
isTransforming = true;
|
|
353
|
-
}
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
// 监听对象变换结束
|
|
357
|
-
this.canvas.on('object:modified', (e: any) => {
|
|
358
|
-
if (e.target && e.target.type === 'activeSelection') {
|
|
359
|
-
isTransforming = false;
|
|
360
|
-
this.canvas.requestRenderAll();
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
// 监听鼠标松开(防止某些情况下modified事件未触发)
|
|
365
|
-
this.canvas.on('mouse:up', () => {
|
|
366
|
-
if (isTransforming) {
|
|
367
|
-
isTransforming = false;
|
|
368
|
-
this.canvas.requestRenderAll();
|
|
369
|
-
}
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
// 监听canvas的after:render事件,在所有内容渲染完成后额外绘制多选控制点
|
|
373
|
-
this.canvas.on('after:render', () => {
|
|
374
|
-
// 如果正在变换(移动、缩放、旋转),不绘制控制点
|
|
375
|
-
if (isTransforming) return;
|
|
376
|
-
|
|
377
|
-
const activeObject = this.canvas.getActiveObject();
|
|
378
|
-
if (activeObject && activeObject.type === 'activeSelection') {
|
|
379
|
-
const ctx = this.canvas.getContext();
|
|
380
|
-
if (!ctx) return;
|
|
381
|
-
|
|
382
|
-
ctx.save();
|
|
383
|
-
|
|
384
|
-
// 确保控制点在最上层
|
|
385
|
-
ctx.globalCompositeOperation = 'source-over';
|
|
386
|
-
|
|
387
|
-
// 额外绘制一次控制点,确保在所有边框之上
|
|
388
|
-
activeObject.forEachControl((control: any, key: string) => {
|
|
389
|
-
if (control.getVisibility(activeObject, key)) {
|
|
390
|
-
const p = activeObject.oCoords[key];
|
|
391
|
-
if (p) {
|
|
392
|
-
control.render(
|
|
393
|
-
ctx,
|
|
394
|
-
p.x,
|
|
395
|
-
p.y,
|
|
396
|
-
{},
|
|
397
|
-
activeObject
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
});
|
|
402
|
-
|
|
403
|
-
ctx.restore();
|
|
404
|
-
}
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
destroy() {
|
|
409
|
-
console.log('pluginDestroy');
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
export default ControlsPlugin;
|
|
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
|
+
/**
|
|
16
|
+
* 统一的控制点样式配置
|
|
17
|
+
*/
|
|
18
|
+
const CONTROL_STYLES = {
|
|
19
|
+
transparentCorners: false,
|
|
20
|
+
borderColor: '#51B9F9',
|
|
21
|
+
cornerColor: '#FFF',
|
|
22
|
+
borderScaleFactor: 2.5,
|
|
23
|
+
cornerStyle: 'circle' as const,
|
|
24
|
+
cornerStrokeColor: 'rgba(0,0,0,0.25)', // 更淡的灰色边框
|
|
25
|
+
borderOpacityWhenMoving: 1,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
function drawImg(
|
|
29
|
+
ctx: CanvasRenderingContext2D,
|
|
30
|
+
left: number,
|
|
31
|
+
top: number,
|
|
32
|
+
img: HTMLImageElement,
|
|
33
|
+
wSize: number,
|
|
34
|
+
hSize: number,
|
|
35
|
+
angle: number | undefined
|
|
36
|
+
) {
|
|
37
|
+
if (angle === undefined) return;
|
|
38
|
+
ctx.save();
|
|
39
|
+
ctx.translate(left, top);
|
|
40
|
+
ctx.rotate(fabric.util.degreesToRadians(angle));
|
|
41
|
+
ctx.drawImage(img, -wSize / 2, -hSize / 2, wSize, hSize);
|
|
42
|
+
ctx.restore();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// 中间横杠
|
|
46
|
+
function intervalControl() {
|
|
47
|
+
const verticalImgIcon = document.createElement('img');
|
|
48
|
+
verticalImgIcon.src = verticalImg;
|
|
49
|
+
|
|
50
|
+
const horizontalImgIcon = document.createElement('img');
|
|
51
|
+
horizontalImgIcon.src = horizontalImg;
|
|
52
|
+
|
|
53
|
+
function renderIcon(
|
|
54
|
+
ctx: CanvasRenderingContext2D,
|
|
55
|
+
left: number,
|
|
56
|
+
top: number,
|
|
57
|
+
styleOverride: any,
|
|
58
|
+
fabricObject: fabric.Object
|
|
59
|
+
) {
|
|
60
|
+
drawImg(ctx, left, top, verticalImgIcon, 20, 25, fabricObject.angle);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function renderIconHoz(
|
|
64
|
+
ctx: CanvasRenderingContext2D,
|
|
65
|
+
left: number,
|
|
66
|
+
top: number,
|
|
67
|
+
styleOverride: any,
|
|
68
|
+
fabricObject: fabric.Object
|
|
69
|
+
) {
|
|
70
|
+
drawImg(ctx, left, top, horizontalImgIcon, 25, 20, fabricObject.angle);
|
|
71
|
+
}
|
|
72
|
+
// 中间横杠
|
|
73
|
+
fabric.Object.prototype.controls.ml = new fabric.Control({
|
|
74
|
+
x: -0.5,
|
|
75
|
+
y: 0,
|
|
76
|
+
offsetX: -1,
|
|
77
|
+
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
78
|
+
actionHandler: fabric.controlsUtils.scalingXOrSkewingY,
|
|
79
|
+
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
80
|
+
render: renderIcon,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
fabric.Object.prototype.controls.mr = new fabric.Control({
|
|
84
|
+
x: 0.5,
|
|
85
|
+
y: 0,
|
|
86
|
+
offsetX: 1,
|
|
87
|
+
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
88
|
+
actionHandler: fabric.controlsUtils.scalingXOrSkewingY,
|
|
89
|
+
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
90
|
+
render: renderIcon,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
fabric.Object.prototype.controls.mb = new fabric.Control({
|
|
94
|
+
x: 0,
|
|
95
|
+
y: 0.5,
|
|
96
|
+
offsetY: 1,
|
|
97
|
+
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
98
|
+
actionHandler: fabric.controlsUtils.scalingYOrSkewingX,
|
|
99
|
+
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
100
|
+
render: renderIconHoz,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
fabric.Object.prototype.controls.mt = new fabric.Control({
|
|
104
|
+
x: 0,
|
|
105
|
+
y: -0.5,
|
|
106
|
+
offsetY: -1,
|
|
107
|
+
cursorStyleHandler: fabric.controlsUtils.scaleSkewCursorStyleHandler,
|
|
108
|
+
actionHandler: fabric.controlsUtils.scalingYOrSkewingX,
|
|
109
|
+
getActionName: fabric.controlsUtils.scaleOrSkewActionName,
|
|
110
|
+
render: renderIconHoz,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 顶点
|
|
115
|
+
function peakControl() {
|
|
116
|
+
const img = document.createElement('img');
|
|
117
|
+
img.src = edgeImg;
|
|
118
|
+
|
|
119
|
+
function renderIconEdge(
|
|
120
|
+
ctx: CanvasRenderingContext2D,
|
|
121
|
+
left: number,
|
|
122
|
+
top: number,
|
|
123
|
+
styleOverride: any,
|
|
124
|
+
fabricObject: fabric.Object
|
|
125
|
+
) {
|
|
126
|
+
drawImg(ctx, left, top, img, 25, 25, fabricObject.angle);
|
|
127
|
+
}
|
|
128
|
+
// 四角图标
|
|
129
|
+
fabric.Object.prototype.controls.tl = new fabric.Control({
|
|
130
|
+
x: -0.5,
|
|
131
|
+
y: -0.5,
|
|
132
|
+
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
133
|
+
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
134
|
+
render: renderIconEdge,
|
|
135
|
+
});
|
|
136
|
+
fabric.Object.prototype.controls.bl = new fabric.Control({
|
|
137
|
+
x: -0.5,
|
|
138
|
+
y: 0.5,
|
|
139
|
+
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
140
|
+
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
141
|
+
render: renderIconEdge,
|
|
142
|
+
});
|
|
143
|
+
fabric.Object.prototype.controls.tr = new fabric.Control({
|
|
144
|
+
x: 0.5,
|
|
145
|
+
y: -0.5,
|
|
146
|
+
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
147
|
+
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
148
|
+
render: renderIconEdge,
|
|
149
|
+
});
|
|
150
|
+
fabric.Object.prototype.controls.br = new fabric.Control({
|
|
151
|
+
x: 0.5,
|
|
152
|
+
y: 0.5,
|
|
153
|
+
cursorStyleHandler: fabric.controlsUtils.scaleCursorStyleHandler,
|
|
154
|
+
actionHandler: fabric.controlsUtils.scalingEqually,
|
|
155
|
+
render: renderIconEdge,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// 删除
|
|
160
|
+
/*function deleteControl(canvas: fabric.Canvas) {
|
|
161
|
+
const deleteIcon =
|
|
162
|
+
"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";
|
|
163
|
+
const delImg = document.createElement('img');
|
|
164
|
+
delImg.src = deleteIcon;
|
|
165
|
+
|
|
166
|
+
function renderDelIcon(
|
|
167
|
+
ctx: CanvasRenderingContext2D,
|
|
168
|
+
left: number,
|
|
169
|
+
top: number,
|
|
170
|
+
styleOverride: any,
|
|
171
|
+
fabricObject: fabric.Object
|
|
172
|
+
) {
|
|
173
|
+
drawImg(ctx, left, top, delImg, 24, 24, fabricObject.angle);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// 删除选中元素
|
|
177
|
+
function deleteObject(mouseEvent: MouseEvent, target: fabric.Transform) {
|
|
178
|
+
if (target.action === 'rotate') return true;
|
|
179
|
+
const activeObject = canvas.getActiveObjects();
|
|
180
|
+
if (activeObject) {
|
|
181
|
+
activeObject.map((item) => canvas.remove(item));
|
|
182
|
+
canvas.requestRenderAll();
|
|
183
|
+
canvas.discardActiveObject();
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// 删除图标
|
|
189
|
+
fabric.Object.prototype.controls.deleteControl = new fabric.Control({
|
|
190
|
+
x: 0.5,
|
|
191
|
+
y: -0.5,
|
|
192
|
+
offsetY: -16,
|
|
193
|
+
offsetX: 16,
|
|
194
|
+
cursorStyle: 'pointer',
|
|
195
|
+
mouseUpHandler: deleteObject,
|
|
196
|
+
render: renderDelIcon,
|
|
197
|
+
// cornerSize: 24,
|
|
198
|
+
});
|
|
199
|
+
}*/
|
|
200
|
+
|
|
201
|
+
// 旋转
|
|
202
|
+
function rotationControl() {
|
|
203
|
+
const img = document.createElement('img');
|
|
204
|
+
img.src = rotateImg;
|
|
205
|
+
function renderIconRotate(
|
|
206
|
+
ctx: CanvasRenderingContext2D,
|
|
207
|
+
left: number,
|
|
208
|
+
top: number,
|
|
209
|
+
styleOverride: any,
|
|
210
|
+
fabricObject: fabric.Object
|
|
211
|
+
) {
|
|
212
|
+
drawImg(ctx, left, top, img, 40, 40, fabricObject.angle);
|
|
213
|
+
}
|
|
214
|
+
// 旋转图标
|
|
215
|
+
fabric.Object.prototype.controls.mtr = new fabric.Control({
|
|
216
|
+
x: 0,
|
|
217
|
+
y: 0.5,
|
|
218
|
+
cursorStyleHandler: fabric.controlsUtils.rotationStyleHandler,
|
|
219
|
+
actionHandler: fabric.controlsUtils.rotationWithSnapping,
|
|
220
|
+
offsetY: 30,
|
|
221
|
+
// withConnecton: false,
|
|
222
|
+
actionName: 'rotate',
|
|
223
|
+
render: renderIconRotate,
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
class ControlsPlugin implements IPluginTempl {
|
|
228
|
+
static pluginName = 'ControlsPlugin';
|
|
229
|
+
constructor(
|
|
230
|
+
public canvas: fabric.Canvas,
|
|
231
|
+
public editor: IEditor
|
|
232
|
+
) {
|
|
233
|
+
this.init();
|
|
234
|
+
}
|
|
235
|
+
init() {
|
|
236
|
+
// 删除图标
|
|
237
|
+
// deleteControl(this.canvas);
|
|
238
|
+
// 顶点图标
|
|
239
|
+
peakControl();
|
|
240
|
+
// 中间横杠图标
|
|
241
|
+
intervalControl();
|
|
242
|
+
// 旋转图标
|
|
243
|
+
rotationControl();
|
|
244
|
+
|
|
245
|
+
// 选中样式
|
|
246
|
+
fabric.Object.prototype.set(CONTROL_STYLES);
|
|
247
|
+
// textbox保持一致
|
|
248
|
+
// fabric.Textbox.prototype.controls = fabric.Object.prototype.controls;
|
|
249
|
+
|
|
250
|
+
// 自定义多选控制点渲染
|
|
251
|
+
this.customizeActiveSelection();
|
|
252
|
+
|
|
253
|
+
// 监听 loadJson 事件,重新应用控制点样式
|
|
254
|
+
this.editor.on('loadJson', () => {
|
|
255
|
+
this.applyControlStyles();
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 为画布上所有对象应用统一的控制点样式
|
|
261
|
+
*/
|
|
262
|
+
applyControlStyles() {
|
|
263
|
+
const objects = this.canvas.getObjects();
|
|
264
|
+
objects.forEach((obj) => {
|
|
265
|
+
// 跳过 workspace 等特殊对象
|
|
266
|
+
if (obj.id === 'workspace' || obj.id === 'coverMask') {
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
obj.set(CONTROL_STYLES);
|
|
270
|
+
});
|
|
271
|
+
this.canvas.renderAll();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* 自定义多选控制点,添加白色填充并确保在边框上方
|
|
276
|
+
*/
|
|
277
|
+
customizeActiveSelection() {
|
|
278
|
+
// 自定义控制点渲染函数
|
|
279
|
+
const renderCircleControl = (
|
|
280
|
+
ctx: CanvasRenderingContext2D,
|
|
281
|
+
left: number,
|
|
282
|
+
top: number,
|
|
283
|
+
styleOverride: any,
|
|
284
|
+
fabricObject: fabric.Object
|
|
285
|
+
) => {
|
|
286
|
+
const size = fabricObject.cornerSize || 10;
|
|
287
|
+
ctx.save();
|
|
288
|
+
ctx.translate(left, top);
|
|
289
|
+
ctx.rotate(fabric.util.degreesToRadians(fabricObject.angle || 0));
|
|
290
|
+
|
|
291
|
+
// 绘制阴影
|
|
292
|
+
ctx.shadowColor = 'rgba(0,0,0,0.15)';
|
|
293
|
+
ctx.shadowBlur = 3;
|
|
294
|
+
ctx.shadowOffsetX = 0;
|
|
295
|
+
ctx.shadowOffsetY = 1;
|
|
296
|
+
|
|
297
|
+
// 绘制白色填充的圆形
|
|
298
|
+
ctx.beginPath();
|
|
299
|
+
ctx.arc(0, 0, size / 2, 0, 2 * Math.PI, false);
|
|
300
|
+
ctx.fillStyle = '#FFF';
|
|
301
|
+
ctx.fill();
|
|
302
|
+
|
|
303
|
+
// 清除阴影,避免边框也有阴影
|
|
304
|
+
ctx.shadowColor = 'transparent';
|
|
305
|
+
ctx.shadowBlur = 0;
|
|
306
|
+
ctx.shadowOffsetX = 0;
|
|
307
|
+
ctx.shadowOffsetY = 0;
|
|
308
|
+
|
|
309
|
+
// 绘制更淡的灰色边框
|
|
310
|
+
ctx.strokeStyle = 'rgba(0,0,0,0.25)';
|
|
311
|
+
ctx.lineWidth = 1.5;
|
|
312
|
+
ctx.stroke();
|
|
313
|
+
|
|
314
|
+
ctx.restore();
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
// 为 ActiveSelection 设置自定义控制点
|
|
318
|
+
const controlKeys = ['tl', 'tr', 'bl', 'br', 'ml', 'mr', 'mt', 'mb', 'mtr'];
|
|
319
|
+
controlKeys.forEach((key) => {
|
|
320
|
+
if (fabric.ActiveSelection.prototype.controls[key]) {
|
|
321
|
+
const originalControl = fabric.ActiveSelection.prototype.controls[key];
|
|
322
|
+
fabric.ActiveSelection.prototype.controls[key] = new fabric.Control({
|
|
323
|
+
...originalControl,
|
|
324
|
+
render: renderCircleControl,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
// 应用样式配置
|
|
330
|
+
fabric.ActiveSelection.prototype.set(CONTROL_STYLES);
|
|
331
|
+
|
|
332
|
+
// 跟踪是否正在移动或缩放对象
|
|
333
|
+
let isTransforming = false;
|
|
334
|
+
|
|
335
|
+
// 监听对象移动开始
|
|
336
|
+
this.canvas.on('object:moving', (e: any) => {
|
|
337
|
+
if (e.target && e.target.type === 'activeSelection') {
|
|
338
|
+
isTransforming = true;
|
|
339
|
+
}
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
// 监听对象缩放开始
|
|
343
|
+
this.canvas.on('object:scaling', (e: any) => {
|
|
344
|
+
if (e.target && e.target.type === 'activeSelection') {
|
|
345
|
+
isTransforming = true;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
// 监听对象旋转开始
|
|
350
|
+
this.canvas.on('object:rotating', (e: any) => {
|
|
351
|
+
if (e.target && e.target.type === 'activeSelection') {
|
|
352
|
+
isTransforming = true;
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// 监听对象变换结束
|
|
357
|
+
this.canvas.on('object:modified', (e: any) => {
|
|
358
|
+
if (e.target && e.target.type === 'activeSelection') {
|
|
359
|
+
isTransforming = false;
|
|
360
|
+
this.canvas.requestRenderAll();
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
// 监听鼠标松开(防止某些情况下modified事件未触发)
|
|
365
|
+
this.canvas.on('mouse:up', () => {
|
|
366
|
+
if (isTransforming) {
|
|
367
|
+
isTransforming = false;
|
|
368
|
+
this.canvas.requestRenderAll();
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
// 监听canvas的after:render事件,在所有内容渲染完成后额外绘制多选控制点
|
|
373
|
+
this.canvas.on('after:render', () => {
|
|
374
|
+
// 如果正在变换(移动、缩放、旋转),不绘制控制点
|
|
375
|
+
if (isTransforming) return;
|
|
376
|
+
|
|
377
|
+
const activeObject = this.canvas.getActiveObject();
|
|
378
|
+
if (activeObject && activeObject.type === 'activeSelection') {
|
|
379
|
+
const ctx = this.canvas.getContext();
|
|
380
|
+
if (!ctx) return;
|
|
381
|
+
|
|
382
|
+
ctx.save();
|
|
383
|
+
|
|
384
|
+
// 确保控制点在最上层
|
|
385
|
+
ctx.globalCompositeOperation = 'source-over';
|
|
386
|
+
|
|
387
|
+
// 额外绘制一次控制点,确保在所有边框之上
|
|
388
|
+
activeObject.forEachControl((control: any, key: string) => {
|
|
389
|
+
if (control.getVisibility(activeObject, key)) {
|
|
390
|
+
const p = activeObject.oCoords[key];
|
|
391
|
+
if (p) {
|
|
392
|
+
control.render(
|
|
393
|
+
ctx,
|
|
394
|
+
p.x,
|
|
395
|
+
p.y,
|
|
396
|
+
{},
|
|
397
|
+
activeObject
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
});
|
|
402
|
+
|
|
403
|
+
ctx.restore();
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
destroy() {
|
|
409
|
+
console.log('pluginDestroy');
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export default ControlsPlugin;
|