谁来帮帮我
为什么画不出圆来,代码一样的呀
为什么画不出圆来,代码一样的呀
2016-12-20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas Clock</title>
</head>
<body>
<div style="margin:100px 100px;">
<canvas id="clock" height="200" width="200"></canvas>
</div>
</body>
<script>
var obj = document.getElementById('clock');
var ctx = obj.getContext('2d');
var width = ctx.canvas.width;
var r = width / 2;
//画圆
function drawBackground() {
ctx.translate(r, r);
ctx.beginPath();
ctx.lineWidth = 10;
//以0,0为原点,r为半径,0为起始角,2*Math.PI为结束角,顺时针画圆
ctx.arc(0, 0, r - 5, 0, 2 * Math.PI, false);
ctx.stroke();
var hourNumber = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2];
ctx.font = '18px Arial';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
//画出1-12的数字
hourNumber.forEach(function (number, i) {
var rad = 2 * Math.PI / 12 * i;
var x = Math.cos(rad) * (r - 30);
var y = Math.sin(rad) * (r - 30);
ctx.fillText(number, x, y);
});
//画出秒针走动的60个点
for (var i = 0; i < 60; i++) {
var rad = 2 * Math.PI / 60 * i;
var x = Math.cos(rad) * (r - 18);
var y = Math.sin(rad) * (r - 18);
ctx.beginPath();
if (i % 5 === 0) {
ctx.fillStyle = '#000';
ctx.arc(x, y, 2, 0, 2, 2 * Math.PI, false);
} else {
ctx.fillStyle = '#ccc';
ctx.arc(x, y, 2, 0, 2, 2 * Math.PI, false);
}
ctx.fill();
}
}
drawBackground();
</script>
举报