Skip to content

Commit fc4cd1a

Browse files
committed
feat(components): 添加蒙版开关
1 parent 731ecae commit fc4cd1a

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

packages/core/plugin/MaskPlugin.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ type IEditor = Editor;
1212

1313
class MaskPlugin implements IPluginTempl {
1414
static pluginName = 'MaskPlugin';
15-
static apis = ['setCoverMask'];
15+
static apis = ['setCoverMask', 'workspaceMaskToggle'];
1616
coverMask: null | fabric.Rect = null;
1717
workspace: null | fabric.Rect = null;
1818
workspaceEl!: HTMLElement;
1919
hackFlag = false;
20-
2120
constructor(public canvas: fabric.Canvas, public editor: IEditor) {
2221
this.init();
2322
}
@@ -28,7 +27,35 @@ class MaskPlugin implements IPluginTempl {
2827
throw new Error('element #workspace is missing, plz check!');
2928
}
3029
this.workspaceEl = workspaceEl;
31-
this.initMask();
30+
this.workspaceMaskToggle();
31+
}
32+
33+
/**
34+
* @desc 蒙版开关
35+
* @param val Boolean false
36+
*/
37+
workspaceMaskToggle() {
38+
const workspaceMask = this.getWorkspaceMask();
39+
console.log('是睡觉觉睡觉觉睡觉觉', workspaceMask);
40+
if (!workspaceMask) {
41+
this.initMask();
42+
} else {
43+
const workspace = this.getWorkspase();
44+
// 如果有 则删除
45+
workspaceMask && this.canvas.remove(workspaceMask);
46+
workspace?.clone((cloned: fabric.Rect) => {
47+
this.canvas.clipPath = cloned;
48+
this.canvas.requestRenderAll();
49+
});
50+
}
51+
}
52+
53+
/**
54+
* @desc 获取蒙版
55+
* @returns Object
56+
*/
57+
getWorkspaceMask() {
58+
return this.canvas.getObjects().find((item) => item.id === 'coverMask') as fabric.Rect;
3259
}
3360

3461
// 返回workspace对象
@@ -76,7 +103,7 @@ class MaskPlugin implements IPluginTempl {
76103
throw new Error('MaskPlugin must be used after WorkspacePlugin!');
77104
}
78105
const coverMask = new fabric.Rect({
79-
fill: 'rgba(0,0,0,0.7)',
106+
fill: 'rgba(0,0,0,0.5)',
80107
id: 'coverMask',
81108
strokeWidth: 0,
82109
});

packages/core/plugin/WorkspacePlugin.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class WorkspacePlugin implements IPluginTempl {
5151
this._initWorkspace();
5252
this._initResizeObserve();
5353
this._bindWheel();
54-
this._bindSizeChange();
5554
}
5655

5756
hookImportAfter() {
@@ -223,8 +222,8 @@ class WorkspacePlugin implements IPluginTempl {
223222
this.canvas.zoomToPoint(new fabric.Point(center.left, center.top), scale);
224223
if (!this.workspace) return;
225224
this.setCenterFromObject(this.workspace);
226-
227-
this.editor.emit('zoomChange');
225+
// console.log(this.editor, this.canvas)
226+
// this.editor && this.editor?.workspaceMaskToggle();
228227

229228
if (cb) cb(this.workspace.left, this.workspace.top);
230229
}
@@ -282,7 +281,7 @@ class WorkspacePlugin implements IPluginTempl {
282281
const center = this.canvas.getCenter();
283282
this.canvas.zoomToPoint(new fabric.Point(center.left, center.top), zoom);
284283

285-
this.editor.emit('zoomChange');
284+
// this.editor && this.editor?.workspaceMaskToggle()
286285

287286
opt.e.preventDefault();
288287
opt.e.stopPropagation();
@@ -301,11 +300,6 @@ class WorkspacePlugin implements IPluginTempl {
301300
}
302301
}
303302

304-
private _bindSizeChange() {
305-
this.editor.on('sizeChange', () => this.clipPathOrRefreshMask());
306-
this.editor.on('zoomChange', () => this.clipPathOrRefreshMask());
307-
}
308-
309303
destroy() {
310304
this.resizeObserver.disconnect();
311305
this.canvas.off();

src/components/bgBar.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,19 @@
2323
</template>
2424
</div>
2525
</div>
26+
27+
<div>
28+
<Divider plain orientation="left">
29+
<h4>蒙版</h4>
30+
</Divider>
31+
32+
<workspaceMaskVue />
33+
</div>
2634
</div>
2735
</template>
2836

2937
<script setup name="BgBar">
38+
import workspaceMaskVue from './workspaceMask.vue';
3039
import { ref } from 'vue';
3140
import useSelect from '@/hooks/select';
3241
const { mixinState, canvasEditor } = useSelect();

0 commit comments

Comments
 (0)