codecombat/app/lib/surface/Dropper.coffee

28 lines
660 B
CoffeeScript
Raw Normal View History

2014-01-03 13:32:13 -05:00
Dropper = class Dropper
lostFrames: 0.0
dropCounter: 0
2014-01-03 13:32:13 -05:00
constructor: ->
@listener = (e) => @tick(e)
tick: ->
unless @tickedOnce
@tickedOnce = true # Can't get measured FPS on the 0th frame.
2014-01-03 13:32:13 -05:00
return
--@dropCounter if @dropCounter > 0
2014-01-03 13:32:13 -05:00
# Track number of frames we've lost since the last tick.
2014-01-03 13:32:13 -05:00
fps = createjs.Ticker.getFPS()
actual = createjs.Ticker.getMeasuredFPS(1)
@lostFrames += (fps - actual) / fps
2014-01-03 13:32:13 -05:00
# If lostFrames > 1, drop that number for the next tick.
@dropCounter += parseInt @lostFrames
@lostFrames = @lostFrames % 1
2014-01-03 13:32:13 -05:00
drop: ->
return @dropCounter > 0
2014-01-03 13:32:13 -05:00
module.exports = new Dropper()