前端如何实现图中红框里的形状
SVG应该可以 只是得研究下怎么绘制。之前做过类似的
感觉可以折中一下,绘制成圆环
通过代码方式控制图形绘制
import React from "react";
interface SectorProps {
outerRadius: number;
innerRadius: number;
startAngle: number;
sweepAngle: number;
fillColor?: string;
gradientColors?: string[];
strokeColor?: string;
strokeWidth?: number;
opacity?: number;
}
const Sector: React.FC<SectorProps> = ({
outerRadius,
innerRadius,
startAngle,
sweepAngle,
fillColor = "blue",
gradientColors,
strokeColor = "black",
strokeWidth = 2,
opacity = 1.0,
}) => {
const center = outerRadius;
const gradientId = `gradient-${Math.random().toString(36).substr(2, 9)}`;
const polarToCartesian = (radius: number, angle: number) => {
const radians = ((angle - 90) * Math.PI) / 180;
return {
x: center + radius * Math.cos(radians),
y: center + radius * Math.sin(radians),
};
};
const outerStart = polarToCartesian(outerRadius, startAngle);
const outerEnd = polarToCartesian(outerRadius, startAngle + sweepAngle);
const innerStart = polarToCartesian(innerRadius, startAngle + sweepAngle);
const innerEnd = polarToCartesian(innerRadius, startAngle);
const largeArcFlag = sweepAngle > 180 ? 1 : 0;
const pathData = `M ${outerStart.x} ${outerStart.y} A ${outerRadius} ${outerRadius} 0 ${largeArcFlag} 1 ${outerEnd.x} ${outerEnd.y}
L ${innerStart.x} ${innerStart.y} A ${innerRadius} ${innerRadius} 0 ${largeArcFlag} 0 ${innerEnd.x} ${innerEnd.y} Z`;
return (
<svg width={outerRadius * 2} height={outerRadius * 2} viewBox={`0 0 ${outerRadius * 2} ${outerRadius * 2}`} opacity={opacity}>
{gradientColors && gradientColors.length > 1 && (
<defs>
<linearGradient id={gradientId} gradientUnits="userSpaceOnUse" x1={outerStart.x} y1={outerStart.y} x2={outerEnd.x} y2={outerEnd.y}>
{gradientColors.map((color, index) => (
<stop key={index} offset={`${(index / (gradientColors.length - 1)) * 100}%`} stopColor={color} />
))}
</linearGradient>
</defs>
)}
<path d={pathData} fill={gradientColors && gradientColors.length > 1 ? `url(#${gradientId})` : fillColor} stroke={strokeColor} strokeWidth={strokeWidth} />
</svg>
);
};
export default Sector;
11 回答1.2k 阅读
4 回答920 阅读✓ 已解决
3 回答955 阅读✓ 已解决
2 回答940 阅读✓ 已解决
3 回答886 阅读✓ 已解决
2 回答639 阅读
3 回答946 阅读