aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/Renderer.kt
blob: 49d6b2c54d3d63199ce3512b6b04a07054c09b8f (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
35
36
37
import kotlinx.browser.document
import org.w3c.dom.CanvasRenderingContext2D
import org.w3c.dom.HTMLCanvasElement

class Renderer(val viewportWidth: Int, val viewportHeight: Int, private val outputScale: Int) {

  private val canvas = (document.getElementById("render-output") as HTMLCanvasElement)
    .apply {
      width = viewportWidth * outputScale
      height = viewportHeight * outputScale
    }

  private val context = (canvas.getContext("2d") as CanvasRenderingContext2D)
    .apply { scale(outputScale.toDouble(), outputScale.toDouble()) }

  fun drawLine(startX: Double, startY: Double, endX: Double, endY: Double, cssColour: String = "#FF0000") {
    context.strokeStyle = cssColour
    context.lineWidth = 2.0
    context.beginPath()
    context.moveTo(startX, startY)
    context.lineTo(endX, endY)
    context.stroke()
  }

  fun drawLine(startX: Int, startY: Double, endX: Int, endY: Double, cssColour: String = "#FF0000") {
    drawLine(startX.toDouble(), startY, endX.toDouble(), endY, cssColour)
  }

  fun drawLine(startX: Int, startY: Double, endX: Int, endY: Int, cssColour: String = "#FF0000") {
    drawLine(startX.toDouble(), startY, endX.toDouble(), endY.toDouble(), cssColour)
  }

  fun clear() {
    context.clearRect(0.0, 0.0, viewportWidth.toDouble(), viewportHeight.toDouble())
  }

}