0

地球自转

您好,我是 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>

抱歉延迟上传代码。只有地球正在进入画布。不知道如何将太阳放在中心。

4

1 回答 1

1

谢谢大家的关注。但我终于做到了。也许你想看看。

<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>

于 2014-04-11T13:39:49.493 回答