Statistics.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * Statistics.js
  3. *
  4. * @author realor
  5. */
  6. import { Panel } from './Panel.js'
  7. class Statistics extends Panel {
  8. constructor(application) {
  9. super(application)
  10. this.id = 'statistics'
  11. this.title = 'Statistics'
  12. this.position = 'right'
  13. this.preferredHeight = 80
  14. this.bodyElem.style.cssText = 'opacity:0.9;z-index:10000;overflow:hidden;'
  15. var fpsPanel = new Statistics.Panel('FPS', '#0ff', '#002')
  16. this.bodyElem.appendChild(fpsPanel.dom)
  17. var msPanel = new Statistics.Panel('MS', '#0f0', '#020')
  18. this.bodyElem.appendChild(msPanel.dom)
  19. var beginTime = (performance || Date).now()
  20. var prevTime = beginTime
  21. var frames = 0
  22. this.animate = function() {
  23. frames++
  24. var time = (performance || Date).now()
  25. msPanel.update(time - beginTime, 200)
  26. if (time >= prevTime + 1000) {
  27. fpsPanel.update((frames * 1000) / (time - prevTime), 100)
  28. prevTime = time
  29. frames = 0
  30. }
  31. beginTime = time
  32. }
  33. }
  34. onShow() {
  35. const application = this.application
  36. application.addEventListener('animation', this.animate)
  37. }
  38. onHide() {
  39. const application = this.application
  40. application.removeEventListener('animation', this.animate)
  41. }
  42. }
  43. Statistics.Panel = function(name, fg, bg) {
  44. var min = Infinity,
  45. max = 0,
  46. round = Math.round
  47. var PR = round(window.devicePixelRatio || 1)
  48. var CANVAS_WIDTH = 116,
  49. CANVAS_HEIGHT = 48,
  50. WIDTH = CANVAS_WIDTH * PR,
  51. HEIGHT = CANVAS_HEIGHT * PR,
  52. TEXT_X = 3 * PR,
  53. TEXT_Y = 2 * PR,
  54. GRAPH_X = 3 * PR,
  55. GRAPH_Y = 15 * PR,
  56. GRAPH_WIDTH = (CANVAS_WIDTH - 6) * PR,
  57. GRAPH_HEIGHT = (CANVAS_HEIGHT - 18) * PR
  58. var canvas = document.createElement('canvas')
  59. canvas.width = WIDTH
  60. canvas.height = HEIGHT
  61. canvas.style.cssText = 'width:' + CANVAS_WIDTH + 'px;height:' + CANVAS_HEIGHT + 'px;margin:2px 2px 0 2px'
  62. var context = canvas.getContext('2d')
  63. context.font = 'bold ' + 9 * PR + 'px Helvetica,Arial,sans-serif'
  64. context.textBaseline = 'top'
  65. context.fillStyle = bg
  66. context.fillRect(0, 0, WIDTH, HEIGHT)
  67. context.fillStyle = fg
  68. context.fillText(name, TEXT_X, TEXT_Y)
  69. context.fillRect(GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT)
  70. context.fillStyle = bg
  71. context.globalAlpha = 0.9
  72. context.fillRect(GRAPH_X, GRAPH_Y, GRAPH_WIDTH, GRAPH_HEIGHT)
  73. return {
  74. dom: canvas,
  75. update: function(value, maxValue) {
  76. min = Math.min(min, value)
  77. max = Math.max(max, value)
  78. context.fillStyle = bg
  79. context.globalAlpha = 1
  80. context.fillRect(0, 0, WIDTH, GRAPH_Y)
  81. context.fillStyle = fg
  82. context.fillText(round(value) + ' ' + name + ' (' + round(min) + '-' + round(max) + ')', TEXT_X, TEXT_Y)
  83. context.drawImage(canvas, GRAPH_X + PR, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT, GRAPH_X, GRAPH_Y, GRAPH_WIDTH - PR, GRAPH_HEIGHT)
  84. context.fillRect(GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, GRAPH_HEIGHT)
  85. context.fillStyle = bg
  86. context.globalAlpha = 0.9
  87. context.fillRect(GRAPH_X + GRAPH_WIDTH - PR, GRAPH_Y, PR, round((1 - value / maxValue) * GRAPH_HEIGHT))
  88. }
  89. }
  90. }
  91. export { Statistics }