用canvas绘制一个太极图案
结果如图:
代码:
<!DOCTYPE html>
<html>
<head>
<title>Canvas tutorial</title>
</head>
<body onload="draw();">
<canvas id="c" width="150" height="150"></canvas>
</body>
<script type="text/javascript">
var deg = Math.PI / 180
function draw() {
var ctx = document.getElementById('c').getContext('2d')
function drawCircle(ctx, type, x, y, r, sa, ea, anti, fStyle) {
if (fStyle) {
ctx.fillStyle = fStyle
}
ctx.beginPath()
ctx.arc(x, y, r, sa, ea, anti)
ctx[type]()
}
// 左边黑鱼
drawCircle(ctx, 'fill', 75, 75, 65, deg * 270, deg * 90, true)
// 外边大圆
drawCircle(ctx, 'stroke', 75, 75, 65, 0, deg * 360, false)
// 两个鱼头
drawCircle(ctx, 'fill', 75, 42.5, 32, deg * 270, deg * 90, true, '#fff')
drawCircle(ctx, 'fill', 75, 107.5, 32, deg * 270, deg * 90, false, '#000')
// 黑白鱼眼睛
drawCircle(ctx, 'fill', 75, 42.5, 5, 0, deg * 360, true, '#000')
drawCircle(ctx, 'fill', 75, 107.5, 5, 0, deg * 360, true, '#fff')
}
</script>
<style type="text/css">
canvas {
border: 1px solid black;
}
</style>
</html>