[英]React konva applies x changing only for 1 rectangle when rendering an array
我有一个这样的对象数组: { id, width, height, margin }
。 当我渲染该数组并将该信息应用于反应像这样的konva Rect组件时:
{
topSide.map((seat, index) => (
<Rect
id={seat.id}
key={seat.id}
fill="brown"
stroke="white"
width={seat.width}
height={seat.height}
x={topSideStartPosition + index * seat.width + seat.margin}
y={y * linesHeight - seat.height - 2}
/>
))
}
所以我的问题是seat.margin
仅适用于1个矩形。 通常我想为第一个矩形应用一个值,为其余矩形应用另一个值。 如果数组中的对象索引为1, seat.margin
等于1;如果对象索引不等于1, seat.margin
等于2。这是它的外观: 结果图像
我认为您可能需要计算每个座位的偏移量,并使用先前的偏移量为下一个座位计算新的偏移量。
// somewhere in the top of render function
// probably need to adjust for the first seat
let offset = 0;
// in component:
topSide.map((seat, index) => {
if (index === 1) {
offset += seat.width + seat.margin
} else {
offset += seat.width
}
return <Rect
id={seat.id}
key={seat.id}
fill="brown"
stroke="white"
width={seat.width}
height={seat.height}
x={offset}
y={y * linesHeight - seat.height - 2}
/>;
});
因此,当我尝试通过添加边距移动矩形时,它没有用。 因此,设置offsetX
并删除seat.margin
,在这种情况下对我有帮助。
回答我的问题:
topSide.map((seat, index) => (
<Rect
id={seat.id}
key={seat.id}
fill="brown"
width={seat.width}
height={seat.height}
offsetX={-2 * index}
x={topSideStartPosition + index * seat.width}
y={y * linesHeight - seat.height - 2}
/>
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.