zhengyiming
16 小时以前 a48be50fb38f21c6dd7ac8545c80d511783449ab
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
<template>
  <div class="box" v-if="isOne && isImage">
    <!-- <Divider plain orientation="left">图像描边</Divider> -->
    <Divider plain orientation="left">
      <h4>图像描边</h4>
    </Divider>
    <div class="hd-wrap">
      <div class="hd">
        <span>启用图像描边</span>
        <Poptip trigger="hover" content="只支持png透明图像">
          <span><Icon type="ios-alert" color="#f34250" /></span>
        </Poptip>
      </div>
 
      <iSwitch v-model="openImgStroke" size="large" class="switch" @on-change="onSwitchChange">
        <template #open>
          <span>开启</span>
        </template>
        <template #close>
          <span>关闭</span>
        </template>
      </iSwitch>
    </div>
 
    <template v-if="openImgStroke">
      <div class="hd-wrap">
        <div class="hd">
          <span>是否只显示描边</span>
        </div>
 
        <iSwitch v-model="isOnlyStroke" size="large" class="switch" @on-change="updateStroke">
          <template #open>
            <span>是</span>
          </template>
          <template #close>
            <span>否</span>
          </template>
        </iSwitch>
      </div>
      <div class="operation">
        <div class="hd" style="flex-basis: 98px">
          <span>描边大小</span>
        </div>
        <div style="width: 100%">
          <Slider v-model="strokeWidth" :max="50" @on-change="onSliderChange"></Slider>
        </div>
      </div>
 
      <div class="operation" style="justify-content: space-between">
        <div class="hd">
          <span>描边颜色</span>
        </div>
 
        <div>
          <ColorPicker v-model="strokeColor" @on-change="onColorChange" placement="left" />
        </div>
      </div>
    </template>
  </div>
</template>
 
<script name="ImgStroke" lang="ts" setup>
import useSelect from '@/hooks/select';
import { Slider } from 'view-ui-plus';
import { fabric } from 'fabric';
import { Utils } from '@kuaitu/core';
 
interface IExtendImage {
  [x: string]: any;
  originWidth?: number;
  originHeight?: number;
  originSrc?: string;
}
 
const { isOne, canvasEditor } = useSelect();
const isImage = ref(false);
const openImgStroke = ref(false);
const strokeWidth = ref(5);
const strokeColor = ref('#000');
const isOnlyStroke = ref(false);
const getActiveObject = (): (fabric.Image & IExtendImage) | undefined => {
  const activeObject = canvasEditor.fabricCanvas?.getActiveObject();
  if (!activeObject || !Utils.isImage(activeObject)) return;
  return activeObject;
};
 
const setOrigin = () => {
  const _activeObject = getActiveObject();
  if (!_activeObject) return;
  _activeObject.set('originWidth', _activeObject?.get('width'));
  _activeObject.set('originHeight', _activeObject?.get('height'));
  _activeObject.set('originSrc', _activeObject?.getSrc());
};
 
const updateStroke = () => {
  const strokeType = unref(isOnlyStroke) ? 'destination-out' : 'source-over';
  canvasEditor.imageStrokeDraw(unref(strokeColor), unref(strokeWidth), strokeType);
};
 
const closeImgStroke = () => {
  strokeWidth.value = 0;
  updateStroke();
};
 
const onSwitchChange = async (val: boolean) => {
  if (val) {
    unref(strokeWidth) === 0 && (strokeWidth.value = 5);
    setOrigin();
    updateStroke();
  } else {
    closeImgStroke();
  }
};
 
const onSliderChange = (val: number) => {
  strokeWidth.value = val;
  updateStroke();
};
 
const onColorChange = (val: string) => {
  strokeColor.value = val;
  updateStroke();
};
 
const handleSelectOne = () => {
  isImage.value = !!getActiveObject();
};
 
onMounted(() => {
  canvasEditor.on('selectOne', handleSelectOne);
});
 
onBeforeUnmount(() => {
  canvasEditor.off('selectOne', handleSelectOne);
});
</script>
 
<style lang="less" scoped>
// :deep(.ivu-divider-plain) {
//   &.ivu-divider-with-text-left {
//     margin: 10px 0;
//     font-weight: bold;
//     font-size: 16px;
//     color: #000000;
//   }
// }
.box {
  margin-bottom: 20px;
  .hd-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .hd {
      flex: 1;
      & > span {
        margin-right: 5px;
      }
    }
  }
  .operation {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .slide-wrap {
      width: 100%;
    }
  }
}
</style>