OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. |
| 5 --> |
| 6 <import src="/sky/framework/sky-element/sky-element.sky" as="SkyElement" /> |
| 7 |
| 8 <sky-element name="fps-counter" attributes="showHistory:boolean"> |
| 9 <template> |
| 10 <template if="{{ showHistory }}"> |
| 11 <template repeat="{{ history }}"> |
| 12 <div>{{ }} ms</div> |
| 13 </template> |
| 14 <div>max = {{ max }} ms</div> |
| 15 </template> |
| 16 <div>fps = {{ framerate }} Hz</div> |
| 17 </template> |
| 18 <script> |
| 19 var kMaxDeltaLength = 10; |
| 20 |
| 21 module.exports = class extends SkyElement { |
| 22 created() { |
| 23 this.framerate = "..."; |
| 24 this.max = 0; |
| 25 this.lastTimeStamp = 0; |
| 26 this.rafId = 0; |
| 27 this.currentDeltaIndex = 0; |
| 28 this.deltas = new Array(kMaxDeltaLength); |
| 29 for (var i = 0; i < kMaxDeltaLength; ++i) |
| 30 this.deltas[i] = 0; |
| 31 this.history = new Array(kMaxDeltaLength); |
| 32 for (var i = 0; i < kMaxDeltaLength; ++i) |
| 33 this.history[i] = 0; |
| 34 } |
| 35 attached() { |
| 36 this.scheduleTick(); |
| 37 } |
| 38 detached() { |
| 39 cancelAnimationFrame(this.rafId); |
| 40 this.rafId = 0; |
| 41 } |
| 42 scheduleTick() { |
| 43 this.rafId = requestAnimationFrame(this.tick.bind(this)); |
| 44 } |
| 45 tick(timeStamp) { |
| 46 this.scheduleTick(); |
| 47 var lastTimeStamp = this.lastTimeStamp; |
| 48 this.lastTimeStamp = timeStamp; |
| 49 if (!lastTimeStamp) |
| 50 return; |
| 51 var delta = timeStamp - lastTimeStamp; |
| 52 this.deltas[this.currentDeltaIndex++ % kMaxDeltaLength] = delta; |
| 53 for (var i = 0; i < kMaxDeltaLength; ++i) { |
| 54 var value = this.deltas[(i + this.currentDeltaIndex) % kMaxDeltaLength]; |
| 55 this.history[i] = value.toFixed(2); |
| 56 } |
| 57 var sum = this.deltas.reduce(function(a, b) { return a + b }, 0); |
| 58 var avg = sum / kMaxDeltaLength; |
| 59 this.framerate = (1000 / avg).toFixed(2); |
| 60 this.max = Math.max(delta, this.max).toFixed(2); |
| 61 } |
| 62 }.register(); |
| 63 </script> |
| 64 </sky-element> |
OLD | NEW |