Compose-Extended-Gestures icon indicating copy to clipboard operation
Compose-Extended-Gestures copied to clipboard

How to get the screen size of the element?

Open cybercoder-naj opened this issue 1 year ago • 1 comments

I am working on the Canvas object such as follows:

fun Component() {
  Canvas(
    modifier = Modifier
      .clipToBounds()
      .pointerMotionEvents(
        onDown = {/* I want the size of the Canvas here */}
      )
  ) {
    /* drawing elements on the Canvas */
  }
}

Do you think adding something to the modifier with the size of the Canvas would be possible? If the user is pressing "Down" on the canvas, it is useful to know where it is with respect to the dimensions of the canvas.

cybercoder-naj avatar Apr 19 '24 16:04 cybercoder-naj

The current workaround is having a local state to keep the size. Wondering if the library can do this without it.

@Composable
fun Component() {
  var canvasSize by remember { mutableStateOf(Size.Unspecified) }

  Canvas(
    modifier = Modifier
      /* look previous comment */
  ) {
    canvasSize = size

    /* drawing elements */
  }
}

cybercoder-naj avatar Apr 19 '24 16:04 cybercoder-naj

you can use PointerInputScope function for this as

Modifier.pointerInput(Unit){
    val size = size
    detectMotionEvents(
        onDown = {
            
        },
        onMove = {
            
        },
        onUp = {
            
        }
    )
}

SmartToolFactory avatar Jun 18 '24 18:06 SmartToolFactory