gpt4 book ai didi

javascript - 使用 javascript 或 jQuery 在另一个图像的中心旋转一个图像

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:53 25 4
gpt4 key购买 nike

Earth Rotation

您好,我是 Javascript 和 Jquery 的新手。我想围绕太阳(图像)移动地球(图像),但我不想使用任何 jQuery 插件。我尝试了几个选项,但它不起作用。所以如果有的话(我猜是)然后请回答。提前致谢

<html>
<head>
<style type="text/css">
body {
background-color: ivory;
}

canvas {
position:absolute;
left:15%;
top:5%;
margin-left:center;
margin-top:center;
background-color:black;
border:1px solid red;
}

#Aditya{
position:relative;
top:25%;
left:20%;

}
</style>
<script type="text/javascript" src="js/Jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){

var canvas = document.getElementById("icanvas");
var ctx = canvas.getContext("2d");

var radianAngle = 0;
var cx = 400;
var cy = 400;
var radius = 230;

var img = document.getElementById("myearth");
img.height="5px";
img.width="5px";
img.onload = start;

function start() {
animate();
}

function animate() {
requestAnimationFrame(animate);

// Drawing code goes here
radianAngle +=Math.PI / 120;
ctx.clearRect(0, 0, canvas.width, canvas.height);

// draw the image rotated around the circumference
ctx.save();
ctx.translate(cx, cy);
ctx.rotate(radianAngle);
ctx.drawImage(img, radius - img.width / 2, -img.height/2);
ctx.restore();
}
});
</script>

</head>
<body>
<canvas id="icanvas" width="800px" height="800px">
<img src="earth.jpg" alt="earth" id="myearth" width="50%" height="50%"></img>
<img src="sun.jpg" alt="Sun" id="Aditya"></img>
</canvas>
</canvas>
</body>

抱歉延迟上传代码。只有地球正在移动到 Canvas 中。不知道如何将太阳放在中心。

最佳答案

谢谢大家表现出兴趣。但我终于做到了。也许你想看看。

<html>
<head>
<style type="text/css">
body {
background-color: ivory;
}

canvas {
position:absolute;
left:15%;
top:5%;
margin-left:center;
margin-top:center;
background-color:black;
border:1px solid red;
}

.sunimg{
position:absolute;
left:40%;
top:40%;


}
</style>
<script type="text/javascript" src="js/Jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#Aditya").addClass("sunimg");
var canvas = document.getElementById("icanvas");
var ctx = canvas.getContext("2d");

var radianAngle = 0;
var cx = 400;
var cy = 400;
var radius = 230;

var img = document.getElementById("myearth");
img.height="5px";
img.width="5px";
img.onload = start;




function start() {
animate();
}

function animate() {
requestAnimationFrame(animate);

// Drawing code goes here
radianAngle +=Math.PI / 120;
ctx.clearRect(0, 0, canvas.width, canvas.height);

// draw the image rotated around the circumference
ctx.save();
ctx.translate(cx, cy);
ctx.rotate(radianAngle);
ctx.drawImage(img, radius - img.width / 2, -img.height/2);
ctx.restore();
}
});
</script>

</head>
<body>
<canvas id="icanvas" width="800px" height="800px">
<img src="earth.jpg" alt="earth" id="myearth" width="50%" height="50%"></img>

</canvas>
<img src="sun.jpg" alt="Sun" id="Aditya"></img>
</body>

关于javascript - 使用 javascript 或 jQuery 在另一个图像的中心旋转一个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22989950/

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