zhengyiming
11 小时以前 d7c8a3a9e1fc5c8e596a17cdadb7079d20e52297
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<template>
  <div>
    <el-divider content-position="left">基础要素</el-divider>
    <div class="tool-box">
      <span @click="() => addText()" :draggable="true" @dragend="addText">
        <textIcon width="26" height="26"></textIcon>
      </span>
      <span @click="() => addTextBox()" :draggable="true" @dragend="addTextBox">
        <textBoxIcon width="26" height="26"></textBoxIcon>
      </span>
      <span @click="() => addRect()" :draggable="true" @dragend="addRect">
        <rectIcon width="26" height="26"></rectIcon>
      </span>
      <span @click="() => addCircle()" :draggable="true" @dragend="addCircle">
        <circleIcon width="26" height="26"></circleIcon>
      </span>
      <span @click="() => addTriangle()" :draggable="true" @dragend="addTriangle">
        <triangleIcon width="26" height="26"></triangleIcon>
      </span>
 
      <span @click="() => addPolygon()" :draggable="true" @dragend="addPolygon">
        <polygonIcon width="26" height="26"></polygonIcon>
      </span>
    </div>
  </div>
</template>
 
<script setup name="Tools">
import { getPolygonVertices } from '@/fabric-editor/utils/math';
import useSelect from '@/fabric-editor/hooks/select';
import circleIcon from '@/fabric-editor/assets/icon/tools/circle.svg?component';
// import draw1Icon from '@/assets/icon/tools/draw1.svg';
// import draw2Icon from '@/assets/icon/tools/draw2.svg';
// import draw3Icon from '@/assets/icon/tools/draw3.svg';
// import draw4Icon from '@/assets/icon/tools/draw4.svg';
 
import polygonIcon from '@/fabric-editor/assets/icon/tools/polygon.svg?component';
import rectIcon from '@/fabric-editor/assets/icon/tools/rect.svg?component';
import textIcon from '@/fabric-editor/assets/icon/tools/text.svg?component';
import textBoxIcon from '@/fabric-editor/assets/icon/tools/textBox.svg?component';
import triangleIcon from '@/fabric-editor/assets/icon/tools/triangle.svg?component';
 
// import qrCodeIcon from '@/assets/icon/tools/qrCode.svg';
// import barCodeIcon from '@/assets/icon/tools/barCode.svg';
 
// import useCalculate from '@/hooks/useCalculate';
// const { getCanvasBound, isOutsideCanvas } = useCalculate();
 
const LINE_TYPE = {
  polygon: 'polygon',
  freeDraw: 'freeDraw',
  pathText: 'pathText',
};
// 默认属性
const defaultPosition = { shadow: '', fontFamily: 'arial' };
 
const { fabric, canvasEditor } = useSelect();
const state = reactive({
  isDrawingLineMode: false,
  lineType: false,
});
 
const addText = (event) => {
  cancelDraw();
  const text = new fabric.IText('万事大吉', {
    ...defaultPosition,
    fontSize: 40,
    fill: '#000000FF',
  });
 
  console.log('canvasEditor: ', canvasEditor);
  canvasEditor.addBaseType(text, { center: true, event });
};
 
const addTextBox = (event) => {
  cancelDraw();
  const text = new fabric.Textbox('诸事顺遂', {
    ...defaultPosition,
    splitByGrapheme: true,
    width: 400,
    fontSize: 80,
    fill: '#000000FF',
  });
 
  canvasEditor.addBaseType(text, { center: true, event });
};
 
const addTriangle = (event) => {
  cancelDraw();
  const triangle = new fabric.Triangle({
    ...defaultPosition,
    width: 400,
    height: 400,
    fill: '#92706BFF',
    name: '三角形',
  });
  canvasEditor.addBaseType(triangle, { center: true, event });
};
 
const addPolygon = (event) => {
  cancelDraw();
  const polygon = new fabric.Polygon(getPolygonVertices(5, 200), {
    ...defaultPosition,
    fill: '#CCCCCCFF',
    name: '多边形',
  });
  polygon.set({
    // 创建完设置宽高,不然宽高会变成自动的值
    width: 400,
    height: 400,
    // 关闭偏移
    pathOffset: {
      x: 0,
      y: 0,
    },
  });
  canvasEditor.addBaseType(polygon, { center: true, event });
};
 
const addCircle = (event) => {
  cancelDraw();
  const circle = new fabric.Circle({
    ...defaultPosition,
    radius: 150,
    fill: '#57606BFF',
    // id: uuid(),
    name: '圆形',
  });
  canvasEditor.addBaseType(circle, { center: true, event });
};
 
const addRect = (event) => {
  cancelDraw();
  const rect = new fabric.Rect({
    ...defaultPosition,
    fill: '#F57274FF',
    width: 400,
    height: 400,
    name: '矩形',
  });
 
  canvasEditor.addBaseType(rect, { center: true, event });
};
const drawPolygon = () => {
  const onEnd = () => {
    state.lineType = false;
    state.isDrawingLineMode = false;
    ensureObjectSelEvStatus(!state.isDrawingLineMode, !state.isDrawingLineMode);
  };
  if (state.lineType !== LINE_TYPE.polygon) {
    endConflictTools();
    endDrawingLineMode();
    state.lineType = LINE_TYPE.polygon;
    state.isDrawingLineMode = true;
    canvasEditor.beginDrawPolygon(onEnd);
    canvasEditor.endDraw();
    ensureObjectSelEvStatus(!state.isDrawingLineMode, !state.isDrawingLineMode);
  } else {
    canvasEditor.discardPolygon();
  }
};
 
const drawPathText = () => {
  if (state.lineType === LINE_TYPE.pathText) {
    state.lineType = false;
    state.isDrawingLineMode = false;
    canvasEditor.endTextPathDraw();
  } else {
    endConflictTools();
    endDrawingLineMode();
    state.lineType = LINE_TYPE.pathText;
    state.isDrawingLineMode = true;
    canvasEditor.startTextPathDraw();
  }
};
 
const freeDraw = () => {
  if (state.lineType === LINE_TYPE.freeDraw) {
    canvasEditor.endDraw();
    state.lineType = false;
    state.isDrawingLineMode = false;
  } else {
    endConflictTools();
    endDrawingLineMode();
    state.lineType = LINE_TYPE.freeDraw;
    state.isDrawingLineMode = true;
    canvasEditor.startDraw({ width: 20 });
  }
};
 
const endConflictTools = () => {
  canvasEditor.discardPolygon();
  canvasEditor.endDraw();
  canvasEditor.endTextPathDraw();
};
const endDrawingLineMode = () => {
  state.isDrawingLineMode = false;
  state.lineType = '';
  canvasEditor.setMode(state.isDrawingLineMode);
  canvasEditor.setLineType(state.lineType);
};
const drawingLineModeSwitch = (type) => {
  if ([LINE_TYPE.polygon, LINE_TYPE.freeDraw, LINE_TYPE.pathText].includes(state.lineType)) {
    endConflictTools();
  }
  if (state.lineType === type) {
    state.isDrawingLineMode = false;
    state.lineType = '';
  } else {
    state.isDrawingLineMode = true;
    state.lineType = type;
  }
  canvasEditor.setMode(state.isDrawingLineMode);
  canvasEditor.setLineType(type);
  // this.canvasEditor.setMode(this.isDrawingLineMode);
  // this.canvasEditor.setArrow(isArrow);
  ensureObjectSelEvStatus(!state.isDrawingLineMode, !state.isDrawingLineMode);
};
 
const ensureObjectSelEvStatus = (evented, selectable) => {
  canvasEditor.canvas.forEachObject((obj) => {
    if (obj.id !== 'workspace') {
      obj.selectable = selectable;
      obj.evented = evented;
    }
  });
};
 
// 退出绘制状态
const cancelDraw = () => {
  if (!state.isDrawingLineMode) return;
  state.isDrawingLineMode = false;
  state.lineType = '';
  canvasEditor.setMode(false);
  endConflictTools();
  ensureObjectSelEvStatus(true, true);
};
 
onDeactivated(() => {
  cancelDraw();
});
</script>
 
<style scoped lang="scss">
@use '@/style/common.scss' as *;
 
.tool-box {
  display: flex;
  justify-content: space-around;
 
  span {
    margin-left: 2px;
    padding: 5px 0;
    text-align: center;
    background: #f6f6f6;
    flex: 1;
    cursor: pointer;
 
    &:hover {
      background: #edf9ff;
 
      svg {
        fill: getCssVar('color', 'primary');
      }
    }
  }
 
  .bg {
    background: #d8d8d8;
 
    &:hover {
      svg {
        fill: getCssVar('color', 'primary');
      }
    }
  }
}
 
.img {
  width: 20px;
}
</style>