import Game from '../core/game'

const { round } = Math

class FpsCounter {
    constructor () {
        this.lastLoop = null
        this.counter = 0
        this._fps = 0
    }

    init () {
        this.lastLoop = new Date

        Game.on('draw', ({ ctx }) => {

            const thisLoop = new Date
            const fps = round(1000 / (thisLoop - this.lastLoop))
            this.lastLoop = thisLoop

            if (!this.counter) {
                this._fps = fps
            }

            this.counter = (this.counter + 1) % 20

            ctx.font = '18px sans-serif'
            ctx.fillStyle = '#fff'
            ctx.fillText(this._fps + ' FPS', 10, 30)

        }, 100)
    }
}

export default new FpsCounter
