Improved shadow performance
Summary:
public
React Native currently exposes the iOS layer shadow properties more-or-less directly, however there are a number of problems with this:
1) Performance when using these properties is poor by default. That's because iOS calculates the shadow by getting the exact pixel mask of the view, including any tranlucent content, and all of its subviews, which is very CPU and GPU-intensive.
2) The iOS shadow properties do not match the syntax or semantics of the CSS box-shadow standard, and are unlikely to be possible to implement on Android.
3) We don't expose the `layer.shadowPath` property, which is crucial to getting good performance out of layer shadows.
This diff solves problem number 1) by implementing a default `shadowPath` that matches the view border for views with an opaque background. This improves the performance of shadows by optimizing for the common usage case. I've also reinstated background color propagation for views which have shadow props - this should help ensure that this best-case scenario occurs more often.
For views with an explicit transparent background, the shadow will continue to work as it did before ( `shadowPath` will be left unset, and the shadow will be derived exactly from the pixels of the view and its subviews). This is the worst-case path for performance, however, so you should avoid it unless absolutely necessary. **Support for this may be disabled by default in future, or dropped altogether.**
For translucent images, it is suggested that you bake the shadow into the image itself, or use another mechanism to pre-generate the shadow. For text shadows, you should use the textShadow properties, which work cross-platform and have much better performance.
Problem number 2) will be solved in a future diff, possibly by renaming the iOS shadowXXX properties to boxShadowXXX, and changing the syntax and semantics to match the CSS standards.
Problem number 3) is now mostly moot, since we generate the shadowPath automatically. In future, we may provide an iOS-specific prop to set the path explicitly if there's a demand for more precise control of the shadow.
Reviewed By: weicool
Differential Revision: D2827581
fb-gh-sync-id: 853aa018e1d61d5f88304c6fc1b78f9d7e739804
2016-01-14 22:03:31 +00:00
|
|
|
/**
|
|
|
|
* The examples provided by Facebook are for non-commercial testing and
|
|
|
|
* evaluation purposes only.
|
|
|
|
*
|
|
|
|
* Facebook reserves all rights not expressly granted.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|
|
|
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var ReactNative = require('react-native');
|
Improved shadow performance
Summary:
public
React Native currently exposes the iOS layer shadow properties more-or-less directly, however there are a number of problems with this:
1) Performance when using these properties is poor by default. That's because iOS calculates the shadow by getting the exact pixel mask of the view, including any tranlucent content, and all of its subviews, which is very CPU and GPU-intensive.
2) The iOS shadow properties do not match the syntax or semantics of the CSS box-shadow standard, and are unlikely to be possible to implement on Android.
3) We don't expose the `layer.shadowPath` property, which is crucial to getting good performance out of layer shadows.
This diff solves problem number 1) by implementing a default `shadowPath` that matches the view border for views with an opaque background. This improves the performance of shadows by optimizing for the common usage case. I've also reinstated background color propagation for views which have shadow props - this should help ensure that this best-case scenario occurs more often.
For views with an explicit transparent background, the shadow will continue to work as it did before ( `shadowPath` will be left unset, and the shadow will be derived exactly from the pixels of the view and its subviews). This is the worst-case path for performance, however, so you should avoid it unless absolutely necessary. **Support for this may be disabled by default in future, or dropped altogether.**
For translucent images, it is suggested that you bake the shadow into the image itself, or use another mechanism to pre-generate the shadow. For text shadows, you should use the textShadow properties, which work cross-platform and have much better performance.
Problem number 2) will be solved in a future diff, possibly by renaming the iOS shadowXXX properties to boxShadowXXX, and changing the syntax and semantics to match the CSS standards.
Problem number 3) is now mostly moot, since we generate the shadowPath automatically. In future, we may provide an iOS-specific prop to set the path explicitly if there's a demand for more precise control of the shadow.
Reviewed By: weicool
Differential Revision: D2827581
fb-gh-sync-id: 853aa018e1d61d5f88304c6fc1b78f9d7e739804
2016-01-14 22:03:31 +00:00
|
|
|
var {
|
|
|
|
Image,
|
|
|
|
StyleSheet,
|
|
|
|
View
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
Improved shadow performance
Summary:
public
React Native currently exposes the iOS layer shadow properties more-or-less directly, however there are a number of problems with this:
1) Performance when using these properties is poor by default. That's because iOS calculates the shadow by getting the exact pixel mask of the view, including any tranlucent content, and all of its subviews, which is very CPU and GPU-intensive.
2) The iOS shadow properties do not match the syntax or semantics of the CSS box-shadow standard, and are unlikely to be possible to implement on Android.
3) We don't expose the `layer.shadowPath` property, which is crucial to getting good performance out of layer shadows.
This diff solves problem number 1) by implementing a default `shadowPath` that matches the view border for views with an opaque background. This improves the performance of shadows by optimizing for the common usage case. I've also reinstated background color propagation for views which have shadow props - this should help ensure that this best-case scenario occurs more often.
For views with an explicit transparent background, the shadow will continue to work as it did before ( `shadowPath` will be left unset, and the shadow will be derived exactly from the pixels of the view and its subviews). This is the worst-case path for performance, however, so you should avoid it unless absolutely necessary. **Support for this may be disabled by default in future, or dropped altogether.**
For translucent images, it is suggested that you bake the shadow into the image itself, or use another mechanism to pre-generate the shadow. For text shadows, you should use the textShadow properties, which work cross-platform and have much better performance.
Problem number 2) will be solved in a future diff, possibly by renaming the iOS shadowXXX properties to boxShadowXXX, and changing the syntax and semantics to match the CSS standards.
Problem number 3) is now mostly moot, since we generate the shadowPath automatically. In future, we may provide an iOS-specific prop to set the path explicitly if there's a demand for more precise control of the shadow.
Reviewed By: weicool
Differential Revision: D2827581
fb-gh-sync-id: 853aa018e1d61d5f88304c6fc1b78f9d7e739804
2016-01-14 22:03:31 +00:00
|
|
|
|
|
|
|
var styles = StyleSheet.create({
|
|
|
|
box: {
|
|
|
|
width: 100,
|
|
|
|
height: 100,
|
|
|
|
borderWidth: 2,
|
|
|
|
},
|
|
|
|
shadow1: {
|
|
|
|
shadowOpacity: 0.5,
|
|
|
|
shadowRadius: 3,
|
|
|
|
shadowOffset: {width: 2, height: 2},
|
|
|
|
},
|
|
|
|
shadow2: {
|
|
|
|
shadowOpacity: 1.0,
|
|
|
|
shadowColor: 'red',
|
|
|
|
shadowRadius: 0,
|
|
|
|
shadowOffset: {width: 3, height: 3},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.title = 'Box Shadow';
|
|
|
|
exports.description = 'Demonstrates some of the shadow styles available to Views.';
|
|
|
|
exports.examples = [
|
|
|
|
{
|
|
|
|
title: 'Basic shadow',
|
|
|
|
description: 'shadowOpacity: 0.5, shadowOffset: {2, 2}',
|
|
|
|
render() {
|
|
|
|
return <View style={[styles.box, styles.shadow1]} />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Colored shadow',
|
|
|
|
description: 'shadowColor: \'red\', shadowRadius: 0',
|
|
|
|
render() {
|
|
|
|
return <View style={[styles.box, styles.shadow2]} />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Shaped shadow',
|
|
|
|
description: 'borderRadius: 50',
|
|
|
|
render() {
|
|
|
|
return <View style={[styles.box, styles.shadow1, {borderRadius: 50}]} />;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Image shadow',
|
|
|
|
description: 'Image shadows are derived exactly from the pixels.',
|
|
|
|
render() {
|
|
|
|
return <Image
|
|
|
|
source={require('./hawk.png')}
|
|
|
|
style={[styles.box, styles.shadow1, {borderWidth: 0, overflow: 'visible'}]}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Child shadow',
|
|
|
|
description: 'For views without an opaque background color, shadow will be derived from the subviews.',
|
|
|
|
render() {
|
|
|
|
return <View style={[styles.box, styles.shadow1, {backgroundColor: 'transparent'}]}>
|
|
|
|
<View style={[styles.box, {width: 80, height: 80, borderRadius: 40, margin: 8, backgroundColor: 'red'}]}/>
|
|
|
|
</View>;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
];
|