|
| 1 | +/** |
| 2 | + * Copyright 2013-2014 Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule Rectangle.art |
| 10 | + * @typechecks |
| 11 | + * |
| 12 | + * Example usage: |
| 13 | + * <Rectangle |
| 14 | + * width={50} |
| 15 | + * height={50} |
| 16 | + * stroke="green" |
| 17 | + * fill="blue" |
| 18 | + * /> |
| 19 | + * |
| 20 | + * Additional optional properties: |
| 21 | + * (Number) radius |
| 22 | + * (Number) radiusTopLeft |
| 23 | + * (Number) radiusTopRight |
| 24 | + * (Number) radiusBottomLeft |
| 25 | + * (Number) radiusBottomRight |
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +var React = require('react-native'); |
| 32 | +var ReactART = require('ReactNativeART'); |
| 33 | + |
| 34 | +var Group = ReactART.Group; |
| 35 | +var Shape = ReactART.Shape; |
| 36 | +var Surface = ReactART.Surface; |
| 37 | +var Transform = ReactART.Transform; |
| 38 | + |
| 39 | +var Props = React.PropTypes; |
| 40 | +var Shape = ReactART.Shape; |
| 41 | +var Path = ReactART.Path; |
| 42 | + |
| 43 | +/** |
| 44 | + * Rectangle is a React component for drawing rectangles. Like other ReactART |
| 45 | + * components, it must be used in a <Surface>. |
| 46 | + */ |
| 47 | +var Rectangle = React.createClass({ |
| 48 | + propTypes: { |
| 49 | + width: Props.number.isRequired, |
| 50 | + height: Props.number.isRequired, |
| 51 | + radius: Props.number, |
| 52 | + radiusTopLeft: Props.number, |
| 53 | + radiusTopRight: Props.number, |
| 54 | + radiusBottomRight: Props.number, |
| 55 | + radiusBottomLeft: Props.number |
| 56 | + }, |
| 57 | + |
| 58 | + render: function() { |
| 59 | + var width = this.props.width; |
| 60 | + var height = this.props.height; |
| 61 | + var radius = this.props.radius ? this.props.radius : 0; |
| 62 | + |
| 63 | + // if unspecified, radius(Top|Bottom)(Left|Right) defaults to the radius |
| 64 | + // property |
| 65 | + var tl = this.props.radiusTopLeft ? this.props.radiusTopLeft : radius; |
| 66 | + var tr = this.props.radiusTopRight ? this.props.radiusTopRight : radius; |
| 67 | + var br = this.props.radiusBottomRight ? |
| 68 | + this.props.radiusBottomRight : radius; |
| 69 | + var bl = this.props.radiusBottomLeft ? this.props.radiusBottomLeft : radius; |
| 70 | + |
| 71 | + var path = Path(); |
| 72 | + |
| 73 | + // for negative width/height, offset the rectangle in the negative x/y |
| 74 | + // direction. for negative radius, just default to 0. |
| 75 | + if (width < 0) { |
| 76 | + path.move(width, 0); |
| 77 | + width = -width; |
| 78 | + } |
| 79 | + if (height < 0) { |
| 80 | + path.move(0, height); |
| 81 | + height = -height; |
| 82 | + } |
| 83 | + if (tl < 0) { tl = 0; } |
| 84 | + if (tr < 0) { tr = 0; } |
| 85 | + if (br < 0) { br = 0; } |
| 86 | + if (bl < 0) { bl = 0; } |
| 87 | + |
| 88 | + // disable border radius if it doesn't fit within the specified |
| 89 | + // width/height |
| 90 | + if (tl + tr > width) { tl = 0; tr = 0; } |
| 91 | + if (bl + br > width) { bl = 0; br = 0; } |
| 92 | + if (tl + bl > height) { tl = 0; bl = 0; } |
| 93 | + if (tr + br > height) { tr = 0; br = 0; } |
| 94 | + |
| 95 | + path.move(0, tl); |
| 96 | + |
| 97 | + if (tl > 0) { path.arc(tl, -tl); } |
| 98 | + path.line(width - (tr + tl), 0); |
| 99 | + |
| 100 | + if (tr > 0) { path.arc(tr, tr); } |
| 101 | + path.line(0, height - (tr + br)); |
| 102 | + |
| 103 | + if (br > 0) { path.arc(-br, br); } |
| 104 | + path.line(- width + (br + bl), 0); |
| 105 | + |
| 106 | + if (bl > 0) { path.arc(-bl, -bl); } |
| 107 | + path.line(0, - height + (bl + tl)); |
| 108 | + |
| 109 | + return <Shape {...this.props} d={path} />; |
| 110 | + } |
| 111 | + |
| 112 | +}); |
| 113 | + |
| 114 | +module.exports = Rectangle; |
0 commit comments