Ugly Android performance with PanResponder
See original GitHub issueTry to zoom — urgh!
import React from 'react'
import {
Dimensions,
PanResponder,
Animated,
} from 'react-native'
import { Svg, Rect, G } from 'react-native-svg'
const { width, height } = Dimensions.get('window')
const GAnimated = Animated.createAnimatedComponent(G)
// utils beginning
function calcDistance(x1, y1, x2, y2) {
const dx = x1 - x2
const dy = y1 - y2
return Math.sqrt(dx * dx + dy * dy)
}
const range = (min, max) =>
Math.floor(Math.random() * (
max - min + 1
)) + min
const genPoly = (count) => {
return Array.from({ length: count }).map((_, i) => {
const x = range(20, 320)
const y = range(170, 470)
const radius = range(10, 30)
return <Rect
onPress={() => alert(i)}
x={x}
y={y}
width={radius}
height={radius}
fill={`rgba(${range(0, 255)}, ${range(0, 255)}, ${range(0, 255)}, 0.2)`}
strokeWidth={1}
stroke='black'
key={i}
style={{
zIndex: 100,
}}
/>
})
}
const polygons = genPoly(100)
// utils ending
function ZoomProblem() {
const zoom = new Animated.Value(1)
const position = new Animated.ValueXY()
let pinchDistanceInit = 0
let zoomInit = zoom.__getValue()
const panResponder = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => false,
onStartShouldSetPanResponderCapture: (evt, gestureState) => false,
onMoveShouldSetPanResponder: (evt, gestureState) => true,
onMoveShouldSetPanResponderCapture: (evt, gestureState) => false,
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderTerminate: (evt, gestureState) => {},
onPanResponderGrant: (evt) => {
if (evt.nativeEvent.touches.length === 2) {
const [
{ locationX: x1, locationY: y1 },
{ locationX: x2, locationY: y2 },
] = evt.nativeEvent.touches
pinchDistanceInit = calcDistance(x1, y1, x2, y2)
zoomInit = zoom.__getValue()
}
position.setOffset(position.__getValue()) // prevents offset from resetting
},
onPanResponderMove: (evt, gesture) => {
switch (gesture.numberActiveTouches) {
case 1: {
position.setValue({ x: gesture.dx, y: gesture.dy })
break
}
case 2: {
const [
{ locationX: x1, locationY: y1 },
{ locationX: x2, locationY: y2 },
] = evt.nativeEvent.touches
const distance = calcDistance(x1, y1, x2, y2)
Animated.timing(
zoom,
{
toValue: zoomInit * (distance / pinchDistanceInit),
duration: 0,
useNativeDriver: true,
},
).start()
break
}
}
},
})
const { top, left } = position.getLayout()
return (
<Animated.View
{...panResponder.panHandlers}
style={{
transform: [
{ translateX: left },
{ translateY: top },
],
}}
>
<Svg
width={width}
height={height}
>
<GAnimated
style={{
transform: [{ scale: zoom }],
}}
>
{polygons}
</GAnimated>
</Svg>
</Animated.View>
)
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:11
Top Results From Across the Web
Ugly Android performance with PanResponder #1064 - GitHub
I've optimized the rendering performance on Android quite a bit, found two low hanging fruits by profiling my latest example.
Read more >React Native: Idea to Product in One Week - Medium
Using the research I did about designing for iOS and Android, we decided that this app is simple enough that it could be...
Read more >Newest 'react-native-scrollview' Questions - Stack Overflow
I am working on React Native in which I want to stop multiline textinput component's scroll inside ScrollView. How we can achieve it?...
Read more >Development - Thoughts from the folks at PROTOTYP
Requests on the frontend application can easily become a big and ugly ... Improving website performance by eliminating render-blocking CSS and JavaScript.
Read more >Build React-native app in the right way. - DEV Community
9.1 iOS; 9.2 Android; 9.3 Resources. 10 Performance (draft). 10.1 shouldComponentUpdate; 10.2 Resources. Resources ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Slightly more advanced viewer example, adapted to svg and slightly stripped down version from: https://github.com/kmagiera/react-native-gesture-handler/issues/546#issuecomment-481341466 https://github.com/Ashoat/squadcal/blob/master/native/media/multimedia-modal.react.js
Hooks instead of components: