你知道如何制作一个x,y坐标数组,然后随机选择一个来放置一个值吗?
<g id="lines">
{data.sites.map((item, i) => {
let findX = (180 + item.attributes.address.Longitude) * (1552 / 360);
let findY = (90 - item.attributes.address.Latitude) * (818/ 180);
let randoXY = findX + ',' findY (and randomly selects one of the X, Y values);
return (
<path key={i + i + '--'} id={'line' + (i + 1)} class="cls-3" d={'M' + findX + ','
+ findY +'L' + randoXY} />
)
})}
</g>
答案 0 :(得分:0)
创建一个包含一些随机点的array
:
let arr = [0,1,2,3,4,5,6,7,8,9];
然后使用Math.random()
选择随机索引,确保索引始终位于array
的长度内(总是将Math.random()
的值乘以数组的长度]它永远不会大于长度)然后使用Math.floor()
将值转换为整数。
使用此选项从数组中选择随机x和y值:
<g id="lines">
{data.sites.map((item, i) => {
let findX = (180 + item.attributes.address.Longitude) * (1552 / 360);
let findY = (90 - item.attributes.address.Latitude) * (818/ 180);
let x = a[Math.floor(Math.random() * 10)];
let y = a[Math.floor(Math.random() * 10)];
return (
<path
key={i + i + '--'}
id={'line' + (i + 1)}
class="cls-3"
d={`M${findX}, ${findY}L${x},${y}`} />
)})
}
</g>