-
Notifications
You must be signed in to change notification settings - Fork 26
/
canvas-growth-radius.html
218 lines (193 loc) · 5.26 KB
/
canvas-growth-radius.html
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
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset=utf-8>
<title>demo</title>
</head>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
.wrapper{
height: 800px;
width: 600px;
position: relative;
}
#myCanvasBackGround{
position: absolute;
top: 0;
left: 0;
color: #ddd;
z-index: 8;
}
#myCanvas{
position: absolute;
top: 0;
left: 0;
z-index: 9;
}
.point-wrapper{
position: absolute;
z-index: 10;
width: 10px;
height: 10px;
background-color: #fff;
border-radius: 50%;
display: none;
border: 8px solid #d1ba70;
}
.point-gold-wrapper{
position: absolute;
z-index: 10;
width: 10px;
height: 10px;
background-color: #d1ba6f;
border-radius: 50%;
display: none;
}
.point-gold-wrapper::after,.point-gold-wrapper::before{
position: absolute;
width: 100px;
font-size: 22px;
right: -130px;
text-align: left;
top: 0;
color: #fff;
}
.drop-left::after,.drop-left::before{
position: absolute;
width: 100px;
font-size: 22px;
left: -130px;
text-align: right;
top: 0;
color: #fff;
}
</style>
<script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
<body>
<div class='wrapper'>
<canvas id="myCanvasBackGround"></canvas>
<div class="point-wrapper"></div>
<div class="point-gold-wrapper"></div>
<canvas id="myCanvas"></canvas>
</div>
<script type="text/javascript">
/*
* canvas弧形增长函数
* (x,y)圆心坐标
* r 半径
* percent 增长的百分比
*/
function drawGrowthCircle(percent) {
var canvas = document.getElementById('myCanvas');
var g = canvas.getContext('2d');
// 使画布的宽度和高度始终与终端屏幕宽度相等(canvas不支持rem)
var clientWidth = document.documentElement.clientWidth;
var canvasWidth = Math.floor(clientWidth);
canvas.setAttribute('width', canvasWidth + 'px');
canvas.setAttribute('height', canvasWidth + 'px');
var x = roundNum(canvasWidth / 2);
var y = roundNum(canvasWidth / 2 + clientWidth * 16 / 375);
var r = roundNum(clientWidth * 23 / 75);
var linewidth = roundNum(clientWidth * 4 / 375);
var start = new Date().getTime();
drawBackGround(x, y, r, canvasWidth, linewidth);
function roundNum(num) {
return (0.5 + num) << 0;
}
function draw() {
g.clearRect(0, 0, canvas.width, canvas.height);
var t = (new Date().getTime() - start) / 1000;
var circles = t / 2;
//终止位置
var ended = (0.75 + 1.5 * (percent / 360)) * Math.PI * Math.min(circles, 1);
// 超出弧度的范围return掉
if (ended < 0.75 * Math.PI || ended > 2.25 * Math.PI) {
window.requestAnimationFrame(draw);
return;
}
// 到达终点时 画弧 && cancelAnimationFrame
if (circles > 1 || circles === 1) {
drawPath(x, y, r, ended);
window.cancelAnimationFrame(draw);
return;
}
if (ended < 1.5 * Math.PI) {
var xPoint = x - Math.abs(r * Math.cos(ended));
var yPoint = y + r * Math.sin(ended);
} else {
var xPoint = x + Math.abs(r * Math.cos(ended));
var yPoint = y + r * Math.sin(ended);
}
drawPath(x, y, r, ended, linewidth);
// point
var pointOff = $('.point-wrapper').width() / 2 + 4;
$('.point-wrapper').css({'left': xPoint - pointOff, 'top': yPoint - pointOff});
$('.point-wrapper').show();
window.requestAnimationFrame(draw);
}
function drawPath(x, y, r, ended, linewidth) {
// 设置渐变
var gradient = g.createLinearGradient(0, 0, 0, r);
gradient.addColorStop('0', '#d1ba70');
gradient.addColorStop('0.5', 'white');
gradient.addColorStop('1.0', '#d1ba70');
// 中心
g.beginPath();
g.arc(x, y, r, 0.75 * Math.PI, ended);
g.lineWidth = linewidth;
g.strokeStyle = gradient;
g.stroke();
g.closePath();
}
draw();
}
/*
* canvas弧形轨道
* (x,y)圆心坐标
* r 半径
* 内圆轨道和外层细圆以及黄金会员分隔值点
*/
function drawBackGround(x, y, r, canvasWidth, linewidth) {
var canvasBack = document.getElementById('myCanvasBackGround');
var gBack = canvasBack.getContext('2d');
canvasBack.setAttribute('width', canvasWidth + 'px');
canvasBack.setAttribute('height', canvasWidth + 'px');
gBack.beginPath();
gBack.arc(x, y, r, 0.75 * Math.PI, Math.PI * 2.25);
gBack.lineWidth = linewidth;
gBack.strokeStyle = 'rgba(0, 0, 0, 0.5)';
gBack.stroke();
gBack.closePath();
gBack.beginPath();
gBack.arc(x, y, r + 13, 0.75 * Math.PI, Math.PI * 2.25);
// gBack.lineWidth = canvasWidth / 375;
gBack.lineWidth = 1.3;
gBack.strokeStyle = 'rgba(209, 186, 111, 0.5)';
gBack.stroke();
gBack.closePath();
var goldPer = 700 / 1000;
var finalPos = (0.75 + 1.5 * goldPer) * Math.PI;
var gxPoint = '';
var gyPoint = '';
// 黄金分值点(第三象限)
if (finalPos > 1.5 * Math.PI) {
gxPoint = x + Math.abs((r + 13) * Math.cos(finalPos));
gyPoint = y + (r + 13) * Math.sin(finalPos);
// 第二象限
} else {
gxPoint = x - Math.abs((r + 13) * Math.cos(finalPos));
gyPoint = y + (r + 13) * Math.sin(finalPos);
$('.point-gold-wrapper').addClass('drop-left');
}
// 黄金分值点div
var goldOff = $('.point-gold-wrapper').width() / 2;
$('.point-gold-wrapper').css({'left': gxPoint - goldOff, 'top': gyPoint - goldOff});
$('.point-gold-wrapper').show();
}
drawGrowthCircle(240);
</script>
</body>
</html>