如何在React Native ART中绘制圆角矩形

时间:2019-06-28 11:08:21

标签: javascript reactjs react-native d3.js svg

我正在使用react-native ART库在移动应用程序中绘制可缩放的矢量元素,ART是一个完美的库,因为它可以使用自己的工具轻松地进行动画处理和变形。但是ART没有像SVG的CircleRect蚀刻这样的自定义元素,ART只有一种叫做Shape的类型,并且功能强大,几乎可以创建任何形状。但是我很难用Shape绘制可缩放的圆角矩形。

import React from 'react'
import PropTypes from 'prop-types'
import {ART} from 'react-native'

const {Group, Shape} = ART

export default class Graphics extends React.Component{
   render(){
      const {x, y, width, height, fill, stroke} = this.props;
      const d = `M0,0L${width},0L${width},${height}L0,${height}L0,0z`
      return(
        <Group x={x} y={y}>
           <Path d={d} fill={fill} stroke={stroke}>
        </Group>
      )
   }
}

如您所见,我创建了具有给定宽度和高度的矩形形状,但是我不知道如何生成圆角。

我不知道d3是否可以通过任一d3来做到这一点?

1 个答案:

答案 0 :(得分:2)

您可以使用ART Path对象与路径curvecurveTo方法或arc arcTo方法一起创建路径。请检查下面的示例Rect组件。

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

在这里,您具有完全可扩展的独立的四角圆形支持矩形组件。

  • 道具widthheight-通常不会矫正任何圆角。
  • 道具widthheightrc-会为您提供等角四舍五入的功能。
  • 道具widthheighttopRightRadius(或其他任何一个角)-将为您提供每个给定的绕角。

请检查此gist的完整用法。enter image description here