fps-detector 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,17 @@
1
+ # fps-detector
2
+ fps-detector
3
+
4
+ # Install
5
+ ```sh
6
+ npm i fps-detector
7
+ ```
8
+
9
+ # Usage
10
+ ```js
11
+ import FPSDetector from 'fps-detector';
12
+ new FPSDetector('.fps-detector', {
13
+ width: 120,
14
+ height: 30
15
+ });
16
+ ```
17
+ see [preview/index.html](preview/index.html)
@@ -0,0 +1,261 @@
1
+ (function webpackUniversalModuleDefinition(root, factory) {
2
+ if(typeof exports === 'object' && typeof module === 'object')
3
+ module.exports = factory();
4
+ else if(typeof define === 'function' && define.amd)
5
+ define("fps-detector", [], factory);
6
+ else if(typeof exports === 'object')
7
+ exports["fps-detector"] = factory();
8
+ else
9
+ root["fps-detector"] = factory();
10
+ })(self, function() {
11
+ return /******/ (() => { // webpackBootstrap
12
+ /******/ "use strict";
13
+ /******/ // The require scope
14
+ /******/ var __webpack_require__ = {};
15
+ /******/
16
+ /************************************************************************/
17
+ /******/ /* webpack/runtime/define property getters */
18
+ /******/ (() => {
19
+ /******/ // define getter functions for harmony exports
20
+ /******/ __webpack_require__.d = (exports, definition) => {
21
+ /******/ for(var key in definition) {
22
+ /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
23
+ /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
24
+ /******/ }
25
+ /******/ }
26
+ /******/ };
27
+ /******/ })();
28
+ /******/
29
+ /******/ /* webpack/runtime/hasOwnProperty shorthand */
30
+ /******/ (() => {
31
+ /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
32
+ /******/ })();
33
+ /******/
34
+ /******/ /* webpack/runtime/make namespace object */
35
+ /******/ (() => {
36
+ /******/ // define __esModule on exports
37
+ /******/ __webpack_require__.r = (exports) => {
38
+ /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
39
+ /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
40
+ /******/ }
41
+ /******/ Object.defineProperty(exports, '__esModule', { value: true });
42
+ /******/ };
43
+ /******/ })();
44
+ /******/
45
+ /************************************************************************/
46
+ var __webpack_exports__ = {};
47
+ /*!**********************!*\
48
+ !*** ./src/index.js ***!
49
+ \**********************/
50
+ __webpack_require__.r(__webpack_exports__);
51
+ /* harmony export */ __webpack_require__.d(__webpack_exports__, {
52
+ /* harmony export */ "FPSDetector": () => (/* binding */ FPSDetector),
53
+ /* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
54
+ /* harmony export */ });
55
+ class FPSDetector {
56
+ constructor($container, option) {
57
+ if (typeof $container === 'string') {
58
+ $container = document.querySelector($container);
59
+ }
60
+
61
+ this.option = Object.assign({
62
+ width: 85,
63
+ height: 30,
64
+ colors: ['red', 'orange', 'green'],
65
+ bgColor: '#fff',
66
+ fgColor: '#ddd',
67
+ padding: 1
68
+ }, option);
69
+ const $canvas = document.createElement('canvas');
70
+ $canvas.style.display = 'block';
71
+ $canvas.setAttribute('width', this.option.width);
72
+ $canvas.setAttribute('height', this.option.height);
73
+ $container.appendChild($canvas);
74
+ $container.title = 'FPS Monitor';
75
+ this.numbers = {
76
+ '-': {
77
+ 'd': 'm2.615 8.485-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431z'
78
+ },
79
+ '0': {
80
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm7.684 15.56-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zM1.319 8.349l1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'
81
+ },
82
+ '1': {
83
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zm.03 14.873-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'
84
+ },
85
+ '2': {
86
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.781-.049l1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm-1.296-.136 1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'
87
+ },
88
+ '3': {
89
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.781-.049l1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zm-7.654.687 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'
90
+ },
91
+ '4': {
92
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm1.326.685-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'
93
+ },
94
+ '5': {
95
+ 'd': 'm1.289 7.8 1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zm-7.654.687 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'
96
+ },
97
+ '6': {
98
+ 'd': 'm1.289 7.8 1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zM1.319 8.349l1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'
99
+ },
100
+ '7': {
101
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.781-.049l1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm7.684 15.56-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'
102
+ },
103
+ '8': {
104
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zM1.319 8.349l1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'
105
+ },
106
+ '9': {
107
+ 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'
108
+ }
109
+ };
110
+ this.initNumbers($canvas);
111
+ this.ctx = $canvas.getContext('2d');
112
+ this.start();
113
+ }
114
+
115
+ render() {
116
+ const w = this.option.width;
117
+ const h = this.option.height;
118
+ const ctx = this.ctx;
119
+ const list = this.list;
120
+ const iw = 11;
121
+ const ih = 16;
122
+ const padding = this.option.padding;
123
+ const lx = padding;
124
+ const ly = padding;
125
+ const lw = w - (iw * 2 + padding * 3);
126
+ const lh = h - padding * 2;
127
+ let startIndex = padding;
128
+
129
+ if (list.length > lw) {
130
+ list.length = lw;
131
+ } else {
132
+ startIndex = padding + (lw - list.length);
133
+ } //console.log(list);
134
+
135
+
136
+ ctx.fillStyle = this.option.bgColor; //ctx.globalAlpha = 1;
137
+
138
+ ctx.fillRect(0, 0, w, h);
139
+ ctx.fillStyle = this.option.fgColor;
140
+ ctx.fillRect(lx, ly, lw, lh);
141
+ const ls = [].concat(list).reverse();
142
+ let lastItem;
143
+ let lastColor;
144
+ ls.forEach((item, i) => {
145
+ lastItem = item;
146
+ const color = this.getColor(item);
147
+ lastColor = color;
148
+ ctx.fillStyle = color;
149
+ const ch = Math.max(Math.floor(item / 60 * lh), 1);
150
+ ctx.fillRect(startIndex + i, lh + ly - ch, 1, ch);
151
+ });
152
+ const str = "".concat(lastItem).padStart(2, '0');
153
+ const x = Math.floor(lx + lw + padding);
154
+ const y = Math.ceil((h - ih) / 2);
155
+ str.split('').forEach((s, i) => {
156
+ const img = this.getImg(s, lastColor);
157
+
158
+ if (img) {
159
+ ctx.drawImage(img, x + i * iw, y);
160
+ }
161
+ });
162
+ }
163
+
164
+ getColor(v) {
165
+ const colors = this.option.colors;
166
+
167
+ if (v < 10) {
168
+ return colors[0];
169
+ }
170
+
171
+ if (v < 30) {
172
+ return colors[1];
173
+ }
174
+
175
+ return colors[2];
176
+ }
177
+
178
+ initNumbers($container) {
179
+ const numbers = this.numbers;
180
+ const colors = this.option.colors;
181
+ Object.keys(numbers).forEach(key => {
182
+ const item = numbers[key];
183
+ colors.forEach(color => {
184
+ if (item[color]) {
185
+ return;
186
+ }
187
+
188
+ const svg = "\n <svg viewBox=\"0 0 11 16\" width=\"11\" height=\"16\" xmlns=\"http://www.w3.org/2000/svg\">\n <path fill=\"".concat(color, "\" d=\"").concat(item.d, "\" />\n </svg>\n ");
189
+ const $img = document.createElement('img');
190
+ $img.src = "data:image/svg+xml;base64,".concat(btoa(svg));
191
+ $container.appendChild($img);
192
+ item[color] = $img;
193
+ });
194
+ });
195
+ }
196
+
197
+ getImg(n, color) {
198
+ const item = this.numbers[n];
199
+
200
+ if (item) {
201
+ return item[color];
202
+ }
203
+ }
204
+
205
+ start() {
206
+ this.stopped = false;
207
+ this.list = [];
208
+ this.frames = 0;
209
+ this.startTime = window.performance.now();
210
+ this.update();
211
+ }
212
+
213
+ update() {
214
+ if (this.stopped) {
215
+ return;
216
+ }
217
+
218
+ window.requestAnimationFrame(() => {
219
+ this.count();
220
+ });
221
+ }
222
+
223
+ count() {
224
+ const list = this.list;
225
+ const now = window.performance.now();
226
+ let d = now - this.startTime; //count each 1s
227
+
228
+ if (d < 1000) {
229
+ this.frames += 1;
230
+ } else {
231
+ d -= 1000;
232
+
233
+ while (d > 1000) {
234
+ list.unshift(0);
235
+ d -= 1000;
236
+ } //1s
237
+
238
+
239
+ list.unshift(this.frames);
240
+ this.render(); //next
241
+
242
+ this.frames = 1;
243
+ this.startTime = now - d;
244
+ }
245
+
246
+ this.update();
247
+ }
248
+
249
+ stop() {
250
+ this.stopped = true;
251
+ }
252
+
253
+ }
254
+
255
+
256
+ /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (FPSDetector);
257
+ /******/ return __webpack_exports__;
258
+ /******/ })()
259
+ ;
260
+ });
261
+ //# sourceMappingURL=fps-detector.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"fps-detector.js","mappings":"AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,CAAC;AACD,O;;UCVA;UACA;;;;;WCDA;WACA;WACA;WACA;WACA,yCAAyC,wCAAwC;WACjF;WACA;WACA;;;;;WCPA;;;;;WCAA;WACA;WACA;WACA,uDAAuD,iBAAiB;WACxE;WACA,gDAAgD,aAAa;WAC7D;;;;;;;;;;;;;ACNA,MAAMA,WAAN,CAAkB;AACdC,EAAAA,WAAW,CAACC,UAAD,EAAaC,MAAb,EAAqB;AAC5B,QAAI,OAAOD,UAAP,KAAsB,QAA1B,EAAoC;AAChCA,MAAAA,UAAU,GAAGE,QAAQ,CAACC,aAAT,CAAuBH,UAAvB,CAAb;AACH;;AAED,SAAKC,MAAL,GAAcG,MAAM,CAACC,MAAP,CAAc;AACxBC,MAAAA,KAAK,EAAE,EADiB;AAExBC,MAAAA,MAAM,EAAE,EAFgB;AAGxBC,MAAAA,MAAM,EAAE,CAAC,KAAD,EAAQ,QAAR,EAAkB,OAAlB,CAHgB;AAIxBC,MAAAA,OAAO,EAAE,MAJe;AAKxBC,MAAAA,OAAO,EAAE,MALe;AAMxBC,MAAAA,OAAO,EAAE;AANe,KAAd,EAOXV,MAPW,CAAd;AASA,UAAMW,OAAO,GAAGV,QAAQ,CAACW,aAAT,CAAuB,QAAvB,CAAhB;AACAD,IAAAA,OAAO,CAACE,KAAR,CAAcC,OAAd,GAAwB,OAAxB;AACAH,IAAAA,OAAO,CAACI,YAAR,CAAqB,OAArB,EAA8B,KAAKf,MAAL,CAAYK,KAA1C;AACAM,IAAAA,OAAO,CAACI,YAAR,CAAqB,QAArB,EAA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KAFD,MAEO;AAEHN,MAAAA,CAAC,IAAI,IAAL;;AACA,aAAOA,CAAC,GAAG,IAAX,EAAiB;AACbvC,QAAAA,IAAI,CAACqD,OAAL,CAAa,CAAb;AACAd,QAAAA,CAAC,IAAI,IAAL;AACH,OANE,CAQH;;;AACAvC,MAAAA,IAAI,CAACqD,OAAL,CAAa,KAAKR,MAAlB;AACA,WAAKhD,MAAL,GAVG,CAWH;;AACA,WAAKgD,MAAL,GAAc,CAAd;AACA,WAAKC,SAAL,GAAiBG,GAAG,GAAGV,CAAvB;AACH;;AAED,SAAKW,MAAL;AACH;;AAEDI,EAAAA,IAAI,GAAG;AACH,SAAKV,OAAL,GAAe,IAAf;AACH;;AArNa;;AAyNlB;AAEA,iEAAezE,WAAf,E","sources":["webpack://fps-detector/webpack/universalModuleDefinition","webpack://fps-detector/webpack/bootstrap","webpack://fps-detector/webpack/runtime/define property getters","webpack://fps-detector/webpack/runtime/hasOwnProperty shorthand","webpack://fps-detector/webpack/runtime/make namespace object","webpack://fps-detector/./src/index.js"],"sourcesContent":["(function webpackUniversalModuleDefinition(root, factory) {\n\tif(typeof exports === 'object' && typeof module === 'object')\n\t\tmodule.exports = factory();\n\telse if(typeof define === 'function' && define.amd)\n\t\tdefine(\"fps-detector\", [], factory);\n\telse if(typeof exports === 'object')\n\t\texports[\"fps-detector\"] = factory();\n\telse\n\t\troot[\"fps-detector\"] = factory();\n})(self, function() {\nreturn ","// The require scope\nvar __webpack_require__ = {};\n\n","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","class FPSDetector {\r\n constructor($container, option) {\r\n if (typeof $container === 'string') {\r\n $container = document.querySelector($container);\r\n }\r\n\r\n this.option = Object.assign({\r\n width: 85,\r\n height: 30,\r\n colors: ['red', 'orange', 'green'],\r\n bgColor: '#fff',\r\n fgColor: '#ddd',\r\n padding: 1\r\n }, option);\r\n\r\n const $canvas = document.createElement('canvas');\r\n $canvas.style.display = 'block';\r\n $canvas.setAttribute('width', this.option.width);\r\n $canvas.setAttribute('height', this.option.height);\r\n\r\n $container.appendChild($canvas);\r\n $container.title = 'FPS Monitor';\r\n\r\n this.numbers = {\r\n '-': {\r\n 'd': 'm2.615 8.485-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431z'\r\n },\r\n '0': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm7.684 15.56-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zM1.319 8.349l1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'\r\n },\r\n '1': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zm.03 14.873-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'\r\n },\r\n '2': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.781-.049l1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm-1.296-.136 1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'\r\n },\r\n '3': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.781-.049l1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zm-7.654.687 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'\r\n },\r\n '4': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm1.326.685-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'\r\n },\r\n '5': {\r\n 'd': 'm1.289 7.8 1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zm-7.654.687 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'\r\n },\r\n '6': {\r\n 'd': 'm1.289 7.8 1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zM1.319 8.349l1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'\r\n },\r\n '7': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.781-.049l1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm7.684 15.56-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'\r\n },\r\n '8': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502zM1.319 8.349l1.724 1.652v.391h.001v3.551L1.32 15.511v-3.404l-.001-.255V8.35zm.492 7.849 1.652-1.724h3.942l1.568 1.724H5.568l-.255.002H1.811z'\r\n },\r\n '9': {\r\n 'd': 'M9.435.638 7.711 2.29v.391l-.001.001v3.551l1.724 1.568V4.396l.001-.255V.639zM1.289 7.8l1.724-1.652v-.391h.001V2.206L1.29.638v3.404l-.001.255v3.502zm.492-7.849 1.652 1.724h.391l.001.001h3.551L8.944-.048H5.539l-.255-.001H1.782zm.834 8.534-.558-.431 1.116-.862h4.378l1.116.862-1.116.862H3.173l-.558-.431zm6.85 7.026-1.724-1.652v-.391H7.74V9.917l1.724-1.568v3.405l.001.255v3.502z'\r\n }\r\n };\r\n this.initNumbers($canvas);\r\n\r\n this.ctx = $canvas.getContext('2d');\r\n \r\n this.start();\r\n\r\n }\r\n\r\n render() {\r\n\r\n const w = this.option.width;\r\n const h = this.option.height;\r\n const ctx = this.ctx;\r\n const list = this.list;\r\n\r\n const iw = 11;\r\n const ih = 16;\r\n\r\n const padding = this.option.padding;\r\n\r\n const lx = padding;\r\n const ly = padding;\r\n const lw = w - (iw * 2 + padding * 3);\r\n const lh = h - padding * 2;\r\n\r\n let startIndex = padding;\r\n if (list.length > lw) {\r\n list.length = lw;\r\n } else {\r\n startIndex = padding + (lw - list.length);\r\n }\r\n\r\n //console.log(list);\r\n \r\n ctx.fillStyle = this.option.bgColor;\r\n //ctx.globalAlpha = 1;\r\n ctx.fillRect(0, 0, w, h);\r\n\r\n ctx.fillStyle = this.option.fgColor;\r\n ctx.fillRect(lx, ly, lw, lh);\r\n\r\n const ls = [].concat(list).reverse();\r\n\r\n let lastItem;\r\n let lastColor;\r\n ls.forEach((item, i) => {\r\n lastItem = item;\r\n const color = this.getColor(item);\r\n lastColor = color;\r\n ctx.fillStyle = color;\r\n const ch = Math.max(Math.floor(item / 60 * lh), 1);\r\n ctx.fillRect(startIndex + i, lh + ly - ch, 1, ch);\r\n });\r\n\r\n const str = `${lastItem}`.padStart(2, '0');\r\n \r\n const x = Math.floor(lx + lw + padding);\r\n const y = Math.ceil((h - ih) / 2);\r\n\r\n str.split('').forEach((s, i) => {\r\n const img = this.getImg(s, lastColor);\r\n if (img) {\r\n ctx.drawImage(img, x + i * iw, y);\r\n }\r\n });\r\n\r\n }\r\n\r\n getColor(v) {\r\n const colors = this.option.colors;\r\n if (v < 10) {\r\n return colors[0];\r\n }\r\n if (v < 30) {\r\n return colors[1];\r\n }\r\n return colors[2];\r\n }\r\n\r\n initNumbers($container) {\r\n const numbers = this.numbers;\r\n const colors = this.option.colors;\r\n Object.keys(numbers).forEach(key => {\r\n const item = numbers[key];\r\n colors.forEach(color => {\r\n if (item[color]) {\r\n return;\r\n }\r\n const svg = `\r\n <svg viewBox=\"0 0 11 16\" width=\"11\" height=\"16\" xmlns=\"http://www.w3.org/2000/svg\">\r\n <path fill=\"${color}\" d=\"${item.d}\" />\r\n </svg>\r\n `;\r\n const $img = document.createElement('img');\r\n $img.src = `data:image/svg+xml;base64,${btoa(svg)}`;\r\n $container.appendChild($img);\r\n item[color] = $img;\r\n });\r\n });\r\n }\r\n\r\n getImg(n, color) {\r\n const item = this.numbers[n];\r\n if (item) {\r\n return item[color];\r\n }\r\n }\r\n\r\n start() {\r\n this.stopped = false;\r\n this.list = [];\r\n this.frames = 0;\r\n this.startTime = window.performance.now();\r\n this.update();\r\n }\r\n\r\n update() {\r\n if (this.stopped) {\r\n return;\r\n }\r\n window.requestAnimationFrame(() => {\r\n this.count();\r\n });\r\n }\r\n\r\n count() {\r\n\r\n const list = this.list;\r\n\r\n const now = window.performance.now();\r\n let d = now - this.startTime;\r\n //count each 1s\r\n if (d < 1000) {\r\n this.frames += 1;\r\n } else {\r\n\r\n d -= 1000;\r\n while (d > 1000) {\r\n list.unshift(0);\r\n d -= 1000;\r\n }\r\n\r\n //1s\r\n list.unshift(this.frames);\r\n this.render();\r\n //next\r\n this.frames = 1;\r\n this.startTime = now - d;\r\n }\r\n \r\n this.update();\r\n }\r\n\r\n stop() {\r\n this.stopped = true;\r\n }\r\n\r\n}\r\n\r\nexport { FPSDetector };\r\n\r\nexport default FPSDetector;"],"names":["FPSDetector","constructor","$container","option","document","querySelector","Object","assign","width","height","colors","bgColor","fgColor","padding","$canvas","createElement","style","display","setAttribute","appendChild","title","numbers","initNumbers","ctx","getContext","start","render","w","h","list","iw","ih","lx","ly","lw","lh","startIndex","length","fillStyle","fillRect","ls","concat","reverse","lastItem","lastColor","forEach","item","i","color","getColor","ch","Math","max","floor","str","padStart","x","y","ceil","split","s","img","getImg","drawImage","v","keys","key","svg","d","$img","src","btoa","n","stopped","frames","startTime","window","performance","now","update","requestAnimationFrame","count","unshift","stop"],"sourceRoot":""}
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "fps-detector",
3
+ "version": "1.0.0",
4
+ "description": "fps-detector",
5
+ "main": "dist/fps-detector.js",
6
+ "scripts": {},
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/cenfun/fps-detector.git"
13
+ },
14
+ "devDependencies": {
15
+ "eslint": "^8.10.0",
16
+ "eslint-plugin-html": "^6.2.0"
17
+ },
18
+ "dependencies": {}
19
+ }