gpt4 book ai didi

javascript - 在 d3 中设置旋转方向

转载 作者:行者123 更新时间:2023-11-29 19:02:17 25 4
gpt4 key购买 nike

我有一个几乎没有可点击点的圆圈。当我单击其中一个时,我希望它旋转到右侧(零度)位置。当我单击顶部的点 (90deg) 时,我希望圆顺时针旋转,当我单击底部的点 (270deg) 时,我希望它逆时针旋转。本质上,走最短路径到达右侧。问题是,当我单击底部点(蓝星)时,圆圈以最长的路径位于右侧。如果你再玩一下,你会发现它似乎为每个点设置了一个特定的方向,无论它们在哪里,它都会遵循这个方向。

请问如何控制旋转方向?对此的任何指示表示赞赏。

function rotate(angle) {
d3.select('.container')
.attr('transform', `rotate(${angle})`);
}
body {
margin: 0 auto;
text-align: center;
padding: 10px;
}

.container {
transform-origin: 250px 250px;
transition: all 0.5s ease-in;
}

.star {
cursor: pointer;
}

.star--red {
fill: red;
}

.star--green {
fill: green;
}

.star--blue {
fill: lightblue;
}

.star--pink {
fill: salmon;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500" viewbox="0 0 500 500">
<g class="container">
<ellipse id="main-circle" stroke-width="2" ry="145" rx="145" cy="250" cx="250" stroke="#000" fill="#fff"/>
<path id="svg_8" class="star star--red" onclick="rotate(90);"
d="m228.500007,135.025599l16.424656,0l5.075345,-15.603354l5.075348,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643555l-13.287804,9.643555l5.075609,-15.603354l-13.287806,-9.643293z"/>
<path id="svg_9" class="star star--green" onclick="rotate(180);"
d="m119.362495,245.178355l16.424656,0l5.075345,-15.603354l5.075347,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643556l-13.287805,9.643556l5.07561,-15.603354l-13.287806,-9.643293z"/>
<path id="svg_10" class="star star--blue" onclick="rotate(270);"
d="m228.500007,355.025599l16.424656,0l5.075345,-15.603354l5.075347,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643556l-13.287805,9.643556l5.07561,-15.603354l-13.287806,-9.643293z"/>
<path id="svg_11" class="star star--pink" onclick="rotate(0);"
d="m337.362495,245.178355l16.424656,0l5.075345,-15.603354l5.075347,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643556l-13.287805,9.643556l5.07561,-15.603354l-13.287806,-9.643293z"/>
</g>
</svg>

本质上,我已经将代码笔代码复制粘贴到此处,但该笔也可在 https://codepen.io/anon/pen/LjMVzM 获得。

最佳答案

您不能仅仅跟踪所需的旋转,您还需要跟踪总旋转。例如,如果您一直单击顶部的任何星星,继续前进所需的度数将变为 [0, 90, 180, 270, 360, 450, ...]。跟踪总旋转后,您需要做的就是确定增量方向,如下所示。

var totAngle = 0;
function rotate(angle) {
d3.select('.container')
.attr('transform', () => {
// Clamp the current rotation to 0-360
var minAngle = ((totAngle % 360) + 360) % 360;
// Calculate the required rotation 0/90/180/270
var rot = (angle - minAngle + 360) % 360;
// Determine the direction clockwise/counter-clockwise
totAngle += rot <= 180 ? rot : -90
return `rotate(${totAngle})`;
});
}
body {
margin: 0 auto;
text-align: center;
padding: 10px;
}

.container {
transform-origin: 250px 250px;
transition: all 0.5s ease-in;
}

.star {
cursor: pointer;
}

.star--red {
fill: red;
}

.star--green {
fill: green;
}

.star--blue {
fill: lightblue;
}

.star--pink {
fill: salmon;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg xmlns="http://www.w3.org/2000/svg" width="500" height="500" viewbox="0 0 500 500">
<g class="container">
<ellipse id="main-circle" stroke-width="2" ry="145" rx="145" cy="250" cx="250" stroke="#000" fill="#fff"/>
<path id="svg_8" class="star star--red" onclick="rotate(90);"
d="m228.500007,135.025599l16.424656,0l5.075345,-15.603354l5.075348,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643555l-13.287804,9.643555l5.075609,-15.603354l-13.287806,-9.643293z"/>
<path id="svg_9" class="star star--green" onclick="rotate(180);" d="m119.362495,245.178355l16.424656,0l5.075345,-15.603354l5.075347,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643556l-13.287805,9.643556l5.07561,-15.603354l-13.287806,-9.643293z"/>
<path id="svg_10" class="star star--blue" onclick="rotate(270);"
d="m228.500007,355.025599l16.424656,0l5.075345,-15.603354l5.075347,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643556l-13.287805,9.643556l5.07561,-15.603354l-13.287806,-9.643293z"/>
<path id="svg_11" class="star star--pink" onclick="rotate(0);"
d="m337.362495,245.178355l16.424656,0l5.075345,-15.603354l5.075347,15.603354l16.424653,0l-13.287801,9.643293l5.075608,15.603354l-13.287807,-9.643556l-13.287805,9.643556l5.07561,-15.603354l-13.287806,-9.643293z"/>
</g>
</svg>

关于javascript - 在 d3 中设置旋转方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45977346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com