效果图: 方法一: 复制代码代码如下: your browser does not support the canvas tag <br /> var deg = 0; <br /> var r = 30; <br /> var rl = 100; <br /> function drawTaiji() { <br /> var canvas = document.getElementById('myCanvas'); <br /> var context = canvas.getContext('2d'); <br /> var colorA = "rgb(0, 0, 0)"; <br /> var colorB = "red"; <br /> <br /> var px =Math.sin(deg)*r; <br /> var py =Math.cos(deg)*r; <br /> context.clearRect(0, 0, 300, 300); <br /> context.beginPath(); <br /> context.fillStyle = colorA; <br /> context.arc(rl, rl, 60, 0.5 * Math.PI +deg, 1.5 * Math.PI +deg, true); <br /> context.closePath(); <br /> context.fill(); <br /> context.fillStyle = colorB; <br /> context.beginPath(); <br /> context.arc(rl, rl, 60, 1.5* Math.PI +deg, 0.5 * Math.PI +deg, true); <br /> context.closePath(); <br /> context.fill(); <br /> context.fillStyle = colorB; <br /> context.beginPath(); <br /> context.arc(rl+px, rl-py, 30, 0.5 * Math.PI + deg, 1.5 * Math.PI + deg, true); <br /> context.closePath(); <br /> context.fill(); <br /> context.fillStyle = colorA; <br /> context.beginPath(); <br /> context.arc(rl-px, rl+py, 30, 1.5 * Math.PI + deg, 0.5 * Math.PI + deg, true); <br /> context.closePath(); <br /> context.fill(); <br /> context.fillStyle = colorA; <br /> context.beginPath(); <br /> context.arc(rl+px, rl-py, 8, 0, 2 * Math.PI, true); <br /> context.closePath(); <br /> context.fill(); <br /> context.fillStyle = colorB; <br /> context.beginPath(); <br /> context.arc(rl-px, rl+py, 8, 0, 2 * Math.PI, true); <br /> context.closePath(); <br /> context.fill(); <br /> deg +=0.1; <br /> } <br /> setInterval(drawTaiji, 100);<br /> 方法二: 复制代码代码如下: your browser does not support the canvas tag <br /> var canvas = document.getElementById('myCanvas'); <br /> var ctx = canvas.getContext("2d"); <br /> var angle = 0; <br /> var count = 360; <br /> var clrA = '#000'; <br /> var clrB = 'red'; <br /> <br /> function taiji(x, y, radius, angle, wise) { <br /> angleangle = angle || 0; <br /> wisewise = wise ? 1 : -1; <br /> ctx.save(); <br /> ctx.translate(x, y); <br /> ctx.rotate(angle); <br /> ctx.fillStyle = clrA; <br /> ctx.beginPath(); <br /> ctx.arc(0, 0, radius, 0, Math.PI, true); <br /> ctx.fill(); <br /> ctx.beginPath(); <br /> ctx.fillStyle = clrB; <br /> ctx.arc(0, 0, radius, 0, Math.PI, false); <br /> ctx.fill(); <br /> ctx.fillStyle = clrB; <br /> ctx.beginPath(); <br /> ctx.arc(wise * -0.5 * radius, 0, radius / 2, 0, Math.PI * 2, true); <br /> ctx.fill(); <br /> ctx.beginPath(); <br /> ctx.fillStyle = clrA; <br /> ctx.arc(wise * +0.5 * radius, 0, radius / 2, 0, Math.PI * 2, false); <br /> ctx.arc(wise * -0.5 * radius, 0, radius / 10, 0, Math.PI * 2, true); <br /> ctx.fill(); <br /> ctx.beginPath(); <br /> ctx.fillStyle = clrB; <br /> ctx.arc(wise * +0.5 * radius, 0, radius / 10, 0, Math.PI * 2, true); <br /> ctx.fill(); <br /> ctx.restore(); <br /> } <br /> <br /> loop = setInterval(function () { <br /> beginTag = true; <br /> ctx.clearRect(0, 0, canvas.width, canvas.height); <br /> taiji(200, 200, 50, Math.PI * (angle / count) * 2, true); <br /> //taiji(350, 350, 50, Math.PI * ((count - angle) / count) * 2, false); <br /> angle = (angle + 5) % count; <br /> }, 50); <br />