-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (135 loc) · 4.33 KB
/
index.js
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
/**
* 生成svg圆形进度条
* @Author: qiqislh
* @Date: 2018-03-01 16:46:36
* @Last Modified by: qiqislh
* @Last Modified time: 2018-03-02 10:57:20
*
* @param {element} element 插入的位置,默认body
* @param {float} percent 百分比,默认0
* @param {int} width svg宽度,默认200
* @param {int} height svg高度,默认200
* @param {int} strokeWidth 圆的边宽,默认7
* @param {int} r 圆半径,默认通过svg宽高及圆的变宽计算
* @param {string} rotate 进度起始位置,默认'rotate(-90, 0, 0)',也就是12点方向
* @param {*} color 进度条的颜色,默认'#05d57f'
* @param {*} bgColor 进度条背景颜色,默认'#f1f1f1'
*/
class SvgProgress {
constructor (options) {
options = options || {}
this.element = document.querySelector(options.element) || document.body
// 百分比
this.percent = options.percent || 20
this.width = options.width || 200
this.height = options.height || 200
// 坐标默认中间,不可修改
this.cx = parseInt(this.width / 2)
this.cy = parseInt(this.width / 2)
// 进度条宽度
this.strokeWidth = options.strokeWidth || 7
// 半径:如果没有传入,则通过计算长宽的大小计算最大半径
this.r = options.r || this.cx >= this.cy ? (this.cx - this.strokeWidth) : (this.cy - this.strokeWidth)
// 周长
this.perimeter = parseInt(2 * Math.PI * this.r)
// 默认起点在顶点
this.rotate = options.rotate || 'rotate(-90, 0, 0)'
// 颜色
this.color = options.color || '#05d57f'
this.bgColor = options.bgColor || '#f1f1f1'
this.svg = null
this.circleActive = null
this.text = null
this.render()
}
/**
* 渲染
*/
render () {
this.svg = this.generateSvg()
const g = this.generateG()
const strokeDasharray = `${this.perimeter} ${this.perimeter}`
const circleBg = this.generateCircle(this.bgColor, strokeDasharray)
g.appendChild(circleBg)
this.circleActive = this.generateCircle(this.color, this.strokeDasharrayActive())
g.appendChild(this.circleActive)
// 如果进度为0,隐藏那啥
if (this.percent <= 0) {
this.circleActive.style.opacity = 0
}
this.text = this.gererateText()
this.svg.appendChild(g)
this.svg.appendChild(this.text)
this.element.appendChild(this.svg)
}
/**
* 更新svg进度
*/
update () {
if (this.percent <= 0) {
this.circleActive.style.opacity = 0
} else {
this.circleActive.style.opacity = 1
}
this.circleActive.setAttribute('stroke-dasharray', this.strokeDasharrayActive())
this.text.innerHTML = `${this.percent}%`
}
/**
* 生成进度值
*/
strokeDasharrayActive () {
const activeNum = parseInt(this.percent / 100 * this.perimeter)
const strokeDasharrayActive = `${activeNum} ${this.perimeter}`
return strokeDasharrayActive
}
/**
* 创建svg
*/
generateSvg () {
let svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg.setAttribute('width', this.width)
svg.setAttribute('height', this.height)
return svg
}
/**
* 创建g
*/
generateG () {
let g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
g.setAttribute('transform', this.rotate)
g.setAttribute('transform-origin', 'center')
g.setAttribute('stroke-width', this.strokeWidth)
g.setAttribute('fill', 'none')
g.setAttribute('stroke-linecap', 'round')
return g
}
/**
* 创建圆
* @param {*} color 圆边框颜色
* @param {*} strokeDasharray 圆进度
*/
generateCircle (color, strokeDasharray) {
let circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
circle.setAttribute('cx', this.cx)
circle.setAttribute('cy', this.cy)
circle.setAttribute('r', this.r)
circle.setAttribute('stroke', color)
circle.setAttribute('stroke-dasharray', strokeDasharray)
// circle.style.transition = 'all .5s'
return circle
}
/**
* 创建文字
*/
gererateText () {
let text = document.createElementNS('http://www.w3.org/2000/svg', 'text')
text.setAttribute('x', this.cx)
text.setAttribute('y', this.cy)
text.setAttribute('text-anchor', 'middle')
text.setAttribute('font-size', 12)
text.setAttribute('stroke', '#000000')
text.innerHTML = `${this.percent}%`
return text
}
}
export default SvgProgress