aboutsummaryrefslogtreecommitdiffstats
path: root/lib/canvasapp.shared.moon
blob: f05bfec4d5e0c9f036e57774e1585cfc002ab5a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
window = js.global

class CanvasApp
  width: 500
  height: 400
  new: =>
    @canvas = window.document\createElement 'canvas'
    @canvas.width, @canvas.height = @width, @height
    @ctx = @canvas\getContext '2d'
    @time = 0

    @canvas.tabIndex = 0
    @canvas\addEventListener 'click', (_, e) -> @click and @click e.offsetX, e.offsetY, e.button
    @canvas\addEventListener 'keydown', (_, e) -> @keydown and @keydown e.key, e.code

    lastMillis = window.performance\now!
    t = @
    animationFrame = (_, millis) ->
      if not @paused
        @update (millis - lastMillis) / 1000
        @ctx\resetTransform!
        @draw!
        window\setTimeout (->
          window\requestAnimationFrame animationFrame
        ), 0
      lastMillis = millis
    window\requestAnimationFrame animationFrame

  update: (dt) =>
    @time += dt

{
  :CanvasApp
}