# All files / utils drawShapes.ts

8.45% Statements 6/71
0% Branches 0/12
0% Functions 0/6
8.45% Lines 6/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    1201x

    1x

    1x

    1x

    1x

    1x

     export const drawCircle = (
      ctx: CanvasRenderingContext2D,
      x: number,
      y: number,
      diameter: number = 5
    ) => {
      ctx.lineWidth = diameter
      ctx.beginPath()
      ctx.lineCap = 'round'
      ctx.moveTo(x, y)
      ctx.lineTo(x, y)
      ctx.stroke()
      ctx.lineCap = 'butt'
    }

    export const drawSquare = (
      ctx: CanvasRenderingContext2D,
      centerX: number,
      centerY: number,
      size: number = 4
    ) => {
      const x = centerX - size / 2
      const y = centerY - size / 2
      ctx.beginPath()
      ctx.rect(x, y, size, size)
      ctx.fill()
    }

    export const drawTriangle = (
      ctx: CanvasRenderingContext2D,
      centerX: number,
      centerY: number,
      sideLength: number = 3
    ) => {
      // Stroking and filling an area will result in a shape whose sides extend a
      // little bit further than the same shape that is only filled
      const STROKE_DELTA = 0.5
      const halfSideLength = sideLength / 2 + STROKE_DELTA

      ctx.beginPath()
      ctx.moveTo(centerX - halfSideLength, centerY + halfSideLength)
      ctx.lineTo(centerX + halfSideLength, centerY + halfSideLength)
      ctx.lineTo(centerX, centerY - halfSideLength)
      ctx.fill()
    }

    export const drawPlus = (
      ctx: CanvasRenderingContext2D,
      centerX: number,
      centerY: number,
      size: number = 6
    ) => {
      const mid = size / 2

      ctx.lineWidth = 2

      ctx.beginPath()
      ctx.moveTo(centerX - mid, centerY)
      ctx.lineTo(centerX + mid, centerY)
      ctx.stroke()

      ctx.beginPath()
      ctx.moveTo(centerX, centerY - mid)
      ctx.lineTo(centerX, centerY + mid)
      ctx.stroke()
    }

    export const drawEx = (
      ctx: CanvasRenderingContext2D,
      centerX: number,
      centerY: number,
      size: number = 6
    ) => {
      const mid = size / 2

      ctx.lineWidth = 2

      ctx.beginPath()
      ctx.moveTo(centerX - mid, centerY - mid)
      ctx.lineTo(centerX + mid, centerY + mid)
      ctx.closePath()
      ctx.stroke()

      ctx.beginPath()
      ctx.moveTo(centerX - mid, centerY + mid)
      ctx.lineTo(centerX + mid, centerY - mid)
      ctx.closePath()
      ctx.stroke()
    }

    export const drawTritip = (
      ctx: CanvasRenderingContext2D,
      centerX: number,
      centerY: number,
      size: number = 3
    ) => {
      const cos30 = 0.86602540378
      const sin30 = 0.5

      ctx.lineWidth = 1

      ctx.beginPath()
      ctx.moveTo(centerX, centerY)
      ctx.lineTo(centerX + cos30 * size, centerY + sin30 * size)
      ctx.closePath()
      ctx.stroke()

      ctx.beginPath()
      ctx.moveTo(centerX, centerY)
      ctx.lineTo(centerX - cos30 * size, centerY + sin30 * size)
      ctx.closePath()
      ctx.stroke()

      ctx.beginPath()
      ctx.moveTo(centerX, centerY)
      ctx.lineTo(centerX, centerY - size)
      ctx.closePath()
      ctx.stroke()
    }