Skip to content

Instantly share code, notes, and snippets.

@kamaals
Created June 28, 2019 11:45
Show Gist options
  • Select an option

  • Save kamaals/2e9be46f703612e0420b883d89ad0fba to your computer and use it in GitHub Desktop.

Select an option

Save kamaals/2e9be46f703612e0420b883d89ad0fba to your computer and use it in GitHub Desktop.
Drawing rounded cornered rectangle in react-native ART.
import React from 'react'
import { View, ART } from 'react-native'
import Rect from './Rect'
const {Surface} = ART
function App(){
return(
<View style={{backgroundColor: 'rgba(37, 72, 101, 1.0)'}}>
<Surface width={300} height={300}>
<Rect width={100} height={100} fill={'yellow'} stroke={'green'} x={10} y={20} bottomRightRadius={100} bottomLeftRadius={20} topRightRadius={10}/>
<Rect strokeDash={[0,4]} width={20} height={100} fill={'rgba(255,0,0, 0.09)'} stroke={'red'} x={10} y={130} rc={50}/>
</Surface>
</View>
)
}
import React from 'react'
import PropTypes from 'prop-types'
import {ART} from 'react-native'
const {Group, Shape, Path} = ART
function Rect({fill, stroke, x, width, y, rc, height, topLeftRadius, topRightRadius, bottomRightRadius, bottomLeftRadius, ...rest}){
const startX = 0;
const startY = 0;
const smallDimension = width > height ? height : width;
let tlr = topLeftRadius !== null ? topLeftRadius : rc; tlr = tlr > smallDimension/2 ? smallDimension /2 : tlr;
let trr = topRightRadius !== null ? topRightRadius : rc; trr = trr > smallDimension/2 ? smallDimension /2 : trr;
let brr = bottomRightRadius !== null ? bottomRightRadius : rc; brr = brr > smallDimension/2 ? smallDimension /2 : brr;
let blr = bottomLeftRadius !== null ? bottomLeftRadius : rc; blr = blr > smallDimension/2 ? smallDimension /2 : blr;
const d = Path()
.move(startX, startY)
.move(startX, tlr)
.arc( tlr, startY-tlr, tlr, tlr, false, false) // top left
.lineTo(width - trr, startY)
.arc( trr, startX+trr, trr, trr, false, false) // top right
.lineTo(width, startY+ (height - brr))
.arc(startX-brr, brr, brr, brr, false, false) // bottom right
.lineTo(startX + blr, height)
.arc(startX-blr, startY-blr, blr, blr, false, false) // bottom right
.close()
return(
<Group x={x} y={y}>
<Shape {...rest} fill={fill} stroke={stroke} d={d}/>
</Group>
)
}
Rect.propTypes = {
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
x: PropTypes.number,
y: PropTypes.number,
fill: PropTypes.string,
stroke: PropTypes.string,
topLeftRadius: PropTypes.number,
topRightRadius: PropTypes.number,
bottomRightRadius: PropTypes.number,
bottomLeftRadius: PropTypes.number,
rc: PropTypes.number
}
Rect.defaultProps = {
x: 0,
y: 0,
fill: 'transparent',
stroke: 'red',
topLeftRadius: null,
topRightRadius: null,
bottomRightRadius: null,
bottomLeftRadius: null,
rc: 0
}
export default Rect
@kamaals
Copy link
Author

kamaals commented Jun 28, 2019

image
React native ios device. rounded corner rectangles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment