Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: sky/examples/fps-counter.sky

Issue 803283006: Add a fps-counter widget to some Sky demos (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | sky/examples/spinning-square.sky » ('j') | sky/examples/touch-demo.sky » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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>
OLDNEW
« no previous file with comments | « no previous file | sky/examples/spinning-square.sky » ('j') | sky/examples/touch-demo.sky » ('J')

Powered by Google App Engine
This is Rietveld 408576698