link.vue
8.51 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!--
* User: CHT
* Date: 2020/5/25
* Time: 9:11
-->
<template>
<canvas class="super-flow__line"></canvas>
</template>
<script>
import { isFun, isObject, isString, vector } from './utils'
export default {
props: {
padding: {
type: Number,
default: 50,
},
linkDesc: [Function, null],
linkStyle: [Function, null],
linkBaseStyle: Object,
index: Number,
graph: Object,
link: Object,
},
data() {
return {
top: 0,
right: 0,
bottom: 0,
left: 0,
currentPointList: [],
currentPathPointList: null,
}
},
computed: {
styles() {
return Object.assign(
{
hover: '#FF0000',
color: '#666666',
textColor: '#666666',
textHover: '#FF0000',
font: '14px Arial',
dotted: false,
lineDash: [4, 4],
background: 'rgba(255,255,255,0.6)',
},
this.linkBaseStyle
)
},
inPath: {
get() {
return this.graph.mouseOnLink === this.link
},
set(bol) {
if (bol && !this.graph.mouseonNode) {
this.graph.mouseOnLink = this.link
this.$el.style.zIndex = '1'
this.graph.toLastLink(this.index)
} else if (this.inPath) {
this.graph.mouseOnLink = null
this.$el.style.zIndex = '0'
}
},
},
},
watch: {
'link.pathPointList'() {
this.draw()
},
inPath() {
this.initLine()
},
'link.meta': {
deep: true,
handler() {
this.draw()
},
},
linkBaseStyle: {
deep: true,
handler() {
this.draw()
},
},
},
mounted() {
this.ctx = this.$el.getContext('2d')
this.draw()
this.graph.add('mousemove', this.rootMousemove)
this.$once('hook:beforeDestroy', () => {
this.graph.remove('mousemove', this.rootMousemove)
})
},
methods: {
draw() {
const { pointList, minX, minY, maxX, maxY } =
(this.currentPathPointList = this.link.pathPointList)
this.top = minY - this.padding
this.right = maxX + this.padding
this.bottom = maxY + this.padding
this.left = minX - this.padding
this.currentPointList = pointList.map((point) => {
return [
Math.floor(point[0] - this.left),
Math.floor(point[1] - this.top),
]
})
this.changeStyle()
this.initLine()
},
changeStyle() {
this.$el.width = this.right - this.left
this.$el.height = this.bottom - this.top
this.$el.style.top = this.top + 'px'
this.$el.style.left = this.left + 'px'
},
initLine() {
this.ctx.clearRect(0, 0, this.$el.width, this.$el.height)
if (this.linkStyle) {
const style = this.linkStyle(this.link)
if (isObject(style)) {
Object.assign(this.styles, style)
}
}
if (this.inPath) {
const color = this.styles.hover
const textColor = this.styles.textHover
this.drawLine(color)
this.drawDesc(textColor)
this.drawArrow(color)
} else {
const color = this.styles.color
const textColor = this.styles.textColor
this.drawLine(color)
this.drawDesc(textColor)
this.drawArrow(color)
}
},
drawLine(strokeStyle) {
const lineWidth = 2
const ctx = this.ctx
ctx.lineJoin = 'round'
ctx.beginPath()
if (this.styles.dotted) {
ctx.setLineDash(this.styles.lineDash)
}
ctx.lineWidth = lineWidth
ctx.strokeStyle = strokeStyle
this.currentPointList.forEach((point, idx) => {
if (idx === 0) {
ctx.moveTo(...point)
} else {
ctx.lineTo(...point)
ctx.stroke()
}
})
ctx.save()
},
drawDesc(color) {
const ctx = this.ctx
let desc
if (isFun(this.linkDesc)) {
desc = this.linkDesc(this.link)
}
if (isString(desc)) {
const { font, background } = this.styles
this.ctx.font = font
this.ctx.textAlign = 'center'
this.ctx.textBaseline = 'middle'
const { text, width } = this.descIntercept(desc)
const height = parseInt(font.match(/(\d+px)/g)[0])
const descPosition = this.descPosition()
const position = vector(descPosition).minus([
width / 2,
height / 2,
]).end
this.ctx.fillStyle = background
ctx.fillRect(position[0], position[1], width, height)
this.ctx.fillStyle = color
ctx.fillText(text, ...descPosition)
}
},
descPosition() {
let lineLen = 0
let contrastLen = 0
let descPosition = null
this.currentPointList.reduce((prev, current) => {
const vec = vector(prev).minus(current).end
lineLen += Math.abs(vec[0]) + Math.abs(vec[1])
return current
})
this.currentPointList.reduce((prev, current) => {
const vec = vector(prev).minus(current).end
const size = Math.abs(vec[0]) + Math.abs(vec[1])
const prevLen = contrastLen
const middle = lineLen / 2
contrastLen += size
if (contrastLen >= lineLen / 2 && descPosition === null) {
descPosition = prev
if (prev[0] === current[0]) {
if (prev[1] < current[1]) {
descPosition = vector(prev).add([0, middle - prevLen]).end
} else {
descPosition = vector(prev).add([0, -(middle - prevLen)]).end
}
} else {
if (prev[0] < current[0]) {
descPosition = vector(prev).add([middle - prevLen, 0]).end
} else {
descPosition = vector(prev).add([-(middle - prevLen), 0]).end
}
}
}
return current
})
return descPosition
},
descIntercept(str) {
const ctx = this.ctx
let strWidth = ctx.measureText(str).width
const maxWidth = this.padding * 2 - 10
const ellipsis = '...'
const ellipsisWidth = ctx.measureText(ellipsis).width
if (strWidth <= maxWidth || maxWidth <= ellipsisWidth) {
return {
text: str,
width: strWidth,
}
} else {
let len = str.length
while (strWidth >= maxWidth - ellipsisWidth && len-- > 0) {
str = str.slice(0, len)
strWidth = ctx.measureText(str).width
}
return {
text: str + ellipsis,
width: maxWidth + ellipsisWidth,
}
}
},
drawArrow(fillStyle) {
const size = 4
const len = this.currentPointList.length
if (len < 2) return
const start = this.currentPointList[len - 2]
const end = this.currentPointList[len - 1]
const ctx = this.ctx
// 移动中心点
ctx.translate(...end)
// 箭头原方向垂直向下,
// 算出直线偏离Y的角, 然后旋转 ,
// rotate是顺时针旋转的, 所以加个负号
let ang = Math.atan((end[0] - start[0]) / (end[1] - start[1]))
ctx.beginPath()
if (end[1] - start[1] >= 0) {
ctx.rotate(-ang)
} else {
ctx.rotate(Math.PI - ang) // 加个180度,反过来
}
ctx.fillStyle = fillStyle
ctx.lineTo(-size, -size * 2)
ctx.lineTo(0, -size)
ctx.lineTo(size, -size * 2)
ctx.lineTo(0, 0)
ctx.closePath()
ctx.fill()
ctx.restore()
},
getCoordinates(evt) {
const { clientX, clientY } = evt
const { top, left } = this.$el.getBoundingClientRect()
return [clientX - left, clientY - top]
},
isPointInStroke(evt) {
const [x, y] = this.getCoordinates(evt)
return this.link.isPointInLink(
[this.left + x, this.top + y],
this.currentPathPointList
)
},
rootMousemove({ evt }) {
this.inPath = this.isPointInStroke(evt)
return this.inPath
},
},
}
</script>
<style lang="scss" scoped>
.super-flow__line {
position: absolute;
z-index: 0;
&.inPath {
cursor: pointer;
}
}
</style>