Advertisement
Guest User

Untitled

a guest
Mar 7th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. var scale by remember { mutableStateOf(1f) }
  2. var canvasOffset by remember { mutableStateOf(Offset.Zero) }
  3. val rectangles = remember { mutableStateListOf<Offset>() }
  4.  
  5. Box(
  6. Modifier.windowObject()
  7. .fillMaxWidth(0.8f)
  8. .fillMaxHeight()
  9. .background(MaterialTheme.colors.surface)
  10. .onPointerEvent(PointerEventType.Scroll) {
  11. // Mouse scroll to zoom in and out
  12. scale = (scale * exp(-it.changes.first().scrollDelta.y.toInt().sign * 0.2f)).coerceIn(0.25f, 1.75f)
  13. }.pointerInput(Unit) {
  14. detectDragGestures(
  15. matcher = PointerMatcher.mouse(PointerButton.Tertiary),
  16. onDrag = { dragAmount ->
  17. // Middle click move canvas
  18. canvasOffset += dragAmount
  19. }
  20. )
  21. }.pointerInput(Unit) {
  22. detectTapGestures(matcher = PointerMatcher.mouse(PointerButton.Secondary)) { clickLocation ->
  23. // Right click to add a rectangle
  24. rectangles.add(clickLocation - canvasOffset)
  25. }
  26. }
  27. ) {
  28.  
  29. Canvas(
  30. modifier = Modifier
  31. .fillMaxSize()
  32. .clipToBounds()
  33. .graphicsLayer(
  34. scaleX = scale,
  35. scaleY = scale,
  36. translationX = canvasOffset.x,
  37. translationY = canvasOffset.y,
  38. )
  39. ) {
  40.  
  41. rectangles.forEach { rect ->
  42. drawRect(
  43. topLeft = rect,
  44. color = Color.Red,
  45. size = Size(100f, 100f)
  46. )
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement