【发布时间】:2023-02-03 18:37:28
【问题描述】:
const courtStructure = [
{ class: 'legend-rect', shape: 'rect', x: 0, y: TLP + 38, width: 50, height: 8, fill: headerStyle.backgroundColor }, // rectangle behind legend
{ class: 'backboard', shape: 'line', x1: 22, x2: 28, y1: 3.75, y2: 3.75, stroke: hoopColor, opacity: hoopOpacity, strokeWidth: 0.5 }, // backboard
{ class: 'rim', shape: 'circle', r: 0.95, cx: 25, cy: 5.1, opacity: hoopOpacity, fill: hoopColor }, // the circle part of rim
{ class: 'back-iron', shape: 'rect', x: 24.625, y: 3.95, width: 0.75, height: 0.5, stroke: 0, opacity: hoopOpacity, fill: hoopColor } // back iron on rim
];
<rect
className='legend-rect'
x={0}
y={TLP + 38}
width={50}
height={8}
fill={headerStyle.backgroundColor}
/>
...
我们有 courtStructure 对象数组,我们想 .map() 数组并创建描述的 SVG 元素。第一个rect 的示例如上所示。 courtStructure 中每个对象中的键应创建为 SVG 元素上的字段。 shape 确定元素类型。
实现这一目标最困难的部分似乎是:
- 创建正确的元素(矩形、线、圆等)
- 处理不同对象对 SVG 具有不同键/参数的事实
这可能吗?
【问题讨论】:
标签: javascript svg ecmascript-6