javascript - 使用 javascript 和数学将 div 元素排列在正方形中

标签 javascript trigonometry

使用 JavaScript 将许多 div 排列成一个圆圈。现在我将每个 div 的尺寸设置为 40×40。以下是我迄今为止能够实现的目标。这就是我找到每个 div 的 X 和 Y 的方法。

x = 100 * Math.cos(angle) + hCenter;
y = 100 * Math.sin(angle) + vCenter;

其中 hCenter 和 vCenter 是屏幕的中心点

Circles

  1. 当有很多圆圈时,它们开始相互重叠。如何 我可以找到每个 div 的高度和宽度,以便它们适合圆形 彼此之间有一点空间。

  2. 如何在正方形中排列相同的圆圈。意味着动画来自 圆变正方形。如何找到每个 div 的新 X,Y 位置。

最佳答案

How can I find the height & width of each div so that they fit in circle with a little space between each other.

每个圆的宽度和高度与其直径相同,直径(加上小地方)等于由它们的位置形成的多边形边的长度。您知道排列的大正方形/圆形的大小(“直径”),因此您可以轻松计算 length of the sides从那个和项目的数量。然后减去一个小的常数或因子,就得到了结果。

How to find new X,Y position of each div so that they are arranged in a square?

从 Angular 计算它们将位于正方形的哪一侧。你已经得到了第一个坐标。然后,使用 sin/cos 计算该侧的位置。

var dir = Math.round(angle / Math.PI * 2) % 4,
    dis = dir<2 ? 100 : -100;
if (dir % 2 == 0) {
    x = hCenter + dis;
    y = vCenter + dis * Math.tan(angle);
} else {
    x = hCenter + dis / Math.tan(angle);
    y = vCenter + dis;
}

关于javascript - 使用 javascript 和数学将 div 元素排列在正方形中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23720712/

相关文章:

javascript - 事件处理程序访问上下文 JavaScript

javascript - 如何设计带有内部 float 的 div 以水平扩展以允许所有内部 float div 适合?

javascript - 更改 Javascript 中的时间格式

math - 知道一个矩形的两个点,我怎样才能找出另外两个?

javascript - 在javascript中在线查找新的点坐标

java - Android、Java 以正弦波移动手榴弹

python - 正切 x 的反正切未精确计算

c# - 数学 - 使用三角函数在单个 View 中显示多个对象

javascript - 无法理解 async/await Nodejs

javascript - React-native 不应该只渲染与 React 工作方式类似的更改组件吗?