saket / telephoto

Building blocks for designing media experiences in Compose UI
https://saket.github.io/telephoto/
Apache License 2.0
869 stars 28 forks source link

Restricting Zoom and Scroll Direction (X/Y Axis Only) and Callback for Current ZoomableState Updates. #59

Closed Mett-Barr closed 6 months ago

Mett-Barr commented 6 months ago

Hello, first off, thanks for your work on this library. I'm currently integrating it into a project where I need to restrict zoom and scroll functionality to either the X or Y axis. Additionally, I'm looking for a way to access and update the current ZoomableState during zoom events via a callback. Is there an existing method in the library to handle these requirements, or are these features something that could be considered for future development?

saket commented 6 months ago

I need to restrict zoom and scroll functionality to either the X or Y axis

Additionally, I'm looking for a way to access and update the current ZoomableState during zoom events via a callback

Can you share some more context for both of these usecases?

saket commented 6 months ago

For your question posted here, wanna try applying the transformations yourself? This way you can filter the scale/offset values reported by telephoto's gesture listener along your desired axis.

val state = rememberZoomableState(
  zoomSpec = ZoomSpec(10f),
  autoApplyTransformations = false,
)

SineWave(
  Modifier
    .weight(1f)
    .fillMaxWidth()
    .zoomable(state, clipToBounds = false)
    .applyTransformation(state.contentTransformation)
)
fun Modifier.applyTransformation(transformation: ZoomableContentTransformation): Modifier {
  return graphicsLayer {
    scaleX = transformation.scale.scaleX
    //scaleY = transformation.scale.scaleY
    translationX = transformation.offset.x
    //translationY = transformation.offset.y
    transformOrigin = transformation.transformOrigin
  }
}
Mett-Barr commented 6 months ago

Thank you very much for your response. The solution you provided works perfectly. I probably should have thought of this approach myself after realizing that the Current ZoomableState Update was available. Nevertheless, I am extremely grateful for your assistance. It has been incredibly helpful. Thank you again for providing such an excellent library.