平日学习点滴
背景:
今天无意中发现一个方法
- window.requestAnimationFrame
Google之后,就完成了下面得到浏览器每秒帧数的方法。
使用:
- showFPS.go(); // go!
代码:
- /**
- ** 得到浏览器每秒帧数fps
- **
- ** @Date Mar 13 2013
- **/
- var showFPS = (function(){
- var requestAnimationFrame =
- window.requestAnimationFrame || //Chromium
- window.webkitRequestAnimationFrame || //Webkit
- window.mozRequestAnimationFrame || //Mozilla Geko
- window.oRequestAnimationFrame || //Opera Presto
- window.msRequestAnimationFrame || //IE Trident?
- function(callback) { //Fallback function
- window.setTimeout(callback, 1000/60);
- };
- var e,pe,pid,fps,last,offset,step,appendFps;
- fps = 0;
- last = Date.now();
- step = function(){
- offset = Date.now() - last;
- fps += 1;
- if( offset >= 1000 ){
- last += offset;
- appendFps(fps);
- fps = 0;
- }
- requestAnimationFrame( step );
- };
- //显示fps; 如果未指定元素id,默认<body>标签
- appendFps = function(fps){
- if(!e) e=document.createElement('span');
- pe=pid?document.getElementById(pid):document.getElementsByTagName('body')[0];
- e.innerHTML = "fps: " + fps;
- pe.appendChild(e);
- }
- return {
- setParentElementId : function(id){pid=id;},
- go : function(){step();}
- }
- })();
Vaenow
2013-03-13
转载于:https://blog.51cto.com/snowdream/1153407