# All files / utils brush.ts

13.33% Statements 2/15
0% Branches 0/2
0% Functions 0/2
15.38% Lines 2/13

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

    1x

    1x

     import {Scale} from '../types'
    import {DragEvent} from '../utils/useDragEvent'

    export const getRectDimensions = (
      event: DragEvent,
      plotWidth: number,
      plotHeight: number
    ) => {
      Iif (event.direction === 'x') {
        const x = Math.max(0, Math.min(event.initialX, event.x))

        const width = Math.min(Math.max(event.initialX, event.x) - x, plotWidth - x)

        return {
          x,
          y: 0,
          width,
          height: plotHeight,
        }
      }

      Iif (event.direction === 'y') {
        const y = Math.max(0, Math.min(event.initialY, event.y))

        const height = Math.min(
          Math.max(event.initialY, event.y) - y,
          plotHeight - y
        )

        return {
          x: 0,
          y,
          width: plotWidth,
          height: height,
        }
      }

      return null
    }

    export const rangeToDomain = (
      [p0, p1]: number[],
      scale: Scale<number, number>,
      length: number
    ): number[] => [
      scale.invert(Math.max(Math.min(p0, p1), 0)),
      scale.invert(Math.min(Math.max(p0, p1), length)),
    ]