原理就是每个小圆都在自己围绕一个大圆做旋转, 只要让他们的初始角度不一样组合在一起看上去就像是在运动, 只要一直观察其中一个圆他们都在公转
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
</head>
<body>
This is my HTML page. <br>
<a href="pages">pages</a>
<canvas id="canvas" style="display: bloack; margin: auto; background-color: black;" ></canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = window.innerWidth * 0.9;
var height = 600;
var cx = width / 2;
var cy = height / 2;
var size = 100;
canvas.width = width;
canvas.height = height;
var list = new Array();
drawGrid();
setInterval(function(){
clear();
list.forEach(function(c){
c.draw();
});
}, 50);
function clear(){
ctx.beginPath();
ctx.clearRect(0, 0, width, height);
}
function ArcClass(){
this.angle = -90;
this.radian = 5;
this.cx = 0;
this.cy = 0;
this.x = 0;
this.y = 0;
this.r = 8;
this.size = 20;
}
ArcClass.prototype.setAngle = function(angle){
this.angle += angle;
if(this.angle >= 360 - 90)
this.angle = -90;
};
ArcClass.prototype.draw = function(){
var radian = this.angle * Math.PI / 180;
this.x = Math.cos(radian) * this.size + this.cx;
this.y = Math.sin(radian) * this.size + this.cy;
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2);
ctx.fillStyle = "white";
ctx.fill();
ctx.closePath();
this.setAngle(this.radian);
};
function drawGrid(){
var sx = 200;
var sy = 100;
var x = 0;
var y = 0;
var c = null;
for(var i = 0 ; i <= 20 ; i++){
for(var j = 0 ; j <= 20 ; j++){
x = sx + i * 20;
y = sy + j * 20;
c = new ArcClass();
list.push(c);
c.cx = x;
c.cy = y;
c.angle += i * 18;
/*
ctx.beginPath();
ctx.arc(x, y, 20, 0, Math.PI * 2);
ctx.strokeStyle = "white";
ctx.stroke();
ctx.closePath();
*/
}
}
}
</script>
</html>