Skip to content

Commit ffe2fb6

Browse files
committed
feat(plugin): 添加mask插件开关
1 parent b99e5f3 commit ffe2fb6

File tree

5 files changed

+41
-86
lines changed

5 files changed

+41
-86
lines changed

.eslintrc-auto-import.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,4 @@
6464
"watchPostEffect": true,
6565
"watchSyncEffect": true
6666
}
67-
}
67+
}

packages/core/plugin/MaskPlugin.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class MaskPlugin implements IPluginTempl {
2727
throw new Error('element #workspace is missing, plz check!');
2828
}
2929
this.workspaceEl = workspaceEl;
30-
this.workspaceMaskToggle();
3130
}
3231

3332
/**
@@ -36,17 +35,16 @@ class MaskPlugin implements IPluginTempl {
3635
*/
3736
workspaceMaskToggle() {
3837
const workspaceMask = this.getWorkspaceMask();
39-
console.log('是睡觉觉睡觉觉睡觉觉', workspaceMask);
4038
if (!workspaceMask) {
4139
this.initMask();
4240
} else {
43-
const workspace = this.getWorkspase();
4441
// 如果有 则删除
4542
workspaceMask && this.canvas.remove(workspaceMask);
46-
workspace?.clone((cloned: fabric.Rect) => {
43+
this.workspace?.clone((cloned: fabric.Rect) => {
4744
this.canvas.clipPath = cloned;
4845
this.canvas.requestRenderAll();
4946
});
47+
this.editor.off('loadJson', this.initMask);
5048
}
5149
}
5250

packages/core/plugin/WorkspacePlugin.ts

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type IEditor = Editor;
1313

1414
class WorkspacePlugin implements IPluginTempl {
1515
static pluginName = 'WorkspacePlugin';
16-
static events = ['sizeChange', 'zoomChange'];
16+
static events = ['sizeChange'];
1717
static apis = [
1818
'big',
1919
'small',
@@ -148,69 +148,6 @@ class WorkspacePlugin implements IPluginTempl {
148148
this.auto();
149149
}
150150

151-
setCoverMask(hack = false) {
152-
if (!this.coverMask || !this.workspace) {
153-
return;
154-
}
155-
const center = this.canvas.getCenter();
156-
const zoom = this.canvas.getZoom();
157-
this.canvas.zoomToPoint(
158-
new fabric.Point(center.left, center.top),
159-
hack ? zoom - 0.0000001 : zoom // 比较hack的方法,判断为fabric内部的数据更新问题
160-
);
161-
if (zoom) {
162-
const { workspaceEl } = this;
163-
const width = workspaceEl.offsetWidth;
164-
const height = workspaceEl.offsetHeight;
165-
const cWidth = width / zoom;
166-
const cHeight = height / zoom;
167-
this.coverMask.width = cWidth;
168-
this.coverMask.height = cHeight;
169-
this.coverMask.left = (this.workspace.left || 0) + (this.workspace.width! - cWidth) / 2;
170-
this.coverMask.top = (this.workspace.top || 0) + (this.workspace.height! - cHeight) / 2;
171-
this.workspace.clone((clone: fabric.Rect) => {
172-
clone.left = -clone.width! / 2;
173-
clone.top = -clone.height! / 2;
174-
clone.inverted = true;
175-
this.coverMask!.objectCaching = false;
176-
this.coverMask!.clipPath = clone;
177-
this.canvas.requestRenderAll();
178-
});
179-
}
180-
}
181-
182-
clipPath() {
183-
if (this.coverMask) {
184-
return;
185-
}
186-
// 超出画布不展示
187-
this.workspace?.clone((cloned: fabric.Rect) => {
188-
this.canvas.clipPath = cloned;
189-
this.canvas.requestRenderAll();
190-
});
191-
}
192-
193-
maskEnable(needBindLoadJSON = true) {
194-
const coverMask = new fabric.Rect({
195-
fill: 'rgba(0,0,0,0.7)',
196-
id: 'coverMask',
197-
strokeWidth: 0,
198-
});
199-
coverMask.set('selectable', false);
200-
coverMask.set('hasControls', false);
201-
coverMask.set('evented', false);
202-
coverMask.hoverCursor = 'default';
203-
this.canvas.on('object:added', () => {
204-
coverMask.bringToFront();
205-
});
206-
this.canvas.clipPath = undefined;
207-
this.canvas.add(coverMask);
208-
this.coverMask = coverMask;
209-
this.setCoverMask();
210-
// 适配模板和psd的loadjson,在加载完成后再入mask
211-
needBindLoadJSON && this.editor.on('loadJson', () => this.maskEnable(false));
212-
}
213-
214151
setZoomAuto(scale: number, cb?: (left?: number, top?: number) => void) {
215152
const { workspaceEl } = this;
216153
const width = workspaceEl.offsetWidth;
@@ -222,8 +159,7 @@ class WorkspacePlugin implements IPluginTempl {
222159
this.canvas.zoomToPoint(new fabric.Point(center.left, center.top), scale);
223160
if (!this.workspace) return;
224161
this.setCenterFromObject(this.workspace);
225-
// console.log(this.editor, this.canvas)
226-
// this.editor && this.editor?.workspaceMaskToggle();
162+
this.editor.getPlugin('MaskPlugin') && this.editor?.setCoverMask(true);
227163

228164
if (cb) cb(this.workspace.left, this.workspace.top);
229165
}
@@ -281,25 +217,13 @@ class WorkspacePlugin implements IPluginTempl {
281217
const center = this.canvas.getCenter();
282218
this.canvas.zoomToPoint(new fabric.Point(center.left, center.top), zoom);
283219

284-
// this.editor && this.editor?.workspaceMaskToggle()
220+
this.editor.getPlugin('MaskPlugin') && this.editor?.setCoverMask(true);
285221

286222
opt.e.preventDefault();
287223
opt.e.stopPropagation();
288224
});
289225
}
290226

291-
clipPathOrRefreshMask() {
292-
if (this.editor.getPlugin('MaskPlugin')) {
293-
this.editor.setCoverMask(true);
294-
} else {
295-
// 超出画布不展示
296-
this.workspace?.clone((cloned: fabric.Rect) => {
297-
this.canvas.clipPath = cloned;
298-
this.canvas.requestRenderAll();
299-
});
300-
}
301-
}
302-
303227
destroy() {
304228
this.resizeObserver.disconnect();
305229
this.canvas.off();

src/components/bgBar.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
<h4>蒙版</h4>
3030
</Divider>
3131

32-
<workspaceMaskVue />
32+
<workspaceMask />
3333
</div>
3434
</div>
3535
</template>
3636

3737
<script setup name="BgBar">
38-
import workspaceMaskVue from './workspaceMask.vue';
38+
import workspaceMask from './workspaceMask.vue';
3939
import { ref } from 'vue';
4040
import useSelect from '@/hooks/select';
4141
const { mixinState, canvasEditor } = useSelect();

src/components/workspaceMask.vue

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<template>
2+
<div class="mask-wrap">
3+
<div>开启背景蒙版</div>
4+
5+
<iSwitch v-model="openMask" size="large" @on-change="onMaskChange">
6+
<template #open>
7+
<span>开启</span>
8+
</template>
9+
<template #close>
10+
<span>关闭</span>
11+
</template>
12+
</iSwitch>
13+
</div>
14+
</template>
15+
16+
<script lang="ts" setup>
17+
import useSelect from '@/hooks/select';
18+
19+
const { canvasEditor }: any = useSelect();
20+
21+
const openMask = ref(false);
22+
const onMaskChange = () => {
23+
canvasEditor?.workspaceMaskToggle();
24+
};
25+
</script>
26+
27+
<style lang="less" scoped>
28+
.mask-wrap {
29+
display: flex;
30+
justify-content: space-between;
31+
align-items: center;
32+
}
33+
</style>

0 commit comments

Comments
 (0)