-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathattributeColor.vue
252 lines (235 loc) · 6.51 KB
/
attributeColor.vue
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
<!--
* @Author: 秦少卫
* @Date: 2024-05-21 10:59:48
* @LastEditors: June [email protected]
* @LastEditTime: 2024-06-20 17:46:45
* @Description: 渐变
-->
<template>
<div
class="box attr-item-box"
v-if="
mixinState.mSelectMode === 'one' &&
mixinState.mSelectOneType !== 'image' &&
mixinState.mSelectOneType !== 'group'
"
>
<Divider plain orientation="left">
<!-- <h4>颜色 / 纹理</h4> -->
<Switch size="large" true-color="#13ce66" false-color="#ff4949" @on-change="colorshowChange">
<template #open>
<span>颜色</span>
</template>
<template #close>
<span>纹理</span>
</template>
</Switch>
</Divider>
<!-- 通用属性 -->
<div class="bg-item" v-if="colorShow">
<Tooltip placement="top" theme="light">
<div class="color-bar" :style="{ background: baseAttr.fill }"></div>
<template #content>
<color-picker
v-model:value="baseAttr.fill"
:modes="['渐变', '纯色']"
@change="colorChange"
@nativePick="dropColor"
></color-picker>
</template>
</Tooltip>
</div>
<div style="margin: 10px; display: flex" v-if="!colorShow">
<Upload action="#" :on-error="updateTexture" style="width: 50%">
<Button type="primary">字体纹理</Button>
</Upload>
<!-- 可以自己上传图片作为背景 -->
<Select
v-model="fontTextureDirection"
style="width: 150px; margin-left: 20px"
placeholder="纹理方向"
@on-change="changeTexture"
>
<Option v-for="item in fontTextureList" :value="item.value" :key="item.value">
{{ item.label }}
</Option>
</Select>
</div>
<!-- <Divider plain></Divider> -->
</div>
</template>
<script setup name="AttrBute">
import useSelect from '@/hooks/select';
import colorPicker from './color-picker';
import { toRaw } from 'vue';
const update = getCurrentInstance();
const { fabric, mixinState, canvasEditor } = useSelect();
const angleKey = 'gradientAngle';
// 属性值
const baseAttr = reactive({
fill: '#ffffffff',
});
const colorShow = ref(true);
const fontTextureList = reactive([
{
label: 'repeat',
value: 'repeat',
},
{
label: 'repeat-x',
value: 'repeat-x',
},
{
label: 'repeat-y',
value: 'repeat-y',
},
{
label: 'no-repeat',
value: 'no-repeat',
},
]);
const fontTextureDirection = ref('repeat');
const colorshowChange = () => {
colorShow.value = !colorShow.value;
};
// 属性获取
const getObjectAttr = (e) => {
const activeObject = canvasEditor.canvas.getActiveObject();
// 不是当前obj,跳过
if (e && e.target && e.target !== activeObject) return;
if (activeObject && mixinState.mSelectMode === 'one') {
const fill = activeObject.get('fill');
if (typeof fill === 'string' && !fill.source) {
baseAttr.fill = fill;
} else if (!fill.source) {
baseAttr.fill = fabricGradientToCss(fill, activeObject);
} else {
colorShow.value = false;
return;
}
}
};
const updateTexture = (event, file, fileList) => {
const reader = new FileReader();
reader.onload = function (e) {
const activeObject = canvasEditor.canvas.getActiveObject();
fabric.util.loadImage(e.target.result, function (img) {
activeObject.set(
'fill',
new fabric.Pattern({
source: img,
repeat: fontTextureDirection.value,
})
);
canvasEditor.canvas.renderAll();
});
};
reader.readAsDataURL(fileList);
};
const changeTexture = () => {
const activeObject = canvasEditor.canvas.getActiveObject();
activeObject.set(
'fill',
new fabric.Pattern({
source: activeObject.fill.source,
repeat: fontTextureDirection.value,
})
);
canvasEditor.canvas.renderAll();
};
const colorChange = (value) => {
const activeObject = canvasEditor.canvas.getActiveObjects()[0];
if (activeObject) {
const color = String(value.color).replace('NaN', '');
if (value.mode === '纯色') {
activeObject.set('fill', color);
} else if (value.mode === '渐变') {
const currentGradient = cssToFabricGradient(
toRaw(value.stops),
activeObject.width,
activeObject.height,
value.angle
);
activeObject.set('fill', currentGradient, value.angle);
activeObject.set(angleKey, value.angle);
}
canvasEditor.canvas.renderAll();
}
};
const dropColor = (value) => {
colorChange(value);
};
const fabricGradientToCss = (val, activeObject) => {
// 渐变类型
if (!val) return;
if (activeObject.fill.source) return;
const angle = activeObject.get(angleKey, val.degree);
const colorStops = val.colorStops.map((item) => {
return item.color + ' ' + item.offset * 100 + '%';
});
return `linear-gradient(${angle}deg, ${colorStops})`;
};
// css转Fabric渐变
const cssToFabricGradient = (stops, width, height, angle) => {
const gradAngleToCoords = (paramsAngle) => {
const anglePI = -parseInt(paramsAngle, 10) * (Math.PI / 180);
return {
x1: Math.round(50 + Math.sin(anglePI) * 50) / 100,
y1: Math.round(50 + Math.cos(anglePI) * 50) / 100,
x2: Math.round(50 + Math.sin(anglePI + Math.PI) * 50) / 100,
y2: Math.round(50 + Math.cos(anglePI + Math.PI) * 50) / 100,
};
};
const angleCoords = gradAngleToCoords(angle);
return new fabric.Gradient({
type: 'linear',
gradientUnits: 'pencentage', // pixels or pencentage 像素 或者 百分比
coords: {
x1: angleCoords.x1 * width,
y1: angleCoords.y1 * height,
x2: angleCoords.x2 * width,
y2: angleCoords.y2 * height,
},
colorStops: [...stops],
});
};
const selectCancel = () => {
update?.proxy?.$forceUpdate();
};
onMounted(() => {
// 获取字体数据
getObjectAttr();
canvasEditor.on('selectCancel', selectCancel);
canvasEditor.on('selectOne', getObjectAttr);
canvasEditor.canvas.on('object:modified', getObjectAttr);
});
onBeforeUnmount(() => {
canvasEditor.off('selectCancel', selectCancel);
canvasEditor.off('selectOne', getObjectAttr);
canvasEditor.canvas.off('object:modified', getObjectAttr);
});
</script>
<style scoped lang="less">
.color-bar {
// width: 30px;
height: 30px;
cursor: pointer;
border: 2px solid #f6f7f9;
}
:deep(.ivu-input-number) {
display: block;
width: 100%;
}
:deep(.ivu-tooltip) {
display: flex;
}
.ivu-form-item {
background: #f6f7f9;
border-radius: 5px;
padding: 0 5px;
margin-bottom: 10px;
}
.ivu-row {
margin-bottom: 10px;
}
</style>