Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var scale by remember { mutableStateOf(1f) }
- var canvasOffset by remember { mutableStateOf(Offset.Zero) }
- val rectangles = remember { mutableStateListOf<Offset>() }
- Box(
- Modifier.windowObject()
- .fillMaxWidth(0.8f)
- .fillMaxHeight()
- .background(MaterialTheme.colors.surface)
- .onPointerEvent(PointerEventType.Scroll) {
- // Mouse scroll to zoom in and out
- scale = (scale * exp(-it.changes.first().scrollDelta.y.toInt().sign * 0.2f)).coerceIn(0.25f, 1.75f)
- }.pointerInput(Unit) {
- detectDragGestures(
- matcher = PointerMatcher.mouse(PointerButton.Tertiary),
- onDrag = { dragAmount ->
- // Middle click move canvas
- canvasOffset += dragAmount
- }
- )
- }.pointerInput(Unit) {
- detectTapGestures(matcher = PointerMatcher.mouse(PointerButton.Secondary)) { clickLocation ->
- // Right click to add a rectangle
- rectangles.add(clickLocation - canvasOffset)
- }
- }
- ) {
- Canvas(
- modifier = Modifier
- .fillMaxSize()
- .clipToBounds()
- .graphicsLayer(
- scaleX = scale,
- scaleY = scale,
- translationX = canvasOffset.x,
- translationY = canvasOffset.y,
- )
- ) {
- rectangles.forEach { rect ->
- drawRect(
- topLeft = rect,
- color = Color.Red,
- size = Size(100f, 100f)
- )
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement