-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect measurements for SVG elements #1231
Comments
Repro? |
Hey @msand, did you had any chance to take a look? |
Not yet no |
Seems the issue is that the bounds aren't calculated in the measure phase, but only in the draw phase. You can use the onLayout nativeEvent layout property instead, it's called as soon as the bounds are known: import React from 'react';
import {Svg, Circle} from 'react-native-svg';
export default () => (
<Svg height="100%">
<Circle
x={50}
y={70}
r={65}
onLayout={e => {
console.log(e.nativeEvent.layout);
}}
/>
</Svg>
); |
@msand the reason why I'm using "measureInWindow" is that I need to get absolute (not relative) positions of the element in "window". |
That happens only on Android and only with SVG elements. |
It seems to me like it should be possible to achieve, by measuring the svg root / containing view, and using the layout values from the primitive you're interested in: import React, {useRef, useCallback} from 'react';
import {View, findNodeHandle, UIManager} from 'react-native';
import {Svg, Circle} from 'react-native-svg';
const measureInWindow = handler =>
new Promise(resolve => {
UIManager.measureInWindow(handler, (x, y, width, height) => {
resolve({x, y, width, height});
});
});
export default () => {
const svg = useRef(null);
const handleLayout = useCallback(async e => {
console.log('circle', e.nativeEvent.layout);
const handler = findNodeHandle(svg.current);
if (handler === null) {
return;
}
const result = await measureInWindow(handler);
console.warn('measure', result);
}, []);
return (
<View
style={{flex: 1, justifyContent: 'center'}}
onLayout={e => {
console.log('view', e.nativeEvent.layout);
}}>
<Svg
ref={svg}
height="50%"
onLayout={e => {
console.log('svg', e.nativeEvent.layout);
}}>
<Circle x={50} y={70} r={65} onLayout={handleLayout} />
</Svg>
</View>
);
}; |
I think I have a fix now, can you try with the latest commit from the develop branch? |
I'm getting this output using android and this code now:
And ios:
import React, {useRef, useCallback} from 'react';
import {View, findNodeHandle, UIManager} from 'react-native';
import {Svg, Circle} from 'react-native-svg';
export default () => {
const svg = useRef(null);
const handleLayout = useCallback(async e => {
console.log('circle', e.nativeEvent.layout);
const handle = findNodeHandle(svg.current);
if (handle === null) {
return;
}
UIManager.measureInWindow(handle, (x, y, width, height) => {
console.warn('measure', {x, y, width, height});
});
}, []);
return (
<View
style={{flex: 1, justifyContent: 'center'}}
onLayout={e => {
console.log('view', e.nativeEvent.layout);
}}>
<Svg
height="50%"
onLayout={e => {
console.log('svg', e.nativeEvent.layout);
}}>
<Circle x={50} y={70} r={65} onLayout={handleLayout} ref={svg} />
</Svg>
</View>
);
}; |
# [9.14.0](v9.13.6...v9.14.0) (2020-01-04) ### Bug Fixes * **android:** correct values for measureInWindow, fixes [#1231](#1231) ([3bf07f8](3bf07f8)) * **android:** elements not touchable if below opacity limit ([ebc7220](ebc7220)), closes [#1200](#1200) * **android:** fix radial gradient vertical center offset scaling ([d5bddd5](d5bddd5)) * **ios:** crash when offset is outside range [#1201](#1201) ([a2ef51f](a2ef51f)) * **web:** fix gesture responder dimensions measurement ([36c20b3](36c20b3)) * extraction of clip rule, fixes [#1233](#1233) ([f93bdde](f93bdde)) * Text color doesn't work with inlineSize [#1225](#1225) ([027b8c1](027b8c1)) ### Features * **android:** support using other native views in e.g. masks ([15b4ac6](15b4ac6)) * **ios:** support using other native views in e.g. masks ([518a3b1](518a3b1)) * **web:** Implement support for event, touch & responder handlers ([60561ec](60561ec)) * **web:** Optimize: only set responders if press handler exists ([23250ad](23250ad)) * Implement display="none" ([3e3ad13](3e3ad13)), closes [#1220](#1220)
🎉 This issue has been resolved in version 9.14.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Released in v10 (breaking change wasn't registered correctly by semantic-release and forgot to do dry-run first) |
@msand Hey! Sorry for the late response. |
Bug
UIManager.measureInWindow returns incorrect values for (Circle) element on Android platform. (iOS is ok).
Environment info
Library version: 9.13.6
The text was updated successfully, but these errors were encountered: