5.2_帧速率的计算
复制代码
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>帧速率的计算</title>
<style>
body{
background: #ddd;
}
#canvas{
background: #fff;
cursor: pointer;
margin-left: 10px;
margin-top: 10px;
-webkit-box-shadow: 3px 3px 6px rgba(0,0,0,0.5);
-moz-box-shadow: 3px 3px 6px rgba(0,0,0,0.5);
box-shadow: 3px 3px 6px rgba(0,0,0,0.5);
}
#controls{
margin-top: 10px;
margin-left: 15px;
}
</style>
</head>
<body>
<div id="controls">
<input id="animateButton" type="button" value="Animate" />
</div>
<canvas id="canvas" width="750" height="500"></canvas>
</body>
<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var paused = true;
var discs = [ //三个圆的各种属性
{
x:150,
y:250,
lastX:150,
lastY:250,
velocityX:-3.2,
velocityY:3.5,
radius:25,
innerColor:'rgba(255,255,0,1)',
middleColor:'rgba(100,145,230,0.7)',
outerColor:'rgba(100,145,230,0.5)',
strokeStyle:'gray'
},
{
x:50,
y:150,
lastX:50,
lastY:150,
velocityX:2.2,
velocityY:2.5,
radius:25,
innerColor:'rgba(100,145,230,1)',
middleColor:'rgba(100,145,230,0.7)',
outerColor:'rgba(100,145,230,0.5)',
strokeStyle:'blue'
},
{
x:150,
y:75,
lastX:150,
lastY:75,
velocityX:1.2,
velocityY:1.5,
radius:25,
innerColor:'rgba(255,0,0,1)',
middleColor:'rgba(255,0,0,0.7)',
outerColor:'rgba(255,0,0,0.5)',
strokeStyle:'orange'
},
];
var numDiscs = discs.length;
var animateButton = document.getElementById('animateButton');
var lastTime = 0;
//初始化
context.font = '48px Helvetica';
drawBackground();
//事件控制
animateButton.onclick = function(){
paused = paused? false:true;
if(paused){ //如果停止动画
animateButton.value = 'Animate';
}else{ //播放动画
window.requestAnimationFrame(animate);
animateButton.value = 'Pause';
}
}
//执行每次动画效果
function animate(time){
if(!paused){
context.clearRect(0,0,canvas.width,canvas.height);//每帧清空一次画布
drawBackground();
update(); //更新此帧的三个圆的各种新属性
drawCircles();//开始绘制更新后的圆
//绘制帧速率
context.fillStyle = 'cornflowerblue';
context.fillText(calculateFps().toFixed()+'fps',20,60);
//执行下一次animate
window.requestAnimationFrame(animate);
}
}
//绘制帧速率
function calculateFps(){
//这里会有一个加号是因为一元+操作符会有Number()转型函数的功能,将时间对象转换到数字类型的秒数,以便后续计算
var now = +(new Date);
//console.log(new Date);
//console.log(now);
var fps = 1000/(now - lastTime);
lastTime = now;
return fps;
}
//更新
function update(){
var disc;
for(var i=0;i<numDiscs;i++){
disc = discs[i];
if(disc.x+disc.radius+disc.velocityX>context.canvas.width || disc.x-disc.radius+disc.velocityX<0){
disc.velocityX = -disc.velocityX;
}
if(disc.y+disc.radius+disc.velocityY>context.canvas.height || disc.y-disc.radius+disc.velocityY<0){
disc.velocityY = -disc.velocityY;
}
disc.x += disc.velocityX;
disc.y += disc.velocityY;
}
}
//绘制更新后的圆
function drawCircles(){
var disc;
var gradient;
for(var i=0;i<numDiscs;i++){
disc = discs[i];
gradient = context.createRadialGradient(disc.x,disc.y,0,disc.x,disc.y,disc.radius);
gradient.addColorStop(0.3,disc.innerColor);
gradient.addColorStop(0.5,disc.middleColor);
gradient.addColorStop(1.0,disc.outerColor);
context.save();
context.beginPath();
context.arc(disc.x,disc.y,disc.radius,0,Math.PI*2,false);
context.fillStyle = gradient;
context.strokeStyle = disc.strokeStyle;
context.fill();
context.stroke();
context.restore();
}
}
//绘制横隔背景
function drawBackground(){
context.save();
context.shadowColor = undefined;
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;
context.shadowBlur = 0;
var step_y = 12;
var left_margin = step_y*4;
var top_margin = step_y*3;
var i = canvas.height;
context.strokeStyle = 'lightgray';
context.lineWidth = 0.5;
while(i>top_margin){
context.beginPath();
context.moveTo(0,i);
context.lineTo(canvas.width,i);
context.stroke();
i-=step_y;
}
context.restore();
}
</script>
</html>
最后
以上就是慈祥黑猫最近收集整理的关于5.2_帧速率的计算的全部内容,更多相关5.2_帧速率内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复