【发布时间】:2021-08-10 03:38:43
【问题描述】:
- 我正在尝试围绕红色圆圈移动蓝色圆圈并仅使用红色圆圈在画布上绘图。
问题:
-
我无法理解如何在蓝色圆圈围绕红色圆圈旋转并同时移动时保持两个圆圈的中心。
-
当它围绕红色圆圈旋转时,如何只使用红色圆圈而不绘制蓝色圆圈。
const canvas = document.getElementById('canvas1');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let positionX = 100;
let positionY = 100;
let X = 50;
let Y = 50;
let angle = 0;
function circle(){
ctx.fillStyle = 'red';
ctx.beginPath();
ctx.arc(X, Y, 20, 0, Math.PI*2);
ctx.closePath();
ctx.fill();
}
function direction(){
ctx.fillStyle = 'blue';
ctx.beginPath();
ctx.arc(positionX, positionY, 10, 0, Math.PI*2);
ctx.closePath();
ctx.fill();
}
function animate(){
ctx.clearRect(0,0, canvas.width, canvas.height);
circle();
direction();
positionX += 0.5 * Math.sin(angle);
positionY += 0.5 * Math.cos(angle);
angle += 0.1;
requestAnimationFrame(animate);
}
animate();
#canvas1{
position: absolute;
top:0;
left: 0;
width: 100%;
height: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Canvas basics</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas1"></canvas>
<script src="script.js"></script>
</body>
</html>
【问题讨论】:
-
如果你有兴趣查看我的repo,你可以使用它并使用renatureWorking exemple of the repo调整它来制作太阳系
标签: javascript html css animation dom