gpt4 book ai didi

javascript - React Native ART中如何绘制圆 Angular 矩形

转载 作者:行者123 更新时间:2023-11-30 13:58:16 25 4
gpt4 key购买 nike

我正在使用 react-native ART 库在我的移动应用程序中绘制可缩放的矢量元素,ART 是完美的库,因为它可以使用自己的工具轻松制作动画和变形。但是 ART 没有像 SVG 的 CircleRect etch... 这样的自定义元素,ART 只有一种类型叫做 Shape 并且强大到足以创建几乎任何形状。但是我在使用 Shape 绘制可缩放的圆 Angular 矩形时遇到了困难。

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>
)
}
}

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

我不知道 d3 是否可以用任何一个 d3 来做到这一点?

最佳答案

您可以使用 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

这里有完全可扩展的独立圆 Angular 支持矩形组件。

  • Props width & height - 将正常 rect 完全没有圆 Angular 。
  • Props widthheightrc - 将为您提供所有 Angular 均等的圆 Angular 。
  • Props widthheighttopRightRadius(或任何其他 Angular )- 将为您提供每个给定的圆 Angular 。

请检查这个gist完整使用。 enter image description here

关于javascript - React Native ART中如何绘制圆 Angular 矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com