@hprint/plugins 0.0.4 → 0.0.6
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 +65 -65
- package/dist/index.mjs +6610 -6561
- package/dist/src/plugins/ControlsPlugin.d.ts +8 -0
- package/dist/src/plugins/ControlsPlugin.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/plugins/ControlsPlugin.ts +171 -9
|
@@ -5,6 +5,14 @@ declare class ControlsPlugin implements IPluginTempl {
|
|
|
5
5
|
static pluginName: string;
|
|
6
6
|
constructor(canvas: fabric.Canvas, editor: IEditor);
|
|
7
7
|
init(): void;
|
|
8
|
+
/**
|
|
9
|
+
* 为画布上所有对象应用统一的控制点样式
|
|
10
|
+
*/
|
|
11
|
+
applyControlStyles(): void;
|
|
12
|
+
/**
|
|
13
|
+
* 自定义多选控制点,添加白色填充并确保在边框上方
|
|
14
|
+
*/
|
|
15
|
+
customizeActiveSelection(): void;
|
|
8
16
|
destroy(): void;
|
|
9
17
|
}
|
|
10
18
|
export default ControlsPlugin;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ControlsPlugin.d.ts","sourceRoot":"","sources":["../../../src/plugins/ControlsPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAMtC,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"ControlsPlugin.d.ts","sourceRoot":"","sources":["../../../src/plugins/ControlsPlugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAMtC,OAAO,KAAK,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AA4N1D,cAAM,cAAe,YAAW,YAAY;IAG7B,MAAM,EAAE,MAAM,CAAC,MAAM;IACrB,MAAM,EAAE,OAAO;IAH1B,MAAM,CAAC,UAAU,SAAoB;gBAE1B,MAAM,EAAE,MAAM,CAAC,MAAM,EACrB,MAAM,EAAE,OAAO;IAI1B,IAAI;IAwBJ;;OAEG;IACH,kBAAkB;IAYlB;;OAEG;IACH,wBAAwB;IAmIxB,OAAO;CAGV;AAED,eAAe,cAAc,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hprint/plugins",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"qs": "~6.12.3",
|
|
23
23
|
"socket.io-client": "^4.8.1",
|
|
24
24
|
"uuid": "~8.3.2",
|
|
25
|
-
"@hprint/
|
|
26
|
-
"@hprint/
|
|
25
|
+
"@hprint/shared": "0.0.6",
|
|
26
|
+
"@hprint/core": "0.0.6"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/bwip-js": "^3.2.3",
|
|
@@ -12,6 +12,19 @@ import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
|
12
12
|
*/
|
|
13
13
|
fabric.Object.NUM_FRACTION_DIGITS = 4;
|
|
14
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
|
+
|
|
15
28
|
function drawImg(
|
|
16
29
|
ctx: CanvasRenderingContext2D,
|
|
17
30
|
left: number,
|
|
@@ -230,17 +243,166 @@ class ControlsPlugin implements IPluginTempl {
|
|
|
230
243
|
rotationControl();
|
|
231
244
|
|
|
232
245
|
// 选中样式
|
|
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
|
-
});
|
|
246
|
+
fabric.Object.prototype.set(CONTROL_STYLES);
|
|
242
247
|
// textbox保持一致
|
|
243
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
|
+
});
|
|
244
406
|
}
|
|
245
407
|
|
|
246
408
|
destroy() {
|