|
| 1 | +/* |
| 2 | + * stats.js 1.2 |
| 3 | + * http://code.google.com/p/mrdoob/wiki/stats_js |
| 4 | + * |
| 5 | + * Released under MIT license: |
| 6 | + * http://www.opensource.org/licenses/mit-license.php |
| 7 | + * |
| 8 | + * How to use: |
| 9 | + * |
| 10 | + * var stats = new Stats(); |
| 11 | + * dom_element.appendChild( stats.getDisplayElement() ); |
| 12 | + * |
| 13 | + * setInterval(loop, 1000/60); |
| 14 | + * |
| 15 | + * function loop() |
| 16 | + * { |
| 17 | + * stats.update(); |
| 18 | + * } |
| 19 | + * |
| 20 | + * version log: |
| 21 | + * |
| 22 | + * 10.03.01 1.2 Mr.doob + Simplified |
| 23 | + * 10.02.21 1.1 Mr.doob + Accurate FPS calculation (thx Spite!) |
| 24 | + * 09.08.10 1.0 Mr.doob + Base code |
| 25 | + * |
| 26 | + */ |
| 27 | + |
| 28 | +function Stats() |
| 29 | +{ |
| 30 | + this.init(); |
| 31 | +} |
| 32 | + |
| 33 | +Stats.prototype = |
| 34 | +{ |
| 35 | + init: function() |
| 36 | + { |
| 37 | + this.frames = 0; |
| 38 | + this.framesMin = 1000; |
| 39 | + this.framesMax = 0; |
| 40 | + |
| 41 | + this.time = new Date().getTime(); |
| 42 | + this.timePrev = new Date().getTime(); |
| 43 | + |
| 44 | + this.container = document.createElement("div"); |
| 45 | + this.container.style.position = 'absolute'; |
| 46 | + this.container.style.fontFamily = 'Arial'; |
| 47 | + this.container.style.fontSize = '10px'; |
| 48 | + this.container.style.backgroundColor = '#000020'; |
| 49 | + this.container.style.opacity = '0.9'; |
| 50 | + this.container.style.width = '80px'; |
| 51 | + this.container.style.paddingTop = '2px'; |
| 52 | + |
| 53 | + this.framesText = document.createElement("div"); |
| 54 | + this.framesText.style.color = '#00ffff'; |
| 55 | + this.framesText.style.marginLeft = '3px'; |
| 56 | + this.framesText.style.marginBottom = '3px'; |
| 57 | + this.framesText.innerHTML = '<strong>FPS</strong>'; |
| 58 | + this.container.appendChild(this.framesText); |
| 59 | + |
| 60 | + this.canvas = document.createElement("canvas"); |
| 61 | + this.canvas.width = 74; |
| 62 | + this.canvas.height = 30; |
| 63 | + this.canvas.style.display = 'block'; |
| 64 | + this.canvas.style.marginLeft = '3px'; |
| 65 | + this.canvas.style.marginBottom = '3px'; |
| 66 | + this.container.appendChild(this.canvas); |
| 67 | + |
| 68 | + this.context = this.canvas.getContext("2d"); |
| 69 | + this.context.fillStyle = '#101030'; |
| 70 | + this.context.fillRect(0, 0, this.canvas.width, this.canvas.height ); |
| 71 | + |
| 72 | + this.contextImageData = this.context.getImageData(0, 0, this.canvas.width, this.canvas.height); |
| 73 | + }, |
| 74 | + |
| 75 | + getDisplayElement: function() |
| 76 | + { |
| 77 | + return this.container; |
| 78 | + }, |
| 79 | + |
| 80 | + update: function() |
| 81 | + { |
| 82 | + this.frames++ |
| 83 | + |
| 84 | + this.time = new Date().getTime(); |
| 85 | + |
| 86 | + if (this.time >= this.timePrev + 1000) |
| 87 | + { |
| 88 | + this.fps = Math.round((this.frames * 1000 ) / (this.time - this.timePrev)); |
| 89 | + |
| 90 | + this.framesMin = Math.min(this.framesMin, this.fps); |
| 91 | + this.framesMax = Math.max(this.framesMax, this.fps); |
| 92 | + |
| 93 | + this.framesText.innerHTML = '<strong>' + this.fps + ' FPS</strong> (' + this.framesMin + '-' + this.framesMax + ')'; |
| 94 | + |
| 95 | + this.contextImageData = this.context.getImageData(1, 0, this.canvas.width - 1, 30); |
| 96 | + this.context.putImageData(this.contextImageData, 0, 0); |
| 97 | + |
| 98 | + this.context.fillStyle = '#101030'; |
| 99 | + this.context.fillRect(this.canvas.width - 1, 0, 1, 30); |
| 100 | + |
| 101 | + this.index = ( Math.floor(30 - Math.min(30, (this.fps / 60) * 30)) ); |
| 102 | + |
| 103 | + this.context.fillStyle = '#80ffff'; |
| 104 | + this.context.fillRect(this.canvas.width - 1, this.index, 1, 1); |
| 105 | + |
| 106 | + this.context.fillStyle = '#00ffff'; |
| 107 | + this.context.fillRect(this.canvas.width - 1, this.index + 1, 1, 30 - this.index); |
| 108 | + |
| 109 | + this.timePrev = this.time; |
| 110 | + this.frames = 0; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments