gpt4 book ai didi

javascript - 在 html/js 中旋转嵌入的 svg 对象

转载 作者:行者123 更新时间:2023-12-02 21:24:07 24 4
gpt4 key购买 nike

我正在尝试创建一个单手时钟,用作基于 HTML 的桌面壁纸。

我已将手的 SVG 嵌入到 HTML 中,并试图让手引用时间旋转,这就是我陷入困境的地方......

这是我目前所在的位置:

<!DOCTYPE html>
<html>
<body>

<svg width="100%" height="100%" viewBox="0 0 2560 1440" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<!-- <rect x="0" y="0" width="2560" height="1440"/> -->
<path id="Hand" d="M1284.85,707.293C1289.96,709.248 1293.6,714.203 1293.6,720C1293.6,725.722 1290.06,730.623 1285.05,732.63L1285.05,865.05L1281.8,1215.35L1278.2,1215.35L1274.95,865.05L1274.95,732.63C1269.94,730.623 1266.4,725.722 1266.4,720C1266.4,714.203 1270.04,709.248 1275.15,707.293L1276.4,626.7L1283.6,626.7L1284.85,707.293Z" style="fill:#ffd000;"/>
</svg>

<script>
var now = new Date();
var hour = now.getHours();
var minute = now.getMinutes();
var second = now.getSeconds();

hour = hour%24;
hour = (hour*Math.PI/12)+(minute*Math.PI/(12*60))+(second*Math.PI/(12*60*60));

setInterval(drawClock, 1000);

function drawClock() {
var vector = document.getElementById("Hand")
vector.setAttribute("transform", "rotate("hour", 1280, 720)");
}
</script>


</html>
</body>

任何帮助或指示将不胜感激!

最佳答案

你应该使用CSS transform,使其工作的关键成分是transform-origin 这个CSS属性允许你说,元素应该旋转手的根部而不是中部。

function moveHand() {
// Create time-related variables.
const now = new Date();

/** Divide current hour by hours in day,
* then multiply by 360 to get it to circle and
* offset by 180 to have 0 on top
*/
document.querySelector("#hours").style.transform = `rotate(${now.getHours() / 24 * 360 - 180}deg)`;
// Same for minutes and seconds
document.querySelector("#minutes").style.transform = `rotate(${now.getMinutes() / 60 * 360 - 180}deg)`;
document.querySelector("#seconds").style.transform = `rotate(${now.getSeconds() / 60 * 360 - 180}deg)`;
;

requestAnimationFrame(moveHand)
}

// Use `requestAnimationFrame` instead of interval, it is the way to schedule render tasks
requestAnimationFrame(moveHand);
.hand {
transform-origin: 50% 50%;
}
<svg id="clock" width="100%" height="100%" viewBox="0 0 2560 1440" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<!-- <rect x="0" y="0" width="2560" height="1440"/> -->
<path class="hand" id="seconds" d="M1284.85,707.293C1289.96,709.248 1293.6,714.203 1293.6,720C1293.6,725.722 1290.06,730.623 1285.05,732.63L1285.05,865.05L1281.8,1215.35L1278.2,1215.35L1274.95,865.05L1274.95,732.63C1269.94,730.623 1266.4,725.722 1266.4,720C1266.4,714.203 1270.04,709.248 1275.15,707.293L1276.4,626.7L1283.6,626.7L1284.85,707.293Z" style="fill:#ffd000;" />
<path class="hand" id="minutes" d="M1284.85,707.293C1289.96,709.248 1293.6,714.203 1293.6,720C1293.6,725.722 1290.06,730.623 1285.05,732.63L1285.05,865.05L1281.8,1215.35L1278.2,1215.35L1274.95,865.05L1274.95,732.63C1269.94,730.623 1266.4,725.722 1266.4,720C1266.4,714.203 1270.04,709.248 1275.15,707.293L1276.4,626.7L1283.6,626.7L1284.85,707.293Z" style="fill:#d000ff;" />
<path class="hand" id="hours" d="M1284.85,707.293C1289.96,709.248 1293.6,714.203 1293.6,720C1293.6,725.722 1290.06,730.623 1285.05,732.63L1285.05,865.05L1281.8,1215.35L1278.2,1215.35L1274.95,865.05L1274.95,732.63C1269.94,730.623 1266.4,725.722 1266.4,720C1266.4,714.203 1270.04,709.248 1275.15,707.293L1276.4,626.7L1283.6,626.7L1284.85,707.293Z" style="fill:#d0ff00;" />
</svg>

关于javascript - 在 html/js 中旋转嵌入的 svg 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60782926/

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