, , , ( ) . , - .
, Snack: https://snack.expo.io/BJnDImQlr
:
, , , . transformOrigin , OP . , , ( ).
function transformOrigin(matrix, origin) {
const { x, y, z } = origin;
const translate = MatrixMath.createIdentityMatrix();
MatrixMath.reuseTranslate3dCommand(translate, x, y, z);
MatrixMath.multiplyInto(matrix, translate, matrix);
}
, ( MatrixMath.createIdentityMatrix), . , , , , x.
function scale(x) {
return [
x, 0, 0, 0,
0, x, 0, 0,
0, 0, x, 0,
0, 0, 0, 1
];
}
.
ref .- .
- .
- (
xAxis: 0, yAxis: 0). MatrixMath.multiplyInto .ref setNativeProps.
function transformScale(ref, scaleBy, width, height) {
const matrix = MatrixMath.createIdentityMatrix();
const toScale = scale(scaleBy);
transformOrigin(matrix, {
x: (width * scaleBy - width) / 2,
y: (height * scaleBy - height) / 2,
z: 0
});
MatrixMath.multiplyInto(matrix, matrix, toScale);
ref.setNativeProps({
style: { transform: [{ matrix }] }
});
}
, , React. , transformScale(this._target, 3, width, height). , scaleBy , .
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
width: 0,
height: 0
};
}
handleBaseLayout = (e) => {
const { width, height } = e.nativeEvent.layout;
this.setState({ width, height }, () => {
transformScale(this._target, 3, width, height);
});
};
render() {
return (
<View style={styles.container}>
<View
style={styles.target}
ref={c => (this._target = c)}
onLayout={this.handleBaseLayout}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
paddingTop: Constants.statusBarHeight,
backgroundColor: '#ecf0f1',
padding: 8,
},
target: {
width: 100,
height: 100,
backgroundColor: 'blue',
},
});