import React, { ForwardedRef, forwardRef, useImperativeHandle, useState, useMemo } from "react"; import "./index.less"; const segment = 30; const sectorAngle = Math.PI / 3; const ringNum = 3; const ringWidth = 1; const getCircleRadianPoint = (x, y, radius, radian) => [x + Math.cos(radian) * radius, y + Math.sin(radian) * radius]; interface ComponentStyle { mainColor?: string; subColor?: string; } export interface ComponentProps { style?: ComponentStyle; base?: { width?: number, height?: number, id: string } } export interface ComponentRef { updateConfig: (config: ComponentProps) => void; } const Index = forwardRef((props: ComponentProps, ref: ForwardedRef) => { const { style: defStyle, base: defBase } = props; const gId = `decoration-11-g-${defBase.id}`; const gradientId = `decoration-11-gradient-${defBase.id}`; const [style, setStyle] = useState({ ...defStyle }); const [width, setWidth] = useState(defBase?.width || 200); const [height, setHeight] = useState(defBase?.height || 200); const { mainColor = "#83BFF6", subColor = "#00CED1" } = style || {}; useImperativeHandle(ref, () => ({ updateConfig: (newConfig) => { setStyle({ ...newConfig.style }); setWidth(newConfig.base.width); setHeight(newConfig.base.height); }, })); const { x, y } = useMemo(() => ({ x: width / 2, y: height / 2 }), [width, height]); const pathD = useMemo(() => { const startAngle = -Math.PI / 2; const angleGap = sectorAngle / segment; const r = width / 4; let lastEndPoints = getCircleRadianPoint(x, y, r, startAngle); return new Array(segment) .fill('') .map((_, i) => { const endPoints = getCircleRadianPoint(x, y, r, startAngle - (i + 1) * angleGap).map(_ => _.toFixed(5)); const d = `M${lastEndPoints.join(',')} A${r}, ${r} 0 0 0 ${endPoints.join(',')}`; lastEndPoints = endPoints; return d; }); }, [x, y]); const circleR = useMemo(() => { const radiusGap = (width / 2 - ringWidth / 2) / ringNum; return new Array(ringNum).fill(0).map((_, i) => radiusGap * (i + 1)); }, [width]); const splitLinePoints = useMemo(() => { const angleGap = Math.PI / 6; const r = width / 2; return new Array(6).fill('').map((_, i) => { const startAngle = angleGap * (i + 1); const endAngle = startAngle + Math.PI; const startPoint = getCircleRadianPoint(x, y, r, startAngle); const endPoint = getCircleRadianPoint(x, y, r, endAngle); return `${startPoint.join(',')} ${endPoint.join(',')}`; }); }, [x, y]); const arcD = useMemo(() => { const angleGap = Math.PI / 6; const r = width / 2 - 1; return new Array(4).fill('').map((_, i) => { const startAngle = angleGap * (3 * i + 1); const endAngle = startAngle + angleGap; const startPoint = getCircleRadianPoint(x, y, r, startAngle); const endPoint = getCircleRadianPoint(x, y, r, endAngle); return `M${startPoint.join(',')} A${x}, ${y} 0 0 1 ${endPoint.join(',')}`; }); }, [x, y]); return
{pathD.map((d, i) => )} {circleR.map(r => )} {splitLinePoints.map(p => )} {arcD.map(d => )}
; }); export default Index;