canvas绘制地球围绕太阳转、月球围绕地球转

制作整理不易,以上内容均为原创(参考了部分官方文档和老师整理的案例)。如要引用请附上本文链接,如有疑问可以在评论区畅所欲言,作者看到会第一时间回复,欢迎交流!
摘要由CSDN通过智能技术生成

素材(放到img中,与html同级):
太阳

太阳

地球

地球

月亮

月亮

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			body {
				background-color: #99CCFF;
			}

			#canvas {
				background-color: #fff;
				color: rgba(255, 0, 0, 1);
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				margin: auto;

			}
		</style>
	</head>
	<body>
		<canvas id="canvas">
			您的浏览器不支持,canvas标签
		</canvas&g
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,这是一个比较复杂的问题,需要绘制三个不同大小的球体,并模拟它们之间的运动关系。以下是一个基本的实现思路: 1. 创建一个画布,并设置一个黑色背景。 2. 绘制太阳,可以使用 `fillStyle` 属性设置为黄色,并使用 `arc` 方法绘制一个圆形。 3. 绘制地球,可以使用 `fillStyle` 属性设置为蓝色,并使用 `arc` 方法绘制一个圆形。 4. 绘制月球,可以使用 `fillStyle` 属性设置为灰色,并使用 `arc` 方法绘制一个圆形。 5. 计算地球月球的初始位置,并以太阳为中心绘制它们。 6. 创建一个函数,用于更新地球月球的位置,可以使用三角函数计算它们的坐标。 7. 使用 `requestAnimationFrame` 方法循环调用更新函数,实现动态效果。 以下是一个简单的示例代码,可以根据需要进行修改和优化: ```html <canvas id="canvas" width="600" height="600"></canvas> ``` ```javascript const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const sunRadius = 50; const earthRadius = 20; const moonRadius = 10; let earthAngle = 0; let moonAngle = 0; function draw() { ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); // 绘制太阳 ctx.fillStyle = 'yellow'; ctx.beginPath(); ctx.arc(canvas.width / 2, canvas.height / 2, sunRadius, 0, Math.PI * 2); ctx.fill(); // 计算地球坐标 const earthX = canvas.width / 2 + Math.cos(earthAngle) * (sunRadius + 100); const earthY = canvas.height / 2 + Math.sin(earthAngle) * (sunRadius + 100); // 绘制地球 ctx.fillStyle = 'blue'; ctx.beginPath(); ctx.arc(earthX, earthY, earthRadius, 0, Math.PI * 2); ctx.fill(); // 计算月球坐标 const moonX = earthX + Math.cos(moonAngle) * (earthRadius + 50); const moonY = earthY + Math.sin(moonAngle) * (earthRadius + 50); // 绘制月球 ctx.fillStyle = 'gray'; ctx.beginPath(); ctx.arc(moonX, moonY, moonRadius, 0, Math.PI * 2); ctx.fill(); // 更新地球月球角度 earthAngle += 0.01; moonAngle += 0.05; requestAnimationFrame(draw); } draw(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

℡啨天丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值