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