Skip to content

Commit f6e1aaa

Browse files
committed
refactor: remove style dom and fix animate, close #12
1 parent 9548729 commit f6e1aaa

File tree

7 files changed

+89
-122
lines changed

7 files changed

+89
-122
lines changed

Diff for: docs/home.vue

+8-9
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:data="data"
99
:gradient="gradient"
1010
auto-draw
11-
:stroke-width="1.4"
1211
smooth>
1312
</trend>
1413

@@ -19,24 +18,24 @@
1918
</template>
2019

2120
<script>
22-
import Trend from '../src/index.js'
23-
import hanabi from 'hanabi'
24-
import GithubBadge from 'vue-github-badge'
21+
import Trend from '../src/index.js';
22+
import hanabi from 'hanabi';
23+
import GithubBadge from 'vue-github-badge';
2524
2625
export default {
2726
components: { Trend, GithubBadge },
2827
29-
created () {
30-
this.data = [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0]
31-
this.gradient = ['#6fa8dc', '#42b983', '#2c3e50']
28+
created() {
29+
this.data = [0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0];
30+
this.gradient = ['#6fa8dc', '#42b983', '#2c3e50'];
3231
this.code = hanabi(`<trend
3332
:data="[0, 2, 5, 9, 5, 10, 3, 5, 0, 0, 1, 8, 2, 9, 0]"
3433
:gradient="['#6fa8dc', '#42b983', '#2c3e50']"
3534
auto-draw
3635
smooth>
37-
</trend>`)
38-
}
36+
</trend>`);
3937
}
38+
};
4039
</script>
4140

4241
<style>

Diff for: src/components/gradient.js

+25-16
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@ export default {
33

44
render (h) {
55
const { gradient, id } = this
6-
const len = gradient.length - 1
7-
const stops = gradient.slice().reverse().map((color, index) =>
8-
h('stop', {
9-
attrs: {
10-
offset: index / len,
11-
'stop-color': color
12-
}
13-
})
14-
)
6+
const len = gradient.length - 1 || 1
7+
const stops = gradient
8+
.slice()
9+
.reverse()
10+
.map((color, index) =>
11+
h('stop', {
12+
attrs: {
13+
offset: index / len,
14+
'stop-color': color
15+
}
16+
})
17+
)
1518

1619
return h('defs', [
17-
h('linearGradient', {
18-
attrs: {
19-
id,
20-
x1: 0, y1: 0,
21-
x2: 0, y2: 1
22-
}
23-
}, stops)
20+
h(
21+
'linearGradient',
22+
{
23+
attrs: {
24+
id,
25+
x1: 0,
26+
y1: 0,
27+
x2: 0,
28+
y2: 1
29+
}
30+
},
31+
stops
32+
)
2433
])
2534
}
2635
}

Diff for: src/components/path.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { genPoints, linearPath, smoothPath } from '../helpers/path'
1+
import { genPoints, genPath } from '../helpers/path'
22

33
export default {
44
props: ['smooth', 'data', 'boundary', 'radius', 'id'],
55

66
render (h) {
77
const points = genPoints(this.data, this.boundary)
8-
const d = (this.smooth ? smoothPath : linearPath)(points, this.radius)
8+
const d = genPath(points, this.smooth ? this.radius : 0)
99

1010
return h('path', {
1111
attrs: { d, fill: 'none', stroke: `url(#${this.id})` }

Diff for: src/components/trend.js

+45-71
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Path from './path'
22
import Gradient from './gradient'
3-
import { injectStyle } from '../helpers/dom'
43

54
export default {
65
name: 'Trend',
@@ -19,7 +18,10 @@ export default {
1918
type: String,
2019
default: 'ease'
2120
},
22-
gradient: Array,
21+
gradient: {
22+
type: Array,
23+
default: () => ['#000']
24+
},
2325
height: Number,
2426
width: Number,
2527
padding: {
@@ -33,59 +35,30 @@ export default {
3335
smooth: Boolean
3436
},
3537

36-
destroyed () {
37-
this.removeStyle()
38-
},
39-
40-
methods: {
41-
addStyle () {
42-
this.removeStyle()
43-
const len = this.$refs.path.$el.getTotalLength()
44-
const { pathId, autoDrawDuration, autoDrawEasing, autoDraw } = this
45-
46-
if (!autoDraw) {
47-
return
48-
}
49-
50-
this.styleEl = injectStyle(`
51-
@keyframes ${pathId}-autodraw {
52-
0% {
53-
stroke-dashoffset: ${len};
54-
stroke-dasharray: ${len};
55-
}
56-
100% {
57-
stroke-dashoffset: 0;
58-
stroke-dasharray: ${len};
59-
}
60-
100% {
61-
stroke-dashoffset: '';
62-
stroke-dasharray: '';
63-
}
64-
}
65-
@keyframes ${pathId}-autodraw-cleanup {
66-
to {
67-
stroke-dashoffset: '';
68-
stroke-dasharray: '';
69-
}
70-
}
71-
#${pathId} {
72-
animation:
73-
${pathId}-autodraw ${autoDrawDuration}ms ${autoDrawEasing},
74-
${pathId}-autodraw-cleanup 1ms ${autoDrawDuration}ms;
75-
}`)
76-
},
77-
78-
removeStyle () {
79-
this.styleEl && this.styleEl.remove()
80-
}
81-
},
82-
8338
watch: {
8439
data: {
8540
immediate: true,
8641
handler (val) {
87-
if (!val || val.length < 2) return
88-
this.$nextTick(this.addStyle)
42+
this.$nextTick(() => {
43+
if (!this.autoDraw) {
44+
return
45+
}
46+
47+
const path = this.$refs.path.$el
48+
const length = path.getTotalLength()
49+
50+
path.style.transition = 'none'
51+
path.style.strokeDasharray = length + ' ' + length
52+
path.style.strokeDashoffset = Math.abs(
53+
length - (this.lastLength || 0)
54+
)
55+
path.getBoundingClientRect()
56+
path.style.transition = `stroke-dashoffset ${
57+
this.autoDrawDuration
58+
}ms ${this.autoDrawEasing}`
59+
path.style.strokeDashoffset = 0
60+
this.lastLength = length
61+
})
8962
}
9063
}
9164
},
@@ -96,30 +69,31 @@ animation:
9669
const viewWidth = width || 300
9770
const viewHeight = height || 75
9871
const boundary = {
99-
minX: padding, minY: padding,
100-
maxX: viewWidth - padding, maxY: viewHeight - padding
72+
minX: padding,
73+
minY: padding,
74+
maxX: viewWidth - padding,
75+
maxY: viewHeight - padding
10176
}
10277
const props = this.$props
10378

10479
props.boundary = boundary
10580
props.id = 'vue-trend-' + this._uid
106-
this.pathId = props.id + '-path'
107-
108-
return h('svg', {
109-
attrs: {
110-
stroke: 'black',
111-
'stroke-width': '1',
112-
width: width || '100%',
113-
height: height || '25%',
114-
viewBox: `0 0 ${viewWidth} ${viewHeight}`
115-
}
116-
}, [
117-
h(Gradient, { props }),
118-
h(Path, {
119-
props,
120-
attrs: { id: this.pathId },
121-
ref: 'path'
122-
})
123-
])
81+
return h(
82+
'svg',
83+
{
84+
attrs: {
85+
width: width || '100%',
86+
height: height || '25%',
87+
viewBox: `0 0 ${viewWidth} ${viewHeight}`
88+
}
89+
},
90+
[
91+
h(Gradient, { props }),
92+
h(Path, {
93+
props,
94+
ref: 'path'
95+
})
96+
]
97+
)
12498
}
12599
}

Diff for: src/helpers/dom.js

-11
This file was deleted.

Diff for: src/helpers/math.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ function int (value) {
88
* y=(y1+y2)/2
99
*/
1010
export function checkCollinear (p0, p1, p2) {
11-
return int(p0.x + p2.x) === int(2 * p1.x) && int(p0.y + p2.y) === int(2 * p1.y)
11+
return (
12+
int(p0.x + p2.x) === int(2 * p1.x) && int(p0.y + p2.y) === int(2 * p1.y)
13+
)
1214
}
1315

1416
export function getDistance (p1, p2) {
15-
return Math.sqrt(
16-
Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)
17-
)
17+
return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2))
1818
}
1919

2020
export function moveTo (to, from, radius) {
2121
const vector = { x: to.x - from.x, y: to.y - from.y }
22-
const length = Math.sqrt((vector.x * vector.x) + (vector.y * vector.y))
22+
const length = Math.sqrt(vector.x * vector.x + vector.y * vector.y)
2323
const unitVector = { x: vector.x / length, y: vector.y / length }
2424

2525
return {

Diff for: src/helpers/path.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,10 @@ export function genPoints (arr, { minX, minY, maxX, maxY }) {
2424
})
2525
}
2626

27-
export function linearPath (points) {
28-
const { x, y } = points.shift()
29-
30-
return `M${x} ${y}` + points.map(({ x, y }) => `L${x} ${y}`).join('')
31-
}
32-
3327
/**
3428
* From https://github.com/unsplash/react-trend/blob/master/src/helpers/DOM.helpers.js#L18
3529
*/
36-
export function smoothPath (points, radius) {
30+
export function genPath (points, radius) {
3731
const start = points.shift()
3832

3933
return (
@@ -58,7 +52,9 @@ export function smoothPath (points, radius) {
5852
const before = moveTo(prev, point, radiusForPoint)
5953
const after = moveTo(next, point, radiusForPoint)
6054

61-
return `L${before.x} ${before.y}S${point.x} ${point.y} ${after.x} ${after.y}`
55+
return `L${before.x} ${before.y}S${point.x} ${point.y} ${after.x} ${
56+
after.y
57+
}`
6258
})
6359
.join('')
6460
)

0 commit comments

Comments
 (0)