用 canvas 画布绘制出一个会旋转的太极图
方法一:
运用css动画来实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态版太极图</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width:600px;
height:600px;
background-color:#C1C1C1;
text-align:center;
margin:0 auto;
margin-top: 50px;
}
#canvas{
margin-top:100px;
animation:anima 2s infinite linear;
}
@keyframes anima {
0% {
transform:rotate(0deg);
}
100% {
transform:rotate(360deg);
}
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
//先画一个大圆
context.beginPath();
context.arc(200,200,200,0,2*Math.PI,false);
context.closePath();
context.fillStyle = "#fff";
context.lineWidth = 1;
context.fill();
//画左半边的半圆(从0.5*Math.PI-1.5*Math.PI),颜色填充为黑色
context.beginPath();
context.arc(200,200,200,0.5*Math.PI,1.5*Math.PI,false);
context.closePath();
context.fillStyle = "#000";
context.fill();
//画左半边上面的半圆(从0.5*Math.PI-1.5*Math.PI),颜色填充为白色
context.beginPath();
context.arc(200,100,100,0.5*Math.PI,1.5*Math.PI,false);
context.closePath();
context.fillStyle = "#fff";
context.fill();
//画右半边下面的半圆(从0.5*Math.PI-1.5*Math.PI),颜色填充为黑色
context.beginPath();
context.arc(200,300,100,0.5*Math.PI,1.5*Math.PI,true);
context.closePath();
context.fillStyle = "#000";
context.fill();
//在上面的半圆内画一个比半圆小的整圆,颜色填充为黑色
context.beginPath();
context.arc(200,100,30,0,2*Math.PI,false);
context.closePath();
context.fillStyle = "#000";
context.fill();
//在下面的半圆内画一个比半圆小的整圆,颜色填充为白色
context.beginPath();
context.arc(200,300,30,0,2*Math.PI,false);
context.closePath();
context.fillStyle = "#fff";
context.fill();
}
</script>
</head>
<body>
<div>
<canvas id="canvas" width="400px" height="400px"></canvas>
</div>
</body>
</html>
方法二
运用 js 里的 setInterval()
间歇调用来动画实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>动态版太极图</title>
<style>
*{
margin: 0;
padding: 0;
}
div{
width:600px;
height:600px;
background-color:#C1C1C1;
text-align:center;
margin:0 auto;
margin-top: 50px;
}
#canvas{
margin-top:100px;
}
</style>
<script>
window.onload = function(){
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var q =0;
var pointX = 0;
var pointY = 0;
setInterval(function(){
canvas.width =400;
canvas.height = 400;
context.translate(200,200);
q += Math.PI/180;
context.rotate(q);
//左半边的半圆
context.beginPath();
context.arc(0,0,200,Math.PI*0.5,Math.PI*1.5, false);
context.closePath();
context.fillStyle = 'white';
context.fill();
//右半边的半圆
context.beginPath();
context.arc(0,0,200,Math.PI*0.5,Math.PI*1.5,true);
context.closePath();
context.fillStyle = 'black';
context.fill();
//左半边上面的半圆
context.beginPath();
context.arc(0,-100,100,0,Math.PI*2,true);
context.closePath();
context.fillStyle = 'black';
context.fill();
//右半边上面的半圆
context.beginPath();
context.arc(0,100,100,0,Math.PI*2,true);
context.closePath();
context.fillStyle = 'white';
context.fill();
//在上面的半圆内画一个比半圆小的整圆,颜色填充为白色
context.beginPath();
context.arc(0,-100,30,0,Math.PI*2,true);
context.closePath();
context.fillStyle = 'white';
context.fill();
//在下面的半圆内画一个比半圆小的整圆,颜色填充为黑色
context.beginPath();
context.arc(0,100,30,0,Math.PI*2,true);
context.closePath();
context.fillStyle = 'black';
context.fill();
},5);
}
</script>
</head>
<body>
<div>
<canvas id="canvas" width="400px" height="400px"></canvas>
</div>
</body>
</html>