【发布时间】:2014-11-11 20:32:51
【问题描述】:
我需要画一个有 7 个分区的圆。
我已经完成了这段代码:
HTML
<body style="background:#F6A631;">
<canvas id="wheelcanvas" width="730" height="730"></canvas>
</body>
JS
var services = ["paragraphs", "paragraphs", "paragraphs", "paragraphs", "paragraphs", "paragraphs", "paragraphs"];
function serviceCircle() {
var canvas = document.getElementById("wheelcanvas");
if (canvas.getContext) {
var outsideRadius = 360;
var textRadius = 280;
var insideRadius = 220;
ctx = canvas.getContext("2d");
ctx.clearRect(0,0,720,720); // 720 - 360 - 180 (500 - 250)
ctx.strokeStyle = "#fff";
ctx.lineWidth = 5;
ctx.font = 'bold 18px Tahoma';
for(var i = 0; i < 7; i++) {
var angle = startAngle + i * arc;
ctx.fillStyle = "#ffc000";
ctx.translate(360 + Math.cos(angle + arc / 2) * textRadius, 360 + Math.sin(angle + arc / 2) * textRadius);
// ctx.rotate(angle + arc / 2 + Math.PI / 2);
var text = services[i];
ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
ctx.restore();
}
//Arrow
ctx.fillStyle = "#fff";
ctx.beginPath();
ctx.moveTo(250 - 4, 250 - (outsideRadius + 5));
ctx.lineTo(250 + 4, 250 - (outsideRadius + 5));
ctx.lineTo(250 + 4, 250 - (outsideRadius - 5));
ctx.lineTo(250 + 9, 250 - (outsideRadius - 5));
ctx.lineTo(250 + 0, 250 - (outsideRadius - 13));
ctx.lineTo(250 - 9, 250 - (outsideRadius - 5));
ctx.lineTo(250 - 4, 250 - (outsideRadius - 5));
ctx.lineTo(250 - 4, 250 - (outsideRadius + 5));
ctx.fill();
}
}
draw();
我的结果 - Image 我需要这个。将鼠标悬停在分区上并更改其背景。
不知道如何解决。
【问题讨论】:
-
我不确定您需要帮助哪些部分——我猜是它的自动换行。首先使用
var words=services[0].split(' ')将每个标签分成单独的单词。然后使用context.measureText得到每个单词的宽度。然后,您可以对这些单词进行自动换行以适应分区。你会发现context.textAlign='center'有助于将你的包装词居中。 -
文本问题已解决。我需要带有 7 个带边框的分区和悬停更改分区背景的圆圈。
标签: javascript html canvas